Skip to content

Comments

treewide: define default C&C++ versions for compilers#255979

Draft
tobim wants to merge 1 commit intoNixOS:masterfrom
tobim:define-compiler-defaults
Draft

treewide: define default C&C++ versions for compilers#255979
tobim wants to merge 1 commit intoNixOS:masterfrom
tobim:define-compiler-defaults

Conversation

@tobim
Copy link
Contributor

@tobim tobim commented Sep 18, 2023

Description of changes

The variables defaultCStandard and defaultCXXStandard make the defaults of the -std option known to Nix. This knowledge can be used to decide whether that option must be passed explictly for packages with specific standard requirements.

Usage is demonstrated in protobuf and grpc.

This should not cause any rebuilds.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.11 Release Notes (or backporting 23.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

The variables `defaultCStandard` and `defaultCXXStandard` make the
defaults of the `-std` option known to Nix. This knowledge can be
used to decide whether that option must be passed explictly for
packages with specific standard requirements.

Usage is demonstrated in `protobuf` and `grpc`.
@tobim tobim requested a review from a user September 18, 2023 22:09
Comment on lines +83 to +84
] ++ lib.optionals (stdenv.cc.cc.defaultCxxStandard < 14) [
"-DCMAKE_CXX_STANDARD=14"
Copy link
Member

@AndersonTorres AndersonTorres Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
] ++ lib.optionals (stdenv.cc.cc.defaultCxxStandard < 14) [
"-DCMAKE_CXX_STANDARD=14"
] ++ lib.optionals (stdenv.cc.cc.defaultCxxStandard < 14) [
"-DCMAKE_CXX_STANDARD=14"

What??

How can stdenv.cc.cc.defaultCxxStandard be less than 14 but -DCMAKE_CXX_STANDARD be 14?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is to pass -DCMAKE_CXX_STANDARD only if the compiler in use defaults to an older language version. C++14 is the minimum required to build protobuf.

Copy link
Member

@AndersonTorres AndersonTorres Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I understood. The default C++ standard is not the same as the supported C++ standard.

However it looks too superfluous.
"If the default C++ standard of this compiler is not 14, then force-set the C++ standard to 14".
Why not force-set the C++ standard to 14 and ignore the compiler's default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the default is newer than 14 some of the dependencies are not API compatible (cough, abseil, cough), so that approach doesn't work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should write a comment to explain that so that the next guy who looks as this doesn't go through the same process.
something like

# protobuf needs at least c++14 to be compiled
# if the default is higher don't override it as abseil for example won't be api compatible

passthru = {
inherit libllvm;
isClang = true;
defaultCStandard = 11;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be treated as int?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean?
In case you are thinking of gnu11 vs c++11: That almost never makes a difference. Allowing arithmetic on this value is what makes it useful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understandable.
The problem to me is because e.g C20 comes after C99 however 20<99.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a very astute observation. It seems I'll need to change to interface to a defaultCxxStandardless = YY: ... function. Bummer.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another reason why it's better to detect "is the compiler going to use a standard equal to or later than XYZ" like autoconf does -- no need to encode the entire list of standards in nix, along with their partial order.

@tobim tobim mentioned this pull request Sep 18, 2023
12 tasks
@tobim tobim requested review from Artturin and risicle and removed request for matthewbauer September 18, 2023 22:23
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. labels Sep 18, 2023
@tobim tobim marked this pull request as draft September 19, 2023 06:00
@happysalada
Copy link
Contributor

This should fix some of the troubles we had with onnx, protobuf and abseil.
I'll make the relevant changes once this is merged (we don't need to override the cxx version anymore after this).

@happysalada
Copy link
Contributor

happysalada commented Sep 21, 2023

btw, just for information, but the default c++ version on darwin is 11
cf the errors I was getting trying to compile onnx without specifying anything
onnx/onnx#5579 (comment)
Just to say that the impact on darwin might not be trivial.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no examples of defaultCStandard being used, so it should not be included.

Moreover, the C++ standard in use is not a property of the compiler. It is a property of the compiler as well as the flags passed to the compiler. This includes lots of things, like NIX_CFLAGS_COMPILE and overlays. Putting these values in passthru attributes will lead to incorrect results and headaches in the future.

If a codebase needs a particular version of the C++ standard, it can specify that need with -std=.

In the unlikely case that a codebase needs to know the standard used by the compiler without changing it, that can be detected by invoking the compiler. In fact, autoconf does precisely this. Detecting these values with compilation attempts is the right way to do this. It will pick up any extra flags added by NIX_CFLAGS_COMPILE or even ordinary CFLAGS environment variables.

Detect, don't define.

This whole issue looks like a CMake problem IMHO.

@ghost
Copy link

ghost commented Sep 22, 2023

This whole issue looks like a CMake problem IMHO.

It looks like these project should be using this CMake feature.

Try patching the cmake file; if it works, send it upstream.

@wegank wegank added 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 2.status: merge conflict This PR has merge conflicts with the target branch labels Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants