Skip to content

Commit

Permalink
Merge pull request #557 from tweag/cc_std
Browse files Browse the repository at this point in the history
Make the C++ -std flag configurable
  • Loading branch information
mergify[bot] authored Jun 25, 2024
2 parents 7e1d203 + 55afe3b commit ecd3028
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
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-cross_cpu">cross_cpu</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-cc_std">cc_std</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-cc_std">
<td><code>cc_std</code></td>
<td>

optional.
default is <code>"c++0x"</code>

<p>

string, `"c++0x"` by default. Used to populate `CXX_FLAG` so the compiler uses the given language standard. Can be set to `"none"` together with appropriate `copts` in the `cc_library` call: see above.

</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-cross_cpu">cross_cpu</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-cc_std">cc_std</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-cc_std">
<td><code>cc_std</code></td>
<td>

optional.
default is <code>"c++0x"</code>

<p>

string, `"c++0x"` by default. Used to populate `CXX_FLAG` so the compiler uses the given language standard. Can be set to `"none"` together with appropriate `copts` in the `cc_library` call: see above.

</p>
</td>
</tr>
Expand Down
11 changes: 11 additions & 0 deletions toolchains/cc/cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ def nixpkgs_cc_configure(
target_constraints = None,
register = True,
cc_lang = "c++",
cc_std = "c++0x",
cross_cpu = ""):
"""Use a CC toolchain from Nixpkgs. No-op if not a nix-based platform.
Expand Down Expand Up @@ -382,6 +383,7 @@ 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.
cc_std: string, `"c++0x"` by default. Used to populate `CXX_FLAG` so the compiler uses the given language standard. 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)
Expand Down Expand Up @@ -413,6 +415,9 @@ def nixpkgs_cc_configure(
"--argstr",
"ccLang",
cc_lang,
"--argstr",
"ccStd",
cc_std,
])
elif nix_expr:
nixopts.extend([
Expand All @@ -425,6 +430,9 @@ def nixpkgs_cc_configure(
"--argstr",
"ccLang",
cc_lang,
"--argstr",
"ccStd",
cc_std,
])
else:
nixopts.extend([
Expand All @@ -434,6 +442,9 @@ def nixpkgs_cc_configure(
"--argstr",
"ccLang",
cc_lang,
"--argstr",
"ccStd",
cc_std,
])

# Invoke `cc.nix` which generates `CC_TOOLCHAIN_INFO`.
Expand Down
7 changes: 4 additions & 3 deletions toolchains/cc/cc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
, ccExpr ? null
, ccPkgs ? import <nixpkgs> { config = { }; overlays = [ ]; }
, ccLang ? "c++"
, ccStd ? "c++0x"
}:

let
Expand Down Expand Up @@ -165,10 +166,10 @@ pkgs.runCommand "bazel-${cc.orignalName or cc.name}-toolchain"
include_dirs_for c++
if is_compiler_option_supported -fno-canonical-system-headers; then
include_dirs_for c -fno-canonical-system-headers
include_dirs_for c++ -std=c++0x -fno-canonical-system-headers
include_dirs_for c++ -std=${ccStd} -fno-canonical-system-headers
elif is_compiler_option_supported -no-canonical-prefixes; then
include_dirs_for c -no-canonical-prefixes
include_dirs_for c++ -std=c++0x -no-canonical-prefixes
include_dirs_for c++ -std=${ccStd} -no-canonical-prefixes
fi
} 2>&1 | sort -u))
unset IFS
Expand Down Expand Up @@ -201,7 +202,7 @@ pkgs.runCommand "bazel-${cc.orignalName or cc.name}-toolchain"
)
CXX_FLAGS=(
-x ${ccLang}
-std=c++0x
-std=${ccStd}
)
LINK_FLAGS=(
$(
Expand Down

0 comments on commit ecd3028

Please sign in to comment.