Skip to content

Commit

Permalink
Fix cmake shenanigans with version_gen.h. Refs. darktable-org#10354
Browse files Browse the repository at this point in the history
E.g., previously, if you would do the following, the build would fail:
$ cd ~/darktable/build/ && rm -rf * && CC=gcc CXX=g++ cmake ../ && make -j1 darkroom
...
[ 38%] Building C object src/CMakeFiles/lib_darktable.dir/common/darktable.c.o
In file included from /home/lebedevri/darktable/src/common/darktable.c:24:0:
/home/lebedevri/darktable/build/src/version.h:7:25: fatal error: version_gen.h: No such file or directory
compilation terminated.
src/CMakeFiles/lib_darktable.dir/build.make:292: recipe for target 'src/CMakeFiles/lib_darktable.dir/common/darktable.c.o' failed
make[3]: *** [src/CMakeFiles/lib_darktable.dir/common/darktable.c.o] Error 1
CMakeFiles/Makefile2:1290: recipe for target 'src/CMakeFiles/lib_darktable.dir/all' failed
make[2]: *** [src/CMakeFiles/lib_darktable.dir/all] Error 2
CMakeFiles/Makefile2:1514: recipe for target 'src/views/CMakeFiles/darkroom.dir/rule' failed
make[1]: *** [src/views/CMakeFiles/darkroom.dir/rule] Error 2
Makefile:626: recipe for target 'darkroom' failed
make: *** [darkroom] Error 2
  • Loading branch information
LebedevRI committed Feb 25, 2016
1 parent eaca247 commit dc2dfcd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
66 changes: 46 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,39 +132,65 @@ endif(WIN32)
#
# Set package version
#
set(DO_GENERATE_VERSION Off)
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/src/") # the src/ subdirectory won't exist yet
if(DEFINED PROJECT_VERSION)
#project version is defined by -D on the cmake command line
# only use that value, do not upate it at make time
execute_process(
COMMAND ${CMAKE_SOURCE_DIR}/tools/create_version_h.sh ${CMAKE_CURRENT_BINARY_DIR}/src/version_gen.h ${PROJECT_VERSION}

# adds custom command to generate header containing version info.
# takes 1 optional parameter - version override.
function(generate_version_gen_h)
if(ARGC EQUAL 1)
# if a version override was specified, use it
set(_VERSION "${ARGV0}")
else()
# else, the tool will autodetect the version
set(_VERSION "")
endif()

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src/version_gen.h
COMMAND ${CMAKE_SOURCE_DIR}/tools/create_version_h.sh ${CMAKE_CURRENT_BINARY_DIR}/src/version_gen.h ${_VERSION}
DEPENDS ${CMAKE_SOURCE_DIR}/tools/create_version_h.sh
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Updating version string"
VERBATIM # else might break when export-subst was needed but did not happen
)
endfunction(generate_version_gen_h)

if(DEFINED PROJECT_VERSION)
#project version is defined by -D on the cmake command line
# only use that value, do not update it at make time
generate_version_gen_h(${PROJECT_VERSION})
else(DEFINED PROJECT_VERSION)
if(NOT SOURCE_PACKAGE) # i.e., a git checkout
# this part is setting the corresponding CMake variable which gets used for example when creating a source package
execute_process(
COMMAND git describe --tags --dirty
COMMAND sed "s,^release-,,;s,-,+,;s,-,~,;" OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE PROJECT_VERSION
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
add_custom_target(generate_version ALL
COMMAND ${CMAKE_SOURCE_DIR}/tools/create_version_h.sh ${CMAKE_CURRENT_BINARY_DIR}/src/version_gen.h
COMMENT "Updating version string"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
set(DO_GENERATE_VERSION On)
elseif(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/version_gen.h)
# no need to create version_gen.h if it's already shipped. that is for example the case with our release tarballs
# else(NOT SOURCE_PACKAGE)
set(PROJECT_VERSION "archive-$Format:%H$")
execute_process(
COMMAND ${CMAKE_SOURCE_DIR}/tools/create_version_h.sh ${CMAKE_CURRENT_BINARY_DIR}/src/version_gen.h ${PROJECT_VERSION}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
generate_version_gen_h()
else(NOT SOURCE_PACKAGE)
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/version_gen.h)
set(PROJECT_VERSION "archive-$Format:%H$")
generate_version_gen_h(${PROJECT_VERSION})
else(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/version_gen.h)
# no need to create version_gen.h if it's already shipped. that is for example the case with our release tarballs
# but generate_version target expects it to be in build dir, so we need to copy it
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src/version_gen.h
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/src/version_gen.h ${CMAKE_CURRENT_BINARY_DIR}/src/version_gen.h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Updating version string"
)
endif(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/version_gen.h)
endif(NOT SOURCE_PACKAGE)
endif(DEFINED PROJECT_VERSION)

# WARNING: no target should reference version_gen.h directly. instead, they should add_dependencies(yourtarget generate_version)
add_custom_target(
generate_version ALL
DEPENDS ${CMAKE_SOURCE_DIR}/tools/create_version_h.sh
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/src/version_gen.h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

#
# Initial cmake/debian/postinst and prerm script for debian package
Expand Down
4 changes: 1 addition & 3 deletions cmake/darktable-packaging.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,4 @@ ADD_CUSTOM_TARGET(pkgsrc
COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_SOURCE_DIR}/src/version_gen.h
)

if(DO_GENERATE_VERSION)
add_dependencies(pkgsrc generate_version)
endif(DO_GENERATE_VERSION)
add_dependencies(pkgsrc generate_version)
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ add_library(metadata_dummy ${CMAKE_CURRENT_BINARY_DIR}/metadata_gen.c ${CMAKE_CU
# build libdarktable
#
add_library(lib_darktable SHARED ${CMAKE_CURRENT_BINARY_DIR}/preferences_gen.h ${CMAKE_CURRENT_BINARY_DIR}/metadata_gen.h ${CMAKE_CURRENT_BINARY_DIR}/metadata_gen.c ${SOURCES})
add_dependencies(lib_darktable generate_version)

if(APPLE)
set_target_properties(lib_darktable PROPERTIES MACOSX_RPATH TRUE)
endif(APPLE)
Expand Down

0 comments on commit dc2dfcd

Please sign in to comment.