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