Skip to content

Commit 8653aca

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

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)
@@ -183,77 +181,56 @@ llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
183181
common.o: examples/common.cpp examples/common.h
184182
$(CXX) $(CXXFLAGS) -c $< -o $@
185183

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

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

192190
#
193191
# Examples
194192
#
195193

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

206-
TARGETS_CPP += quantize-stats
207-
DEPS_quantize-stats := examples/quantize-stats/quantize-stats.cpp ggml.o llama.o
200+
quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
201+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
208202

209-
TARGETS_CPP += perplexity
210-
DEPS_perplexity := examples/perplexity/perplexity.cpp ggml.o llama.o common.o
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)
211205

212-
TARGETS_CPP += embedding
213-
DEPS_embedding := examples/embedding/embedding.cpp ggml.o llama.o common.o
206+
perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
207+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
214208

215-
TARGETS_CPP += save-load-state
216-
DEPS_save-load-state := examples/save-load-state/save-load-state.cpp ggml.o llama.o common.o
209+
embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
210+
$(CXX) $(CXXFLAGS) $(filter-out *.h,$^) -o $@ $(LDFLAGS)
217211

218-
TARGETS_CPP += vdot
219-
DEPS_vdot := pocs/vdot/vdot.cpp ggml.o
220-
221-
#
222-
# libllama
223-
#
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)
224214

225-
libllama.so: llama.o ggml.o $(OBJS)
226-
$(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
227222

228223
#
229224
# Tests
230225
#
231226

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

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