diff --git a/cxx/compile.bzl b/cxx/compile.bzl index af7c52e4f..a1ef040e4 100644 --- a/cxx/compile.bzl +++ b/cxx/compile.bzl @@ -608,14 +608,15 @@ def _get_category(ext: CxxExtension) -> str: # This should be unreachable as long as we handle all enum values fail("Unknown extension: " + ext.value) -def _get_compile_base(compiler_info: typing.Any) -> cmd_args: +def _get_compile_base(toolchain: CxxToolchainInfo, compiler_info: typing.Any) -> cmd_args: """ Given a compiler info returned by _get_compiler_info, form the base compile args. """ - cmd = cmd_args(compiler_info.compiler) - - return cmd + if toolchain.remap_cwd and compiler_info.compiler_type in ["clang", "clang_windows", "clang_cl"]: + return cmd_args(toolchain.remap_cwd, compiler_info.compiler) + else: + return cmd_args(compiler_info.compiler) def _dep_file_type(ext: CxxExtension) -> [DepFileType, None]: # Raw assembly doesn't make sense to capture dep files for. @@ -761,7 +762,7 @@ def _generate_base_compile_command( """ toolchain = get_cxx_toolchain_info(ctx) compiler_info = _get_compiler_info(toolchain, ext) - base_compile_cmd = _get_compile_base(compiler_info) + base_compile_cmd = _get_compile_base(toolchain, compiler_info) category = _get_category(ext) headers_dep_files = None diff --git a/cxx/cxx_toolchain.bzl b/cxx/cxx_toolchain.bzl index 6baa445b9..bbadebac3 100644 --- a/cxx/cxx_toolchain.bzl +++ b/cxx/cxx_toolchain.bzl @@ -179,6 +179,7 @@ def cxx_toolchain_impl(ctx): dumpbin_toolchain_path = ctx.attrs._dumpbin_toolchain_path[DefaultInfo].default_outputs[0] if ctx.attrs._dumpbin_toolchain_path else None, target_sdk_version = get_toolchain_target_sdk_version(ctx), dist_lto_tools_info = ctx.attrs.dist_lto_tools[DistLtoToolsInfo], + remap_cwd = ctx.attrs._remap_cwd_tool[RunInfo] if ctx.attrs.remap_cwd else None, ) def cxx_toolchain_extra_attributes(is_toolchain_rule): @@ -225,6 +226,7 @@ def cxx_toolchain_extra_attributes(is_toolchain_rule): "public_headers_symlinks_enabled": attrs.bool(default = True), "ranlib": attrs.option(dep_type(providers = [RunInfo]), default = None), "rc_compiler": attrs.option(dep_type(providers = [RunInfo]), default = None), + "remap_cwd": attrs.bool(default = False), "requires_objects": attrs.bool(default = False), "sanitizer_runtime_enabled": attrs.bool(default = False), "sanitizer_runtime_files": attrs.set(attrs.dep(), sorted = True, default = []), # Use `attrs.dep()` as it's not a tool, always propagate target platform @@ -255,6 +257,7 @@ def cxx_toolchain_extra_attributes(is_toolchain_rule): # FIXME: prelude// should be standalone (not refer to fbsource//) "_mk_hmap": attrs.default_only(dep_type(providers = [RunInfo], default = "prelude//cxx/tools:hmap_wrapper")), "_msvc_hermetic_exec": attrs.default_only(dep_type(providers = [RunInfo], default = "prelude//windows/tools:msvc_hermetic_exec")), + "_remap_cwd_tool": attrs.default_only(dep_type(providers = [RunInfo], default = "prelude//cxx/tools:remap_cwd")), } | cxx_toolchain_allow_cache_upload_args() def _cxx_toolchain_inheriting_target_platform_attrs(): diff --git a/cxx/cxx_toolchain_types.bzl b/cxx/cxx_toolchain_types.bzl index b474b7216..bee3525ef 100644 --- a/cxx/cxx_toolchain_types.bzl +++ b/cxx/cxx_toolchain_types.bzl @@ -204,6 +204,7 @@ CxxToolchainInfo = provider( "pic_behavior": provider_field(typing.Any, default = None), "dumpbin_toolchain_path": provider_field(typing.Any, default = None), "target_sdk_version": provider_field([str, None], default = None), + "remap_cwd": provider_field(RunInfo | None, default = None), }, ) @@ -257,7 +258,8 @@ def cxx_toolchain_infos( platform_deps_aliases = [], pic_behavior = PicBehavior("supported"), dumpbin_toolchain_path = None, - target_sdk_version = None): + target_sdk_version = None, + remap_cwd = None): """ Creates the collection of cxx-toolchain Infos for a cxx toolchain. @@ -301,6 +303,7 @@ def cxx_toolchain_infos( pic_behavior = pic_behavior, dumpbin_toolchain_path = dumpbin_toolchain_path, target_sdk_version = target_sdk_version, + remap_cwd = remap_cwd, ) # Provide placeholder mappings, used primarily by cxx_genrule. diff --git a/cxx/tools/BUCK.v2 b/cxx/tools/BUCK.v2 index 8ca3c303b..eaa5ff204 100644 --- a/cxx/tools/BUCK.v2 +++ b/cxx/tools/BUCK.v2 @@ -54,6 +54,12 @@ prelude.python_bootstrap_binary( visibility = ["PUBLIC"], ) +prelude.python_bootstrap_binary( + name = "remap_cwd", + main = "remap_cwd.py", + visibility = ["PUBLIC"], +) + # Required to support the $(cxx-header-tree) macro cxx_hacks( name = "cxx_hacks", diff --git a/cxx/tools/remap_cwd.py b/cxx/tools/remap_cwd.py new file mode 100755 index 000000000..b2b75689b --- /dev/null +++ b/cxx/tools/remap_cwd.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under both the MIT license found in the +# LICENSE-MIT file in the root directory of this source tree and the Apache +# License, Version 2.0 found in the LICENSE-APACHE file in the root directory +# of this source tree. + +""" +Usage: remap_cwd.py path/to/clang++ [args...] + +Runs `path/to/clang++ -ffile-prefix-map=$PWD= [args...]` +""" + +import os +import subprocess +import sys + + +if __name__ == "__main__": + cwd = os.getcwd() + # Add trailing slash + cwd = os.path.join(cwd, "") + + ret = subprocess.call( + [ + sys.argv[1], + f"-ffile-prefix-map={cwd}=", + *sys.argv[2:], + ], + ) + sys.exit(ret)