Skip to content

cc-wrapper/setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE#191724

Merged
vcunat merged 1 commit intoNixOS:stagingfrom
danielbarter:setup-hook-duplication-fix-master
Oct 5, 2022
Merged

cc-wrapper/setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE#191724
vcunat merged 1 commit intoNixOS:stagingfrom
danielbarter:setup-hook-duplication-fix-master

Conversation

@danielbarter
Copy link
Contributor

@danielbarter danielbarter commented Sep 18, 2022

Description of changes

Currently, nix build environments have duplicate flags in NIX_CFLAGS_COMPILE. For example, consider the following shell.nix:

with (import <nixpkgs> {});

mkShell {
    buildInputs = [
      cmake
      gtest
    ];
}

which gives us

$ echo $NIX_CFLAGS_COMPILE
-frandom-seed=1cxirkw4xs 
-isystem /nix/store/qgifygqcicn2zr9fmaflzqc670d0b4ns-gtest-1.11.0-dev/include 
-isystem /nix/store/qgifygqcicn2zr9fmaflzqc670d0b4ns-gtest-1.11.0-dev/include

It is easy to remove this duplication by making ccWrapper_addCVars idempotent. After this change, we have

$ echo $NIX_CFLAGS_COMPILE
-frandom-seed=1cxirkw4xs 
-isystem /nix/store/qgifygqcicn2zr9fmaflzqc670d0b4ns-gtest-1.11.0-dev/include

This is mostly a cosmetic change, and will trigger a rebuild of the entire universe. I did so for the standard environment today and everything works fine. It has been bugging me since I started doing C++ dev on nix, especially for cmake projects with a lot of dependencies where I use NIX_CFLAGS_COMPILE to generate a compile_commands.json

Things done

Make ccWrapper_addCVars idempotent.

  • 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/)
  • 22.11 Release Notes (or backporting 22.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
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@winterqt
Copy link
Member

This needs to go to staging, please rebase.

@danielbarter danielbarter changed the base branch from master to staging September 18, 2022 01:39
@danielbarter danielbarter changed the title remove duplicate flags in NIX_CFLAGS_COMPILE setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE Sep 18, 2022
Copy link
Member

@winterqt winterqt left a comment

Choose a reason for hiding this comment

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

No opinion on the changes (sorry, not my area of expertise), but the commit message should be made a bit more clear (we have a lot of setup hooks):

- setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE
+ cc-wrapper: don't add duplicate flags to NIX_CFLAGS_COMPILE

(I reworded it a bit, feel free to play around with it, though.)

@ofborg ofborg bot added 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. labels Sep 18, 2022
@danielbarter danielbarter changed the title setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE cc-wrapper setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE Sep 18, 2022
@danielbarter danielbarter changed the title cc-wrapper setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE cc-wrapper/setup-hook.sh: remove duplicate flags in NIX_CFLAGS_COMPILE Sep 18, 2022
@danielbarter danielbarter force-pushed the setup-hook-duplication-fix-master branch from 4b20142 to 9402e62 Compare September 18, 2022 18:20
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-get-c-headers-included-in-nix-cflags-compile/21638/2

@bjornfor
Copy link
Contributor

For later: I wonder if there's a more central place to remove duplicates, and whether we should do it there? E.g., what about -L flags? And all the kinds of env vars that get set up by stdenv hooks (e.g. $PATH).

@danielbarter danielbarter force-pushed the setup-hook-duplication-fix-master branch 2 times, most recently from e53c0f0 to c27315b Compare September 19, 2022 20:09
Copy link
Member

@vcunat vcunat left a comment

Choose a reason for hiding this comment

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

Sounds OK, but I haven't tested anything.

Nit suggestions:

  • -F for grep to get more precision of matching (and perhaps faster)
  • -q for grep instead of redirection, maybe?

@bobby285271 bobby285271 added the 12.approvals: 1 This PR was reviewed and approved by one person. label Sep 28, 2022
@danielbarter danielbarter force-pushed the setup-hook-duplication-fix-master branch from c27315b to 529c715 Compare September 28, 2022 15:45
@danielbarter
Copy link
Contributor Author

@vcunat: Good suggestions! Have updated. Rebuilding now to make sure everything still works

@danielbarter
Copy link
Contributor Author

everything looks good.

@vcunat
Copy link
Member

vcunat commented Sep 28, 2022

Why do you grep for "$1" and not the full path that's being added? For example, I wonder if both include and Library/Frameworks could be there at once. If so, this change would drop the latter.

@danielbarter
Copy link
Contributor Author

danielbarter commented Sep 28, 2022

You are totally right! Thanks for catching that @vcunat

@danielbarter danielbarter force-pushed the setup-hook-duplication-fix-master branch from 529c715 to 0bea4a1 Compare September 28, 2022 20:25
@bobby285271 bobby285271 removed the 12.approvals: 1 This PR was reviewed and approved by one person. label Sep 28, 2022
@trofi
Copy link
Contributor

trofi commented Oct 27, 2022

I think eduke32 breaks because NIX_CFLAGS_COMPILE already contains manually added -I/...-SDL2-2.24.0-dev/include/SDL2 and grep only searches for -I/...-SDL2-2.24.0-dev/include substring. I think it's a major bug that happens to filter out any attempts of adding -I paths into /include.

@vcunat
Copy link
Member

vcunat commented Oct 27, 2022

This imprecision seems not easy to fix completely, I'm afraid.

@vcunat
Copy link
Member

vcunat commented Oct 27, 2022

Actually yes, we could grep -q -F ":$1/include:" in :$PATH:

trofi added a commit to trofi/nixpkgs that referenced this pull request Oct 27, 2022
In NixOS#197798 (comment)
we noticed that `eduke32` fails to build as:

    source/build/include/sdl_inc.h:17:12: fatal error: SDL2/SDL.h: No such file or directory
       17 | #  include <SDL2/SDL.h>
          |            ^~~~~~~~~~~~

It statred after NixOS#191724 where
`NIX_CFLAGS_COMPILE` entries were cleaned up slightly.

This change works build failure around. But I think it's a good change:
there should be no reason to set SDL2 offset via forced include paths.
@trofi
Copy link
Contributor

trofi commented Oct 27, 2022

Doesn't $PATH only contain /bin paths? I don't see any includes in $ nix develop -f. eduke32; echo $PATH.

@vcunat
Copy link
Member

vcunat commented Oct 27, 2022

Oops... correction: grep -q -F " $1/include " in " $NIX_CFLAGS_COMPILE "

@Ericson2314
Copy link
Member

I had a failure like the eduke32 one.


I don't think this PR was a good idea. The only reason we have multiple entries is because of strictDeps being disabled. It would be better to finally after all these years just enable it everywhere.

@vcunat
Copy link
Member

vcunat commented Oct 27, 2022

Can you expand on how strictDeps is related?

@Ericson2314
Copy link
Member

Ericson2314 commented Oct 27, 2022

strictDeps=0 causes packages to be processed with the "wrong" env hooks (wrong in the cross compilation sense) as a sledgehammer approach to dealing with incorrectly categorized deps. However running the same hooks from multiple compilers can bloat search paths by appending the same new element to them more than once.

@vcunat
Copy link
Member

vcunat commented Oct 27, 2022

From that it doesn't sound to me that strictDeps would eliminate all duplicates, but I don't recall all corners (say recursive propagation of inputs).

@vcunat
Copy link
Member

vcunat commented Oct 28, 2022

Ah, the role_post suffix was ignored 🤦🏽 Perhaps you meant that?

vcunat added a commit that referenced this pull request Oct 28, 2022
Let's redo this later, with more testing and less bugs.
This reverts commits 4f6e998 and d700d8e.
@vcunat
Copy link
Member

vcunat commented Oct 28, 2022

OK, I suppose it will be better to revert until we have a better version of this deduplication.

Any preliminary comments on this approach?

ccWrapper_addCVars () {
    # See ../setup-hooks/role.bash
    local role_post
    getHostRoleEnvHook
    # Avoid adding non-existent directories and duplicates.

    local CFLAGS_varname="NIX_CFLAGS_COMPILE${role_post}"
    if [ -d "$1/include" ] \
            && (! printf %s " ${!CFLAGS_varname} " | grep -q -F " $1/include " ); then
        export ${CFLAGS_varname}+=" -isystem $1/include"
    fi

    if [ -d "$1/Library/Frameworks" ] \
            && (! printf %s " ${!CFLAGS_varname} " | grep -q -F " $1/Library/Frameworks " ); then
        export ${CFLAGS_varname}+=" -iframework $1/Library/Frameworks"
    fi
}
  • I quickly tried <<< but it caused some issues even on linux. I didn't look deeply what was wrong there, as printf sounds nice.
  • I fixed the missing $role_post suffix, but I couldn't avoid some complication of the code.
  • I moved this new condition into the if, because it seemed cleaner.
  • I checked that the original problem with darwin is gone (on x86_64-darwin).

@Ericson2314
Copy link
Member

I guess because we are appending, this is safe with respect to search path order?

If so, the new version is good.

Ericson2314 added a commit to Ericson2314/nixpkgs that referenced this pull request Nov 4, 2022
Always set `SRCTOP`, set it with abs path

llvmPackages: Bump minimum version for FreeBSD

llvmPackages_*, libgcc, compiler_rt: Hack in enough libs that one can compiler C

freebsd.compat: Rename some things to work around cc-wrapper change

0bea4a1 / NixOS#191724 in particular
@danielbarter
Copy link
Contributor Author

danielbarter commented Nov 7, 2022

Sorry, just catching up on this. These deep files are hard, but so interesting to hack on 😅

@vcunat
Copy link
Member

vcunat commented Nov 7, 2022

I was waiting anyway, as it felt better not to hurry with this into 21.11 release and wait for forking it off.

@danielbarter
Copy link
Contributor Author

danielbarter commented Nov 8, 2022

We could just revert if that is what we think is best. Duplicates aren't a huge deal. I thought it would be an easy fix, but clearly there was some subtlety that I missed

@vcunat
Copy link
Member

vcunat commented Nov 8, 2022

Right now it is reverted on all platforms (for a week or two, see above).

@danielbarter
Copy link
Contributor Author

danielbarter commented Nov 8, 2022

sorry, I was unclear. I meant to say that I would be fine with not dealing with the duplicates after 22.11 if it is going to be complicated to get working. If you and Ericson2314 have it sorted now, then everything is good! I need to learn more about cross compiling ....

@vcunat
Copy link
Member

vcunat commented Nov 8, 2022

I'd hope that the suggestion above will work well, but we can only be really sure after it's spent months in nixpkgs master (after it switches to 23.05).

rtimush pushed a commit to rtimush/nixpkgs that referenced this pull request Sep 21, 2023
Always set `SRCTOP`, set it with abs path

llvmPackages: Bump minimum version for FreeBSD

llvmPackages_*, libgcc, compiler_rt: Hack in enough libs that one can compiler C

freebsd.compat: Rename some things to work around cc-wrapper change

0bea4a1 / NixOS#191724 in particular
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants