Skip to content

Commit 40aec68

Browse files
committed
Broke out build-info.cmake, added find_package fallback, and added build into to all examples
1 parent c0a3acc commit 40aec68

File tree

9 files changed

+75
-38
lines changed

9 files changed

+75
-38
lines changed

CMakeLists.txt

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

79-
file(WRITE ${CMAKE_BINARY_DIR}/BUILD_INFO.cmake "\
80-
set(HEAD \"unknown\")
81-
set(COUNT 0)
82-
83-
find_package(Git)
84-
if(Git_FOUND)
85-
execute_process(
86-
COMMAND \${GIT_EXECUTABLE} rev-parse HEAD
87-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
88-
OUTPUT_VARIABLE TEMP_HEAD
89-
OUTPUT_STRIP_TRAILING_WHITESPACE
90-
RESULT_VARIABLE GIT_HEAD_RESULT
91-
)
92-
execute_process(
93-
COMMAND \${GIT_EXECUTABLE} rev-list --count HEAD
94-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
95-
OUTPUT_VARIABLE TEMP_COUNT
96-
OUTPUT_STRIP_TRAILING_WHITESPACE
97-
RESULT_VARIABLE GIT_COUNT_RESULT
98-
)
99-
if(GIT_HEAD_RESULT EQUAL 0 AND GIT_COUNT_RESULT EQUAL 0)
100-
set(HEAD \${TEMP_HEAD})
101-
set(COUNT \${TEMP_COUNT})
102-
endif()
103-
endif()
104-
105-
file(WRITE \"${CMAKE_CURRENT_SOURCE_DIR}/build-info.h\" \"#pragma once\\n#define BUILD_NUMBER \${COUNT}\\n#define BUILD_COMMIT \\\"\${HEAD}\\\"\\n\")
106-
")
107-
108-
# Call the script to generate build-info.h initially
109-
execute_process(
110-
COMMAND ${CMAKE_COMMAND} -DGIT_EXECUTABLE=${GIT_EXECUTABLE} -P ${CMAKE_BINARY_DIR}/BUILD_INFO.cmake
111-
)
79+
# Generate build-info.h initially
80+
include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake)
11281

11382
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
11483
# Add a custom target to regenerate build-info.h when .git/index changes
@@ -118,10 +87,13 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
11887
add_custom_command(
11988
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h"
12089
COMMENT "Generating build details from Git"
121-
COMMAND ${CMAKE_COMMAND} -DGIT_EXECUTABLE=${GIT_EXECUTABLE} -P ${CMAKE_BINARY_DIR}/BUILD_INFO.cmake
90+
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake"
91+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
12292
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/.git/index"
12393
VERBATIM
12494
)
95+
else()
96+
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.")
12597
endif()
12698

12799
#

examples/benchmark/benchmark-q4_0-matmult.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <locale.h>
1010
#include "ggml.h"
11+
#include "build-info.h"
1112
#include <assert.h>
1213
#include <math.h>
1314
#include <cstring>
@@ -98,6 +99,7 @@ int main(int argc, char ** argv) {
9899
}
99100
}
100101

102+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
101103

102104
// create the ggml context
103105
printf("Starting Test\n");

examples/embedding/embedding.cpp

Lines changed: 4 additions & 1 deletion
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) {

examples/main/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ int main(int argc, char ** argv) {
5353
return 1;
5454
}
5555

56-
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
57-
5856
// save choice to use color for later
5957
// (note for later: this is a slightly awkward choice)
6058
con_st.use_color = params.use_color;
@@ -84,6 +82,8 @@ int main(int argc, char ** argv) {
8482
"expect poor results\n", __func__, params.n_ctx);
8583
}
8684

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

examples/perplexity/perplexity.cpp

Lines changed: 4 additions & 1 deletion
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) {

examples/quantize-stats/quantize-stats.cpp

Lines changed: 3 additions & 0 deletions
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

examples/quantize/quantize.cpp

Lines changed: 3 additions & 0 deletions
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();

examples/save-load-state/save-load-state.cpp

Lines changed: 3 additions & 0 deletions
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
}

scripts/build-info.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
set(HEAD "unknown")
2+
set(COUNT 0)
3+
4+
find_package(Git)
5+
if(NOT Git_FOUND)
6+
execute_process(
7+
COMMAND which git
8+
OUTPUT_VARIABLE GIT_EXECUTABLE
9+
OUTPUT_STRIP_TRAILING_WHITESPACE
10+
)
11+
if(NOT GIT_EXECUTABLE STREQUAL "")
12+
set(Git_FOUND TRUE)
13+
message(STATUS "Found Git using 'which': ${GIT_EXECUTABLE}")
14+
else()
15+
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.")
16+
endif()
17+
endif()
18+
19+
if(Git_FOUND)
20+
execute_process(
21+
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
22+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
23+
OUTPUT_VARIABLE TEMP_HEAD
24+
OUTPUT_STRIP_TRAILING_WHITESPACE
25+
RESULT_VARIABLE GIT_HEAD_RESULT
26+
)
27+
execute_process(
28+
COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
29+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
30+
OUTPUT_VARIABLE TEMP_COUNT
31+
OUTPUT_STRIP_TRAILING_WHITESPACE
32+
RESULT_VARIABLE GIT_COUNT_RESULT
33+
)
34+
if(GIT_HEAD_RESULT EQUAL 0 AND GIT_COUNT_RESULT EQUAL 0)
35+
set(HEAD ${TEMP_HEAD})
36+
set(COUNT ${TEMP_COUNT})
37+
endif()
38+
endif()
39+
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+
")

0 commit comments

Comments
 (0)