From 1e573bd557e6b79b0d94fc7bed1b048f76b2c2ac Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 5 Mar 2019 08:35:53 -0500 Subject: [PATCH 1/3] when compiling LAMMPS in a git checkout, embed and print git branch and revision info into the executable --- cmake/CMakeLists.txt | 28 +++++++++++++++++++++++++++- src/Makefile | 21 +++++++++++++++++++-- src/Purge.list | 1 + src/info.cpp | 10 +++++++--- src/lammps.cpp | 5 +++++ src/lammps.h | 1 + 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index d9c87bdf5b..f54cf9d887 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -11,6 +11,8 @@ get_filename_component(LAMMPS_LIB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../lib get_filename_component(LAMMPS_LIB_BINARY_DIR ${CMAKE_BINARY_DIR}/lib ABSOLUTE) get_filename_component(LAMMPS_DOC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../doc ABSOLUTE) +find_package(Git) + # by default, install into $HOME/.local (not /usr/local), so that no root access (and sudo!!) is needed if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "$ENV{HOME}/.local" CACHE PATH "default install path" FORCE ) @@ -85,7 +87,7 @@ string(TOUPPER "${CMAKE_BUILD_TYPE}" BTYPE) # this is fast, so check for it all the time message(STATUS "Running check for auto-generated files from make-based build system") file(GLOB SRC_AUTOGEN_FILES ${LAMMPS_SOURCE_DIR}/style_*.h) -list(APPEND SRC_AUTOGEN_FILES ${LAMMPS_SOURCE_DIR}/lmpinstalledpkgs.h) +list(APPEND SRC_AUTOGEN_FILES ${LAMMPS_SOURCE_DIR}/lmpinstalledpkgs.h ${LAMMPS_SOURCE_DIR}/lmpgitversion.h) foreach(_SRC ${SRC_AUTOGEN_FILES}) get_filename_component(FILENAME "${_SRC}" NAME) if(EXISTS ${LAMMPS_SOURCE_DIR}/${FILENAME}) @@ -1326,6 +1328,30 @@ message(STATUS "Generating lmpinstalledpkgs.h...") file(WRITE "${LAMMPS_STYLE_HEADERS_DIR}/lmpinstalledpkgs.h.tmp" "${temp}" ) execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${LAMMPS_STYLE_HEADERS_DIR}/lmpinstalledpkgs.h.tmp" "${LAMMPS_STYLE_HEADERS_DIR}/lmpinstalledpkgs.h") +###################################### +# Generate lmpgitversion.h +###################################### +set(temp "#ifndef LMP_GIT_VERSION_H\n#define LMP_GIT_VERSION_H\n") +set(temp "${temp}const char LAMMPS_NS::LAMMPS::git_version[] =") +if(GIT_FOUND) + execute_process(COMMAND ${GIT_EXECUTABLE} describe HEAD + OUTPUT_VARIABLE temp_git_version + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD + OUTPUT_VARIABLE temp_git_branch + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + set(temp_git "${temp_git_branch} / ${temp_git_version}") +else() + set(temp_git "") +endif() + +set(temp "${temp} \"${temp_git}\";\n#endif\n\n") +message(STATUS "Generating lmpgitversion.h...") +file(WRITE "${LAMMPS_STYLE_HEADERS_DIR}/lmpgitversion.h.tmp" "${temp}" ) +execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${LAMMPS_STYLE_HEADERS_DIR}/lmpgitversion.h.tmp" "${LAMMPS_STYLE_HEADERS_DIR}/lmpgitversion.h") + ########################################### # Actually add executable and lib to build ############################################ diff --git a/src/Makefile b/src/Makefile index f954d84e5d..058a415238 100644 --- a/src/Makefile +++ b/src/Makefile @@ -19,7 +19,7 @@ OBJDIR = Obj_$@ OBJSHDIR = Obj_shared_$@ SRC = $(wildcard *.cpp) -INC = $(filter-out lmpinstalledpkgs.h,$(wildcard *.h)) +INC = $(filter-out lmpinstalledpkgs.h lmpgitversion.h,$(wildcard *.h)) OBJ = $(SRC:.cpp=.o) SRCLIB = $(filter-out main.cpp,$(SRC)) @@ -167,6 +167,23 @@ lmpinstalledpkgs.h: $(SRC) $(INC) mv ${TMPNAME}.lmpinstalled lmpinstalledpkgs.h || rm ${TMPNAME}.lmpinstalled ; \ else mv ${TMPNAME}.lmpinstalled lmpinstalledpkgs.h ; fi +gitversion: + @echo 'Gathering git version information' + @echo '#ifndef LMP_GIT_VERSION_H' > ${TMPNAME}.lmpgitversion + @echo '#define LMP_GIT_VERSION_H' >> ${TMPNAME}.lmpgitversion + @echo 'const char LAMMPS_NS::LAMMPS::git_version[] = ' >> ${TMPNAME}.lmpgitversion + @if (type git && git describe HEAD ) >> /dev/null 2>> /dev/null ; then \ + export v1=$$(git rev-parse --abbrev-ref HEAD); export v2=$$(git describe HEAD); \ + echo "\"$${v1} / $${v2}\";" >> ${TMPNAME}.lmpgitversion ; \ + else \ + echo '"";' >> ${TMPNAME}.lmpgitversion ; \ + fi + @echo '#endif' >> ${TMPNAME}.lmpgitversion + @if [ -f lmpgitversion.h ]; \ + then test "`diff --brief ${TMPNAME}.lmpgitversion lmpgitversion.h`" != "" && \ + mv ${TMPNAME}.lmpgitversion lmpgitversion.h || rm ${TMPNAME}.lmpgitversion ; \ + else mv ${TMPNAME}.lmpgitversion lmpgitversion.h ; fi + # Build LAMMPS in one of 4 modes # exe = exe with static compile in Obj_machine (default) # shexe = exe with shared compile in Obj_shared_machine @@ -180,7 +197,7 @@ lmpinstalledpkgs.h: $(SRC) $(INC) -f MAKE/MACHINES/Makefile.$@ -o -f MAKE/MINE/Makefile.$@ @if [ ! -d $(objdir) ]; then mkdir $(objdir); fi @$(SHELL) Make.sh style - @$(MAKE) $(MFLAGS) lmpinstalledpkgs.h + @$(MAKE) $(MFLAGS) lmpinstalledpkgs.h gitversion @echo 'Compiling LAMMPS for machine $@' @if [ -f MAKE/MACHINES/Makefile.$@ ]; \ then cp MAKE/MACHINES/Makefile.$@ $(objdir)/Makefile; fi diff --git a/src/Purge.list b/src/Purge.list index 32b42f5ef1..59c35efca5 100644 --- a/src/Purge.list +++ b/src/Purge.list @@ -24,6 +24,7 @@ style_nstencil.h style_ntopo.h # other auto-generated files lmpinstalledpkgs.h +lmpgitversion.h # renamed on 7 January 2019 pair_lebedeva.cpp pair_lebedeva.h diff --git a/src/info.cpp b/src/info.cpp index 983a1e9e92..f17a00b8d2 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -260,9 +260,13 @@ void Info::command(int narg, char **arg) fprintf(out,"Printed on %s\n",ctime(&now)); if (flags & CONFIG) { - fprintf(out,"\nLAMMPS version: %s / %s\n\n", - universe->version, universe->num_ver); - + if (strlen(lmp->git_version) > 0) { + fprintf(out,"\nLAMMPS version: %s / %s\nGit revision: %s\n\n", + universe->version, universe->num_ver,lmp->git_version); + } else { + fprintf(out,"\nLAMMPS version: %s / %s\n\n", + universe->version, universe->num_ver); + } const char *infobuf = get_os_info(); fprintf(out,"OS information: %s\n\n",infobuf); delete[] infobuf; diff --git a/src/lammps.cpp b/src/lammps.cpp index 8a23a4297b..80ea1d8c9d 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -52,6 +52,7 @@ #include "error.h" #include "lmpinstalledpkgs.h" +#include "lmpgitversion.h" using namespace LAMMPS_NS; @@ -418,6 +419,10 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if ((universe->me == 0) && !helpflag) { if (screen) fprintf(screen,"LAMMPS (%s)\n",universe->version); if (logfile) fprintf(logfile,"LAMMPS (%s)\n",universe->version); + if (strlen(git_version) > 0) { + if (screen) fprintf(screen,"Git revision (%s)\n",git_version); + if (logfile) fprintf(logfile,"Git revision (%s)\n",git_version); + } } // universe is one or more worlds, as setup by partition switch diff --git a/src/lammps.h b/src/lammps.h index 151b4fc49b..7ae7ab7ecb 100644 --- a/src/lammps.h +++ b/src/lammps.h @@ -64,6 +64,7 @@ class LAMMPS { class CiteMe *citeme; // citation info static const char * installed_packages[]; + static const char git_version[]; LAMMPS(int, char **, MPI_Comm); ~LAMMPS(); From 1c7d1919882e76a688ab2e37962a51c999bbbdbf Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 5 Mar 2019 09:03:12 -0500 Subject: [PATCH 2/3] also print git version (if available) in multi-partition runs and in help message --- src/lammps.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/lammps.cpp b/src/lammps.cpp index 80ea1d8c9d..126a88b3e1 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -497,11 +497,15 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if ((universe->me == 0) && (!helpflag)) { if (universe->uscreen) { fprintf(universe->uscreen,"LAMMPS (%s)\n",universe->version); + if (strlen(git_version) > 0) + fprintf(universe->uscreen,"Git revision (%s)\n",git_version); fprintf(universe->uscreen,"Running on %d partitions of processors\n", universe->nworlds); } if (universe->ulogfile) { fprintf(universe->ulogfile,"LAMMPS (%s)\n",universe->version); + if (strlen(git_version) > 0) + fprintf(universe->ulogfile,"Git revision (%s)\n",git_version); fprintf(universe->ulogfile,"Running on %d partitions of processors\n", universe->nworlds); } @@ -510,10 +514,14 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if ((me == 0) && (!helpflag)) { if (screen) { fprintf(screen,"LAMMPS (%s)\n",universe->version); + if (strlen(git_version) > 0) + fprintf(screen,"Git revision (%s)\n",git_version); fprintf(screen,"Processor partition = %d\n",universe->iworld); } if (logfile) { fprintf(logfile,"LAMMPS (%s)\n",universe->version); + if (strlen(git_version) > 0) + fprintf(logfile,"Git revision (%s)\n",git_version); fprintf(logfile,"Processor partition = %d\n",universe->iworld); } } @@ -903,9 +911,16 @@ void LAMMPS::help() // general help message about command line and flags + if (strlen(git_version) > 0) { + fprintf(fp, + "\nLarge-scale Atomic/Molecular Massively Parallel Simulator - " + LAMMPS_VERSION "\nGit revision (%s)\n\n",git_version); + } else { + fprintf(fp, + "\nLarge-scale Atomic/Molecular Massively Parallel Simulator - " + LAMMPS_VERSION "\n\n"); + } fprintf(fp, - "\nLarge-scale Atomic/Molecular Massively Parallel Simulator - " - LAMMPS_VERSION "\n\n" "Usage example: %s -var t 300 -echo screen -in in.alloy\n\n" "List of command line options supported by this LAMMPS executable:\n\n" "-echo none/screen/log/both : echoing of input script (-e)\n" From c5a3b034ce54ad213fe1c30f4636dc922f5607cf Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 5 Mar 2019 17:29:34 -0500 Subject: [PATCH 3/3] implement various suggestions to improve the use of git information gathered --- cmake/CMakeLists.txt | 36 ++++++++++++++++++++++++++---------- src/Makefile | 18 +++++++++++++----- src/info.cpp | 7 ++++--- src/lammps.cpp | 22 ++++------------------ src/lammps.h | 6 +++++- 5 files changed, 52 insertions(+), 37 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index f54cf9d887..ec32049445 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -1332,22 +1332,38 @@ execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${LAMMPS_STYLE_HE # Generate lmpgitversion.h ###################################### set(temp "#ifndef LMP_GIT_VERSION_H\n#define LMP_GIT_VERSION_H\n") -set(temp "${temp}const char LAMMPS_NS::LAMMPS::git_version[] =") +set(temp_git_commit "(unknown)") +set(temp_git_branch "(unknown)") +set(temp_git_describe "(unknown)") +set(temp_git_info "false") if(GIT_FOUND) execute_process(COMMAND ${GIT_EXECUTABLE} describe HEAD - OUTPUT_VARIABLE temp_git_version + RESULT_VARIABLE temp_in_git_checkout ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) - execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD - OUTPUT_VARIABLE temp_git_branch - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE) - set(temp_git "${temp_git_branch} / ${temp_git_version}") -else() - set(temp_git "") + if(temp_in_git_checkout EQUAL 0) + set(temp_git_info "true") + execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD + OUTPUT_VARIABLE temp_git_commit + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD + OUTPUT_VARIABLE temp_git_branch + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${GIT_EXECUTABLE} describe --dirty=-modified + OUTPUT_VARIABLE temp_git_describe + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() endif() -set(temp "${temp} \"${temp_git}\";\n#endif\n\n") +set(temp "${temp}const bool LAMMPS_NS::LAMMPS::has_git_info = ${temp_git_info};\n") +set(temp "${temp}const char LAMMPS_NS::LAMMPS::git_commit[] = \"${temp_git_commit}\";\n") +set(temp "${temp}const char LAMMPS_NS::LAMMPS::git_branch[] = \"${temp_git_branch}\";\n") +set(temp "${temp}const char LAMMPS_NS::LAMMPS::git_descriptor[] = \"${temp_git_describe}\";\n") +set(temp "${temp}#endif\n\n") + message(STATUS "Generating lmpgitversion.h...") file(WRITE "${LAMMPS_STYLE_HEADERS_DIR}/lmpgitversion.h.tmp" "${temp}" ) execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${LAMMPS_STYLE_HEADERS_DIR}/lmpgitversion.h.tmp" "${LAMMPS_STYLE_HEADERS_DIR}/lmpgitversion.h") diff --git a/src/Makefile b/src/Makefile index 058a415238..f720abe6ec 100644 --- a/src/Makefile +++ b/src/Makefile @@ -171,13 +171,21 @@ gitversion: @echo 'Gathering git version information' @echo '#ifndef LMP_GIT_VERSION_H' > ${TMPNAME}.lmpgitversion @echo '#define LMP_GIT_VERSION_H' >> ${TMPNAME}.lmpgitversion - @echo 'const char LAMMPS_NS::LAMMPS::git_version[] = ' >> ${TMPNAME}.lmpgitversion @if (type git && git describe HEAD ) >> /dev/null 2>> /dev/null ; then \ - export v1=$$(git rev-parse --abbrev-ref HEAD); export v2=$$(git describe HEAD); \ - echo "\"$${v1} / $${v2}\";" >> ${TMPNAME}.lmpgitversion ; \ + git='true'; \ + commit=$$(git rev-parse HEAD); \ + branch=$$(git rev-parse --abbrev-ref HEAD); \ + describe=$$(git describe --dirty=-modified); \ else \ - echo '"";' >> ${TMPNAME}.lmpgitversion ; \ - fi + git='false' ; \ + commit='(unknown)' ; \ + branch='(unknown)' ; \ + describe='(unknown)' ; \ + fi ; \ + echo "const bool LAMMPS_NS::LAMMPS::has_git_info = $${git};" >> ${TMPNAME}.lmpgitversion ; \ + echo "const char LAMMPS_NS::LAMMPS::git_commit[] = \"$${commit}\";" >> ${TMPNAME}.lmpgitversion ; \ + echo "const char LAMMPS_NS::LAMMPS::git_branch[] = \"$${branch}\";" >> ${TMPNAME}.lmpgitversion ; \ + echo "const char LAMMPS_NS::LAMMPS::git_descriptor[] = \"$${describe}\";" >> ${TMPNAME}.lmpgitversion @echo '#endif' >> ${TMPNAME}.lmpgitversion @if [ -f lmpgitversion.h ]; \ then test "`diff --brief ${TMPNAME}.lmpgitversion lmpgitversion.h`" != "" && \ diff --git a/src/info.cpp b/src/info.cpp index f17a00b8d2..3d8a8d7b9e 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -260,9 +260,10 @@ void Info::command(int narg, char **arg) fprintf(out,"Printed on %s\n",ctime(&now)); if (flags & CONFIG) { - if (strlen(lmp->git_version) > 0) { - fprintf(out,"\nLAMMPS version: %s / %s\nGit revision: %s\n\n", - universe->version, universe->num_ver,lmp->git_version); + if (lmp->has_git_info) { + fprintf(out,"\nLAMMPS version: %s / %s\nGit info: %s / %s / %s\n\n", + universe->version, universe->num_ver,lmp->git_branch, + lmp->git_descriptor,lmp->git_commit); } else { fprintf(out,"\nLAMMPS version: %s / %s\n\n", universe->version, universe->num_ver); diff --git a/src/lammps.cpp b/src/lammps.cpp index 126a88b3e1..2b3f001b09 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -419,10 +419,6 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if ((universe->me == 0) && !helpflag) { if (screen) fprintf(screen,"LAMMPS (%s)\n",universe->version); if (logfile) fprintf(logfile,"LAMMPS (%s)\n",universe->version); - if (strlen(git_version) > 0) { - if (screen) fprintf(screen,"Git revision (%s)\n",git_version); - if (logfile) fprintf(logfile,"Git revision (%s)\n",git_version); - } } // universe is one or more worlds, as setup by partition switch @@ -497,15 +493,11 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if ((universe->me == 0) && (!helpflag)) { if (universe->uscreen) { fprintf(universe->uscreen,"LAMMPS (%s)\n",universe->version); - if (strlen(git_version) > 0) - fprintf(universe->uscreen,"Git revision (%s)\n",git_version); fprintf(universe->uscreen,"Running on %d partitions of processors\n", universe->nworlds); } if (universe->ulogfile) { fprintf(universe->ulogfile,"LAMMPS (%s)\n",universe->version); - if (strlen(git_version) > 0) - fprintf(universe->ulogfile,"Git revision (%s)\n",git_version); fprintf(universe->ulogfile,"Running on %d partitions of processors\n", universe->nworlds); } @@ -514,14 +506,10 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if ((me == 0) && (!helpflag)) { if (screen) { fprintf(screen,"LAMMPS (%s)\n",universe->version); - if (strlen(git_version) > 0) - fprintf(screen,"Git revision (%s)\n",git_version); fprintf(screen,"Processor partition = %d\n",universe->iworld); } if (logfile) { fprintf(logfile,"LAMMPS (%s)\n",universe->version); - if (strlen(git_version) > 0) - fprintf(logfile,"Git revision (%s)\n",git_version); fprintf(logfile,"Processor partition = %d\n",universe->iworld); } } @@ -911,13 +899,11 @@ void LAMMPS::help() // general help message about command line and flags - if (strlen(git_version) > 0) { - fprintf(fp, - "\nLarge-scale Atomic/Molecular Massively Parallel Simulator - " - LAMMPS_VERSION "\nGit revision (%s)\n\n",git_version); + if (has_git_info) { + fprintf(fp,"\nLarge-scale Atomic/Molecular Massively Parallel Simulator - " + LAMMPS_VERSION "\nGit info (%s / %s)\n\n",git_branch, git_descriptor); } else { - fprintf(fp, - "\nLarge-scale Atomic/Molecular Massively Parallel Simulator - " + fprintf(fp,"\nLarge-scale Atomic/Molecular Massively Parallel Simulator - " LAMMPS_VERSION "\n\n"); } fprintf(fp, diff --git a/src/lammps.h b/src/lammps.h index 7ae7ab7ecb..2e052e5ed2 100644 --- a/src/lammps.h +++ b/src/lammps.h @@ -64,7 +64,11 @@ class LAMMPS { class CiteMe *citeme; // citation info static const char * installed_packages[]; - static const char git_version[]; + + static const bool has_git_info; + static const char git_commit[]; + static const char git_branch[]; + static const char git_descriptor[]; LAMMPS(int, char **, MPI_Comm); ~LAMMPS();