Force CMake to generate configure_file target every build

前端 未结 2 1428
别那么骄傲
别那么骄傲 2021-01-12 06:03

I have following command in my CMakeLists.txt file

configure_file([...]/Version.h.in [...]/Version.h @ONLY)

How do I make it r

相关标签:
2条回答
  • 2021-01-12 06:39

    I transfered my version string generation into its own CMake script that I call with ${CMAKE_COMMAND} -P ... in a add_custom_command()/add_custom_target() and make my target using it depend on it.

    In your case let's say you have a DateToVersionH.cmake script like this:

    string(TIMESTAMP _date "%d %m %Y %H:%M")
    file(WRITE ${VERSION_FILE_NAME} "#define MY_VERSION \"Bla-bla-bla ${FOOBAR} built on ${_date}\"\n")
    

    you can do something like:

    add_custom_command(
        TARGET MyExe
        PRE_BUILD
        COMMAND ${CMAKE_COMMAND} -DVERSION_FILE_NAME=Version.h -DFOOBAR="${FOOBAR}" -P "${CMAKE_CURRENT_LIST_DIR}/DateToVersionH.cmake"
    )
    

    See also use CMake to get build-time svn revision

    That may not be enough if your make environment checks all the "needs to be rebuild" prior of calling the necessary steps (e.g. in Ninja only the outputs of custom commands are re-scanned; for more details see the Ninja documentation for the restat rule variable and this discussion which led to the introduction of the BYPRODUCTS option in add_custom_command()).

    So you could simply add to your build shell script - prior to calling the actual make - removing the object file containing the version information.

    Or you are forcing the re-compilation of the object before linking. This would - assuming you have a Version.c which includes Version.h - look like this:

    include_directories(${CMAKE_CURRENT_BINARY_DIR})
    execute_process(COMMAND "${CMAKE_COMMAND}" -DVERSION_FILE_NAME=Version.h -DFOOBAR="${FOOBAR}" -P "${CMAKE_CURRENT_LIST_DIR}/DateToVersionH.cmake")
    add_library(MyVersionObj OBJECT Version.c)
    
    add_executable(MyExe ... $<TARGET_OBJECTS:MyVersionObj>)
    
    add_custom_command(
        TARGET MyExe
        PRE_LINK 
        COMMAND "${CMAKE_COMMAND}" -DVERSION_FILE_NAME=Version.h -DFOOBAR="${FOOBAR}" -P "${CMAKE_CURRENT_LIST_DIR}/DateToVersionH.cmake"
        COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --config $<CONFIG> --target "MyVersionObj"
    )
    

    Thinking about it, just deleting the object file and utilizing your current __DATE__ solution is the simplest solution.

    For another "git version solution" see CMake: Automatically use git tags as version strings and How can I pass git SHA1 to compiler as definition using cmake?

    0 讨论(0)
  • 2021-01-12 06:46

    Just to submit an alternative answer that seems simpler to me and does the trick if you are under any *nix OS :

    Running touch Version.h.in as a pre-build command, either implemented in your IDE, manually executed before running your cmake commands in your shell, or in your CI (where it might be useless), allows to systematically generate the Version.h file, even if it has not been modified.

    0 讨论(0)
提交回复
热议问题