Skip to content

Commit b1dc97b

Browse files
committed
Short hash, less fancy Makefile, and don't modify build-info.h if it wouldn't change it
1 parent 5b98702 commit b1dc97b

File tree

4 files changed

+69
-76
lines changed

4 files changed

+69
-76
lines changed

CMakeLists.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,25 @@ option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
7676
# Build info header
7777
#
7878

79-
# Generate build-info.h initially
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
8091
include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake)
8192

8293
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
83-
# Add a custom target to regenerate build-info.h when .git/index changes
94+
# Add a custom target for build-info.h
8495
add_custom_target(BUILD_INFO ALL DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h")
8596

86-
# Add a custom command to generate build-info.h when .git/index changes
97+
# Add a custom command to rebuild build-info.h when .git/index changes
8798
add_custom_command(
8899
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h"
89100
COMMENT "Generating build details from Git"

Makefile

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ endif
1616
CCV := $(shell $(CC) --version | head -n 1)
1717
CXXV := $(shell $(CXX) --version | head -n 1)
1818

19-
GIT_INDEX = $(wildcard .git/index)
20-
2119
# Mac OS + Arm can report x86_64
2220
# ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
2321
ifeq ($(UNAME_S),Darwin)
@@ -181,77 +179,56 @@ llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
181179
common.o: examples/common.cpp examples/common.h
182180
$(CXX) $(CXXFLAGS) -c $< -o $@
183181

184-
clean:
185-
rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-q4_0-matmult build-info.h
182+
libllama.so: llama.o ggml.o $(OBJS)
183+
$(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
186184

187-
build-info.h: $(GIT_INDEX)
188-
scripts/build-info.sh > $@
185+
clean:
186+
rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-q4_0-matmult save-load-state build-info.h
189187

190188
#
191189
# Examples
192190
#
193191

194-
TARGETS_CPP += main
195-
DEPS_main := examples/main/main.cpp ggml.o llama.o common.o
196-
EXEC_main :=\
197-
@echo;\
198-
echo "==== Run ./main -h for help. ====";\
199-
echo
200-
201-
TARGETS_CPP += quantize
202-
DEPS_quantize := examples/quantize/quantize.cpp ggml.o llama.o
192+
main: examples/main/main.cpp build-info.h ggml.o llama.o common.o $(OBJS)
193+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
194+
@echo
195+
@echo '==== Run ./main -h for help. ===='
196+
@echo
203197

204-
TARGETS_CPP += quantize-stats
205-
DEPS_quantize-stats := examples/quantize-stats/quantize-stats.cpp ggml.o llama.o
198+
quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
199+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
206200

207-
TARGETS_CPP += perplexity
208-
DEPS_perplexity := examples/perplexity/perplexity.cpp ggml.o llama.o common.o
201+
quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
202+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
209203

210-
TARGETS_CPP += embedding
211-
DEPS_embedding := examples/embedding/embedding.cpp ggml.o llama.o common.o
204+
perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
205+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
212206

213-
TARGETS_CPP += save-load-state
214-
DEPS_save-load-state := examples/save-load-state/save-load-state.cpp ggml.o llama.o common.o
207+
embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
208+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
215209

216-
TARGETS_CPP += vdot
217-
DEPS_vdot := pocs/vdot/vdot.cpp ggml.o
218-
219-
#
220-
# libllama
221-
#
210+
save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
211+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
222212

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

226221
#
227222
# Tests
228223
#
229224

230-
TARGETS_CPP += benchmark
231-
DEPS_benchmark := examples/benchmark/benchmark-q4_0-matmult.c build-info.h ggml.o $(OBJS)
232-
OUTP_benchmark := benchmark-q4_0-matmult
233-
EXEC_benchmark := ./benchmark-q4_0-matmult
225+
benchmark: examples/benchmark/benchmark-q4_0-matmult.c build-info.h ggml.o $(OBJS)
226+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o benchmark-q4_0-matmult $(LDFLAGS)
227+
./benchmark-q4_0-matmult
228+
229+
vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
230+
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
234231

235232
.PHONY: tests
236233
tests:
237234
bash ./tests/run-tests.sh
238-
239-
#
240-
# Templates
241-
#
242-
243-
# C++ template
244-
# To use this template:
245-
# 1. Add your target to the TARGETS variable: TARGETS_CPP += target
246-
# 2. Set target-specific dependencies: DEPS_target := source1 dependency1 dependency2 ...
247-
# 3. Optionally, set target-specific output: OUTP_target := output_name
248-
# 4. Optionally, set target-specific command: EXEC_target := command
249-
define template_cpp
250-
OUTP_$(1) ?= $(1)
251-
$(1): $$(DEPS_$(1)) $$(OBJS) build-info.h
252-
$$(CXX) $$(CXXFLAGS) $$(filter-out build-info.h,$$^) -o $$(OUTP_$(1)) $$(LDFLAGS)
253-
$$(if $$(value EXEC_$(1)),$$(EXEC_$(1)))
254-
endef
255-
256-
# This iterates through TARGETS_CPP and call the template for each target
257-
$(foreach target,$(TARGETS_CPP),$(eval $(call template_cpp,$(target))))

scripts/build-info.cmake

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
set(HEAD "unknown")
2-
set(COUNT 0)
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")
35

6+
# Look for git
47
find_package(Git)
58
if(NOT Git_FOUND)
69
execute_process(
@@ -16,33 +19,35 @@ if(NOT Git_FOUND)
1619
endif()
1720
endif()
1821

22+
# Get the commit count and hash
1923
if(Git_FOUND)
2024
execute_process(
21-
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
25+
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
2226
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
23-
OUTPUT_VARIABLE TEMP_HEAD
27+
OUTPUT_VARIABLE HEAD
2428
OUTPUT_STRIP_TRAILING_WHITESPACE
2529
RESULT_VARIABLE GIT_HEAD_RESULT
2630
)
2731
execute_process(
2832
COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
2933
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
30-
OUTPUT_VARIABLE TEMP_COUNT
34+
OUTPUT_VARIABLE COUNT
3135
OUTPUT_STRIP_TRAILING_WHITESPACE
3236
RESULT_VARIABLE GIT_COUNT_RESULT
3337
)
3438
if(GIT_HEAD_RESULT EQUAL 0 AND GIT_COUNT_RESULT EQUAL 0)
35-
set(HEAD ${TEMP_HEAD})
36-
set(COUNT ${TEMP_COUNT})
39+
set(BUILD_COMMIT ${HEAD})
40+
set(BUILD_NUMBER ${COUNT})
3741
endif()
3842
endif()
3943

40-
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h" "\
41-
#ifndef BUILD_INFO_H\n\
42-
#define BUILD_INFO_H\n\
43-
\n\
44-
#define BUILD_NUMBER ${COUNT}\n\
45-
#define BUILD_COMMIT \"${HEAD}\"\n\
46-
\n\
47-
#endif // BUILD_INFO_H\n\
48-
")
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()

scripts/build-info.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
BUILD_NUMBER="0"
44
BUILD_COMMIT="unknown"
55

6-
REV_LIST=$(git rev-list HEAD --count)
6+
REV_LIST=$(git rev-list --count HEAD)
77
if [ $? -eq 0 ]; then
88
BUILD_NUMBER=$REV_LIST
99
fi
1010

11-
REV_PARSE=$(git rev-parse HEAD)
11+
REV_PARSE=$(git rev-parse --short HEAD)
1212
if [ $? -eq 0 ]; then
1313
BUILD_COMMIT=$REV_PARSE
1414
fi

0 commit comments

Comments
 (0)