How to properly use bracket arguments in install(code)?

I have been studying an example, by Ivan Onyshenko, which demonstrates how to use fixup_bundle and how cmake and cpack can create, for macOS, a bundle and also copy .dylib dependencies into that bundle.

That’s all fine. Now, in that CMakeLists.tx file there’s some code like the following:


set(APPS "\${CMAKE_INSTALL_PREFIX}/${APP_NAME}.app")

# Directories to look for dependencies
set(DIRS "${CMAKE_BINARY_DIR}")

install(CODE "include(BundleUtilities)
    fixup_bundle(\"${APPS}\" \"\" \"${DIRS}\")")

This works fine.

My question is: is it possible, in this case, to use bracket arguments in fixup_bundle. I can’t seem to get it right.

I’ve tried to convert to the following without success:

install(CODE [[
    include(BundleUtilities)
    fixup_bundle("${APPS}" "" "${DIRS}")
]])

And then I read somewhere that only ${CMAKE_INSTALL_PREFIX} and generator expressions work in bracketted code. But even in this case, it doesn’t work:

install(CODE [[
    include(BundleUtilities)
    fixup_bundle(
    "${CMAKE_INSTALL_PREFIX}/PortableApp.app"
    "" 
    "${CMAKE_INSTALL_PREFIX}")
]])

So are bracket arguments usable in this context? If not, how to avoid all this escaping which I personally find tedious and error-prone?

install(SCRIPT) would be the way to go I think. Note that the “what variables work” part is about what variables are present in the install script environment. If you want configure-time variables to be used, @var@ expansion and configure_file can be used to create the path given to install(SCRIPT).

Thanks @ben.boeckel for your reply.

Just to flesh out your answer. Would it mean that I have something like a fixup_osx.cmake.in file which contains the @var@ variables which are defined somewhere in the CMakeList.txt file, and then use configure_file to take fixup_osx.cmake.in and output fixup_osx.cmake with the vars now expanded and then use install(SCRIPT fixup_osx.cmake) to execute the installation commands in that file.

Does that sequence sound about right?

Sounds like the right direction to me.

Thanks @ben.boeckel, that was very helpful.