Skip to content

cc-wrapper: Allow user to override -target for clang#176152

Merged
Ericson2314 merged 3 commits intoNixOS:stagingfrom
mmlb:cc-wrapper-allow-target-override
Jun 15, 2022
Merged

cc-wrapper: Allow user to override -target for clang#176152
Ericson2314 merged 3 commits intoNixOS:stagingfrom
mmlb:cc-wrapper-allow-target-override

Conversation

@mmlb
Copy link
Copy Markdown
Member

@mmlb mmlb commented Jun 3, 2022

Description of changes

Only sets -target in wrapped compiler if none is provided by the user. This is especially helpful for generating bpf objects.

Fixes #176128

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/)
  • 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.

@mmlb mmlb requested a review from Ericson2314 as a code owner June 3, 2022 21:07
@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 3, 2022

ping @Mindavi for review too

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 3, 2022

I couldn't find anything in the repo that sets -target (as expected) which leads me to believe that its relatively safe.

@mmlb mmlb force-pushed the cc-wrapper-allow-target-override branch from 32afeff to c109773 Compare June 3, 2022 21:13
@ofborg ofborg bot added 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin 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: 2501-5000 This PR causes many rebuilds on Linux and should target the staging branches. labels Jun 3, 2022
@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 7, 2022

@06kellyjac give this a try

@Ericson2314
Copy link
Copy Markdown
Member

This needs to point at staging instead.

@Ericson2314
Copy link
Copy Markdown
Member

Also I don't think this is a good fix for #176128, because clang might try to produce a far binary for both arches instead. What you want is logic in cc-wrapper itself to only add a -target ... if the user didn't already pass one themselves.

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 7, 2022

Also I don't think this is a good fix for #176128, because clang might try to produce a far binary for both arches instead.

I find this surprising if true, got any links to documentation? So far everything I've seen to generate fat binaries or bundles is very much opt in with either clang args (clang offload bundler) or different tools (lipo, but I think this is osx only)

What you want is logic in cc-wrapper itself to only add a -target ... if the user didn't already pass one themselves.

Setting aside the fat binaries, I think this makes sense anyway so will look into updating cc-wrapper.

@mmlb mmlb changed the base branch from master to staging June 7, 2022 16:37
@mmlb mmlb force-pushed the cc-wrapper-allow-target-override branch 3 times, most recently from 263f994 to b7ae4b6 Compare June 7, 2022 21:06
@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 7, 2022

Hey @Ericson2314 I've rebased on-top of staging and re-targted the PR accordingly. Also made it so -target should only show up once in the final params just once. This ends up causing a full rebuild of clang which then ripples out, this did not happen with the previous approach, just an fyi.

@mmlb mmlb force-pushed the cc-wrapper-allow-target-override branch from b7ae4b6 to b1d3216 Compare June 7, 2022 21:10
@Mindavi
Copy link
Copy Markdown
Contributor

Mindavi commented Jun 7, 2022

I feel like there was a reason for the if, elif chaining, but maybe it's just how it was written.

Could that be because we're using a very old bash on MacOS and this is used during bootstrap or something? Hopefully someone else can confirm or deny.

@happysalada do you know?

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 7, 2022

I feel like there was a reason for the if, elif chaining, but maybe it's just how it was written.

Could that be because we're using a very old bash on MacOS and this is used during bootstrap or something? Hopefully someone else can confirm or deny.

@happysalada do you know?

I thought it might have been due to sh vs bash, but I verified that dash supports case and so I'd suspect old bash would too.

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 7, 2022

The if/elif chaining makes more sense in the other params handling further down (for paths) because of the extra conditionals. The if/elif I replaced had a few of them too but I think the nested case works just fine for them. I got rid of the && $isCxx = 0 because it doesn't really matter that we're setting isCxx=1 if its already 1 anyway and that way we don't need to nest an if.

@mmlb mmlb force-pushed the cc-wrapper-allow-target-override branch from b1d3216 to 6faae98 Compare June 7, 2022 21:41
@mmlb mmlb changed the title cc-wrapper: Move -target ... from cc-cflags -> cc-cflags-before to allow override cc-wrapper: Allow user to override -target for clang Jun 7, 2022
@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 7, 2022

case is in posix sh (see 2.9.4 in https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09) so should be ok to use for old mac bash.

@ofborg ofborg bot added 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. and removed 10.rebuild-linux: 2501-5000 This PR causes many rebuilds on Linux and should target the staging branches. labels Jun 7, 2022
@Mindavi
Copy link
Copy Markdown
Contributor

Mindavi commented Jun 8, 2022

Thanks for checking! It does look nicer with case IMO, so that's good.

Could you point me to where clang sets the target? I cannot find it in a pinch.

This enables users to make use of clang's multi-platform/target support
without having to go through full cross system setup. This is especially useful
for generating bpf object files, I'm not even usre what would a no-userland
cross compile system tuple even look like to even try going that route.

Fixes NixOS#176128
@mmlb mmlb force-pushed the cc-wrapper-allow-target-override branch from a4a2ab7 to 19b6ccd Compare June 9, 2022 20:57
@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 13, 2022

@Ericson2314 how does this look?

@mmlb mmlb force-pushed the cc-wrapper-allow-target-override branch from 789ba28 to 0b039ef Compare June 13, 2022 18:31
@Ericson2314
Copy link
Copy Markdown
Member

I would tweak the names and make them consistent, but otherwise looks good!

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 13, 2022

I would tweak the names and make them consistent, but otherwise looks good!

Can you elaborate? I copied the naming scheme used in bintools-wrapper:
add-darwin-ldflags-before.sh -> add-clang-cflags-before.sh
add-local-ldflags-before.sh -> add-local-cflags-before.sh

Do you want me to prefix -cflags with cc?

@Mindavi
Copy link
Copy Markdown
Contributor

Mindavi commented Jun 15, 2022

Does this file already exist or is it missing: add-local-cflags-before.sh?

@Ericson2314
Copy link
Copy Markdown
Member

Ericson2314 commented Jun 15, 2022

@mmlb I don't know why bintools wrapper uses two different names either. Do you, @thefloweringash?

@thefloweringash
Copy link
Copy Markdown
Member

@mmlb I don't know why bintools wrapper uses two different names either. Do you, @thefloweringash?

Are we looking at extra "ld" in "add-local-ldflags-before.sh" that's not in "add-flags.sh"? If I was the one that introduced those (and history seems to indicate it was), then it was just an oversight.

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 15, 2022

Does this file already exist or is it missing: add-local-cflags-before.sh?

Does not exist. default.nix will create it for clang, if gcc ever needs a gcc only flag at runtime we could have a add-gcc-local-cflags-before.sh.

@mmlb I don't know why bintools wrapper uses two different names either. Do you, @thefloweringash?

Are we looking at extra "ld" in "add-local-ldflags-before.sh" that's not in "add-flags.sh"? If I was the one that introduced those (and history seems to indicate it was), then it was just an oversight.

Are we talking about the reason for add-local-cflags-before.sh vs cc-cflags-before (and the equivalent ld in bintools)? If so I think it makes sense to have the local in there. add-flags doesn't source the add-cflag.sh it reads it in directly to the variable being assigned. Both uses of local need to be run so that it can interpret the ${params[@]} array.

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 15, 2022

I'm going to modify the commit so the files have cc- in the name. It feels weird compared to all the cc-cflags here.

This way gcc doesn't need to be rebuilt because of clang. This also avoids
rebuilding clang when only the wrapper needs to be tweaked.
@mmlb mmlb force-pushed the cc-wrapper-allow-target-override branch from 0b039ef to 0fdc72b Compare June 15, 2022 15:35
@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 15, 2022

or do you mean I should have named add-local-cc-cflags-before.sh -> local-cc-cflags-before to follow the naming scheme better?

@Ericson2314
Copy link
Copy Markdown
Member

I don't understand why the file in the Nixpkgs src has one name (with .sh) and the file "installed" as part of the built wrapper has a different name (still with .sh). I get it when we strip the .sh on install (e.g. installing something to bin/) but I don't get why for something very internal like this.

I also don't understand why anything is called "local". What does "local" mean here?

Finally, I don't like the use of "clang" because this applies equally well to any compiler that would accept -target. Just as we renamed the variables in bash to get rid of "clang" we should also rename the file to get rid of "clang".

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 15, 2022

ah gotcha. Yeah I think the idea is that there's an "interface" and clang and gcc can both implement it by having a file named local-cc-cflags-before.sh dropped into $out somewhere. I think that makes some sense. The other option would be instead of file names we'd source all files in a dir that match a pattern but that would be up to collation order and would be totally different than what is being done currently, and idk if its even warranted tbh.

I took local as an indicator that file has code to run vs just a list of values to be read, open to w/e name makes sense to show that distinction.

re clang vs more fine grained names. I can rename so its just for target, but some of the other files here are specific to the tool not to the features so I went with that too since only clang does -target and thus yagni.

Sounds like you want me to rename from clang -> target and maybe come up with something else more relevant than local. I'll do the former rn and maybe come up with a better name than local. We can discuss that separately.

@Ericson2314
Copy link
Copy Markdown
Member

OK that makes some sense we can live with this for now.

@Ericson2314 Ericson2314 merged commit e23e71f into NixOS:staging Jun 15, 2022
@Ericson2314
Copy link
Copy Markdown
Member

Ericson2314 commented Jun 15, 2022

(the approval and had some old junk queued up from before accidentally)

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 15, 2022

(the approval and some old junk queued up from before acidently)

what do you mean?

@mmlb mmlb deleted the cc-wrapper-allow-target-override branch June 15, 2022 19:24
@Mindavi
Copy link
Copy Markdown
Contributor

Mindavi commented Jun 18, 2022

John means that he had some comments in an 'open review' and they were submitted when he merged/approved. That wasn't the intention since they were stale.

@Ericson2314
Copy link
Copy Markdown
Member

Yes exactly. Thanks.

@mmlb
Copy link
Copy Markdown
Member Author

mmlb commented Jun 20, 2022

ah, gotcha.

@06kellyjac 06kellyjac mentioned this pull request Jun 28, 2022
13 tasks
06kellyjac added a commit to 06kellyjac/nixpkgs that referenced this pull request Oct 2, 2022
Also simplified the package since after NixOS#176152 the tracee build process can
now pass in a -target of bpf without weird overrides
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.

clang won't build for bpf target

5 participants