Home > Enterprise >  Build c/c projects with different configurations one at a time
Build c/c projects with different configurations one at a time

Time:01-04


English is not my mother tongue. Sorry for language mistakes :-)


I have a project written in C that contains a lot of macros to control functionality, each option can be enabled or disabled individually. They share the same core (the core also includes some macros to enable or disable certain features, so ccache doesn't work very well).

Can be compiled by the following script:

cd build
cmake .. ${some options...}
make

It works fine, but I have to run it with different cmake options, to set different macros, and most of the code compiles many times. This is a waste of resources and time!

So, I need a tool like this:

cd build
build-make-file .. --futures="future1,future2,...,futureN"
make

After this script, all code will be compiled into binaries.

For this, I think caching should be a bit more complicated. It can cache AST or machine code etc. In other words, the cache manager should split the code into more parts.

So is there some tool to do this?

CodePudding user response:

Because the question doesn't have a minimal reproducible sample, I will provide you only with a very general answer.

From what I understand the project is using CMake as it's build tool. further more those options are actual variables and for one specific build you need to correctly set let's say N variables, i.e.

cmake -S. -Bbuild -DVar1=ON -DVar2=OFF -DVar3=ON etc....

What you instead want is something like

cmake -S. -Bbuild -DFeature1=TRUE

If that is your case, there is absolutely no reason to introduce another script. That just brings more complications to the whole project.

Instead what you want is to configure the root CMakeLists.txt to already configure these variables for you, e.g.:

#This is the simplest way on how to do this
if(Feature1) 
  SET(VAR1 ON)
  SET(VAR2 OFF)
  SET(VAR3 ON)
  #etc..
endif()

I've seen this too many times, that people add unnecessary .bat .ps .sh files to the mix when a simple adjustment to CMake was all that was needed.

EDIT: If you wish to save time on compilation then you will have to completely restructure your project and make it more modular, i.e. separate it into respective libraries that you then reuse without compilation or you need to rewrite the CMakeLists.txt to link against individual .obj files - there really is no easy, straightforward way to do this and it is more of a design question. And as a side note, most of the time CMake already does this for you. So if it's being completely recompiled you either have a very bad CMakeLists.txt file or those .obj files really need to be changed.

  •  Tags:  
  • c c
  • Related