pytorch/c10/macros/cmake_configure_file.bzl
mikey dagitses ba8dd48d8a use cross-platform sed command in cmake_configure_file (#73589)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73589

Long-form flags do not work and neither does the \w character class.

Test Plan: Imported from OSS

Reviewed By: malfet

Differential Revision: D34558345

Pulled By: dagitses

fbshipit-source-id: 73e1b18bdd55d67fd3936428400c3835684549b0
(cherry picked from commit fc85796fadb8dac0043ba0bf43fd54cf817665b7)
2022-03-02 12:28:51 +00:00

66 lines
2.1 KiB
Python

# Forked from header_template_rule. header_template_rule is not
# compatible with our usage of select because its substitutions
# attribute is a dict, and dicts may not be appended with select. We
# get around this limitation by using a list as our substitutions.
def _cmake_configure_file_impl(ctx):
command = ["cat $1"]
for definition in ctx.attr.definitions:
command.append(
"| sed 's@#cmakedefine {}@#define {}@'".format(
definition,
definition,
),
)
# Replace any that remain with /* #undef FOO */.
command.append("| sed -r 's@#cmakedefine ([A-Z0-9_]+)@/* #undef \\1 */@'")
command.append("> $2")
ctx.actions.run_shell(
inputs = [ctx.file.src],
outputs = [ctx.outputs.out],
command = " ".join(command),
arguments = [
ctx.file.src.path,
ctx.outputs.out.path,
],
)
return [
# create a provider which says that this
# out file should be made available as a header
CcInfo(compilation_context = cc_common.create_compilation_context(
# pass out the include path for finding this header
includes = depset([ctx.outputs.out.dirname, ctx.bin_dir.path]),
# and the actual header here.
headers = depset([ctx.outputs.out]),
)),
]
cmake_configure_file = rule(
implementation = _cmake_configure_file_impl,
doc = """
Mimics CMake's configure_file in Bazel.
Args:
name: A unique name for this rule.
src: The input file template.
out: The generated output.
definitions: A mapping of identifier in template to its value.
""",
attrs = {
# We use attr.string_list for compatibility with select and
# config_setting. See the comment above _cmake_configure_file_impl
# for more information.
"definitions": attr.string_list(mandatory = True),
"out": attr.output(mandatory = True),
"src": attr.label(
mandatory = True,
allow_single_file = True,
),
},
# output_to_genfiles is required for header files.
output_to_genfiles = True,
)