Added a new `platform_info` target to the foreign_cc framework (#629)

* Added a new `platform_info` target to the foreign_cc framework

* Update foreign_cc/private/framework/platform.bzl

Co-authored-by: James Sharpe <james.sharpe@zenotech.com>

Co-authored-by: James Sharpe <james.sharpe@zenotech.com>
This commit is contained in:
UebelAndre 2021-05-05 13:35:54 -07:00 committed by GitHub
parent 923cd88ed4
commit 4eb5c5c0c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 91 additions and 23 deletions

View File

@ -6,7 +6,7 @@ load(
"FOREIGN_CC_BUILT_TOOLS_HOST_FRAGMENTS",
"built_tool_rule_impl",
)
load("//foreign_cc/private/framework:helpers.bzl", "os_name")
load("//foreign_cc/private/framework:platform.bzl", "os_name")
def _make_tool_impl(ctx):
script = [

View File

@ -6,7 +6,7 @@ load(
"FOREIGN_CC_BUILT_TOOLS_HOST_FRAGMENTS",
"built_tool_rule_impl",
)
load("//foreign_cc/private/framework:helpers.bzl", "os_name")
load("//foreign_cc/private/framework:platform.bzl", "os_name")
def _ninja_tool_impl(ctx):
script = [

View File

@ -11,7 +11,14 @@ FOREIGN_CC_BUILT_TOOLS_ATTRS = {
doc = "The target containing the build tool's sources",
mandatory = True,
),
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_foreign_cc_framework_platform": attr.label(
doc = "Information about the execution platform",
cfg = "exec",
default = Label("@rules_foreign_cc//foreign_cc/private/framework:platform_info"),
),
}
# Common host fragments for all built_tool rules

View File

@ -16,7 +16,10 @@ load(
"cc_external_rule_impl",
"create_attrs",
)
load("//foreign_cc/private/framework:helpers.bzl", "os_name")
load(
"//foreign_cc/private/framework:platform.bzl",
"os_name",
)
load(
"//toolchains/native_tools:tool_access.bzl",
"get_cmake_data",

View File

@ -14,7 +14,7 @@ load(
"cc_external_rule_impl",
"create_attrs",
)
load("//foreign_cc/private/framework:helpers.bzl", "os_name")
load("//foreign_cc/private/framework:platform.bzl", "os_name")
load("//toolchains/native_tools:tool_access.bzl", "get_make_data")
def _configure_make(ctx):

View File

@ -10,10 +10,10 @@ load(
"//foreign_cc/private/framework:helpers.bzl",
"convert_shell_script",
"create_function",
"os_name",
"script_extension",
"shebang",
)
load("//foreign_cc/private/framework:platform.bzl", "os_name")
load(
"//toolchains/native_tools:tool_access.bzl",
"get_make_data",
@ -192,6 +192,11 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_foreign_cc_framework_platform": attr.label(
doc = "Information about the execution platform",
cfg = "exec",
default = Label("@rules_foreign_cc//foreign_cc/private/framework:platform_info"),
),
}
# A list of common fragments required by rules using this framework

View File

@ -1,4 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":platform.bzl", "framework_platform_info")
toolchain_type(
name = "shell_toolchain",
@ -11,3 +12,5 @@ bzl_library(
visibility = ["//:__subpackages__"],
deps = ["//foreign_cc/private/framework/toolchains:bzl_srcs"],
)
framework_platform_info()

View File

@ -34,9 +34,6 @@ of the function bodies to populate the "prelude" part of the script.
load("//foreign_cc/private/framework/toolchains:access.bzl", "call_shell", "create_context")
load("//foreign_cc/private/framework/toolchains:commands.bzl", "PLATFORM_COMMANDS")
def os_name(ctx):
return call_shell(create_context(ctx), "os_name")
def script_extension(ctx):
"""A helper method for getting the script extension of the current foreign_cc framework toolchain

View File

@ -0,0 +1,67 @@
"""A helper module containing tools for detecting platform information"""
ForeignCcPlatformInfo = provider(
doc = "A provider containing information about the current platform",
fields = {
"os": "The platform os",
},
)
def _framework_platform_info_impl(ctx):
"""The implementation of `framework_platform_info`
Args:
ctx (ctx): The rule's context object
Returns:
list: A provider containing platform info
"""
return [ForeignCcPlatformInfo(
os = ctx.attr.os,
)]
_framework_platform_info = rule(
doc = "A rule defining platform information used by the foreign_cc framework",
implementation = _framework_platform_info_impl,
attrs = {
"os": attr.string(
doc = "The platform's operating system",
),
},
)
def framework_platform_info(name = "platform_info"):
"""Define a target containing platform information used in the foreign_cc framework"""
_framework_platform_info(
name = name,
os = select({
"@platforms//os:android": "android",
"@platforms//os:freebsd": "freebsd",
"@platforms//os:ios": "ios",
"@platforms//os:linux": "linux",
"@platforms//os:macos": "macos",
"@platforms//os:none": "none",
"@platforms//os:openbsd": "openbsd",
"@platforms//os:qnx": "qnx",
"@platforms//os:tvos": "tvos",
"@platforms//os:watchos": "watchos",
"@platforms//os:windows": "windows",
"//conditions:default": "unknown",
}),
visibility = ["//visibility:public"],
)
def os_name(ctx):
"""A helper function for getting the operating system name from a `ForeignCcPlatformInfo` provider
Args:
ctx (ctx): The current rule's context object
Returns:
str: The string of the current platform
"""
platform_info = getattr(ctx.attr, "_foreign_cc_framework_platform")
if not platform_info:
return "unknown"
return platform_info[ForeignCcPlatformInfo].os

View File

@ -145,10 +145,6 @@ PLATFORM_COMMANDS = {
arguments = [_argument_info(name = "path", data_type = type(""), doc = "Path to directory")],
doc = "Creates a directory and, if neccesary, it's parents",
),
"os_name": _command_info(
arguments = [],
doc = "Returns OS name",
),
"path": _command_info(
arguments = [
_argument_info(name = "expression", data_type = type(""), doc = "Path"),

View File

@ -4,9 +4,6 @@ load(":commands.bzl", "FunctionAndCallInfo")
_REPLACE_VALUE = "\\${EXT_BUILD_DEPS}"
def os_name():
return "linux"
def shebang():
return "#!/usr/bin/env bash"

View File

@ -4,9 +4,6 @@ load(":commands.bzl", "FunctionAndCallInfo")
_REPLACE_VALUE = "\\${EXT_BUILD_DEPS}"
def os_name():
return "macos"
def shebang():
return "#!/usr/bin/env bash"

View File

@ -4,9 +4,6 @@ load(":commands.bzl", "FunctionAndCallInfo")
_REPLACE_VALUE = "\\${EXT_BUILD_DEPS}"
def os_name():
return "windows"
def shebang():
return "#!/usr/bin/env bash"

View File

@ -108,7 +108,6 @@ def _do_function_call_test(ctx):
cases = {
"##echo## \"\ntext\n\"": "echo1 \"\ntext\n\"",
"##os_name##": "Fuchsia",
"##script_prelude##": "set -euo pipefail",
"##symlink_contents_to_dir## 1 2": "1_2",
"export ROOT=\"A B C\"": "export1 ROOT=\"A B C\"",