CMake escape special character in file name for OS X content copy

Viewed 160

Long time reader of stackoverflow (thank you all!), but this is my first question.

I am trying to create a CMake file for an existing Xcode project. The goal is to create an *.app file(directory seems more appropriate) that includes all the resources that are needed for the application to run. (Or more accurately, the goal is to not use Xcode but use CLion instead, which I already use for the same project on Windows and Linux)

I am running into a problem when CMake generates the code to copy a few files that contain a special character. Cmake runs fine, but when I try to build I get the following error:

====================[ Build | MyAppDebug | Debug ]==========================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/<user>/MyApp/cmake-build-debug --target MyAppDebug -- -j 8
CMakeFiles/MyAppDebug.dir/build.make:4112: *** missing separator.  Stop.
make[2]: *** [CMakeFiles/MyAppDebug.dir/all] Error 2
make[1]: *** [CMakeFiles/MyAppDebug.dir/rule] Error 2
make: *** [MyAppDebug] Error 2

This is in the build.make file around line 4112:

4111.
4112.    ../bin/DebugOsx/MyApp.app/Contents/Resources/my\ resource\ folder/file_name#1.gif: /Users/<user>/resources/my\ resource\ folder/file_name#1.gif
4113.    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Copying OS X content ../bin/DebugOsx/MyApp.app/Contents/Resources/my resource folder/file_name#1.gif"
4114.    $(CMAKE_COMMAND) -E copy "/Users/<user>/resources/my resource folder/file_name#1.gif" "../bin/DebugOsx/MyApp.app/Contents/Resources/my resource folder/file_name#1.gif"
4115.

If I manually escape the # by adding a backslash to the filenames on line 4112, these lines don't give an error when trying to build.

4112.    ../bin/DebugOsx/MyApp.app/Contents/Resources/my\ resource\ folder/file_name\#1.gif: /Users/<user>/resources/my\ resource\ folder/file_name\#1.gif

This is roughly how my CMake file looks: (I actually use a bunch of globbing to get all the files and appropriate folder tree)

set(TESTFILE "/Users/<user>/resources/my resource folder/file_name#1") #Edit: I tried this with and without escaping
set_property(SOURCE ${TESTFILE} PROPERTY MACOSX_PACKAGE_LOCATION "Resources/my resource folder")
add_executable(MyAppDebug MACOSX_BUNDLE ${SOURCE_LIST} ${TESTFILE})

target_link_libraries(MyAppDebug <some libs>)
set_target_properties(MyAppDebug PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/DebugOsx"
    OUTPUT_NAME "MyApp"
    MACOSX_BUNDLE TRUE
    MACOSX_BUNDLE_BUNDLE_NAME MyApp
    MACOSX_BUNDLE_BUNDLE_VERSION 1
    MACOSX_BUNDLE_SHORT_VERSION_STRING "1.0.0"
    MACOSX_BUNDLE_GUI_IDENTIFIER org.mycompany.myapp
    )

Note: I cannot change the filenames due to compatibility reasons.

1 Answers

Edit: To be precise, this has been fixed in CMake version 1.18.0. 1.17.5 still has the issue.

Apparently this has been fixed between CMake version 3.17.3(Shipped with latest version of CLion) and version 3.19.2 (currently latest).

CLion (as of 2020.3) does not support CMake version >= 3.18, but after clearing all the cache and reloading the project it does work, and the special characters in the filename are escaped. For now I will simply add

cmake_minimum_required(VERSION 3.19)

to the Apple part of the CMake files.

Note: I looked in all the release notes of CMake since version 3.18, but I could not find any note of this being fixed.

Related