-
Notifications
You must be signed in to change notification settings - Fork 0
/
BUILD.bazel
163 lines (147 loc) · 4.62 KB
/
BUILD.bazel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_binary")
load("@rules_oci//oci:defs.bzl", "oci_push")
load("@rules_rust//rust:defs.bzl", "rust_doc", "rust_doc_test")
load("//:build/binary.bzl", "build_binary_opt")
load("//:build/container.bzl", "build_multi_arch_image", "tag_with_commit_and_timestamp")
###############################################################################
# This setting is needed for release mode compiler optimization
###############################################################################
config_setting(
name = "release",
values = {
"compilation_mode": "opt",
},
)
###############################################################################
# Rust Binary
###############################################################################
package(default_visibility = ["//visibility:public"])
filegroup(
name = "all",
srcs = [
":hello_world_aarch64",
":hello_world_host",
":hello_world_x86_64",
],
)
build_binary_opt(
name = "hello_world_host",
srcs = ["src/main.rs"],
deps = [
# External crates
"//thirdparty/crates:mimalloc",
"//thirdparty/crates:lz4-sys",
"//thirdparty/crates:diesel",
"//thirdparty/crates:tokio",
],
)
# https://docs.aspect.build/rulesets/aspect_bazel_lib/docs/transitions/#platform_transition_binary
platform_transition_binary(
name = "hello_world_x86_64",
binary = ":hello_world_host",
target_platform ="//build/platforms:linux-x86_64",
)
platform_transition_binary(
name = "hello_world_aarch64",
binary = ":hello_world_host",
target_platform = "//build/platforms:linux-aarch64",
)
###############################################################################
# Documentation
###############################################################################
# Only generate the docs once for the host platform.
rust_doc(
name = "doc",
crate = ":hello_world_host",
tags = ["doc"],
visibility = ["//visibility:public"],
)
rust_doc_test(
name = "doc_test",
crate = ":hello_world_host",
tags = ["doc-test"],
visibility = ["//visibility:public"],
)
###############################################################################
# Tests
###############################################################################
# Test if the host binary works.
# Note, we cannot test for platform since Bazel determines the host platform automatically
sh_test(
name = "test_hello_world_host",
srcs = ["test_hello_world.sh"],
args = [
"$(rlocationpath :hello_world_host)",
],
data = [
":hello_world_host",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)
# Test the for x86_64 architecture
sh_test(
name = "test_linux_x86_64",
srcs = ["test_platform.sh"],
args = [
"$(rootpath :hello_world_x86_64)",
"x86_64",
],
data = [
":hello_world_x86_64",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)
# Test for ARM architecture
sh_test(
name = "test_linux_arm64",
srcs = ["test_platform.sh"],
args = [
"$(rootpath :hello_world_aarch64)",
"aarch64",
],
data = [
":hello_world_aarch64",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)
###############################################################################
# OCI Container Image
###############################################################################
# Note, build_multi_arch_image uses platform transition internally so you only
# need to declare a binary once for the host platform and this rule does all the heavy lifting for you
# to create a new binary for each platform and stuff them all into a proper multi-arch OCI image.
# If you only deliver an OCI image, you can remove the platform_transition_binaries defined above.
build_multi_arch_image(
name = "image_index",
srcs = ["hello_world_host"],
base = "//images/base_image",
entry_point = "hello_world_host",
exposed_ports = [
"8080",
],
platforms = [
"//build/platforms:linux-x86_64",
"//build/platforms:linux-aarch64",
],
visibility = ["//visibility:public"],
)
# Tag example: 458b6779-1729045897
# This tag format is unique, immutable, and sortable
# because some CI systems (i.e. FluxCD) require sortable image tags.
tag_with_commit_and_timestamp(
name = "remote_tag",
target = ":image_index",
)
oci_push(
name = "push",
image = ":image_index",
remote_tags = ":remote_tag",
repository = "myregistry/myrepo/hello_world_multiarch",
visibility = ["//visibility:public"],
)