Search code examples
cmake

Problem escaping semicolon and quotes in cmake


I am trying to set the CMAKE_CROSSCOMPILING_EMULATOR variable in my cmake toolchain file so that it runs wine with the WINEPATH environment set to WINEPATH="/usr/x86_64-w64-mingw32/lib;/usr/lib/gcc/x86_64-w64-mingw32/9.3-posix".

Here is what I tried so far:

set(CMAKE_CROSSCOMPILING_EMULATOR WINEPATH="/usr/x86_64-w64-mingw32/lib;/usr/lib/gcc/x86_64-w64-mingw32/9.3-posix" wine)

produces

 WINEPATH=\"/usr/x86_64-w64-mingw32/lib /usr/lib/gcc/x86_64-w64-mingw32/9.3-posix" wine

It does not work because the semicolon was replaced by a space, and the initial quote is escaped.

I tried to escape the semicolon:

set(CMAKE_CROSSCOMPILING_EMULATOR WINEPATH="/usr/x86_64-w64-mingw32/lib\;/usr/lib/gcc/x86_64-w64-mingw32/9.3-posix" wine)

But that produces the same result. I read that question: How do I correctly pass CMake list (semicolon-sep) of flags to set_target_properties?, and tried this:

set(CMAKE_CROSSCOMPILING_EMULATOR WINEPATH="/usr/x86_64-w64-mingw32/lib\\\;/usr/lib/gcc/x86_64-w64-mingw32/9.3-posix" wine)

It does produce a semicolon, but the whole WINEPATH part of the string is now quoted, which fails to run:

"WINEPATH=\"/usr/x86_64-w64-mingw32/lib;/usr/lib/gcc/x86_64-w64-mingw32/9.3-posix\"" wine

So my question is: how do I set the CMAKE_CROSSCOMPILING_EMULATOR variable so that it produces:

WINEPATH="/usr/x86_64-w64-mingw32/lib;/usr/lib/gcc/x86_64-w64-mingw32/9.3-posix" wine

Solution

  • You need to quote the word, not part of the word.

    WINEPATH= is not a program. env is a program.

    set(CMAKE_CROSSCOMPILING_EMULATOR
       env
       "WINEPATH=/usr/x86_64-w64-mingw32/lib;/usr/lib/gcc/x86_64-w64-mingw32/9.3-posix"
       wine
    )
    

    See https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#unquoted-argument the around sections and the note section.

    A much simpler way is to just create a (jump-) shell script.