Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 75b3719

Browse files
matteosalszha
andauthored
[FEATURE] Hardcode build-time branch and commit hash into the library (#20755)
* Embed commit hash and branch into library * set git branch and commit from CMake * Hook up new functions to python * Remove old system * Use GIT_EXECUTABLE * Fix script formatting * Linter fixes * Move python functions * Switch between old and new system in diagnose.py * Update exception types Co-authored-by: Sheng Zha <szha@users.noreply.github.com>
1 parent a7ac1c8 commit 75b3719

7 files changed

Lines changed: 84 additions & 10 deletions

File tree

CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,32 @@ message(STATUS "CMAKE_SYSTEM_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR}")
102102

103103
message(STATUS "CMAKE_SYSTEM_NAME ${CMAKE_SYSTEM_NAME}")
104104

105+
find_package(Git QUIET)
106+
if(${GIT_FOUND})
107+
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
108+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
109+
OUTPUT_VARIABLE GIT_BRANCH
110+
RESULT_VARIABLE BRANCH_FAILED
111+
)
112+
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
113+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
114+
OUTPUT_VARIABLE GIT_COMMIT
115+
RESULT_VARIABLE COMMIT_FAILED
116+
)
117+
if(NOT BRANCH_FAILED)
118+
string(REGEX REPLACE "\n$" "" GIT_BRANCH "${GIT_BRANCH}")
119+
add_compile_definitions(MXNET_BRANCH="${GIT_BRANCH}")
120+
else()
121+
add_compile_definitions(MXNET_BRANCH="Unavailable")
122+
endif()
123+
if(NOT COMMIT_FAILED)
124+
string(REGEX REPLACE "\n$" "" GIT_COMMIT "${GIT_COMMIT}")
125+
add_compile_definitions(MXNET_COMMIT_HASH="${GIT_COMMIT}")
126+
else()
127+
add_compile_definitions(MXNET_COMMIT_HASH="Unavailable")
128+
endif()
129+
endif()
130+
105131
if(USE_TVM_OP)
106132
add_definitions(-DMXNET_USE_TVM_OP=1)
107133
endif()

include/mxnet/c_api.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ extern "C" {
5353
#define MXNET_DLL
5454
#endif
5555

56+
#ifndef MXNET_BRANCH
57+
#define MXNET_BRANCH "NotProvided"
58+
#endif
59+
60+
#ifndef MXNET_COMMIT_HASH
61+
#define MXNET_COMMIT_HASH "NotProvided"
62+
#endif
63+
5664
/*! \brief manually define unsigned int */
5765
typedef uint32_t mx_uint;
5866
/*! \brief manually define float */
@@ -542,6 +550,20 @@ MXNET_DLL int MXGetGPUMemoryInformation64(int dev, uint64_t* free_mem, uint64_t*
542550
*/
543551
MXNET_DLL int MXGetVersion(int* out);
544552

553+
/*!
554+
* \brief get the MXNet library branch at build time, usually provided by cmake
555+
* \param pointer to the string holding the branch name
556+
* \return 0 when success, -1 when failure happens
557+
*/
558+
MXNET_DLL int MXGetBranch(const char** out);
559+
560+
/*!
561+
* \brief get the MXNet library commit hash at build time, usually provided by cmake
562+
* \param pointer to the string holding the commit hash
563+
* \return 0 when success, -1 when failure happens
564+
*/
565+
MXNET_DLL int MXGetCommitHash(const char** out);
566+
545567
/*!
546568
* \brief Load TVM operator from the binary library
547569
* \param libpath TVM operators lib file

python/mxnet/runtime.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,13 @@ def is_enabled(self, feature_name):
116116
raise RuntimeError("Feature '{}' is unknown, known features are: {}".format(
117117
feature_name, list(self.keys())))
118118
return self[feature_name].enabled
119+
120+
def get_branch():
121+
out = ctypes.c_char_p()
122+
check_call(_LIB.MXGetBranch(ctypes.byref(out)))
123+
return out.value.decode('utf-8')
124+
125+
def get_commit_hash():
126+
out = ctypes.c_char_p()
127+
check_call(_LIB.MXGetCommitHash(ctypes.byref(out)))
128+
return out.value.decode('utf-8')

src/c_api/c_api.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,18 @@ int MXGetVersion(int* out) {
19591959
API_END();
19601960
}
19611961

1962+
int MXGetBranch(const char** out) {
1963+
API_BEGIN();
1964+
*out = MXNET_BRANCH;
1965+
API_END();
1966+
}
1967+
1968+
int MXGetCommitHash(const char** out) {
1969+
API_BEGIN();
1970+
*out = MXNET_COMMIT_HASH;
1971+
API_END();
1972+
}
1973+
19621974
#if MXNET_USE_TVM_OP
19631975
int MXLoadTVMOp(const char* libpath) {
19641976
API_BEGIN();

tools/diagnose.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,24 @@ def check_mxnet():
9696
print('Version :', mxnet.__version__)
9797
mx_dir = os.path.dirname(mxnet.__file__)
9898
print('Directory :', mx_dir)
99-
commit_hash = os.path.join(mx_dir, 'COMMIT_HASH')
100-
if os.path.exists(commit_hash):
101-
with open(commit_hash, 'r') as f:
102-
ch = f.read().strip()
103-
print('Commit Hash :', ch)
104-
else:
105-
print('Commit hash file "{}" not found. Not installed from pre-built package or built from source.'.format(commit_hash))
99+
try:
100+
branch = mxnet.runtime.get_branch()
101+
commit_hash = mxnet.runtime.get_commit_hash()
102+
print('Branch :', branch)
103+
print('Commit Hash :', commit_hash)
104+
except AttributeError:
105+
commit_hash = os.path.join(mx_dir, 'COMMIT_HASH')
106+
if os.path.exists(commit_hash):
107+
with open(commit_hash, 'r') as f:
108+
ch = f.read().strip()
109+
print('Commit Hash :', ch)
110+
else:
111+
print('Commit hash file "{}" not found. Not installed from pre-built package or built from source.'.format(commit_hash))
106112
print('Library :', mxnet.libinfo.find_lib_path())
107113
try:
108114
print('Build features:')
109115
print(get_build_features_str())
110-
except Exception:
116+
except (AttributeError, ModuleNotFoundError):
111117
print('No runtime build feature info available')
112118
except ImportError:
113119
print('No MXNet installed.')

tools/pip/MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ include README
1919
include LICENSE
2020
include DISCLAIMER
2121
include NOTICE
22-
include mxnet/COMMIT_HASH
2322
recursive-include mxnet/tools *
2423
recursive-include mxnet *.py
2524
recursive-include mxnet *.so

tools/staticbuild/build_wheel.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
# under the License.
1919

2020
# This script builds the wheel for binary distribution and performs sanity check.
21-
echo $(git rev-parse HEAD) >> python/mxnet/COMMIT_HASH
2221
cd python/
2322

2423
# Make wheel for testing

0 commit comments

Comments
 (0)