diff --git a/tools/generators/target_build_settings/README.md b/tools/generators/target_build_settings/README.md index f759999952..719870fef1 100644 --- a/tools/generators/target_build_settings/README.md +++ b/tools/generators/target_build_settings/README.md @@ -15,6 +15,7 @@ The generator accepts the following command-line arguments: - If `swift-debug-settings-output-path` is set: positional `include-self-swift-debug-settings` - If `swift-debug-settings-output-path` is set: positional `transitive-swift-debug-setting-paths-count` - If `swift-debug-settings-output-path` is set: positional list ` ...` +- Positional `execution-root-file-path` - Positional `device-family` - Positional `extension-safe` - Positional `generates-dsyms` diff --git a/tools/generators/target_build_settings/src/Generator/Generator.swift b/tools/generators/target_build_settings/src/Generator/Generator.swift index cf30f56f15..fc382271ea 100644 --- a/tools/generators/target_build_settings/src/Generator/Generator.swift +++ b/tools/generators/target_build_settings/src/Generator/Generator.swift @@ -48,13 +48,16 @@ struct Generator { transitiveSwiftDebugSettingPaths = [] } + let executionRootFilePath = try rawArguments.consumeArg("execution-root-file-path", as: URL?.self) + let (buildSettings, clangArgs, frameworkIncludes, swiftIncludes) = try await environment.processArgs( rawArguments: rawArguments, generateBuildSettings: buildSettingsOutputPath != nil, includeSelfSwiftDebugSettings: includeSelfSwiftDebugSettings, transitiveSwiftDebugSettingPaths: - transitiveSwiftDebugSettingPaths + transitiveSwiftDebugSettingPaths, + executionRootFilePath: executionRootFilePath ) let writeBuildSettingsTask = Task { diff --git a/tools/generators/target_build_settings/src/Generator/ProcessArgs.swift b/tools/generators/target_build_settings/src/Generator/ProcessArgs.swift index 989527c470..5340ab7bd7 100644 --- a/tools/generators/target_build_settings/src/Generator/ProcessArgs.swift +++ b/tools/generators/target_build_settings/src/Generator/ProcessArgs.swift @@ -30,7 +30,8 @@ extension Generator { rawArguments: Array.SubSequence, generateBuildSettings: Bool, includeSelfSwiftDebugSettings: Bool, - transitiveSwiftDebugSettingPaths: [URL] + transitiveSwiftDebugSettingPaths: [URL], + executionRootFilePath: URL? ) async throws -> ( buildSettings: [(key: String, value: String)], clangArgs: [String], @@ -44,6 +45,7 @@ extension Generator { includeSelfSwiftDebugSettings, /*transitiveSwiftDebugSettingPaths:*/ transitiveSwiftDebugSettingPaths, + /*executionRootFilePath:*/ executionRootFilePath, /*processCArgs:*/ processCArgs, /*processCxxArgs:*/ processCxxArgs, /*processSwiftArgs:*/ processSwiftArgs @@ -60,6 +62,7 @@ extension Generator.ProcessArgs { _ generateBuildSettings: Bool, _ includeSelfSwiftDebugSettings: Bool, _ transitiveSwiftDebugSettingPaths: [URL], + _ executionRootFilePath: URL?, _ processCArgs: Generator.ProcessCArgs, _ processCxxArgs: Generator.ProcessCxxArgs, _ processSwiftArgs: Generator.ProcessSwiftArgs @@ -75,6 +78,7 @@ extension Generator.ProcessArgs { generateBuildSettings: Bool, includeSelfSwiftDebugSettings: Bool, transitiveSwiftDebugSettingPaths: [URL], + executionRootFilePath: URL?, processCArgs: Generator.ProcessCArgs, processCxxArgs: Generator.ProcessCxxArgs, processSwiftArgs: Generator.ProcessSwiftArgs @@ -130,12 +134,14 @@ extension Generator.ProcessArgs { let cHasDebugInfo = try await processCArgs( argsStream: argsStream, - buildSettings: &buildSettings + buildSettings: &buildSettings, + executionRootFilePath: executionRootFilePath ) let cxxHasDebugInfo = try await processCxxArgs( argsStream: argsStream, - buildSettings: &buildSettings + buildSettings: &buildSettings, + executionRootFilePath: executionRootFilePath ) if generatesDsyms || swiftHasDebugInfo || cHasDebugInfo || diff --git a/tools/generators/target_build_settings/src/Generator/ProcessCArgs.swift b/tools/generators/target_build_settings/src/Generator/ProcessCArgs.swift index c402af9fbd..9920e2b226 100644 --- a/tools/generators/target_build_settings/src/Generator/ProcessCArgs.swift +++ b/tools/generators/target_build_settings/src/Generator/ProcessCArgs.swift @@ -1,5 +1,6 @@ import Foundation import PBXProj +import ToolCommon extension Generator { struct ProcessCArgs { @@ -25,11 +26,13 @@ extension Generator { /// Processes all the C/Objective-C arguments. func callAsFunction( argsStream: AsyncThrowingStream, - buildSettings: inout [(key: String, value: String)] + buildSettings: inout [(key: String, value: String)], + executionRootFilePath: URL? ) async throws -> Bool { try await callable( /*argsStream:*/ argsStream, /*buildSettings:*/ &buildSettings, + executionRootFilePath, /*processCcArgs:*/ processCcArgs, /*write:*/ write ) @@ -43,6 +46,7 @@ extension Generator.ProcessCArgs { typealias Callable = ( _ argsStream: AsyncThrowingStream, _ buildSettings: inout [(key: String, value: String)], + _ executionRootFilePath: URL?, _ processCcArgs: Generator.ProcessCcArgs, _ write: Write ) async throws -> Bool @@ -50,6 +54,7 @@ extension Generator.ProcessCArgs { static func defaultCallable( argsStream: AsyncThrowingStream, buildSettings: inout [(key: String, value: String)], + executionRootFilePath: URL?, processCcArgs: Generator.ProcessCcArgs, write: Write ) async throws -> Bool { @@ -70,7 +75,34 @@ extension Generator.ProcessCArgs { argsStream: argsStream ) - let content = args.map { $0 + "\n" }.joined() + var PROJECT_DIR = "" + if let execRootURL = executionRootFilePath { + PROJECT_DIR = try String(contentsOf: execRootURL, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines) + } + let BAZEL_OUT = PROJECT_DIR + "/bazel-out" + + let (DEVELOPER_DIR, SDKROOT) = (ProcessInfo.processInfo.environment["DEVELOPER_DIR"] ?? "", ProcessInfo.processInfo.environment["SDKROOT"] ?? "") + guard !DEVELOPER_DIR.isEmpty, !SDKROOT.isEmpty, !PROJECT_DIR.isEmpty else { + throw PreconditionError(message: """ +`DEVELOPER_DIR`, `SDKROOT`, and `PROJECT_DIR` must be set in the environment. +""") + } + + let environmentVariables: [String: String] = [ + "$(PROJECT_DIR)": PROJECT_DIR, + "$(BAZEL_OUT)": BAZEL_OUT, + "$(DEVELOPER_DIR)": DEVELOPER_DIR, + "$(SDKROOT)": SDKROOT + ] + + let content = try args.map { arg -> String in + var newArg = arg + for (key, value) in environmentVariables { + newArg = newArg.replacingOccurrences(of: key, with: value) + } + return newArg + "\n" + }.joined() + try write(content, to: URL(fileURLWithPath: String(outputPath))) buildSettings.append( @@ -92,7 +124,7 @@ extension Generator.ProcessCArgs { ( "ASAN_OTHER_CFLAGS__NO", #""" -"@$(DERIVED_FILE_DIR)/c.compile.params \# +"@$(BAZEL_OUT)\#(outputPath.dropFirst(9)) \# -D_FORTIFY_SOURCE=\#(fortifySourceLevel)" """# ) @@ -100,7 +132,9 @@ extension Generator.ProcessCArgs { buildSettings.append( ( "ASAN_OTHER_CFLAGS__YES", - #""@$(DERIVED_FILE_DIR)/c.compile.params""# + #""" +"@$(BAZEL_OUT)\#(outputPath.dropFirst(9))" +"""# ) ) buildSettings.append( @@ -115,7 +149,9 @@ extension Generator.ProcessCArgs { buildSettings.append( ( "OTHER_CFLAGS", - #""@$(DERIVED_FILE_DIR)/c.compile.params""# + #""" +"@$(BAZEL_OUT)\#(outputPath.dropFirst(9))" +"""# ) ) } diff --git a/tools/generators/target_build_settings/src/Generator/ProcessCxxArgs.swift b/tools/generators/target_build_settings/src/Generator/ProcessCxxArgs.swift index 2a6c04d2c2..21cff0e282 100644 --- a/tools/generators/target_build_settings/src/Generator/ProcessCxxArgs.swift +++ b/tools/generators/target_build_settings/src/Generator/ProcessCxxArgs.swift @@ -1,5 +1,6 @@ import Foundation import PBXProj +import ToolCommon extension Generator { struct ProcessCxxArgs { @@ -25,11 +26,13 @@ extension Generator { /// Processes all the C++/Objective-C++ arguments. func callAsFunction( argsStream: AsyncThrowingStream, - buildSettings: inout [(key: String, value: String)] + buildSettings: inout [(key: String, value: String)], + executionRootFilePath: URL? ) async throws -> Bool { try await callable( /*argsStream:*/ argsStream, /*buildSettings:*/ &buildSettings, + executionRootFilePath, /*processCcArgs:*/ processCcArgs, /*write:*/ write ) @@ -43,6 +46,7 @@ extension Generator.ProcessCxxArgs { typealias Callable = ( _ argsStream: AsyncThrowingStream, _ buildSettings: inout [(key: String, value: String)], + _ executionRootFilePath: URL?, _ processCcArgs: Generator.ProcessCcArgs, _ write: Write ) async throws -> Bool @@ -50,6 +54,7 @@ extension Generator.ProcessCxxArgs { static func defaultCallable( argsStream: AsyncThrowingStream, buildSettings: inout [(key: String, value: String)], + executionRootFilePath: URL?, processCcArgs: Generator.ProcessCcArgs, write: Write ) async throws -> Bool { @@ -70,7 +75,34 @@ extension Generator.ProcessCxxArgs { argsStream: argsStream ) - let content = args.map { $0 + "\n" }.joined() + var PROJECT_DIR = "" + if let execRootURL = executionRootFilePath { + PROJECT_DIR = try String(contentsOf: execRootURL, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines) + } + let BAZEL_OUT = PROJECT_DIR + "/bazel-out" + + let (DEVELOPER_DIR, SDKROOT) = (ProcessInfo.processInfo.environment["DEVELOPER_DIR"] ?? "", ProcessInfo.processInfo.environment["SDKROOT"] ?? "") + guard !DEVELOPER_DIR.isEmpty, !SDKROOT.isEmpty, !PROJECT_DIR.isEmpty else { + throw PreconditionError(message: """ +`DEVELOPER_DIR`, `SDKROOT`, and `PROJECT_DIR` must be set in the environment. +""") + } + + let environmentVariables: [String: String] = [ + "$(PROJECT_DIR)": PROJECT_DIR, + "$(BAZEL_OUT)": BAZEL_OUT, + "$(DEVELOPER_DIR)": DEVELOPER_DIR, + "$(SDKROOT)": SDKROOT + ] + + let content = try args.map { arg -> String in + var newArg = arg + for (key, value) in environmentVariables { + newArg = newArg.replacingOccurrences(of: key, with: value) + } + return newArg + "\n" + }.joined() + try write(content, to: URL(fileURLWithPath: String(outputPath))) buildSettings.append( @@ -93,7 +125,7 @@ extension Generator.ProcessCxxArgs { ( "ASAN_OTHER_CPLUSPLUSFLAGS__NO", #""" -"@$(DERIVED_FILE_DIR)/cxx.compile.params \# +"@$(BAZEL_OUT)\#(outputPath.dropFirst(9)) \# -D_FORTIFY_SOURCE=\#(fortifySourceLevel)" """# ) @@ -101,7 +133,9 @@ extension Generator.ProcessCxxArgs { buildSettings.append( ( "ASAN_OTHER_CPLUSPLUSFLAGS__YES", - #""@$(DERIVED_FILE_DIR)/cxx.compile.params""# + #""" +"@$(BAZEL_OUT)\#(outputPath.dropFirst(9))" +"""# ) ) buildSettings.append( @@ -116,7 +150,9 @@ extension Generator.ProcessCxxArgs { buildSettings.append( ( "OTHER_CPLUSPLUSFLAGS", - #""@$(DERIVED_FILE_DIR)/cxx.compile.params""# + #""" +"@$(BAZEL_OUT)\#(outputPath.dropFirst(9))" +"""# ) ) } diff --git a/xcodeproj/internal/pbxproj_partials.bzl b/xcodeproj/internal/pbxproj_partials.bzl index 1bb32fa7ba..22101f97a9 100644 --- a/xcodeproj/internal/pbxproj_partials.bzl +++ b/xcodeproj/internal/pbxproj_partials.bzl @@ -1,6 +1,7 @@ """Actions for creating `PBXProj` partials.""" load("//xcodeproj/internal/files:files.bzl", "join_paths_ignoring_empty") +load("//xcodeproj/internal:execution_root.bzl", "write_execution_root_file") load(":collections.bzl", "uniq") load( ":memory_efficiency.bzl", @@ -11,6 +12,7 @@ load( "TRUE_ARG", ) load(":platforms.bzl", "PLATFORM_NAME") +load("@build_bazel_apple_support//lib:apple_support.bzl", "apple_support") _UNIT_TEST_PRODUCT_TYPE = "u" # com.apple.product-type.bundle.unit-test @@ -1092,6 +1094,9 @@ def _write_target_build_settings( swift_args, swift_debug_settings_to_merge = EMPTY_DEPSET, team_id = None, + xcode_config, + apple_fragment, + bin_dir_path, tool): """Creates the `OTHER_SWIFT_FLAGS` build setting string file for a target. @@ -1129,6 +1134,9 @@ def _write_target_build_settings( swift_debug_settings_to_merge: A `depset` of `Files` containing Swift debug settings from dependencies. team_id: The team ID to use for code signing. + xcode_config: `apple_common.XcodeVersionConfig` providerfound in ccontext + apple_fragment: A reference to the apple fragment `ctx.fragments.apple`. + bin_dir_path: `ctx.bin_dir.path` tool: The executable that will generate the output files. Returns: @@ -1147,6 +1155,12 @@ def _write_target_build_settings( outputs = [] params = [] + execution_root_file = write_execution_root_file( + actions = actions, + bin_dir_path = bin_dir_path, + name = name, + ) + args = actions.args() # colorize @@ -1185,15 +1199,17 @@ def _write_target_build_settings( terminate_with = "", ) - inputs = swift_debug_settings_to_merge + inputs = depset(transitive = [swift_debug_settings_to_merge, depset([execution_root_file])]) else: debug_settings_output = None # swiftDebugSettingsOutputPath args.add("") + inputs = [execution_root_file] - inputs = [] - + # executionRootFilePath + args.add(execution_root_file) + # deviceFamily args.add(device_family) @@ -1263,7 +1279,10 @@ def _write_target_build_settings( # cxxParams cxx_output_args.add(cxx_params) - actions.run( + apple_support.run( + actions = actions, + xcode_config = xcode_config, + apple_fragment = apple_fragment, arguments = ( [args] + swift_args + [c_output_args] + conly_args + [cxx_output_args] + cxx_args diff --git a/xcodeproj/internal/processed_targets/incremental_library_targets.bzl b/xcodeproj/internal/processed_targets/incremental_library_targets.bzl index 47e3ee2861..05d5354dc3 100644 --- a/xcodeproj/internal/processed_targets/incremental_library_targets.bzl +++ b/xcodeproj/internal/processed_targets/incremental_library_targets.bzl @@ -129,6 +129,9 @@ def _process_incremental_library_target( target = target, ) + xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] + apple_fragment = ctx.fragments.apple + ( target_build_settings, swift_debug_settings_file, @@ -144,6 +147,9 @@ def _process_incremental_library_target( name = label.name, swift_args = args.swift, tool = ctx.executable._target_build_settings_generator, + xcode_config = xcode_config, + apple_fragment = apple_fragment, + bin_dir_path = bin_dir_path, ) swift_debug_settings = depset( diff --git a/xcodeproj/internal/processed_targets/incremental_top_level_targets.bzl b/xcodeproj/internal/processed_targets/incremental_top_level_targets.bzl index c7cd9e513a..ce997c3933 100644 --- a/xcodeproj/internal/processed_targets/incremental_top_level_targets.bzl +++ b/xcodeproj/internal/processed_targets/incremental_top_level_targets.bzl @@ -439,6 +439,9 @@ def _process_focused_top_level_target( ), ] + xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] + apple_fragment = ctx.fragments.apple + ( target_build_settings, swift_debug_settings_file, @@ -472,6 +475,9 @@ def _process_focused_top_level_target( swift_debug_settings_to_merge = swift_debug_settings_to_merge, team_id = provisioning_profile_props.team_id, tool = ctx.executable._target_build_settings_generator, + xcode_config = xcode_config, + apple_fragment = apple_fragment, + bin_dir_path = bin_dir_path, ) xcode_product = product.xcode_product @@ -756,6 +762,9 @@ def _process_unfocused_top_level_target( ], ) + xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] + apple_fragment = ctx.fragments.apple + ( _, swift_debug_settings_file, @@ -774,6 +783,9 @@ def _process_unfocused_top_level_target( swift_args = args.swift, swift_debug_settings_to_merge = swift_debug_settings_to_merge, tool = ctx.executable._target_build_settings_generator, + xcode_config = xcode_config, + apple_fragment = apple_fragment, + bin_dir_path = ctx.bin_dir.path, ) xcode_product = slim_product.xcode_product diff --git a/xcodeproj/internal/processed_targets/mixed_language_library_targets.bzl b/xcodeproj/internal/processed_targets/mixed_language_library_targets.bzl index 6a8f164235..16239b07ae 100644 --- a/xcodeproj/internal/processed_targets/mixed_language_library_targets.bzl +++ b/xcodeproj/internal/processed_targets/mixed_language_library_targets.bzl @@ -147,6 +147,9 @@ def _process_mixed_language_library_target( ) indexstore_overrides = [] + xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] + apple_fragment = ctx.fragments.apple + ( target_build_settings, _, @@ -163,6 +166,9 @@ def _process_mixed_language_library_target( name = label.name, swift_args = args.swift, tool = ctx.executable._target_build_settings_generator, + xcode_config = xcode_config, + apple_fragment = apple_fragment, + bin_dir_path = bin_dir_path, ) (