1 # Permission is hereby granted, free of charge, to any person obtaining a copy
2 # of this software and associated documentation files (the "Software"), to deal
3 # in the Software without restriction, including without limitation the rights
4 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
5 # copies of the Software, and to permit persons to whom the Software is
6 # furnished to do so, subject to the following conditions:
8 # The above copyright notice and this permission notice shall be included in
9 # all copies or substantial portions of the Software.
11 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 # GN host architecture helpers.
21 # BUILD_ARCH's value corresponds to what uname returns as the machine name.
22 # The mapping in gn_host_arch_name() tries to match several possible values
23 # returned by the Linux kernel in uname(2) into the corresponding values GN
25 def gn_host_arch_name(d):
26 """Returns a GN architecture name corresponding to the build host's machine
30 r'aarch64.*': 'arm64',
35 build_arch = d.getVar("BUILD_ARCH")
36 for arch_regexp, gn_arch_name in arch_translations.items():
37 if re.match(arch_regexp, build_arch):
39 bb.fatal('Unsuported BUILD_ARCH value: "%s"' % build_arch)
41 # GN target architecture helpers.
43 # Determining the target architecture is more difficult, as there are many
44 # different values we can use on the Yocto side (e.g. TUNE_ARCH, TARGET_ARCH,
45 # MACHINEOVERRIDES etc). What we do is define the mapping with regular,
46 # non-Python variables with overrides that are generic enough (i.e. "x86"
47 # instead of "i586") and then use gn_target_arch_name() to return the right
48 # value with some validation.
49 GN_TARGET_ARCH_NAME_aarch64 = "arm64"
50 GN_TARGET_ARCH_NAME_arm = "arm"
51 GN_TARGET_ARCH_NAME_x86 = "x86"
52 GN_TARGET_ARCH_NAME_x86-64 = "x64"
54 BUILD_CC_toolchain-clang = "clang"
55 BUILD_CXX_toolchain-clang = "clang++"
56 BUILD_LD_toolchain-clang = "clang"
58 # knob for clang, when using meta-clang to provide clang and case where
59 # clang happens to be default compiler for OE we should let it use clang
60 def is_default_cc_clang(d):
61 """Return true if clang is default cross compiler."""
62 toolchain = d.getVar("TOOLCHAIN")
63 overrides = d.getVar("OVERRIDES")
64 if toolchain == "clang" and "toolchain-clang" in overrides.split(":"):
68 def clang_install_path(d):
69 """Return clang compiler install path."""
70 return d.getVar("STAGING_BINDIR_NATIVE")
72 def gn_target_arch_name(d):
73 """Returns a GN architecture name corresponding to the target machine's
75 name = d.getVar("GN_TARGET_ARCH_NAME")
77 bb.fatal('Unsupported target architecture. A valid override for the '
78 'GN_TARGET_ARCH_NAME variable could not be found.')
81 def write_toolchain_file(d, file_path):
82 """Creates a complete GN toolchain file in |file_path|."""
84 gcc_toolchain_tmpl = string.Template(
85 'gcc_toolchain("${toolchain_name}") {\n'
89 ' ld = cxx # GN expects a compiler, not a linker.\n'
91 ' readelf = "${readelf}"\n'
92 ' extra_cflags = "${extra_cflags}"\n'
93 ' extra_cppflags = "${extra_cppflags}"\n'
94 ' extra_cxxflags = "${extra_cxxflags}"\n'
95 ' extra_ldflags = "${extra_ldflags}"\n'
96 ' toolchain_args = {\n'
97 ' current_cpu = "${current_cpu}"\n'
98 ' current_os = "linux"\n'
103 clang_toolchain_tmpl = string.Template(
104 'clang_toolchain("clang_${toolchain_name}") {\n'
105 ' extra_cflags = "${extra_cflags}"\n'
106 ' extra_cppflags = "${extra_cppflags}"\n'
107 ' extra_cxxflags = "${extra_cxxflags}"\n'
108 ' extra_ldflags = "${extra_ldflags}"\n'
109 ' toolchain_args = {\n'
110 ' current_cpu = "${current_cpu}"\n'
111 ' current_os = "linux"\n'
119 'toolchain_name': 'yocto_native',
120 'current_cpu': gn_host_arch_name(d),
121 'cc': d.expand('${BUILD_CC}'),
122 'cxx': d.expand('${BUILD_CXX}'),
123 'ar': d.expand('${BUILD_AR}'),
124 'nm': d.expand('${BUILD_NM}'),
125 'readelf': d.expand('${BUILD_PREFIX}readelf'),
126 'extra_cflags': d.expand('${BUILD_CFLAGS}'),
127 'extra_cppflags': d.expand('${BUILD_CPPFLAGS}'),
128 'extra_cxxflags': d.expand('${BUILD_CXXFLAGS}'),
129 'extra_ldflags': d.expand('${BUILD_LDFLAGS}'),
132 'toolchain_name': 'yocto_target',
133 'current_cpu': gn_target_arch_name(d),
134 'cc': d.expand('${CC}'),
135 'cxx': d.expand('${CXX}'),
136 'ar': d.expand('${AR}'),
137 'nm': d.expand('${NM}'),
138 'readelf': d.expand('${TARGET_PREFIX}readelf'),
139 'extra_cflags': d.expand('${TARGET_CFLAGS}'),
140 'extra_cppflags': d.expand('${TARGET_CPPFLAGS}'),
141 'extra_cxxflags': d.expand('${TARGET_CXXFLAGS}'),
142 'extra_ldflags': d.expand('${TARGET_LDFLAGS}'),
146 with open(file_path, 'w') as toolchain_file:
147 toolchain_file.write(
148 '# This file has been generated automatically.\n'
150 'import("//build/config/sysroot.gni")\n'
151 'import("//build/toolchain/gcc_toolchain.gni")\n'
154 toolchain_file.write(gcc_toolchain_tmpl.substitute(native_toolchain))
155 toolchain_file.write(gcc_toolchain_tmpl.substitute(target_toolchain))
156 toolchain_file.write(clang_toolchain_tmpl.substitute(native_toolchain))
157 toolchain_file.write(clang_toolchain_tmpl.substitute(target_toolchain))