1

I can't seem to find much about this, so maybe it isn't possible... or I am searching with the wrong keywords.

Anyway, I have a directory of source. Let's say that there is a folder within Source called Tests and another called Products. I have the usual hierarchy of CMakeLists.txt files so that I can build all the source in each directory and subdirectory, etc... What I want to know is if it is possible to pass a command line argument with cmake so that it will only build the tests folder or the source folder.

e.g. something like cmake TEST ..

Thanks

2 Answers 2

3

Of course you can use flags.

if(TEST) include needed folders else() do something else endif() 

And call cmake like this:

cmake -DTEST=1 .. 
Sign up to request clarification or add additional context in comments.

Comments

2

You can create them as different targets. CMake will configure all of them, but then you can run make test or make product. An example would be:

project(myProject) add_subdirectory(tests) add_subdirectory(products) add_executable(test EXCLUDE_FROM_ALL ${TEST_SRC_FILES}) add_executable(product EXCLUDE_FROM_ALL ${PRODUCT_SRC_FILES}) 

In each subdirectory you would have a CMakeLists.txt that builds the source file variables. The EXCLUDE_FROM_ALL means that just typing make won't build either tests or products, but you can decide if you want that to happen or not.

To build, you can do make test or make product and it will build only that target (and any dependencies).

2 Comments

I have a question. How does this change if the test and products subdirectories have more than just one project in them? i.e. tests might have test1 and test2 where they each generate an executable.
Then you just need to have more add_executable lines, one for each executable. So you would defined TEST1_SRC_FILES, TEST2_SRC_FILES, etc. in the subdirectories. The advantage of this approach over the other is that everything is configured and ready to go, so you can build/run tests and then build your final code also if it all worked, without having to re-run CMake.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.