Skip to content

Commit

Permalink
add objc_compiler_flags and objcxx_compiler_flags to cxx_toolchain
Browse files Browse the repository at this point in the history
Summary:
We have separate fields for c and cxx compiler flags, but not objc and objcxx. This would be useful to specificy ObjC specific flags like `-fobjc-arc`.

For backwards compatibility the ObjC flags include the C flags and the ObjC++ flags include the C++ flags.

Reviewed By: IanChilds, drodriguez

Differential Revision: D69687115

fbshipit-source-id: 1dec288009d5dd52217fdb9850abd6e5b3bd7691
  • Loading branch information
rmaz authored and facebook-github-bot committed Feb 19, 2025
1 parent 0aaae3f commit ee66d08
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
8 changes: 6 additions & 2 deletions prelude/cxx/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,14 @@ def _validate_target_headers(ctx: AnalysisContext, preprocessor: list[CPreproces

def _get_compiler_info(toolchain: CxxToolchainInfo, ext: CxxExtension) -> typing.Any:
compiler_info = None
if ext.value in (".cpp", ".cc", ".cl", ".mm", ".cxx", ".c++", ".h", ".hpp", ".hh", ".h++", ".hxx", ".bc"):
if ext.value in (".cpp", ".cc", ".cl", ".cxx", ".c++", ".h", ".hpp", ".hh", ".h++", ".hxx", ".bc"):
compiler_info = toolchain.cxx_compiler_info
elif ext.value in (".c", ".m"):
elif ext.value == ".c":
compiler_info = toolchain.c_compiler_info
elif ext.value == ".m":
compiler_info = toolchain.objc_compiler_info
elif ext.value == ".mm":
compiler_info = toolchain.objcxx_compiler_info
elif ext.value in (".s", ".sx", ".S"):
compiler_info = toolchain.as_compiler_info
elif ext.value == ".cu":
Expand Down
20 changes: 20 additions & 0 deletions prelude/cxx/cxx_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ load(
"HipCompilerInfo",
"LinkerInfo",
"LinkerType",
"ObjcCompilerInfo",
"ObjcxxCompilerInfo",
"PicBehavior",
"RcCompilerInfo",
"ShlibInterfacesMode",
Expand Down Expand Up @@ -56,6 +58,14 @@ def cxx_toolchain_impl(ctx):
preprocessor_flags = cmd_args(ctx.attrs.c_preprocessor_flags),
allow_cache_upload = ctx.attrs.c_compiler_allow_cache_upload,
)
objc_info = ObjcCompilerInfo(
compiler = c_compiler,
compiler_type = ctx.attrs.c_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.c_compiler_flags, c_lto_flags, ctx.attrs.objc_compiler_flags),
preprocessor = c_compiler,
preprocessor_flags = cmd_args(ctx.attrs.c_preprocessor_flags),
allow_cache_upload = ctx.attrs.c_compiler_allow_cache_upload,
)
cxx_compiler = _get_maybe_wrapped_msvc(ctx.attrs.cxx_compiler[RunInfo], ctx.attrs.cxx_compiler_type or ctx.attrs.compiler_type, ctx.attrs._msvc_hermetic_exec[RunInfo])
cxx_info = CxxCompilerInfo(
compiler = cxx_compiler,
Expand All @@ -65,6 +75,14 @@ def cxx_toolchain_impl(ctx):
preprocessor_flags = cmd_args(ctx.attrs.cxx_preprocessor_flags),
allow_cache_upload = ctx.attrs.cxx_compiler_allow_cache_upload,
)
objcxx_info = ObjcxxCompilerInfo(
compiler = cxx_compiler,
compiler_type = ctx.attrs.cxx_compiler_type or ctx.attrs.compiler_type,
compiler_flags = cmd_args(ctx.attrs.cxx_compiler_flags, c_lto_flags, ctx.attrs.objcxx_compiler_flags),
preprocessor = cxx_compiler,
preprocessor_flags = cmd_args(ctx.attrs.cxx_preprocessor_flags),
allow_cache_upload = ctx.attrs.cxx_compiler_allow_cache_upload,
)
asm_info = AsmCompilerInfo(
compiler = ctx.attrs.asm_compiler[RunInfo],
compiler_type = ctx.attrs.asm_compiler_type or ctx.attrs.compiler_type,
Expand Down Expand Up @@ -193,6 +211,8 @@ def cxx_toolchain_impl(ctx):
linker_info = linker_info,
lipo = ctx.attrs.lipo[RunInfo] if ctx.attrs.lipo else None,
llvm_link = ctx.attrs.llvm_link[RunInfo] if ctx.attrs.llvm_link else None,
objc_compiler_info = objc_info,
objcxx_compiler_info = objcxx_info,
object_format = CxxObjectFormat(object_format),
optimization_compiler_flags_EXPERIMENTAL = ctx.attrs.optimization_compiler_flags_EXPERIMENTAL,
pic_behavior = PicBehavior(ctx.attrs.pic_behavior),
Expand Down
20 changes: 19 additions & 1 deletion prelude/cxx/cxx_toolchain_types.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ CudaCompilerInfo = provider(fields = _compiler_fields)
CvtresCompilerInfo = provider(fields = _compiler_fields)
CxxCompilerInfo = provider(fields = _compiler_fields)
HipCompilerInfo = provider(fields = _compiler_fields)
ObjcCompilerInfo = provider(fields = _compiler_fields)
ObjcxxCompilerInfo = provider(fields = _compiler_fields)
RcCompilerInfo = provider(fields = _compiler_fields)

DistLtoToolsInfo = provider(fields = dict(
Expand Down Expand Up @@ -213,6 +215,8 @@ CxxToolchainInfo = provider(
"linker_info": provider_field(typing.Any, default = None),
"lipo": provider_field([RunInfo, None], default = None),
"llvm_link": provider_field(typing.Any, default = None),
"objc_compiler_info": provider_field([ObjcCompilerInfo, None], default = None),
"objcxx_compiler_info": provider_field([ObjcxxCompilerInfo, None], default = None),
"object_format": provider_field(typing.Any, default = None),
"optimization_compiler_flags_EXPERIMENTAL": provider_field(typing.Any, default = []),
"pic_behavior": provider_field(typing.Any, default = None),
Expand Down Expand Up @@ -279,7 +283,9 @@ def cxx_toolchain_infos(
target_sdk_version = None,
lipo = None,
remap_cwd = False,
optimization_compiler_flags_EXPERIMENTAL = []):
optimization_compiler_flags_EXPERIMENTAL = [],
objc_compiler_info = None,
objcxx_compiler_info = None):
"""
Creates the collection of cxx-toolchain Infos for a cxx toolchain.
Expand All @@ -291,6 +297,16 @@ def cxx_toolchain_infos(
# TODO(T110378099): verify types of the inner info objects.
_validate_linker_info(linker_info)

# Maintain backwards compatibility with ObjC compilation using the C compiler.
if objc_compiler_info == None:
objc_compiler_info = ObjcCompilerInfo(
**{k: getattr(c_compiler_info, k, None) for k in _compiler_fields}
)
if objcxx_compiler_info == None:
objcxx_compiler_info = ObjcxxCompilerInfo(
**{k: getattr(cxx_compiler_info, k, None) for k in _compiler_fields}
)

toolchain_info = CxxToolchainInfo(
as_compiler_info = as_compiler_info,
asm_compiler_info = asm_compiler_info,
Expand All @@ -314,6 +330,8 @@ def cxx_toolchain_infos(
linker_info = linker_info,
lipo = lipo,
llvm_link = llvm_link,
objc_compiler_info = objc_compiler_info,
objcxx_compiler_info = objcxx_compiler_info,
object_format = object_format,
optimization_compiler_flags_EXPERIMENTAL = optimization_compiler_flags_EXPERIMENTAL,
pic_behavior = pic_behavior,
Expand Down
24 changes: 24 additions & 0 deletions prelude/cxx/user/cxx_toolchain_override.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ load(
"CxxToolchainInfo",
"LinkerInfo",
"LinkerType",
"ObjcCompilerInfo",
"ObjcxxCompilerInfo",
"PicBehavior",
"ShlibInterfacesMode",
"StripFlagsInfo",
Expand Down Expand Up @@ -70,6 +72,16 @@ def _cxx_toolchain_override(ctx):
preprocessor_flags = _pick(ctx.attrs.c_preprocessor_flags, base_c_info.preprocessor_flags),
allow_cache_upload = _pick_raw(ctx.attrs.c_compiler_allow_cache_upload, base_c_info.allow_cache_upload),
)
base_objc_info = base_toolchain.objc_compiler_info
objc_info = ObjcCompilerInfo(
compiler = _pick_bin(ctx.attrs.c_compiler, base_c_info.compiler),
compiler_type = base_c_info.compiler_type,
compiler_flags = _pick_and_add(ctx.attrs.c_compiler_flags, ctx.attrs.additional_c_compiler_flags, base_objc_info.compiler_flags),
preprocessor = _pick_bin(ctx.attrs.c_compiler, base_c_info.preprocessor),
preprocessor_type = base_c_info.preprocessor_type,
preprocessor_flags = _pick(ctx.attrs.c_preprocessor_flags, base_c_info.preprocessor_flags),
allow_cache_upload = _pick_raw(ctx.attrs.c_compiler_allow_cache_upload, base_c_info.allow_cache_upload),
)
base_cxx_info = base_toolchain.cxx_compiler_info
cxx_info = CxxCompilerInfo(
compiler = _pick_bin(ctx.attrs.cxx_compiler, base_cxx_info.compiler),
Expand All @@ -80,6 +92,16 @@ def _cxx_toolchain_override(ctx):
preprocessor_flags = _pick(ctx.attrs.cxx_preprocessor_flags, base_cxx_info.preprocessor_flags),
allow_cache_upload = _pick_raw(ctx.attrs.cxx_compiler_allow_cache_upload, base_cxx_info.allow_cache_upload),
)
base_objcxx_info = base_toolchain.objcxx_compiler_info
objcxx_info = ObjcxxCompilerInfo(
compiler = _pick_bin(ctx.attrs.cxx_compiler, base_cxx_info.compiler),
compiler_type = base_cxx_info.compiler_type,
compiler_flags = _pick_and_add(ctx.attrs.cxx_compiler_flags, ctx.attrs.additional_cxx_compiler_flags, base_objcxx_info.compiler_flags),
preprocessor = _pick_bin(ctx.attrs.cxx_compiler, base_cxx_info.preprocessor),
preprocessor_type = base_cxx_info.preprocessor_type,
preprocessor_flags = _pick(ctx.attrs.cxx_preprocessor_flags, base_cxx_info.preprocessor_flags),
allow_cache_upload = _pick_raw(ctx.attrs.cxx_compiler_allow_cache_upload, base_cxx_info.allow_cache_upload),
)
base_linker_info = base_toolchain.linker_info
linker_type = LinkerType(ctx.attrs.linker_type) if ctx.attrs.linker_type != None else base_linker_info.type
pdb_expected = is_pdb_generated(linker_type, ctx.attrs.linker_flags) if ctx.attrs.linker_flags != None else base_linker_info.is_pdb_generated
Expand Down Expand Up @@ -168,6 +190,8 @@ def _cxx_toolchain_override(ctx):
bolt_enabled = value_or(ctx.attrs.bolt_enabled, base_toolchain.bolt_enabled),
c_compiler_info = c_info,
cxx_compiler_info = cxx_info,
objc_compiler_info = objc_info,
objcxx_compiler_info = objcxx_info,
llvm_link = ctx.attrs.llvm_link if ctx.attrs.llvm_link != None else base_toolchain.llvm_link,
# the rest are used without overrides
cuda_compiler_info = base_toolchain.cuda_compiler_info,
Expand Down
2 changes: 2 additions & 0 deletions prelude/decls/cxx_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,9 @@ cxx_toolchain = prelude_rule(
"linker_flags": attrs.list(attrs.arg(anon_target_compatible = True), default = []),
"linker_type": attrs.enum(LinkerProviderType),
"nm": attrs.source(),
"objc_compiler_flags": attrs.list(attrs.arg(), default = []),
"objcopy_for_shared_library_interface": attrs.source(),
"objcxx_compiler_flags": attrs.list(attrs.arg(), default = []),
"objdump": attrs.option(attrs.source(), default = None),
"object_file_extension": attrs.string(default = ""),
"post_linker_flags": attrs.list(attrs.arg(anon_target_compatible = True), default = []),
Expand Down

0 comments on commit ee66d08

Please sign in to comment.