Skip to content

Add new cc_preprocess rule for preprocessing arbitrary files #844

Open

Description

@backtr4ce

Add a simple cc_preprocess rule that invokes the configured C/C++ compiler's preprocessor on an arbitrary input file.

This would be useful for generating files such as linker scripts, preprocessing Microsoft assembly files, or using the C preprocessor as a lightweight template engine. Using the configured C/C++ toolchain would also avoid having to manually determine the compiler-specific preprocessing flags.

Usage could look something like this:

load("@rules_cc//cc:defs.bzl", "cc_preprocess")

cc_preprocess(
    name = "linker_script",
    input = "linker.ld.in",
    output = "linker.ld",
    copts = [],
    defines = [
        "HIGHERHALF_ADDRESS=0xFFFFFFFF80000000",
    ],
)

defines could alternatively be represented as a dictionary:

defines = {
    "HIGHERHALF_ADDRESS": "0xFFFFFFFF80000000",
}

However, an array such as ["HIGHERHALF_ADDRESS=..."] may be simpler, since both MSVC and GNU-compatible compilers use essentially the same syntax for command-line preprocessor definitions.

The rule should handle translating its attributes into the appropriate compiler-specific flags and use the C/C++ toolchain selected for the target.

Having this functionality directly in rules_cc would be useful for projects that need standalone preprocessing and would avoid requiring users to implement or patch a custom preprocessing rule themselves.

Activity

  1. backtr4ce commented on Aug 11, 2026

    @backtr4ce
    Author

    As a concrete example, this is the custom rule I currently use for the MASM/libffi case mentioned above:

    load("@rules_cc//cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
    load("@rules_cc//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_ATTRS", "find_cc_toolchain", "use_cc_toolchain")
    load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
    load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
    
    def _msvc_asm_impl(ctx):
        cc_toolchain = find_cc_toolchain(ctx)
        feature_configuration = cc_common.configure_features(
            ctx = ctx,
            cc_toolchain = cc_toolchain,
            requested_features = ctx.features,
            unsupported_features = ctx.disabled_features,
        )
        compiler = cc_common.get_tool_for_action(
            feature_configuration = feature_configuration,
            action_name = C_COMPILE_ACTION_NAME,
        )
        environment = cc_common.get_environment_variables(
            feature_configuration = feature_configuration,
            action_name = C_COMPILE_ACTION_NAME,
            variables = cc_common.create_compile_variables(
                feature_configuration = feature_configuration,
                cc_toolchain = cc_toolchain,
            ),
        )
        context = cc_common.merge_compilation_contexts(
            compilation_contexts = [
                dep[CcInfo].compilation_context
                for dep in ctx.attr.deps
            ],
        )
    
        out = ctx.actions.declare_file(ctx.label.name + ".asm")
        args = ctx.actions.args()
        args.add("/nologo")
    
        # /EP drops the #line directives MASM chokes on, /P writes a file rather
        # than stdout, /TC is needed because .S means nothing to cl.
        args.add_all(["/EP", "/P", "/TC"])
        args.add(out, format = "/Fi%s")
        args.add_all(context.includes, format_each = "/I%s")
        args.add_all(context.quote_includes, format_each = "/I%s")
        args.add_all(context.system_includes, format_each = "/I%s")
        args.add_all(ctx.attr.defines, format_each = "/D%s")
        args.add(ctx.file.src)
    
        ctx.actions.run(
            executable = compiler,
            arguments = [args],
            inputs = depset(
                [ctx.file.src] + ctx.files.hdrs,
                transitive = [context.headers, cc_toolchain.all_files],
            ),
            outputs = [out],
            env = environment,
            mnemonic = "MsvcPreprocessAsm",
            progress_message = "Preprocessing %{input} into MASM",
        )
        return DefaultInfo(files = depset([out]))
    
    msvc_asm = rule(
        implementation = _msvc_asm_impl,
        attrs = {
            "src": attr.label(allow_single_file = [".S"], mandatory = True),
            "hdrs": attr.label_list(
                allow_files = True,
                doc = "Headers `src` includes from its own directory.",
            ),
            "deps": attr.label_list(providers = [CcInfo]),
            "defines": attr.string_list(),
        } | CC_TOOLCHAIN_ATTRS,
        toolchains = use_cc_toolchain(),
        fragments = ["cpp"],
    )

    With a generic cc_preprocess rule, all of this could essentially become:

    cc_preprocess(
        name = "ffi_asm",
        input = "win64.S",
        output = "win64.asm",
        deps = [...],
        defines = [...],
    )
  2. added
    type: feature request
    P4
    on Aug 11, 2026
  3. armandomontanez commented on Aug 14, 2026

    @armandomontanez
    Collaborator

    I suspect there's a number of home-grown variants of this given its omission from rules_cc. Would definitely be nice to not expect projects to implement a variant of this themselves.

  4. backtr4ce commented on Aug 14, 2026

    @backtr4ce
    Author

    I think so too. I built one for MSVC as mentioned above and it would generally be easier if there was a "builtin" variant. But obviously, other Issues take priority over new features. I fully respect your work

  5. backtr4ce commented on Aug 15, 2026

    @backtr4ce
    Author

    I don't seem to be able to sign that CLA thingy with my correct Google account but I wrote a patch. I could attach it to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4category: core rulestype: feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions