Skip to content

Commit

Permalink
[29.0.6] add git version info
Browse files Browse the repository at this point in the history
  • Loading branch information
prife committed May 8, 2020
1 parent a1f8f8f commit b521793
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
cmake_minimum_required(VERSION 3.5.1)
project(my-adb)
project(adb)

# Add a custom command that produces version.cpp, plus
# a dummy output that's not actually produced, in order
# to force version.cmake to always be re-run before the build
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/version.cpp
${CMAKE_CURRENT_BINARY_DIR}/_version.cpp
COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_SOURCE_DIR}/version.cmake)
set(version_src ${CMAKE_CURRENT_BINARY_DIR}/version.cpp)

set(libadb_srcs
${version_src}
adb.cpp
adb_io.cpp
adb_listeners.cpp
Expand Down
14 changes: 14 additions & 0 deletions src/adb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,28 @@
using namespace std::chrono_literals;
#endif

#if !ADB_NON_ANDROID
extern "C" {
extern const char* GIT_BRANCH;
extern const char* GIT_REV;
extern const char* GIT_TAG;
}
#endif

std::string adb_version() {
// Don't change the format of this --- it's parsed by ddmlib.
return android::base::StringPrintf(
"Android Debug Bridge version %d.%d.%d\n"
"Version %s-%s\n"
#if !ADB_NON_ANDROID
"Wetest branch:%s rev:%s tag:%s\n"
#endif
"Installed as %s\n",
ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION,
PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str(),
#if !ADB_NON_ANDROID
GIT_BRANCH, GIT_REV, GIT_TAG,
#endif
android::base::GetExecutablePath().c_str());
}

Expand Down
43 changes: 43 additions & 0 deletions src/version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
execute_process(COMMAND git log --pretty=format:'%h' -n 1
OUTPUT_VARIABLE GIT_REV
ERROR_QUIET)

# Check whether we got any revision (which isn't
# always the case, e.g. when someone downloaded a zip
# file from Github instead of a checkout)
if ("${GIT_REV}" STREQUAL "")
set(GIT_REV "N/A")
set(GIT_DIFF "")
set(GIT_TAG "N/A")
set(GIT_BRANCH "N/A")
else()
execute_process(
COMMAND bash -c "git diff --quiet --exit-code || echo +"
OUTPUT_VARIABLE GIT_DIFF)
execute_process(
COMMAND git describe --exact-match --tags
OUTPUT_VARIABLE GIT_TAG ERROR_QUIET)
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
OUTPUT_VARIABLE GIT_BRANCH)

string(STRIP "${GIT_REV}" GIT_REV)
string(SUBSTRING "${GIT_REV}" 1 7 GIT_REV)
string(STRIP "${GIT_DIFF}" GIT_DIFF)
string(STRIP "${GIT_TAG}" GIT_TAG)
string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
endif()

set(VERSION "const char* GIT_REV=\"${GIT_REV}${GIT_DIFF}\";
const char* GIT_TAG=\"${GIT_TAG}\";
const char* GIT_BRANCH=\"${GIT_BRANCH}\";")

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp VERSION_)
else()
set(VERSION_ "")
endif()

if (NOT "${VERSION}" STREQUAL "${VERSION_}")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp "${VERSION}")
endif()

0 comments on commit b521793

Please sign in to comment.