From 62e2e7069986cb2a7a5fb62adf68bfed993888c6 Mon Sep 17 00:00:00 2001 From: Malte Poll <1780588+malt3@users.noreply.github.com> Date: Thu, 16 Mar 2023 16:13:48 +0100 Subject: [PATCH] bazel: use host platform by default (#1434) --- .bazelrc | 3 -- CMakeLists.txt | 28 ++++++------- bazel/devbuild/BUILD.bazel | 8 ++-- bazel/go/platform.bzl | 49 +++++++++++++++++++++++ bootstrapper/cmd/bootstrapper/BUILD.bazel | 8 ++++ debugd/cmd/debugd/BUILD.bazel | 8 ++++ disk-mapper/cmd/BUILD.bazel | 8 ++++ measurement-reader/cmd/BUILD.bazel | 8 ++++ upgrade-agent/cmd/BUILD.bazel | 8 ++++ 9 files changed, 107 insertions(+), 21 deletions(-) create mode 100644 bazel/go/platform.bzl diff --git a/.bazelrc b/.bazelrc index cecc519ec..6ec0b5b0b 100644 --- a/.bazelrc +++ b/.bazelrc @@ -20,9 +20,6 @@ build --strip=always # set build mode to opt by default (better reproducibility and performance) build --compilation_mode=opt -# compile for linux_amd64 by default (this is the target for any binaries that go into the cluster) -build --platforms @zig_sdk//libc_aware/platform:linux_amd64_gnu.2.34 - # enable tpm simulator for tests test --//bazel/settings:tpm_simulator diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dc574579..714bacdb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,8 +9,8 @@ enable_testing() # disk-mapper # add_custom_target(disk-mapper ALL - COMMAND ${BAZEL} build //disk-mapper/cmd:cmd - COMMAND cp \$$\(${BAZEL} cquery --output=files //disk-mapper/cmd:cmd\) ${CMAKE_BINARY_DIR}/disk-mapper + COMMAND ${BAZEL} build //disk-mapper/cmd:disk-mapper_linux_amd64 + COMMAND cp \$$\(${BAZEL} cquery --output=files //disk-mapper/cmd:disk-mapper_linux_amd64\) ${CMAKE_BINARY_DIR}/disk-mapper COMMAND chmod +w ${CMAKE_BINARY_DIR}/disk-mapper WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} BYPRODUCTS disk-mapper @@ -20,8 +20,8 @@ add_custom_target(disk-mapper ALL # measurement-reader # add_custom_target(measurement-reader ALL - COMMAND ${BAZEL} build //measurement-reader/cmd:cmd - COMMAND cp \$$\(${BAZEL} cquery --output=files //measurement-reader/cmd:cmd\) ${CMAKE_BINARY_DIR}/measurement-reader + COMMAND ${BAZEL} build //measurement-reader/cmd:measurement-reader_linux_amd64 + COMMAND cp \$$\(${BAZEL} cquery --output=files //measurement-reader/cmd:measurement-reader_linux_amd64\) ${CMAKE_BINARY_DIR}/measurement-reader COMMAND chmod +w ${CMAKE_BINARY_DIR}/measurement-reader WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} BYPRODUCTS measurement-reader @@ -31,8 +31,8 @@ add_custom_target(measurement-reader ALL # bootstrapper # add_custom_target(bootstrapper ALL - COMMAND ${BAZEL} build //bootstrapper/cmd/bootstrapper:bootstrapper - COMMAND cp \$$\(${BAZEL} cquery --output=files //bootstrapper/cmd/bootstrapper:bootstrapper\) ${CMAKE_BINARY_DIR}/bootstrapper + COMMAND ${BAZEL} build //bootstrapper/cmd/bootstrapper:bootstrapper_linux_amd64 + COMMAND cp \$$\(${BAZEL} cquery --output=files //bootstrapper/cmd/bootstrapper:bootstrapper_linux_amd64\) ${CMAKE_BINARY_DIR}/bootstrapper COMMAND chmod +w ${CMAKE_BINARY_DIR}/bootstrapper WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} BYPRODUCTS bootstrapper @@ -42,8 +42,8 @@ add_custom_target(bootstrapper ALL # upgrade-agent # add_custom_target(upgrade-agent ALL - COMMAND ${BAZEL} build //upgrade-agent/cmd:cmd - COMMAND cp \$$\(${BAZEL} cquery --output=files //upgrade-agent/cmd:cmd\) ${CMAKE_BINARY_DIR}/upgrade-agent + COMMAND ${BAZEL} build //upgrade-agent/cmd:upgrade_agent_linux_amd64 + COMMAND cp \$$\(${BAZEL} cquery --output=files //upgrade-agent/cmd:upgrade_agent_linux_amd64\) ${CMAKE_BINARY_DIR}/upgrade-agent COMMAND chmod +w ${CMAKE_BINARY_DIR}/upgrade-agent WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} BYPRODUCTS upgrade-agent @@ -53,8 +53,8 @@ add_custom_target(upgrade-agent ALL # cli # add_custom_target(cli ALL - COMMAND ${BAZEL} build --@io_bazel_rules_go//go/config:tags='${CLI_BUILD_TAGS}' --platforms=@local_config_platform//:host //cli:cli_oss - COMMAND cp \$$\(${BAZEL} cquery --@io_bazel_rules_go//go/config:tags='${CLI_BUILD_TAGS}' --platforms=@local_config_platform//:host --output=files //cli:cli_oss\) ${CMAKE_BINARY_DIR}/constellation + COMMAND ${BAZEL} build --@io_bazel_rules_go//go/config:tags='${CLI_BUILD_TAGS}' //cli:cli_oss_host + COMMAND cp \$$\(${BAZEL} cquery --@io_bazel_rules_go//go/config:tags='${CLI_BUILD_TAGS}' --output=files //cli:cli_oss_host\) ${CMAKE_BINARY_DIR}/constellation COMMAND chmod +w ${CMAKE_BINARY_DIR}/constellation WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} BYPRODUCTS constellation @@ -64,8 +64,8 @@ add_custom_target(cli ALL # debugd # add_custom_target(debugd ALL - COMMAND ${BAZEL} build //debugd/cmd/debugd:debugd - COMMAND cp \$$\(${BAZEL} cquery --output=files //debugd/cmd/debugd:debugd\) ${CMAKE_BINARY_DIR}/debugd + COMMAND ${BAZEL} build //debugd/cmd/debugd:debugd_linux_amd64 + COMMAND cp \$$\(${BAZEL} cquery --output=files //debugd/cmd/debugd:debugd_linux_amd64\) ${CMAKE_BINARY_DIR}/debugd COMMAND chmod +w ${CMAKE_BINARY_DIR}/debugd WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} BYPRODUCTS debugd @@ -75,8 +75,8 @@ add_custom_target(debugd ALL # cdbg # add_custom_target(cdbg ALL - COMMAND ${BAZEL} build --platforms=@local_config_platform//:host //debugd/cmd/cdbg:cdbg - COMMAND cp \$$\(${BAZEL} cquery --platforms=@local_config_platform//:host --output=files //debugd/cmd/cdbg:cdbg\) ${CMAKE_BINARY_DIR}/cdbg + COMMAND ${BAZEL} build //debugd/cmd/cdbg:cdbg_host + COMMAND cp \$$\(${BAZEL} cquery --output=files //debugd/cmd/cdbg:cdbg_host\) ${CMAKE_BINARY_DIR}/cdbg COMMAND chmod +w ${CMAKE_BINARY_DIR}/cdbg WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} BYPRODUCTS cdbg diff --git a/bazel/devbuild/BUILD.bazel b/bazel/devbuild/BUILD.bazel index 73220965d..f6fa68537 100644 --- a/bazel/devbuild/BUILD.bazel +++ b/bazel/devbuild/BUILD.bazel @@ -3,16 +3,16 @@ load("//bazel/sh:def.bzl", "sh_template") sh_template( name = "devbuild", data = [ - "//bootstrapper/cmd/bootstrapper", + "//bootstrapper/cmd/bootstrapper:bootstrapper_linux_amd64", "//cli:cli_oss_host", "//debugd/cmd/cdbg:cdbg_host", - "//upgrade-agent/cmd", + "//upgrade-agent/cmd:upgrade_agent_linux_amd64", ], substitutions = { - "@@BOOTSTRAPPER@@": "$(rootpath //bootstrapper/cmd/bootstrapper)", + "@@BOOTSTRAPPER@@": "$(rootpath //bootstrapper/cmd/bootstrapper:bootstrapper_linux_amd64)", "@@CDBG@@": "$(rootpath //debugd/cmd/cdbg:cdbg_host)", "@@CLI@@": "$(rootpath //cli:cli_oss_host)", - "@@UPGRADE_AGENT@@": "$(rootpath //upgrade-agent/cmd)", + "@@UPGRADE_AGENT@@": "$(rootpath //upgrade-agent/cmd:upgrade_agent_linux_amd64)", }, template = "prepare_developer_workspace.sh.in", visibility = ["//visibility:public"], diff --git a/bazel/go/platform.bzl b/bazel/go/platform.bzl new file mode 100644 index 000000000..c4e48ec1e --- /dev/null +++ b/bazel/go/platform.bzl @@ -0,0 +1,49 @@ +"""A rule to build a single executable for a specific platform.""" + +def _platform_transition_impl(settings, attr): + _ignore = settings # @unused + return { + "//command_line_option:platforms": "{}".format(attr.platform), + } + +_platform_transition = transition( + implementation = _platform_transition_impl, + inputs = [], + outputs = [ + "//command_line_option:platforms", + ], +) + +def _platform_binary_impl(ctx): + out = ctx.actions.declare_file("{}_{}".format(ctx.file.target_file.basename, ctx.attr.platform)) + ctx.actions.symlink(output = out, target_file = ctx.file.target_file) + + return [ + DefaultInfo( + executable = out, + files = depset([out]), + runfiles = ctx.runfiles(files = ctx.files.target_file), + ), + ] + +_attrs = { + "platform": attr.string( + doc = "The platform to build the target for.", + ), + "target_file": attr.label( + allow_single_file = True, + mandatory = True, + doc = "Target to build.", + ), + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), +} + +# wrap a single exectable and build it for the specified platform. +platform_binary = rule( + implementation = _platform_binary_impl, + cfg = _platform_transition, + attrs = _attrs, + executable = True, +) diff --git a/bootstrapper/cmd/bootstrapper/BUILD.bazel b/bootstrapper/cmd/bootstrapper/BUILD.bazel index 654b24171..3f843316d 100644 --- a/bootstrapper/cmd/bootstrapper/BUILD.bazel +++ b/bootstrapper/cmd/bootstrapper/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("//bazel/go:platform.bzl", "platform_binary") go_library( name = "bootstrapper_lib", @@ -54,3 +55,10 @@ go_binary( }), visibility = ["//visibility:public"], ) + +platform_binary( + name = "bootstrapper_linux_amd64", + platform = "@zig_sdk//libc_aware/platform:linux_amd64_gnu.2.34", + target_file = ":bootstrapper", + visibility = ["//visibility:public"], +) diff --git a/debugd/cmd/debugd/BUILD.bazel b/debugd/cmd/debugd/BUILD.bazel index 3fca6914b..6e6e1b8bd 100644 --- a/debugd/cmd/debugd/BUILD.bazel +++ b/debugd/cmd/debugd/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("//bazel/go:platform.bzl", "platform_binary") go_library( name = "debugd_lib", @@ -33,3 +34,10 @@ go_binary( pure = "on", visibility = ["//visibility:public"], ) + +platform_binary( + name = "debugd_linux_amd64", + platform = "@zig_sdk//libc_aware/platform:linux_amd64_gnu.2.34", + target_file = ":debugd", + visibility = ["//visibility:public"], +) diff --git a/disk-mapper/cmd/BUILD.bazel b/disk-mapper/cmd/BUILD.bazel index 93e87730a..0022857ff 100644 --- a/disk-mapper/cmd/BUILD.bazel +++ b/disk-mapper/cmd/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("//bazel/go:platform.bzl", "platform_binary") go_library( name = "cmd_lib", @@ -35,3 +36,10 @@ go_binary( embed = [":cmd_lib"], visibility = ["//visibility:public"], ) + +platform_binary( + name = "disk-mapper_linux_amd64", + platform = "@zig_sdk//libc_aware/platform:linux_amd64_gnu.2.34", + target_file = ":cmd", + visibility = ["//visibility:public"], +) diff --git a/measurement-reader/cmd/BUILD.bazel b/measurement-reader/cmd/BUILD.bazel index 5006b7b91..ae341d987 100644 --- a/measurement-reader/cmd/BUILD.bazel +++ b/measurement-reader/cmd/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("//bazel/go:platform.bzl", "platform_binary") go_library( name = "cmd_lib", @@ -21,3 +22,10 @@ go_binary( embed = [":cmd_lib"], visibility = ["//visibility:public"], ) + +platform_binary( + name = "measurement-reader_linux_amd64", + platform = "@zig_sdk//libc_aware/platform:linux_amd64_gnu.2.34", + target_file = ":cmd", + visibility = ["//visibility:public"], +) diff --git a/upgrade-agent/cmd/BUILD.bazel b/upgrade-agent/cmd/BUILD.bazel index a2567ae1b..e1cc48bdd 100644 --- a/upgrade-agent/cmd/BUILD.bazel +++ b/upgrade-agent/cmd/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("//bazel/go:platform.bzl", "platform_binary") go_library( name = "cmd_lib", @@ -22,3 +23,10 @@ go_binary( pure = "on", visibility = ["//visibility:public"], ) + +platform_binary( + name = "upgrade_agent_linux_amd64", + platform = "@zig_sdk//libc_aware/platform:linux_amd64_gnu.2.34", + target_file = ":cmd", + visibility = ["//visibility:public"], +)