Search code examples
windowslistmakefilecmakecmake-language

How do I make a list in CMake with the semicolon value?


I think it should be super trivial, but seems like it's not supported... Is it even possible in CMake to have one value of a list contain a semicolon?

The reason is super simple - because I'm running on Windows, and on Windows the semicolon is the separator between two or more folders in one environment variable (i.e. PATH).

    list(
      APPEND
      MY_TEST_ENVIRONMENT
      "MY_FLAG=1"
    )

    # <...>

    list(
      APPEND
      MY_TEST_ENVIRONMENT
      "PATH=first_folder_path;second_folder_path"
    #                        ^--- here is the problem
    )

    # <...>


   set_property(TEST MyTests PROPERTY ENVIRONMENT ${MY_TEST_ENVIRONMENT})

I tried removing and adding double quotes, I tried escaping \;, I tried to add same env variable two times - but none of those work!


Solution

  • You are on the right track by escaping the semicolon with a backslash. However, when setting the list in the ENVIRONMENT test property, this is not the last place the list will be used (and interpreted by CMake). The MY_TEST_ENVIRONMENT list is then used to populate CTestTestfile.cmake, which is later read by CTest to setup your test environment.

    In short, you need more escape characters to propagate the semicolons through to the test environment. Specifically, use a double backslash \\ to escape an extra backslash into the list, along with the \; escaped semicolon. Here is CMake's escape characters documentation for reference. In total, three backslashes should work:

    list(
      APPEND
      MY_TEST_ENVIRONMENT
      "MY_FLAG=1"
    )
    
    # <...>
    
    list(
      APPEND
      MY_TEST_ENVIRONMENT
      "PATH=first_folder_path\\\;second_folder_path"
    #                         ^--- Use three backslashes here
    )
    
    # <...>
    
    set_property(TEST MyTests PROPERTY ENVIRONMENT ${MY_TEST_ENVIRONMENT})