Skip to content

Commit a6b7476

Browse files
committed
compiler version detection
1 parent 05adde4 commit a6b7476

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

CMakeLists.txt

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -412,42 +412,38 @@ endif()
412412

413413
if (LLAMA_ALL_WARNINGS)
414414
if (NOT MSVC)
415-
set(warning_flags
416-
-Wall
417-
-Wextra
418-
-Wpedantic
419-
-Wcast-qual
420-
-Wno-unused-function
421-
)
422-
if (CMAKE_C_COMPILER_ID MATCHES "Clang") # clang only
415+
set(warning_flags -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function)
416+
set(c_flags -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int
417+
-Werror=implicit-function-declaration)
418+
set(cxx_flags -Wmissing-declarations -Wmissing-noreturn)
419+
420+
if (CMAKE_C_COMPILER_ID MATCHES "Clang")
423421
set(warning_flags ${warning_flags} -Wunreachable-code-break -Wunreachable-code-return)
424-
endif()
425-
set(c_flags
426-
${warning_flags}
427-
-Wdouble-promotion
428-
-Wshadow
429-
-Wstrict-prototypes
430-
-Wpointer-arith
431-
-Wmissing-prototypes
432-
-Werror=implicit-int
433-
-Werror=implicit-function-declaration
434-
)
435-
set(cxx_flags
436-
${warning_flags}
437-
-Wmissing-declarations
438-
-Wmissing-noreturn
439-
-Wextra-semi
440-
)
441-
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # clang++ only
442-
set(cxx_flags ${cxx_flags} -Wmissing-prototypes)
443-
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # g++ only
444-
set(cxx_flags ${cxx_flags} -Wno-format-truncation -Wno-array-bounds)
422+
set(cxx_flags ${cxx_flags} -Wmissing-prototypes -Wextra-semi)
423+
424+
if (
425+
(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 3.8.0) OR
426+
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 7.3.0)
427+
)
428+
set(c_flags ${c_flags} -Wdouble-promotion)
429+
endif()
430+
elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU")
431+
set(c_flags ${c_flags} -Wdouble-promotion)
432+
set(cxx_flags ${cxx_flags} -Wno-array-bounds)
433+
434+
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.1.0)
435+
set(cxx_flags ${cxx_flags} -Wno-format-truncation)
436+
endif()
437+
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.1.0)
438+
set(cxx_flags ${cxx_flags} -Wextra-semi)
439+
endif()
445440
endif()
446441
else()
447442
# todo : msvc
448443
endif()
449444

450445
add_compile_options(
446+
${warning_flags}
451447
"$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
452448
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
453449
)

Makefile

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ ifndef UNAME_M
1919
UNAME_M := $(shell uname -m)
2020
endif
2121

22+
ifeq '' '$(findstring clang,$(shell $(CC) --version))'
23+
CC_IS_GCC=1
24+
CC_VER := $(shell $(CC) -dumpfullversion -dumpversion | awk -F. '{ printf("%02d%02d%02d", $$1, $$2, $$3) }')
25+
else
26+
CC_IS_CLANG=1
27+
ifeq '' '$(findstring Apple LLVM,$(shell $(CC) --version))'
28+
CC_IS_LLVM_CLANG=1
29+
else
30+
CC_IS_APPLE_CLANG=1
31+
endif
32+
CC_VER := $(shell $(CC) --version | sed -n 's/^.* version \([0-9.]*\) .*$$/\1/p' \
33+
| awk -F. '{ printf("%02d%02d%02d", $$1, $$2, $$3) }')
34+
endif
35+
2236
# Mac OS + Arm can report x86_64
2337
# ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
2438
ifeq ($(UNAME_S),Darwin)
@@ -87,9 +101,6 @@ CC := riscv64-unknown-linux-gnu-gcc
87101
CXX := riscv64-unknown-linux-gnu-g++
88102
endif
89103

90-
CCV := $(shell $(CC) --version | head -n 1)
91-
CXXV := $(shell $(CXX) --version | head -n 1)
92-
93104
#
94105
# Compile flags
95106
#
@@ -174,21 +185,36 @@ endif # LLAMA_DISABLE_LOGS
174185

175186
# warnings
176187
WARN_FLAGS = -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
177-
MK_CFLAGS += $(WARN_FLAGS) -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes \
178-
-Werror=implicit-int -Werror=implicit-function-declaration
179-
MK_CXXFLAGS += $(WARN_FLAGS) -Wmissing-declarations -Wmissing-noreturn -Wextra-semi
188+
MK_CFLAGS += $(WARN_FLAGS) -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int \
189+
-Werror=implicit-function-declaration
190+
MK_CXXFLAGS += $(WARN_FLAGS) -Wmissing-declarations -Wmissing-noreturn
180191

181192
# TODO(cebtenzzre): remove this once PR #2632 gets merged
182193
TTFS_CXXFLAGS = $(CXXFLAGS) -Wno-missing-declarations
183194

184-
ifneq '' '$(findstring clang,$(shell $(CC) --version))'
185-
# clang only
195+
ifeq ($(CC_IS_CLANG), 1)
196+
# clang options
186197
MK_CFLAGS += -Wunreachable-code-break -Wunreachable-code-return
187-
MK_HOST_CXXFLAGS += -Wunreachable-code-break -Wunreachable-code-return -Wmissing-prototypes
198+
MK_HOST_CXXFLAGS += -Wunreachable-code-break -Wunreachable-code-return -Wmissing-prototypes -Wextra-semi
188199
TTFS_CXXFLAGS += -Wno-missing-prototypes
200+
201+
ifneq '' '$(and $(CC_IS_LLVM_CLANG),$(filter 1,$(shell expr $(CC_VER) \>= 030800)))'
202+
MK_CFLAGS += -Wdouble-promotion
203+
endif
204+
ifneq '' '$(and $(CC_IS_APPLE_CLANG),$(filter 1,$(shell expr $(CC_VER) \>= 070300)))'
205+
MK_CFLAGS += -Wdouble-promotion
206+
endif
189207
else
190-
# gcc only
191-
MK_HOST_CXXFLAGS += -Wno-format-truncation -Wno-array-bounds
208+
# gcc options
209+
MK_CFLAGS += -Wdouble-promotion
210+
MK_HOST_CXXFLAGS += -Wno-array-bounds
211+
212+
ifeq ($(shell expr $(CC_VER) \>= 070100), 1)
213+
MK_HOST_CXXFLAGS += -Wno-format-truncation
214+
endif
215+
ifeq ($(shell expr $(CC_VER) \>= 080100), 1)
216+
MK_HOST_CXXFLAGS += -Wextra-semi
217+
endif
192218
endif
193219

194220
# OS specific
@@ -472,8 +498,8 @@ $(info I CFLAGS: $(CFLAGS))
472498
$(info I CXXFLAGS: $(CXXFLAGS))
473499
$(info I NVCCFLAGS: $(NVCCFLAGS))
474500
$(info I LDFLAGS: $(LDFLAGS))
475-
$(info I CC: $(CCV))
476-
$(info I CXX: $(CXXV))
501+
$(info I CC: $(shell $(CC) --version | head -n 1))
502+
$(info I CXX: $(shell $(CXX) --version | head -n 1))
477503
$(info )
478504

479505
#

0 commit comments

Comments
 (0)