Skip to content

Commit b396783

Browse files
committed
make : support overriding CFLAGS/CXXFLAGS/CPPFLAGS/LDFLAGS
1 parent bc6bcdf commit b396783

File tree

1 file changed

+65
-79
lines changed

1 file changed

+65
-79
lines changed

Makefile

Lines changed: 65 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ OPT = -Ofast
6767
else
6868
OPT = -O3
6969
endif
70-
CPPFLAGS = -I. -Icommon
71-
CFLAGS = $(CPPFLAGS) $(OPT) -std=c11 -fPIC
72-
CXXFLAGS = $(CPPFLAGS) $(OPT) -std=c++11 -fPIC
73-
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 =
7474

7575
ifdef LLAMA_DEBUG
76-
CFLAGS += -O0 -g
77-
CXXFLAGS += -O0 -g
78-
LDFLAGS += -g
76+
MK_CFLAGS += -O0 -g
77+
MK_CXXFLAGS += -O0 -g
78+
MK_LDFLAGS += -g
7979
else
80-
CPPFLAGS += -DNDEBUG
80+
MK_CPPFLAGS += -DNDEBUG
8181
endif
8282

8383
ifdef LLAMA_SERVER_VERBOSE
84-
CPPFLAGS += -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,11 +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-
CPPFLAGS += -DGGML_PERF
136+
MK_CPPFLAGS += -DGGML_PERF
157137
endif
158138

159139
# Architecture specific
@@ -164,16 +144,16 @@ ifndef RISCV
164144

165145
ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
166146
# Use all CPU extensions that are available:
167-
CFLAGS += -march=native -mtune=native
168-
CXXFLAGS += -march=native -mtune=native
147+
MK_CFLAGS += -march=native -mtune=native
148+
MK_CXXFLAGS += -march=native -mtune=native
169149

170150
# Usage AVX-only
171-
#CFLAGS += -mfma -mf16c -mavx
172-
#CXXFLAGS += -mfma -mf16c -mavx
151+
#MK_CFLAGS += -mfma -mf16c -mavx
152+
#MK_CXXFLAGS += -mfma -mf16c -mavx
173153

174154
# Usage SSSE3-only (Not is SSE3!)
175-
#CFLAGS += -mssse3
176-
#CXXFLAGS += -mssse3
155+
#MK_CFLAGS += -mssse3
156+
#MK_CXXFLAGS += -mssse3
177157
endif
178158

179159
# The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
@@ -187,33 +167,33 @@ endif
187167
ifneq ($(filter aarch64%,$(UNAME_M)),)
188168
# Apple M1, M2, etc.
189169
# Raspberry Pi 3, 4, Zero 2 (64-bit)
190-
CFLAGS += -mcpu=native
191-
CXXFLAGS += -mcpu=native
170+
MK_CFLAGS += -mcpu=native
171+
MK_CXXFLAGS += -mcpu=native
192172
endif
193173

194174
ifneq ($(filter armv6%,$(UNAME_M)),)
195175
# Raspberry Pi 1, Zero
196-
CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
197-
CXXFLAGS += -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
203-
CXXFLAGS += -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
204184
endif
205185

206186
ifneq ($(filter armv8%,$(UNAME_M)),)
207187
# Raspberry Pi 3, 4, Zero 2 (32-bit)
208-
CFLAGS += -mfp16-format=ieee -mno-unaligned-access
209-
CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
188+
MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
189+
MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
210190
endif
211191

212192
ifneq ($(filter ppc64%,$(UNAME_M)),)
213193
POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
214194
ifneq (,$(findstring POWER9,$(POWER9_M)))
215-
CFLAGS += -mcpu=power9
216-
CXXFLAGS += -mcpu=power9
195+
MK_CFLAGS += -mcpu=power9
196+
MK_CXXFLAGS += -mcpu=power9
217197
endif
218198
endif
219199

@@ -223,43 +203,43 @@ else
223203
endif
224204

225205
ifndef LLAMA_NO_K_QUANTS
226-
CPPFLAGS += -DGGML_USE_K_QUANTS
206+
MK_CPPFLAGS += -DGGML_USE_K_QUANTS
227207
OBJS += k_quants.o
228208
ifdef LLAMA_QKK_64
229-
CPPFLAGS += -DGGML_QKK_64
209+
MK_CPPFLAGS += -DGGML_QKK_64
230210
endif
231211
endif
232212

233213
ifndef LLAMA_NO_ACCELERATE
234214
# Mac M1 - include Accelerate framework.
235215
# `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
236216
ifeq ($(UNAME_S),Darwin)
237-
CPPFLAGS += -DGGML_USE_ACCELERATE
238-
LDFLAGS += -framework Accelerate
217+
MK_CPPFLAGS += -DGGML_USE_ACCELERATE
218+
MK_LDFLAGS += -framework Accelerate
239219
endif
240220
endif # LLAMA_NO_ACCELERATE
241221

242222
ifdef LLAMA_MPI
243-
CPPFLAGS += -DGGML_USE_MPI
244-
CFLAGS += -Wno-cast-qual
245-
CXXFLAGS += -Wno-cast-qual
223+
MK_CPPFLAGS += -DGGML_USE_MPI
224+
MK_CFLAGS += -Wno-cast-qual
225+
MK_CXXFLAGS += -Wno-cast-qual
246226
OBJS += ggml-mpi.o
247227
endif # LLAMA_MPI
248228

249229
ifdef LLAMA_OPENBLAS
250-
CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
251-
CFLAGS += $(shell pkg-config --cflags-only-other openblas)
252-
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)
253233
endif # LLAMA_OPENBLAS
254234

255235
ifdef LLAMA_BLIS
256-
CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
257-
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
258238
endif # LLAMA_BLIS
259239

260240
ifdef LLAMA_CUBLAS
261-
CPPFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
262-
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
263243
OBJS += ggml-cuda.o
264244
NVCCFLAGS = --forward-unknown-to-host-compiler -use_fast_math
265245
ifdef LLAMA_CUDA_NVCC
@@ -310,15 +290,15 @@ endif # LLAMA_CUBLAS
310290

311291
ifdef LLAMA_CLBLAST
312292

313-
CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
314-
CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
315-
CXXFLAGS += $(shell pkg-config --cflags-only-other 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)
316296

317297
# Mac provides OpenCL as a framework
318298
ifeq ($(UNAME_S),Darwin)
319-
LDFLAGS += -lclblast -framework OpenCL
299+
MK_LDFLAGS += -lclblast -framework OpenCL
320300
else
321-
LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
301+
MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
322302
endif
323303
OBJS += ggml-opencl.o
324304

@@ -333,9 +313,9 @@ ifdef LLAMA_HIPBLAS
333313
LLAMA_CUDA_DMMV_X ?= 32
334314
LLAMA_CUDA_MMV_Y ?= 1
335315
LLAMA_CUDA_KQUANTS_ITER ?= 2
336-
CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
337-
LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
338-
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
339319
HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
340320
HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
341321
HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
@@ -350,9 +330,9 @@ ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
350330
endif # LLAMA_HIPBLAS
351331

352332
ifdef LLAMA_METAL
353-
CPPFLAGS += -DGGML_USE_METAL #-DGGML_METAL_NDEBUG
354-
LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
355-
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
356336
endif # LLAMA_METAL
357337

358338
ifdef LLAMA_METAL
@@ -370,6 +350,12 @@ k_quants.o: k_quants.c k_quants.h
370350
$(CC) $(CFLAGS) -c $< -o $@
371351
endif # LLAMA_NO_K_QUANTS
372352

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+
373359
#
374360
# Print build information
375361
#

0 commit comments

Comments
 (0)