From 903ae13d643c305c48c4271ed4a05939656ed041 Mon Sep 17 00:00:00 2001 From: "Lauren N. Liberda" Date: Sat, 8 Apr 2023 02:56:35 +0200 Subject: [PATCH 1/2] add gn args specifying libc for linux --- build/config/compiler/BUILD.gn | 8 ++- build/config/linux/detect_host_libc.py | 42 ++++++++++++ build/config/linux/libc.gni | 51 +++++++++++++++ build/toolchain/linux/BUILD.gn | 89 ++++++++++++++------------ 4 files changed, 147 insertions(+), 43 deletions(-) create mode 100755 build/config/linux/detect_host_libc.py create mode 100644 build/config/linux/libc.gni diff --git a/engine/src/build/config/compiler/BUILD.gn b/engine/src/build/config/compiler/BUILD.gn index 4c9a1ee..155d433 100644 --- ./engine/src/build/config/compiler/BUILD.gn.orig +++ ./engine/src/build/config/compiler/BUILD.gn @@ -12,6 +12,9 @@ if (is_win) { import("//build/config/win/visual_studio_version.gni") } +if (is_linux) { + import("//build/config/linux/libc.gni") +} import("//build/config/c++/c++.gni") import("//build/config/profiler.gni") @@ -339,8 +342,8 @@ ldflags += [ "-pthread" ] if (current_cpu == "arm64") { - cflags += [ "--target=aarch64-linux-gnu" ] - ldflags += [ "--target=aarch64-linux-gnu" ] + cflags += [ "--target=aarch64-alpine-linux-musl" ] + ldflags += [ "--target=aarch64-alpine-linux-musl" ] cflags += [ "-DBORINGSSL_CLANG_SUPPORTS_DOT_ARCH" ] } } @@ -622,12 +625,15 @@ default_warning_flags += [ # Enables. "-Wendif-labels", # Weird old-style text after an #endif. - "-Werror", # Warnings as errors. # Disables. "-Wno-missing-field-initializers", # "struct foo f = {0};" "-Wno-unused-parameter", # Unused function parameters. ] + if (!is_linux || is_glibc) { + # Warnings as errors. Fails on musl. + default_warning_flags += [ "-Werror" ] + } if (is_wasm) { default_warning_flags += [ diff --git a/engine/src/build/config/linux/detect_host_libc.py b/engine/src/build/config/linux/detect_host_libc.py new file mode 100755 index 0000000..1ebb498 --- /dev/null +++ b/engine/src/build/config/linux/detect_host_libc.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2013 The Flutter Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +from optparse import OptionParser +import re +import subprocess +import sys + + +def main(): + if 'linux' not in sys.platform: + return 1 + + parser = OptionParser() + parser.add_option('--compiler-path', action='store', + type='string', default='gcc') + (options, args) = parser.parse_args() + + # should work with both gcc and clang + # + # example outputs: "aarch64-unknown-linux-gnu", "x86_64-alpine-linux-musl", + # "armv7-unknown-linux-musleabihf", "mipsel-linux-muslhf" + default_target = subprocess.check_output([ + options.compiler_path, '-dumpmachine']).decode('utf-8').strip() + + host_libc_re = re.match( + r'^(?:[^-]+-){1,2}linux-(?Pgnu|musl|uclibc)(?:eabi)?(?:hf)?$', + default_target) + if host_libc_re is None: + print(f'Target not understood: {default_target}') + + host_libc = host_libc_re.group('libc') + # print would add a newline + sys.stdout.write(host_libc) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/engine/src/build/config/linux/libc.gni b/engine/src/build/config/linux/libc.gni new file mode 100644 index 0000000..653f312 --- /dev/null +++ b/engine/src/build/config/linux/libc.gni @@ -0,0 +1,51 @@ +# Copyright (c) 2013 The Flutter Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//build/config/sysroot.gni") + +declare_args() { + # The C library used by the compilation host. + # Expecting one of: "gnu" (glibc), "uclibc", "musl" + host_libc = "" + + # The C library of the compilation target. + # Expecting one of: "gnu" (glibc), "uclibc", "musl" + target_libc = "" +} + +# try to detect host's libc if not explicitly provided +if (host_libc == "" && is_linux) { + if (sysroot == "") { + host_compiler = "gcc" + if (is_clang) { + host_compiler = "clang" + } + + # tries to determine libc from "gcc/clang -printmachine" + host_libc = exec_script("//build/config/linux/detect_host_libc.py", + [ + "--compiler-path", + host_compiler, + ], + "string") + } else { + # currently there is only debian sysroot + host_libc = "gnu" + } +} + +assert(!is_linux || host_libc != "", "Host libc not specified and not detected") + +# assume compiling for host if not explicitly specified +if (target_libc == "") { + target_libc = host_libc +} + +is_cross_libc = host_libc != target_libc + +is_glibc = target_libc == "gnu" +is_musl = target_libc == "musl" + +is_host_glibc = host_libc == "gnu" +is_host_musl = host_libc == "musl" diff --git a/engine/src/build/toolchain/linux/BUILD.gn b/engine/src/build/toolchain/linux/BUILD.gn index 05630d0..b1288ce 100644 --- a/engine/src/build/toolchain/linux/BUILD.gn +++ b/engine/src/build/toolchain/linux/BUILD.gn @@ -2,6 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//build/config/linux/libc.gni") import("//build/config/sysroot.gni") import("//build/toolchain/ccache.gni") import("//build/toolchain/gcc_toolchain.gni") @@ -33,16 +34,10 @@ link_prefix = "" } -if (host_cpu == "arm64") { - rebased_clang_dir = - rebase_path("$buildtools_path/linux-arm64/clang/bin", root_build_dir) -} else { - rebased_clang_dir = - rebase_path("$buildtools_path/linux-x64/clang/bin", root_build_dir) -} +rebased_clang_dir = "" gcc_toolchain("arm") { - prefix = "arm-linux-gnueabihf-" + prefix = "arm-linux-${target_libc}eabihf-" if (toolchain_prefix != "") { prefix = toolchain_prefix } @@ -64,15 +59,15 @@ gcc_toolchain("clang_arm") { prefix = rebased_clang_dir - asm = "${prefix}/clang" - cc = "${compiler_prefix}${prefix}/clang" - cxx = "${compiler_prefix}${prefix}/clang++" + asm = "${prefix}clang" + cc = "${compiler_prefix}${prefix}clang" + cxx = "${compiler_prefix}${prefix}clang++" - readelf = "${prefix}/llvm-readelf" - nm = "${prefix}/llvm-nm" - ar = "${prefix}/llvm-ar" - ld = "${link_prefix}${prefix}/clang++" - llvm_objcopy = "${prefix}/llvm-objcopy" + readelf = "${prefix}llvm-readelf" + nm = "${prefix}llvm-nm" + ar = "${prefix}llvm-ar" + ld = "${link_prefix}${prefix}clang++" + llvm_objcopy = "${prefix}llvm-objcopy" toolchain_cpu = "arm" toolchain_os = "linux" @@ -80,7 +75,7 @@ } gcc_toolchain("arm64") { - prefix = "aarch64-linux-gnu-" + prefix = "aarch64-linux-${target_libc}-" if (toolchain_prefix != "") { prefix = toolchain_prefix } @@ -102,15 +97,15 @@ gcc_toolchain("clang_arm64") { prefix = rebased_clang_dir - asm = "${prefix}/clang" - cc = "${compiler_prefix}${prefix}/clang" - cxx = "${compiler_prefix}${prefix}/clang++" + asm = "${prefix}clang" + cc = "${compiler_prefix}${prefix}clang" + cxx = "${compiler_prefix}${prefix}clang++" readelf = "readelf" - nm = "${prefix}/llvm-nm" - ar = "${prefix}/llvm-ar" - ld = "${link_prefix}${prefix}/clang++" - llvm_objcopy = "${prefix}/llvm-objcopy" + nm = "${prefix}llvm-nm" + ar = "${prefix}llvm-ar" + ld = "${link_prefix}${prefix}clang++" + llvm_objcopy = "${prefix}llvm-objcopy" toolchain_cpu = "arm64" toolchain_os = "linux" @@ -119,15 +114,15 @@ gcc_toolchain("clang_x86") { prefix = rebased_clang_dir - asm = "${prefix}/clang" - cc = "${compiler_prefix}${prefix}/clang" - cxx = "${compiler_prefix}${prefix}/clang++" + asm = "${prefix}clang" + cc = "${compiler_prefix}${prefix}clang" + cxx = "${compiler_prefix}${prefix}clang++" - readelf = "${prefix}/llvm-readelf" - nm = "${prefix}/llvm-nm" - ar = "${prefix}/llvm-ar" - ld = "${link_prefix}${prefix}/clang++" - llvm_objcopy = "${prefix}/llvm-objcopy" + readelf = "${prefix}llvm-readelf" + nm = "${prefix}llvm-nm" + ar = "${prefix}llvm-ar" + ld = "${link_prefix}${prefix}clang++" + llvm_objcopy = "${prefix}llvm-objcopy" toolchain_cpu = "x86" toolchain_os = "linux" @@ -153,15 +148,15 @@ gcc_toolchain("clang_x64") { prefix = rebased_clang_dir - asm = "${prefix}/clang" - cc = "${compiler_prefix}${prefix}/clang" - cxx = "${compiler_prefix}${prefix}/clang++" + asm = "${prefix}clang" + cc = "${compiler_prefix}${prefix}clang" + cxx = "${compiler_prefix}${prefix}clang++" - readelf = "${prefix}/llvm-readelf" - nm = "${prefix}/llvm-nm" - ar = "${prefix}/llvm-ar" - ld = "${link_prefix}${prefix}/clang++" - llvm_objcopy = "${prefix}/llvm-objcopy" + readelf = "${prefix}llvm-readelf" + nm = "${prefix}llvm-nm" + ar = "${prefix}llvm-ar" + ld = "${link_prefix}${prefix}clang++" + llvm_objcopy = "${prefix}llvm-objcopy" toolchain_cpu = "x64" toolchain_os = "linux" @@ -186,7 +181,7 @@ } gcc_toolchain("riscv32") { - prefix = "riscv32-linux-gnu-" + prefix = "riscv32-linux-${target_libc}-" if (toolchain_prefix != "") { prefix = toolchain_prefix } @@ -224,7 +219,7 @@ } gcc_toolchain("riscv64") { - prefix = "riscv64-linux-gnu-" + prefix = "riscv64-linux-${target_libc}-" if (toolchain_prefix != "") { prefix = toolchain_prefix } @@ -246,15 +241,15 @@ gcc_toolchain("clang_riscv64") { prefix = rebased_clang_dir - asm = "${prefix}/clang" - cc = "${compiler_prefix}${prefix}/clang" - cxx = "${compiler_prefix}${prefix}/clang++" + asm = "${prefix}clang" + cc = "${compiler_prefix}${prefix}clang" + cxx = "${compiler_prefix}${prefix}clang++" readelf = "readelf" - nm = "${prefix}/llvm-nm" - ar = "${prefix}/llvm-ar" - ld = "${link_prefix}${prefix}/clang++" - llvm_objcopy = "${prefix}/llvm-objcopy" + nm = "${prefix}llvm-nm" + ar = "${prefix}llvm-ar" + ld = "${link_prefix}${prefix}clang++" + llvm_objcopy = "${prefix}llvm-objcopy" toolchain_cpu = "riscv64" toolchain_os = "linux"