devilutionX/CMake/git.cmake
Gleb Mazovetskiy 7b72b3d4ea CMake: Use generator expressions more
Generator expressions are the only way to distinguish between build
types in multi-configuration builds (Visual Studio).

This adds generator expressions for some of build type dependent
values as a starting point.

See a more detailed explanation at:
https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#build-specification-with-generator-expressions

Notably:

> Some buildsystems generated by cmake(1) have a predetermined build-configuration set in the CMAKE_BUILD_TYPE variable. The buildsystem for the IDEs such as Visual Studio and Xcode are generated independent of the build-configuration, and the actual build configuration is not known until build-time. Therefore, code such as
>
>     string(TOLOWER ${CMAKE_BUILD_TYPE} _type)
>     if (_type STREQUAL debug)
>       target_compile_definitions(exe1 PRIVATE DEBUG_BUILD)
>     endif()
>
> may appear to work for Makefile Generators and Ninja generators, but is not portable to IDE generators.
2020-04-08 04:36:34 +02:00

17 lines
588 B
CMake

function(get_git_tag output_var)
execute_process(
COMMAND git describe --abbrev=0 --tags
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(${output_var} ${GIT_TAG} PARENT_SCOPE)
endfunction(get_git_tag)
function(get_git_commit_hash output_var)
execute_process(
COMMAND git log -1 --format=%h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(${output_var} ${GIT_COMMIT_HASH} PARENT_SCOPE)
endfunction(get_git_commit_hash)