Skip to content

Commit f4cef87

Browse files
authoredMay 1, 2023
Add git-based build information for better issue tracking (LostRuins#1232)
* Add git-based build information for better issue tracking * macOS fix * "build (hash)" and "CMAKE_SOURCE_DIR" changes * Redo "CMAKE_CURRENT_SOURCE_DIR" and clearer build messages * Fix conditional dependency on missing target * Broke out build-info.cmake, added find_package fallback, and added build into to all examples, added dependencies to Makefile * 4 space indenting for cmake, attempt to clean up my mess in Makefile * Short hash, less fancy Makefile, and don't modify build-info.h if it wouldn't change it
1 parent 58b367c commit f4cef87

18 files changed

+185
-21
lines changed
 

Diff for: ‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ models/*
3232
/vdot
3333
/Pipfile
3434

35+
build-info.h
3536
arm_neon.h
3637
compile_commands.json
3738

Diff for: ‎CMakeLists.txt

+35
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,41 @@ option(LLAMA_CLBLAST "llama: use CLBlast"
7272
option(LLAMA_BUILD_TESTS "llama: build tests" ${LLAMA_STANDALONE})
7373
option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
7474

75+
#
76+
# Build info header
77+
#
78+
79+
# Write header template to binary dir to keep source directory clean
80+
file(WRITE "${CMAKE_BINARY_DIR}/BUILD_INFO.h.in" "\
81+
#ifndef BUILD_INFO_H\n\
82+
#define BUILD_INFO_H\n\
83+
\n\
84+
#define BUILD_NUMBER @BUILD_NUMBER@\n\
85+
#define BUILD_COMMIT \"@BUILD_COMMIT@\"\n\
86+
\n\
87+
#endif // BUILD_INFO_H\n\
88+
")
89+
90+
# Generate initial build-info.h
91+
include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake)
92+
93+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
94+
# Add a custom target for build-info.h
95+
add_custom_target(BUILD_INFO ALL DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h")
96+
97+
# Add a custom command to rebuild build-info.h when .git/index changes
98+
add_custom_command(
99+
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h"
100+
COMMENT "Generating build details from Git"
101+
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake"
102+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
103+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/.git/index"
104+
VERBATIM
105+
)
106+
else()
107+
message(WARNING "Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository.")
108+
endif()
109+
75110
#
76111
# Compile flags
77112
#

Diff for: ‎Makefile

+32-17
Original file line numberDiff line numberDiff line change
@@ -181,41 +181,56 @@ llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
181181
common.o: examples/common.cpp examples/common.h
182182
$(CXX) $(CXXFLAGS) -c $< -o $@
183183

184+
libllama.so: llama.o ggml.o $(OBJS)
185+
$(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
186+
184187
clean:
185-
rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-matmult
188+
rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state build-info.h
186189

187-
main: examples/main/main.cpp ggml.o llama.o common.o $(OBJS)
188-
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
190+
#
191+
# Examples
192+
#
193+
194+
main: examples/main/main.cpp build-info.h ggml.o llama.o common.o $(OBJS)
195+
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
189196
@echo
190197
@echo '==== Run ./main -h for help. ===='
191198
@echo
192199

193-
quantize: examples/quantize/quantize.cpp ggml.o llama.o $(OBJS)
194-
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
200+
quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
201+
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
195202

196-
quantize-stats: examples/quantize-stats/quantize-stats.cpp ggml.o llama.o $(OBJS)
197-
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
203+
quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
204+
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
198205

199-
perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o common.o $(OBJS)
200-
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
206+
perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
207+
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
201208

202-
embedding: examples/embedding/embedding.cpp ggml.o llama.o common.o $(OBJS)
203-
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
209+
embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
210+
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
204211

205-
vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
206-
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
212+
save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
213+
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
207214

208-
libllama.so: llama.o ggml.o $(OBJS)
209-
$(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
215+
build-info.h: $(wildcard .git/index) scripts/build-info.sh
216+
@scripts/build-info.sh > $@.tmp
217+
@if ! cmp -s $@.tmp $@; then \
218+
mv $@.tmp $@; \
219+
else \
220+
rm $@.tmp; \
221+
fi
210222

211223
#
212224
# Tests
213225
#
214226

215-
benchmark-matmult: examples/benchmark/benchmark-matmult.cpp ggml.o $(OBJS)
216-
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
227+
benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
228+
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
217229
./$@
218230

231+
vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
232+
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
233+
219234
.PHONY: tests
220235
tests:
221236
bash ./tests/run-tests.sh

Diff for: ‎examples/benchmark/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ set(TARGET benchmark)
22
add_executable(${TARGET} benchmark-matmult.cpp)
33
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
44
target_compile_features(${TARGET} PRIVATE cxx_std_11)
5+
if(TARGET BUILD_INFO)
6+
add_dependencies(${TARGET} BUILD_INFO)
7+
endif()

Diff for: ‎examples/benchmark/benchmark-matmult.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <locale.h>
22
#include "ggml.h"
3+
#include "build-info.h"
34
#include <assert.h>
45
#include <math.h>
56
#include <cstring>
@@ -90,9 +91,10 @@ int main(int argc, char ** argv) {
9091
}
9192
}
9293

93-
// create the ggml context
94+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
9495
printf("Starting Test\n");
9596

97+
// create the ggml context
9698
struct ggml_context * ctx;
9799
//const int sizex = 4096;
98100
//const int sizey = 11008;

Diff for: ‎examples/embedding/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ set(TARGET embedding)
22
add_executable(${TARGET} embedding.cpp)
33
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
44
target_compile_features(${TARGET} PRIVATE cxx_std_11)
5+
if(TARGET BUILD_INFO)
6+
add_dependencies(${TARGET} BUILD_INFO)
7+
endif()

Diff for: ‎examples/embedding/embedding.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "common.h"
22
#include "llama.h"
3+
#include "build-info.h"
34

45
#include <ctime>
56

@@ -18,11 +19,13 @@ int main(int argc, char ** argv) {
1819
"expect poor results\n", __func__, params.n_ctx);
1920
}
2021

22+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
23+
2124
if (params.seed <= 0) {
2225
params.seed = time(NULL);
2326
}
2427

25-
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
28+
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
2629

2730
std::mt19937 rng(params.seed);
2831
if (params.random_prompt) {

Diff for: ‎examples/main/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ set(TARGET main)
22
add_executable(${TARGET} main.cpp)
33
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
44
target_compile_features(${TARGET} PRIVATE cxx_std_11)
5+
if(TARGET BUILD_INFO)
6+
add_dependencies(${TARGET} BUILD_INFO)
7+
endif()

Diff for: ‎examples/main/main.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "common.h"
77
#include "llama.h"
8+
#include "build-info.h"
89

910
#include <cassert>
1011
#include <cinttypes>
@@ -81,11 +82,13 @@ int main(int argc, char ** argv) {
8182
"expect poor results\n", __func__, params.n_ctx);
8283
}
8384

85+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
86+
8487
if (params.seed <= 0) {
8588
params.seed = time(NULL);
8689
}
8790

88-
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
91+
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
8992

9093
std::mt19937 rng(params.seed);
9194
if (params.random_prompt) {

Diff for: ‎examples/perplexity/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ set(TARGET perplexity)
22
add_executable(${TARGET} perplexity.cpp)
33
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
44
target_compile_features(${TARGET} PRIVATE cxx_std_11)
5+
if(TARGET BUILD_INFO)
6+
add_dependencies(${TARGET} BUILD_INFO)
7+
endif()

Diff for: ‎examples/perplexity/perplexity.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "common.h"
22
#include "llama.h"
3+
#include "build-info.h"
34

45
#include <cmath>
56
#include <ctime>
@@ -106,11 +107,13 @@ int main(int argc, char ** argv) {
106107
"expect poor results\n", __func__, params.n_ctx);
107108
}
108109

110+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
111+
109112
if (params.seed <= 0) {
110113
params.seed = time(NULL);
111114
}
112115

113-
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
116+
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
114117

115118
std::mt19937 rng(params.seed);
116119
if (params.random_prompt) {

Diff for: ‎examples/quantize-stats/quantize-stats.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ggml.h"
2+
#include "build-info.h"
23

34
#define LLAMA_API_INTERNAL
45
#include "llama.h"
@@ -308,6 +309,8 @@ int main(int argc, char ** argv) {
308309
return 1;
309310
}
310311

312+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
313+
311314
// load the model
312315
fprintf(stderr, "Loading model\n");
313316

Diff for: ‎examples/quantize/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ set(TARGET quantize)
22
add_executable(${TARGET} quantize.cpp)
33
target_link_libraries(${TARGET} PRIVATE llama ${CMAKE_THREAD_LIBS_INIT})
44
target_compile_features(${TARGET} PRIVATE cxx_std_11)
5+
if(TARGET BUILD_INFO)
6+
add_dependencies(${TARGET} BUILD_INFO)
7+
endif()

Diff for: ‎examples/quantize/quantize.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ggml.h"
22
#include "llama.h"
3+
#include "build-info.h"
34

45
#include <cstdio>
56
#include <map>
@@ -50,6 +51,8 @@ int main(int argc, char ** argv) {
5051
ftype = (enum llama_ftype)atoi(argv[3]);
5152
}
5253

54+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
55+
5356
int nthread = argc > 4 ? atoi(argv[4]) : 0;
5457

5558
const int64_t t_main_start_us = ggml_time_us();

Diff for: ‎examples/save-load-state/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ set(TARGET save-load-state)
22
add_executable(${TARGET} save-load-state.cpp)
33
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
44
target_compile_features(${TARGET} PRIVATE cxx_std_11)
5+
if(TARGET BUILD_INFO)
6+
add_dependencies(${TARGET} BUILD_INFO)
7+
endif()

Diff for: ‎examples/save-load-state/save-load-state.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "common.h"
22
#include "llama.h"
3+
#include "build-info.h"
34

45
#include <vector>
56
#include <cstdio>
@@ -17,6 +18,8 @@ int main(int argc, char ** argv) {
1718
return 1;
1819
}
1920

21+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
22+
2023
if (params.n_predict < 0) {
2124
params.n_predict = 16;
2225
}

Diff for: ‎scripts/build-info.cmake

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
set(TEMPLATE_FILE "${CMAKE_BINARY_DIR}/BUILD_INFO.h.in")
2+
set(HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h")
3+
set(BUILD_NUMBER 0)
4+
set(BUILD_COMMIT "unknown")
5+
6+
# Look for git
7+
find_package(Git)
8+
if(NOT Git_FOUND)
9+
execute_process(
10+
COMMAND which git
11+
OUTPUT_VARIABLE GIT_EXECUTABLE
12+
OUTPUT_STRIP_TRAILING_WHITESPACE
13+
)
14+
if(NOT GIT_EXECUTABLE STREQUAL "")
15+
set(Git_FOUND TRUE)
16+
message(STATUS "Found Git using 'which': ${GIT_EXECUTABLE}")
17+
else()
18+
message(WARNING "Git not found using 'find_package' or 'which'. Build info will not be accurate. Consider installing Git or ensuring it is in the PATH.")
19+
endif()
20+
endif()
21+
22+
# Get the commit count and hash
23+
if(Git_FOUND)
24+
execute_process(
25+
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
26+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
27+
OUTPUT_VARIABLE HEAD
28+
OUTPUT_STRIP_TRAILING_WHITESPACE
29+
RESULT_VARIABLE GIT_HEAD_RESULT
30+
)
31+
execute_process(
32+
COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
33+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
34+
OUTPUT_VARIABLE COUNT
35+
OUTPUT_STRIP_TRAILING_WHITESPACE
36+
RESULT_VARIABLE GIT_COUNT_RESULT
37+
)
38+
if(GIT_HEAD_RESULT EQUAL 0 AND GIT_COUNT_RESULT EQUAL 0)
39+
set(BUILD_COMMIT ${HEAD})
40+
set(BUILD_NUMBER ${COUNT})
41+
endif()
42+
endif()
43+
44+
# Only write the header if it's changed to prevent unnecessary recompilation
45+
if(EXISTS ${HEADER_FILE})
46+
file(STRINGS ${HEADER_FILE} CONTENTS REGEX "BUILD_COMMIT \"([^\"]*)\"")
47+
list(GET CONTENTS 0 EXISTING)
48+
if(NOT EXISTING STREQUAL "#define BUILD_COMMIT \"${BUILD_COMMIT}\"")
49+
configure_file(${TEMPLATE_FILE} ${HEADER_FILE})
50+
endif()
51+
else()
52+
configure_file(${TEMPLATE_FILE} ${HEADER_FILE})
53+
endif()

Diff for: ‎scripts/build-info.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
BUILD_NUMBER="0"
4+
BUILD_COMMIT="unknown"
5+
6+
REV_LIST=$(git rev-list --count HEAD)
7+
if [ $? -eq 0 ]; then
8+
BUILD_NUMBER=$REV_LIST
9+
fi
10+
11+
REV_PARSE=$(git rev-parse --short HEAD)
12+
if [ $? -eq 0 ]; then
13+
BUILD_COMMIT=$REV_PARSE
14+
fi
15+
16+
echo "#ifndef BUILD_INFO_H"
17+
echo "#define BUILD_INFO_H"
18+
echo ""
19+
echo "#define BUILD_NUMBER $BUILD_NUMBER"
20+
echo "#define BUILD_COMMIT \"$BUILD_COMMIT\""
21+
echo ""
22+
echo "#endif // BUILD_INFO_H"

0 commit comments

Comments
 (0)
Please sign in to comment.