From b28905a2281d31997fbc9a45c0810624f1a9633d Mon Sep 17 00:00:00 2001 From: Ulrich Germann Date: Tue, 21 Jul 2020 11:32:08 +0100 Subject: [PATCH] Fix bug in finding path to ./git/log/HEAD if Marian is a submodule. (#644) * Fix bug in finding path to ./git/log/HEAD if Marian is a submodule. * Build build_info.cpp in build dir, not source dir. --- CHANGELOG.md | 2 ++ CMakeLists.txt | 5 ++++- cmake/GetVersionFromFile.cmake | 2 +- src/CMakeLists.txt | 27 ++++++++++++++------------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53fac0c92..d565c05fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - In concatenation make sure that we do not multiply 0 with nan (which results in nan) - Change Approx.epsilon(0.01) to Approx.margin(0.001) in unit tests. Tolerance is now absolute and not relative. We assumed incorrectly that epsilon is absolute tolerance. +- Fixed bug in finding .git/logs/HEAD when Marian is a submodule in another project. +- Properly record cmake variables in the cmake build directory instead of the source tree. ### Changed - Move Simple-WebSocket-Server to submodule diff --git a/CMakeLists.txt b/CMakeLists.txt index 836c4099d..2133396d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -442,8 +442,11 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/common/project_version.h.in # Generate build_info.cpp with CMake cache variables include(GetCacheVariables) +# make sure src/common/build_info.cpp has been removed +execute_process(COMMAND rm ${CMAKE_CURRENT_SOURCE_DIR}/src/common/build_info.cpp + OUTPUT_QUIET ERROR_QUIET) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/common/build_info.cpp.in - ${CMAKE_CURRENT_SOURCE_DIR}/src/common/build_info.cpp @ONLY) + ${CMAKE_CURRENT_BINARY_DIR}/src/common/build_info.cpp @ONLY) # Compile source files include_directories(${marian_SOURCE_DIR}/src) diff --git a/cmake/GetVersionFromFile.cmake b/cmake/GetVersionFromFile.cmake index 29b9d2bca..0e9c54fe8 100644 --- a/cmake/GetVersionFromFile.cmake +++ b/cmake/GetVersionFromFile.cmake @@ -18,7 +18,7 @@ if(PROJECT_VERSION_FILE) file(STRINGS ${PROJECT_VERSION_FILE} PROJECT_VERSION_STRING) else() - file(STRINGS ${CMAKE_SOURCE_DIR}/VERSION PROJECT_VERSION_STRING) + file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/VERSION PROJECT_VERSION_STRING) endif() # Get current commit SHA from git diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 94dc6a258..bcee1b385 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,7 +20,7 @@ add_library(marian STATIC common/config_validator.cpp common/options.cpp common/binary.cpp - common/build_info.cpp + ${CMAKE_CURRENT_BINARY_DIR}/common/build_info.cpp common/io.cpp common/filesystem.cpp common/file_stream.cpp @@ -118,21 +118,22 @@ target_compile_options(marian PUBLIC ${ALL_WARNINGS}) # Git updates .git/logs/HEAD file whenever you pull or commit something. # If Marian is checked out as a submodule in another repository, -# there's no .git directory in ${CMAKE_SOURCE_DIR}. Instead .git is a -# file that specifies the relative path from ${CMAKE_SOURCE_DIR} to -# ./git/modules/ in the root of the repository that -# contains Marian as a submodule. We set MARIAN_GIT_DIR to the appropriate -# path, depending on whether ${CMAKE_SOURCE_DIR}/.git is a directory or file. -if(IS_DIRECTORY ${CMAKE_SOURCE_DIR}/.git) # not a submodule - set(MARIAN_GIT_DIR ${CMAKE_SOURCE_DIR}/.git) -else(IS_DIRECTORY ${CMAKE_SOURCE_DIR}/.git) - file(READ ${CMAKE_SOURCE_DIR}/.git MARIAN_GIT_DIR) +# ${CMAKE_CURRENT_SOURCE_DIR}/../.git is not a directory but a file +# that specifies the relative path from ${CMAKE_CURRENT_SOURCE_DIR}/.. +# to ./git/modules/ in the root of the check_out of +# the project that contains Marian as a submodule. +# +# We set MARIAN_GIT_DIR to the appropriate path, depending on whether +# ${CMAKE_CURRENT_SOURCE_DIR}/../.git is a directory or file. +set(MARIAN_GIT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../.git) +if(NOT IS_DIRECTORY ${MARIAN_GIT_DIR}) # i.e., it's a submodule + file(READ ${MARIAN_GIT_DIR} MARIAN_GIT_DIR) string(REGEX REPLACE "gitdir: (.*)\n" "\\1" MARIAN_GIT_DIR ${MARIAN_GIT_DIR}) - get_filename_component(MARIAN_GIT_DIR "${CMAKE_SOURCE_DIR}/${MARIAN_GIT_DIR}" ABSOLUTE) -endif(IS_DIRECTORY ${CMAKE_SOURCE_DIR}/.git) + get_filename_component(MARIAN_GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${MARIAN_GIT_DIR}" ABSOLUTE) +endif(NOT IS_DIRECTORY ${MARIAN_GIT_DIR}) add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/common/git_revision.h - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND git log -1 --pretty=format:\#define\ GIT_REVISION\ \"\%h\ \%ai\" > ${CMAKE_CURRENT_SOURCE_DIR}/common/git_revision.h DEPENDS ${MARIAN_GIT_DIR}/logs/HEAD VERBATIM