Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "-c opt" and "--copt=-O3" in Bazel build (or GCC)

I'm learning GCC and Bazel. I want to enable all the optimization for Bazel to build a project which requires the best performance.

Then I found -c opt which means to set the compilation mode to optimized without debug information.

And --copt=-O3 means set the optimization level to the third one. There are -O2, -Os, etc.

I'm confused with these two options.

  1. What is the difference between -c opt and --copt=-O3?
  2. Will they trigger each other. So I only need to write one of them with bazel build?
like image 601
Yu Zhang Avatar asked May 18 '18 14:05

Yu Zhang


People also ask

What does-C opt-Copt=-O3 mean in Bazel?

I want to enable all the optimization for Bazel to build a project which requires the best performance. Then I found -c opt which means to set the compilation mode to optimized without debug information. And - -copt=-O3 means set the optimization level to the third one. There are -O2, -Os, etc. I'm confused with these two options.

What does the-C opt flag do in Bazel?

The -c opt flag is for telling Bazel to build with optimization settings enabled and no debug information. Like you mentioned --compilation_mode opt. This is related to the flags used to compile any code. The --config=opt is telling Bazel, to look in the .bazelrc file during compilation and read any settings that match the opt configuration.

What is the difference between--config and-C in Bazel?

But --config is a flexible tool to choose Bazel settings from a .bazelrc file. -c is really just for building code with optimizations. --config set a configuration that expands in a set of flag defined in a .rc file. E.g. if the rc file contains build:opt -c opt, setting --config opt on the command line will expand to -c opt.

What is the difference between bazelrc and opt configuration in TensorFlow?

After you run your configure script with tensorflow, you should have a .bazelrc file sitting in the root of your workspace which defines settings for multiple configurations. For the opt configuration, it adds the extra -march-native for compilation. So it is a bit coincidental that they are named the same way.


1 Answers

--copt is for passing args to to the compiler.

-c is a short form of --compilation-mode. Its effect is described in the user-manual:

  • It sets compiler options (e.g. -c opt implies -O2 -DNDEBUG)
  • There are different output directories per compilation mode, so you can switch between debug and optimized builds without full recompilation.

So usually, -c optis enough. If you want the behaviour of -c opt but with a different optimization level, you combine the two options like in -c opt --copt=-O3 and the compiler will get both options -O2 and -O3, but the last one will win.

And watch out, there is a third similar option:

--config=configname is for selecting a configuration. You can have a .bazelrc which defines default options. Some of them are not always active, but some only if you activate them by the --config=configname command line option. Now opt is a popular configname, so if you have a .bazelrc that contains

build:opt --copt=-O3

then bazel build --config=opt has the same effect as bazel build --copt=-O3

like image 177
Till Brychcy Avatar answered Sep 20 '22 17:09

Till Brychcy