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

Expand make variables in env #788

Merged
merged 2 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 9 additions & 3 deletions examples/third_party/openssl/BUILD.openssl.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -91,6 +96,7 @@ configure_make(
"libcrypto.a",
],
targets = MAKE_TARGETS,
toolchains = ["@rules_perl//:current_toolchain"],
)

filegroup(
Expand Down
11 changes: 11 additions & 0 deletions examples/third_party/openssl/openssl_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions examples/third_party/openssl/openssl_setup.bzl
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions examples/third_party/setup.bzl
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion foreign_cc/built_tools/private/built_tools_framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion foreign_cc/cmake.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion foreign_cc/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 ""
Expand Down
14 changes: 12 additions & 2 deletions foreign_cc/private/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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."
),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down