Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bazel: Compile a single file without linking

Tags:

c++

bazel

Question

In ninja, I can compile a single C++ file by running ninja path/to/my/object.file.o.

Is there a way to achieve the same in bazel?

Use case / Background

During refactoring, in particular when changing interfaces in .hpp files, I usually want to focus on one single complex user of the interface first. I want to iterate on that one user until my refactoring works as expected on complex_user.cpp and I am happy with the new interfaces. Only afterwards, I want to adjust all other users. I hence want to get the compiler errors / warnings only from my complex_user.cpp file while ignoring all other places where .hpp might be included

like image 838
Vogelsgesang Avatar asked Oct 24 '25 02:10

Vogelsgesang


2 Answers

Try --save_temps. bazel build --save_temps //my:library will give you the .o, .s, and similar files for only the targets listed on the command line.

--compile_one_dependency is designed for a similar use case, if you want to specify the target to build by the .cpp file instead of specifying a particular cc_library.

like image 96
Brian Silverman Avatar answered Oct 25 '25 16:10

Brian Silverman


You need to implement a custom-made rule cc_object_file. Since the Bazel cc_rules are open source you can use this as a starting point.

like image 33
Vertexwahn Avatar answered Oct 25 '25 16:10

Vertexwahn