How can I pass git SHA1 to compiler as definition using cmake?

后端 未结 10 1875
梦毁少年i
梦毁少年i 2020-11-28 02:32

In a Makefile this would be done with something like:

g++ -DGIT_SHA1=\"`git log -1 | head -n 1`\" ...

This is very useful, because the bina

10条回答
  •  忘掉有多难
    2020-11-28 02:40

    I'd use sth. like this in my CMakeLists.txt:

    exec_program(
        "git"
        ${CMAKE_CURRENT_SOURCE_DIR}
        ARGS "describe"
        OUTPUT_VARIABLE VERSION )
    
    string( REGEX MATCH "-g.*$" VERSION_SHA1 ${VERSION} )
    string( REGEX REPLACE "[-g]" "" VERSION_SHA1 ${VERSION_SHA1} )
    
    add_definitions( -DGIT_SHA1="${VERSION_SHA1}" )
    

提交回复
热议问题