From fcc08f25b083178ad13dc18f003b6d1c15904620 Mon Sep 17 00:00:00 2001 From: Nicholas O'Connor Date: Wed, 5 Jul 2017 13:13:04 -0700 Subject: [PATCH] push git related logic to cmake, now that i know how to do that kind of thing --- CMakeLists.txt | 8 ++++++- cmake/Modules/Git.cmake | 52 +++++++++++++++++++++++++++++++++++++++++ cogrc | 3 +-- src/version.cpp.cog | 33 ++++++++++++++------------ 4 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 cmake/Modules/Git.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e4756d..fbd03f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,9 @@ SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/cellar") # which point to directories outside the build tree to the install RPATH SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) +include(Git) +git_init() + include(Ronn) find_package(PythonInterp) @@ -40,7 +43,10 @@ http://tclap.sourceforge.net and put the headers in ./include (wink, nudge)") endif(NOT TCLAP_FOUND) -include_directories(include) +include_directories("${CMAKE_SOURCE_DIR}/include") +configure_file("${CMAKE_SOURCE_DIR}/include/cmake.hpp.in" + "${CMAKE_CURRENT_BINARY_DIR}/include/cmake.hpp") +include_directories("${CMAKE_CURRENT_BINARY_DIR}/include") set(src "${CMAKE_SOURCE_DIR}/src") set(coggedfiles) diff --git a/cmake/Modules/Git.cmake b/cmake/Modules/Git.cmake new file mode 100644 index 0000000..702c1ea --- /dev/null +++ b/cmake/Modules/Git.cmake @@ -0,0 +1,52 @@ +function(git_init) + set(options "") + set(oneValueArgs SUBMODULE_DIR) + set(multiValueArgs "") + cmake_parse_arguments(GIT_INIT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + set(GIT_SUBMODULE_DIR "${GIT_INIT_SUBMODULE_DIR}" PARENT_SCOPE) + set(GIT_SUBMODULE_DIR_ABS "${CMAKE_SOURCE_DIR}/${GIT_INIT_SUBMODULE_DIR}" PARENT_SCOPE) + + execute_process(COMMAND git rev-parse --git-dir + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_QUIET + RESULT_VARIABLE GIT_REPO_RESULT) + + if ("${GIT_REPO_RESULT}" STREQUAL "0") + set(IS_GIT_REPO TRUE) + set(IS_GIT_REPO TRUE PARENT_SCOPE) + else("${GIT_REPO_RESULT}" STREQUAL "0") + set(IS_GIT_REPO FALSE) + set(IS_GIT_REPO FALSE PARENT_SCOPE) + endif("${GIT_REPO_RESULT}" STREQUAL "0") + + if(IS_GIT_REPO) + execute_process(COMMAND git log --pretty=format:%H -n 1 + OUTPUT_VARIABLE GIT_COMMIT_HASH) + string(STRIP "${GIT_COMMIT_HASH}" GIT_COMMIT_HASH) + set(GIT_COMMIT_HASH ${GIT_COMMIT_HASH} PARENT_SCOPE) + + execute_process(COMMAND git symbolic-ref HEAD + COMMAND cut -d/ -f3 + OUTPUT_VARIABLE GIT_BRANCH) + string(STRIP "${GIT_BRANCH}" GIT_BRANCH) + set(GIT_BRANCH ${GIT_BRANCH} PARENT_SCOPE) + + message(STATUS "commit ${GIT_COMMIT_HASH}, branch ${GIT_BRANCH}") + else(IS_GIT_REPO) + message(STATUS "This is not a git repo, assuming this tarball actually packaged everything...") + endif(IS_GIT_REPO) +endfunction(git_init) + +function(git_submodule) + if(IS_GIT_REPO) + set(oneValueArgs TARGET REMOTE_URL) + set(multiValueArgs "") + cmake_parse_arguments(GIT_SUBMODULE "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + execute_process(COMMAND git submodule update --init -- "${GIT_SUBMODULE_DIR}/${GIT_SUBMODULE_TARGET}" + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + COMMENT "Updating submodule ${GIT_SUBMODULE_DIR}/${GIT_SUBMODULE_TARGET}" + VERBATIM) + endif(IS_GIT_REPO) +endfunction(git_submodule) diff --git a/cogrc b/cogrc index 97faaaa..bb156c8 100644 --- a/cogrc +++ b/cogrc @@ -1,6 +1,5 @@ [version] -release=no -release_version=0.3 +release_version=0.4 [defaults] wine-path=wine diff --git a/src/version.cpp.cog b/src/version.cpp.cog index e7d0bef..704c320 100644 --- a/src/version.cpp.cog +++ b/src/version.cpp.cog @@ -1,9 +1,13 @@ // vim: filetype=cpp : #include #include +#ifdef IS_GIT_REPO +#include +#endif #include #include "cellar.hpp" +#include "cmake.hpp" #include "internal/core.hpp" #include "commands.hpp" #include "output.hpp" @@ -24,25 +28,24 @@ string cellar::version::short_version() { cparse.read("cogrc") config = cparse["version"] - if config.getboolean("release"): - outstring = "version {0}, built {1}".format(config["release_version"], str(today)) - else: - try: - import subprocess - cmdoutput = subprocess.check_output(["git", "symbolic-ref", "HEAD"]).decode().strip() - gitbranch = cmdoutput.split("/")[2] - - githash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip() - outstring = "{0} {1}, built {2}".format(gitbranch, githash, str(today)) - except: - outstring = "exception raised when trying to read git data at precompile time" - raise - - cog.outl("return string(\"{0}\");".format(outstring)) + cog.outl("return \"version {0}, built {1}\";".format(config["release_version"], str(today))) ]]]*/ //[[[end]]] } void cellar::core::print_version(int argc, vector argv) { cellar::output::statement(short_version()); + + #ifdef IS_GIT_REPO + stringstream sstr; + sstr << "git commit "; + sstr << GIT_COMMIT_HASH; + + sstr << " (branch "; + sstr << GIT_BRANCH; + sstr << ")"; + + // " >> git commit d34db33fcafe (branch master)" + cellar::output::statement(sstr.str()); + #endif }