Home > Blockchain >  Cmake custom command conditional on build
Cmake custom command conditional on build

Time:11-27

I have an issue with cmake. Iv'e tried seraching all online but no answers and Ive tried many possible solutions and nothing has worked.

Im trying to copy a dll file into the executable directory automatically after the build and only if the dll file exists.

code Ive tried:

1.

add_custom_command(TARGET Game PRE_BUILD COMMAND -E
    if(EXISTS "${PROJECT_SOURCE_DIR}/build/vendor/assimp/bin/$<CONFIGURATION>/assimp-vc143-mtd.dll")
        COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/build/vendor/assimp/bin/$<CONFIGURATION>/assimp-vc143-mtd.dll" $<TARGET_FILE_DIR:Game>
    endif()
)

Errors: bunch of visual studio errors. you can't nest Cmake custom commands

  1. Ive also tried

in CMakeLists.txt

add_custom_command(TARGET Game PRE_BUILD COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/checkFile.cmake)

in checkFile.cmake

if(EXISTS "${PROJECT_SOURCE_DIR}/build/vendor/assimp/bin/$<CONFIGURATION>/assimp-vc143-mtd.dll")
    add_custom_command(TARGET Game POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/build/vendor/assimp/bin/$<CONFIGURATION>/assimp-vc143-mtd.dll" $<TARGET_FILE_DIR:Game>) 
endif()

Errors: nothing it silently fails

  1. Also tried in checkFile.cmake
add_custom_command(TARGET Game POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/build/vendor/assimp/bin/$<CONFIGURATION>/assimp-vc143-mtd.dll" $<TARGET_FILE_DIR:Game>) 

Errors: add_custom_command command is not scriptable

Nothing I try actually works. Is this even possible for Cmake? As for why I'm doing this in the first place. this library I'm trying to use sometimes decides to use dynamic library despite being set to static only. On Linux it does static every time but on windows it's kind of random maybe.

Iv'e searched online but found nothing which can help solve this issue. There are two problems. This dll copying needs to be done after the build is complete and it needs to do it automatically, so this whole setup must be invoked by a custom command and needs to run a custom command within a custom command which is possible, but Cmake says you can't do that see error 3.

Can anyone help me with this?

CodePudding user response:

in checkFile.cmake

Do not add_custom_command in the script. It's in script mode, not cmake configuration. Actually copy the file. In checkFile.cmake:

if(EXISTS "${FROM}")
    FILE(COPY "${FROM}" "${TO}")
endif()

and pass variabels:

add_custom_command(TARGET Game POST_BUILD
    COMMAND ${CMAKE_COMMAND}
       -P ${CMAKE_CURRENT_SOURCE_DIR}/checkFile.cmake
       -D FROM="${PROJECT_SOURCE_DIR}/build/vendor/assimp/bin/$CONFIGURATION/assimp-vc143-mtd.dll"
       -D TO=<TARGET_FILE_DIR:game>
 )
  • Related