Skip to content

Commit

Permalink
Merge pull request OSGeo#5677 from rouault/cmake_fix_sudo_make_install
Browse files Browse the repository at this point in the history
CMake: fix 'sudo make [install]' from a checkout owned by a non-root user
  • Loading branch information
rouault authored May 6, 2022
2 parents 67b862d + 86d7ed6 commit c9670b1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
59 changes: 38 additions & 21 deletions cmake/helpers/generate_gdal_version_h.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include(${SOURCE_DIR}/cmake/modules/init.cmake)
include("${SOURCE_DIR}/cmake/modules/init.cmake")

file(READ ${SOURCE_DIR}/gcore/gdal_version.h.in GDAL_VERSION_H_CONTENTS)
file(READ "${SOURCE_DIR}/gcore/gdal_version.h.in" GDAL_VERSION_H_CONTENTS)
string(CONCAT
GDAL_VERSION_H_CONTENTS
"/* This is a generated file from gdal_version.h.in. DO NOT MODIFY !!!! */\n"
Expand All @@ -20,34 +20,51 @@ elseif (EXISTS ${SOURCE_DIR}/.git)
find_package(Git)
if (GIT_FOUND)
include(GetGitRevisionDescription)
get_git_head_revision(GDAL_GIT_REFSPEC GDAL_GIT_HASH ${SOURCE_DIR})
include(GetGitHeadDate)
get_git_head_date(GDAL_GIT_DATE ${SOURCE_DIR})
git_local_changes(GIT_LOCAL_CHG ${SOURCE_DIR})
string(SUBSTRING ${GDAL_GIT_HASH} 0 10 REV)
set(GDAL_DEV_REVISION "dev-${REV}")
if (GIT_LOCAL_CHG STREQUAL "DIRTY")
set(GDAL_DEV_REVISION "${GDAL_DEV_REVISION}-dirty")
endif()
get_git_head_revision(GDAL_GIT_REFSPEC GDAL_GIT_HASH "${SOURCE_DIR}")
if ("${GDAL_GIT_HASH}" STREQUAL "")
message(WARNING "Cannot determine git hash")
else()
include(GetGitHeadDate)
get_git_head_date(GDAL_GIT_DATE "${SOURCE_DIR}")
git_local_changes(GIT_LOCAL_CHG "${SOURCE_DIR}")
string(SUBSTRING "${GDAL_GIT_HASH}" 0 10 REV)
set(GDAL_DEV_REVISION "dev-${REV}")
if (GIT_LOCAL_CHG STREQUAL "DIRTY")
set(GDAL_DEV_REVISION "${GDAL_DEV_REVISION}-dirty")
elseif (GIT_LOCAL_CHG STREQUAL "UNKNOWN" AND
EXISTS "${BINARY_DIR}/gcore/gdal_version.h" AND
EXISTS "${BINARY_DIR}/gcore/gdal_version_full/gdal_version.h")
# Happens typically when running "sudo make install" after "make" as a regular user
file(READ "${BINARY_DIR}/gcore/gdal_version_full/gdal_version.h" GDAL_FULL_VERSION_H_CONTENTS)
if( "${GDAL_FULL_VERSION_H_CONTENTS}" MATCHES "${REV}" )
# Assume the -dirty state has not been modified. This avoids
# rebuilding files
return()
endif()
# If we get here, the revision number has changed from the latest build
# but we are not able to determine if there are local changes.
# Assume none...
endif()

string(REPLACE "dev" "${GDAL_DEV_REVISION}"
GDAL_VERSION_H_CONTENTS "${GDAL_VERSION_H_CONTENTS}")
string(REGEX REPLACE "(define GDAL_RELEASE_DATE[ ]+)([0-9]+)(.*)" "\\1${GDAL_GIT_DATE}\\3"
GDAL_VERSION_H_CONTENTS "${GDAL_VERSION_H_CONTENTS}")
string(REPLACE "dev" "${GDAL_DEV_REVISION}"
GDAL_VERSION_H_CONTENTS "${GDAL_VERSION_H_CONTENTS}")
string(REGEX REPLACE "(define GDAL_RELEASE_DATE[ ]+)([0-9]+)(.*)" "\\1${GDAL_GIT_DATE}\\3"
GDAL_VERSION_H_CONTENTS "${GDAL_VERSION_H_CONTENTS}")
endif()
endif ()
endif ()

file(WRITE ${BINARY_DIR}/gcore/gdal_version_full/gdal_version.h.tmp "${GDAL_VERSION_H_CONTENTS}")
file(WRITE "${BINARY_DIR}/gcore/gdal_version_full/gdal_version.h.tmp" "${GDAL_VERSION_H_CONTENTS}")
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BINARY_DIR}/gcore/gdal_version_full/gdal_version.h.tmp
${BINARY_DIR}/gcore/gdal_version_full/gdal_version.h)
"${BINARY_DIR}/gcore/gdal_version_full/gdal_version.h.tmp"
"${BINARY_DIR}/gcore/gdal_version_full/gdal_version.h")

# For faster builds, generate a strip down version without GDAL_RELEASE_DATE
# and GDAL_RELEASE_NAME which change quite often in git builds, to save rebuilt
# time
string(REGEX REPLACE "(DO_NOT_DEFINE_GDAL_DATE_NAME\\))(.*)" "\\1\n#endif\n"
GDAL_VERSION_MINIMUM_H_CONTENTS "${GDAL_VERSION_H_CONTENTS}")
file(WRITE ${BINARY_DIR}/gcore/gdal_version.h.tmp "${GDAL_VERSION_MINIMUM_H_CONTENTS}")
file(WRITE "${BINARY_DIR}/gcore/gdal_version.h.tmp" "${GDAL_VERSION_MINIMUM_H_CONTENTS}")
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BINARY_DIR}/gcore/gdal_version.h.tmp
${BINARY_DIR}/gcore/gdal_version.h)
"${BINARY_DIR}/gcore/gdal_version.h.tmp"
"${BINARY_DIR}/gcore/gdal_version.h")
13 changes: 11 additions & 2 deletions cmake/modules/thirdparty/GetGitRevisionDescription.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ function(get_git_head_revision _refspecvar _hashvar _repo_dir)
OUTPUT_STRIP_TRAILING_WHITESPACE)

if(NOT res EQUAL 0)
return()
# git rev-parse can fail if run as root (e.g "sudo make install")
# on a git repo owned by another user
if(EXISTS ${_repo_dir}/.git/HEAD)
set(GIT_DIR ${_repo_dir}/.git)
else()
return()
endif()
endif()

get_filename_component(GIT_DIR "${GIT_DIR}" ABSOLUTE BASE_DIR "${_repo_dir}")
Expand Down Expand Up @@ -166,7 +172,10 @@ function(git_local_changes _var _repo_dir)
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(res EQUAL 0)
set(${_var} "CLEAN" PARENT_SCOPE)
else()
elseif(res EQUAL 1)
set(${_var} "DIRTY" PARENT_SCOPE)
else()
# happens typically if running as root on a directory not owned by root
set(${_var} "UNKNOWN" PARENT_SCOPE)
endif()
endfunction()

0 comments on commit c9670b1

Please sign in to comment.