2021-06-02 18:58:56 +00:00
|
|
|
"""A rule for building projects using the [GNU Make](https://www.gnu.org/software/make/) build tool"""
|
|
|
|
|
2024-11-19 12:19:37 +00:00
|
|
|
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
|
2021-03-12 16:54:14 +00:00
|
|
|
load(
|
|
|
|
"//foreign_cc/private:cc_toolchain_util.bzl",
|
|
|
|
"get_flags_info",
|
|
|
|
"get_tools_info",
|
|
|
|
)
|
|
|
|
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",
|
2022-02-09 22:34:12 +00:00
|
|
|
"expand_locations_and_make_variables",
|
2021-03-12 16:54:14 +00:00
|
|
|
)
|
2021-03-15 16:00:46 +00:00
|
|
|
load("//foreign_cc/private:make_script.bzl", "create_make_script")
|
2023-01-03 11:50:47 +00:00
|
|
|
load("//foreign_cc/private:transitions.bzl", "foreign_cc_rule_variant")
|
2021-03-12 16:54:14 +00:00
|
|
|
load("//toolchains/native_tools:tool_access.bzl", "get_make_data")
|
|
|
|
|
|
|
|
def _make(ctx):
|
|
|
|
make_data = get_make_data(ctx)
|
|
|
|
|
2022-12-10 20:16:40 +00:00
|
|
|
tools_data = [make_data]
|
2021-03-12 16:54:14 +00:00
|
|
|
|
|
|
|
attrs = create_attrs(
|
|
|
|
ctx.attr,
|
2021-07-29 13:13:26 +00:00
|
|
|
configure_name = "Make",
|
2021-03-12 16:54:14 +00:00
|
|
|
create_configure_script = _create_make_script,
|
2022-12-10 20:16:40 +00:00
|
|
|
tools_data = tools_data,
|
2021-03-12 16:54:14 +00:00
|
|
|
make_path = make_data.path,
|
|
|
|
)
|
|
|
|
return cc_external_rule_impl(ctx, attrs)
|
|
|
|
|
|
|
|
def _create_make_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
|
|
|
|
|
|
|
|
root = detect_root(ctx.attr.lib_source)
|
|
|
|
|
|
|
|
tools = get_tools_info(ctx)
|
|
|
|
flags = get_flags_info(ctx)
|
|
|
|
|
2021-06-14 16:59:49 +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
|
|
|
|
])
|
|
|
|
|
2022-02-09 22:34:12 +00:00
|
|
|
user_env = expand_locations_and_make_variables(ctx, ctx.attr.env, "env", data)
|
2021-11-27 15:52:54 +00:00
|
|
|
|
2021-03-17 13:42:44 +00:00
|
|
|
make_commands = []
|
2022-02-09 22:34:12 +00:00
|
|
|
prefix = "{} ".format(expand_locations_and_make_variables(ctx, attrs.tool_prefix, "tool_prefix", data)) if attrs.tool_prefix else ""
|
2021-04-12 15:23:42 +00:00
|
|
|
for target in ctx.attr.targets:
|
2022-06-22 19:35:28 +00:00
|
|
|
make_commands.append("{prefix}{make} {target} {args} PREFIX={install_prefix}".format(
|
2021-06-14 16:59:49 +00:00
|
|
|
prefix = prefix,
|
2021-04-12 15:23:42 +00:00
|
|
|
make = attrs.make_path,
|
|
|
|
args = args,
|
|
|
|
target = target,
|
2021-08-03 13:11:07 +00:00
|
|
|
install_prefix = ctx.attr.install_prefix,
|
2021-04-12 15:23:42 +00:00
|
|
|
))
|
2021-03-12 16:54:14 +00:00
|
|
|
|
2024-11-19 12:19:37 +00:00
|
|
|
cc_toolchain = find_cpp_toolchain(ctx)
|
|
|
|
is_msvc = cc_toolchain.compiler == "msvc-cl"
|
|
|
|
|
2021-03-12 16:54:14 +00:00
|
|
|
return create_make_script(
|
2021-11-27 15:52:54 +00:00
|
|
|
workspace_name = ctx.workspace_name,
|
|
|
|
tools = tools,
|
|
|
|
flags = flags,
|
2021-03-12 16:54:14 +00:00
|
|
|
root = root,
|
2021-11-27 15:52:54 +00:00
|
|
|
deps = ctx.attr.deps,
|
2021-03-12 16:54:14 +00:00
|
|
|
inputs = inputs,
|
2021-11-27 15:52:54 +00:00
|
|
|
env_vars = user_env,
|
2024-11-18 23:11:17 +00:00
|
|
|
make_prefix = prefix,
|
|
|
|
make_path = attrs.make_path,
|
|
|
|
make_targets = ctx.attr.targets,
|
|
|
|
make_args = args,
|
|
|
|
make_install_prefix = ctx.attr.install_prefix,
|
|
|
|
executable_ldflags_vars = ctx.attr.executable_ldflags_vars,
|
|
|
|
shared_ldflags_vars = ctx.attr.shared_ldflags_vars,
|
2024-11-19 12:19:37 +00:00
|
|
|
is_msvc = is_msvc,
|
2021-03-12 16:54:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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`",
|
|
|
|
),
|
2024-11-18 23:11:17 +00:00
|
|
|
"executable_ldflags_vars": attr.string_list(
|
|
|
|
doc = (
|
|
|
|
"A string list of variable names use as LDFLAGS for executables. These variables " +
|
|
|
|
"will be passed to the make command as make vars and overwrite what is defined in " +
|
|
|
|
"the Makefile."
|
|
|
|
),
|
|
|
|
mandatory = False,
|
|
|
|
default = [],
|
|
|
|
),
|
2021-08-03 13:11:07 +00:00
|
|
|
"install_prefix": attr.string(
|
|
|
|
doc = (
|
|
|
|
"Install prefix, i.e. relative path to where to install the result of the build. " +
|
|
|
|
"Passed as an arg to \"make\" as PREFIX=<install_prefix>."
|
|
|
|
),
|
|
|
|
mandatory = False,
|
|
|
|
default = "$$INSTALLDIR$$",
|
|
|
|
),
|
2024-11-18 23:11:17 +00:00
|
|
|
"shared_ldflags_vars": attr.string_list(
|
|
|
|
doc = (
|
|
|
|
"A string list of variable names use as LDFLAGS for shared libraries. These variables " +
|
|
|
|
"will be passed to the make command as make vars and overwrite what is defined in " +
|
|
|
|
"the Makefile."
|
|
|
|
),
|
|
|
|
mandatory = False,
|
|
|
|
default = [],
|
|
|
|
),
|
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 " +
|
2023-06-13 22:22:52 +00:00
|
|
|
"a call to the underlying build system with no explicit target set. However, in order to extract build " +
|
|
|
|
"outputs, you must execute at least an equivalent of make install, and have your make file copy the build " +
|
|
|
|
"outputs into the directory specified by `install_prefix`."
|
2021-03-12 16:54:14 +00:00
|
|
|
),
|
|
|
|
mandatory = False,
|
2021-03-17 13:42:44 +00:00
|
|
|
default = ["", "install"],
|
2021-03-12 16:54:14 +00:00
|
|
|
),
|
|
|
|
})
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
make = rule(
|
|
|
|
doc = (
|
|
|
|
"Rule for building external libraries with GNU Make. " +
|
2021-08-03 13:11:07 +00:00
|
|
|
"GNU Make commands (make and make install by default) are invoked with PREFIX=\"install\" " +
|
2021-03-12 16:54:14 +00:00
|
|
|
"(by default), and other environment variables for compilation and linking, taken from Bazel C/C++ " +
|
2023-06-13 22:22:52 +00:00
|
|
|
"toolchain and passed dependencies. " +
|
|
|
|
"Not all Makefiles will work equally well here, and some may require patching." +
|
|
|
|
"Your Makefile must either support passing the install prefix using the PREFIX flag, or " +
|
|
|
|
"it needs to have a different way to pass install prefix to it. An equivalent of " +
|
|
|
|
"make install MUST be specified as one of the targets." +
|
|
|
|
"This is because all the paths with param names prefixed by out_* are expressed " +
|
|
|
|
"as relative to INSTALLDIR, not the source directory." +
|
|
|
|
"That is, if you execute only make, but not make install, this rule will not be able " +
|
|
|
|
"to pick up any build outputs. Finally, your make install rule must dereference symlinks " +
|
|
|
|
"to ensure that the installed files don't end up being symlinks to files in the sandbox. " +
|
|
|
|
"For example, installation lines like `cp $SOURCE $DEST` must become `cp -L $SOURCE $DEST`, " +
|
|
|
|
"as the -L will ensure that symlinks are dereferenced."
|
2021-03-12 16:54:14 +00:00
|
|
|
),
|
|
|
|
attrs = _attrs(),
|
|
|
|
fragments = CC_EXTERNAL_RULE_FRAGMENTS,
|
|
|
|
output_to_genfiles = True,
|
2023-09-19 17:47:35 +00:00
|
|
|
provides = [CcInfo],
|
2021-03-12 16:54:14 +00:00
|
|
|
implementation = _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-07-28 14:30:04 +00:00
|
|
|
|
|
|
|
def make_variant(name, toolchain, **kwargs):
|
|
|
|
""" Wrapper macro around the 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
|
|
|
|
"""
|
2023-01-03 11:50:47 +00:00
|
|
|
foreign_cc_rule_variant(
|
2021-07-28 14:30:04 +00:00
|
|
|
name = name,
|
|
|
|
rule = make,
|
|
|
|
toolchain = toolchain,
|
|
|
|
**kwargs
|
|
|
|
)
|