2021-06-22 20:10:31 +00:00
|
|
|
"""A rule for building projects using the [Configure+Make](https://www.gnu.org/prep/standards/html_node/Configuration.html)
|
|
|
|
build tool
|
2021-06-02 18:58:56 +00:00
|
|
|
"""
|
|
|
|
|
2021-03-12 16:54:14 +00:00
|
|
|
load(
|
|
|
|
"//foreign_cc/private:cc_toolchain_util.bzl",
|
|
|
|
"get_flags_info",
|
|
|
|
"get_tools_info",
|
|
|
|
"is_debug_mode",
|
|
|
|
)
|
|
|
|
load("//foreign_cc/private:configure_script.bzl", "create_configure_script")
|
|
|
|
load("//foreign_cc/private:detect_root.bzl", "detect_root")
|
|
|
|
load(
|
|
|
|
"//foreign_cc/private:framework.bzl",
|
|
|
|
"CC_EXTERNAL_RULE_ATTRIBUTES",
|
|
|
|
"CC_EXTERNAL_RULE_FRAGMENTS",
|
|
|
|
"cc_external_rule_impl",
|
|
|
|
"create_attrs",
|
2021-06-21 15:24:34 +00:00
|
|
|
"expand_locations",
|
2021-10-19 14:51:53 +00:00
|
|
|
"expand_locations_and_make_variables",
|
2021-03-12 16:54:14 +00:00
|
|
|
)
|
2021-07-28 14:30:04 +00:00
|
|
|
load("//foreign_cc/private:transitions.bzl", "make_variant")
|
2021-05-05 20:35:54 +00:00
|
|
|
load("//foreign_cc/private/framework:platform.bzl", "os_name")
|
2021-03-12 16:54:14 +00:00
|
|
|
load("//toolchains/native_tools:tool_access.bzl", "get_make_data")
|
|
|
|
|
|
|
|
def _configure_make(ctx):
|
|
|
|
make_data = get_make_data(ctx)
|
|
|
|
|
|
|
|
tools_deps = ctx.attr.tools_deps + make_data.deps
|
|
|
|
|
2021-05-09 05:24:30 +00:00
|
|
|
if ctx.attr.autogen and not ctx.attr.configure_in_place:
|
|
|
|
fail("`autogen` requires `configure_in_place = True`. Please update {}".format(
|
|
|
|
ctx.label,
|
|
|
|
))
|
|
|
|
|
|
|
|
if ctx.attr.autoconf and not ctx.attr.configure_in_place:
|
|
|
|
fail("`autoconf` requires `configure_in_place = True`. Please update {}".format(
|
|
|
|
ctx.label,
|
|
|
|
))
|
|
|
|
|
|
|
|
if ctx.attr.autoreconf and not ctx.attr.configure_in_place:
|
|
|
|
fail("`autoreconf` requires `configure_in_place = True`. Please update {}".format(
|
|
|
|
ctx.label,
|
|
|
|
))
|
|
|
|
|
2021-03-12 16:54:14 +00:00
|
|
|
copy_results = "##copy_dir_contents_to_dir## $$BUILD_TMPDIR$$/$$INSTALL_PREFIX$$ $$INSTALLDIR$$\n"
|
|
|
|
|
|
|
|
attrs = create_attrs(
|
|
|
|
ctx.attr,
|
|
|
|
configure_name = "Configure",
|
|
|
|
create_configure_script = _create_configure_script,
|
|
|
|
postfix_script = copy_results + "\n" + ctx.attr.postfix_script,
|
|
|
|
tools_deps = tools_deps,
|
|
|
|
make_path = make_data.path,
|
|
|
|
)
|
|
|
|
return cc_external_rule_impl(ctx, attrs)
|
|
|
|
|
|
|
|
def _create_configure_script(configureParameters):
|
|
|
|
ctx = configureParameters.ctx
|
2021-03-17 13:42:44 +00:00
|
|
|
attrs = configureParameters.attrs
|
2021-03-12 16:54:14 +00:00
|
|
|
inputs = configureParameters.inputs
|
|
|
|
|
|
|
|
install_prefix = _get_install_prefix(ctx)
|
|
|
|
|
|
|
|
tools = get_tools_info(ctx)
|
|
|
|
flags = get_flags_info(ctx)
|
|
|
|
|
2021-03-17 21:50:25 +00:00
|
|
|
define_install_prefix = ["export INSTALL_PREFIX=\"" + _get_install_prefix(ctx) + "\""]
|
2021-03-12 16:54:14 +00:00
|
|
|
|
2021-06-21 15:24:34 +00:00
|
|
|
data = ctx.attr.data + ctx.attr.build_data
|
2021-03-17 13:42:44 +00:00
|
|
|
|
|
|
|
# Generate a list of arguments for make
|
|
|
|
args = " ".join([
|
|
|
|
ctx.expand_location(arg, data)
|
|
|
|
for arg in ctx.attr.args
|
|
|
|
])
|
|
|
|
|
2021-10-19 14:51:53 +00:00
|
|
|
user_env = expand_locations_and_make_variables(ctx, "env", data)
|
2021-06-21 15:24:34 +00:00
|
|
|
|
2021-03-17 13:42:44 +00:00
|
|
|
make_commands = []
|
2021-07-19 19:44:21 +00:00
|
|
|
prefix = "{} ".format(expand_locations(ctx, attrs.tool_prefix, data)) if attrs.tool_prefix else ""
|
|
|
|
configure_prefix = "{} ".format(expand_locations(ctx, ctx.attr.configure_prefix, data)) if ctx.attr.configure_prefix else ""
|
2021-03-17 13:42:44 +00:00
|
|
|
|
2021-06-16 13:09:15 +00:00
|
|
|
for target in ctx.attr.targets:
|
|
|
|
# Configure will have generated sources into `$BUILD_TMPDIR` so make sure we `cd` there
|
|
|
|
make_commands.append("{prefix}{make} -C $$BUILD_TMPDIR$$ {target} {args}".format(
|
|
|
|
prefix = prefix,
|
|
|
|
make = attrs.make_path,
|
|
|
|
args = args,
|
|
|
|
target = target,
|
|
|
|
))
|
2021-03-17 13:42:44 +00:00
|
|
|
|
2021-03-12 16:54:14 +00:00
|
|
|
configure = create_configure_script(
|
|
|
|
workspace_name = ctx.workspace_name,
|
|
|
|
# as default, pass execution OS as target OS
|
|
|
|
target_os = os_name(ctx),
|
|
|
|
tools = tools,
|
|
|
|
flags = flags,
|
2021-06-16 13:09:15 +00:00
|
|
|
root = detect_root(ctx.attr.lib_source),
|
2021-03-12 16:54:14 +00:00
|
|
|
user_options = ctx.attr.configure_options,
|
|
|
|
is_debug = is_debug_mode(ctx),
|
2021-06-14 16:59:49 +00:00
|
|
|
configure_prefix = configure_prefix,
|
2021-03-12 16:54:14 +00:00
|
|
|
configure_command = ctx.attr.configure_command,
|
|
|
|
deps = ctx.attr.deps,
|
|
|
|
inputs = inputs,
|
2021-06-21 15:24:34 +00:00
|
|
|
env_vars = user_env,
|
2021-03-12 16:54:14 +00:00
|
|
|
configure_in_place = ctx.attr.configure_in_place,
|
|
|
|
autoconf = ctx.attr.autoconf,
|
|
|
|
autoconf_options = ctx.attr.autoconf_options,
|
|
|
|
autoreconf = ctx.attr.autoreconf,
|
|
|
|
autoreconf_options = ctx.attr.autoreconf_options,
|
|
|
|
autogen = ctx.attr.autogen,
|
|
|
|
autogen_command = ctx.attr.autogen_command,
|
|
|
|
autogen_options = ctx.attr.autogen_options,
|
2021-03-17 13:42:44 +00:00
|
|
|
make_commands = make_commands,
|
2021-05-09 22:44:57 +00:00
|
|
|
make_path = attrs.make_path,
|
2021-03-12 16:54:14 +00:00
|
|
|
)
|
2021-03-17 21:50:25 +00:00
|
|
|
return define_install_prefix + configure
|
2021-03-12 16:54:14 +00:00
|
|
|
|
|
|
|
def _get_install_prefix(ctx):
|
|
|
|
if ctx.attr.install_prefix:
|
|
|
|
return ctx.attr.install_prefix
|
|
|
|
if ctx.attr.lib_name:
|
|
|
|
return ctx.attr.lib_name
|
|
|
|
return ctx.attr.name
|
|
|
|
|
|
|
|
def _attrs():
|
|
|
|
attrs = dict(CC_EXTERNAL_RULE_ATTRIBUTES)
|
|
|
|
attrs.update({
|
2021-03-17 13:42:44 +00:00
|
|
|
"args": attr.string_list(
|
|
|
|
doc = "A list of arguments to pass to the call to `make`",
|
|
|
|
),
|
2021-03-12 16:54:14 +00:00
|
|
|
"autoconf": attr.bool(
|
|
|
|
mandatory = False,
|
|
|
|
default = False,
|
|
|
|
doc = (
|
|
|
|
"Set to True if 'autoconf' should be invoked before 'configure', " +
|
2021-05-09 05:24:30 +00:00
|
|
|
"currently requires `configure_in_place` to be True."
|
2021-03-12 16:54:14 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
"autoconf_options": attr.string_list(
|
|
|
|
doc = "Any options to be put in the 'autoconf.sh' command line.",
|
|
|
|
),
|
|
|
|
"autogen": attr.bool(
|
|
|
|
doc = (
|
|
|
|
"Set to True if 'autogen.sh' should be invoked before 'configure', " +
|
2021-05-09 05:24:30 +00:00
|
|
|
"currently requires `configure_in_place` to be True."
|
2021-03-12 16:54:14 +00:00
|
|
|
),
|
|
|
|
mandatory = False,
|
|
|
|
default = False,
|
|
|
|
),
|
|
|
|
"autogen_command": attr.string(
|
|
|
|
doc = (
|
|
|
|
"The name of the autogen script file, default: autogen.sh. " +
|
|
|
|
"Many projects use autogen.sh however the Autotools FAQ recommends bootstrap " +
|
|
|
|
"so we provide this option to support that."
|
|
|
|
),
|
|
|
|
default = "autogen.sh",
|
|
|
|
),
|
|
|
|
"autogen_options": attr.string_list(
|
|
|
|
doc = "Any options to be put in the 'autogen.sh' command line.",
|
|
|
|
),
|
|
|
|
"autoreconf": attr.bool(
|
|
|
|
doc = (
|
|
|
|
"Set to True if 'autoreconf' should be invoked before 'configure.', " +
|
2021-05-09 05:24:30 +00:00
|
|
|
"currently requires `configure_in_place` to be True."
|
2021-03-12 16:54:14 +00:00
|
|
|
),
|
|
|
|
mandatory = False,
|
|
|
|
default = False,
|
|
|
|
),
|
|
|
|
"autoreconf_options": attr.string_list(
|
|
|
|
doc = "Any options to be put in the 'autoreconf.sh' command line.",
|
|
|
|
),
|
|
|
|
"configure_command": attr.string(
|
|
|
|
doc = (
|
|
|
|
"The name of the configuration script file, default: configure. " +
|
|
|
|
"The file must be in the root of the source directory."
|
|
|
|
),
|
|
|
|
default = "configure",
|
|
|
|
),
|
|
|
|
"configure_in_place": attr.bool(
|
|
|
|
doc = (
|
|
|
|
"Set to True if 'configure' should be invoked in place, i.e. from its enclosing " +
|
|
|
|
"directory."
|
|
|
|
),
|
|
|
|
mandatory = False,
|
|
|
|
default = False,
|
|
|
|
),
|
|
|
|
"configure_options": attr.string_list(
|
|
|
|
doc = "Any options to be put on the 'configure' command line.",
|
|
|
|
),
|
2021-06-14 16:59:49 +00:00
|
|
|
"configure_prefix": attr.string(
|
|
|
|
doc = "A prefix for the call to the `configure_command`.",
|
|
|
|
),
|
2021-03-12 16:54:14 +00:00
|
|
|
"install_prefix": attr.string(
|
|
|
|
doc = (
|
|
|
|
"Install prefix, i.e. relative path to where to install the result of the build. " +
|
|
|
|
"Passed to the 'configure' script with --prefix flag."
|
|
|
|
),
|
|
|
|
mandatory = False,
|
|
|
|
),
|
2021-03-17 13:42:44 +00:00
|
|
|
"targets": attr.string_list(
|
|
|
|
doc = (
|
|
|
|
"A list of targets within the foreign build system to produce. An empty string (`\"\"`) will result in " +
|
|
|
|
"a call to the underlying build system with no explicit target set"
|
|
|
|
),
|
|
|
|
mandatory = False,
|
|
|
|
default = ["", "install"],
|
|
|
|
),
|
2021-03-12 16:54:14 +00:00
|
|
|
})
|
2021-03-17 13:42:44 +00:00
|
|
|
|
2021-03-12 16:54:14 +00:00
|
|
|
return attrs
|
|
|
|
|
|
|
|
configure_make = rule(
|
|
|
|
doc = (
|
|
|
|
"Rule for building external libraries with configure-make pattern. " +
|
|
|
|
"Some 'configure' script is invoked with --prefix=install (by default), " +
|
|
|
|
"and other parameters for compilation and linking, taken from Bazel C/C++ " +
|
|
|
|
"toolchain and passed dependencies. " +
|
|
|
|
"After configuration, GNU Make is called."
|
|
|
|
),
|
|
|
|
attrs = _attrs(),
|
|
|
|
fragments = CC_EXTERNAL_RULE_FRAGMENTS,
|
|
|
|
output_to_genfiles = True,
|
|
|
|
implementation = _configure_make,
|
|
|
|
toolchains = [
|
|
|
|
"@rules_foreign_cc//toolchains:make_toolchain",
|
2021-04-30 16:03:32 +00:00
|
|
|
"@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain",
|
2021-03-12 16:54:14 +00:00
|
|
|
"@bazel_tools//tools/cpp:toolchain_type",
|
|
|
|
],
|
2021-04-19 15:01:50 +00:00
|
|
|
# TODO: Remove once https://github.com/bazelbuild/bazel/issues/11584 is closed and the min supported
|
|
|
|
# version is updated to a release of Bazel containing the new default for this setting.
|
|
|
|
incompatible_use_toolchain_transition = True,
|
2021-03-12 16:54:14 +00:00
|
|
|
)
|
2021-07-28 14:30:04 +00:00
|
|
|
|
|
|
|
def configure_make_variant(name, toolchain, **kwargs):
|
|
|
|
""" Wrapper macro around the configure_make() rule to force usage of the given make variant toolchain.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: The target name
|
|
|
|
toolchain: The desired make variant toolchain to use, e.g. @rules_foreign_cc//toolchains:preinstalled_nmake_toolchain
|
|
|
|
**kwargs: Remaining keyword arguments
|
|
|
|
"""
|
|
|
|
make_variant(
|
|
|
|
name = name,
|
|
|
|
rule = configure_make,
|
|
|
|
toolchain = toolchain,
|
|
|
|
**kwargs
|
|
|
|
)
|