summaryrefslogtreecommitdiffstats
path: root/build/toolchain/gcc_toolchain.gni
blob: a6b3add86d6cc360e785b451ff12c94134f4386f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/nacl/config.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build/toolchain/cc_wrapper.gni")
import("//build/toolchain/goma.gni")
import("//build/toolchain/toolchain.gni")

# This value will be inherited in the toolchain below.
if (is_lto) {
  concurrent_links =
      exec_script("get_concurrent_links.py", [ "--lto" ], "value")
} else {
  concurrent_links = exec_script("get_concurrent_links.py", [], "value")
}

# This template defines a toolchain for something that works like gcc
# (including clang).
#
# It requires the following variables specifying the executables to run:
#  - cc
#  - cxx
#  - ar
#  - ld
# and the following which is used in the toolchain_args
#  - toolchain_cpu  (What "current_cpu" should be set to when invoking a
#                    build using this toolchain.)
#  - toolchain_os  (What "current_os" should be set to when invoking a
#                   build using this toolchain.)
#
# Optional parameters that control the tools:
#
#  - libs_section_prefix
#  - libs_section_postfix
#      The contents of these strings, if specified, will be placed around
#      the libs section of the linker line. It allows one to inject libraries
#      at the beginning and end for all targets in a toolchain.
#  - solink_libs_section_prefix
#  - solink_libs_section_postfix
#      Same as libs_section_{pre,post}fix except used for solink instead of link.
#  - link_outputs
#      The content of this array, if specified, will be added to the list of
#      outputs from the link command. This can be useful in conjunction with
#      the post_link parameter.
#  - post_link
#      The content of this string, if specified, will be run as a separate
#      command following the the link command.
#  - deps
#      Just forwarded to the toolchain definition.
#  - executable_extension
#      If this string is specified it will be used for the file extension
#      for an executable, rather than using no extension; targets will
#      still be able to override the extension using the output_extension
#      variable.
#  - rebuild_define
#      The contents of this string, if specified, will be passed as a #define
#      to the toolchain. It can be used to force recompiles whenever a
#      toolchain is updated.
#  - shlib_extension
#      If this string is specified it will be used for the file extension
#      for a shared library, rather than default value specified in
#      toolchain.gni
#  - strip
#      Location of the strip executable. When specified, strip will be run on
#      all shared libraries and executables as they are built. The pre-stripped
#      artifacts will be put in lib.unstripped/ and exe.unstripped/.
#
# Optional build argument contols.
#
#  - clear_sanitizers
#      When set to true, is_asan, is_msan, etc.will all be set to false. Often
#      secondary toolchains do not want to run with sanitizers.
#  - is_clang
#      Whether to use clang instead of gcc.
#  - is_component_build
#      Whether to forcibly enable or disable component builds for this
#      toolchain; if not specified, the toolchain will inherit the
#      default setting.
#  - is_nacl_glibc
#      Whether NaCl code is built using Glibc instead of Newlib.
#  - cc_wrapper
#      Override the global cc_wrapper setting. e.g. "ccache" or "icecc".
#      useful to opt-out of cc_wrapper in a particular toolchain by setting
#      cc_wrapper = "" in it.
#  - use_goma
#      Override the global use_goma setting, useful to opt-out of goma in a
#      particular toolchain by setting use_gome = false in it.
#  - use_gold
#      Override the global use_gold setting, useful if the particular
#      toolchain has a custom link step that is not actually using Gold.
template("gcc_toolchain") {
  toolchain(target_name) {
    assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
    assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
    assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
    assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
    assert(defined(invoker.toolchain_cpu),
           "gcc_toolchain() must specify a \"toolchain_cpu\"")
    assert(defined(invoker.toolchain_os),
           "gcc_toolchain() must specify a \"toolchain_os\"")

    if (defined(invoker.cc_wrapper)) {
      cc_wrapper = invoker.cc_wrapper
    }
    if (defined(invoker.use_goma)) {
      use_goma = invoker.use_goma
    }
    if (use_goma) {
      assert(cc_wrapper == "", "Goma and cc_wrapper can't be used together.")
      compiler_prefix = "$goma_dir/gomacc "
    } else if (cc_wrapper != "") {
      compiler_prefix = cc_wrapper + " "
    } else {
      compiler_prefix = ""
    }

    # This define changes when the toolchain changes, forcing a rebuild.
    # Nothing should ever use this define.
    if (defined(invoker.rebuild_define)) {
      rebuild_string = "-D" + invoker.rebuild_define + " "
    } else {
      rebuild_string = ""
    }

    cc = compiler_prefix + invoker.cc
    cxx = compiler_prefix + invoker.cxx
    ar = invoker.ar
    ld = invoker.ld
    if (defined(invoker.readelf)) {
      readelf = invoker.readelf
    } else {
      readelf = "readelf"
    }
    if (defined(invoker.nm)) {
      nm = invoker.nm
    } else {
      nm = "nm"
    }

    if (defined(invoker.shlib_extension)) {
      default_shlib_extension = invoker.shlib_extension
    } else {
      default_shlib_extension = shlib_extension
    }

    if (defined(invoker.executable_extension)) {
      default_executable_extension = invoker.executable_extension
    } else {
      default_executable_extension = ""
    }

    # Bring these into our scope for string interpolation with default values.
    if (defined(invoker.libs_section_prefix)) {
      libs_section_prefix = invoker.libs_section_prefix
    } else {
      libs_section_prefix = ""
    }

    if (defined(invoker.libs_section_postfix)) {
      libs_section_postfix = invoker.libs_section_postfix
    } else {
      libs_section_postfix = ""
    }

    if (defined(invoker.solink_libs_section_prefix)) {
      solink_libs_section_prefix = invoker.solink_libs_section_prefix
    } else {
      solink_libs_section_prefix = ""
    }

    if (defined(invoker.solink_libs_section_postfix)) {
      solink_libs_section_postfix = invoker.solink_libs_section_postfix
    } else {
      solink_libs_section_postfix = ""
    }

    # These library switches can apply to all tools below.
    lib_switch = "-l"
    lib_dir_switch = "-L"

    # Object files go in this directory.
    object_subdir = "{{target_out_dir}}/{{label_name}}"

    tool("cc") {
      depfile = "{{output}}.d"
      command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
      depsformat = "gcc"
      description = "CC {{output}}"
      outputs = [
        "$object_subdir/{{source_name_part}}.o",
      ]
    }

    tool("cxx") {
      depfile = "{{output}}.d"
      command = "$cxx -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
      depsformat = "gcc"
      description = "CXX {{output}}"
      outputs = [
        "$object_subdir/{{source_name_part}}.o",
      ]
    }

    tool("asm") {
      # For GCC we can just use the C compiler to compile assembly.
      depfile = "{{output}}.d"
      command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
      depsformat = "gcc"
      description = "ASM {{output}}"
      outputs = [
        "$object_subdir/{{source_name_part}}.o",
      ]
    }

    tool("alink") {
      rspfile = "{{output}}.rsp"
      arflags = ""
      if (is_lto && invoker.toolchain_os != "nacl") {
        gold_plugin_path = rebase_path(
                "//third_party/llvm-build/Release+Asserts/lib/LLVMgold.so",
                root_build_dir)
        arflags = "--plugin \"$gold_plugin_path\""
      }

      # This needs a Python script to avoid using simple sh features in this
      # command, in case the host does not use a POSIX shell (e.g. compiling
      # POSIX-like toolchains such as NaCl on Windows).
      ar_wrapper =
          rebase_path("//build/toolchain/gcc_ar_wrapper.py", root_build_dir)
      command = "$python_path \"$ar_wrapper\" --output={{output}} --ar=\"$ar\" $arflags rcsD @\"$rspfile\""
      description = "AR {{output}}"
      rspfile_content = "{{inputs}}"
      outputs = [
        "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
      ]
      default_output_extension = ".a"
      output_prefix = "lib"
    }

    tool("solink") {
      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
      sofile = "{{root_out_dir}}/$soname"  # Possibly including toolchain dir.
      if (shlib_subdir != ".") {
        sofile = "{{root_out_dir}}/$shlib_subdir/$soname"
      }
      rspfile = sofile + ".rsp"

      unstripped_sofile = sofile
      if (defined(invoker.strip)) {
        unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$soname"
      }

      # These variables are not built into GN but are helpers that
      # implement (1) linking to produce a .so, (2) extracting the symbols
      # from that file (3) if the extracted list differs from the existing
      # .TOC file, overwrite it, otherwise, don't change it.
      tocfile = sofile + ".TOC"

      link_command = "$ld -shared {{ldflags}} -o \"$unstripped_sofile\" -Wl,-soname=\"$soname\" @\"$rspfile\""

      assert(defined(readelf), "to solink you must have a readelf")
      assert(defined(nm), "to solink you must have an nm")
      strip_switch = ""
      if (defined(invoker.strip)) {
        strip_switch = "--strip=${invoker.strip}"
      }

      # This needs a Python script to avoid using a complex shell command
      # requiring sh control structures, pipelines, and POSIX utilities.
      # The host might not have a POSIX shell and utilities (e.g. Windows).
      solink_wrapper = rebase_path("//build/toolchain/gcc_solink_wrapper.py")
      command = "$python_path \"$solink_wrapper\" --readelf=\"$readelf\" --nm=\"$nm\" $strip_switch --sofile=\"$unstripped_sofile\" --tocfile=\"$tocfile\" --output=\"$sofile\" -- $link_command"

      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"

      description = "SOLINK $sofile"

      # Use this for {{output_extension}} expansions unless a target manually
      # overrides it (in which case {{output_extension}} will be what the target
      # specifies).
      default_output_extension = default_shlib_extension

      output_prefix = "lib"

      # Since the above commands only updates the .TOC file when it changes, ask
      # Ninja to check if the timestamp actually changed to know if downstream
      # dependencies should be recompiled.
      restat = true

      # Tell GN about the output files. It will link to the sofile but use the
      # tocfile for dependency management.
      outputs = [
        sofile,
        tocfile,
      ]
      if (sofile != unstripped_sofile) {
        outputs += [ unstripped_sofile ]
      }
      link_output = sofile
      depend_output = tocfile
    }

    tool("solink_module") {
      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
      sofile = "{{root_out_dir}}/$soname"  # Possibly including toolchain dir.
      if (shlib_subdir != ".") {
        sofile = "{{root_out_dir}}/$shlib_subdir/$soname"
      }
      rspfile = sofile + ".rsp"

      unstripped_sofile = sofile
      if (defined(invoker.strip)) {
        unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$soname"
      }

      command = "$ld -shared {{ldflags}} -o \"$unstripped_sofile\" -Wl,-soname=\"$soname\" @\"$rspfile\""

      if (defined(invoker.strip)) {
        strip_command = "${invoker.strip} --strip-unneeded -o \"$sofile\" \"$unstripped_sofile\""
        command += " && " + strip_command
      }
      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"

      description = "SOLINK_MODULE $sofile"

      # Use this for {{output_extension}} expansions unless a target manually
      # overrides it (in which case {{output_extension}} will be what the target
      # specifies).
      if (defined(invoker.loadable_module_extension)) {
        default_output_extension = invoker.loadable_module_extension
      } else {
        default_output_extension = default_shlib_extension
      }

      output_prefix = "lib"

      outputs = [
        sofile,
      ]
      if (sofile != unstripped_sofile) {
        outputs += [ unstripped_sofile ]
      }
    }

    tool("link") {
      exename = "{{target_output_name}}{{output_extension}}"
      outfile = "{{root_out_dir}}/$exename"
      rspfile = "$outfile.rsp"
      unstripped_outfile = outfile

      # Use this for {{output_extension}} expansions unless a target manually
      # overrides it (in which case {{output_extension}} will be what the target
      # specifies).
      default_output_extension = default_executable_extension

      if (defined(invoker.strip)) {
        unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$exename"
      }

      command = "$ld {{ldflags}} -o \"$unstripped_outfile\" -Wl,--start-group @\"$rspfile\" {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
      if (defined(invoker.strip)) {
        link_wrapper =
            rebase_path("//build/toolchain/gcc_link_wrapper.py", root_build_dir)
        command = "$python_path \"$link_wrapper\" --strip=\"${invoker.strip}\" --unstripped-file=\"$unstripped_outfile\" --output=\"$outfile\" -- $command"
      }
      description = "LINK $outfile"
      rspfile_content = "{{inputs}}"
      outputs = [
        outfile,
      ]
      if (outfile != unstripped_outfile) {
        outputs += [ unstripped_outfile ]
      }
      if (defined(invoker.link_outputs)) {
        outputs += invoker.link_outputs
      }
    }

    # These two are really entirely generic, but have to be repeated in
    # each toolchain because GN doesn't allow a template to be used here.
    # See //build/toolchain/toolchain.gni for details.
    tool("stamp") {
      command = stamp_command
      description = stamp_description
    }
    tool("copy") {
      command = copy_command
      description = copy_description
    }

    # When invoking this toolchain not as the default one, these args will be
    # passed to the build. They are ignored when this is the default toolchain.
    toolchain_args() {
      current_cpu = invoker.toolchain_cpu
      current_os = invoker.toolchain_os

      # These values need to be passed through unchanged.
      host_toolchain = host_toolchain
      target_os = target_os
      target_cpu = target_cpu

      forward_variables_from(invoker,
                             [
                               "is_clang",
                               "is_component_build",
                               "is_nacl_glibc",
                               "use_gold",
                               "symbol_level",
                             ])

      if (defined(invoker.clear_sanitizers) && invoker.clear_sanitizers) {
        is_asan = false
        is_cfi = false
        is_lsan = false
        is_msan = false
        is_syzyasan = false
        is_tsan = false
        is_ubsan = false
        is_ubsan_vptr = false
      }
    }

    forward_variables_from(invoker, [ "deps" ])
  }
}

# This is a shorthand for gcc_toolchain instances based on the
# Chromium-built version of Clang.  Only the toolchain_cpu and
# toolchain_os variables need to be specified by the invoker, and
# optionally toolprefix if it's a cross-compile case.  Note that for
# a cross-compile case this toolchain requires a config to pass the
# appropriate -target option, or else it will actually just be doing
# a native compile.  The invoker can optionally override use_gold too.
template("clang_toolchain") {
  assert(defined(invoker.toolchain_cpu),
         "clang_toolchain() must specify a \"toolchain_cpu\"")
  assert(defined(invoker.toolchain_os),
         "clang_toolchain() must specify a \"toolchain_os\"")
  if (defined(invoker.toolprefix)) {
    toolprefix = invoker.toolprefix
  } else {
    toolprefix = ""
  }

  gcc_toolchain(target_name) {
    prefix = rebase_path("//third_party/llvm-build/Release+Asserts/bin",
                         root_build_dir)
    cc = "$prefix/clang"
    cxx = "$prefix/clang++"
    ld = cxx
    is_clang = true

    readelf = "${toolprefix}readelf"
    ar = "${toolprefix}ar"
    nm = "${toolprefix}nm"

    forward_variables_from(invoker,
                           [
                             "toolchain_cpu",
                             "toolchain_os",
                             "use_gold",
                           ])
  }
}