diff --git a/WORKSPACE b/WORKSPACE index 669fbbf..d93ac10 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -19,3 +19,14 @@ http_archive( strip_prefix = "pico-examples-sdk-1.4.0", sha256 = "a07789d702f8e6034c42e04a3f9dda7ada4ae7c8e8d320c6be6675090c007861", ) + +# Hermetic toolchain +git_repository( + name = "arm_none_eabi", + commit = "4f3f31d629259e65e98b3aa7d8cb8c916cf7e03c", + remote = "https://github.com/hexdae/bazel-arm-none-eabi", +) + +load("@arm_none_eabi//:deps.bzl", "arm_none_eabi_deps") + +arm_none_eabi_deps() diff --git a/pico/BUILD.pico-sdk b/pico/BUILD.pico-sdk index d0012a2..9cb155a 100644 --- a/pico/BUILD.pico-sdk +++ b/pico/BUILD.pico-sdk @@ -54,8 +54,9 @@ genrule( name = "bs2_default_bin", srcs = ["bs2_default.elf"], outs = ["bs2_default.bin"], - cmd = "$(OBJCOPY) -O binary $< $@", + cmd = "$(execpath @arm_none_eabi//:objcopy) -O binary $< $@", toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"], + tools = ["@arm_none_eabi//:objcopy"], ) cc_binary( diff --git a/pico/repositories.bzl b/pico/repositories.bzl index 3f53fab..947400a 100644 --- a/pico/repositories.bzl +++ b/pico/repositories.bzl @@ -1,6 +1,5 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") -load("//toolchain/private:defs.bzl", "gcc_arm_embedded_toolchain") def rules_pico_dependencies(): maybe( @@ -39,10 +38,5 @@ def rules_pico_dependencies(): sha256 = "329f125f681b9adbfc89fff716026a6c3dbe517f172451dd04ceb91b10e7c5e1", ) - maybe( - gcc_arm_embedded_toolchain, - name = "gcc-arm-embedded", - ) - def rules_pico_toolchains(): - native.register_toolchains("//toolchain:gcc-pico") + native.register_toolchains("//toolchain:gcc-pico_linux_x86_64") diff --git a/platforms/BUILD b/platforms/BUILD index 7144984..3e16edf 100644 --- a/platforms/BUILD +++ b/platforms/BUILD @@ -5,5 +5,4 @@ platform( "@platforms//cpu:armv6-m", "@koro-platforms//cxx:14", ], - target_platform = True, ) diff --git a/toolchain/BUILD b/toolchain/BUILD index 99d58b9..6c86ec7 100644 --- a/toolchain/BUILD +++ b/toolchain/BUILD @@ -1,10 +1,8 @@ -load(":defs.bzl", "gcc_embedded_toolchain") +load("@arm_none_eabi//toolchain:toolchain.bzl", "arm_none_eabi_toolchain") package(default_visibility = ["//visibility:public"]) -filegroup(name = "empty") - -gcc_embedded_toolchain( +arm_none_eabi_toolchain( name = "gcc-pico", copts = [ "-mcpu=cortex-m0plus", diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl deleted file mode 100644 index 4f9d2f9..0000000 --- a/toolchain/defs.bzl +++ /dev/null @@ -1,28 +0,0 @@ -load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS") -load("@rules_cc//cc:defs.bzl", "cc_toolchain") -load("@gcc-arm-embedded//:cc_toolchain_config.bzl", "cc_toolchain_config") - -def gcc_embedded_toolchain(*, name, copts, cxxopts = None, target_compatible_with): - cc_toolchain_config( - name = "{}-config".format(name), - copts = copts, - cxxopts = cxxopts, - ) - cc_toolchain( - name = "{}-toolchain".format(name), - all_files = "@rules_pico//toolchain:empty", - compiler_files = "@rules_pico//toolchain:empty", - dwp_files = "@rules_pico//toolchain:empty", - linker_files = "@rules_pico//toolchain:empty", - objcopy_files = "@rules_pico//toolchain:empty", - strip_files = "@rules_pico//toolchain:empty", - supports_param_files = 0, - toolchain_config = ":{}-config".format(name), - ) - native.toolchain( - name = name, - exec_compatible_with = HOST_CONSTRAINTS, - target_compatible_with = target_compatible_with, - toolchain = ":{}-toolchain".format(name), - toolchain_type = "@rules_cc//cc:toolchain_type", - ) diff --git a/toolchain/private/BUILD b/toolchain/private/BUILD deleted file mode 100644 index e69de29..0000000 diff --git a/toolchain/private/cc_toolchain_config.bzl.tpl b/toolchain/private/cc_toolchain_config.bzl.tpl deleted file mode 100644 index ba32b9d..0000000 --- a/toolchain/private/cc_toolchain_config.bzl.tpl +++ /dev/null @@ -1,251 +0,0 @@ -# -*- bazel-starlark -*- - -load( - "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", - "feature", - "flag_group", - "flag_set", - "tool_path", - "with_feature_set", -) -load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") -load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") - -all_compile_actions = [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ACTION_NAMES.linkstamp_compile, - ACTION_NAMES.assemble, - ACTION_NAMES.preprocess_assemble, - ACTION_NAMES.cpp_header_parsing, - ACTION_NAMES.cpp_module_compile, - ACTION_NAMES.cpp_module_codegen, - ACTION_NAMES.clif_match, - ACTION_NAMES.lto_backend, -] - -all_cpp_compile_actions = [ - ACTION_NAMES.cpp_compile, - ACTION_NAMES.linkstamp_compile, - ACTION_NAMES.cpp_header_parsing, - ACTION_NAMES.cpp_module_compile, - ACTION_NAMES.cpp_module_codegen, - ACTION_NAMES.clif_match, -] - -all_link_actions = [ - ACTION_NAMES.cpp_link_executable, - ACTION_NAMES.cpp_link_dynamic_library, - ACTION_NAMES.cpp_link_nodeps_dynamic_library, -] - -def _tool(ctx, tool_name): - return "{gccpath}/bin/arm-none-eabi-{}".format(tool_name) - -def _impl(ctx): - default_link_flags_feature = feature( - name = "default_link_flags", - enabled = True, - flag_sets = [ - flag_set( - actions = all_link_actions, - flag_groups = [ - flag_group( - flags = [ - "-no-canonical-prefixes", - #"--verbose", - "-lstdc++", - "-lm", - ], - ), - ], - ), - ], - ) - - unfiltered_compile_flags_feature = feature( - name = "unfiltered_compile_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\"", - ], - ), - ], - ), - ], - ) - - supports_pic_feature = feature(name = "supports_pic", enabled = True) - - flag_sets = [ - flag_set( - actions = all_compile_actions, - flag_groups = [ - flag_group( - flags = [ - "-U_FORTIFY_SOURCE", - "-D_FORTIFY_SOURCE=1", - "-DFREESTANDING", - "-fstack-protector", - "-Wall", - "-fno-omit-frame-pointer", - # "-isystem {gccpath}/arm-none-eabi/include", - ] + ctx.attr.copts, - ), - ], - ), - flag_set( - actions = all_compile_actions, - flag_groups = [flag_group(flags = ["-g"])], - with_features = [with_feature_set(features = ["dbg"])], - ), - flag_set( - actions = all_compile_actions, - flag_groups = [ - flag_group( - flags = [ - "-g", - "-O2", - "-DNDEBUG", - "-ffunction-sections", - "-fdata-sections", - ], - ), - ], - with_features = [with_feature_set(features = ["opt"])], - ), - ] - if ctx.attr.cxxopts: - flag_sets.append( - flag_set( - actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend], - flag_groups = [flag_group(flags = [] + ctx.attr.cxxopts)], - ) - ) - - default_compile_flags_feature = feature( - name = "default_compile_flags", - enabled = True, - flag_sets = flag_sets, - ) - - opt_feature = feature(name = "opt") - - supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True) - - objcopy_embed_flags_feature = feature( - name = "objcopy_embed_flags", - enabled = True, - flag_sets = [ - flag_set( - actions = ["objcopy_embed_data"], - flag_groups = [flag_group(flags = ["-I", "binary"])], - ), - ], - ) - - dbg_feature = feature(name = "dbg") - - user_compile_flags_feature = feature( - name = "user_compile_flags", - enabled = True, - flag_sets = [ - flag_set( - actions = all_compile_actions, - flag_groups = [ - flag_group( - flags = ["%{user_compile_flags}"], - iterate_over = "user_compile_flags", - expand_if_available = "user_compile_flags", - ), - ], - ), - ], - ) - - tool_paths = [ - tool_path( - name = "gcc", - path = _tool(ctx, "gcc"), - ), - tool_path( - name = "ld", - path = _tool(ctx, "ld"), - ), - tool_path( - name = "ar", - path = _tool(ctx, "ar"), - ), - tool_path( - name = "cpp", - path = _tool(ctx, "cpp"), - ), - tool_path( - name = "gcov", - path = _tool(ctx, "gcov"), - ), - tool_path( - name = "nm", - path = _tool(ctx, "nm"), - ), - tool_path( - name = "objcopy", - path = _tool(ctx, "objcopy"), - ), - tool_path( - name = "objdump", - path = _tool(ctx, "objdump"), - ), - tool_path( - name = "strip", - path = _tool(ctx, "strip"), - ), - ] - features = [ - default_compile_flags_feature, - default_link_flags_feature, - objcopy_embed_flags_feature, - opt_feature, - dbg_feature, - user_compile_flags_feature, - #sysroot_feature, - unfiltered_compile_flags_feature, - ] - cxx_builtin_include_directories = [ - "{gccpath}/arm-none-eabi/include", - "{gccpath}/lib/gcc/arm-none-eabi/{gccver}/include", - "{gccpath}/lib/gcc/arm-none-eabi/{gccver}/include-fixed", - "{gccpath}/include/newlib", - ] - return cc_common.create_cc_toolchain_config_info( - ctx = ctx, - features = features, - toolchain_identifier = "gcc-toolchain", - host_system_name = "local", - target_system_name = "local", - target_cpu = "local", - target_libc = "local", - compiler = "gcc", - abi_version = "local", - abi_libc_version = "local", - tool_paths = tool_paths, - cxx_builtin_include_directories = cxx_builtin_include_directories, - ) - -cc_toolchain_config = rule( - implementation = _impl, - attrs = { - "copts": attr.string_list(default = []), - "cxxopts": attr.string_list(default = []), - }, - provides = [CcToolchainConfigInfo], -) diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl deleted file mode 100644 index 28b8f01..0000000 --- a/toolchain/private/defs.bzl +++ /dev/null @@ -1,45 +0,0 @@ -def _get_gcc_path(rctx): - res = rctx.execute([ - rctx.which("python3"), - rctx.path(rctx.attr._detect), - ]) - if res.return_code: - fail("Failed to detect arm-none-eabi toolchain path: \n%s\n%s" % (res.stdout, res.stderr)) - return res.stdout.strip() - -def _get_gcc_version(rctx, gccpath): - res = rctx.execute([gccpath + "/bin/arm-none-eabi-gcc", "-v"]) - if res.return_code == 0: - for line in res.stderr.split("\n"): - if line.startswith("gcc version "): - return line.split(" ")[2] - fail("Failed to detect arm-none-eabi toolchain version: \n%s\n%s" % (res.stdout, res.stderr)) - -def _gcc_arm_embedded_toolchain_impl(rctx): - gccpath = _get_gcc_path(rctx) - gccver = _get_gcc_version(rctx, gccpath) - rctx.file("BUILD", content = "") - rctx.file("WORKSPACE", content = "") - rctx.template( - "cc_toolchain_config.bzl", - rctx.attr._toolchain_config_template, - { - "{gccpath}": gccpath, - "{gccver}": gccver, - }, - ) - -gcc_arm_embedded_toolchain = repository_rule( - implementation = _gcc_arm_embedded_toolchain_impl, - local = True, - attrs = { - "_toolchain_config_template": attr.label( - default = "@rules_pico//toolchain/private:cc_toolchain_config.bzl.tpl", - allow_single_file = True, - ), - "_detect": attr.label( - default = "@rules_pico//toolchain/private:detect.py", - allow_single_file = True, - ), - }, -) diff --git a/toolchain/private/detect.py b/toolchain/private/detect.py deleted file mode 100644 index a2cb4e2..0000000 --- a/toolchain/private/detect.py +++ /dev/null @@ -1,39 +0,0 @@ -import os -import platform -import sys -from pathlib import Path -import shutil - -if platform.system() == "FreeBSD": - path = os.popen("readlink -f /usr/local/gcc-arm-embedded", mode = "r").read() -elif platform.system() == "Linux": - path = "/usr" -elif platform.system() == "Darwin": - gcc = shutil.which('arm-none-eabi-gcc') - if not gcc: - print(f"arm-none-eabi-gcc not found on PATH", file=sys.stderr) - sys.exit(1) - # Ask GCC for its own location, in case we've found a shim (e.g. Chocolatey) - sysroot = os.popen(f"{gcc} -print-sysroot", mode = "r").read() - if sysroot and not sysroot.isspace(): - path = Path(sysroot).parent.resolve().as_posix() - else: - # Some installs don't know their own sysroot. Use the binary's location. - path = Path(gcc).parent.parent.resolve().as_posix() -elif platform.system() == "Windows": - gcc = shutil.which('arm-none-eabi-gcc') - if not gcc: - print(f"arm-none-eabi-gcc not found on PATH", file=sys.stderr) - sys.exit(1) - # Ask GCC for its own location, in case we've found a shim (e.g. Chocolatey) - sysroot = os.popen(f"{gcc} -print-sysroot", mode = "r").read() - if sysroot and not sysroot.isspace(): - path = Path(sysroot).parent.resolve().as_posix() - else: - # Some installs don't know their own sysroot. Use the binary's location. - path = Path(gcc).parent.parent.resolve().as_posix() -else: - print(f"Don't know how to detect toolchain path on {platform.system()}", file=sys.stderr) - sys.exit(1) - -print(path)