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