|
| 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] |
0 commit comments