Skip to content

Commit bc054af

Browse files
authored
make : support overriding CFLAGS/CXXFLAGS/CPPFLAGS/LDFLAGS (#2886)
* make : remove unused -DGGML_BIG_ENDIAN * make : put preprocessor stuff in CPPFLAGS * make : pass Raspberry Pi arch flags to g++ as well * make : support overriding CFLAGS/CXXFLAGS/CPPFLAGS/LDFLAGS * make : fix inverted conditional
1 parent 3358c38 commit bc054af

File tree

1 file changed

+66
-84
lines changed

1 file changed

+66
-84
lines changed

Makefile

Lines changed: 66 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ OPT = -Ofast
6767
else
6868
OPT = -O3
6969
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 =
7374

7475
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
7879
else
79-
CFLAGS += -DNDEBUG
80-
CXXFLAGS += -DNDEBUG
80+
MK_CPPFLAGS += -DNDEBUG
8181
endif
8282

8383
ifdef LLAMA_SERVER_VERBOSE
84-
CXXFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
84+
MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
8585
endif
8686

8787
ifdef LLAMA_DISABLE_LOGS
@@ -90,9 +90,9 @@ ifdef LLAMA_DISABLE_LOGS
9090
endif # LLAMA_DISABLE_LOGS
9191

9292
# 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
9696

9797
ifeq '' '$(findstring clang++,$(CXX))'
9898
# g++ only
@@ -101,29 +101,9 @@ endif
101101

102102
# OS specific
103103
# 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
127107
endif
128108

129109
# detect Windows
@@ -149,12 +129,11 @@ ifeq ($(_WIN32),1)
149129
endif
150130

151131
ifdef LLAMA_GPROF
152-
CFLAGS += -pg
153-
CXXFLAGS += -pg
132+
MK_CFLAGS += -pg
133+
MK_CXXFLAGS += -pg
154134
endif
155135
ifdef LLAMA_PERF
156-
CFLAGS += -DGGML_PERF
157-
CXXFLAGS += -DGGML_PERF
136+
MK_CPPFLAGS += -DGGML_PERF
158137
endif
159138

160139
# Architecture specific
@@ -165,16 +144,16 @@ ifndef RISCV
165144

166145
ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
167146
# 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
170149

171150
# 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
174153

175154
# Usage SSSE3-only (Not is SSE3!)
176-
#CFLAGS += -mssse3
177-
#CXXFLAGS += -mssse3
155+
#MK_CFLAGS += -mssse3
156+
#MK_CXXFLAGS += -mssse3
178157
endif
179158

180159
# The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
@@ -188,34 +167,33 @@ endif
188167
ifneq ($(filter aarch64%,$(UNAME_M)),)
189168
# Apple M1, M2, etc.
190169
# 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
193172
endif
194173

195174
ifneq ($(filter armv6%,$(UNAME_M)),)
196175
# 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
198178
endif
199179

200180
ifneq ($(filter armv7%,$(UNAME_M)),)
201181
# 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
203184
endif
204185

205186
ifneq ($(filter armv8%,$(UNAME_M)),)
206187
# 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
208190
endif
209191

210192
ifneq ($(filter ppc64%,$(UNAME_M)),)
211193
POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
212194
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
219197
endif
220198
endif
221199

@@ -225,44 +203,43 @@ else
225203
endif
226204

227205
ifndef LLAMA_NO_K_QUANTS
228-
CFLAGS += -DGGML_USE_K_QUANTS
229-
CXXFLAGS += -DGGML_USE_K_QUANTS
206+
MK_CPPFLAGS += -DGGML_USE_K_QUANTS
230207
OBJS += k_quants.o
231208
ifdef LLAMA_QKK_64
232-
CFLAGS += -DGGML_QKK_64
233-
CXXFLAGS += -DGGML_QKK_64
209+
MK_CPPFLAGS += -DGGML_QKK_64
234210
endif
235211
endif
236212

237213
ifndef LLAMA_NO_ACCELERATE
238214
# Mac M1 - include Accelerate framework.
239215
# `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
240216
ifeq ($(UNAME_S),Darwin)
241-
CFLAGS += -DGGML_USE_ACCELERATE
242-
LDFLAGS += -framework Accelerate
217+
MK_CPPFLAGS += -DGGML_USE_ACCELERATE
218+
MK_LDFLAGS += -framework Accelerate
243219
endif
244220
endif # LLAMA_NO_ACCELERATE
245221

246222
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
249226
OBJS += ggml-mpi.o
250227
endif # LLAMA_MPI
251228

252229
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)
255233
endif # LLAMA_OPENBLAS
256234

257235
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
260238
endif # LLAMA_BLIS
261239

262240
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
266243
OBJS += ggml-cuda.o
267244
NVCCFLAGS = --forward-unknown-to-host-compiler -use_fast_math
268245
ifdef LLAMA_CUDA_NVCC
@@ -313,14 +290,15 @@ endif # LLAMA_CUBLAS
313290

314291
ifdef LLAMA_CLBLAST
315292

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)
318296

319297
# Mac provides OpenCL as a framework
320298
ifeq ($(UNAME_S),Darwin)
321-
LDFLAGS += -lclblast -framework OpenCL
299+
MK_LDFLAGS += -lclblast -framework OpenCL
322300
else
323-
LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
301+
MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
324302
endif
325303
OBJS += ggml-opencl.o
326304

@@ -335,10 +313,9 @@ ifdef LLAMA_HIPBLAS
335313
LLAMA_CUDA_DMMV_X ?= 32
336314
LLAMA_CUDA_MMV_Y ?= 1
337315
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
342319
HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
343320
HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
344321
HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
@@ -353,10 +330,9 @@ ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
353330
endif # LLAMA_HIPBLAS
354331

355332
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
360336
endif # LLAMA_METAL
361337

362338
ifdef LLAMA_METAL
@@ -369,11 +345,17 @@ ggml-mpi.o: ggml-mpi.c ggml-mpi.h
369345
$(CC) $(CFLAGS) -c $< -o $@
370346
endif # LLAMA_MPI
371347

372-
ifdef LLAMA_NO_K_QUANTS
348+
ifndef LLAMA_NO_K_QUANTS
373349
k_quants.o: k_quants.c k_quants.h
374350
$(CC) $(CFLAGS) -c $< -o $@
375351
endif # LLAMA_NO_K_QUANTS
376352

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+
377359
#
378360
# Print build information
379361
#

0 commit comments

Comments
 (0)