diff --git a/BUILD b/BUILD index 15a4d6b..e69de29 100644 --- a/BUILD +++ b/BUILD @@ -1,7 +0,0 @@ -filegroup( - name = "clang_tidy_config", - srcs = [ - ".clang-tidy", - ], - visibility = ["//visibility:public"], -) diff --git a/Dockerfile b/Dockerfile index d64f3ce..7f42320 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,8 +43,12 @@ RUN apt-get update && apt-get install -y \ clang-format-18 \ clang-tidy-18 \ lld-18 \ - lldb-18 \ - && rm -rf /var/lib/apt/lists/* + lldb-18 + +# Cross-compilation +RUN apt-get install -y \ + gcc-aarch64-linux-gnu \ + g++-aarch64-linux-gnu RUN pip install yamllint diff --git a/MODULE.bazel b/MODULE.bazel index dd9e523..0bd5c81 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -20,5 +20,6 @@ git_override( bazel_dep(name = "platforms", version = "0.0.10") register_toolchains( - "//toolchain:cc_toolchain_for_linux_x86_64", + "//toolchain/x86_64_linux", + "//toolchain/aarch64_linux", ) diff --git a/bazel/platforms/BUILD b/bazel/platforms/BUILD new file mode 100644 index 0000000..436a02e --- /dev/null +++ b/bazel/platforms/BUILD @@ -0,0 +1,33 @@ +config_setting( + name = "x86_64_config", + constraint_values = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + visibility = ["//visibility:public"], +) + +config_setting( + name = "aarch64_config", + constraint_values = [ + "@platforms//cpu:aarch64", + "@platforms//os:linux", + ], + visibility = ["//visibility:public"], +) + +platform( + name = "x86_64_linux", + constraint_values = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], +) + +platform( + name = "aarch64_linux", + constraint_values = [ + "@platforms//cpu:aarch64", + "@platforms//os:linux", + ], +) diff --git a/examples/cpp/BUILD b/examples/cpp/BUILD index b43cd02..0ee6532 100644 --- a/examples/cpp/BUILD +++ b/examples/cpp/BUILD @@ -2,17 +2,19 @@ cc_library( name = "lib", srcs = ["hello.cpp"], hdrs = ["hello.hpp"], - features = [ - "warnings_critical_code", - ], + features = select({ + "//bazel/platforms:aarch64_config": ["warnings_critical_code_gcc"], + "//conditions:default": ["warnings_critical_code_clang"], + }), ) cc_binary( name = "bin", srcs = ["main.cpp"], - features = [ - "warnings_critical_code", - ], + features = select({ + "//bazel/platforms:aarch64_config": ["warnings_critical_code_gcc"], + "//conditions:default": ["warnings_critical_code_clang"], + }), deps = [ ":lib", ], diff --git a/toolchain/BUILD b/toolchain/BUILD index fa04a03..e69de29 100644 --- a/toolchain/BUILD +++ b/toolchain/BUILD @@ -1,34 +0,0 @@ -load(":cc_toolchain_config.bzl", "cc_toolchain_config") - -package(default_visibility = ["//visibility:public"]) - -filegroup(name = "empty") - -cc_toolchain_config(name = "linux_x86_64_toolchain_config") - -cc_toolchain( - name = "linux_x86_64_toolchain", - all_files = ":empty", - compiler_files = ":empty", - dwp_files = ":empty", - linker_files = ":empty", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":linux_x86_64_toolchain_config", - toolchain_identifier = "linux_x86_64_toolchain", -) - -toolchain( - name = "cc_toolchain_for_linux_x86_64", - exec_compatible_with = [ - "@platforms//cpu:x86_64", - "@platforms//os:linux", - ], - target_compatible_with = [ - "@platforms//cpu:x86_64", - "@platforms//os:linux", - ], - toolchain = ":linux_x86_64_toolchain", - toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", -) diff --git a/toolchain/aarch64_linux/BUILD b/toolchain/aarch64_linux/BUILD new file mode 100644 index 0000000..953a4af --- /dev/null +++ b/toolchain/aarch64_linux/BUILD @@ -0,0 +1,34 @@ +load("//toolchain/aarch64_linux:cc_toolchain_config.bzl", "cc_toolchain_config") + +package(default_visibility = ["//visibility:public"]) + +filegroup(name = "empty") + +cc_toolchain_config(name = "toolchain_config") + +cc_toolchain( + name = "cc_toolchain", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":toolchain_config", + toolchain_identifier = "aarch64_linux_toolchain", +) + +toolchain( + name = "aarch64_linux", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:linux", + ], + toolchain = "cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/toolchain/aarch64_linux/cc_toolchain_config.bzl b/toolchain/aarch64_linux/cc_toolchain_config.bzl new file mode 100644 index 0000000..538c140 --- /dev/null +++ b/toolchain/aarch64_linux/cc_toolchain_config.bzl @@ -0,0 +1,258 @@ +""" +Toolchains +""" + +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool_path") + +all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + +all_compile_actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.c_compile, + ACTION_NAMES.clif_match, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.lto_backend, + ACTION_NAMES.preprocess_assemble, +] + +def _impl(ctx): + tool_paths = [ + tool_path(name = "gcc", path = "/usr/bin/aarch64-linux-gnu-gcc"), + tool_path(name = "ld", path = "/usr/bin/aarch64-linux-gnu-ld"), + tool_path(name = "ar", path = "/usr/bin/aarch64-linux-gnu-ar"), + tool_path(name = "cpp", path = "/usr/bin/aarch64-linux-gnu-cpp"), + tool_path(name = "gcov", path = "/usr/bin/aarch64-linux-gnu-gcov"), + tool_path(name = "nm", path = "/usr/bin/aarch64-linux-gnu-nm"), + tool_path(name = "objdump", path = "/usr/bin/aarch64-linux-gnu-ld-objdump"), + tool_path(name = "strip", path = "/usr/bin/aarch64-linux-gnu-strip"), + ] + + features = [ + feature( + name = "default_compiler_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + "-no-canonical-prefixes", + "-fno-canonical-system-headers", + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + ], + ), + ], + ), + ], + ), + feature( + name = "default_linker_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group( + flags = [ + "-lstdc++", + "-lm", + ], + ), + ], + ), + ], + ), + feature( + name = "stack_protector", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions + all_compile_actions, + flag_groups = [ + flag_group( + flags = ["-fstack-protector-strong"], + ), + ], + ), + ], + ), + feature( + name = "debug_symbols", + enabled = False, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group(flags = ["-g"]), + ], + ), + ], + ), + feature( + name = "pic", + enabled = True, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.assemble, ACTION_NAMES.preprocess_assemble], + flag_groups = [ + flag_group(flags = ["-fPIC"]), + ], + ), + ], + ), + # Sanitizer + feature( + name = "asan", + enabled = False, + flag_sets = [ + flag_set( + actions = all_link_actions + all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + "-fsanitize=address", + "-fno-omit-frame-pointer", + ], + ), + ], + ), + ], + ), + feature( + name = "ubsan", + enabled = False, + flag_sets = [ + flag_set( + actions = all_link_actions + all_compile_actions, + flag_groups = [ + flag_group( + flags = ["-fsanitize=undefined"], + ), + ], + ), + ], + ), + feature( + name = "tsan", + enabled = False, + flag_sets = [ + flag_set( + actions = all_link_actions + all_compile_actions, + flag_groups = [ + flag_group( + flags = ["-fsanitize=thread"], + ), + ], + ), + ], + ), + # Warning options + feature( + name = "warnings_critical_code_clang", + enabled = False, + flag_sets = [ + flag_set( + actions = all_link_actions + all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + "-Weverything", + "-Werror", + "-Wno-c++98-compat", + "-Wno-c++98-compat-pedantic", + ], + ), + ], + ), + ], + ), + feature( + name = "warnings_critical_code_gcc", + enabled = False, + flag_sets = [ + flag_set( + actions = all_link_actions + all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wnon-virtual-dtor", + "-Wold-style-cast", + "-Wcast-align", + "-Wunused", + "-Woverloaded-virtual", + "-Wconversion", + "-Wsign-conversion", + "-Wmisleading-indentation", + "-Wnull-dereference", + "-Wformat=2", + "-Wimplicit-fallthrough", + "-Werror", + "-Wno-c++98-compat", + "-Wno-c++98-compat-pedantic", + ], + ), + ], + ), + ], + ), + feature( + name = "warnings_default", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions + all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + "-Wall", + "-Wshadow", + "-Wno-c++98-compat", + "-Wno-c++98-compat-pedantic", + ], + ), + ], + ), + ], + ), + ] + + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = features, + cxx_builtin_include_directories = [ + "/usr/aarch64-linux-gnu", + "/usr/lib/gcc-cross/aarch64-linux-gnu", + ], + toolchain_identifier = "aarch64_linux-toolchain", + host_system_name = "local", + target_system_name = "unknown", + target_cpu = "unknown", + target_libc = "unknown", + compiler = "unknown", + abi_version = "unknown", + abi_libc_version = "unknown", + tool_paths = tool_paths, + ) + +cc_toolchain_config = rule( + implementation = _impl, + attrs = {}, + provides = [CcToolchainConfigInfo], +) diff --git a/toolchain/x86_64_linux/BUILD b/toolchain/x86_64_linux/BUILD new file mode 100644 index 0000000..2d9bbbe --- /dev/null +++ b/toolchain/x86_64_linux/BUILD @@ -0,0 +1,34 @@ +load("//toolchain/x86_64_linux:cc_toolchain_config.bzl", "cc_toolchain_config") + +package(default_visibility = ["//visibility:public"]) + +filegroup(name = "empty") + +cc_toolchain_config(name = "toolchain_config") + +cc_toolchain( + name = "cc_toolchain", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":toolchain_config", + toolchain_identifier = "x86_64_linux_toolchain", +) + +toolchain( + name = "x86_64_linux", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/x86_64_linux/cc_toolchain_config.bzl similarity index 74% rename from toolchain/cc_toolchain_config.bzl rename to toolchain/x86_64_linux/cc_toolchain_config.bzl index 23eff2e..b9d87c4 100644 --- a/toolchain/cc_toolchain_config.bzl +++ b/toolchain/x86_64_linux/cc_toolchain_config.bzl @@ -11,6 +11,19 @@ all_link_actions = [ ACTION_NAMES.cpp_link_nodeps_dynamic_library, ] +all_compile_actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.c_compile, + ACTION_NAMES.clif_match, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.lto_backend, + ACTION_NAMES.preprocess_assemble, +] + def _impl(ctx): tool_paths = [ tool_path(name = "gcc", path = "/usr/bin/clang-18"), @@ -25,7 +38,26 @@ def _impl(ctx): ] features = [ - # Default linker flags + feature( + name = "default_compiler_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + "-no-canonical-prefixes", + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + ], + ), + ], + ), + ], + ), feature( name = "default_linker_flags", enabled = True, @@ -33,24 +65,25 @@ def _impl(ctx): flag_set( actions = all_link_actions, flag_groups = [ - flag_group(flags = ["-lstdc++", "-lm"]), + flag_group( + flags = [ + "-lstdc++", + "-lm", + ], + ), ], ), ], ), - # Enable stack protector feature( name = "stack_protector", enabled = True, flag_sets = [ flag_set( - actions = all_link_actions + [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ], + actions = all_link_actions + all_compile_actions, flag_groups = [ flag_group( - flags = ["-fstack-protector-strong"], # Or use -fstack-protector/-fstack-protector-all + flags = ["-fstack-protector-strong"], ), ], ), @@ -68,7 +101,6 @@ def _impl(ctx): ), ], ), - # Enable position-independent code feature( name = "pic", enabled = True, @@ -81,16 +113,13 @@ def _impl(ctx): ), ], ), - # Enable Sanitizer + # Sanitizer feature( name = "asan", enabled = False, flag_sets = [ flag_set( - actions = all_link_actions + [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ], + actions = all_link_actions + all_compile_actions, flag_groups = [ flag_group( flags = [ @@ -102,16 +131,12 @@ def _impl(ctx): ), ], ), - # Enable UndefinedBehaviorSanitizer feature( name = "ubsan", enabled = False, flag_sets = [ flag_set( - actions = all_link_actions + [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ], + actions = all_link_actions + all_compile_actions, flag_groups = [ flag_group( flags = ["-fsanitize=undefined"], @@ -120,16 +145,12 @@ def _impl(ctx): ), ], ), - # Enable ThreadSanitizer feature( name = "tsan", enabled = False, flag_sets = [ flag_set( - actions = all_link_actions + [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ], + actions = all_link_actions + all_compile_actions, flag_groups = [ flag_group( flags = ["-fsanitize=thread"], @@ -138,16 +159,12 @@ def _impl(ctx): ), ], ), - # Warning options feature( - name = "warnings_critical_code", + name = "warnings_critical_code_clang", enabled = False, flag_sets = [ flag_set( - actions = all_link_actions + [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ], + actions = all_link_actions + all_compile_actions, flag_groups = [ flag_group( flags = [ @@ -162,14 +179,11 @@ def _impl(ctx): ], ), feature( - name = "warnings_medium_code", + name = "warnings_critical_code_gcc", enabled = False, flag_sets = [ flag_set( - actions = all_link_actions + [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ], + actions = all_link_actions + all_compile_actions, flag_groups = [ flag_group( flags = [ @@ -202,10 +216,7 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_link_actions + [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ], + actions = all_link_actions + all_compile_actions, flag_groups = [ flag_group( flags = [ @@ -226,13 +237,14 @@ def _impl(ctx): features = features, cxx_builtin_include_directories = [ "/usr/lib/llvm-18/lib/clang/18/include", - "/usr/lib/llvm-18/lib/clang/18/share", # Add the directory containing `asan_ignorelist.txt` + "/usr/lib/llvm-18/lib/clang/18/share", # Dir containing `asan_ignorelist.txt` + "/usr/lib/clang/18/include", "/usr/include", ], - toolchain_identifier = "local", + toolchain_identifier = "linux_aarch64-toolchain", host_system_name = "local", - target_system_name = "local", - target_cpu = "k8", + target_system_name = "unknown", + target_cpu = "unknown", target_libc = "unknown", compiler = "clang", abi_version = "unknown",