Skip to content

Commit 10d47d4

Browse files
authored
configure_make: support autotools cross-compilation (#1247)
1 parent 06e6964 commit 10d47d4

File tree

11 files changed

+258
-28
lines changed

11 files changed

+258
-28
lines changed

foreign_cc/built_tools/make_build.bzl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ load(
1515
"get_flags_info",
1616
"get_tools_info",
1717
)
18+
load("//foreign_cc/private:detect_xcompile.bzl", "detect_xcompile")
1819
load("//foreign_cc/private/framework:platform.bzl", "os_name")
1920

2021
def _make_tool_impl(ctx):
@@ -74,6 +75,25 @@ def _make_tool_impl(ctx):
7475
if os_name(ctx) == "macos":
7576
non_sysroot_ldflags += ["-undefined", "error"]
7677

78+
configure_options = [
79+
"--without-guile",
80+
"--with-guile=no",
81+
"--disable-dependency-tracking",
82+
"--prefix=$$INSTALLDIR$$",
83+
]
84+
85+
install_cmd = ["./make install"]
86+
87+
xcompile_options = detect_xcompile(ctx)
88+
if xcompile_options:
89+
configure_options.extend(xcompile_options)
90+
91+
# We can't use make to install make when cross-compiling
92+
install_cmd = [
93+
"mkdir -p $$INSTALLDIR$$/bin",
94+
"cp -p make $$INSTALLDIR$$/bin/make",
95+
]
96+
7797
env.update({
7898
"AR": absolute_ar,
7999
"ARFLAGS": _join_flags_list(ctx.workspace_name, arflags),
@@ -85,11 +105,10 @@ def _make_tool_impl(ctx):
85105

86106
configure_env = " ".join(["%s=\"%s\"" % (key, value) for key, value in env.items()])
87107
script = [
88-
"%s ./configure --without-guile --with-guile=no --disable-dependency-tracking --prefix=$$INSTALLDIR$$" % configure_env,
108+
"%s ./configure %s" % (configure_env, " ".join(configure_options)),
89109
"cat build.cfg",
90110
"./build.sh",
91-
"./make install",
92-
]
111+
] + install_cmd
93112

94113
return built_tool_rule_impl(
95114
ctx,

foreign_cc/built_tools/pkgconfig_build.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ load(
1515
"get_flags_info",
1616
"get_tools_info",
1717
)
18+
load("//foreign_cc/private:detect_xcompile.bzl", "detect_xcompile")
1819
load("//foreign_cc/private/framework:platform.bzl", "os_name")
1920
load("//toolchains/native_tools:tool_access.bzl", "get_make_data")
2021

@@ -61,6 +62,15 @@ def _pkgconfig_tool_impl(ctx):
6162

6263
make_data = get_make_data(ctx)
6364

65+
configure_options = [
66+
"--with-internal-glib",
67+
"--prefix=$$INSTALLDIR$$",
68+
]
69+
70+
xcompile_options = detect_xcompile(ctx)
71+
if xcompile_options:
72+
configure_options.extend(xcompile_options)
73+
6474
env.update({
6575
"AR": absolute_ar,
6676
"ARFLAGS": _join_flags_list(ctx.workspace_name, arflags),
@@ -73,7 +83,7 @@ def _pkgconfig_tool_impl(ctx):
7383

7484
configure_env = " ".join(["%s=\"%s\"" % (key, value) for key, value in env.items()])
7585
script = [
76-
"%s ./configure --with-internal-glib --prefix=$$INSTALLDIR$$" % configure_env,
86+
"%s ./configure %s" % (configure_env, " ".join(configure_options)),
7787
"%s" % make_data.path,
7888
"%s install" % make_data.path,
7989
]

foreign_cc/built_tools/private/built_tools_framework.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ load("//foreign_cc/private:cc_toolchain_util.bzl", "absolutize_path_in_str")
55
load("//foreign_cc/private:detect_root.bzl", "detect_root")
66
load("//foreign_cc/private:framework.bzl", "get_env_prelude", "wrap_outputs")
77
load("//foreign_cc/private/framework:helpers.bzl", "convert_shell_script", "shebang")
8+
load("//foreign_cc/private/framework:platform.bzl", "PLATFORM_CONSTRAINTS_RULE_ATTRIBUTES")
89

910
# Common attributes for all built_tool rules
1011
FOREIGN_CC_BUILT_TOOLS_ATTRS = {
12+
"configure_xcompile": attr.bool(
13+
doc = (
14+
"If this is set and an xcompile scenario is detected, pass the necessary autotools flags. (Only applies if autotools is used)"
15+
),
16+
default = False,
17+
),
1118
"env": attr.string_dict(
1219
doc = "Environment variables to set during the build. This attribute is subject to make variable substitution.",
1320
default = {},
@@ -26,6 +33,9 @@ FOREIGN_CC_BUILT_TOOLS_ATTRS = {
2633
),
2734
}
2835

36+
# this would be cleaner as x | y, but that's not supported in bazel 5.4.0
37+
FOREIGN_CC_BUILT_TOOLS_ATTRS.update(PLATFORM_CONSTRAINTS_RULE_ATTRIBUTES)
38+
2939
# Common fragments for all built_tool rules
3040
FOREIGN_CC_BUILT_TOOLS_FRAGMENTS = [
3141
"apple",

foreign_cc/cmake.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def _create_configure_script(configureParameters):
253253

254254
configure_script = create_cmake_script(
255255
workspace_name = ctx.workspace_name,
256+
current_label = ctx.label,
256257
target_os = target_os_name(ctx),
257258
target_arch = target_arch_name(ctx),
258259
host_os = os_name(ctx),

foreign_cc/configure.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load(
99
)
1010
load("//foreign_cc/private:configure_script.bzl", "create_configure_script")
1111
load("//foreign_cc/private:detect_root.bzl", "detect_root")
12+
load("//foreign_cc/private:detect_xcompile.bzl", "detect_xcompile")
1213
load(
1314
"//foreign_cc/private:framework.bzl",
1415
"CC_EXTERNAL_RULE_ATTRIBUTES",
@@ -78,6 +79,10 @@ def _create_configure_script(configureParameters):
7879
configure_prefix = "{} ".format(expand_locations_and_make_variables(ctx, ctx.attr.configure_prefix, "configure_prefix", data)) if ctx.attr.configure_prefix else ""
7980
configure_options = [expand_locations_and_make_variables(ctx, option, "configure_option", data) for option in ctx.attr.configure_options] if ctx.attr.configure_options else []
8081

82+
xcompile_options = detect_xcompile(ctx)
83+
if xcompile_options:
84+
configure_options.extend(xcompile_options)
85+
8186
for target in ctx.attr.targets:
8287
# Configure will have generated sources into `$BUILD_TMPDIR` so make sure we `cd` there
8388
make_commands.append("{prefix}{make} {target} {args}".format(
@@ -187,6 +192,12 @@ def _attrs():
187192
"configure_prefix": attr.string(
188193
doc = "A prefix for the call to the `configure_command`.",
189194
),
195+
"configure_xcompile": attr.bool(
196+
doc = (
197+
"If this is set and an xcompile scenario is detected, pass the necessary autotools flags."
198+
),
199+
default = False,
200+
),
190201
"install_prefix": attr.string(
191202
doc = (
192203
"Install prefix, i.e. relative path to where to install the result of the build. " +

foreign_cc/private/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ bzl_library(
3939
deps = [
4040
":cc_toolchain_util",
4141
":detect_root",
42+
":detect_xcompile.bzl",
4243
":run_shell_file_utils",
4344
"//foreign_cc:providers",
4445
"//foreign_cc/private/framework:helpers",
46+
"//foreign_cc/private/framework:platform",
4547
"@bazel_skylib//lib:collections",
4648
"@bazel_skylib//lib:paths",
4749
"@bazel_tools//tools/cpp:toolchain_utils.bzl",

foreign_cc/private/cmake_script.bzl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ _TARGET_ARCH_PARAMS = {
3131
"aarch64": {
3232
"CMAKE_SYSTEM_PROCESSOR": "aarch64",
3333
},
34+
"s390x": {
35+
"CMAKE_SYSTEM_PROCESSOR": "s390x",
36+
},
3437
"x86_64": {
3538
"CMAKE_SYSTEM_PROCESSOR": "x86_64",
3639
},
3740
}
3841

3942
def create_cmake_script(
4043
workspace_name,
44+
current_label,
4145
target_os,
4246
target_arch,
4347
host_os,
@@ -59,6 +63,7 @@ def create_cmake_script(
5963
6064
Args:
6165
workspace_name: current workspace name
66+
current_label: The label of the target currently being built
6267
target_os: The target OS for the build
6368
target_arch: The target arch for the build
6469
host_os: The execution OS for the build
@@ -120,7 +125,10 @@ def create_cmake_script(
120125
# by setting CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR,
121126
# see https://github.com/bazelbuild/rules_foreign_cc/issues/289,
122127
# and https://github.com/bazelbuild/rules_foreign_cc/pull/1062
123-
if target_os != host_os and target_os != "unknown":
128+
if target_os == "unknown":
129+
# buildifier: disable=print
130+
print("target_os is unknown, please update foreign_cc/private/framework/platform.bzl and foreign_cc/private/cmake_script.bzl; triggered by", current_label)
131+
elif target_os != host_os:
124132
params.cache.update(_TARGET_OS_PARAMS.get(target_os, {}))
125133
params.cache.update(_TARGET_ARCH_PARAMS.get(target_arch, {}))
126134

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# buildifier: disable=module-docstring
2+
load(
3+
"//foreign_cc/private/framework:platform.bzl",
4+
"arch_name",
5+
"os_name",
6+
"target_arch_name",
7+
"target_os_name",
8+
"triplet_name",
9+
)
10+
11+
def detect_xcompile(ctx):
12+
"""A helper function for detecting and setting autoconf-style xcompile flags
13+
14+
Args:
15+
ctx (ctx): The current rule's context object
16+
17+
Returns:
18+
list(str): The flags to set, or None if xcompiling is disabled
19+
"""
20+
21+
if not ctx.attr.configure_xcompile:
22+
return None
23+
24+
host_triplet = triplet_name(
25+
os_name(ctx),
26+
arch_name(ctx),
27+
)
28+
29+
if host_triplet == "unknown":
30+
# buildifier: disable=print
31+
print("host is unknown; please update foreign_cc/private/framework/platform.bzl; triggered by", ctx.label)
32+
return None
33+
34+
target_triplet = triplet_name(
35+
target_os_name(ctx),
36+
target_arch_name(ctx),
37+
)
38+
39+
if target_triplet == "unknown":
40+
# buildifier: disable=print
41+
print("target is unknown; please update foreign_cc/private/framework/platform.bzl; triggered by", ctx.label)
42+
return None
43+
44+
if target_triplet == host_triplet:
45+
return None
46+
47+
# We pass both --host and --build here, even though we technically only
48+
# need to pass --host. This is because autotools compares them (without
49+
# normalization) to determine if a build is a cross-compile
50+
#
51+
# If we don't pass --build, autotools will populate it itself, and it might
52+
# be normalized such that autotools thinks it's a cross-compile, but it
53+
# shouldn't be.
54+
#
55+
# An example of this is if we pass --host=x86_64-pc-linux-gnu but the
56+
# target compiler thinks it's for x86_64-unknown-linux-gnu; if we don't
57+
# pass --build, that will incorrectly be considered a cross-compile.
58+
#
59+
# Also, no, this isn't backwards. --host means target
60+
# https://www.gnu.org/software/automake/manual/html_node/Cross_002dCompilation.html
61+
return ["--host=" + target_triplet, "--build=" + host_triplet]

foreign_cc/private/framework.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ load(
1515
"script_extension",
1616
"shebang",
1717
)
18+
load("//foreign_cc/private/framework:platform.bzl", "PLATFORM_CONSTRAINTS_RULE_ATTRIBUTES")
1819
load(
1920
":cc_toolchain_util.bzl",
2021
"LibrariesToLinkInfo",
@@ -209,8 +210,6 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
209210
cfg = "exec",
210211
default = [],
211212
),
212-
"_aarch64_constraint": attr.label(default = Label("@platforms//cpu:aarch64")),
213-
"_android_constraint": attr.label(default = Label("@platforms//os:android")),
214213
# we need to declare this attribute to access cc_toolchain
215214
"_cc_toolchain": attr.label(
216215
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
@@ -220,10 +219,11 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
220219
cfg = "exec",
221220
default = Label("@rules_foreign_cc//foreign_cc/private/framework:platform_info"),
222221
),
223-
"_linux_constraint": attr.label(default = Label("@platforms//os:linux")),
224-
"_x86_64_constraint": attr.label(default = Label("@platforms//cpu:x86_64")),
225222
}
226223

224+
# this would be cleaner as x | y, but that's not supported in bazel 5.4.0
225+
CC_EXTERNAL_RULE_ATTRIBUTES.update(PLATFORM_CONSTRAINTS_RULE_ATTRIBUTES)
226+
227227
# A list of common fragments required by rules using this framework
228228
CC_EXTERNAL_RULE_FRAGMENTS = [
229229
"apple",

0 commit comments

Comments
 (0)