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

Detect whether compiler is Clang at nix eval time #216

Merged
merged 2 commits into from
Apr 13, 2022

Conversation

YorikSar
Copy link
Member

@YorikSar YorikSar commented Apr 8, 2022

While working on #215 I noticed that we can use cc.isClang to detect if compiler is Clang. It requires us to pull this flag through cc package wrappers. I split this from #215 because this affects our API: compiler packages in Nix would now be required to provide isClang flag that might not be the case for compilers provided by custom Nix expressions.

We can use cc.isClang to detect if compiler is Clang that would simplify
runtime code.
@YorikSar YorikSar requested a review from aherrmann April 8, 2022 15:12
Copy link
Member

@aherrmann aherrmann left a comment

Choose a reason for hiding this comment

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

Nice, thanks!

@aherrmann aherrmann added the merge-queue merge on green CI label Apr 13, 2022
Base automatically changed from yuriy.taraday/macos-rust-example to master April 13, 2022 07:31
@mergify mergify bot merged commit 3331f42 into master Apr 13, 2022
@mergify mergify bot deleted the yuriy.taraday/use-cc.isclang branch April 13, 2022 07:53
@mergify mergify bot removed the merge-queue merge on green CI label Apr 13, 2022
cbley-da added a commit to digital-asset/daml that referenced this pull request May 6, 2022
In rules_nixpkgs, this attribute is now used to determine whether the compiler is clang, see [#216].

[#216]: tweag/rules_nixpkgs#216
cbley-da added a commit to digital-asset/daml that referenced this pull request May 6, 2022
In rules_nixpkgs, this attribute is now used to determine whether the compiler is clang, see [#216].

[#216]: tweag/rules_nixpkgs#216

CHANGELOG_BEGIN
CHANGELOG_END
cbley-da added a commit to digital-asset/daml that referenced this pull request May 6, 2022
In rules_nixpkgs, this attribute is now used to determine whether the compiler is clang, see [#216].

[#216]: tweag/rules_nixpkgs#216

CHANGELOG_BEGIN
CHANGELOG_END
cbley-da added a commit to digital-asset/daml that referenced this pull request May 10, 2022
…3798)

* Update `rules_nixpgks` to HEAD

Since [rules_nixpkgs#191] has been merged, we can drop the `rules-nixpkgs-arm.patch` from rules_nixpkgs.

Also, rules_nixpkgs has been split into several components which need to be
added explicitly in `deps.bzl`, see [#182].

[#191]: tweag/rules_nixpkgs#191
[#182]: tweag/rules_nixpkgs#182

* Adapt `compatibility/deps.bzl`

* Update platforms repository to version 0.0.4

It has been updated in rules_nixpkgs, so this just follows suite.

* Pass through `isClang` attribute for the cc-toolchain

In rules_nixpkgs, this attribute is now used to determine whether the compiler is clang, see [#216].

[#216]: tweag/rules_nixpkgs#216

CHANGELOG_BEGIN
CHANGELOG_END
@dmadisetti
Copy link
Contributor

dmadisetti commented Jun 17, 2022

This breaks for me with a custom toolchain? I think we should have a fallback mechanism, or a required module for custom toolchains (to allow for attribute defaults)

Cannot build Nix attribute ''.
Command: [/run/current-system/sw/bin/nix-build, /home/dylan/.cache/bazel/_bazel_dylan/8bbdcf70ee87482f767b06aac75f66d1/external/nixpkgs_config_cc_info/external/rules_nixpkgs_cc/cc.nix, "-A", "", "--out-link", "bazel-support/nix-out-link", "--argstr", "ccType", "ccTypeExpression", "--arg", "ccExpr", "import ./external/toolchain.nix", "-I", "nixpkgs=/home/dylan/.cache/bazel/_bazel_dylan/8bbdcf70ee87482f767b06aac75f66d1/external/nixpkgs/nixpkgs"]
Return code: 1
Error output:
error: attribute 'isClang' missing

       at /home/dylan/.cache/bazel/_bazel_dylan/8bbdcf70ee87482f767b06aac75f66d1/external/nixpkgs_config_cc_info/external/rules_nixpkgs_cc/cc.nix:282:12:

          281|     IS_CLANG=(
          282|       ${if cc.isClang then "True" else "False"}
             |            ^
          283|     )
(use '--show-trace' to show detailed location information)

Maybe ${if cc ? isClang then if cc.isClang then "True" else "False" else "$(fallback command)"}

@aherrmann
Copy link
Member

@dmadisetti Thanks for raising this.

This breaks for me with a custom toolchain?

Would it be feasible to extend the custom toolchain such that cc.isClang is defined?

Otherwise, yes, a fallback branch seems like a reasonable option.

@dmadisetti
Copy link
Contributor

@aherrmann it is possible to define cc.isClang on the toolchain, but the error is not immediately obvious. I think a little defensive programming is the right option here.

I think the nicest option would be to create toolchain modules (I think one can call pkg.lib.evalModules with an options definition and the custom expr?). This is self documenting and can easily provide defaults + more options (like cxx_opts #218). The downside is that it may take a bit of work, and for consistency, should be replicated for the other toolchains.

Failing that, the branch should be fine! I've been running a patch of rules for awhile, I just wanted to raise my pain points upstream.

Let me know if we should move this concern to its own issue

@aherrmann
Copy link
Member

@aherrmann it is possible to define cc.isClang on the toolchain, but the error is not immediately obvious. I think a little defensive programming is the right option here.

Perhaps improving the error message is the right path then. I think this could also be achieved with a branch like you suggest: If the field is missing, the error could instruct the user to provide it.

Thinking a bit more about it, I wonder if this issue is related to what @avdv encountered in tweag/rules_haskell#1750 (comment).

@YorikSar
Copy link
Member Author

I would recommend to not fall back to assuming that it's not Clang. When I ran into this issue, it was not trivial to debug the error. I think recommending people to add passthru = { inherit (cc) isClang; } or something similar if it's missing is the way to go. By the way, wrapCCWith is already doing this.

@avdv
Copy link
Member

avdv commented Jun 24, 2022

I would recommend to not fall back to assuming that it's not Clang.

AFAIU, the idea was not to use a default value, but to fall back to the old behavior of calling cc -v | grep clang in case isClang is not available on the toolchain.

We could even print a deprecation warning in this case.

Also, I like the idea about the module! Currently you need to be aware of the current expectations and that is not very user friendly.

@aherrmann
Copy link
Member

AFAIU, the idea was not to use a default value, but to fall back to the old behavior of calling cc -v | grep clang in case isClang is not available on the toolchain.

Yes, that was also my understanding. This sounds far less dangerous than a hard-coded fall-back. @YorikSar were there any issues with that approach that #216 fixed?

Also, I like the idea about the module! Currently you need to be aware of the current expectations and that is not very user friendly.

To be sure I'm on the same page. This is talking about NixOS modules, correct? I'm not quite sure I understand how they fit here. Could you perhaps sketch out what this would look like?

@YorikSar
Copy link
Member Author

AFAIU, the idea was not to use a default value, but to fall back to the old behavior of calling cc -v | grep clang in case isClang is not available on the toolchain.

Yes, that was also my understanding. This sounds far less dangerous than a hard-coded fall-back. @YorikSar were there any issues with that approach that #216 fixed?

As far as I remember, cc -v approach just didn't work, and we ended up with IS_CLANG=False which lead to wrong arguments being passed to the compiler. I think, cc -v was just failing or something like that.

@aherrmann
Copy link
Member

I think, cc -v was just failing or something like that.

Oh, I see. I think in this case this should be fixed by the switch to using nix-support/cc-flags. However, it shows that cc -v | grep clang is a little too naive. It should be able to distinguish three states: Is clang, is not clang, is broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants