Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix darwin flag when cross-compiling from Linux to MacOS #413

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 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,18 @@
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 image with Nix, issue the following command:
```
nix-shell --command 'bazel build --config=cross :hello_image_tarball'
```
nix-shell --command 'bazel run --config=cross :hello'

You can then do load the image into Docker with:
benradf marked this conversation as resolved.
Show resolved Hide resolved
```
docker load -i bazel-bin/hello_image_tarball/tarball.tar
```

And run it as you normally would with Docker.
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"
benradf marked this conversation as resolved.
Show resolved Hide resolved

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