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

-fdiagnostics-color=always can sometimes be added when it isn't supported #141

Closed
DavisVaughan opened this issue Aug 24, 2022 · 7 comments · Fixed by #148
Closed

-fdiagnostics-color=always can sometimes be added when it isn't supported #141

DavisVaughan opened this issue Aug 24, 2022 · 7 comments · Fixed by #148
Labels
feature a feature request or enhancement

Comments

@DavisVaughan
Copy link
Member

DavisVaughan commented Aug 24, 2022

When running pkgbuild::build(), if your C compiler supports this flag then it will get unconditionally added to an internal Makevars that it used while compiling the package. This can be problematic if your C++ compiler doesn't support this flag.

This situation arose when testing cpp11. We have one GHA workflow that tests on Ubuntu against an old gcc version, in particular, gcc 4.8. Note that support for this flag was added in gcc 4.9. The workflow was setting:

echo $'CXX1X=g++-4.8\nCXX11=g++-4.8' >> ~/.R/Makevars

which meant the C++ compiler was using gcc 4.8, but the C compiler was still using the default, which was a much newer version.

When the package was being built, the FAQ vignette which had {cpp11} knitr chunks in it failed to compile those chunks, because it tried to do so with the -fdiagnostics-color=always flag set, which wasn't recognized on this old version of gcc.

This prompted me to patch this in cpp11 by adding CC=gcc-4.8 to the Makevars as well, so it also looks like we are using an old C compiler r-lib/cpp11#279 (comment), but in theory I wouldn't have to do that.

build() sets the compiler flags here:

withr::local_makevars(compiler_flags(debug = FALSE), .assignment = "+=")

And here is where compiler_flags() adds it

if (crayon::has_color() && has_compiler_colored_diagnostics()) {
flags <- c(
"CFLAGS", "CXXFLAGS", "CXX11FLAGS",
"CXX14FLAGS", "CXX17FLAGS", "CXX20FLAGS"
)
res[flags] <- paste(res[flags], "-fdiagnostics-color=always")
}
res
}

has_compiler_colored_diagnostics() returns TRUE if the C file that is compiled by has_compiler() compiles successfully while CFLAGS = "-fdiagnostics-color=always" is set

res <- withr::with_makevars(c(CFLAGS = "-fdiagnostics-color=always"), has_compiler())

I wonder if it should only return TRUE if both a C and C++ file can successfully be compiled with color turned on? But I guess we also have to worry about exactly which version of C++ we are testing with (CXX11 vs CXX14, etc), so that makes this a little tough to check ahead of time.

@gaborcsardi
Copy link
Member

I wonder if it should only return TRUE if both a C and C++ file can successfully be compiled with color turned on? But I guess we also have to worry about exactly which version of C++ we are testing with (CXX11 vs CXX14, etc), so that makes this a little tough to check ahead of time.

Yeah, we should do this. I would not worry too much about CXX11 etc. I think it would be pretty weird if CXX supports this flag and the other CXXxx compilers don't.

@DavisVaughan
Copy link
Member Author

Would that have been the case above though? i.e. where we set

echo $'CXX1X=g++-4.8\nCXX11=g++-4.8' >> ~/.R/Makevars

But we didn't set CXX (which I'm assuming defaulted to a much newer version?)

@gaborcsardi
Copy link
Member

Why don't you set CXX? I get it that it is not used by the package in this case, but I would guess that most people use the same compiler for everything, no?

@DavisVaughan
Copy link
Member Author

I assume they mainly targeted CXX11 because cpp11 has SystemRequirements: C++11? I'm not sure why they didn't just use CXX instead if that would have also worked

@DavisVaughan
Copy link
Member Author

Some notes here r-lib/cpp11#78 (comment)

@gaborcsardi
Copy link
Member

Anyway, we can check CXX11 as well. Or check what the package uses.

@gaborcsardi gaborcsardi added the feature a feature request or enhancement label Nov 22, 2022
@gaborcsardi
Copy link
Member

It is not trivial to check for each CXX* compiler separately and it is also quite slow. So we are not going to do that now.

We can have an env var to opt out, that is probably enough to work around cases where a mixture of compiler versions is used.

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

Successfully merging a pull request may close this issue.

2 participants