Skip to content

Commit

Permalink
Merge pull request #413 from tweag/fix_cross_cpu
Browse files Browse the repository at this point in the history
Fix `darwin` flag when cross-compiling from Linux to MacOS
  • Loading branch information
mergify[bot] authored Sep 7, 2023
2 parents 69e71da + c604e89 commit 4306671
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
17 changes: 10 additions & 7 deletions examples/toolchains/cc_cross_osx_to_linux_amd64/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
C++ With Dependencies Toolchain Example
=======================================
Linux C++ In Docker, Cross Compiled on MacOS
============================================

This is an example C++ project with dependencies that uses `rules_cc`.

This example uses the Nix package manager to provide C++ dependencies, and as such only works with Nix installed. Demonstrating other methods of providing C++ dependencies is out of scope of this example.
Builds a Docker image containing a C++ program cross compiled for Linux, on MacOS.

# Usage

To run the example with Nix, issue the following command:
To build the Docker image with Nix, issue the following command:
```
nix-shell --command 'bazel build --config=cross :hello_image_tarball'
```

Or if you have Docker installed, you can build and run the image with a single command:
```
nix-shell --command 'bazel run --config=cross :hello'
nix-shell --command 'bazel run --config=cross :hello_image'
```
8 changes: 4 additions & 4 deletions toolchains/cc/cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def _parse_cc_toolchain_info(content, filename):

def _nixpkgs_cc_toolchain_config_impl(repository_ctx):
host_cpu = get_cpu_value(repository_ctx)
cross_cpu = repository_ctx.attr.cross_cpu or host_cpu
darwin = (host_cpu == "darwin" or host_cpu == "darwin_arm64") and cross_cpu == host_cpu
cpu_value = repository_ctx.attr.cross_cpu or host_cpu
darwin = cpu_value == "darwin" or cpu_value == "darwin_arm64"

cc_toolchain_info_file = repository_ctx.path(repository_ctx.attr.cc_toolchain_info)
if not cc_toolchain_info_file.exists and not repository_ctx.attr.fail_not_supported:
Expand Down Expand Up @@ -187,7 +187,7 @@ def _nixpkgs_cc_toolchain_config_impl(repository_ctx):
repository_ctx.path(repository_ctx.attr._build),
{
"%{cc_toolchain_identifier}": "local",
"%{name}": cross_cpu,
"%{name}": cpu_value,
"%{modulemap}": ("\":module.modulemap\"" if needs_module_map else "None"),
"%{supports_param_files}": "0" if darwin else "1",
"%{cc_compiler_deps}": get_starlark_list(
Expand All @@ -200,7 +200,7 @@ def _nixpkgs_cc_toolchain_config_impl(repository_ctx):
"%{abi_libc_version}": "local",
"%{host_system_name}": "local",
"%{target_libc}": "macosx" if darwin else "local",
"%{target_cpu}": cross_cpu,
"%{target_cpu}": cpu_value,
"%{target_system_name}": "local",
"%{tool_paths}": ",\n ".join(
['"%s": "%s"' % (k, v) for (k, v) in info.tool_paths.items()],
Expand Down

0 comments on commit 4306671

Please sign in to comment.