# CMake variables in CTest?

**URL:** <https://discourse.cmake.org/t/cmake-variables-in-ctest/1244>\
**Category:** Code\
**Created:** [May 20, 2020, 1:12pm UTC](https://discourse.cmake.org/t/cmake-variables-in-ctest/1244 "2020-05-20T13:12:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbahadir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dbahadir/32/197_2.png) [@dbahadir](https://discourse.cmake.org/u/dbahadir)\
**Post date:** [May 20, 2020, 1:12pm UTC](https://discourse.cmake.org/t/cmake-variables-in-ctest/1244/1 "2020-05-20T13:12:23Z")

</div>

I am currently playing around with generating CTest scripts and running these using `ctest`.

In one of those scripts I wanted to create a directory and thought I would be nice and use a platform agnostic approach:

```cmake
execute_process( COMMAND "${CMAKE_COMMAND}" -E make_directory ${new_directory})

```

However, I realized quickly that the variable `CMAKE_COMMAND` is not defined when running `ctest`. The same is true for the variable `CMAKE_CTEST_COMMAND`. (Other variables I did not test.)

Is this a known limitation or an oversight?

And if the reason should be that `ctest` could be run without any `cmake` executable around, wouldn’t it be useful to give `ctest` the `-E` option that `cmake` has and define at least the `CMAKE_CTEST_COMMAND` variable?

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [May 20, 2020, 1:53pm UTC](https://discourse.cmake.org/t/cmake-variables-in-ctest/1244/2 "2020-05-20T13:53:54Z")

</div>

`CMAKE_COMMAND` is indeed defined when running `ctest`. The following CTest dashboard script works for me:

```cmake
message(STATUS "${CMAKE_COMMAND}")

set(CTEST_SOURCE_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
set(CTEST_BINARY_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
ctest_start(Experimental)

```

So your approach should work.

Though, incidentally, if you’re trying to create a directory, you can also do the following:

```cmake
file(MAKE_DIRECTORY ${new_directory})

```

---

<div class="post-metadata">

**Author:** ![dbahadir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dbahadir/32/197_2.png) [@dbahadir](https://discourse.cmake.org/u/dbahadir)\
**Post date:** [May 20, 2020, 4:24pm UTC](https://discourse.cmake.org/t/cmake-variables-in-ctest/1244/3 "2020-05-20T16:24:59Z")

</div>

> [@kyle.edwards](#):
>
> `CMAKE_COMMAND` is indeed defined when running `ctest` . The following CTest dashboard script works for me:
> 
> ```auto
> message(STATUS "${CMAKE_COMMAND}")
> 
> set(CTEST_SOURCE_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
> set(CTEST_BINARY_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
> ctest_start(Experimental)
> 
> ```
> 
> So your approach should work.

Ah, ok, I see what you did.  
You are calling the script with the `ctest` option `-S`. That seems to work.

However, I am just calling it without any option in the directory, where the `CTestTestfile.cmake` file is located. I do not want to call a dashboard script, I just want to run my built tests.

Try the following and you will notice that the variables are not defined:

1. Create `CTestTestfile.cmake` with the following content:

```cmake
message(NOTICE "CMAKE_COMMAND=${CMAKE_COMMAND}")
message(NOTICE "CMAKE_CTEST_COMMAND=${CMAKE_CTEST_COMMAND}")

```

1. Run `ctest` in that directory without any arguments.

The output will look like this:

```auto
Test project <working-dir>
CMAKE_COMMAND=
CMAKE_CTEST_COMMAND=
No tests were found!!!

```

(Interestingly, messages printed to STDOUT as does `message(STATUS ...)` are swallowed.)

> [@kyle.edwards](#):
>
> Though, incidentally, if you’re trying to create a directory, you can also do the following:
> 
> ```auto
> file(MAKE_DIRECTORY ${new_directory})
> 
> ```

Thanks, I forgot about that `file(MAKE_DIRECTORY ...)` command. That helped in my specific case.

Still, I am wondering if the mentioned variables (or even more of the general CMake variables) shouldn’t be defined when running `ctest`, even without any options.

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [May 20, 2020, 5:21pm UTC](https://discourse.cmake.org/t/cmake-variables-in-ctest/1244/4 "2020-05-20T17:21:30Z")

</div>

> [@dbahadir](#):
>
> Try the following and you will notice that the variables are not defined:

Indeed you are correct. That’s rather surprising to me… they probably should be defined, especially since [`TEST_INCLUDE_FILES`](https://cmake.org/cmake/help/latest/prop_dir/TEST_INCLUDE_FILES.html) lets you inject arbitrary code into the `CTestTestfile.cmake` context.
