SPEC-3723: restructure meta-agl-demo
[AGL/meta-agl-demo.git] / recipes-wam / chromium / gn-utils.inc
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:
7 #
8 # The above copyright notice and this permission notice shall be included in
9 # all copies or substantial portions of the Software.
10 #
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
17 # THE SOFTWARE.
18
19 # GN host architecture helpers.
20 #
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
24 # understands.
25 def gn_host_arch_name(d):
26     """Returns a GN architecture name corresponding to the build host's machine
27     architecture."""
28     import re
29     arch_translations = {
30         r'aarch64.*': 'arm64',
31         r'arm.*': 'arm',
32         r'i[3456]86$': 'x86',
33         r'x86_64$': 'x64',
34     }
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):
38             return gn_arch_name
39     bb.fatal('Unsuported BUILD_ARCH value: "%s"' % build_arch)
40
41 # GN target architecture helpers.
42 #
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"
53
54 BUILD_CC_toolchain-clang = "clang"
55 BUILD_CXX_toolchain-clang = "clang++"
56 BUILD_LD_toolchain-clang = "clang"
57
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(":"):
65         return "true"
66     return "false"
67
68 def clang_install_path(d):
69     """Return clang compiler install path."""
70     return d.getVar("STAGING_BINDIR_NATIVE")
71
72 def gn_target_arch_name(d):
73     """Returns a GN architecture name corresponding to the target machine's
74     architecture."""
75     name = d.getVar("GN_TARGET_ARCH_NAME")
76     if name is None:
77         bb.fatal('Unsupported target architecture. A valid override for the '
78                  'GN_TARGET_ARCH_NAME variable could not be found.')
79     return name
80
81 def write_toolchain_file(d, file_path):
82     """Creates a complete GN toolchain file in |file_path|."""
83     import string
84     gcc_toolchain_tmpl = string.Template(
85         'gcc_toolchain("${toolchain_name}") {\n'
86         '  cc = "${cc}"\n'
87         '  cxx = "${cxx}"\n'
88         '  ar = "${ar}"\n'
89         '  ld = cxx  # GN expects a compiler, not a linker.\n'
90         '  nm = "${nm}"\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'
99         '    is_clang = false\n'
100         '  }\n'
101         '}\n'
102     )
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'
112         '    is_clang = true\n'
113         '    use_gold = true\n'
114         '  }\n'
115         '}\n'
116     )
117
118     native_toolchain = {
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}'),
130     }
131     target_toolchain = {
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}'),
143         'strip': '',
144     }
145
146     with open(file_path, 'w') as toolchain_file:
147         toolchain_file.write(
148             '# This file has been generated automatically.\n'
149             '\n'
150             'import("//build/config/sysroot.gni")\n'
151             'import("//build/toolchain/gcc_toolchain.gni")\n'
152             '\n'
153         )
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))