1 # GN host architecture helpers.
3 # Copied from https://github.com/OSSystems/meta-browser
5 # BUILD_ARCH's value corresponds to what uname returns as the machine name.
6 # The mapping in gn_host_arch_name() tries to match several possible values
7 # returned by the Linux kernel in uname(2) into the corresponding values GN
10 def gn_host_arch_name(d):
11 """Returns a GN architecture name corresponding to the build host's machine
15 r'aarch64.*': 'arm64',
20 build_arch = d.getVar("BUILD_ARCH")
21 for arch_regexp, gn_arch_name in arch_translations.items():
22 if re.match(arch_regexp, build_arch):
24 bb.fatal('Unsuported BUILD_ARCH value: "%s"' % build_arch)
26 # GN target architecture helpers.
28 # Determining the target architecture is more difficult, as there are many
29 # different values we can use on the Yocto side (e.g. TUNE_ARCH, TARGET_ARCH,
30 # MACHINEOVERRIDES etc). What we do is define the mapping with regular,
31 # non-Python variables with overrides that are generic enough (i.e. "x86"
32 # instead of "i586") and then use gn_target_arch_name() to return the right
33 # value with some validation.
34 GN_TARGET_ARCH_NAME:aarch64 = "arm64"
35 GN_TARGET_ARCH_NAME:arm = "arm"
36 GN_TARGET_ARCH_NAME:x86 = "x86"
37 GN_TARGET_ARCH_NAME:x86-64 = "x64"
39 def clang_install_path(d):
40 """Return clang compiler install path."""
41 return d.getVar("STAGING_BINDIR_NATIVE")
43 def gn_target_arch_name(d):
44 """Returns a GN architecture name corresponding to the target machine's
46 name = d.getVar("GN_TARGET_ARCH_NAME")
48 bb.fatal('Unsupported target architecture. A valid override for the '
49 'GN_TARGET_ARCH_NAME variable could not be found.')
52 def write_toolchain_file(d, file_path):
53 """Creates a complete GN toolchain file in |file_path|."""
55 # Even though we always use clang, the "clang_toolchain" GN template is too
56 # restrictive in the way it sets variables such as |cxx|. Since it is just
57 # a wrapper on top of the "gcc_toolchain" template, we keep using the
58 # latter directly to accommodate our cross-compilation needs.
59 toolchain_tmpl = string.Template(
60 'gcc_toolchain("${toolchain_name}") {\n'
64 ' ld = cxx # GN expects a compiler, not a linker.\n'
66 ' readelf = "${readelf}"\n'
67 ' extra_cflags = "${extra_cflags}"\n'
68 ' extra_cppflags = "${extra_cppflags}"\n'
69 ' extra_cxxflags = "${extra_cxxflags}"\n'
70 ' extra_ldflags = "${extra_ldflags}"\n'
71 ' toolchain_args = {\n'
72 ' current_cpu = "${current_cpu}"\n'
73 ' current_os = "linux"\n'
80 'toolchain_name': 'yocto_native',
81 'current_cpu': gn_host_arch_name(d),
82 'cc': d.expand('${BUILD_CC}'),
83 'cxx': d.expand('${BUILD_CXX}'),
84 'ar': d.expand('${BUILD_AR}'),
85 'nm': d.expand('${BUILD_NM}'),
86 'readelf': d.expand('${BUILD_PREFIX}readelf'),
87 'extra_cflags': d.expand('${BUILD_CFLAGS}'),
88 'extra_cppflags': d.expand('${BUILD_CPPFLAGS}'),
89 'extra_cxxflags': d.expand('${BUILD_CXXFLAGS}'),
90 'extra_ldflags': d.expand('${BUILD_LDFLAGS}'),
93 'toolchain_name': 'yocto_target',
94 'current_cpu': gn_target_arch_name(d),
95 'cc': d.expand('${CC}'),
96 'cxx': d.expand('${CXX}'),
97 'ar': d.expand('${AR}'),
98 'nm': d.expand('${NM}'),
99 'readelf': d.expand('${READELF}'),
100 'extra_cflags': d.expand('${CFLAGS}'),
101 'extra_cppflags': d.expand('${CPPFLAGS}'),
102 'extra_cxxflags': d.expand('${CXXFLAGS}'),
103 'extra_ldflags': d.expand('${LDFLAGS}'),
106 with open(file_path, 'w') as toolchain_file:
107 toolchain_file.write(
108 '# This file has been generated automatically.\n'
110 'import("//build/toolchain/gcc_toolchain.gni")\n'
113 toolchain_file.write(toolchain_tmpl.substitute(native_toolchain))
114 toolchain_file.write(toolchain_tmpl.substitute(target_toolchain))