# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
def _execution_platform_impl(ctx: AnalysisContext) -> list[Provider]:
constraints = dict()
constraints.update(ctx.attrs.cpu_configuration[ConfigurationInfo].constraints)
constraints.update(ctx.attrs.os_configuration[ConfigurationInfo].constraints)
cfg = ConfigurationInfo(constraints = constraints, values = {})
name = ctx.label.raw_target()
platform = ExecutionPlatformInfo(
label = name,
configuration = cfg,
executor_config = CommandExecutorConfig(
local_enabled = True,
remote_enabled = False,
use_windows_path_separators = ctx.attrs.use_windows_path_separators,
),
)
return [
DefaultInfo(),
platform,
PlatformInfo(label = str(name), configuration = cfg),
ExecutionPlatformRegistrationInfo(platforms = [platform]),
]
execution_platform = rule(
impl = _execution_platform_impl,
attrs = {
"cpu_configuration": attrs.dep(providers = [ConfigurationInfo]),
"os_configuration": attrs.dep(providers = [ConfigurationInfo]),
"use_windows_path_separators": attrs.bool(),
},
)
def _host_cpu_configuration() -> str:
arch = host_info().arch
if arch.is_aarch64:
return "prelude//cpu:arm64"
elif arch.is_arm:
return "prelude//cpu:arm32"
elif arch.is_i386:
return "prelude//cpu:x86_32"
else:
return "prelude//cpu:x86_64"
def _host_os_configuration() -> str:
os = host_info().os
if os.is_macos:
return "prelude//os:macos"
elif os.is_windows:
return "prelude//os:windows"
else:
return "prelude//os:linux"
host_configuration = struct(
cpu = _host_cpu_configuration(),
os = _host_os_configuration(),
)