diff --git a/swift/toolchains/config/compile_config.bzl b/swift/toolchains/config/compile_config.bzl index 241dc9385..5e2ed2519 100644 --- a/swift/toolchains/config/compile_config.bzl +++ b/swift/toolchains/config/compile_config.bzl @@ -1231,6 +1231,52 @@ def compile_action_configs( return action_configs +def command_line_objc_copts(compilation_mode, cpp_fragment, objc_fragment): + """Returns copts that should be passed to `clang` from the `objc` fragment. + + Args: + compilation_mode: The current compilation mode. + cpp_fragment: The `cpp` configuration fragment. + objc_fragment: The `objc` configuration fragment. + + Returns: + A list of `clang` copts, each of which is preceded by `-Xcc` so that + they can be passed through `swiftc` to its underlying ClangImporter + instance. + """ + + # In general, every compilation mode flag from native `objc_*` rules should + # be passed, but `-g` seems to break Clang module compilation. Since this + # flag does not make much sense for module compilation and only touches + # headers, it's ok to omit. + # TODO(b/153867054): These flags were originally being set by Bazel's legacy + # hardcoded Objective-C behavior, which has been migrated to crosstool. In + # the long term, we should query crosstool for the flags we're interested in + # and pass those to ClangImporter, and do this across all platforms. As an + # immediate short-term workaround, we preserve the old behavior by passing + # the exact set of flags that Bazel was originally passing if the list we + # get back from the configuration fragment is empty. + legacy_copts = objc_fragment.copts_for_current_compilation_mode + if not legacy_copts: + if compilation_mode == "dbg": + legacy_copts = [ + "-O0", + "-DDEBUG=1", + "-fstack-protector", + "-fstack-protector-all", + ] + elif compilation_mode == "opt": + legacy_copts = [ + "-Os", + "-DNDEBUG=1", + "-Wno-unused-variable", + "-Winit-self", + "-Wno-extra", + ] + + clang_copts = cpp_fragment.objccopts + legacy_copts + return [copt for copt in clang_copts if copt != "-g"] + def _output_or_file_map(output_file_map, outputs, args): """Adds the output file map or single object file to the command line.""" if output_file_map: diff --git a/swift/toolchains/xcode_swift_toolchain.bzl b/swift/toolchains/xcode_swift_toolchain.bzl index f5fb5877b..491d79a0b 100644 --- a/swift/toolchains/xcode_swift_toolchain.bzl +++ b/swift/toolchains/xcode_swift_toolchain.bzl @@ -91,7 +91,11 @@ load( "//swift/toolchains/config:all_actions_config.bzl", "all_actions_action_configs", ) -load("//swift/toolchains/config:compile_config.bzl", "compile_action_configs") +load( + "//swift/toolchains/config:compile_config.bzl", + "command_line_objc_copts", + "compile_action_configs", +) load( "//swift/toolchains/config:compile_module_interface_config.bzl", "compile_module_interface_action_configs", @@ -105,52 +109,6 @@ load("//swift/toolchains/config:tool_config.bzl", "ToolConfigInfo") # TODO: Remove once we drop bazel 7.x _OBJC_PROVIDER_LINKING = hasattr(apple_common.new_objc_provider(), "linkopt") -def _command_line_objc_copts(compilation_mode, cpp_fragment, objc_fragment): - """Returns copts that should be passed to `clang` from the `objc` fragment. - - Args: - compilation_mode: The current compilation mode. - cpp_fragment: The `cpp` configuration fragment. - objc_fragment: The `objc` configuration fragment. - - Returns: - A list of `clang` copts, each of which is preceded by `-Xcc` so that - they can be passed through `swiftc` to its underlying ClangImporter - instance. - """ - - # In general, every compilation mode flag from native `objc_*` rules should - # be passed, but `-g` seems to break Clang module compilation. Since this - # flag does not make much sense for module compilation and only touches - # headers, it's ok to omit. - # TODO(b/153867054): These flags were originally being set by Bazel's legacy - # hardcoded Objective-C behavior, which has been migrated to crosstool. In - # the long term, we should query crosstool for the flags we're interested in - # and pass those to ClangImporter, and do this across all platforms. As an - # immediate short-term workaround, we preserve the old behavior by passing - # the exact set of flags that Bazel was originally passing if the list we - # get back from the configuration fragment is empty. - legacy_copts = objc_fragment.copts_for_current_compilation_mode - if not legacy_copts: - if compilation_mode == "dbg": - legacy_copts = [ - "-O0", - "-DDEBUG=1", - "-fstack-protector", - "-fstack-protector-all", - ] - elif compilation_mode == "opt": - legacy_copts = [ - "-Os", - "-DNDEBUG=1", - "-Wno-unused-variable", - "-Winit-self", - "-Wno-extra", - ] - - clang_copts = cpp_fragment.objccopts + legacy_copts - return [copt for copt in clang_copts if copt != "-g"] - def _platform_developer_framework_dir( apple_toolchain, target_triple): @@ -707,7 +665,7 @@ def _xcode_swift_toolchain_impl(ctx): toolchain_root = toolchain_root, ) all_action_configs = _all_action_configs( - additional_objc_copts = _command_line_objc_copts( + additional_objc_copts = command_line_objc_copts( ctx.var["COMPILATION_MODE"], ctx.fragments.cpp, ctx.fragments.objc,