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 def gn_arch_name(yocto_arch):
20 """Translates between Yocto's architecture values and the corresponding
29 return translation_table[yocto_arch]
31 bb.msg.fatal('"%s" is not a supported architecture.' % yocto_arch)
34 def write_toolchain_file(d, file_path):
35 """Creates a complete GN toolchain file in |file_path|."""
37 gcc_toolchain_tmpl = string.Template(
38 'gcc_toolchain("${toolchain_name}") {\n'
42 ' ld = cxx # GN expects a compiler, not a linker.\n'
44 ' readelf = "${readelf}"\n'
45 ' extra_cflags = "${extra_cflags}"\n'
46 ' extra_cppflags = "${extra_cppflags}"\n'
47 ' extra_cxxflags = "${extra_cxxflags}"\n'
48 ' extra_ldflags = "${extra_ldflags}"\n'
49 ' toolchain_args = {\n'
50 ' current_cpu = "${current_cpu}"\n'
51 ' current_os = "linux"\n'
56 clang_toolchain_tmpl = string.Template(
57 'clang_toolchain("clang_${toolchain_name}") {\n'
58 ' extra_cflags = "${extra_cflags}"\n'
59 ' extra_cppflags = "${extra_cppflags}"\n'
60 ' extra_cxxflags = "${extra_cxxflags}"\n'
61 ' extra_ldflags = "${extra_ldflags}"\n'
62 ' toolchain_args = {\n'
63 ' current_cpu = "${current_cpu}"\n'
64 ' current_os = "linux"\n'
72 'toolchain_name': 'yocto_native',
73 'current_cpu': gn_arch_name(d.getVar('BUILD_ARCH', True)),
74 'cc': d.expand('${BUILD_CC}'),
75 'cxx': d.expand('${BUILD_CXX}'),
76 'ar': d.expand('${BUILD_AR}'),
77 'nm': d.expand('${BUILD_NM}'),
78 'readelf': d.expand('${BUILD_PREFIX}readelf'),
79 'extra_cflags': d.expand('${BUILD_CFLAGS}'),
80 'extra_cppflags': d.expand('${BUILD_CPPFLAGS}'),
81 'extra_cxxflags': d.expand('${BUILD_CXXFLAGS}'),
82 'extra_ldflags': d.expand('${BUILD_LDFLAGS}'),
85 'toolchain_name': 'yocto_target',
86 'current_cpu': gn_arch_name(d.getVar('TUNE_ARCH', True)),
87 'cc': d.expand('${CC}'),
88 'cxx': d.expand('${CXX}'),
89 'ar': d.expand('${AR}'),
90 'nm': d.expand('${NM}'),
91 'readelf': d.expand('${TARGET_PREFIX}readelf'),
92 'extra_cflags': d.expand('${TARGET_CFLAGS}'),
93 'extra_cppflags': d.expand('${TARGET_CPPFLAGS}'),
94 'extra_cxxflags': d.expand('${TARGET_CXXFLAGS}'),
95 'extra_ldflags': d.expand('${TARGET_LDFLAGS}'),
99 with open(file_path, 'w') as toolchain_file:
100 toolchain_file.write(
101 '# This file has been generated automatically.\n'
103 'import("//build/config/sysroot.gni")\n'
104 'import("//build/toolchain/gcc_toolchain.gni")\n'
107 toolchain_file.write(gcc_toolchain_tmpl.substitute(native_toolchain))
108 toolchain_file.write(gcc_toolchain_tmpl.substitute(target_toolchain))
109 toolchain_file.write(clang_toolchain_tmpl.substitute(native_toolchain))
110 toolchain_file.write(clang_toolchain_tmpl.substitute(target_toolchain))