Skip to content

Commit 6cbc31a

Browse files
committed
meson: add cross file support
1 parent 66b9b32 commit 6cbc31a

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

foreign_cc/meson.bzl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ load(
1313
"//foreign_cc/private:detect_root.bzl",
1414
"detect_root",
1515
)
16+
load("//foreign_cc/private:detect_xcompile.bzl", "detect_xcompile")
1617
load(
1718
"//foreign_cc/private:framework.bzl",
1819
"CC_EXTERNAL_RULE_ATTRIBUTES",
@@ -23,6 +24,11 @@ load(
2324
)
2425
load("//foreign_cc/private:make_script.bzl", "pkgconfig_script")
2526
load("//foreign_cc/private:transitions.bzl", "foreign_cc_rule_variant")
27+
load(
28+
"//foreign_cc/private/framework:platform.bzl",
29+
"target_arch_name",
30+
"target_os_name",
31+
)
2632
load("//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
2733
load("//toolchains/native_tools:tool_access.bzl", "get_cmake_data", "get_meson_data", "get_ninja_data", "get_pkgconfig_data")
2834

@@ -141,6 +147,15 @@ def _create_meson_script(configureParameters):
141147

142148
target_args[target_name] = args
143149

150+
# Generate cross file if needed
151+
if ctx.attr.generate_crosstool_file:
152+
cross_args = _write_cross_file(ctx, script)
153+
if cross_args:
154+
if "setup" not in target_args:
155+
target_args["setup"] = cross_args
156+
else:
157+
target_args["setup"] += cross_args
158+
144159
script.append("{meson} setup --prefix={install_dir} {setup_args} {options} {source_dir}".format(
145160
meson = meson_path,
146161
install_dir = "$$INSTALLDIR$$",
@@ -208,6 +223,15 @@ def _attrs():
208223
doc = "__deprecated__: please use `target_args` with `'build'` target key.",
209224
mandatory = False,
210225
),
226+
"generate_crosstool_file": attr.bool(
227+
doc = (
228+
"When True, a Meson cross file will be generated from the platform values. " +
229+
"A cross compile must be detected and no existing --cross-file passed as a " +
230+
"setup option."
231+
),
232+
mandatory = False,
233+
default = False,
234+
),
211235
"install": attr.bool(
212236
doc = "__deprecated__: please use `targets` if you want to skip install.",
213237
default = True,
@@ -300,3 +324,28 @@ def _absolutize(workspace_name, text, force = False):
300324

301325
def _join_flags_list(workspace_name, flags):
302326
return " ".join([_absolutize(workspace_name, flag) for flag in flags])
327+
328+
def _write_cross_file(ctx, script):
329+
# generate a cross file from platform values
330+
if not detect_xcompile(ctx, autoconf = False):
331+
return []
332+
333+
# adjust the system name Meson expects
334+
os_name = "darwin" if target_os_name(ctx) == "macos" else target_os_name(ctx)
335+
336+
# write cross file
337+
script.append("cat > crosstool_bazel.txt << EOF")
338+
script.append("[host_machine]")
339+
script.append("system = '{}'".format(os_name))
340+
script.append("cpu_family = '{}'".format(target_arch_name(ctx)))
341+
script.append("cpu = '{}'".format(target_arch_name(ctx)))
342+
script.append("endian = 'little'")
343+
script.append("")
344+
script.append("[properties]")
345+
script.append("skip_sanity_check = true")
346+
script.append("EOF")
347+
script.append("")
348+
349+
# return setup arg
350+
return ["--cross-file crosstool_bazel.txt"]
351+

foreign_cc/private/detect_xcompile.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ load(
88
"triplet_name",
99
)
1010

11-
def detect_xcompile(ctx):
11+
def detect_xcompile(ctx, autoconf = True):
1212
"""A helper function for detecting and setting autoconf-style xcompile flags
1313
1414
Args:
1515
ctx (ctx): The current rule's context object
16+
autoconf: True if being called from make/pkgconfig
1617
1718
Returns:
1819
list(str): The flags to set, or None if xcompiling is disabled
1920
"""
2021

21-
if not ctx.attr.configure_xcompile:
22+
if autoconf and not ctx.attr.configure_xcompile:
2223
return None
2324

2425
host_triplet = triplet_name(

0 commit comments

Comments
 (0)