Skip to content

Commit 8cf9f34

Browse files
Adding missing features of CMakeLists.txt & Refactoring (#131)
* Functionality addition CMakeLists.txt Refactoring: 1. Simplify more options that are negation of negation. LLAMA_NO_ACCELERATE -> LLAMA_ACCELERATE 2. Changed to an optional expression instead of forcing to enable AVX2 in MSVC. 3. Make CMAKE_CXX_STANDARD, which is different from Makefile, the same. 4. Use add_compile_options instead of adding options to CMAKE_C_FLAGS. 5. Make utils use target_link_libraries instead of directly referencing code. Added features: 1. Added some options. LLAMA_STATIC_LINK,LLAMA_NATIVE,LLAMA_LTO,LLAMA_GPROF,LLAMA_OPENBLAS * Fix Accelerate link in CMake * Windows build Fix * C++11 to C++17 * Reflects C/C++ standard individually * Change the version to 3.12 --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent bd4b46d commit 8cf9f34

File tree

1 file changed

+153
-76
lines changed

1 file changed

+153
-76
lines changed

CMakeLists.txt

Lines changed: 153 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,208 @@
1-
cmake_minimum_required(VERSION 3.8)
2-
project("llama.cpp")
3-
4-
set(CMAKE_CXX_STANDARD 20)
5-
set(CMAKE_CXX_STANDARD_REQUIRED true)
6-
set(CMAKE_C_STANDARD 11)
7-
set(THREADS_PREFER_PTHREAD_FLAG ON)
8-
find_package(Threads REQUIRED)
1+
cmake_minimum_required(VERSION 3.12)
2+
project("llama.cpp" C CXX)
93

104
if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
115
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
126
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
137
endif()
148

15-
option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON)
16-
option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF)
9+
#
10+
# Option list
11+
#
1712

18-
option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF)
19-
option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF)
20-
option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF)
13+
# general
14+
option(LLAMA_STATIC "llama: static link libraries" OFF)
15+
option(LLAMA_NATIVE "llama: enable -march=native flag" OFF)
16+
option(LLAMA_LTO "llama: enable link time optimization" OFF)
2117

22-
if (APPLE)
23-
option(LLAMA_NO_ACCELERATE "llama: disable Accelerate framework" OFF)
24-
option(LLAMA_NO_AVX "llama: disable AVX" OFF)
25-
option(LLAMA_NO_AVX2 "llama: disable AVX2" OFF)
26-
option(LLAMA_NO_FMA "llama: disable FMA" OFF)
27-
endif()
18+
# debug
19+
option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON)
20+
option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF)
21+
option(LLAMA_GPROF "llama: enable gprof" OFF)
22+
23+
# sanitizers
24+
option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF)
25+
option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF)
26+
option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF)
27+
28+
# instruction set specific
29+
option(LLAMA_AVX "llama: enable AVX" ON)
30+
option(LLAMA_AVX2 "llama: enable AVX2" ON)
31+
option(LLAMA_FMA "llama: enable FMA" ON)
32+
33+
# 3rd party libs
34+
option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON)
35+
option(LLAMA_OPENBLAS "llama: use OpenBLAS" OFF)
36+
37+
#
38+
# Compile flags
39+
#
40+
41+
set(CMAKE_CXX_STANDARD_REQUIRED true)
42+
set(CMAKE_C_STANDARD_REQUIRED true)
43+
set(THREADS_PREFER_PTHREAD_FLAG ON)
44+
find_package(Threads REQUIRED)
2845

2946
if (NOT MSVC)
3047
if (LLAMA_SANITIZE_THREAD)
31-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
32-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
48+
add_compile_options(-fsanitize=thread)
3349
endif()
3450

3551
if (LLAMA_SANITIZE_ADDRESS)
36-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
37-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
52+
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
3853
endif()
3954

4055
if (LLAMA_SANITIZE_UNDEFINED)
41-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
42-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
56+
add_compile_options(-fsanitize=undefined)
4357
endif()
4458
endif()
4559

46-
if (APPLE AND NOT LLAMA_NO_ACCELERATE)
60+
if (APPLE AND LLAMA_ACCELERATE)
4761
find_library(ACCELERATE_FRAMEWORK Accelerate)
4862
if (ACCELERATE_FRAMEWORK)
4963
message(STATUS "Accelerate framework found")
5064

51-
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
52-
set(LLAMA_EXTRA_FLAGS ${LLAMA_EXTRA_FLAGS} -DGGML_USE_ACCELERATE)
65+
add_compile_definitions(GGML_USE_ACCELERATE)
66+
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
5367
else()
5468
message(WARNING "Accelerate framework not found")
5569
endif()
5670
endif()
71+
if (LLAMA_OPENBLAS)
72+
if (LLAMA_STATIC)
73+
set(BLA_STATIC ON)
74+
endif()
75+
76+
set(BLA_VENDOR OpenBLAS)
77+
find_package(BLAS)
78+
if (BLAS_FOUND)
79+
message(STATUS "OpenBLAS found")
80+
81+
add_compile_definitions(GGML_USE_OPENBLAS)
82+
add_link_options(${BLAS_LIBRARIES})
83+
else()
84+
message(WARNING "OpenBLAS not found")
85+
endif()
86+
endif()
5787

5888
if (LLAMA_ALL_WARNINGS)
5989
if (NOT MSVC)
60-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \
61-
-Wall \
62-
-Wextra \
63-
-Wpedantic \
64-
-Wshadow \
65-
-Wcast-qual \
66-
-Wstrict-prototypes \
67-
-Wpointer-arith \
68-
-Wno-unused-function \
69-
")
70-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
71-
-Wall \
72-
-Wextra \
73-
-Wpedantic \
74-
-Wcast-qual \
75-
")
90+
set(c_flags
91+
-Wall
92+
-Wextra
93+
-Wpedantic
94+
-Wshadow
95+
-Wcast-qual
96+
-Wstrict-prototypes
97+
-Wpointer-arith
98+
-Wno-unused-function
99+
)
100+
set(cxx_flags
101+
-Wall
102+
-Wextra
103+
-Wpedantic
104+
-Wcast-qual
105+
)
76106
else()
77107
# todo : msvc
78108
endif()
109+
110+
add_compile_options(
111+
"$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
112+
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
113+
)
114+
79115
endif()
80116

117+
if (LLAMA_LTO)
118+
include(CheckIPOSupported)
119+
check_ipo_supported(RESULT result OUTPUT output)
120+
if (result)
121+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
122+
else()
123+
message(WARNING "IPO is not supported: ${output}")
124+
endif()
125+
endif()
126+
127+
# Architecture specific
128+
# TODO: probably these flags need to be tweaked on some architectures
129+
# feel free to update the Makefile for your architecture and send a pull request or issue
81130
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
131+
if (NOT MSVC)
132+
if (LLAMA_STATIC)
133+
add_link_options(-static)
134+
if (MINGW)
135+
add_link_options(-static-libgcc -static-libstdc++)
136+
endif()
137+
endif()
138+
if (LLAMA_GPROF)
139+
add_compile_options(-pg)
140+
endif()
141+
if (LLAMA_NATIVE)
142+
add_compile_options(-march=native)
143+
endif()
144+
endif()
82145

83146
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
84147
message(STATUS "ARM detected")
85-
else()
148+
if (MSVC)
149+
# TODO: arm msvc?
150+
else()
151+
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
152+
add_compile_options(-mcpu=native)
153+
endif()
154+
# TODO: armv6,7,8 version specific flags
155+
endif()
156+
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
86157
message(STATUS "x86 detected")
87158
if (MSVC)
88-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
89-
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX2")
90-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2")
159+
if (LLAMA_AVX2)
160+
add_compile_options(/arch:AVX2)
161+
elseif (LLAMA_AVX)
162+
add_compile_options(/arch:AVX)
163+
endif()
91164
else()
92-
if(NOT LLAMA_NO_AVX)
93-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx")
165+
add_compile_options(-mf16c)
166+
if (LLAMA_FMA)
167+
add_compile_options(-mfma)
94168
endif()
95-
if(NOT LLAMA_NO_AVX2)
96-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2")
169+
if (LLAMA_AVX)
170+
add_compile_options(-mavx)
97171
endif()
98-
if(NOT LLAMA_NO_FMA)
99-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfma")
172+
if (LLAMA_AVX2)
173+
add_compile_options(-mavx2)
100174
endif()
101-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mf16c")
102175
endif()
176+
else()
177+
# TODO: support PowerPC
178+
message(STATUS "Unknown architecture")
103179
endif()
104180

105-
# if (LLAMA_PERF)
106-
# set(LLAMA_EXTRA_FLAGS ${LLAMA_EXTRA_FLAGS} -DGGML_PERF)
107-
# endif()
108181

109-
add_executable(llama
110-
main.cpp
111-
utils.cpp
112-
utils.h)
182+
#
183+
# Build library
184+
#
185+
186+
add_executable(llama main.cpp)
113187

114-
add_executable(quantize
115-
quantize.cpp
116-
utils.cpp
117-
utils.h)
188+
add_executable(quantize quantize.cpp)
118189

119-
add_library(ggml
120-
ggml.c
121-
ggml.h)
190+
add_library(ggml OBJECT
191+
ggml.c
192+
ggml.h)
122193

123-
target_compile_definitions(ggml PUBLIC ${LLAMA_EXTRA_FLAGS})
124-
target_compile_definitions(llama PUBLIC ${LLAMA_EXTRA_FLAGS})
125-
target_compile_definitions(quantize PUBLIC ${LLAMA_EXTRA_FLAGS})
194+
add_library(utils OBJECT
195+
utils.cpp
196+
utils.h)
126197

127-
target_link_libraries(ggml PRIVATE ${LLAMA_EXTRA_LIBS})
128198
target_include_directories(ggml PUBLIC .)
129-
target_link_libraries(quantize PRIVATE ggml)
130-
target_link_libraries(llama PRIVATE ggml)
131-
target_link_libraries(ggml PRIVATE Threads::Threads)
199+
target_compile_features(ggml PUBLIC c_std_11)
200+
target_compile_features(utils PUBLIC cxx_std_17)
201+
202+
#
203+
# Linking
204+
#
205+
206+
target_link_libraries(ggml PRIVATE Threads::Threads ${LLAMA_EXTRA_LIBS})
207+
target_link_libraries(llama PRIVATE ggml utils)
208+
target_link_libraries(quantize PRIVATE ggml utils)

0 commit comments

Comments
 (0)