From 0a69af3c9c290120d6b142a6ce72d0f42a797a36 Mon Sep 17 00:00:00 2001 From: Ben Radford Date: Wed, 6 Sep 2023 11:22:49 +0100 Subject: [PATCH 1/3] Attempt at fixing darwin flag for Linux to MacOS cross-compilation. --- toolchains/cc/cc.bzl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/toolchains/cc/cc.bzl b/toolchains/cc/cc.bzl index b846e14f5..052c21c3d 100644 --- a/toolchains/cc/cc.bzl +++ b/toolchains/cc/cc.bzl @@ -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: @@ -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( @@ -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()], From 5cfcc44bcf11a4c7350f5d37ad40551c1cd6b25b Mon Sep 17 00:00:00 2001 From: Ben Radford Date: Wed, 6 Sep 2023 11:23:43 +0100 Subject: [PATCH 2/3] Update README. --- .../cc_cross_osx_to_linux_amd64/README.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/toolchains/cc_cross_osx_to_linux_amd64/README.md b/examples/toolchains/cc_cross_osx_to_linux_amd64/README.md index 54a0dfbc5..87f05e8e7 100644 --- a/examples/toolchains/cc_cross_osx_to_linux_amd64/README.md +++ b/examples/toolchains/cc_cross_osx_to_linux_amd64/README.md @@ -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: ``` +docker load -i bazel-bin/hello_image_tarball/tarball.tar +``` + +And run it as you normally would with Docker. From 969c81b04f8ee4785f38ebe36d05ca705a6a23b7 Mon Sep 17 00:00:00 2001 From: Ben Radford Date: Thu, 7 Sep 2023 09:11:57 +0100 Subject: [PATCH 3/3] Update README again. --- examples/toolchains/cc_cross_osx_to_linux_amd64/README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/toolchains/cc_cross_osx_to_linux_amd64/README.md b/examples/toolchains/cc_cross_osx_to_linux_amd64/README.md index 87f05e8e7..49375abc1 100644 --- a/examples/toolchains/cc_cross_osx_to_linux_amd64/README.md +++ b/examples/toolchains/cc_cross_osx_to_linux_amd64/README.md @@ -5,14 +5,12 @@ Builds a Docker image containing a C++ program cross compiled for Linux, on MacO # Usage -To build the image 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' ``` -You can then do load the image into Docker with: +Or if you have Docker installed, you can build and run the image with a single command: ``` -docker load -i bazel-bin/hello_image_tarball/tarball.tar +nix-shell --command 'bazel run --config=cross :hello_image' ``` - -And run it as you normally would with Docker.