Expand make variables in env (#788)

This commit is contained in:
Daniel Wagner-Hall 2021-10-19 15:51:53 +01:00 committed by GitHub
parent 091dc5d381
commit ec9c9a4612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 54 additions and 8 deletions

View File

@ -35,6 +35,10 @@ load("@rules_foreign_cc_examples_third_party//:repositories.bzl", examples_third
examples_third_party_repositories()
load("@rules_foreign_cc_examples_third_party//:setup.bzl", examples_third_party_setup = "setup")
examples_third_party_setup()
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(

View File

@ -60,7 +60,7 @@ configure_make_variant(
env = {
# The Zi flag must be set otherwise OpenSSL fails to build due to missing .pdb files
"CFLAGS": "-Zi",
"PATH": "$(dirname $(execpath @nasm//:nasm)):$PATH",
"PATH": "$$(dirname $(execpath @nasm//:nasm)):$$PATH",
"PERL": "$(execpath @perl//:perl)",
},
lib_name = LIB_NAME,
@ -79,8 +79,13 @@ configure_make(
configure_in_place = True,
configure_options = CONFIGURE_OPTIONS,
env = select({
"@platforms//os:macos": {"AR": ""},
"//conditions:default": {},
"@platforms//os:macos": {
"AR": "",
"PERL": "$$EXT_BUILD_ROOT$$/$(PERL)",
},
"//conditions:default": {
"PERL": "$$EXT_BUILD_ROOT$$/$(PERL)",
},
}),
lib_name = LIB_NAME,
lib_source = ":all_srcs",
@ -91,6 +96,7 @@ configure_make(
"libcrypto.a",
],
targets = MAKE_TARGETS,
toolchains = ["@rules_perl//:current_toolchain"],
)
filegroup(

View File

@ -28,7 +28,18 @@ def openssl_repositories():
"https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/nasm-2.15.05-win64.zip",
],
)
maybe(
http_archive,
name = "rules_perl",
sha256 = "55fbe071971772758ad669615fc9aac9b126db6ae45909f0f36de499f6201dd3",
strip_prefix = "rules_perl-2f4f36f454375e678e81e5ca465d4d497c5c02da",
urls = [
"https://github.com/bazelbuild/rules_perl/archive/2f4f36f454375e678e81e5ca465d4d497c5c02da.tar.gz",
],
)
# rules_perl doesn't currently support Windows, so we need to bring along our own Perl.
# https://github.com/bazelbuild/rules_perl/issues/30
maybe(
http_archive,
name = "perl",

View File

@ -0,0 +1,7 @@
"""A module initialising the third party dependencies OpenSSL"""
load("@rules_perl//perl:deps.bzl", "perl_register_toolchains", "perl_rules_dependencies")
def openssl_setup():
perl_rules_dependencies()
perl_register_toolchains()

6
examples/third_party/setup.bzl vendored Normal file
View File

@ -0,0 +1,6 @@
"""A centralized module initializing repositories required for third party examples of rules_foreign_cc which require loading from repositories which themselves were loaded in repositories.bzl."""
load("//openssl:openssl_setup.bzl", "openssl_setup")
def setup():
openssl_setup()

View File

@ -8,7 +8,7 @@ load("//foreign_cc/private/framework:helpers.bzl", "convert_shell_script", "sheb
# Common attributes for all built_tool rules
FOREIGN_CC_BUILT_TOOLS_ATTRS = {
"env": attr.string_dict(
doc = "Environment variables to set during the build.",
doc = "Environment variables to set during the build. This attribute is subject to make variable substitution.",
default = {},
),
"srcs": attr.label(

View File

@ -143,6 +143,7 @@ load(
"cc_external_rule_impl",
"create_attrs",
"expand_locations",
"expand_locations_and_make_variables",
)
load("//foreign_cc/private:transitions.bzl", "make_variant")
load(
@ -262,7 +263,7 @@ def _create_configure_script(configureParameters):
root = root,
no_toolchain_file = no_toolchain_file,
user_cache = dict(ctx.attr.cache_entries),
user_env = expand_locations(ctx, ctx.attr.env, data),
user_env = expand_locations_and_make_variables(ctx, "env", data),
options = attrs.generate_args,
cmake_commands = cmake_commands,
cmake_prefix = prefix,

View File

@ -17,6 +17,7 @@ load(
"cc_external_rule_impl",
"create_attrs",
"expand_locations",
"expand_locations_and_make_variables",
)
load("//foreign_cc/private:transitions.bzl", "make_variant")
load("//foreign_cc/private/framework:platform.bzl", "os_name")
@ -74,7 +75,7 @@ def _create_configure_script(configureParameters):
for arg in ctx.attr.args
])
user_env = expand_locations(ctx, ctx.attr.env, data)
user_env = expand_locations_and_make_variables(ctx, "env", data)
make_commands = []
prefix = "{} ".format(expand_locations(ctx, attrs.tool_prefix, data)) if attrs.tool_prefix else ""

View File

@ -103,6 +103,7 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
"`$(execpath)` macros may be used to point at files which are listed as `data`, `deps`, or `build_data`, " +
"but unlike with other rules, these will be replaced with absolute paths to those files, " +
"because the build does not run in the exec root. " +
"This attribute is subject to make variable substitution. " +
"No other macros are supported." +
"Variables containing `PATH` (e.g. `PATH`, `LD_LIBRARY_PATH`, `CPATH`) entries will be prepended to the existing variable."
),
@ -306,7 +307,7 @@ def get_env_prelude(ctx, lib_name, data_dependencies, target_root):
env.update({"PATH": _normalize_path(linker_path) + ":" + env.get("PATH")})
# Add all user defined variables
user_vars = expand_locations(ctx, ctx.attr.env, data_dependencies)
user_vars = expand_locations_and_make_variables(ctx, "env", data_dependencies)
env.update(user_vars)
# If user has defined a PATH variable (e.g. PATH, LD_LIBRARY_PATH, CPATH) prepend it to the existing variable
@ -388,7 +389,7 @@ def cc_external_rule_impl(ctx, attrs):
installdir_copy = copy_directory(ctx.actions, "$$INSTALLDIR$$", "copy_{}/{}".format(lib_name, lib_name))
target_root = paths.dirname(installdir_copy.file.dirname)
data_dependencies = ctx.attr.data + ctx.attr.build_data
data_dependencies = ctx.attr.data + ctx.attr.build_data + ctx.attr.toolchains
# Also add legacy dependencies while they're still available
data_dependencies += ctx.attr.tools_deps + ctx.attr.additional_tools
@ -915,6 +916,15 @@ def _expand_command_path(binary, path, command):
else:
return command
def expand_locations_and_make_variables(ctx, attr_name, data):
unexpanded = getattr(ctx.attr, attr_name)
location_expanded = expand_locations(ctx, unexpanded, data)
# Make variable expansion will treat $$ as escaped values for $ and strip the second one.
# Double-escape $s which we insert in expand_locations.
make_variable_expanded = {k: ctx.expand_make_variables(attr_name, v.replace("$$EXT_BUILD_ROOT$$", "$$$$EXT_BUILD_ROOT$$$$"), {}) for k, v in location_expanded.items()}
return make_variable_expanded
def expand_locations(ctx, expandable, data):
"""Expand locations on a dictionary while ensuring `execpath` is always set to an absolute path