-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Feature request: more powerful way to set C/C++ compile/link flags #5198
Comments
Hi @rongjiecomputer, somehow this issue fell of my radar, and I'm on training till Thursday. Let me come back to you on Friday. |
@mhlopko ping? |
Let me get one my assumptions out of the way. I think the crosstool is a good abstraction, although not super user-friendly (yet :) In my ideal world, users don't have to think about the compiler flags (or even the compiler used, or even the platform used, to some extent). Specifying the flags using I fully agree that the legacy crosstool fields ( Example: toolchain author creates a feature named
You can also enable a feature for the entire build, disable features conditionally and some other things. I don't want to dive into that here in this issue, we are in the process of writing docs for all of this with @spomorski. Currently one thing that cannot be done is to enable a certain feature for all transitive dependencies of a particular target. We will work with @gregestren on this eventually, but we are not at the moment. I agree that changing the crosstool is hard, and we're trying to make it simpler. Ideally there are way less crosstools than there are projects, and projects don't have to duplicate all the flags over and over, and instead they can reuse features. Issue tracking the migration in bazel will be #5187. Currently this effort is blocked by #4571 which is seeing good progress. I assume this will raise many questions, please don't hesitate to ask/challenge me :) |
This seems to solve part of my problem. Can it work backward (i.e. a target can force everyone that depends on it to turn on/off certain feature, such
For me, Crosstool/Toolchain author's responsibility is only to specify the least flags needed to use a toolchain ("least" is the keyword here). Project-specific flags should not appear in Crosstool. Effect of my assumption:
Crosstool is useful, but as far as I know, only one Crosstool file can be used in one build. If I have one project with 5 dependencies, all with one custom Crosstool just to set compiler/linker flags for its project, then which one should user chooses to use for the entire build?
I don't think this is possible in the world of C and C++. GCC, Clang and MSVC all have thousands of warning and optimization flags, no one can abstract that away. You can abstract away more general things such as
My point is Crosstool should not be the one to force certain flags down these projects' throat. Unless in Bazel's ideal world, each project should have its own Crosstool. Note that in the above examples, those flags need to be set for all targets of a project or you might get some obscure compile/runtime errors. I can sort of guess how Google uses Crosstool internally by looking at how Chromium uses GN. Chromium's
Some other comments about Crosstool.
I agree on this too. I have written a working
That is also my dream. I wish I can just
I have been tracking some Github issues about C++ Crosstool too. I have read Crosstool in Skylark since it was published for review, but I don't fully understand what I read (not much example and have too much reference to Bazel's Java implementation). I think it is already partially implemented? I can see I am willing to help with the Windows part when it is ready for external contributors to contribute. Some more complaints about CrosstoolWhen can Bazel finally decouple operating system from Bazel should be able to figure out cpu and os easily. Compiler can be selected based on Thanks for reading till the end. |
There are 2 parallel efforts. One is changing crosstool format from text protobuf to Skylark code. That what Crosstool in Skylark is about. Second it making it easier to write cc_configure which is an autotools-like script that inspects the environment and autogenerates the crosstool. We are migrating away from --cpu and --compiler options towards platforms. It's not only about figuring it out (bazel already does that), but sometimes you need to cross compile, sometimes you need to cross compile on a different machine, etc. But yes, we're working on it :) Thanks for taking the time to comment! |
What is left on cc_configure to allow it to used for this? I'd love to disable exceptions, disable rtti and compile with C++17, but maintaining a custom crosstool seems way too painful. |
Feel free to comment on #6926, which is tracking the effort to make cc_configure better. I'll close this issue since I don't know what the action item here is. Please complain if I'm wrong. |
I have read through all the comments in this issue, but it seems like none of these solve the problem raised in the original feature request. What if the project:
What is the suggested way to do this, except repeating the same And, what's wrong with having a |
The problem of having Can you elaborate why having a package-local or project-local macro doesn't work? |
Thanks for your reply, that sounds very reasonable. Project-local macros work okay for this, it's just a little bit hard to setup and enforce, I agree it's not a problem of Bazel itself and is rather minor. |
Problem
Currently, Bazel does not have a way to set C/C++ compile/link flags in
BUILD/.bzl
files to all targets other than manually addingcopts/linkotps
manually to allcc_*
rules.Not being able to set
copts/linkopts
to all targets has caused a lot of problems in Tensorflow (e.g. compile error due to some targets not getting-DNOGDI
flag in MSVC build). Other Google projects such as Protobuf and gRPC are also affected, but less severe than Tensorflow.As far as I know, Abseil C++ is the only Google project that has the discipline to set copts to every cc_* with ABSL_DEFAULT_COPTS/ABSL_TEST_COPTS from copts.bzl manually, and even then accident can happen (uses “-fexceptions` directly instead of ABSL_EXCEPTIONS_FLAG, which break MSVC build).
Existing workarounds
--copt
in.bazelrc
to set global compile flags.BUILD
and.bzl
toconfigure.py
. Need to implement compiler/platform detection yourself.CROSSTOOL
.CROSSTOOL
which is painful.cc_library
llvm_cc_library
,tf_cc_library
..bazelrc
andCROSSTOOL
workarounds as above, which in my opinion are even worse.llvm_cc_library
macro).What we need
A way to set compile/link flags to targets with custom visibility (current package only, current package with subpackages or whole
@project//*
).Internal version of Blaze seems to have
default_copts
parameter inpackage
rule.What CMake does
In CMake, you can use
add_compile_options
andadd_definitions
to add compile/define flags to all targets under this current directory and sub-directories. (You can also directly add/replace/delete flags toCMAKE_C_FLAGS
andCMAKE_CXX_FLAGS
, but that will affect all directories which is very intrusive).The text was updated successfully, but these errors were encountered: