Allow setting env vars (#489)

* Allow setting env vars

Also, bring all runfiles of data deps along for execution - currently
data deps which consist of more than one file aren't copied into the
action, which means that they will fail at runtime when they try to use
their runfiles.

* Update docs
This commit is contained in:
Daniel Wagner-Hall 2021-02-04 11:59:01 +00:00 committed by GitHub
parent 7c4490a2ee
commit ece188f9ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 11 deletions

View File

@ -22,8 +22,8 @@
<pre>
boost_build(<a href="#boost_build-name">name</a>, <a href="#boost_build-additional_inputs">additional_inputs</a>, <a href="#boost_build-additional_tools">additional_tools</a>, <a href="#boost_build-alwayslink">alwayslink</a>, <a href="#boost_build-binaries">binaries</a>, <a href="#boost_build-bootstrap_options">bootstrap_options</a>,
<a href="#boost_build-data">data</a>, <a href="#boost_build-defines">defines</a>, <a href="#boost_build-deps">deps</a>, <a href="#boost_build-headers_only">headers_only</a>, <a href="#boost_build-interface_libraries">interface_libraries</a>, <a href="#boost_build-lib_name">lib_name</a>, <a href="#boost_build-lib_source">lib_source</a>, <a href="#boost_build-linkopts">linkopts</a>,
<a href="#boost_build-make_commands">make_commands</a>, <a href="#boost_build-out_bin_dir">out_bin_dir</a>, <a href="#boost_build-out_include_dir">out_include_dir</a>, <a href="#boost_build-out_lib_dir">out_lib_dir</a>, <a href="#boost_build-postfix_script">postfix_script</a>,
<a href="#boost_build-data">data</a>, <a href="#boost_build-defines">defines</a>, <a href="#boost_build-deps">deps</a>, <a href="#boost_build-env">env</a>, <a href="#boost_build-headers_only">headers_only</a>, <a href="#boost_build-interface_libraries">interface_libraries</a>, <a href="#boost_build-lib_name">lib_name</a>, <a href="#boost_build-lib_source">lib_source</a>,
<a href="#boost_build-linkopts">linkopts</a>, <a href="#boost_build-make_commands">make_commands</a>, <a href="#boost_build-out_bin_dir">out_bin_dir</a>, <a href="#boost_build-out_include_dir">out_include_dir</a>, <a href="#boost_build-out_lib_dir">out_lib_dir</a>, <a href="#boost_build-postfix_script">postfix_script</a>,
<a href="#boost_build-shared_libraries">shared_libraries</a>, <a href="#boost_build-static_libraries">static_libraries</a>, <a href="#boost_build-tools_deps">tools_deps</a>, <a href="#boost_build-user_options">user_options</a>)
</pre>
@ -43,6 +43,7 @@ Rule for building Boost. Invokes bootstrap.sh and then b2 install.
| <a id="boost_build-data"></a>data | Files needed by this rule at runtime. May list file or rule targets. Generally allows any target. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="boost_build-defines"></a>defines | Optional compilation definitions to be passed to the dependencies of this library. They are NOT passed to the compiler, you should duplicate them in the configuration options. | List of strings | optional | [] |
| <a id="boost_build-deps"></a>deps | Optional dependencies to be copied into the directory structure. Typically those directly required for the external building of the library/binaries. (i.e. those that the external buidl system will be looking for and paths to which are provided by the calling rule) | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="boost_build-env"></a>env | Environment variables to set during the build. $(execpath) macros may be used to point at files which are listed as data deps, tools_deps, or additional_tools, 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. No other macros are supported. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="boost_build-headers_only"></a>headers_only | Flag variable to indicate that the library produces only headers | Boolean | optional | False |
| <a id="boost_build-interface_libraries"></a>interface_libraries | Optional names of the resulting interface libraries. | List of strings | optional | [] |
| <a id="boost_build-lib_name"></a>lib_name | Library name. Defines the name of the install directory and the name of the static library, if no output files parameters are defined (any of static_libraries, shared_libraries, interface_libraries, binaries_names) Optional. If not defined, defaults to the target's name. | String | optional | "" |
@ -65,10 +66,10 @@ Rule for building Boost. Invokes bootstrap.sh and then b2 install.
<pre>
cmake_external(<a href="#cmake_external-name">name</a>, <a href="#cmake_external-additional_inputs">additional_inputs</a>, <a href="#cmake_external-additional_tools">additional_tools</a>, <a href="#cmake_external-alwayslink">alwayslink</a>, <a href="#cmake_external-binaries">binaries</a>, <a href="#cmake_external-cache_entries">cache_entries</a>,
<a href="#cmake_external-cmake_options">cmake_options</a>, <a href="#cmake_external-data">data</a>, <a href="#cmake_external-defines">defines</a>, <a href="#cmake_external-deps">deps</a>, <a href="#cmake_external-env_vars">env_vars</a>, <a href="#cmake_external-generate_crosstool_file">generate_crosstool_file</a>, <a href="#cmake_external-headers_only">headers_only</a>,
<a href="#cmake_external-install_prefix">install_prefix</a>, <a href="#cmake_external-interface_libraries">interface_libraries</a>, <a href="#cmake_external-lib_name">lib_name</a>, <a href="#cmake_external-lib_source">lib_source</a>, <a href="#cmake_external-linkopts">linkopts</a>, <a href="#cmake_external-make_commands">make_commands</a>,
<a href="#cmake_external-out_bin_dir">out_bin_dir</a>, <a href="#cmake_external-out_include_dir">out_include_dir</a>, <a href="#cmake_external-out_lib_dir">out_lib_dir</a>, <a href="#cmake_external-postfix_script">postfix_script</a>, <a href="#cmake_external-shared_libraries">shared_libraries</a>,
<a href="#cmake_external-static_libraries">static_libraries</a>, <a href="#cmake_external-tools_deps">tools_deps</a>, <a href="#cmake_external-working_directory">working_directory</a>)
<a href="#cmake_external-cmake_options">cmake_options</a>, <a href="#cmake_external-data">data</a>, <a href="#cmake_external-defines">defines</a>, <a href="#cmake_external-deps">deps</a>, <a href="#cmake_external-env">env</a>, <a href="#cmake_external-env_vars">env_vars</a>, <a href="#cmake_external-generate_crosstool_file">generate_crosstool_file</a>,
<a href="#cmake_external-headers_only">headers_only</a>, <a href="#cmake_external-install_prefix">install_prefix</a>, <a href="#cmake_external-interface_libraries">interface_libraries</a>, <a href="#cmake_external-lib_name">lib_name</a>, <a href="#cmake_external-lib_source">lib_source</a>, <a href="#cmake_external-linkopts">linkopts</a>,
<a href="#cmake_external-make_commands">make_commands</a>, <a href="#cmake_external-out_bin_dir">out_bin_dir</a>, <a href="#cmake_external-out_include_dir">out_include_dir</a>, <a href="#cmake_external-out_lib_dir">out_lib_dir</a>, <a href="#cmake_external-postfix_script">postfix_script</a>,
<a href="#cmake_external-shared_libraries">shared_libraries</a>, <a href="#cmake_external-static_libraries">static_libraries</a>, <a href="#cmake_external-tools_deps">tools_deps</a>, <a href="#cmake_external-working_directory">working_directory</a>)
</pre>
Rule for building external library with CMake.
@ -88,6 +89,7 @@ Rule for building external library with CMake.
| <a id="cmake_external-data"></a>data | Files needed by this rule at runtime. May list file or rule targets. Generally allows any target. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="cmake_external-defines"></a>defines | Optional compilation definitions to be passed to the dependencies of this library. They are NOT passed to the compiler, you should duplicate them in the configuration options. | List of strings | optional | [] |
| <a id="cmake_external-deps"></a>deps | Optional dependencies to be copied into the directory structure. Typically those directly required for the external building of the library/binaries. (i.e. those that the external buidl system will be looking for and paths to which are provided by the calling rule) | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="cmake_external-env"></a>env | Environment variables to set during the build. $(execpath) macros may be used to point at files which are listed as data deps, tools_deps, or additional_tools, 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. No other macros are supported. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="cmake_external-env_vars"></a>env_vars | CMake environment variable values to join with toolchain-defined. For example, additional CXXFLAGS. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="cmake_external-generate_crosstool_file"></a>generate_crosstool_file | When True, CMake crosstool file will be generated from the toolchain values, provided cache-entries and env_vars (some values will still be passed as -Dkey=value and environment variables). If CMAKE_TOOLCHAIN_FILE cache entry is passed, specified crosstool file will be used When using this option, it makes sense to specify CMAKE_SYSTEM_NAME in the cache_entries - the rule makes only a poor guess about the target system, it is better to specify it manually. | Boolean | optional | False |
| <a id="cmake_external-headers_only"></a>headers_only | Flag variable to indicate that the library produces only headers | Boolean | optional | False |
@ -134,7 +136,7 @@ Rule for building CMake. Invokes bootstrap script and make install.
configure_make(<a href="#configure_make-name">name</a>, <a href="#configure_make-additional_inputs">additional_inputs</a>, <a href="#configure_make-additional_tools">additional_tools</a>, <a href="#configure_make-alwayslink">alwayslink</a>, <a href="#configure_make-autoconf">autoconf</a>, <a href="#configure_make-autoconf_env_vars">autoconf_env_vars</a>,
<a href="#configure_make-autoconf_options">autoconf_options</a>, <a href="#configure_make-autogen">autogen</a>, <a href="#configure_make-autogen_command">autogen_command</a>, <a href="#configure_make-autogen_env_vars">autogen_env_vars</a>, <a href="#configure_make-autogen_options">autogen_options</a>,
<a href="#configure_make-autoreconf">autoreconf</a>, <a href="#configure_make-autoreconf_env_vars">autoreconf_env_vars</a>, <a href="#configure_make-autoreconf_options">autoreconf_options</a>, <a href="#configure_make-binaries">binaries</a>, <a href="#configure_make-configure_command">configure_command</a>,
<a href="#configure_make-configure_env_vars">configure_env_vars</a>, <a href="#configure_make-configure_in_place">configure_in_place</a>, <a href="#configure_make-configure_options">configure_options</a>, <a href="#configure_make-data">data</a>, <a href="#configure_make-defines">defines</a>, <a href="#configure_make-deps">deps</a>,
<a href="#configure_make-configure_env_vars">configure_env_vars</a>, <a href="#configure_make-configure_in_place">configure_in_place</a>, <a href="#configure_make-configure_options">configure_options</a>, <a href="#configure_make-data">data</a>, <a href="#configure_make-defines">defines</a>, <a href="#configure_make-deps">deps</a>, <a href="#configure_make-env">env</a>,
<a href="#configure_make-headers_only">headers_only</a>, <a href="#configure_make-install_prefix">install_prefix</a>, <a href="#configure_make-interface_libraries">interface_libraries</a>, <a href="#configure_make-lib_name">lib_name</a>, <a href="#configure_make-lib_source">lib_source</a>, <a href="#configure_make-linkopts">linkopts</a>,
<a href="#configure_make-make_commands">make_commands</a>, <a href="#configure_make-out_bin_dir">out_bin_dir</a>, <a href="#configure_make-out_include_dir">out_include_dir</a>, <a href="#configure_make-out_lib_dir">out_lib_dir</a>, <a href="#configure_make-postfix_script">postfix_script</a>,
<a href="#configure_make-shared_libraries">shared_libraries</a>, <a href="#configure_make-static_libraries">static_libraries</a>, <a href="#configure_make-tools_deps">tools_deps</a>)
@ -169,6 +171,7 @@ Rule for building external libraries with configure-make pattern. Some 'configur
| <a id="configure_make-data"></a>data | Files needed by this rule at runtime. May list file or rule targets. Generally allows any target. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="configure_make-defines"></a>defines | Optional compilation definitions to be passed to the dependencies of this library. They are NOT passed to the compiler, you should duplicate them in the configuration options. | List of strings | optional | [] |
| <a id="configure_make-deps"></a>deps | Optional dependencies to be copied into the directory structure. Typically those directly required for the external building of the library/binaries. (i.e. those that the external buidl system will be looking for and paths to which are provided by the calling rule) | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="configure_make-env"></a>env | Environment variables to set during the build. $(execpath) macros may be used to point at files which are listed as data deps, tools_deps, or additional_tools, 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. No other macros are supported. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="configure_make-headers_only"></a>headers_only | Flag variable to indicate that the library produces only headers | Boolean | optional | False |
| <a id="configure_make-install_prefix"></a>install_prefix | Install prefix, i.e. relative path to where to install the result of the build. Passed to the 'configure' script with --prefix flag. | String | optional | "" |
| <a id="configure_make-interface_libraries"></a>interface_libraries | Optional names of the resulting interface libraries. | List of strings | optional | [] |
@ -190,7 +193,7 @@ Rule for building external libraries with configure-make pattern. Some 'configur
## make
<pre>
make(<a href="#make-name">name</a>, <a href="#make-additional_inputs">additional_inputs</a>, <a href="#make-additional_tools">additional_tools</a>, <a href="#make-alwayslink">alwayslink</a>, <a href="#make-binaries">binaries</a>, <a href="#make-data">data</a>, <a href="#make-defines">defines</a>, <a href="#make-deps">deps</a>,
make(<a href="#make-name">name</a>, <a href="#make-additional_inputs">additional_inputs</a>, <a href="#make-additional_tools">additional_tools</a>, <a href="#make-alwayslink">alwayslink</a>, <a href="#make-binaries">binaries</a>, <a href="#make-data">data</a>, <a href="#make-defines">defines</a>, <a href="#make-deps">deps</a>, <a href="#make-env">env</a>,
<a href="#make-headers_only">headers_only</a>, <a href="#make-interface_libraries">interface_libraries</a>, <a href="#make-keep_going">keep_going</a>, <a href="#make-lib_name">lib_name</a>, <a href="#make-lib_source">lib_source</a>, <a href="#make-linkopts">linkopts</a>, <a href="#make-make_commands">make_commands</a>,
<a href="#make-make_env_vars">make_env_vars</a>, <a href="#make-out_bin_dir">out_bin_dir</a>, <a href="#make-out_include_dir">out_include_dir</a>, <a href="#make-out_lib_dir">out_lib_dir</a>, <a href="#make-postfix_script">postfix_script</a>, <a href="#make-prefix">prefix</a>,
<a href="#make-shared_libraries">shared_libraries</a>, <a href="#make-static_libraries">static_libraries</a>, <a href="#make-tools_deps">tools_deps</a>)
@ -211,6 +214,7 @@ Rule for building external libraries with GNU Make. GNU Make commands (make and
| <a id="make-data"></a>data | Files needed by this rule at runtime. May list file or rule targets. Generally allows any target. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="make-defines"></a>defines | Optional compilation definitions to be passed to the dependencies of this library. They are NOT passed to the compiler, you should duplicate them in the configuration options. | List of strings | optional | [] |
| <a id="make-deps"></a>deps | Optional dependencies to be copied into the directory structure. Typically those directly required for the external building of the library/binaries. (i.e. those that the external buidl system will be looking for and paths to which are provided by the calling rule) | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="make-env"></a>env | Environment variables to set during the build. $(execpath) macros may be used to point at files which are listed as data deps, tools_deps, or additional_tools, 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. No other macros are supported. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="make-headers_only"></a>headers_only | Flag variable to indicate that the library produces only headers | Boolean | optional | False |
| <a id="make-interface_libraries"></a>interface_libraries | Optional names of the resulting interface libraries. | List of strings | optional | [] |
| <a id="make-keep_going"></a>keep_going | Keep going when some targets can not be made, -k flag is passed to make (applies only if make_commands attribute is not set). Please have a look at _create_make_script for default make_commands. | Boolean | optional | True |

View File

@ -3,6 +3,10 @@ load("@rules_foreign_cc//tools/build_defs:make.bzl", "make")
make(
name = "make_lib",
tools_deps = ["//simple_make/code:clang_wrapper.sh"],
env = {
"CLANG_WRAPPER": "$(execpath //simple_make/code:clang_wrapper.sh)",
},
lib_source = "//simple_make/code:sources",
static_libraries = ["liba.a"],
)

View File

@ -1,5 +1,10 @@
exports_files(["clang_wrapper.sh"])
filegroup(
name = "sources",
srcs = glob(["**"]),
srcs = glob(
["**"],
exclude = ["clang_wrapper.sh"],
),
visibility = ["//simple_make:__subpackages__"],
)

View File

@ -2,7 +2,7 @@ BUILD_DIR=build-out
default all $(BUILD_DIR)/lib/liba.a: liba.cpp liba.h
rm -rf $(BUILD_DIR)/lib
mkdir -p $(BUILD_DIR)/lib
clang -fPIC -o $(BUILD_DIR)/lib/liba.o -c liba.cpp
$(CLANG_WRAPPER) -fPIC -o $(BUILD_DIR)/lib/liba.o -c liba.cpp
ar rcs $(BUILD_DIR)/lib/liba.a $(BUILD_DIR)/lib/liba.o
install: $(BUILD_DIR)/lib/liba.a

View File

@ -0,0 +1,3 @@
#!/bin/bash -eu
exec clang "$@"

View File

@ -87,6 +87,15 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
allow_files = True,
default = [],
),
"env": attr.string_dict(
doc = (
"Environment variables to set during the build. " +
"$(execpath) macros may be used to point at files which are listed as data deps, tools_deps, or additional_tools, " +
"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. " +
"No other macros are supported."
),
),
"headers_only": attr.bool(
doc = "Flag variable to indicate that the library produces only headers",
mandatory = False,
@ -309,12 +318,21 @@ def cc_external_rule_impl(ctx, attrs):
# we need this fictive file in the root to get the path of the root in the script
empty = fictive_file_in_genroot(ctx.actions, ctx.label.name)
data_dependencies = ctx.attr.data + ctx.attr.tools_deps + ctx.attr.additional_tools
define_variables = [
set_cc_envs,
"export EXT_BUILD_ROOT=##pwd##",
"export BUILD_TMPDIR=##tmpdir##",
"export EXT_BUILD_DEPS=##tmpdir##",
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + empty.file.dirname + "/" + lib_name,
] + [
"export {key}={value}".format(
key=key,
# Prepend the exec root to each $(execpath ) lookup because the working directory will not be the exec root.
value=ctx.expand_location(value.replace("$(execpath ", "$$EXT_BUILD_ROOT$$/$(execpath "), data_dependencies)
)
for key, value in getattr(ctx.attr, "env", {}).items()
]
make_commands = []
@ -365,7 +383,7 @@ def cc_external_rule_impl(ctx, attrs):
empty.file,
wrapped_outputs.log_file,
],
tools = depset([wrapped_outputs.script_file] + ctx.files.data),
tools = depset([wrapped_outputs.script_file] + ctx.files.data + ctx.files.tools_deps + ctx.files.additional_tools, transitive = [data[DefaultInfo].default_runfiles.files for data in data_dependencies]),
# We should take the default PATH passed by Bazel, not that from cc_toolchain
# for Windows, because the PATH under msys2 is different and that is which we need
# for shell commands