1# Copyright 2014 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import("//build/config/ios/ios_sdk.gni") 6import("//build/toolchain/apple/toolchain.gni") 7import("//build/toolchain/goma.gni") 8import("//build/toolchain/rbe.gni") 9import("//build/toolchain/siso.gni") 10import("//build/toolchain/toolchain.gni") 11import("//build_overrides/build.gni") 12 13# This is included by reference in the //build/config/compiler config that 14# is applied to all targets. It is here to separate out the logic. 15config("compiler") { 16 # These flags are shared between the C compiler and linker. 17 common_flags = [] 18 19 # CPU architecture. 20 if (current_cpu == "x64") { 21 triplet_cpu = "x86_64" 22 } else if (current_cpu == "x86") { 23 triplet_cpu = "i386" 24 } else if (current_cpu == "arm" || current_cpu == "armv7") { 25 triplet_cpu = "armv7" 26 } else if (current_cpu == "arm64") { 27 triplet_cpu = "arm64" 28 } else { 29 assert(false, "unsupported cpu: $current_cpu") 30 } 31 32 # Environment. 33 if (target_environment == "simulator") { 34 triplet_environment = "-simulator" 35 } else if (target_environment == "device") { 36 triplet_environment = "" 37 } else if (target_environment == "catalyst") { 38 triplet_environment = "-macabi" 39 } else { 40 assert(false, "unsupported environment: $target_environment") 41 } 42 43 # OS. 44 triplet_os = "apple-ios" 45 46 # Set target. 47 common_flags = [ 48 "-target", 49 "$triplet_cpu-$triplet_os$ios_deployment_target$triplet_environment", 50 ] 51 52 # This is here so that all files get recompiled after an Xcode update. 53 # (defines are passed via the command line, and build system rebuild things 54 # when their commandline changes). Nothing should ever read this define. 55 defines = [ "CR_XCODE_VERSION=$xcode_version" ] 56 57 asmflags = common_flags 58 cflags = common_flags 59 swiftflags = common_flags 60 61 swiftflags += [ 62 "-swift-version", 63 "5", 64 ] 65 66 cflags_objcc = [ 67 # When using -std=c++20 or higher, clang automatically returns true for 68 # `__has_feature(modules)` as it enables cxx modules. This is problematic 69 # because Objective-C code uses this to detect whether `@import` can be 70 # used (this feature is also named modules). 71 # 72 # Since Chromium does not yet enable cxx modules, nor clang modules, 73 # force disable the cxx modules, which cause `__has_features(modules)` 74 # to return false unless clang modules are explicitly enabled. 75 "-Xclang", 76 "-fno-cxx-modules", 77 ] 78 79 ldflags = common_flags 80 81 # This is on by default in ld64 for our deployment target, but not yet 82 # in ld64.lld. Force it on. 83 if (ios_deployment_target != "11.0" && ios_deployment_target != "12.0" && 84 ios_deployment_target != "13.0" && ios_deployment_target != "13.4" && 85 ios_deployment_target != "14.0") { 86 ldflags += [ "-Wl,-fixup_chains" ] 87 } 88 89 if (ios_is_app_extension) { 90 ldflags += [ "-fapplication-extension" ] 91 } 92} 93 94# This is included by reference in the //build/config/compiler:runtime_library 95# config that is applied to all targets. It is here to separate out the logic 96# that is iOS-only. Please see that target for advice on what should go in 97# :runtime_library vs. :compiler. 98config("runtime_library") { 99 # The variable ios_sdk_path is relative to root_build_dir when using Goma RBE 100 # and system Xcode (since Goma RBE only supports paths relative to source). 101 # Rebase the value in that case since gn does not convert paths in compiler 102 # flags (since it is not aware they are paths). 103 _sdk_root = ios_sdk_path 104 if (use_system_xcode && (use_goma || use_remoteexec || use_siso)) { 105 _sdk_root = rebase_path(ios_sdk_path, root_build_dir) 106 } 107 108 common_flags = [ 109 "-isysroot", 110 _sdk_root, 111 ] 112 swiftflags = [ 113 "-sdk", 114 _sdk_root, 115 ] 116 117 if (target_environment == "catalyst") { 118 common_flags += [ 119 "-isystem", 120 "$_sdk_root/System/iOSSupport/usr/include", 121 "-iframework", 122 "$_sdk_root/System/iOSSupport/System/Library/Frameworks", 123 ] 124 125 swiftflags += [ 126 "-isystem", 127 "$_sdk_root/System/iOSSupport/usr/include", 128 "-Fsystem", 129 "$_sdk_root/System/iOSSupport/System/Library/Frameworks", 130 ] 131 } 132 133 asmflags = common_flags 134 cflags = common_flags 135 ldflags = common_flags 136} 137 138config("ios_executable_flags") { 139 ldflags = [] 140 141 # On "catalyst", the bundle structure is different (uses the same structure 142 # as a regular macOS app), so an additional -rpath is required. 143 if (target_environment == "catalyst") { 144 ldflags += [ "-Wl,-rpath,@loader_path/../Frameworks" ] 145 } 146 147 ldflags += [ "-Wl,-rpath,@executable_path/Frameworks" ] 148} 149 150config("ios_extension_executable_flags") { 151 configs = default_executable_configs 152 153 ldflags = [ 154 "-e", 155 "_NSExtensionMain", 156 ] 157 158 # On "catalyst", the bundle structure is different (uses the same structure 159 # as a regular macOS app), so an additional -rpath is required. 160 if (target_environment == "catalyst") { 161 ldflags += [ "-Wl,-rpath,@loader_path/../../../../Frameworks" ] 162 } 163 164 ldflags += [ "-Wl,-rpath,@executable_path/../../Frameworks" ] 165} 166 167config("ios_dynamic_flags") { 168 ldflags = [ 169 # Always load Objective-C categories and class. 170 "-Wl,-ObjC", 171 ] 172 173 # The path to the Swift compatibility libraries (required to run code built 174 # with version N of the SDK on older version of the OS) is relative to the 175 # toolchains directory and changes with the environment when using the 176 # system toolchain. When using the hermetic swift toolchain instead, those 177 # libraries are relative to $swift_toolchain_path. 178 if (swift_toolchain_path == "") { 179 _swift_compatibility_libs_prefix = ios_toolchains_path 180 } else { 181 _swift_compatibility_libs_prefix = swift_toolchain_path 182 } 183 184 if (target_environment == "simulator") { 185 _swift_compatibility_libs_suffix = "iphonesimulator" 186 } else if (target_environment == "device") { 187 _swift_compatibility_libs_suffix = "iphoneos" 188 } else if (target_environment == "catalyst") { 189 # The Swift compatibility libraries have changed location starting with 190 # Xcode 13.0, so check the version of Xcode when deciding which path to 191 # use. 192 if (xcode_version_int >= 1300) { 193 _swift_compatibility_libs_suffix = "macosx" 194 } else { 195 _swift_compatibility_libs_suffix = "maccatalyst" 196 } 197 } 198 199 lib_dirs = [ 200 "$ios_sdk_path/usr/lib/swift", 201 "$_swift_compatibility_libs_prefix/usr/lib/swift/" + 202 "$_swift_compatibility_libs_suffix", 203 ] 204 205 # When building for catalyst, some Swift support libraries are in a 206 # different directory which needs to be added to the search path. 207 if (target_environment == "catalyst") { 208 lib_dirs += [ "$ios_sdk_path/System/iOSSupport/usr/lib/swift" ] 209 } 210} 211 212config("ios_shared_library_flags") { 213 ldflags = [ 214 "-Wl,-rpath,@executable_path/Frameworks", 215 "-Wl,-rpath,@loader_path/Frameworks", 216 ] 217} 218 219config("xctest_config") { 220 # Add some directories to the system framework search path to make 221 # them available to the compiler while silencing warnings in the 222 # framework headers. This is required for XCTest. 223 common_flags = [ 224 "-iframework", 225 rebase_path("$ios_sdk_platform_path/Developer/Library/Frameworks", 226 root_build_dir), 227 "-iframework", 228 rebase_path("$ios_sdk_path/Developer/Library/Frameworks", root_build_dir), 229 ] 230 cflags = common_flags 231 ldflags = common_flags 232 swiftflags = common_flags 233 234 include_dirs = [ "$ios_sdk_platform_path/Developer/usr/lib" ] 235 lib_dirs = [ "$ios_sdk_platform_path/Developer/usr/lib" ] 236 frameworks = [ 237 "Foundation.framework", 238 "XCTest.framework", 239 ] 240} 241 242# TODO(crbug.com/1444308): any target that uses this config will miscompile. 243# This needs to be fixed if we want to use Swift - C++ interop. 244config("enable_swift_cxx_interop") { 245 swiftflags = [ "-enable-experimental-cxx-interop" ] 246} 247 248group("xctest") { 249 public_configs = [ ":xctest_config" ] 250} 251 252_xctrunner_path = 253 "$ios_sdk_platform_path/Developer/Library/Xcode/Agents/XCTRunner.app" 254 255# When building with Goma RBE, $ios_sdk_platform_path corresponds to a symlink 256# below $root_build_dir that points to the real SDK to use. Because the files 257# are below $root_build_dir, it is not possible to list them as a target input 258# without gn complaining (as it can't find a target creating those files). 259# 260# The symlinks are created by //build/config/apple/sdk_info.py script invoked 261# via exec_script() from //build/config/{ios/ios_sdk.gni,mac/mac_sdk.gni}. 262# As the invocation is done by exec_script, there is no target that can list 263# those files as output. 264# 265# To workaround this, add a target that pretends to create those files 266# (but does nothing). See https://crbug.com/1061487 for why this is needed. 267if (use_system_xcode && (use_goma || use_remoteexec || use_siso)) { 268 action("copy_xctrunner_app") { 269 testonly = true 270 script = "//build/noop.py" 271 outputs = [ 272 "$_xctrunner_path/Info.plist", 273 "$_xctrunner_path/PkgInfo", 274 "$_xctrunner_path/XCTRunner", 275 ] 276 } 277} 278 279# When creating the test runner for an XCUITest, the arm64e slice of the binary 280# must be removed (at least until the app ships with arm64e slice which is not 281# yet supported by Apple). 282action("xctest_runner_without_arm64e") { 283 testonly = true 284 script = "//build/config/ios/strip_arm64e.py" 285 sources = [ "$_xctrunner_path/XCTRunner" ] 286 outputs = [ "$target_out_dir/XCTRunner" ] 287 args = [ 288 "--output", 289 rebase_path(outputs[0], root_build_dir), 290 "--input", 291 rebase_path(sources[0], root_build_dir), 292 "--xcode-version", 293 xcode_version, 294 ] 295 296 # When running under ASan, the ASan runtime library must be packaged alongside 297 # the test runner binary. 298 deps = [ "//build/config/sanitizers:deps" ] 299 if (use_system_xcode && (use_goma || use_remoteexec || use_siso)) { 300 deps += [ ":copy_xctrunner_app" ] 301 } 302} 303