From f4e7febf228532d95bf085bc7a8961612d777c4a Mon Sep 17 00:00:00 2001 From: Dhruba Borthakur Date: Tue, 21 Aug 2012 00:33:21 -0700 Subject: [PATCH] Record the version of the source repository that was used to build the leveldb library. Summary: Record the version of the source that we are compiling. We keep a record of the git revision in util/version.cc. This source file is then built as a regular source file as part of the compilation process. One can run "strings executable_filename | grep _build_" to find the version of the source that we used to build the executable file. Test Plan: none Differential Revision: https://reviews.facebook.net/D4785 --- Makefile | 8 ++++++-- build_detect_platform | 2 ++ build_detect_version | 25 +++++++++++++++++++++++++ db/db_impl.cc | 10 ++++++++++ util/build_version.h | 14 ++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100755 build_detect_version create mode 100644 util/build_version.h diff --git a/Makefile b/Makefile index d937bce50..65600739d 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,7 @@ TOOLS = \ PROGRAMS = db_bench $(TESTS) $(TOOLS) BENCHMARKS = db_bench_sqlite3 db_bench_tree_db +VERSIONFILE=util/build_version.cc LIBRARY = libleveldb.a MEMENVLIBRARY = libmemenv.a @@ -83,13 +84,13 @@ $(SHARED1): $(SHARED3) ln -fs $(SHARED3) $(SHARED1) endif -all: $(SHARED) $(LIBRARY) $(THRIFTSERVER) +all: $(VERSIONFILE) $(SHARED) $(LIBRARY) $(THRIFTSERVER) check: all $(PROGRAMS) $(TESTS) $(TOOLS) for t in $(TESTS); do echo "***** Running $$t"; ./$$t || exit 1; done clean: - -rm -f $(PROGRAMS) $(BENCHMARKS) $(LIBRARY) $(SHARED) $(MEMENVLIBRARY) $(THRIFTSERVER) */*.o */*/*.o ios-x86/*/*.o ios-arm/*/*.o build_config.mk + -rm -f $(PROGRAMS) $(BENCHMARKS) $(LIBRARY) $(SHARED) $(MEMENVLIBRARY) $(THRIFTSERVER) */*.o */*/*.o ios-x86/*/*.o ios-arm/*/*.o build_config.mk $(VERSIONFILE) -rm -rf ios-x86/* ios-arm/* $(LIBRARY): $(LIBOBJECTS) @@ -178,6 +179,9 @@ manifest_dump: tools/manifest_dump.o $(LIBOBJECTS) filelock_test: util/filelock_test.o $(LIBOBJECTS) $(TESTHARNESS) $(CXX) util/filelock_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@ $(LDFLAGS) +# recreate the version file with the latest git revision +$(VERSIONFILE): build_detect_version + $(shell ./build_detect_platform build_config.mk) ifeq ($(PLATFORM), IOS) # For iOS, create universal object files to be used on both the simulator and diff --git a/build_detect_platform b/build_detect_platform index dfd598d3d..ce33e2c92 100755 --- a/build_detect_platform +++ b/build_detect_platform @@ -96,6 +96,8 @@ case "$TARGET_OS" in exit 1 esac +./build_detect_version + # We want to make a list of all cc files within util, db, table, and helpers # except for the test and benchmark files. By default, find will output a list # of all files matching either rule, so we need to append -print to make the diff --git a/build_detect_version b/build_detect_version new file mode 100755 index 000000000..ab56eadc6 --- /dev/null +++ b/build_detect_version @@ -0,0 +1,25 @@ +#!/bin/sh +# +# Record the version of the source that we are compiling. +# We keep a record of the git revision in util/version.cc. This source file +# is then built as a regular source file as part of the compilation process. +# One can run "strings executable_filename | grep _build_" to find the version of +# the source that we used to build the executable file. +# + +# create git version file +VFILE=util/build_version.cc + +# check to see if git is in the path +which git > /dev/null + +if [ "$?" = 0 ]; then + git rev-parse HEAD | awk ' BEGIN {print "#include \"build_version.h\""} {print "const char * leveldb_build_git_sha = \"leveldb_build_git_sha:" $0"\";"} END {}' > ${VFILE} +else + echo "git not found"| awk ' BEGIN {print "#include \"build_version.h\""} {print "const char * leveldb_build_git_sha = \"leveldb_build_git_sha:git not found\";"} END {}' > ${VFILE} +fi + +date | awk 'BEGIN {} {print "const char * leveldb_build_git_datetime = \"leveldb_build_git_datetime:"$0"\";"} END {} ' >> ${VFILE} +echo "const char * leveldb_build_compile_date = __DATE__;" >> ${VFILE} +echo "const char * leveldb_build_compile_time = __TIME__;" >> ${VFILE} + diff --git a/db/db_impl.cc b/db/db_impl.cc index c4caae14c..1b5acb4bf 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -33,6 +33,7 @@ #include "util/coding.h" #include "util/logging.h" #include "util/mutexlock.h" +#include "util/build_version.h" namespace leveldb { @@ -1544,4 +1545,13 @@ Status DestroyDB(const std::string& dbname, const Options& options) { return result; } +// +// A global method that can dump out the build version +void printLeveldbBuildVersion() { + printf("Git sha %s", leveldb_build_git_sha); + printf("Git datetime %s", leveldb_build_git_datetime); + printf("Compile time %s", leveldb_build_compile_time); + printf("Compile date %s", leveldb_build_compile_date); +} + } // namespace leveldb diff --git a/util/build_version.h b/util/build_version.h new file mode 100644 index 000000000..bcf869372 --- /dev/null +++ b/util/build_version.h @@ -0,0 +1,14 @@ +/*version.h*/ +#ifndef VERSION_H_ +#define VERSION_H_ + +// these variables tell us about the git config and time +extern const char* leveldb_build_git_sha; +extern const char* leveldb_build_git_datetime; + +// these variables tell us when the compilation occured +extern const char* leveldb_build_compile_time; +extern const char* leveldb_build_compile_date; + + +#endif /* VERSION_H_ */