meta-qt6/recipes-qt/qt6/gn-utils.inc
Khem Raj 3e9d7c06da gn: Compute is_clang correctly
is_clang is hardcoded to 'false' which works great when using gcc
compiler, however it does not work when using clang compiler with
yocto from meta-clang, since toolchain file encodes is_clang = false
but then uses clang for cc/cxx lot of GN checks in chromium go wrong
due to is_clang assuming wrong value and compilation fails

e.g.

| CMake Error at /home/khem/actions-runner-yoe/_work/yoe-distro/yoe-distro/build/tmp/work/cortexa57-yoe-linux/qtwebengine/6.9.0/git/cmake/QtGnGen.cmake:95 (message):
|
|
|   -- GN FAILED
|
|   ERROR at
|   //base/allocator/partition_allocator/src/partition_alloc/BUILD.gn:499:7
|   (//build/toolchain/yocto:yocto_native): Assertion failed.
|
|         assert(stack_scan_supported)
|         ^-----
|
|   See //base/allocator/partition_allocator/BUILD.gn:7:19: which caused the
|   file to be included.
|
|     public_deps = [ "src/partition_alloc:raw_ptr" ]
|                     ^----------------------------
|

Therefore, compute this flag based upon toolchains used to compile QT
in yocto environment.

Pick-to: 6.9
Change-Id: I42d3fc77d7241d842e3fbbc991ee5cdcfc39f46f
Reviewed-by: Samuli Piippo <samuli.piippo@qt.io>
2025-04-01 22:10:28 -07:00

135 lines
5.1 KiB
C++

# GN host architecture helpers.
#
# BUILD_ARCH's value corresponds to what uname returns as the machine name.
# The mapping in gn_host_arch_name() tries to match several possible values
# returned by the Linux kernel in uname(2) into the corresponding values GN
# understands.
def gn_host_arch_name(d):
"""Returns a GN architecture name corresponding to the build host's machine
architecture."""
import re
arch_translations = {
r'aarch64.*': 'arm64',
r'arm.*': 'arm',
r'i[3456]86$': 'x86',
r'x86_64$': 'x64',
}
build_arch = d.getVar("BUILD_ARCH")
for arch_regexp, gn_arch_name in arch_translations.items():
if re.match(arch_regexp, build_arch):
return gn_arch_name
bb.fatal('Unsuported BUILD_ARCH value: "%s"' % build_arch)
# GN target architecture helpers.
#
# Determining the target architecture is more difficult, as there are many
# different values we can use on the Yocto side (e.g. TUNE_ARCH, TARGET_ARCH,
# MACHINEOVERRIDES etc). What we do is define the mapping with regular,
# non-Python variables with overrides that are generic enough (i.e. "x86"
# instead of "i586") and then use gn_target_arch_name() to return the right
# value with some validation.
GN_TARGET_ARCH_NAME:aarch64 = "arm64"
GN_TARGET_ARCH_NAME:arm = "arm"
GN_TARGET_ARCH_NAME:x86 = "x86"
GN_TARGET_ARCH_NAME:x86-64 = "x64"
def clang_install_path(d):
"""Return clang compiler install path."""
return d.getVar("STAGING_BINDIR_NATIVE")
def gn_target_arch_name(d):
"""Returns a GN architecture name corresponding to the target machine's
architecture."""
name = d.getVar("GN_TARGET_ARCH_NAME")
if name is None:
bb.fatal('Unsupported target architecture. A valid override for the '
'GN_TARGET_ARCH_NAME variable could not be found.')
return name
def gn_host_pkg_config(d):
"""Return absolute paths to pkg-config-native."""
return d.getVar("STAGING_BINDIR_NATIVE") + "/" + "pkg-config-native"
def gn_clang(d):
"""Return true if using clang compiler else return false"""
if "clang++" in d.getVar("CXX"):
return "true"
else:
return "false"
def gn_clang_native(d):
"""Return true if using native clang compiler else return false"""
if "clang++" in d.getVar("BUILD_CXX"):
return "true"
else:
return "false"
def write_toolchain_file(d, file_path):
"""Creates a complete GN toolchain file in |file_path|."""
import string
# Even though we always use clang, the "clang_toolchain" GN template is too
# restrictive in the way it sets variables such as |cxx|. Since it is just
# a wrapper on top of the "gcc_toolchain" template, we keep using the
# latter directly to accommodate our cross-compilation needs.
toolchain_tmpl = string.Template(
'gcc_toolchain("${toolchain_name}") {\n'
' cc = "${cc}"\n'
' cxx = "${cxx}"\n'
' ar = "${ar}"\n'
' ld = cxx # GN expects a compiler, not a linker.\n'
' nm = "${nm}"\n'
' readelf = "${readelf}"\n'
' extra_cflags = "${extra_cflags}"\n'
' extra_cppflags = "${extra_cppflags}"\n'
' extra_cxxflags = "${extra_cxxflags}"\n'
' extra_ldflags = "${extra_ldflags}"\n'
' toolchain_args = {\n'
' current_cpu = "${current_cpu}"\n'
' current_os = "linux"\n'
' is_clang = ${is_clang}\n'
' host_pkg_config = "${host_pkg_config}"\n'
' }\n'
'}\n'
)
native_toolchain = {
'toolchain_name': 'yocto_native',
'is_clang': gn_clang_native(d),
'current_cpu': gn_host_arch_name(d),
'host_pkg_config': gn_host_pkg_config(d),
'cc': d.expand('${BUILD_CC}'),
'cxx': d.expand('${BUILD_CXX}'),
'ar': d.expand('${BUILD_AR}'),
'nm': d.expand('${BUILD_NM}'),
'readelf': d.expand('${BUILD_PREFIX}readelf'),
'extra_cflags': d.expand('${BUILD_CFLAGS}'),
'extra_cppflags': d.expand('${BUILD_CPPFLAGS}'),
'extra_cxxflags': d.expand('${BUILD_CXXFLAGS}'),
'extra_ldflags': d.expand('${BUILD_LDFLAGS}'),
}
target_toolchain = {
'toolchain_name': 'yocto_target',
'is_clang': gn_clang(d),
'current_cpu': gn_target_arch_name(d),
'host_pkg_config': gn_host_pkg_config(d),
'cc': d.expand('${CC}'),
'cxx': d.expand('${CXX}'),
'ar': d.expand('${AR}'),
'nm': d.expand('${NM}'),
'readelf': d.expand('${TARGET_PREFIX}readelf'),
'extra_cflags': d.expand('${TARGET_CFLAGS}'),
'extra_cppflags': d.expand('${TARGET_CPPFLAGS}'),
'extra_cxxflags': d.expand('${TARGET_CXXFLAGS}'),
'extra_ldflags': d.expand('${TARGET_LDFLAGS}'),
}
with open(file_path, 'w') as toolchain_file:
toolchain_file.write(
'# This file has been generated automatically.\n'
'\n'
'import("//build/toolchain/gcc_toolchain.gni")\n'
'\n'
)
toolchain_file.write(toolchain_tmpl.substitute(native_toolchain))
toolchain_file.write(toolchain_tmpl.substitute(target_toolchain))