Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple execution platforms with system_cxx_toolchain #629

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/visual_studio/.buckconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ fbsource = none
buck = none

[parser]
target_platform_detector_spec = target:root//...->toolchains//:default
target_platform_detector_spec = target:root//...->root//buck2_utils/platforms:windows_debug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this?


[build]
execution_platforms = prelude//platforms:default
execution_platforms = root//buck2_utils/platforms:default

[buck2_re_client]
action_cache_address = grpc://localhost:8980
engine_address = grpc://localhost:8980
cas_address = grpc://localhost:8980
tls = false
instance_name = fuse
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def gen_compilation_database_impl(ctx):

if not "write" in actions_by_category:
continue
if not "cxx_compile" in actions_by_category:
if not "cxx_compile" in actions_by_category and not "c_compile" in actions_by_category:
continue

argsfiles = {}
Expand All @@ -111,7 +111,7 @@ def gen_compilation_database_impl(ctx):
argsfiles[identifier] = arguments

# Now walk each compilation action and generate a compilation database entry for it.
for action in actions_by_category["cxx_compile"]:
for action in actions_by_category.get("cxx_compile", []) + actions_by_category.get("c_compile", []):
entry = create_compilation_database_entry(ctx.cli_args.directory, buildfile_folder, argsfiles, action)
is_pic = entry["file"].endswith(" (pic)")
file_name = entry["file"].removesuffix(" (pic)")
Expand Down
3 changes: 2 additions & 1 deletion examples/visual_studio/buck2_utils/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ begin
[parameter(Mandatory=$true)] [String] $SourcePath,
[parameter(Mandatory=$true)] [String] $TargetPath
)
Copy-Item $SourcePath -Destination $TargetPath
New-Item -ItemType File -Path $TargetPath -Force | Out-Null
Copy-Item $SourcePath -Destination $TargetPath | Out-Null
Comment on lines +32 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 2 steps instead of 1?

}
}
process
Expand Down
38 changes: 38 additions & 0 deletions examples/visual_studio/buck2_utils/platforms/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
load(":defs.bzl", "execution_platforms")

execution_platforms(
name = "default",
visibility = ["PUBLIC"],
)

platform(
name = "windows_debug",
constraint_values = [
"config//os/constraints:windows",
"root//buck2_utils/configuration:debug",
],
)

platform(
name = "windows_release",
constraint_values = [
"config//os/constraints:windows",
"root//buck2_utils/configuration:release",
],
)

platform(
name = "linux_debug",
constraint_values = [
"config//os/constraints:linux",
"root//buck2_utils/configuration:debug",
],
)

platform(
name = "linux_release",
constraint_values = [
"config//os/constraints:linux",
"root//buck2_utils/configuration:release",
],
)
61 changes: 61 additions & 0 deletions examples/visual_studio/buck2_utils/platforms/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
def is_remote_enabled() -> bool:
re_enabled = read_config("buck2_re_client", "enabled", "false")
return re_enabled == "true"

def _execution_platforms_impl(ctx: AnalysisContext) -> list[Provider]:
is_re_enabled = is_remote_enabled()

name = ctx.label.raw_target()

constraints_windows = dict()
constraints_windows.update(ctx.attrs.os_configuration_windows[ConfigurationInfo].constraints)
constraints_linux = dict()
constraints_linux.update(ctx.attrs.os_configuration_linux[ConfigurationInfo].constraints)

platforms_details = [
("windows", host_info().os.is_windows, constraints_windows),
("linux", host_info().os.is_linux, constraints_linux),
]

platforms = []
for platform_name, is_local_enabled, constraints in platforms_details:
if is_re_enabled or is_local_enabled:
platforms.append(
ExecutionPlatformInfo(
label = name,
configuration = ConfigurationInfo(
constraints = constraints,
values = {},
),
executor_config = CommandExecutorConfig(
local_enabled = is_local_enabled,
remote_enabled = is_re_enabled,
use_limited_hybrid = True,
remote_execution_properties = {
"OSFamily": platform_name,
"container-image": "docker://" + platform_name + "_build",
},
remote_execution_use_case = "buck2-default",
use_windows_path_separators = platform_name == "windows",
),
)
)

return [
DefaultInfo(),
ExecutionPlatformRegistrationInfo(platforms = platforms),
]

execution_platforms = rule(
impl = _execution_platforms_impl,
attrs = {
"os_configuration_windows": attrs.dep(
providers = [ConfigurationInfo],
default = "config//os:windows",
),
"os_configuration_linux": attrs.dep(
providers = [ConfigurationInfo],
default = "config//os:linux",
),
},
)
2 changes: 2 additions & 0 deletions examples/visual_studio/library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#else
#define DLL_API __declspec(dllimport)
#endif
#else
#define DLL_API
#endif

#ifdef __cplusplus
Expand Down
29 changes: 21 additions & 8 deletions examples/visual_studio/toolchains/BUCK
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
load("@prelude//toolchains:cxx.bzl", "system_cxx_toolchain")
load("@prelude//toolchains/cxx/clang:tools.bzl", "path_clang_tools")
load("@prelude//toolchains/msvc:tools.bzl", "find_msvc_tools")
load("@prelude//toolchains:python.bzl", "system_python_bootstrap_toolchain")
load("@root//buck2_utils/platforms:defs.bzl", "is_remote_enabled")

find_msvc_tools(
name = "msvc_tools",
use_path_compilers = is_remote_enabled(),
use_path_linkers = is_remote_enabled() and not host_info().os.is_windows,
target_compatible_with = ["config//os:windows"],
visibility = ["PUBLIC"],
)

path_clang_tools(
name = "clang_tools",
target_compatible_with = ["config//os:linux"],
visibility = ["PUBLIC"],
)

system_cxx_toolchain(
name = "cxx",
toolchain_info = select({
"config//os:windows": ":msvc_tools",
"config//os:linux": ":clang_tools",
}),
#The flags in the below attributes' Windows config are copied from Visual Studio's project "Console App", with some changes listed in each attribute
c_flags = select({
"config//os:linux": [],
Expand Down Expand Up @@ -111,11 +132,3 @@ system_python_bootstrap_toolchain(
name = "python_bootstrap",
visibility = ["PUBLIC"],
)

configuration = read_config("cxx", "configuration", "debug")

platform(
name = "default",
constraint_values = ["root//buck2_utils/configuration:release"] if configuration == "release" else ["root//buck2_utils/configuration:debug"],
deps = ["prelude//platforms:default"],
)