• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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
5import("//build/config/ios/deployment_target.gni")
6
7declare_args() {
8  # Configure whether whole module optimization is enabled when compiling
9  # swift modules.
10  swift_whole_module_optimization = true
11}
12
13template("ios_toolchain") {
14  toolchain(target_name) {
15    assert(defined(invoker.toolchain_args),
16           "Toolchains must declare toolchain_args")
17
18    toolchain_args = {
19      forward_variables_from(invoker.toolchain_args, "*")
20    }
21
22    _sdk_info = exec_script("//build/config/ios/scripts/sdk_info.py",
23                            [
24                              "--target-cpu",
25                              current_cpu,
26                              "--deployment-target",
27                              ios_deployment_target,
28                            ],
29                            "json")
30
31    cc = "clang -target ${_sdk_info.target} -isysroot ${_sdk_info.sdk_path}"
32    cxx = "clang++ -target ${_sdk_info.target} -isysroot ${_sdk_info.sdk_path}"
33
34    swiftmodule_switch = "-Wl,-add_ast_path,"
35
36    tool("link") {
37      output = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
38      rspfile = output + ".rsp"
39      rspfile_content = "{{inputs_newline}}"
40
41      outputs = [ output ]
42      command = "$cxx {{ldflags}} -o $output -Wl,-filelist,$rspfile {{libs}} {{solibs}} {{frameworks}} {{swiftmodules}}"
43      description = "LINK {{output}}"
44
45      default_output_dir = "{{root_out_dir}}"
46      default_output_extension = ""
47      output_prefix = ""
48    }
49
50    tool("solink") {
51      dylib = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
52      rspfile = dylib + ".rsp"
53      rspfile_content = "{{inputs_newline}}"
54
55      outputs = [ dylib ]
56      command = "$cxx -dynamiclib {{ldflags}} -o $dylib -Wl,-filelist,$rspfile {{libs}} {{solibs}} {{frameworks}} {{swiftmodules}}"
57      description = "SOLINK {{output}}"
58
59      default_output_dir = "{{root_out_dir}}"
60      default_output_extension = ".dylib"
61      output_prefix = "lib"
62    }
63
64    tool("cc") {
65      depfile = "{{output}}.d"
66      precompiled_header_type = "gcc"
67      command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
68      depsformat = "gcc"
69      description = "CC {{output}}"
70      outputs = [ "{{target_out_dir}}/{{label_name}}/{{source_name_part}}.o" ]
71    }
72
73    tool("cxx") {
74      depfile = "{{output}}.d"
75      precompiled_header_type = "gcc"
76      command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
77      depsformat = "gcc"
78      description = "CXX {{output}}"
79      outputs = [ "{{target_out_dir}}/{{label_name}}/{{source_name_part}}.o" ]
80    }
81
82    tool("objc") {
83      depfile = "{{output}}.d"
84      precompiled_header_type = "gcc"
85      command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}"
86      depsformat = "gcc"
87      description = "OBJC {{output}}"
88      outputs = [ "{{target_out_dir}}/{{label_name}}/{{source_name_part}}.o" ]
89    }
90
91    tool("objcxx") {
92      depfile = "{{output}}.d"
93      precompiled_header_type = "gcc"
94      command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objcc}} -c {{source}} -o {{output}}"
95      depsformat = "gcc"
96      description = "OBJCXX {{output}}"
97      outputs = [ "{{target_out_dir}}/{{label_name}}/{{source_name_part}}.o" ]
98    }
99
100    tool("stamp") {
101      command = "touch {{output}}"
102      description = "STAMP {{output}}"
103    }
104
105    tool("copy_bundle_data") {
106      command = "rm -rf {{output}} && cp -PR {{source}} {{output}}"
107      description = "COPY_BUNDLE_DATA {{output}}"
108    }
109
110    tool("swift") {
111      depfile = "{{target_out_dir}}/{{module_name}}.d"
112      depsformat = "gcc"
113
114      outputs = [
115        # The module needs to be the first output to ensure the
116        # depfile generate works correctly with ninja < 1.9.0.
117        "{{target_gen_dir}}/{{module_name}}.swiftmodule",
118
119        "{{target_gen_dir}}/{{module_name}}.h",
120        "{{target_gen_dir}}/{{module_name}}.swiftdoc",
121        "{{target_gen_dir}}/{{module_name}}.swiftsourceinfo",
122      ]
123
124      if (swift_whole_module_optimization) {
125        _extra_flag = "--whole-module-optimization"
126        _object_dir = "{{target_out_dir}}"
127        outputs += [ "{{target_out_dir}}/{{module_name}}.o" ]
128      } else {
129        _extra_flag = ""
130        _object_dir = "{{target_out_dir}}/{{label_name}}"
131        partial_outputs =
132            [ "{{target_out_dir}}/{{label_name}}/{{source_name_part}}.o" ]
133      }
134
135      _swiftc = rebase_path("//build/toolchain/apple/swiftc.py", root_build_dir)
136      command = "$_swiftc --target ${_sdk_info.target} --sdk ${_sdk_info.sdk_path} --module-name {{module_name}} --object-dir $_object_dir --module-path {{target_gen_dir}}/{{module_name}}.swiftmodule --header-path {{target_gen_dir}}/{{module_name}}.h --depfile {{target_out_dir}}/{{module_name}}.d --depfile-filter {{target_gen_dir}}/{{module_name}}.swiftmodule --bridge-header {{bridge_header}} $_extra_flag {{defines}} {{swiftflags}} {{include_dirs}} {{module_dirs}} {{inputs}}"
137    }
138  }
139}
140
141ios_toolchain("clang_x86") {
142  toolchain_args = {
143    current_cpu = "x86"
144    current_os = "ios"
145  }
146}
147
148ios_toolchain("clang_x64") {
149  toolchain_args = {
150    current_cpu = "x64"
151    current_os = "ios"
152  }
153}
154
155ios_toolchain("clang_arm") {
156  toolchain_args = {
157    current_cpu = "arm"
158    current_os = "ios"
159  }
160}
161
162ios_toolchain("clang_arm64") {
163  toolchain_args = {
164    current_cpu = "arm64"
165    current_os = "ios"
166  }
167}
168