Skip to content

Commit 037060f

Browse files
author
stone
committed
cmake : avoid duplicate -march=native when already set in CFLAGS
If -march= is already specified via CMAKE_C_FLAGS/CMAKE_CXX_FLAGS or ARCH_FLAGS, do not append -march=native again. This prevents compiler warnings about redundant flags and allows users to override the march target by setting it in their build environment. Fixes duplicate -march flag in ggml-cpu and ggml-amx backends.
1 parent 174af6b commit 037060f

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

ggml/src/ggml-amx/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ if (CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64" OR CMAKE_GENERATOR_PLATFORM_LWR MA
6161
endif()
6262
else()
6363
if (GGML_NATIVE)
64-
list(APPEND ARCH_FLAGS -march=native)
64+
set(march_flag_found FALSE)
65+
foreach(flag IN LISTS ARCH_FLAGS)
66+
if(flag MATCHES "^-march=")
67+
set(march_flag_found TRUE)
68+
break()
69+
endif()
70+
endforeach()
71+
if(NOT march_flag_found)
72+
list(APPEND ARCH_FLAGS -march=native)
73+
endif()
6574
endif()
6675
if (GGML_F16C)
6776
list(APPEND ARCH_FLAGS -mf16c)

ggml/src/ggml-cpu/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
235235
endif()
236236
else ()
237237
if (GGML_NATIVE)
238-
list(APPEND ARCH_FLAGS -march=native)
238+
string(REGEX MATCH "-march=[^ ]+" MARCH_MATCH "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS}")
239+
if(NOT MARCH_MATCH)
240+
list(APPEND ARCH_FLAGS -march=native)
241+
endif()
239242
else ()
240243
list(APPEND ARCH_FLAGS -msse4.2)
241244
list(APPEND ARCH_DEFINITIONS GGML_SSE42)

0 commit comments

Comments
 (0)