@@ -67,21 +67,21 @@ OPT = -Ofast
67
67
else
68
68
OPT = -O3
69
69
endif
70
- CFLAGS = -I. $(OPT ) -std=c11 -fPIC
71
- CXXFLAGS = -I. -I./common $(OPT ) -std=c++11 -fPIC
72
- LDFLAGS =
70
+ MK_CPPFLAGS = -I. -Icommon
71
+ MK_CFLAGS = $(CPPFLAGS ) $(OPT ) -std=c11 -fPIC
72
+ MK_CXXFLAGS = $(CPPFLAGS ) $(OPT ) -std=c++11 -fPIC
73
+ MK_LDFLAGS =
73
74
74
75
ifdef LLAMA_DEBUG
75
- CFLAGS += -O0 -g
76
- CXXFLAGS += -O0 -g
77
- LDFLAGS += -g
76
+ MK_CFLAGS += -O0 -g
77
+ MK_CXXFLAGS += -O0 -g
78
+ MK_LDFLAGS += -g
78
79
else
79
- CFLAGS += -DNDEBUG
80
- CXXFLAGS += -DNDEBUG
80
+ MK_CPPFLAGS += -DNDEBUG
81
81
endif
82
82
83
83
ifdef LLAMA_SERVER_VERBOSE
84
- CXXFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
84
+ MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
85
85
endif
86
86
87
87
ifdef LLAMA_DISABLE_LOGS
@@ -90,9 +90,9 @@ ifdef LLAMA_DISABLE_LOGS
90
90
endif # LLAMA_DISABLE_LOGS
91
91
92
92
# warnings
93
- CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith \
94
- -Wmissing-prototypes -Werror=implicit-int -Wno-unused-function
95
- CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar
93
+ MK_CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith \
94
+ -Wmissing-prototypes -Werror=implicit-int -Wno-unused-function
95
+ MK_CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar
96
96
97
97
ifeq '' '$(findstring clang++,$(CXX ) ) '
98
98
# g++ only
@@ -101,29 +101,9 @@ endif
101
101
102
102
# OS specific
103
103
# TODO: support Windows
104
- ifeq ($(UNAME_S ) ,Linux)
105
- CFLAGS += -pthread
106
- CXXFLAGS += -pthread
107
- endif
108
- ifeq ($(UNAME_S ) ,Darwin)
109
- CFLAGS += -pthread
110
- CXXFLAGS += -pthread
111
- endif
112
- ifeq ($(UNAME_S ) ,FreeBSD)
113
- CFLAGS += -pthread
114
- CXXFLAGS += -pthread
115
- endif
116
- ifeq ($(UNAME_S ) ,NetBSD)
117
- CFLAGS += -pthread
118
- CXXFLAGS += -pthread
119
- endif
120
- ifeq ($(UNAME_S ) ,OpenBSD)
121
- CFLAGS += -pthread
122
- CXXFLAGS += -pthread
123
- endif
124
- ifeq ($(UNAME_S ) ,Haiku)
125
- CFLAGS += -pthread
126
- CXXFLAGS += -pthread
104
+ ifneq '' '$(filter $(UNAME_S ) ,Linux Darwin FreeBSD NetBSD OpenBSD Haiku) '
105
+ MK_CFLAGS += -pthread
106
+ MK_CXXFLAGS += -pthread
127
107
endif
128
108
129
109
# detect Windows
@@ -149,12 +129,11 @@ ifeq ($(_WIN32),1)
149
129
endif
150
130
151
131
ifdef LLAMA_GPROF
152
- CFLAGS += -pg
153
- CXXFLAGS += -pg
132
+ MK_CFLAGS += -pg
133
+ MK_CXXFLAGS += -pg
154
134
endif
155
135
ifdef LLAMA_PERF
156
- CFLAGS += -DGGML_PERF
157
- CXXFLAGS += -DGGML_PERF
136
+ MK_CPPFLAGS += -DGGML_PERF
158
137
endif
159
138
160
139
# Architecture specific
@@ -165,16 +144,16 @@ ifndef RISCV
165
144
166
145
ifeq ($(UNAME_M ) ,$(filter $(UNAME_M ) ,x86_64 i686 amd64) )
167
146
# Use all CPU extensions that are available:
168
- CFLAGS += -march=native -mtune=native
169
- CXXFLAGS += -march=native -mtune=native
147
+ MK_CFLAGS += -march=native -mtune=native
148
+ MK_CXXFLAGS += -march=native -mtune=native
170
149
171
150
# Usage AVX-only
172
- # CFLAGS += -mfma -mf16c -mavx
173
- # CXXFLAGS += -mfma -mf16c -mavx
151
+ # MK_CFLAGS += -mfma -mf16c -mavx
152
+ # MK_CXXFLAGS += -mfma -mf16c -mavx
174
153
175
154
# Usage SSSE3-only (Not is SSE3!)
176
- # CFLAGS += -mssse3
177
- # CXXFLAGS += -mssse3
155
+ # MK_CFLAGS += -mssse3
156
+ # MK_CXXFLAGS += -mssse3
178
157
endif
179
158
180
159
# The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
@@ -188,34 +167,33 @@ endif
188
167
ifneq ($(filter aarch64% ,$(UNAME_M ) ) ,)
189
168
# Apple M1, M2, etc.
190
169
# Raspberry Pi 3, 4, Zero 2 (64-bit)
191
- CFLAGS += -mcpu=native
192
- CXXFLAGS += -mcpu=native
170
+ MK_CFLAGS += -mcpu=native
171
+ MK_CXXFLAGS += -mcpu=native
193
172
endif
194
173
195
174
ifneq ($(filter armv6% ,$(UNAME_M ) ) ,)
196
175
# Raspberry Pi 1, Zero
197
- CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
176
+ MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
177
+ MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
198
178
endif
199
179
200
180
ifneq ($(filter armv7% ,$(UNAME_M ) ) ,)
201
181
# Raspberry Pi 2
202
- CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
182
+ MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
183
+ MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
203
184
endif
204
185
205
186
ifneq ($(filter armv8% ,$(UNAME_M ) ) ,)
206
187
# Raspberry Pi 3, 4, Zero 2 (32-bit)
207
- CFLAGS += -mfp16-format=ieee -mno-unaligned-access
188
+ MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
189
+ MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
208
190
endif
209
191
210
192
ifneq ($(filter ppc64% ,$(UNAME_M ) ) ,)
211
193
POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
212
194
ifneq (,$(findstring POWER9,$(POWER9_M)))
213
- CFLAGS += -mcpu=power9
214
- CXXFLAGS += -mcpu=power9
215
- endif
216
- # Require c++23's std::byteswap for big-endian support.
217
- ifeq ($(UNAME_M),ppc64)
218
- CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
195
+ MK_CFLAGS += -mcpu=power9
196
+ MK_CXXFLAGS += -mcpu=power9
219
197
endif
220
198
endif
221
199
@@ -225,44 +203,43 @@ else
225
203
endif
226
204
227
205
ifndef LLAMA_NO_K_QUANTS
228
- CFLAGS += -DGGML_USE_K_QUANTS
229
- CXXFLAGS += -DGGML_USE_K_QUANTS
206
+ MK_CPPFLAGS += -DGGML_USE_K_QUANTS
230
207
OBJS += k_quants.o
231
208
ifdef LLAMA_QKK_64
232
- CFLAGS += -DGGML_QKK_64
233
- CXXFLAGS += -DGGML_QKK_64
209
+ MK_CPPFLAGS += -DGGML_QKK_64
234
210
endif
235
211
endif
236
212
237
213
ifndef LLAMA_NO_ACCELERATE
238
214
# Mac M1 - include Accelerate framework.
239
215
# `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
240
216
ifeq ($(UNAME_S),Darwin)
241
- CFLAGS += -DGGML_USE_ACCELERATE
242
- LDFLAGS += -framework Accelerate
217
+ MK_CPPFLAGS += -DGGML_USE_ACCELERATE
218
+ MK_LDFLAGS += -framework Accelerate
243
219
endif
244
220
endif # LLAMA_NO_ACCELERATE
245
221
246
222
ifdef LLAMA_MPI
247
- CFLAGS += -DGGML_USE_MPI -Wno-cast-qual
248
- CXXFLAGS += -DGGML_USE_MPI -Wno-cast-qual
223
+ MK_CPPFLAGS += -DGGML_USE_MPI
224
+ MK_CFLAGS += -Wno-cast-qual
225
+ MK_CXXFLAGS += -Wno-cast-qual
249
226
OBJS += ggml-mpi.o
250
227
endif # LLAMA_MPI
251
228
252
229
ifdef LLAMA_OPENBLAS
253
- CFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags openblas)
254
- LDFLAGS += $(shell pkg-config --libs openblas)
230
+ MK_CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
231
+ MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
232
+ MK_LDFLAGS += $(shell pkg-config --libs openblas)
255
233
endif # LLAMA_OPENBLAS
256
234
257
235
ifdef LLAMA_BLIS
258
- CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
259
- LDFLAGS += -lblis -L/usr/local/lib
236
+ MK_CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
237
+ MK_LDFLAGS += -lblis -L/usr/local/lib
260
238
endif # LLAMA_BLIS
261
239
262
240
ifdef LLAMA_CUBLAS
263
- CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
264
- CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
265
- LDFLAGS += -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64 -L/opt/cuda/lib64 -L$(CUDA_PATH)/targets/x86_64-linux/lib
241
+ MK_CPPFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
242
+ MK_LDFLAGS += -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64 -L/opt/cuda/lib64 -L$(CUDA_PATH)/targets/x86_64-linux/lib
266
243
OBJS += ggml-cuda.o
267
244
NVCCFLAGS = --forward-unknown-to-host-compiler -use_fast_math
268
245
ifdef LLAMA_CUDA_NVCC
@@ -313,14 +290,15 @@ endif # LLAMA_CUBLAS
313
290
314
291
ifdef LLAMA_CLBLAST
315
292
316
- CFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags clblast OpenCL)
317
- CXXFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags clblast OpenCL)
293
+ MK_CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
294
+ MK_CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
295
+ MK_CXXFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
318
296
319
297
# Mac provides OpenCL as a framework
320
298
ifeq ($(UNAME_S),Darwin)
321
- LDFLAGS += -lclblast -framework OpenCL
299
+ MK_LDFLAGS += -lclblast -framework OpenCL
322
300
else
323
- LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
301
+ MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
324
302
endif
325
303
OBJS += ggml-opencl.o
326
304
@@ -335,10 +313,9 @@ ifdef LLAMA_HIPBLAS
335
313
LLAMA_CUDA_DMMV_X ?= 32
336
314
LLAMA_CUDA_MMV_Y ?= 1
337
315
LLAMA_CUDA_KQUANTS_ITER ?= 2
338
- CFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
339
- CXXFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
340
- LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
341
- LDFLAGS += -lhipblas -lamdhip64 -lrocblas
316
+ MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
317
+ MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
318
+ MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
342
319
HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
343
320
HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
344
321
HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
@@ -353,10 +330,9 @@ ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
353
330
endif # LLAMA_HIPBLAS
354
331
355
332
ifdef LLAMA_METAL
356
- CFLAGS += -DGGML_USE_METAL # -DGGML_METAL_NDEBUG
357
- CXXFLAGS += -DGGML_USE_METAL
358
- LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
359
- OBJS += ggml-metal.o
333
+ MK_CPPFLAGS += -DGGML_USE_METAL # -DGGML_METAL_NDEBUG
334
+ MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
335
+ OBJS += ggml-metal.o
360
336
endif # LLAMA_METAL
361
337
362
338
ifdef LLAMA_METAL
@@ -369,11 +345,17 @@ ggml-mpi.o: ggml-mpi.c ggml-mpi.h
369
345
$(CC ) $(CFLAGS ) -c $< -o $@
370
346
endif # LLAMA_MPI
371
347
372
- ifdef LLAMA_NO_K_QUANTS
348
+ ifndef LLAMA_NO_K_QUANTS
373
349
k_quants.o : k_quants.c k_quants.h
374
350
$(CC ) $(CFLAGS ) -c $< -o $@
375
351
endif # LLAMA_NO_K_QUANTS
376
352
353
+ # combine build flags with cmdline overrides
354
+ override CPPFLAGS := $(MK_CPPFLAGS ) $(CPPFLAGS )
355
+ override CFLAGS := $(MK_CFLAGS ) $(CFLAGS )
356
+ override CXXFLAGS := $(MK_CXXFLAGS ) $(CXXFLAGS )
357
+ override LDFLAGS := $(MK_LDFLAGS ) $(LDFLAGS )
358
+
377
359
#
378
360
# Print build information
379
361
#
0 commit comments