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

cc: Add cross_cpu arg to nixpkgs_cc_toolchain_config #401

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ nixpkgs_package(
<pre>
nixpkgs_cc_configure(<a href="#nixpkgs_cc_configure-name">name</a>, <a href="#nixpkgs_cc_configure-attribute_path">attribute_path</a>, <a href="#nixpkgs_cc_configure-nix_file">nix_file</a>, <a href="#nixpkgs_cc_configure-nix_file_content">nix_file_content</a>, <a href="#nixpkgs_cc_configure-nix_file_deps">nix_file_deps</a>, <a href="#nixpkgs_cc_configure-repositories">repositories</a>,
<a href="#nixpkgs_cc_configure-repository">repository</a>, <a href="#nixpkgs_cc_configure-nixopts">nixopts</a>, <a href="#nixpkgs_cc_configure-quiet">quiet</a>, <a href="#nixpkgs_cc_configure-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_cc_configure-exec_constraints">exec_constraints</a>,
<a href="#nixpkgs_cc_configure-target_constraints">target_constraints</a>, <a href="#nixpkgs_cc_configure-register">register</a>, <a href="#nixpkgs_cc_configure-cc_lang">cc_lang</a>)
<a href="#nixpkgs_cc_configure-target_constraints">target_constraints</a>, <a href="#nixpkgs_cc_configure-register">register</a>, <a href="#nixpkgs_cc_configure-cc_lang">cc_lang</a>, <a href="#nixpkgs_cc_configure-cross_cpu">cross_cpu</a>)
</pre>

Use a CC toolchain from Nixpkgs. No-op if not a nix-based platform.
Expand Down Expand Up @@ -380,6 +380,20 @@ default is <code>"c++"</code>

string, `"c++"` by default. Used to populate `CXX_FLAG` so the compiler is called in C++ mode. Can be set to `"none"` together with appropriate `copts` in the `cc_library` call: see above.

</p>
</td>
</tr>
<tr id="nixpkgs_cc_configure-cross_cpu">
<td><code>cross_cpu</code></td>
<td>

optional.
default is <code>""</code>

<p>

string, `""` by default. Used if you want to add a cross compilation C/C++ toolchain. Set this to the CPU architecture for the target CPU. For example x86_64, would be k8.

</p>
</td>
</tr>
Expand Down
16 changes: 15 additions & 1 deletion toolchains/cc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using `nixpkgs_cc_configure(..., cc_lang = "cuda")` or similar.
<pre>
nixpkgs_cc_configure(<a href="#nixpkgs_cc_configure-name">name</a>, <a href="#nixpkgs_cc_configure-attribute_path">attribute_path</a>, <a href="#nixpkgs_cc_configure-nix_file">nix_file</a>, <a href="#nixpkgs_cc_configure-nix_file_content">nix_file_content</a>, <a href="#nixpkgs_cc_configure-nix_file_deps">nix_file_deps</a>, <a href="#nixpkgs_cc_configure-repositories">repositories</a>,
<a href="#nixpkgs_cc_configure-repository">repository</a>, <a href="#nixpkgs_cc_configure-nixopts">nixopts</a>, <a href="#nixpkgs_cc_configure-quiet">quiet</a>, <a href="#nixpkgs_cc_configure-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_cc_configure-exec_constraints">exec_constraints</a>,
<a href="#nixpkgs_cc_configure-target_constraints">target_constraints</a>, <a href="#nixpkgs_cc_configure-register">register</a>, <a href="#nixpkgs_cc_configure-cc_lang">cc_lang</a>)
<a href="#nixpkgs_cc_configure-target_constraints">target_constraints</a>, <a href="#nixpkgs_cc_configure-register">register</a>, <a href="#nixpkgs_cc_configure-cc_lang">cc_lang</a>, <a href="#nixpkgs_cc_configure-cross_cpu">cross_cpu</a>)
</pre>

Use a CC toolchain from Nixpkgs. No-op if not a nix-based platform.
Expand Down Expand Up @@ -287,6 +287,20 @@ default is <code>"c++"</code>

string, `"c++"` by default. Used to populate `CXX_FLAG` so the compiler is called in C++ mode. Can be set to `"none"` together with appropriate `copts` in the `cc_library` call: see above.

</p>
</td>
</tr>
<tr id="nixpkgs_cc_configure-cross_cpu">
<td><code>cross_cpu</code></td>
<td>

optional.
default is <code>""</code>

<p>

string, `""` by default. Used if you want to add a cross compilation C/C++ toolchain. Set this to the CPU architecture for the target CPU. For example x86_64, would be k8.

</p>
</td>
</tr>
Expand Down
24 changes: 15 additions & 9 deletions toolchains/cc/cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ load("@bazel_skylib//lib:sets.bzl", "sets")
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package")
load(
"@rules_nixpkgs_core//:util.bzl",
"is_bazel_version_at_least",
"ensure_constraints",
"execute_or_fail",
"is_bazel_version_at_least",
)

def _parse_cc_toolchain_info(content, filename):
Expand Down Expand Up @@ -121,8 +121,9 @@ def _parse_cc_toolchain_info(content, filename):
)

def _nixpkgs_cc_toolchain_config_impl(repository_ctx):
cpu_value = get_cpu_value(repository_ctx)
darwin = cpu_value == "darwin" or cpu_value == "darwin_arm64"
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"

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 All @@ -147,7 +148,7 @@ def _nixpkgs_cc_toolchain_config_impl(repository_ctx):

# A module map is required for clang starting from Bazel version 3.3.0.
# https://github.com/bazelbuild/bazel/commit/8b9f74649512ee17ac52815468bf3d7e5e71c9fa
bazel_version_match, bazel_from_source = is_bazel_version_at_least("3.3.0")
bazel_version_match, bazel_from_source = is_bazel_version_at_least("3.3.0")
needs_module_map = info.is_clang and (bazel_version_match or bazel_from_source)
if needs_module_map:
generate_system_module_map = [
Expand Down Expand Up @@ -186,7 +187,7 @@ def _nixpkgs_cc_toolchain_config_impl(repository_ctx):
repository_ctx.path(repository_ctx.attr._build),
{
"%{cc_toolchain_identifier}": "local",
"%{name}": cpu_value,
"%{name}": cross_cpu,
"%{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 @@ -199,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}": cpu_value,
"%{target_cpu}": cross_cpu,
"%{target_system_name}": "local",
"%{tool_paths}": ",\n ".join(
['"%s": "%s"' % (k, v) for (k, v) in info.tool_paths.items()],
Expand All @@ -224,6 +225,7 @@ _nixpkgs_cc_toolchain_config = repository_rule(
attrs = {
"cc_toolchain_info": attr.label(),
"fail_not_supported": attr.bool(),
"cross_cpu": attr.string(),
"_unix_cc_toolchain_config": attr.label(
default = Label("@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl"),
),
Expand All @@ -246,7 +248,7 @@ _nixpkgs_cc_toolchain_config = repository_rule(
)

def _nixpkgs_cc_toolchain_impl(repository_ctx):
cpu = get_cpu_value(repository_ctx)
cpu = repository_ctx.attr.cross_cpu or get_cpu_value(repository_ctx)
exec_constraints, target_constraints = ensure_constraints(repository_ctx)

repository_ctx.file(
Expand Down Expand Up @@ -287,6 +289,7 @@ _nixpkgs_cc_toolchain = repository_rule(
"cc_toolchain_config": attr.string(),
"exec_constraints": attr.string_list(),
"target_constraints": attr.string_list(),
"cross_cpu": attr.string(),
},
)

Expand All @@ -304,7 +307,8 @@ def nixpkgs_cc_configure(
exec_constraints = None,
target_constraints = None,
register = True,
cc_lang = "c++"):
cc_lang = "c++",
cross_cpu = ""):
"""Use a CC toolchain from Nixpkgs. No-op if not a nix-based platform.

By default, Bazel auto-configures a CC toolchain from commands (e.g.
Expand Down Expand Up @@ -375,8 +379,8 @@ def nixpkgs_cc_configure(
target_constraints: Constraints for the target platform.
register: bool, enabled by default, Whether to register (with `register_toolchains`) the generated toolchain and install it as the default cc_toolchain.
cc_lang: string, `"c++"` by default. Used to populate `CXX_FLAG` so the compiler is called in C++ mode. Can be set to `"none"` together with appropriate `copts` in the `cc_library` call: see above.
cross_cpu: string, `""` by default. Used if you want to add a cross compilation C/C++ toolchain. Set this to the CPU architecture for the target CPU. For example x86_64, would be k8.
"""

nixopts = list(nixopts)
nix_file_deps = list(nix_file_deps)

Expand Down Expand Up @@ -447,6 +451,7 @@ def nixpkgs_cc_configure(
name = "{}".format(name),
cc_toolchain_info = "@{}_info//:CC_TOOLCHAIN_INFO".format(name),
fail_not_supported = fail_not_supported,
cross_cpu = cross_cpu,
)

# Generate the `cc_toolchain` workspace.
Expand All @@ -457,6 +462,7 @@ def nixpkgs_cc_configure(
cc_toolchain_config = name,
exec_constraints = exec_constraints,
target_constraints = target_constraints,
cross_cpu = cross_cpu,
)

if register:
Expand Down