diff --git a/src/build/config/BUILDCONFIG.gn b/src/build/config/BUILDCONFIG.gn index 5275636760..06858f03cf 100644 --- a/src/build/config/BUILDCONFIG.gn +++ b/src/build/config/BUILDCONFIG.gn @@ -291,6 +291,8 @@ if (target_os == "android") { assert( false, "emscripten is not a supported target_os. It is available only as secondary toolchain.") +} else if (target_os == "openwrt") { + _default_toolchain = "//build/toolchain/openwrt:$target_cpu" } else { assert(false, "Unsupported target_os: $target_os") } @@ -325,7 +327,7 @@ is_android = current_os == "android" is_chromeos = current_os == "chromeos" is_fuchsia = current_os == "fuchsia" is_ios = current_os == "ios" -is_linux = current_os == "linux" +is_linux = current_os == "linux" || current_os == "openwrt" is_mac = current_os == "mac" is_wasm = current_os == "emscripten" is_watchos = current_os == "watchos" diff --git a/src/build/config/compiler/BUILD.gn b/src/build/config/compiler/BUILD.gn index 740208521d..7058034885 100644 --- a/src/build/config/compiler/BUILD.gn +++ b/src/build/config/compiler/BUILD.gn @@ -345,6 +345,9 @@ config("compiler") { ] } else if (is_linux) { configs += [ "//build/config/linux:compiler" ] + if (current_os == "openwrt") { + configs += [ "//build/config/openwrt:compiler" ] + } } else if (is_mac) { configs += [ "//build/config/mac:compiler" ] } else if (is_ios) { diff --git a/src/build/config/compiler_cpu_abi.gn b/src/build/config/compiler_cpu_abi.gn index 240a291767..b5e4d02e58 100644 --- a/src/build/config/compiler_cpu_abi.gn +++ b/src/build/config/compiler_cpu_abi.gn @@ -48,7 +48,7 @@ if (is_chromeos_device && is_chromeos) { if ((is_posix && !is_apple) || is_fuchsia) { # simplicity we always explicitly set the architecture. if (current_cpu == "x64") { - if (is_clang && !is_android && !is_fuchsia && !is_chromeos_device) { + if (is_clang && !is_android && !is_fuchsia && !is_chromeos_device && current_os != "openwrt") { cpu_abi_cflags += [ "--target=x86_64-unknown-linux-gnu" ] cpu_abi_ldflags += [ "--target=x86_64-unknown-linux-gnu" ] } else { @@ -62,7 +62,7 @@ if ((is_posix && !is_apple) || is_fuchsia) { cpu_abi_ldflags += [ "-march=$cros_target_cpu_arch" ] } } else if (current_cpu == "x86") { - if (is_clang && !is_android && !is_chromeos_device) { + if (is_clang && !is_android && !is_chromeos_device && current_os != "openwrt") { cpu_abi_cflags += [ "--target=i386-unknown-linux-gnu" ] cpu_abi_ldflags += [ "--target=i386-unknown-linux-gnu" ] } else { @@ -74,7 +74,7 @@ if ((is_posix && !is_apple) || is_fuchsia) { "-msse3", ] } else if (current_cpu == "arm") { - if (is_clang && !is_android && !is_chromeos_device) { + if (is_clang && !is_android && !is_chromeos_device && current_os != "openwrt") { cpu_abi_cflags += [ "--target=arm-linux-gnueabihf" ] cpu_abi_ldflags += [ "--target=arm-linux-gnueabihf" ] } @@ -88,7 +88,7 @@ if ((is_posix && !is_apple) || is_fuchsia) { } cpu_abi_ldflags += [ "-mfloat-abi=$arm_float_abi" ] } else if (current_cpu == "arm64") { - if (is_clang && !is_android && !is_fuchsia && !is_chromeos_device) { + if (is_clang && !is_android && !is_fuchsia && !is_chromeos_device && current_os != "openwrt") { cpu_abi_cflags += [ "--target=aarch64-linux-gnu" ] cpu_abi_ldflags += [ "--target=aarch64-linux-gnu" ] } @@ -102,7 +102,7 @@ if ((is_posix && !is_apple) || is_fuchsia) { } } else if (current_cpu == "mipsel") { cpu_abi_ldflags += [ "-Wl,--hash-style=sysv" ] - if (custom_toolchain == "") { + if (custom_toolchain == "" && current_os != "openwrt") { if (is_clang) { if (is_android) { cpu_abi_cflags += [ "--target=mipsel-linux-android" ] @@ -233,7 +233,7 @@ if ((is_posix && !is_apple) || is_fuchsia) { } else if (current_cpu == "mips64el") { cpu_abi_cflags += [ "-D__SANE_USERSPACE_TYPES__" ] cpu_abi_ldflags += [ "-Wl,--hash-style=sysv" ] - if (custom_toolchain == "") { + if (custom_toolchain == "" && current_os != "openwrt") { if (is_clang) { if (is_android) { cpu_abi_cflags += [ "--target=mips64el-linux-android" ] @@ -349,13 +349,13 @@ if ((is_posix && !is_apple) || is_fuchsia) { cpu_abi_ldflags += [ "-m64" ] } } else if (current_cpu == "riscv64") { - if (is_clang && !is_android) { + if (is_clang && !is_android && current_os != "openwrt") { cpu_abi_cflags += [ "--target=riscv64-linux-gnu" ] cpu_abi_ldflags += [ "--target=riscv64-linux-gnu" ] } cpu_abi_cflags += [ "-mabi=lp64d" ] } else if (current_cpu == "loong64") { - if (is_clang) { + if (is_clang && current_os != "openwrt") { cpu_abi_cflags += [ "--target=loongarch64-linux-gnu" ] cpu_abi_ldflags += [ "--target=loongarch64-linux-gnu" ] } diff --git a/src/build/config/openwrt/BUILD.gn b/src/build/config/openwrt/BUILD.gn new file mode 100644 index 0000000000..0cb8de6a7c --- /dev/null +++ b/src/build/config/openwrt/BUILD.gn @@ -0,0 +1,88 @@ +# Copyright 2014 The Chromium 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/c++/c++.gni") +import("//build/config/compiler/compiler.gni") +import("//build/config/sanitizers/sanitizers.gni") + +if (current_toolchain == default_toolchain) { + import("//build/toolchain/concurrent_links.gni") +} + +assert(current_os == "openwrt") + +declare_args() { + build_static = false + + no_madvise_syscall = false +} + +# This is included by reference in the //build/config/compiler config that +# is applied to all targets. It is here to separate out the logic that is +# Android-only. +config("compiler") { + cflags = [] + ldflags = [] + + defines = [ + "_LIBCPP_HAS_MUSL_LIBC=1", + "__MUSL__", + ] + + if (build_static) { + # Musl static objects are not built with -fPIC, cannot use -static-pie. + ldflags += [ "-static" ] + defines += [ "OPENWRT_BUILD_STATIC" ] + } else if (current_cpu == "x86") { + ldflags += [ "-Wl,--dynamic-linker=/lib/ld-musl-i386.so.1" ] + } else if (current_cpu == "mipsel") { + ldflags += [ "-Wl,--dynamic-linker=/lib/ld-musl-mipsel-sf.so.1" ] + } + + if (no_madvise_syscall) { + defines += [ "NO_MADVISE_SYSCALL" ] + } + + abi = "musl" + if (current_cpu == "arm") { + abi = "musleabi" + arch = "arm" + } else if (current_cpu == "arm64") { + arch = "aarch64" + } else if (current_cpu == "x86") { + arch = "i486" + } else if (current_cpu == "x64") { + arch = "x86_64" + } else if (current_cpu == "mipsel") { + arch = "mipsel" + } else if (current_cpu == "mips64el") { + arch = "mips64el" + } else if (current_cpu == "riscv64") { + arch = "riscv64" + } else if (current_cpu == "loong64") { + arch = "loongarch64" + } else { + assert(false, "Architecture not supported") + } + cflags += [ + "--target=$arch-openwrt-linux-$abi", + + # musl malloc() alignment is 4*sizeof(size_t), i.e. + # 16 bytes on 32-bit CPUs, and 32 bytes on 64-bit CPUs. See + # https://git.musl-libc.org/cgit/musl/tree/src/malloc/oldmalloc/malloc_impl.h + # And https://www.openwall.com/lists/musl/2019/07/07/2 for its rationale. + # But Clang is unaware of this information and produces + # #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 4U + # for the i486-openwrt-linux-musl target. + # This further causes static_asserts in + # src/third_party/protobuf/src/google/protobuf/arenastring.cc to fail. + # However, PartitionAlloc does not support larger than 16 alignment. + "-fnew-alignment=16", + ] + ldflags += [ "--target=$arch-openwrt-linux-$abi" ] + + # Assign any flags set for the C compiler to asmflags so that they are sent + # to the assembler. + asmflags = cflags +} diff --git a/src/build/toolchain/openwrt/BUILD.gn b/src/build/toolchain/openwrt/BUILD.gn new file mode 100644 index 0000000000..d05b4503d7 --- /dev/null +++ b/src/build/toolchain/openwrt/BUILD.gn @@ -0,0 +1,15 @@ +# Copyright 2013 The Chromium 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") +import("//build/toolchain/gcc_toolchain.gni") + +assert(target_os == "openwrt") + +clang_toolchain(target_cpu) { + toolchain_args = { + current_cpu = target_cpu + current_os = "openwrt" + } +}