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