summaryrefslogtreecommitdiffstats
path: root/build/toolchain/gcc_toolchain.gni
blob: a6d47288799c3e16fcb74558afbea3c23958f0cd (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
# 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.

# This template defines a GCC toolchain.
#
# 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_arch  (What "cpu_arch" should be set to when invoking a
#                         build using this toolchain.)
#  - toolchain_os  (What "os" should be set to when invoking a build using this
#                   toolchain.)
#
# Optional parameters:
#  - 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.
#  - deps
#      Just fowarded to the toolchain definition.
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_arch),
           "gcc_toolchain() must specify a \"toolchain_cpu_arch\"")
    assert(defined(invoker.toolchain_os),
           "gcc_toolchain() must specify a \"toolchain_os\"")

    # We can't do string interpolation ($ in strings) on things with dots in
    # them. To allow us to use $cc below, for example, we create copies of
    # these values in our scope.
    cc = invoker.cc
    cxx = invoker.cxx
    ar = invoker.ar
    ld = invoker.ld

    # 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 = ""
    }

    # Make these apply to all tools below.
    lib_prefix = "-l"
    lib_dir_prefix="-L"

    tool("cc") {
      # cflags_pch_c
      command = "$cc -MMD -MF \$out.d \$defines \$includes \$cflags \$cflags_c -c \$in -o \$out"
      description = "CC \$out"
      depfile = "\$out.d"
      depsformat = "gcc"
    }
    tool("cxx") {
      # cflags_pch_cc
      command = "$cxx -MMD -MF \$out.d \$defines \$includes \$cflags \$cflags_cc -c \$in -o \$out"
      description = "CXX \$out"
      depfile = "\$out.d"
      depsformat = "gcc"
    }
    tool("alink") {
      command = "rm -f \$out && $ar rcs \$out @\$rspfile"
      description = "AR \$out"
      rspfile = "\$out.rsp"
      rspfile_content = "\$in"
    }
    tool("solink") {
      command = "if [ ! -e \$lib -o ! -e \${lib}.TOC ]; then $ld -shared \$ldflags -o \$lib -Wl,-soname=\$soname @\$rspfile && { readelf -d \${lib} | grep SONAME ; nm -gD -f p \${lib} | cut -f1-2 -d' '; } > \${lib}.TOC; else $ld -shared \$ldflags -o \$lib -Wl,-soname=\$soname -Wl,--whole-archive \$in \$solibs -Wl,--no-whole-archive $libs_section_prefix \$libs $libs_section_postfix && { readelf -d \${lib} | grep SONAME ; nm -gD -f p \${lib} | cut -f1-2 -d' '; } > \${lib}.tmp && if ! cmp -s \${lib}.tmp \${lib}.TOC; then mv \${lib}.tmp \${lib}.TOC ; fi; fi"
      description = "SOLINK \$lib"
      rspfile = "\$out.rsp"
      rspfile_content = "-Wl,--whole-archive \$in \$solibs -Wl,--no-whole-archive \$libs"
      #pool = "link_pool"
      restat = "1"
    }
    tool("link") {
      command = "$ld \$ldflags -o \$out -Wl,--start-group @\$rspfile \$solibs -Wl,--end-group $libs_section_prefix \$libs $libs_section_postfix"
      description = "LINK \$out"
      rspfile = "\$out.rsp"
      rspfile_content = "\$in"
      #pool = "link_pool"
    }
    tool("stamp") {
      command = "\${postbuilds}touch \$out"
      description = "STAMP \$out"
    }
    tool("copy") {
      command = "ln -f \$in \$out 2>/dev/null || (rm -rf \$out && cp -af \$in \$out)"
      description = "COPY \$in \$out"
    }

    # 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() {
      cpu_arch = invoker.toolchain_cpu_arch
      os = invoker.toolchain_os
    }

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
  }
}