toolchain.gni 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. # Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # TODO(brettw) Use "gcc_toolchain.gni" like the Linux toolchains. This requires
  5. # some enhancements since the commands on Mac are slightly different than on
  6. # Linux.
  7. import("//build/config/apple/symbols.gni")
  8. import("//build/config/clang/clang.gni")
  9. import("//build/config/compiler/compiler.gni")
  10. import("//build/config/coverage/coverage.gni")
  11. import("//build/config/rust.gni")
  12. import("//build/toolchain/cc_wrapper.gni")
  13. import("//build/toolchain/goma.gni")
  14. import("//build/toolchain/rbe.gni")
  15. import("//build/toolchain/toolchain.gni")
  16. assert((target_os == "ios" && host_os == "mac") || host_os != "win")
  17. declare_args() {
  18. # This controls whether whole module optimization is enabled when building
  19. # Swift modules. If enabled, the compiler will compile the module as one
  20. # unit, generating just one single object file. Otherwise, it will generate
  21. # one object file per .swift file. If unspecified, will default to "true"
  22. # for official builds, and "false" for all other builds.
  23. swift_whole_module_optimization = -1
  24. }
  25. if (swift_whole_module_optimization == -1) {
  26. swift_whole_module_optimization = is_official_build
  27. }
  28. # When implementing tools using Python scripts, a TOOL_VERSION=N env
  29. # variable is placed in front of the command. The N should be incremented
  30. # whenever the script is changed, so that the build system rebuilds all
  31. # edges that utilize the script. Ideally this should be changed to use
  32. # proper input-dirty checking, but that could be expensive. Instead, use a
  33. # script to get the tool scripts' modification time to use as the version.
  34. # This won't cause a re-generation of GN files when the tool script changes
  35. # but it will cause edges to be marked as dirty if the ninja files are
  36. # regenerated. See https://crbug.com/619083 for details. A proper fix
  37. # would be to have inputs to tools (https://crbug.com/621119).
  38. tool_versions =
  39. exec_script("get_tool_mtime.py",
  40. rebase_path([
  41. "//build/toolchain/apple/filter_libtool.py",
  42. "//build/toolchain/apple/linker_driver.py",
  43. "//build/toolchain/ios/compile_xcassets.py",
  44. "//build/toolchain/ios/swiftc.py",
  45. ],
  46. root_build_dir),
  47. "trim scope")
  48. # Shared toolchain definition. Invocations should set current_os to set the
  49. # build args in this definition.
  50. template("apple_toolchain") {
  51. toolchain(target_name) {
  52. # When invoking this toolchain not as the default one, these args will be
  53. # passed to the build. They are ignored when this is the default toolchain.
  54. assert(defined(invoker.toolchain_args),
  55. "Toolchains must declare toolchain_args")
  56. toolchain_args = {
  57. # Populate toolchain args from the invoker.
  58. forward_variables_from(invoker.toolchain_args, "*")
  59. # The host toolchain value computed by the default toolchain's setup
  60. # needs to be passed through unchanged to all secondary toolchains to
  61. # ensure that it's always the same, regardless of the values that may be
  62. # set on those toolchains.
  63. host_toolchain = host_toolchain
  64. # Respect the global setting for whether rustc can make binaries.
  65. rustc_can_link = rustc_can_link
  66. }
  67. # When the invoker has explicitly overridden use_goma or cc_wrapper in the
  68. # toolchain args, use those values, otherwise default to the global one.
  69. # This works because the only reasonable override that toolchains might
  70. # supply for these values are to force-disable them.
  71. if (defined(toolchain_args.use_remoteexec)) {
  72. toolchain_uses_remoteexec = toolchain_args.use_remoteexec
  73. } else {
  74. toolchain_uses_remoteexec = use_remoteexec
  75. }
  76. if (defined(toolchain_args.use_goma)) {
  77. toolchain_uses_goma = toolchain_args.use_goma
  78. } else {
  79. toolchain_uses_goma = use_goma
  80. }
  81. if (defined(toolchain_args.cc_wrapper)) {
  82. toolchain_cc_wrapper = toolchain_args.cc_wrapper
  83. } else {
  84. toolchain_cc_wrapper = cc_wrapper
  85. }
  86. assert(!(toolchain_uses_remoteexec && toolchain_uses_goma),
  87. "Goma and re-client can't be used together.")
  88. assert(!(toolchain_cc_wrapper != "" && toolchain_uses_remoteexec),
  89. "re-client and cc_wrapper can't be used together.")
  90. assert(!(toolchain_cc_wrapper != "" && toolchain_uses_goma),
  91. "Goma and cc_wrapper can't be used together.")
  92. if (defined(toolchain_args.use_lld)) {
  93. toolchain_uses_lld = toolchain_args.use_lld
  94. } else {
  95. toolchain_uses_lld = use_lld
  96. }
  97. # The value of all global variables (such as `is_component_build`) is the
  98. # one from the default toolchain when evaluating a secondary toolchain
  99. # (see https://crbug.com/gn/286). This mean that the value may change when
  100. # evaluating target/configs in the new toolchain if the variable default
  101. # value depends on variable set in `toolchain_args`.
  102. #
  103. # For this reason, "ios" needs to override `is_component_build` as its
  104. # default value depends on `current_os`. Use the overridden value if it
  105. # is set in `toolchain_args`.
  106. if (defined(toolchain_args.is_component_build)) {
  107. toolchain_is_component_build = toolchain_args.is_component_build
  108. } else {
  109. toolchain_is_component_build = is_component_build
  110. }
  111. prefix = rebase_path("$clang_base_path/bin/", root_build_dir)
  112. _cc = "${prefix}clang"
  113. _cxx = "${prefix}clang++"
  114. swiftmodule_switch = "-Wl,-add_ast_path,"
  115. # Compute the compiler prefix.
  116. if (toolchain_uses_remoteexec) {
  117. if (defined(toolchain_args.rbe_cc_cfg_file)) {
  118. toolchain_rbe_cc_cfg_file = toolchain_args.rbe_cc_cfg_file
  119. } else {
  120. toolchain_rbe_cc_cfg_file = rbe_cc_cfg_file
  121. }
  122. # C/C++ (clang) rewrapper prefix to use when use_remoteexec is true.
  123. compiler_prefix = "${rbe_bin_dir}/rewrapper -cfg=${toolchain_rbe_cc_cfg_file} -exec_root=${rbe_exec_root} "
  124. } else if (toolchain_uses_goma) {
  125. assert(toolchain_cc_wrapper == "",
  126. "Goma and cc_wrapper can't be used together.")
  127. compiler_prefix = "$goma_dir/gomacc "
  128. if (use_goma_rust) {
  129. rust_compiler_prefix = compiler_prefix
  130. }
  131. } else if (toolchain_cc_wrapper != "") {
  132. compiler_prefix = toolchain_cc_wrapper + " "
  133. } else {
  134. compiler_prefix = ""
  135. }
  136. cc = compiler_prefix + _cc
  137. cxx = compiler_prefix + _cxx
  138. ld = _cxx
  139. # Set the explicit search path for clang++ so it uses the right linker
  140. # binary.
  141. if (!toolchain_uses_lld) {
  142. ld += " -B " + invoker.bin_path
  143. }
  144. if (defined(toolchain_args.coverage_instrumentation_input_file)) {
  145. toolchain_coverage_instrumentation_input_file =
  146. toolchain_args.coverage_instrumentation_input_file
  147. } else {
  148. toolchain_coverage_instrumentation_input_file =
  149. coverage_instrumentation_input_file
  150. }
  151. _use_clang_coverage_wrapper =
  152. toolchain_coverage_instrumentation_input_file != ""
  153. if (_use_clang_coverage_wrapper) {
  154. _coverage_wrapper =
  155. rebase_path("//build/toolchain/clang_code_coverage_wrapper.py",
  156. root_build_dir) + " --files-to-instrument=" +
  157. rebase_path(toolchain_coverage_instrumentation_input_file,
  158. root_build_dir) + " --target-os=" + target_os
  159. cc = "$python_path $_coverage_wrapper ${cc}"
  160. cxx = "$python_path $_coverage_wrapper ${cxx}"
  161. }
  162. linker_driver =
  163. "TOOL_VERSION=${tool_versions.linker_driver} " +
  164. rebase_path("//build/toolchain/apple/linker_driver.py", root_build_dir)
  165. # Specify an explicit path for the strip binary.
  166. _strippath = invoker.bin_path + "strip"
  167. _installnametoolpath = invoker.bin_path + "install_name_tool"
  168. linker_driver += " -Wcrl,strippath,${_strippath} -Wcrl,installnametoolpath,${_installnametoolpath}"
  169. # On iOS, the final applications are assembled using lipo (to support fat
  170. # builds). The correct flags are passed to the linker_driver.py script
  171. # directly during the lipo call. The test is against the target_os because
  172. # there is no need to create .dSYMs for targets compiled for the host.
  173. if (defined(invoker.strip_with_lipo) && invoker.strip_with_lipo) {
  174. _enable_dsyms = false
  175. _save_unstripped_output = false
  176. } else {
  177. _enable_dsyms = enable_dsyms
  178. _save_unstripped_output = save_unstripped_output
  179. }
  180. # Make these apply to all tools below.
  181. lib_switch = "-l"
  182. lib_dir_switch = "-L"
  183. # Object files go in this directory. Use label_name instead of
  184. # target_output_name since labels will generally have no spaces and will be
  185. # unique in the directory.
  186. object_subdir = "{{target_out_dir}}/{{label_name}}"
  187. # If dSYMs are enabled, this flag will be added to the link tools.
  188. if (_enable_dsyms) {
  189. dsym_switch = " -Wcrl,dsym,{{root_out_dir}} "
  190. dsym_switch += "-Wcrl,dsymutilpath," +
  191. rebase_path("//tools/clang/dsymutil/bin/dsymutil",
  192. root_build_dir) + " "
  193. dsym_output_dir =
  194. "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.dSYM"
  195. dsym_output = [
  196. "$dsym_output_dir/Contents/Info.plist",
  197. "$dsym_output_dir/Contents/Resources/DWARF/" +
  198. "{{target_output_name}}{{output_extension}}",
  199. ]
  200. } else {
  201. dsym_switch = ""
  202. }
  203. if (_save_unstripped_output) {
  204. _unstripped_output = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.unstripped"
  205. }
  206. if (toolchain_has_rust) {
  207. if (!defined(rust_compiler_prefix)) {
  208. rust_compiler_prefix = ""
  209. }
  210. rustc_bin = rebase_path("${rust_sysroot}/bin/rustc", root_build_dir)
  211. rustc = "$rust_compiler_prefix${rustc_bin}"
  212. rust_sysroot_relative_to_out = rebase_path(rust_sysroot, root_out_dir)
  213. rustc_wrapper = rebase_path("//build/rust/rustc_wrapper.py")
  214. # Ideally, we'd add -Clink-args=\"{{ldflags}}\" to each of the Rust
  215. # tools below which may link (i.e. rust_bin, rust_cdylib, rust_macro).
  216. # However, it seems -fuse-ld=lld causes difficulties.
  217. tool("rust_staticlib") {
  218. rust_outfile = "{{output_dir}}/{{target_output_name}}.a"
  219. depfile = "{{output}}.d"
  220. rspfile = "$rust_outfile.rsp"
  221. rspfile_content = "{{rustdeps}} {{externs}}"
  222. command = "$python_path \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"$_cxx\" $rustc_common_args --emit=dep-info=$depfile,link -o $rust_outfile LDFLAGS RUSTENV {{rustenv}}"
  223. description = "RUST $rust_outfile"
  224. rust_sysroot = rust_sysroot_relative_to_out
  225. outputs = [ rust_outfile ]
  226. }
  227. tool("rust_rlib") {
  228. rust_outfile = "{{output_dir}}/lib{{target_output_name}}.rlib"
  229. depfile = "{{output}}.d"
  230. # Do not use rsp files in this (common) case because they occupy the
  231. # ninja main thread, and {{rlibs}} have shorter command lines than
  232. # fully linked targets.
  233. command = "$python_path \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile -- -Clinker=\"$_cxx\" $rustc_common_args {{rustdeps}} {{externs}} --emit=dep-info=$depfile,link -o $rust_outfile LDFLAGS RUSTENV {{rustenv}}"
  234. description = "RUST $rust_outfile"
  235. rust_sysroot = rust_sysroot_relative_to_out
  236. outputs = [ rust_outfile ]
  237. }
  238. if (toolchain_args.rustc_can_link) {
  239. tool("rust_bin") {
  240. rust_outfile = "{{root_out_dir}}/{{target_output_name}}"
  241. depfile = "{{output}}.d"
  242. rspfile = "$rust_outfile.rsp"
  243. rspfile_content = "{{rustdeps}} {{externs}}"
  244. command = "$python_path \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${_cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $rust_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"
  245. description = "RUST $rust_outfile"
  246. rust_sysroot = rust_sysroot_relative_to_out
  247. outputs = [ rust_outfile ]
  248. }
  249. tool("rust_cdylib") {
  250. rust_outfile = "{{output_dir}}/lib{{target_output_name}}.dylib"
  251. depfile = "{{output}}.d"
  252. rspfile = "$rust_outfile.rsp"
  253. rspfile_content = "{{rustdeps}} {{externs}}"
  254. command = "$python_path \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${_cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $rust_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"
  255. description = "RUST $rust_outfile"
  256. rust_sysroot = rust_sysroot_relative_to_out
  257. outputs = [ rust_outfile ]
  258. }
  259. tool("rust_macro") {
  260. rust_outfile = "{{output_dir}}/lib{{target_output_name}}.dylib"
  261. depfile = "{{output}}.d"
  262. rspfile = "$rust_outfile.rsp"
  263. rspfile_content = "{{rustdeps}} {{externs}}"
  264. command = "$python_path \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${_cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $rust_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"
  265. description = "RUST $rust_outfile"
  266. rust_sysroot = rust_sysroot_relative_to_out
  267. outputs = [ rust_outfile ]
  268. }
  269. }
  270. }
  271. tool("cc") {
  272. depfile = "{{output}}.d"
  273. precompiled_header_type = "gcc"
  274. command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
  275. depsformat = "gcc"
  276. description = "CC {{output}}"
  277. outputs = [ "$object_subdir/{{source_name_part}}.o" ]
  278. }
  279. tool("cxx") {
  280. depfile = "{{output}}.d"
  281. precompiled_header_type = "gcc"
  282. command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
  283. depsformat = "gcc"
  284. description = "CXX {{output}}"
  285. outputs = [ "$object_subdir/{{source_name_part}}.o" ]
  286. }
  287. tool("asm") {
  288. # For GCC we can just use the C compiler to compile assembly.
  289. depfile = "{{output}}.d"
  290. command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
  291. depsformat = "gcc"
  292. description = "ASM {{output}}"
  293. outputs = [ "$object_subdir/{{source_name_part}}.o" ]
  294. }
  295. tool("objc") {
  296. depfile = "{{output}}.d"
  297. precompiled_header_type = "gcc"
  298. command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}"
  299. depsformat = "gcc"
  300. description = "OBJC {{output}}"
  301. outputs = [ "$object_subdir/{{source_name_part}}.o" ]
  302. }
  303. tool("objcxx") {
  304. depfile = "{{output}}.d"
  305. precompiled_header_type = "gcc"
  306. command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objcc}} -c {{source}} -o {{output}}"
  307. depsformat = "gcc"
  308. description = "OBJCXX {{output}}"
  309. outputs = [ "$object_subdir/{{source_name_part}}.o" ]
  310. }
  311. tool("alink") {
  312. rspfile = "{{output}}.rsp"
  313. rspfile_content = "{{inputs}}"
  314. if (!toolchain_uses_lld) {
  315. script = rebase_path("//build/toolchain/apple/filter_libtool.py",
  316. root_build_dir)
  317. # Specify explicit path for libtool.
  318. libtool = invoker.bin_path + "libtool"
  319. command = "rm -f {{output}} && TOOL_VERSION=${tool_versions.filter_libtool} $python_path $script $libtool -static -D {{arflags}} -o {{output}} @$rspfile"
  320. description = "LIBTOOL-STATIC {{output}}"
  321. } else {
  322. ar = "${prefix}llvm-ar"
  323. command = "\"$ar\" {{arflags}} -r -c -s -D {{output}} @$rspfile"
  324. # Remove the output file first so that ar doesn't try to modify the
  325. # existing file.
  326. command = "rm -f {{output}} && $command"
  327. description = "AR {{output}}"
  328. }
  329. outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
  330. default_output_dir = "{{target_out_dir}}"
  331. default_output_extension = ".a"
  332. output_prefix = "lib"
  333. }
  334. tool("solink") {
  335. # E.g. "./libfoo.dylib":
  336. dylib = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
  337. rspfile = dylib + ".rsp"
  338. pool = "//build/toolchain:link_pool($default_toolchain)"
  339. # These variables are not built into GN but are helpers that implement
  340. # (1) linking to produce a .dylib, (2) extracting the symbols from that
  341. # file to a temporary file, (3) if the temporary file has differences from
  342. # the existing .TOC file, overwrite it, otherwise, don't change it.
  343. #
  344. # As a special case, if the library reexports symbols from other dynamic
  345. # libraries, we always update the .TOC and skip the temporary file and
  346. # diffing steps, since that library always needs to be re-linked.
  347. tocname = dylib + ".TOC"
  348. temporary_tocname = dylib + ".tmp"
  349. # Use explicit paths to binaries. The binaries present on the default
  350. # search path in /usr/bin are thin wrappers around xcrun, which requires a
  351. # full CommandLineTools or Xcode install, and still may not choose the
  352. # appropriate binary if there are multiple installs.
  353. if (host_os == "mac") {
  354. nm = invoker.bin_path + "nm"
  355. otool = invoker.bin_path + "otool"
  356. } else {
  357. nm = "${prefix}llvm-nm"
  358. otool = "${prefix}llvm-otool"
  359. }
  360. does_reexport_command = "[ ! -e \"$dylib\" -o ! -e \"$tocname\" ] || $otool -l \"$dylib\" | grep -q LC_REEXPORT_DYLIB"
  361. link_command = "$linker_driver $ld -shared "
  362. if (toolchain_is_component_build) {
  363. link_command += " -Wl,-install_name,@rpath/\"{{target_output_name}}{{output_extension}}\" "
  364. }
  365. link_command += dsym_switch
  366. link_command += "{{ldflags}} -o \"$dylib\" \"@$rspfile\""
  367. replace_command = "if ! cmp -s \"$temporary_tocname\" \"$tocname\"; then mv \"$temporary_tocname\" \"$tocname\""
  368. extract_toc_command = "{ $otool -l \"$dylib\" | grep LC_ID_DYLIB -A 5; $nm -gPp \"$dylib\" | cut -f1-2 -d' ' | grep -v U\$\$; true; }"
  369. command = "if $does_reexport_command ; then $link_command && $extract_toc_command > \"$tocname\"; else $link_command && $extract_toc_command > \"$temporary_tocname\" && $replace_command ; fi; fi"
  370. rspfile_content = "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}} {{rlibs}}"
  371. description = "SOLINK {{output}}"
  372. # Use this for {{output_extension}} expansions unless a target manually
  373. # overrides it (in which case {{output_extension}} will be what the target
  374. # specifies).
  375. default_output_dir = "{{root_out_dir}}"
  376. default_output_extension = ".dylib"
  377. output_prefix = "lib"
  378. # Since the above commands only updates the .TOC file when it changes, ask
  379. # Ninja to check if the timestamp actually changed to know if downstream
  380. # dependencies should be recompiled.
  381. restat = true
  382. # Tell GN about the output files. It will link to the dylib but use the
  383. # tocname for dependency management.
  384. outputs = [
  385. dylib,
  386. tocname,
  387. ]
  388. link_output = dylib
  389. depend_output = tocname
  390. if (_enable_dsyms) {
  391. outputs += dsym_output
  392. }
  393. if (_save_unstripped_output) {
  394. outputs += [ _unstripped_output ]
  395. }
  396. }
  397. tool("solink_module") {
  398. # E.g. "./libfoo.so":
  399. sofile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
  400. rspfile = sofile + ".rsp"
  401. pool = "//build/toolchain:link_pool($default_toolchain)"
  402. link_command =
  403. "$linker_driver $ld -bundle {{ldflags}} -o \"$sofile\" \"@$rspfile\""
  404. link_command += dsym_switch
  405. command = link_command
  406. rspfile_content = "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}} {{rlibs}}"
  407. description = "SOLINK_MODULE {{output}}"
  408. # Use this for {{output_extension}} expansions unless a target manually
  409. # overrides it (in which case {{output_extension}} will be what the target
  410. # specifies).
  411. default_output_dir = "{{root_out_dir}}"
  412. default_output_extension = ".so"
  413. outputs = [ sofile ]
  414. if (_enable_dsyms) {
  415. outputs += dsym_output
  416. }
  417. if (_save_unstripped_output) {
  418. outputs += [ _unstripped_output ]
  419. }
  420. }
  421. tool("link") {
  422. outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
  423. rspfile = "$outfile.rsp"
  424. pool = "//build/toolchain:link_pool($default_toolchain)"
  425. command = "$linker_driver $ld $dsym_switch {{ldflags}} -o \"$outfile\" \"@$rspfile\""
  426. description = "LINK $outfile"
  427. rspfile_content = "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}} {{rlibs}}"
  428. outputs = [ outfile ]
  429. if (_enable_dsyms) {
  430. outputs += dsym_output
  431. }
  432. if (_save_unstripped_output) {
  433. outputs += [ _unstripped_output ]
  434. }
  435. default_output_dir = "{{root_out_dir}}"
  436. }
  437. # These two are really entirely generic, but have to be repeated in
  438. # each toolchain because GN doesn't allow a template to be used here.
  439. # See //build/toolchain/toolchain.gni for details.
  440. tool("stamp") {
  441. command = stamp_command
  442. description = stamp_description
  443. }
  444. tool("copy") {
  445. command = copy_command
  446. description = copy_description
  447. }
  448. tool("copy_bundle_data") {
  449. # copy_command use hardlink if possible but this does not work with
  450. # directories. Also when running EG2 tests from Xcode, Xcode tries to
  451. # copy some files into the application bundle which fails if source
  452. # and destination are hardlinked together.
  453. #
  454. # Instead use clonefile to copy the files which is as efficient as
  455. # hardlink but ensure the file have distinct metadata (thus avoid the
  456. # error with ditto, see https://crbug.com/1042182).
  457. if (host_os == "mac") {
  458. command = "rm -rf {{output}} && /bin/cp -Rc {{source}} {{output}}"
  459. } else {
  460. command = "rm -rf {{output}} && /bin/cp -Rl {{source}} {{output}}"
  461. }
  462. description = "COPY_BUNDLE_DATA {{source}} {{output}}"
  463. pool = "//build/toolchain/apple:bundle_pool($default_toolchain)"
  464. }
  465. # Swift is only used on iOS, not macOS. We want to minimize the number
  466. # of Xcode-based tools used by the macOS toolchain, so we intentionally
  467. # disallow future uses of Swift on macOS. https://crbug.com/965663.
  468. if (toolchain_args.current_os == "ios") {
  469. tool("swift") {
  470. _tool = rebase_path("//build/toolchain/ios/swiftc.py", root_build_dir)
  471. depfile = "{{target_out_dir}}/{{module_name}}.d"
  472. depsformat = "gcc"
  473. outputs = [
  474. # The module needs to be the first output listed. The blank line after
  475. # the module is required to prevent `gn format` from changing the file
  476. # order.
  477. "{{target_gen_dir}}/{{module_name}}.swiftmodule",
  478. "{{target_gen_dir}}/{{target_output_name}}.h",
  479. "{{target_gen_dir}}/{{module_name}}.swiftdoc",
  480. "{{target_gen_dir}}/{{module_name}}.swiftsourceinfo",
  481. ]
  482. if (swift_whole_module_optimization) {
  483. _extra_flags = "-whole-module-optimization"
  484. _objects_dir = "{{target_out_dir}}"
  485. outputs += [ "$_objects_dir/{{module_name}}.o" ]
  486. } else {
  487. _extra_flags = ""
  488. _objects_dir = "{{target_out_dir}}/{{label_name}}"
  489. partial_outputs = [ "$_objects_dir/{{source_name_part}}.o" ]
  490. }
  491. _env_vars = "TOOL_VERSION=${tool_versions.swiftc}"
  492. if (invoker.sdk_developer_dir != "") {
  493. _env_vars += " DEVELOPER_DIR=${toolchain_args.sdk_developer_dir}"
  494. }
  495. # Starting with version 5.6, the Swift compiler will always
  496. # generates precompiled headers. In anterior version, it was
  497. # used when bridging headers and whole module optimisation
  498. # where enabled, and it could be disabled with the parameter
  499. # `-disable-bridging-pch`.
  500. #
  501. # The precompiled headers are binary files (i.e. they are not
  502. # regular Objective-C header files and cannot be loaded as such).
  503. #
  504. # There is an hidden requirements that the compiler needs to
  505. # be told where to save those .pch files (via the parameter
  506. # `-pch-output-dir $dir`). If this parameter is not passed, the
  507. # compiler will silently write them at an incorrect location,
  508. # leading later pass to try to load those .pch files as either
  509. # regular header files (.h) or object files (.o) and causing
  510. # to compilation failures.
  511. #
  512. # List the directory where the precompiled header is generated
  513. # as an output, but do not list the .pch file itself. This is
  514. # because the names includes two hashes (one corresponding to
  515. # the compiler revision, and the other probably derived from
  516. # the module itself) that are difficult to generate.
  517. #
  518. # Still we want to avoid creating a directory that has the same
  519. # name as a file generated by another rule, so explicitly list
  520. # the directory in `outputs` so that gn can warn it is conflicts
  521. # with another output file.
  522. _pch_output_dir = "{{target_out_dir}}/{{module_name}}:pch/"
  523. outputs += [ _pch_output_dir ]
  524. command =
  525. "$_env_vars $python_path $_tool -module-name {{module_name}} " +
  526. "-root-dir " + rebase_path("//", root_build_dir) + " " +
  527. "-object-dir $_objects_dir -pch-output-dir $_pch_output_dir " +
  528. "-module-path {{target_gen_dir}}/{{module_name}}.swiftmodule " +
  529. "-header-path {{target_gen_dir}}/{{target_output_name}}.h " +
  530. "-depfile {{target_out_dir}}/{{module_name}}.d " +
  531. "-depfile-filter {{target_gen_dir}}/{{module_name}}.swiftmodule " +
  532. "-bridge-header {{bridge_header}} $_extra_flags " +
  533. "{{swiftflags}} {{include_dirs}} {{module_dirs}} {{inputs}}"
  534. }
  535. }
  536. # xcassets are only used on iOS, not macOS. We want to minimize the number
  537. # of Xcode-based tools used by the macOS toolchain, so we intentionally
  538. # disallow future uses of xcassets on macOS. https://crbug.com/965663.
  539. if (toolchain_args.current_os == "ios") {
  540. tool("compile_xcassets") {
  541. _tool = rebase_path("//build/toolchain/ios/compile_xcassets.py",
  542. root_build_dir)
  543. _env_vars = "TOOL_VERSION=${tool_versions.compile_xcassets}"
  544. if (invoker.sdk_developer_dir != "") {
  545. _env_vars += " DEVELOPER_DIR=${toolchain_args.sdk_developer_dir}"
  546. }
  547. command =
  548. "$_env_vars $python_path $_tool -p \"${invoker.sdk_name}\" " +
  549. "-t \"${invoker.deployment_target}\" " +
  550. "-T \"{{bundle_product_type}}\" " +
  551. "-P \"{{bundle_partial_info_plist}}\" " + "-o {{output}} {{inputs}}"
  552. description = "COMPILE_XCASSETS {{output}}"
  553. pool = "//build/toolchain/apple:bundle_pool($default_toolchain)"
  554. }
  555. }
  556. tool("action") {
  557. pool = "//build/toolchain:action_pool($default_toolchain)"
  558. }
  559. }
  560. }