From 7b3870d67e2a6445cf24274aa98eb417c390a4e9 Mon Sep 17 00:00:00 2001 From: Peter McEvoy Date: Mon, 9 Jan 2023 13:54:11 -0500 Subject: [PATCH] Build with musl instead of glibc on Linux (#27) --- BUILD.bazel | 59 +------ INSTALL.md | 94 ++++++++++ README.md | 98 +---------- WORKSPACE.bazel | 3 +- bazel/third_party/curl/curl.BUILD | 4 +- bazel/third_party/gmp/gmp.BUILD | 4 +- bazel/third_party/secp256k1/secp256k1.BUILD | 4 +- bazel/third_party/sigsegv/sigsegv.BUILD | 4 +- bazel/third_party/uv/uv.BUILD | 4 +- bazel/toolchain/BUILD.bazel | 183 ++++++++++++-------- bazel/toolchain/cfg.bzl | 38 +++- pkg/ent/BUILD.bazel | 5 +- pkg/noun/BUILD.bazel | 20 +++ pkg/ur/BUILD.bazel | 5 +- pkg/vere/BUILD.bazel | 25 ++- 15 files changed, 307 insertions(+), 243 deletions(-) create mode 100644 INSTALL.md diff --git a/BUILD.bazel b/BUILD.bazel index b119ffbdcc..b3f284eb21 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -56,54 +56,25 @@ config_setting( # COMPILERS # -constraint_setting(name = "compiler") - -# Cross compiler from x86_64 to arm64. -constraint_value( - name = "aarch64_linux_gnu_gcc", - constraint_setting = ":compiler", - visibility = ["//visibility:public"], -) - -# Compiler for x86_64. -constraint_value( - name = "clang", - constraint_setting = ":compiler", - visibility = ["//visibility:public"], -) - -# Compiler for x86_64. -constraint_value( - name = "gcc", - constraint_setting = ":compiler", - visibility = ["//visibility:public"], -) - -# Version flag for aarch64-linux-gnu-gcc. -string_flag( - name = "aarch64_linux_gnu_gcc_version", - build_setting_default = "12.2.0", - visibility = ["//visibility:public"], -) - # Version flag for clang. string_flag( name = "clang_version", - build_setting_default = "14.0.6", + # macOS uses `clang-14.0.0` by default. + build_setting_default = "14.0.0", visibility = ["//visibility:public"], ) # Version flag for gcc. string_flag( name = "gcc_version", - build_setting_default = "12.2.0", + # musl-cross-make uses `gcc-9.4.0` by default. + build_setting_default = "9.4.0", visibility = ["//visibility:public"], ) # # PLATFORMS # - # A platform takes one of two formats: `--` or `-` # if the compiler is unspecified. @@ -125,28 +96,6 @@ platform( ], ) -# Linux x86_64 platform with aarch64-linux-gnu-gcc. -platform( - name = "aarch64_linux_gnu_gcc-linux-x86_64", - constraint_values = [ - ":aarch64_linux_gnu_gcc", - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - visibility = ["//visibility:public"], -) - -# Linux x86_64 platform with clang. -platform( - name = "clang-linux-x86_64", - constraint_values = [ - ":clang", - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - visibility = ["//visibility:public"], -) - # Linux x86_64 platform with gcc. platform( name = "gcc-linux-x86_64", diff --git a/INSTALL.md b/INSTALL.md new file mode 100644 index 0000000000..f7b0a5223d --- /dev/null +++ b/INSTALL.md @@ -0,0 +1,94 @@ +# Building Vere + +We use [`bazel`][bazel][^1] to built Vere, which is packaged as a single binary, +`urbit`. We spport the following `(host, target)` pairs, where the host platform +is where [`bazel`][bazel] runs and the target platform is where `urbit` will +run: + +---------------------------------- + Host Platform | Target Platform +---------------------------------- + `linux-x86_64` | `linux-arm64` + `linux-x86_64` | `linux-x86_64` + `macos-arm64` | `macos-arm64` + `macos-x86_64` | `macos-x86_64` + +## Prerequisites + +### Linux + +We use [musl libc][musl libc] instead [glibc][glibc] on Linux, which enables us +to generate statically linked binaries. As a prerequisite, you need to install +the [musl libc][musl libc] toolchain appropriate for your target platform. + +#### x86_64 + +To install the `x86_64-linux-musl-gcc` toolchain at +`/usr/local/x86_64-linux-musl-gcc`, run: +```console +$ bazel run //bazel/toolchain:x86_64-linux-musl-gcc +``` + +This will take a few minutes. + +#### aarch64 + +To install the `aarch64-linux-musl-gcc` toolchain at +`/usr/local/aarch64-linux-musl-gcc`, run: +```console +$ bazel run //bazel/toolchain:aarch64-linux-musl-gcc +``` + +This will take a few minutes. + +### macOS + +Ensure you have up-to-date versions of `bazelisk`, `automake`, and `libtool`: +```console +$ brew install bazelisk automake libtool +``` + +## Build Commands + +Once you install the prerequisites, you're ready to build. If you're performing +a native build (i.e. one in which the host platform and target platform are the +same), run: +```console +$ bazel build //pkg/... +``` + +If you're performing a cross-platform build, you need to specify the target +platform in the build command: +```console +$ bazel build --platforms=//: //pkg/... +``` + +## Test Commands + +You can build and run unit tests only on native builds. If you have a native +build and want to run all unit tests, run: +```console +$ bazel test //pkg/... +``` + +If you want to run a specific test, say +[`pkg/noun/hashtable_tests.c`](pkg/noun/hashtable_tests.c), run: +```console +$ bazel test //pkg/noun:hashtable_tests +``` + +## Common Issues + +If `bazel build` or `bazel test` generates an `undeclared inclusion(s) in rule` +error on macOS, the version of `clang` expected by the build system likely +doesn't match the version of `clang` installed on your system. To address this, +pass `--clang_version=""` to the failing command. + +[^1]: If you're interested in digging into the details of the build system, + check out [`WORKSPACE.bazel`](WORKSPACE.bazel), + [`BUILD.bazel`](BUILD.bazel), [`bazel/`](bazel), and the multiple + `BUILD.bazel` files in [`pkg/`](pkg). + +[bazel]: https://bazel.build +[glibc]: https://www.gnu.org/software/libc +[musl libc]: https://musl.libc.org diff --git a/README.md b/README.md index 988ba89b3c..582499a7ee 100644 --- a/README.md +++ b/README.md @@ -26,105 +26,9 @@ defined in its own package: - [`pkg/noun`](pkg/noun): The Nock virtual machine and snapshotting system. - [`pkg/vere`](pkg/vere): The I/O drivers, event log, and main event loop. -## Pre-requisites - -### macOS - -```console -$ brew install bazelisk automake libtool -``` - ## Build -We use [`bazel`][bazel] to build Urbit's runtime, which is packaged as a single -binary, `urbit`. We support the following `(host, target)` pairs, where the host -platform is where [`bazel`][bazel] runs and the target platform is where `urbit` -will run: - --------------------------------------------------------------------------------- - Host Platform | Target Platform | Required Toolchain --------------------------------------------------------------------------------- - `aarch64_linux_gnu_gcc-linux-x86_64` | `linux-arm64` | `aarch64-linux_gnu_gcc` - `gcc-linux-x86_64` | `linux-x86_64` | `gcc` - `clang-linux-x86_64` | `linux-x86_64` | `clang` - `clang-macos-arm64` | `macos-arm64` | `clang` - `clang-macos-x86_64` | `macos-x86_64` | `clang` - -Once you've identified your `(host, target)` pair, determine the version of the -pair's required toolchain and ensure you have an up-to-date version of -[`bazel`][bazel]. Then, run: - -```console -$ bazel build --_version="" \ - --host_platform=//: \ - --platforms=//: :urbit -``` - -For example, to build a `linux-x86_64` `urbit` binary on a `linux-x86_64` -machine using version `14.0.6` of the `clang` toolchain, run: - -```console -$ bazel build --clang_version="14.0.6" \ - --host_platform=//:gcc-linux-x86_64 \ - --platforms=//:linux-x86_64 -``` - -And to build a `linux-arm64` `urbit` binary on a `linux-x86_64` machine using -version `12.2.0` of the `aarch64-linux-gnu-gcc` toolchain (which you'll have to -install), run: - -```console -$ bazel build --aarch64_linux_gnu_gcc_version="12.2.0" \ - --host_platform=//:aarch64_linux_gnu_gcc-linux-x86_64 \ - --platforms=//:linux-arm64 -``` - -Specifying `--_version`, `--host_platform`, and `--platforms` for -each build is tedious and can be avoided by writing to `.user.bazelrc`: - -```console -$ echo 'build --aarch64_linux_gnu_gcc_version="12.2.0"' >> .user.bazelrc -$ echo 'build --clang_version="14.0.6"' >> .user.bazelrc -$ echo 'build --gcc_version="12.2.0"' >> .user.bazelrc -$ echo 'build --host_platform=//:' >> .user.bazelrc -$ echo 'build --platforms=//:' >> .user.bazelrc -$ bazel build :urbit -``` - -To run the just-built `urbit` binary, run: - -```console -$ bazel-bin/pkg/vere/urbit -``` - -Or, to save yourself a few keystrokes, create a symlink to the `urbit` binary in -the root of the repository: - -```console -$ ln -s bazel-bin/pkg/vere/urbit urbit -$ ./urbit -``` - -The remaining commands in this section assume that `.user.bazlerc` specifies -`--host_platform` and `--platforms`. If not, `--host_platform` and `--platforms` -must be provided at the command line as in the build commands above. - -To run all runtime tests, run: - -```console -$ bazel test //... -``` - -or, to run a specific test, say -[`pkg/noun/hashtable_tests.c`](pkg/noun/hashtable_tests.c), run: - -```console -$ bazel test //pkg/noun:hashtable_tests -``` - -If you're interested in digging into the details of the build system, check out -[`WORKSPACE.bazel`](WORKSPACE.bazel), [`BUILD.bazel`](BUILD.bazel), -[`bazel/`](bazel), and the multiple `BUILD.bazel` files in [`pkg/`](pkg). +See [INSTALL.md](INSTALL.md). ## Contributing diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 42549b5459..3562df8652 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -93,8 +93,7 @@ versioned_http_archive( # Use the toolchains we've configured. register_toolchains( - "//bazel/toolchain:aarch64_linux_gnu_gcc-linux-x86_64-toolchain", - "//bazel/toolchain:clang-linux-x86_64-toolchain", + "//bazel/toolchain:gcc-linux-arm64-toolchain", "//bazel/toolchain:clang-macos-arm64-toolchain", "//bazel/toolchain:clang-macos-x86_64-toolchain", "//bazel/toolchain:gcc-linux-x86_64-toolchain", diff --git a/bazel/third_party/curl/curl.BUILD b/bazel/third_party/curl/curl.BUILD index c4be631cc4..a238c28f48 100644 --- a/bazel/third_party/curl/curl.BUILD +++ b/bazel/third_party/curl/curl.BUILD @@ -53,8 +53,8 @@ configure_make( "--without-zlib", "--without-zstd", ] + select({ - # Native compilation on linux-arm64 isn't supported. - "@//:linux_arm64": ["--host=aarch64-linux-gnu"], + "@//:linux_arm64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], "//conditions:default": [], }), env = { diff --git a/bazel/third_party/gmp/gmp.BUILD b/bazel/third_party/gmp/gmp.BUILD index d04de1b107..1cf816a193 100644 --- a/bazel/third_party/gmp/gmp.BUILD +++ b/bazel/third_party/gmp/gmp.BUILD @@ -18,8 +18,8 @@ configure_make( # but we leave it in for all builds as a precaution. "--with-pic", ] + select({ - # Native compilation on linux-arm64 isn't supported. - "@//:linux_arm64": ["--host=aarch64-linux-gnu"], + "@//:linux_arm64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], "//conditions:default": [], }), lib_source = ":all", diff --git a/bazel/third_party/secp256k1/secp256k1.BUILD b/bazel/third_party/secp256k1/secp256k1.BUILD index 9dc2533c79..ec86fcbfa5 100644 --- a/bazel/third_party/secp256k1/secp256k1.BUILD +++ b/bazel/third_party/secp256k1/secp256k1.BUILD @@ -19,8 +19,8 @@ configure_make( "--enable-module-schnorrsig", "--enable-static", ] + select({ - # Native compilation on linux-arm64 isn't supported. - "@//:linux_arm64": ["--host=aarch64-linux-gnu"], + "@//:linux_arm64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], "//conditions:default": [], }), lib_source = ":all", diff --git a/bazel/third_party/sigsegv/sigsegv.BUILD b/bazel/third_party/sigsegv/sigsegv.BUILD index 2362af35a4..f4a13c5136 100644 --- a/bazel/third_party/sigsegv/sigsegv.BUILD +++ b/bazel/third_party/sigsegv/sigsegv.BUILD @@ -20,8 +20,8 @@ configure_make( "@platforms//os:linux": ["--disable-stackvma"], "//conditions:default": [], }) + select({ - # Native compilation on linux-arm64 isn't supported. - "@//:linux_arm64": ["--host=aarch64-linux-gnu"], + "@//:linux_arm64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], "//conditions:default": [], }), lib_source = ":all", diff --git a/bazel/third_party/uv/uv.BUILD b/bazel/third_party/uv/uv.BUILD index 362bbdbe8f..402b446b79 100644 --- a/bazel/third_party/uv/uv.BUILD +++ b/bazel/third_party/uv/uv.BUILD @@ -17,8 +17,8 @@ configure_make( configure_options = [ "--disable-shared", ] + select({ - # Native compilation on linux-arm64 isn't supported. - "@//:linux_arm64": ["--host=aarch64-linux-gnu"], + "@//:linux_arm64": ["--host=aarch64-linux-musl"], + "@//:linux_x86_64": ["--host=x86_64-linux-musl"], "//conditions:default": [], }), lib_source = ":all", diff --git a/bazel/toolchain/BUILD.bazel b/bazel/toolchain/BUILD.bazel index 0daeac648b..47ad0e8f7e 100644 --- a/bazel/toolchain/BUILD.bazel +++ b/bazel/toolchain/BUILD.bazel @@ -39,30 +39,34 @@ alias( ) # -# aarch64_linux_gnu_gcc-linux-x86_64 +# gcc-linux-aarch64 # # Toolchain identifier. -_aarch64_linux_gnu_gcc = "toolchain-aarch64_linux_gnu_gcc-linux-x86_64" +_arm64_gcc = "toolchain-gcc-linux-arm64" cc_toolchain_config( - name = "aarch64_linux_gnu_gcc-linux-x86_64-config", - ar = "/usr/bin/aarch64-linux-gnu-ar", - cc = "/usr/bin/aarch64-linux-gnu-gcc", - compiler = "aarch64-linux-gnu-gcc", - compiler_version = "//:aarch64_linux_gnu_gcc_version", - ld = "/usr/bin/aarch64-linux-gnu-ld", + name = "gcc-linux-arm64-config", + ar = "/usr/local/aarch64-linux-musl/bin/aarch64-linux-musl-ar", + cc = "/usr/local/aarch64-linux-musl/bin/aarch64-linux-musl-gcc", + cc_flags = [ + "-static", + "--sysroot=/usr/local/aarch64-linux-musl", + ], + compiler = "aarch64-linux-musl-gcc", + compiler_version = "//:gcc_version", + ld = "/usr/local/aarch64-linux-musl/bin/aarch64-linux-musl-ld", + ld_flags = ["--sysroot=/usr/local/aarch64-linux-musl"], sys_includes = [ - "/usr/aarch64-linux-gnu/include", - "/usr/lib/gcc/aarch64-linux-gnu/{compiler_version}/include", - "/usr/lib/gcc/aarch64-linux-gnu/{compiler_version}/include-fixed", + "/usr/local/aarch64-linux-musl/aarch64-linux-musl/include", + "/usr/local/aarch64-linux-musl/lib/gcc/aarch64-linux-musl/{compiler_version}/include", ], target_cpu = "arm64", - toolchain_identifier = _aarch64_linux_gnu_gcc, + toolchain_identifier = _arm64_gcc, ) cc_toolchain( - name = "aarch64_linux_gnu_gcc-linux-x86_64", + name = "gcc-linux-arm64", all_files = ":empty", compiler_files = ":empty", dwp_files = ":empty", @@ -70,12 +74,12 @@ cc_toolchain( objcopy_files = ":empty", strip_files = ":empty", supports_param_files = 0, - toolchain_config = ":aarch64_linux_gnu_gcc-linux-x86_64-config", - toolchain_identifier = _aarch64_linux_gnu_gcc, + toolchain_config = ":gcc-linux-arm64-config", + toolchain_identifier = _arm64_gcc, ) toolchain( - name = "aarch64_linux_gnu_gcc-linux-x86_64-toolchain", + name = "gcc-linux-arm64-toolchain", exec_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64", @@ -84,56 +88,7 @@ toolchain( "@platforms//os:linux", "@platforms//cpu:arm64", ], - toolchain = ":aarch64_linux_gnu_gcc-linux-x86_64", - toolchain_type = ":toolchain_type", -) - -# -# clang-linux-x86_64 -# - -# Toolchain identifier. -_x86_64_clang = "toolchain-clang-linux-x86_64" - -cc_toolchain_config( - name = "clang-linux-x86_64-config", - ar = "/usr/bin/ar", - cc = "/usr/bin/clang", - compiler = "clang", - compiler_version = "//:clang_version", - ld = "/usr/bin/ld", - sys_includes = [ - "/usr/lib/clang/{compiler_version}", - "/usr/include", - ], - target_cpu = "x86_64", - toolchain_identifier = _x86_64_clang, -) - -cc_toolchain( - name = "clang-linux-x86_64", - all_files = ":empty", - compiler_files = ":empty", - dwp_files = ":empty", - linker_files = ":empty", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":clang-linux-x86_64-config", - toolchain_identifier = _x86_64_clang, -) - -toolchain( - name = "clang-linux-x86_64-toolchain", - exec_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - target_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - toolchain = ":clang-linux-x86_64", + toolchain = ":gcc-linux-arm64", toolchain_type = ":toolchain_type", ) @@ -146,15 +101,19 @@ _x86_64_gcc = "toolchain-gcc-linux-x86_64" cc_toolchain_config( name = "gcc-linux-x86_64-config", - ar = "/usr/bin/ar", - cc = "/usr/bin/gcc", - compiler = "gcc", + ar = "/usr/local/x86_64-linux-musl/bin/x86_64-linux-musl-ar", + cc = "/usr/local/x86_64-linux-musl/bin/x86_64-linux-musl-gcc", + cc_flags = [ + "-static", + "--sysroot=/usr/local/x86_64-linux-musl", + ], + compiler = "x86_64-linux-musl-gcc", compiler_version = "//:gcc_version", - ld = "/usr/bin/ld", + ld = "/usr/local/x86_64-linux-musl/bin/x86_64-linux-musl-ld", + ld_flags = ["--sysroot=/usr/local/x86_64-linux-musl"], sys_includes = [ - "/usr/lib/gcc/x86_64-pc-linux-gnu/{compiler_version}/include", - "/usr/lib/gcc/x86_64-pc-linux-gnu/{compiler_version}/include-fixed", - "/usr/include", + "/usr/local/x86_64-linux-musl/x86_64-linux-musl/include", + "/usr/local/x86_64-linux-musl/lib/gcc/x86_64-linux-musl/{compiler_version}/include", ], target_cpu = "x86_64", toolchain_identifier = _x86_64_gcc, @@ -290,7 +249,6 @@ cc_toolchain( toolchain( name = "clang-macos-x86_64-toolchain", exec_compatible_with = [ - "//:clang", "@platforms//os:macos", "@platforms//cpu:x86_64", ], @@ -301,3 +259,80 @@ toolchain( toolchain = ":clang-macos-x86_64", toolchain_type = ":toolchain_type", ) + +# +# BOOTSTRAPPING +# + +# We can't build some artifcacts, like musl libc and its toolchain, in Bazel +# itself (i.e. by adding a remote repository to `WORKSPACE.bazel` and a +# corresponding `BUILD` file in `bazel/third_party/`) because doing +# so introduces a circular dependency during Bazel C/C++ toolchain resolution. + +# The prefix for installed external toolchains. +_install_prefix = "/usr/local" + +# musl-cross-make builds musl-libc-compatible gcc toolchains from source. +_musl_cross_make_version = "fe915821b652a7fa37b34a596f47d8e20bc72338" + +_musl_cross_make_archive = "https://github.com/richfelker/musl-cross-make/archive/{}.tar.gz".format(_musl_cross_make_version) + +genrule( + name = "install-aarch64-linux-musl-gcc", + outs = ["install-aarch64-linux-musl-gcc.sh"], + cmd = """ + echo 'aarch64_linux_musl_install={}/aarch64-linux-musl' > $@ + echo 'if [ ! -d $$aarch64_linux_musl_install ]; then' >> $@ + echo ' wget -c {}' >> $@ + echo ' tar -xf {}.tar.gz' >> $@ + echo ' archive=musl-cross-make-{}' >> $@ + echo ' echo OUTPUT=$$aarch64_linux_musl_install > $$archive/config.mak' >> $@ + echo ' TARGET=aarch64-linux-musl make -C$$archive -j`nproc`' >> $@ + echo ' sudo TARGET=aarch64-linux-musl make -C$$archive -j`nproc` install' >> $@ + echo 'fi' >> $@ + """.format( + _install_prefix, + _musl_cross_make_archive, + _musl_cross_make_version, + _musl_cross_make_version, + ), + exec_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:private"], +) + +sh_binary( + name = "aarch64-linux-musl-gcc", + srcs = ["install-aarch64-linux-musl-gcc"], + exec_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:public"], +) + +genrule( + name = "install-x86_64-linux-musl-gcc", + outs = ["install-x86_64-linux-musl-gcc.sh"], + cmd = """ + echo 'x86_64_linux_musl_install={}/x86_64-linux-musl' > $@ + echo 'if [ ! -d $$x86_64_linux_musl_install ]; then' >> $@ + echo ' wget -c {}' >> $@ + echo ' tar -xf {}.tar.gz' >> $@ + echo ' archive=musl-cross-make-{}' >> $@ + echo ' echo OUTPUT=$$x86_64_linux_musl_install > $$archive/config.mak' >> $@ + echo ' TARGET=x86_64-linux-musl make -C$$archive -j`nproc`' >> $@ + echo ' sudo TARGET=x86_64-linux-musl make -C$$archive -j`nproc` install' >> $@ + echo 'fi' >> $@ + """.format( + _install_prefix, + _musl_cross_make_archive, + _musl_cross_make_version, + _musl_cross_make_version, + ), + exec_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:private"], +) + +sh_binary( + name = "x86_64-linux-musl-gcc", + srcs = ["install-x86_64-linux-musl-gcc"], + exec_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:public"], +) diff --git a/bazel/toolchain/cfg.bzl b/bazel/toolchain/cfg.bzl index 6b74befdc7..aa41e1c2c4 100644 --- a/bazel/toolchain/cfg.bzl +++ b/bazel/toolchain/cfg.bzl @@ -10,7 +10,7 @@ load( ) def _cc_toolchain_config_impl(ctx): - ar_flags_feature = feature( + ar_flags = feature( name = "archiver_flags", flag_sets = [ flag_set( @@ -51,6 +51,38 @@ def _cc_toolchain_config_impl(ctx): ), ], ) + features = [ar_flags] + + if len(ctx.attr.cc_flags) > 0: + cc_flags = feature( + name = "cc_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = [flag_group(flags = ctx.attr.cc_flags)], + ), + ], + ) + features.append(cc_flags) + + + if len(ctx.attr.ld_flags) > 0: + ld_flags = feature( + name = "ld_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ], + flag_groups = [flag_group(flags = ctx.attr.ld_flags)], + ), + ], + ) + features.append(ld_flags) # See # https://bazel.build/rules/lib/cc_common#create_cc_toolchain_config_info. @@ -62,7 +94,7 @@ def _cc_toolchain_config_impl(ctx): path.format(compiler_version = ctx.attr.compiler_version[BuildSettingInfo].value) for path in ctx.attr.sys_includes ], - features = [ar_flags_feature], + features = features, toolchain_identifier = ctx.attr.toolchain_identifier, target_system_name = ctx.attr.target_system_name, target_cpu = ctx.attr.target_cpu, @@ -121,8 +153,10 @@ cc_toolchain_config = rule( "abi_libc_version": attr.string(default = "unknown"), "abi_version": attr.string(default = "unknown"), "ar_flags": attr.string(default = "rcsD"), + "cc_flags": attr.string_list(default = []), "cpp": attr.string(default = "/bin/false"), "gcov": attr.string(default = "/bin/false"), + "ld_flags": attr.string_list(default = []), "nm": attr.string(default = "/bin/false"), "objdump": attr.string(default = "/bin/false"), "strip": attr.string(default = "/bin/false"), diff --git a/pkg/ent/BUILD.bazel b/pkg/ent/BUILD.bazel index f02568058a..399f05159f 100644 --- a/pkg/ent/BUILD.bazel +++ b/pkg/ent/BUILD.bazel @@ -39,7 +39,10 @@ cc_test( name = "tests", timeout = "short", srcs = ["tests.c"], - linkstatic = True, + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":ent"], ) diff --git a/pkg/noun/BUILD.bazel b/pkg/noun/BUILD.bazel index df75681c16..1cef05dfe2 100644 --- a/pkg/noun/BUILD.bazel +++ b/pkg/noun/BUILD.bazel @@ -57,6 +57,10 @@ cc_test( name = "hashtable_tests", timeout = "short", srcs = ["hashtable_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":noun"], ) @@ -65,6 +69,10 @@ cc_test( name = "jets_tests", timeout = "short", srcs = ["jets_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":noun"], ) @@ -73,6 +81,10 @@ cc_test( name = "nock_tests", timeout = "short", srcs = ["nock_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":noun"], ) @@ -81,6 +93,10 @@ cc_test( name = "retrieve_tests", timeout = "short", srcs = ["retrieve_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":noun"], ) @@ -89,6 +105,10 @@ cc_test( name = "serial_tests", timeout = "short", srcs = ["serial_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":noun"], ) diff --git a/pkg/ur/BUILD.bazel b/pkg/ur/BUILD.bazel index 5c94446188..e47e798702 100644 --- a/pkg/ur/BUILD.bazel +++ b/pkg/ur/BUILD.bazel @@ -28,7 +28,10 @@ cc_test( name = "tests", timeout = "short", srcs = ["tests.c"], - linkstatic = True, + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":ur"], ) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index dc960b4076..58072b53f5 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -177,7 +177,10 @@ cc_binary( ":pace", ":version", ], - linkstatic = True, + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:public"], deps = [ "//pkg/c3", @@ -201,6 +204,10 @@ cc_test( name = "ames_tests", timeout = "short", srcs = ["ames_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":vere"], ) @@ -209,6 +216,10 @@ cc_test( name = "newt_tests", timeout = "short", srcs = ["newt_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":vere"], ) @@ -217,6 +228,10 @@ cc_test( name = "noun_tests", timeout = "short", srcs = ["noun_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":vere"], ) @@ -225,6 +240,10 @@ cc_test( name = "unix_tests", timeout = "short", srcs = ["unix_tests.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [":vere"], ) @@ -233,6 +252,10 @@ cc_test( name = "benchmarks", timeout = "short", srcs = ["benchmarks.c"], + features = select({ + "@platforms//os:linux": ["fully_static_link"], + "//conditions:default": [], + }), visibility = ["//visibility:private"], deps = [ ":vere",