Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit 7886e40

Browse files
merge: #3498
3498: feat(ci): enable release and debug builds r=adamhjk a=adamhjk This PR enables release and debug builds for buck2. By default, we will always do debug builds (or, more accurately, whatever the rustc compiler thinks the defaults should be). If you want to build for release or debug explicitly, you can do that with: ``` $ buck2 build `@//mode/release` //bin/sdf:sdf ``` And the result will be compiled with release optimizations. You can also use ``@//mode/debug`` if you want. Those files are just ways of DRY-ing up the configuration options - you could also pass them directly on the command line. We can add as many configuration options as we want, and as many toolchain configurations as we want, by extending the options in the `//config` tree, adding a new toolchain statement, and extending the select statement for the `:rust` alias. <img src="https://media1.giphy.com/media/MFg6kb46z2tNOEIBox/giphy.gif"/> Co-authored-by: Adam Jacob <adam@systeminit.com>
2 parents cbdf26f + 22e0fc5 commit 7886e40

9 files changed

Lines changed: 208 additions & 3 deletions

File tree

.buckconfig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ fbsource = none
1414

1515
[parser]
1616
target_platform_detector_spec = \
17-
target:root//...->prelude//platforms:default \
18-
target:prelude-si//...->prelude//platforms:default
17+
target:root//...->prelude-si//platforms:default \
18+
target:prelude-si//...->prelude-si//platforms:default \
19+
target:toolchains//...->prelude-si//platforms:default \
1920

2021
[project]
2122
ignore = \

config/BUCK

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
constraint_setting(name = "rust_build_mode", visibility = ["PUBLIC"])
2+
constraint_value(name = "build_debug", constraint_setting = ":rust_build_mode", visibility = ["PUBLIC"])
3+
constraint_value(name = "build_release", constraint_setting = ":rust_build_mode", visibility = ["PUBLIC"])

mode/debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--config=rustc.mode=build_debug

mode/release

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--config=rustc.mode=build_release

prelude-si/build_context/build_context_srcs_from_deps.bxl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def _build_context_srcs_from_deps_impl(ctx):
2727
# Add candidate path entries for the Buck2 prelude directories (we want *all* these files)
2828
raw_paths.append("prelude")
2929
raw_paths.append("prelude-si")
30+
raw_paths.append("config")
31+
raw_paths.append("mode")
32+
raw_paths.append("toolchains")
3033

3134
# While not officially in a prelude, there are macros that Reindeer used to create the Rust
3235
# third-party targets, so we will include this directory as well.

prelude-si/platforms/BUCK

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# The custom platform for SI. This is taken from the prelude, but includes our customizations for
2+
# rust build flags.
3+
#
4+
# Essentially, we should be adding any custom configuration into root//config, and then plumbing it
5+
# through the execution_platform defined below.
6+
#
7+
# To actually add new configuration, you'll extend the relevant python function to output the
8+
# correct configuration info, and then you can use it in a select() statement at will.
9+
10+
load(":defs.bzl", "execution_platform", "host_configuration")
11+
12+
prelude = native
13+
14+
_rust_build_mode = read_root_config("rustc", "mode", "build_debug")
15+
_rust_build_mode_constraint = "root//config:" + _rust_build_mode
16+
17+
execution_platform(
18+
name = "default",
19+
cpu_configuration = host_configuration.cpu,
20+
os_configuration = host_configuration.os,
21+
rust_build_mode = _rust_build_mode_constraint,
22+
use_windows_path_separators = host_info().os.is_windows,
23+
)
24+
25+
prelude.constraint_setting(
26+
name = "runs_remote",
27+
)
28+
29+
prelude.constraint_value(
30+
name = "may_run_remote",
31+
constraint_setting = ":runs_remote",
32+
visibility = ["PUBLIC"],
33+
)
34+
35+
prelude.constraint_setting(
36+
name = "runs_local",
37+
visibility = ["PUBLIC"],
38+
)
39+
40+
prelude.constraint_value(
41+
name = "may_run_local",
42+
constraint_setting = ":runs_local",
43+
visibility = ["PUBLIC"],
44+
)
45+
46+
prelude.constraint_setting(
47+
name = "runs_only",
48+
)
49+
50+
prelude.constraint_value(
51+
name = "runs_only_local",
52+
constraint_setting = ":runs_only",
53+
visibility = ["PUBLIC"],
54+
)
55+
56+
prelude.constraint_value(
57+
name = "runs_only_remote",
58+
constraint_setting = ":runs_only",
59+
visibility = ["PUBLIC"],
60+
)
61+
62+
prelude.constraint_setting(
63+
name = "fat_platform_marker",
64+
)
65+
66+
prelude.constraint_value(
67+
name = "fat_platform_enabled",
68+
constraint_setting = ":fat_platform_marker",
69+
visibility = ["PUBLIC"],
70+
)
71+
72+
# This is mostly here for a rule type to add a dependency on it to mark all
73+
# instances of that rule type as incompatible with a fat platform. Ideally,
74+
# toolchains could affect the target compatibility of their users directly but
75+
# toolchains are currently all exec deps and so cannot do that. We'd like
76+
# buck2 to support a form of dep that inherited its users execution platform
77+
# so that toolchains could basically get visibility and affect both target and
78+
# execution configuration, but that's not implemented yet.
79+
export_file(
80+
name = "fat_platform_incompatible",
81+
# @oss-disable: src = "TARGETS.v2",
82+
src = "BUCK", # @oss-enable
83+
target_compatible_with = select({
84+
":fat_platform_enabled": ["config//:none"],
85+
"DEFAULT": [],
86+
}),
87+
visibility = ["PUBLIC"],
88+
)

prelude-si/platforms/defs.bzl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under both the MIT license found in the
4+
# LICENSE-MIT file in the root directory of this source tree and the Apache
5+
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
6+
# of this source tree.
7+
8+
def _execution_platform_impl(ctx: AnalysisContext) -> list[Provider]:
9+
constraints = dict()
10+
constraints.update(ctx.attrs.cpu_configuration[ConfigurationInfo].constraints)
11+
constraints.update(ctx.attrs.os_configuration[ConfigurationInfo].constraints)
12+
constraints.update(ctx.attrs.rust_build_mode[ConfigurationInfo].constraints)
13+
cfg = ConfigurationInfo(constraints = constraints, values = {})
14+
15+
name = ctx.label.raw_target()
16+
platform = ExecutionPlatformInfo(
17+
label = name,
18+
configuration = cfg,
19+
executor_config = CommandExecutorConfig(
20+
local_enabled = True,
21+
remote_enabled = False,
22+
use_windows_path_separators = ctx.attrs.use_windows_path_separators,
23+
),
24+
)
25+
26+
return [
27+
DefaultInfo(),
28+
platform,
29+
PlatformInfo(label = str(name), configuration = cfg),
30+
ExecutionPlatformRegistrationInfo(platforms = [platform]),
31+
]
32+
33+
execution_platform = rule(
34+
impl = _execution_platform_impl,
35+
attrs = {
36+
"cpu_configuration": attrs.dep(providers = [ConfigurationInfo]),
37+
"os_configuration": attrs.dep(providers = [ConfigurationInfo]),
38+
"rust_build_mode": attrs.dep(providers = [ConfigurationInfo]),
39+
"use_windows_path_separators": attrs.bool(),
40+
},
41+
)
42+
43+
def _host_cpu_configuration() -> str:
44+
arch = host_info().arch
45+
if arch.is_aarch64:
46+
return "prelude//cpu:arm64"
47+
elif arch.is_arm:
48+
return "prelude//cpu:arm32"
49+
elif arch.is_i386:
50+
return "prelude//cpu:x86_32"
51+
else:
52+
return "prelude//cpu:x86_64"
53+
54+
def _host_os_configuration() -> str:
55+
os = host_info().os
56+
if os.is_macos:
57+
return "prelude//os:macos"
58+
elif os.is_windows:
59+
return "prelude//os:windows"
60+
else:
61+
return "prelude//os:linux"
62+
63+
host_configuration = struct(
64+
cpu = _host_cpu_configuration(),
65+
os = _host_os_configuration(),
66+
)

toolchains/BUCK

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load(":toolchain.bzl", "toolchain_alias")
12
load("@prelude//toolchains:cxx.bzl", "system_cxx_toolchain")
23
load("@prelude//toolchains:genrule.bzl", "system_genrule_toolchain")
34
load(
@@ -45,9 +46,33 @@ system_python_bootstrap_toolchain(
4546
)
4647

4748
system_rust_toolchain(
48-
name = "rust",
49+
name = "rust_release",
50+
default_edition = "2021",
51+
clippy_toml = "root//:clippy.toml",
52+
visibility = ["PUBLIC"],
53+
rustc_flags = [
54+
"-Copt-level=3",
55+
"-Cdebuginfo=none",
56+
"-Cdebug-assertions=false",
57+
"-Coverflow-checks=false",
58+
"-Clto=false",
59+
"-Ccodegen-units=16"
60+
]
61+
)
62+
63+
system_rust_toolchain(
64+
name = "rust_debug",
4965
default_edition = "2021",
5066
clippy_toml = "root//:clippy.toml",
67+
visibility = ["PUBLIC"]
68+
)
69+
70+
toolchain_alias(
71+
name = "rust",
72+
actual = select({
73+
"root//config:build_debug": ":rust_debug",
74+
"root//config:build_release": ":rust_release",
75+
}),
5176
visibility = ["PUBLIC"],
5277
)
5378

toolchains/toolchain.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def _toolchain_alias_impl(ctx: AnalysisContext) -> list[Provider]:
2+
return ctx.attrs.actual.providers
3+
4+
toolchain_alias = rule(
5+
doc="""
6+
toolchain_alias acts like alias but for toolchain rules.
7+
The toolchain_alias itself is a toolchain rule and the `actual` argument is
8+
expected to be a toolchain_rule as well.
9+
""",
10+
attrs={
11+
"actual": attrs.toolchain_dep(
12+
doc="The actual toolchain that is being aliased. This should be a toolchain rule."
13+
)
14+
},
15+
impl=_toolchain_alias_impl,
16+
is_toolchain_rule=True,
17+
)

0 commit comments

Comments
 (0)