1

I have a CMake file with two targets (say, target1 and target2) defined via "add_custom_target" at the top level of my source directory. I have some external projects in lower level directory. In the extneral project, a "TEST_COMMAND ${Test_Command_Variable}" is defined as part of an ExternalProject_Add().

I would like to change that vaiarble ${Test_Command_Variable} depending on whether I am using custom target1 or target2. Currently, ${Test_Command_Variable} is defined at the top level CMakeLists.txt before either custom targets are defined. I simply want to vary that variable depending on whether target1 or target2 is called. Is there any way to redefine that variable? Maybe do a conditional if statement dependent on whether target1 or target 2 is chosen (this seems like a trivial thing, but I can't find the way to access the "name" of the custom target!).

To clarify: I have two collections of tests. I want to type "make target1" and it runs my first collection of tests. I want to type "make target2" and it tests my second collection of tests. The problem is I also have an external project, where some of the tests are there. The external projects have a TEST_COMMAND(test_command_variable) that does not differentiate between target1 tests and target2 tests. I would like to be able to change that variable depending on whether I run "make target1" or "make target2".

user35959
  • 359
  • 2
  • 13
  • 1
    What are you planning to do, with the redefined variable? – Joel Jul 22 '15 at 00:04
  • 1
    Can you please add some sample code from your `CMakeLists.txt`? If I understand this correctly you are generating a makefile environment with CMake, want to call `make target1` and - because the external project (testing `target1`?) has a dependency to `target1` - it should be executed with your target's name in the command line. If I'm right you should not use `ExternalProject_Add()` but `add_test(NAME target1 COMMAND TEST_COMMAND ${Target1Vars})` and use `ctest --build-and-test --build-target target1` (see also [CMake/Testing With CTest](http://www.cmake.org/Wiki/CMake/Testing_With_CTest)) – Florian Jul 22 '15 at 10:44
  • The redefined variable is being passed into a TEST_COMMAND( my_variable_here) . This TEST_COMMAND spot handles the testing for an external project. The problem is, I sometimes want to test a certain collection of tests (target1 ) and another collection of tests (target2). I toggled the tests to be different with the add_test() command where I added a CONFIGURATIONS target1 or CONFIGURATIONS target2 in the add_test. The hope is that running make target1 will define a variable and pass it onto the external project TEST_COMMAND(my_variable_here) which will then call the right subsets of tests. – user35959 Jul 22 '15 at 18:56

1 Answers1

0

Well, I got it working finally. I did something completely different to meet my objective. I abandoned the goal of trying to toggle variables depending on the target. To be clear, here was my objective:

"make target1" runs only tests in Group 1.

"make target2" runs only tests in Group 2.

I configured each of the tests as per as per this question I asked earlier. My problem was that I had external projects I was trying to pass that variable down so it would toggle Group1 or Group 2 tests depending on the configuration defined at the top level. I knew passing ctest -C group1_configuration flag (or group2_configuration flag for group2 tests)would work, but I couldn't seem to pass down to my external projects. Fortunately, my solution was "simple".

I wrote a shell script that would execute the correct ctest ... -C group1_configuration (or ctest ... -C group2_configuration) and called it test_script1.sh and test_script2.sh. These scripts would move through the build directory and call the aforementioned ctest command in each of the external projects build directories. Then, in my custom target's I defined in the CMakeLists.txt I have it call the shell script and each custom target executes the right shell script, which move through the build directories calling the correct ctest command with the correct configuration flag in each project and external project.

Community
  • 1
  • 1
user35959
  • 359
  • 2
  • 13
  • Glad to hear it worked. So your are doing something similar to [How to run ctest after building my project with cmake](http://stackoverflow.com/questions/15115075/how-to-run-ctest-after-building-my-project-with-cmake)? Could you please add samples of your CMakeLists.txt and shell scripts? It would be interesting to see your solutions code. And I personally think you won't need an external script, because the `COMMAND`s of a custom target could handle everything. – Florian Jul 23 '15 at 07:48