Skip to content

Commit 6990e2f

Browse files
authored
libs : rename libcommon -> libllama-common (ggml-org#21936)
* cmake : allow libcommon to be shared * cmake : rename libcommon to libllama-common * cont : set -fPIC for httplib * cont : export all symbols * cont : fix build_info exports * libs : add libllama-common-base * log : add common_log_get_verbosity_thold()
1 parent fcc7508 commit 6990e2f

58 files changed

Lines changed: 184 additions & 108 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ foreach(FILE_PATH ${EXTRA_LICENSES})
225225
endforeach()
226226

227227
if (LLAMA_BUILD_COMMON)
228-
license_generate(common)
228+
license_generate(llama-common)
229229
endif()
230230

231231
#
@@ -249,6 +249,10 @@ set_target_properties(llama
249249

250250
install(TARGETS llama LIBRARY PUBLIC_HEADER)
251251

252+
if (LLAMA_BUILD_COMMON)
253+
install(TARGETS llama-common LIBRARY)
254+
endif()
255+
252256
configure_package_config_file(
253257
${CMAKE_CURRENT_SOURCE_DIR}/cmake/llama-config.cmake.in
254258
${CMAKE_CURRENT_BINARY_DIR}/llama-config.cmake

common/CMakeLists.txt

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# common
2-
31
find_package(Threads REQUIRED)
42

53
llama_add_compile_flags()
64

5+
#
6+
# llama-common-base
7+
#
8+
79
# Build info header
810

911
if(EXISTS "${PROJECT_SOURCE_DIR}/.git")
@@ -33,17 +35,25 @@ endif()
3335

3436
set(TEMPLATE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.cpp.in")
3537
set(OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/build-info.cpp")
38+
3639
configure_file(${TEMPLATE_FILE} ${OUTPUT_FILE})
3740

38-
set(TARGET build_info)
39-
add_library(${TARGET} OBJECT ${OUTPUT_FILE})
41+
set(TARGET llama-common-base)
42+
add_library(${TARGET} STATIC ${OUTPUT_FILE})
43+
44+
target_include_directories(${TARGET} PUBLIC .)
45+
4046
if (BUILD_SHARED_LIBS)
4147
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
4248
endif()
4349

44-
set(TARGET common)
50+
#
51+
# llama-common
52+
#
4553

46-
add_library(${TARGET} STATIC
54+
set(TARGET llama-common)
55+
56+
add_library(${TARGET}
4757
arg.cpp
4858
arg.h
4959
base64.hpp
@@ -106,17 +116,24 @@ add_library(${TARGET} STATIC
106116
jinja/caps.h
107117
)
108118

119+
set_target_properties(${TARGET} PROPERTIES
120+
VERSION ${LLAMA_INSTALL_VERSION}
121+
SOVERSION 0
122+
MACHO_CURRENT_VERSION 0 # keep macOS linker from seeing oversized version number
123+
)
124+
109125
target_include_directories(${TARGET} PUBLIC . ../vendor)
110126
target_compile_features (${TARGET} PUBLIC cxx_std_17)
111127

112128
if (BUILD_SHARED_LIBS)
113129
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
130+
131+
# TODO: make fine-grained exports in the future
132+
set_target_properties(${TARGET} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
114133
endif()
115134

116-
target_link_libraries(${TARGET} PRIVATE
117-
build_info
118-
cpp-httplib
119-
)
135+
target_link_libraries(${TARGET} PUBLIC llama-common-base)
136+
target_link_libraries(${TARGET} PRIVATE cpp-httplib)
120137

121138
if (LLAMA_LLGUIDANCE)
122139
include(ExternalProject)

common/arg.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "arg.h"
22

3+
#include "build-info.h"
34
#include "chat.h"
45
#include "common.h"
56
#include "download.h"
@@ -1044,8 +1045,8 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
10441045
{"--version"},
10451046
"show version and build info",
10461047
[](common_params &) {
1047-
fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
1048-
fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET);
1048+
fprintf(stderr, "version: %d (%s)\n", llama_build_number(), llama_commit());
1049+
fprintf(stderr, "built with %s for %s\n", llama_compiler(), llama_build_target());
10491050
exit(0);
10501051
}
10511052
));

common/build-info.cpp.in

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
1+
#include "build-info.h"
2+
3+
#include <cstdio>
4+
#include <string>
5+
16
int LLAMA_BUILD_NUMBER = @LLAMA_BUILD_NUMBER@;
2-
char const *LLAMA_COMMIT = "@LLAMA_BUILD_COMMIT@";
3-
char const *LLAMA_COMPILER = "@BUILD_COMPILER@";
4-
char const *LLAMA_BUILD_TARGET = "@BUILD_TARGET@";
7+
char const * LLAMA_COMMIT = "@LLAMA_BUILD_COMMIT@";
8+
char const * LLAMA_COMPILER = "@BUILD_COMPILER@";
9+
char const * LLAMA_BUILD_TARGET = "@BUILD_TARGET@";
10+
11+
int llama_build_number(void) {
12+
return LLAMA_BUILD_NUMBER;
13+
}
14+
15+
const char * llama_commit(void) {
16+
return LLAMA_COMMIT;
17+
}
18+
19+
const char * llama_compiler(void) {
20+
return LLAMA_COMPILER;
21+
}
22+
23+
const char * llama_build_target(void) {
24+
return LLAMA_BUILD_TARGET;
25+
}
26+
27+
const char * llama_build_info(void) {
28+
static std::string s = "b" + std::to_string(LLAMA_BUILD_NUMBER) + "-" + LLAMA_COMMIT;
29+
return s.c_str();
30+
}
31+
32+
void llama_print_build_info(void) {
33+
fprintf(stderr, "%s: build = %d (%s)\n", __func__, llama_build_number(), llama_commit());
34+
fprintf(stderr, "%s: built with %s for %s\n", __func__, llama_compiler(), llama_build_target());
35+
}

common/build-info.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
int llama_build_number(void);
4+
5+
const char * llama_commit(void);
6+
const char * llama_compiler(void);
7+
8+
const char * llama_build_target(void);
9+
const char * llama_build_info(void);
10+
11+
void llama_print_build_info(void);

common/common.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ggml.h"
22
#include "gguf.h"
33

4+
#include "build-info.h"
45
#include "common.h"
56
#include "log.h"
67
#include "llama.h"
@@ -372,7 +373,7 @@ void common_init() {
372373
const char * build_type = " (debug)";
373374
#endif
374375

375-
LOG_DBG("build: %d (%s) with %s for %s%s\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT, LLAMA_COMPILER, LLAMA_BUILD_TARGET, build_type);
376+
LOG_DBG("build: %d (%s) with %s for %s%s\n", llama_build_number(), llama_commit(), llama_compiler(), llama_build_target(), build_type);
376377
}
377378

378379
std::string common_params_get_system_info(const common_params & params) {

common/common.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#pragma once
44

5+
#include "llama-cpp.h"
6+
57
#include "ggml-opt.h"
68
#include "ggml.h"
7-
#include "llama-cpp.h"
89

910
#include <set>
1011
#include <sstream>
@@ -27,11 +28,6 @@
2728
#define die(msg) do { fputs("error: " msg "\n", stderr); exit(1); } while (0)
2829
#define die_fmt(fmt, ...) do { fprintf(stderr, "error: " fmt "\n", __VA_ARGS__); exit(1); } while (0)
2930

30-
#define print_build_info() do { \
31-
fprintf(stderr, "%s: build = %d (%s)\n", __func__, LLAMA_BUILD_NUMBER, LLAMA_COMMIT); \
32-
fprintf(stderr, "%s: built with %s for %s\n", __func__, LLAMA_COMPILER, LLAMA_BUILD_TARGET); \
33-
} while(0)
34-
3531
struct common_time_meas {
3632
common_time_meas(int64_t & t_acc, bool disable = false);
3733
~common_time_meas();
@@ -53,14 +49,6 @@ struct common_adapter_lora_info {
5349

5450
using llama_tokens = std::vector<llama_token>;
5551

56-
// build info
57-
extern int LLAMA_BUILD_NUMBER;
58-
extern const char * LLAMA_COMMIT;
59-
extern const char * LLAMA_COMPILER;
60-
extern const char * LLAMA_BUILD_TARGET;
61-
62-
const static std::string build_info("b" + std::to_string(LLAMA_BUILD_NUMBER) + "-" + LLAMA_COMMIT);
63-
6452
struct common_control_vector_load_info;
6553

6654
//

common/download.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "arg.h"
22

3+
#include "build-info.h"
34
#include "common.h"
45
#include "log.h"
56
#include "download.h"
@@ -303,7 +304,7 @@ static int common_download_file_single_online(const std::string & url,
303304
headers.emplace(h.first, h.second);
304305
}
305306
if (headers.find("User-Agent") == headers.end()) {
306-
headers.emplace("User-Agent", "llama-cpp/" + build_info);
307+
headers.emplace("User-Agent", "llama-cpp/" + std::string(llama_build_info()));
307308
}
308309
if (!opts.bearer_token.empty()) {
309310
headers.emplace("Authorization", "Bearer " + opts.bearer_token);
@@ -441,7 +442,7 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string
441442
headers.emplace(h.first, h.second);
442443
}
443444
if (headers.find("User-Agent") == headers.end()) {
444-
headers.emplace("User-Agent", "llama-cpp/" + build_info);
445+
headers.emplace("User-Agent", "llama-cpp/" + std::string(llama_build_info()));
445446
}
446447

447448
if (params.timeout > 0) {

common/hf-cache.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "hf-cache.h"
22

3+
#include "build-info.h"
34
#include "common.h"
45
#include "log.h"
56
#include "http.h"
@@ -200,7 +201,7 @@ static nl::json api_get(const std::string & url,
200201
auto [cli, parts] = common_http_client(url);
201202

202203
httplib::Headers headers = {
203-
{"User-Agent", "llama-cpp/" + build_info},
204+
{"User-Agent", "llama-cpp/" + std::string(llama_build_info())},
204205
{"Accept", "application/json"}
205206
};
206207

common/log.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
2525

26+
int common_log_get_verbosity_thold(void) {
27+
return common_log_verbosity_thold;
28+
}
29+
2630
void common_log_set_verbosity_thold(int verbosity) {
2731
common_log_verbosity_thold = verbosity;
2832
}

0 commit comments

Comments
 (0)