|
13 | 13 | "//foreign_cc/private:detect_root.bzl", |
14 | 14 | "detect_root", |
15 | 15 | ) |
| 16 | +load("//foreign_cc/private:detect_xcompile.bzl", "detect_xcompile") |
16 | 17 | load( |
17 | 18 | "//foreign_cc/private:framework.bzl", |
18 | 19 | "CC_EXTERNAL_RULE_ATTRIBUTES", |
|
23 | 24 | ) |
24 | 25 | load("//foreign_cc/private:make_script.bzl", "pkgconfig_script") |
25 | 26 | 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 | +) |
26 | 32 | load("//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain") |
27 | 33 | load("//toolchains/native_tools:tool_access.bzl", "get_cmake_data", "get_meson_data", "get_ninja_data", "get_pkgconfig_data") |
28 | 34 |
|
@@ -141,6 +147,15 @@ def _create_meson_script(configureParameters): |
141 | 147 |
|
142 | 148 | target_args[target_name] = args |
143 | 149 |
|
| 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 | + |
144 | 159 | script.append("{meson} setup --prefix={install_dir} {setup_args} {options} {source_dir}".format( |
145 | 160 | meson = meson_path, |
146 | 161 | install_dir = "$$INSTALLDIR$$", |
@@ -208,6 +223,15 @@ def _attrs(): |
208 | 223 | doc = "__deprecated__: please use `target_args` with `'build'` target key.", |
209 | 224 | mandatory = False, |
210 | 225 | ), |
| 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 | + ), |
211 | 235 | "install": attr.bool( |
212 | 236 | doc = "__deprecated__: please use `targets` if you want to skip install.", |
213 | 237 | default = True, |
@@ -300,3 +324,28 @@ def _absolutize(workspace_name, text, force = False): |
300 | 324 |
|
301 | 325 | def _join_flags_list(workspace_name, flags): |
302 | 326 | 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 | + |
0 commit comments