Skip to content

Commit 19ecca1

Browse files
committed
cmake : use list(APPEND ...) instead of set() + dedup linker
ggml-ci
1 parent bd35cb0 commit 19ecca1

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

ggml/src/CMakeLists.txt

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ if (NOT MSVC)
2626
endif()
2727
endif()
2828

29+
unset(GGML_EXTRA_LIBS)
30+
2931
if (APPLE AND GGML_ACCELERATE)
3032
find_library(ACCELERATE_FRAMEWORK Accelerate)
3133
if (ACCELERATE_FRAMEWORK)
@@ -35,7 +37,7 @@ if (APPLE AND GGML_ACCELERATE)
3537
add_compile_definitions(ACCELERATE_NEW_LAPACK)
3638
add_compile_definitions(ACCELERATE_LAPACK_ILP64)
3739

38-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
40+
list(APPEND GGML_EXTRA_LIBS ${ACCELERATE_FRAMEWORK})
3941
else()
4042
message(WARNING "Accelerate framework not found")
4143
endif()
@@ -87,7 +89,7 @@ if (GGML_METAL)
8789
COMMENT "Generate assembly for embedded Metal library"
8890
)
8991

90-
set(GGML_SOURCES_METAL ${GGML_SOURCES_METAL} ${METALLIB_EMBED_ASM})
92+
list(APPEND GGML_SOURCES_METAL ${METALLIB_EMBED_ASM})
9193
else()
9294
if (GGML_METAL_SHADER_DEBUG)
9395
# custom command to do the following:
@@ -132,7 +134,7 @@ if (GGML_METAL)
132134
)
133135
endif() # GGML_METAL_EMBED_LIBRARY
134136

135-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS}
137+
list(APPEND GGML_EXTRA_LIBS
136138
${FOUNDATION_LIBRARY}
137139
${METAL_FRAMEWORK}
138140
${METALKIT_FRAMEWORK}
@@ -157,11 +159,11 @@ if (GGML_OPENMP)
157159

158160
add_compile_definitions(GGML_USE_OPENMP)
159161

160-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} OpenMP::OpenMP_C OpenMP::OpenMP_CXX)
162+
list(APPEND GGML_EXTRA_LIBS OpenMP::OpenMP_C OpenMP::OpenMP_CXX)
161163

162164
if (GGML_MUSA)
163-
set(GGML_EXTRA_INCLUDES ${GGML_EXTRA_INCLUDES} "/usr/lib/llvm-10/include/openmp")
164-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} "/usr/lib/llvm-10/lib/libomp.so")
165+
list(APPEND GGML_EXTRA_INCLUDES "/usr/lib/llvm-10/include/openmp")
166+
list(APPEND GGML_EXTRA_LIBS "/usr/lib/llvm-10/lib/libomp.so")
165167
endif()
166168
else()
167169
message(WARNING "OpenMP not found")
@@ -244,8 +246,8 @@ if (GGML_BLAS)
244246
set(GGML_HEADERS_BLAS ../include/ggml-blas.h)
245247
set(GGML_SOURCES_BLAS ggml-blas.cpp)
246248

247-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} ${BLAS_LIBRARIES})
248-
set(GGML_EXTRA_INCLUDES ${GGML_EXTRA_INCLUDES} ${BLAS_INCLUDE_DIRS})
249+
list(APPEND GGML_EXTRA_LIBS ${BLAS_LIBRARIES})
250+
list(APPEND GGML_EXTRA_INCLUDES ${BLAS_INCLUDE_DIRS})
249251
else()
250252
message(WARNING "BLAS not found, please refer to "
251253
"https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors"
@@ -368,29 +370,29 @@ if (GGML_CUDA)
368370
if (GGML_STATIC)
369371
if (WIN32)
370372
# As of 12.3.1 CUDA Toolkit for Windows does not offer a static cublas library
371-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} CUDA::cudart_static CUDA::cublas CUDA::cublasLt)
373+
list(APPEND GGML_EXTRA_LIBS CUDA::cudart_static CUDA::cublas CUDA::cublasLt)
372374
else ()
373375
if (GGML_MUSA)
374-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} MUSA::musart_static MUSA::mublas_static)
376+
list(APPEND GGML_EXTRA_LIBS MUSA::musart_static MUSA::mublas_static)
375377
else()
376-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
378+
list(APPEND GGML_EXTRA_LIBS CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
377379
endif()
378380
endif()
379381
else()
380382
if (GGML_MUSA)
381-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} MUSA::musart MUSA::mublas)
383+
list(APPEND GGML_EXTRA_LIBS MUSA::musart MUSA::mublas)
382384
else()
383-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} CUDA::cudart CUDA::cublas CUDA::cublasLt)
385+
list(APPEND GGML_EXTRA_LIBS CUDA::cudart CUDA::cublas CUDA::cublasLt)
384386
endif()
385387
endif()
386388

387389
if (GGML_CUDA_NO_VMM)
388390
# No VMM requested, no need to link directly with the cuda driver lib (libcuda.so)
389391
else()
390392
if (GGML_MUSA)
391-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} MUSA::musa_driver) # required by muDeviceGetAttribute(), muMemGetAllocationGranularity(...), ...
393+
list(APPEND GGML_EXTRA_LIBS MUSA::musa_driver) # required by muDeviceGetAttribute(), muMemGetAllocationGranularity(...), ...
392394
else()
393-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} CUDA::cuda_driver) # required by cuDeviceGetAttribute(), cuMemGetAllocationGranularity(...), ...
395+
list(APPEND GGML_EXTRA_LIBS CUDA::cuda_driver) # required by cuDeviceGetAttribute(), cuMemGetAllocationGranularity(...), ...
394396
endif()
395397
endif()
396398
else()
@@ -495,7 +497,7 @@ if (GGML_HIPBLAS)
495497

496498
if (CXX_IS_HIPCC)
497499
set_source_files_properties(${GGML_SOURCES_ROCM} PROPERTIES LANGUAGE CXX)
498-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} hip::device)
500+
list(APPEND GGML_EXTRA_LIBS hip::device)
499501
else()
500502
set_source_files_properties(${GGML_SOURCES_ROCM} PROPERTIES LANGUAGE HIP)
501503
endif()
@@ -504,7 +506,8 @@ if (GGML_HIPBLAS)
504506
message(FATAL_ERROR "Static linking not supported for HIP/ROCm")
505507
endif()
506508

507-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} PUBLIC hip::host roc::rocblas roc::hipblas)
509+
# TODO: this "PUBLIC" here seems wrong
510+
list(APPEND GGML_EXTRA_LIBS PUBLIC hip::host roc::rocblas roc::hipblas)
508511
endif()
509512

510513
if (GGML_SYCL)
@@ -535,7 +538,7 @@ if (GGML_SYCL)
535538
add_compile_definitions(GGML_SYCL_FORCE_MMQ)
536539
endif()
537540

538-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-narrowing -fsycl")
541+
list(APPEND CMAKE_CXX_FLAGS -Wno-narrowing -fsycl)
539542

540543
if (GGML_SYCL_TARGET STREQUAL "NVIDIA")
541544
add_compile_definitions(GGML_SYCL_WARP_SIZE=32)
@@ -559,13 +562,13 @@ if (GGML_SYCL)
559562
if (WIN32)
560563
find_package(IntelSYCL REQUIRED)
561564
find_package(MKL REQUIRED)
562-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} IntelSYCL::SYCL_CXX MKL::MKL MKL::MKL_SYCL)
565+
list(APPEND GGML_EXTRA_LIBS IntelSYCL::SYCL_CXX MKL::MKL MKL::MKL_SYCL)
563566
else()
564567
if (GGML_SYCL_TARGET STREQUAL "INTEL")
565-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} -fsycl OpenCL mkl_core pthread m dl mkl_sycl_blas mkl_intel_ilp64 mkl_tbb_thread)
568+
list(APPEND GGML_EXTRA_LIBS -fsycl OpenCL mkl_core pthread m dl mkl_sycl_blas mkl_intel_ilp64 mkl_tbb_thread)
566569
elseif (GGML_SYCL_TARGET STREQUAL "NVIDIA")
567-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl-targets=nvptx64-nvidia-cuda")
568-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} -fsycl pthread m dl onemkl)
570+
list(APPEND CMAKE_CXX_FLAGS -fsycl-targets=nvptx64-nvidia-cuda)
571+
list(APPEND GGML_EXTRA_LIBS -fsycl pthread m dl onemkl)
569572
endif()
570573
endif()
571574
if (${DNNL_FOUND} AND GGML_SYCL_TARGET STREQUAL "INTEL")
@@ -579,7 +582,7 @@ if (GGML_RPC)
579582
list(APPEND GGML_CDEF_PUBLIC GGML_USE_RPC)
580583

581584
if (WIN32)
582-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} ws2_32)
585+
list(APPEND GGML_EXTRA_LIBS ws2_32)
583586
endif()
584587

585588
set(GGML_HEADERS_RPC ../include/ggml-rpc.h)
@@ -657,8 +660,8 @@ if (GGML_VULKAN)
657660
set(GGML_HEADERS_VULKAN ${CMAKE_CURRENT_SOURCE_DIR}/../include/ggml-vulkan.h ${_ggml_vk_header})
658661
set(GGML_SOURCES_VULKAN ggml-vulkan.cpp ${_ggml_vk_source})
659662

660-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} Vulkan::Vulkan)
661-
set(GGML_EXTRA_INCLUDES ${GGML_EXTRA_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
663+
list(APPEND GGML_EXTRA_LIBS Vulkan::Vulkan)
664+
list(APPEND GGML_EXTRA_INCLUDES ${CMAKE_CURRENT_BINARY_DIR})
662665
else()
663666
message(WARNING "Vulkan not found")
664667
endif()
@@ -817,8 +820,8 @@ if (GGML_KOMPUTE)
817820
818821
list(APPEND GGML_CDEF_PUBLIC GGML_USE_KOMPUTE)
819822
820-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} kompute)
821-
set(GGML_EXTRA_INCLUDES ${GGML_EXTRA_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
823+
list(APPEND GGML_EXTRA_LIBS kompute)
824+
list(APPEND GGML_EXTRA_INCLUDES ${CMAKE_CURRENT_BINARY_DIR})
822825
else()
823826
message(WARNING "Kompute not found")
824827
endif()
@@ -883,9 +886,10 @@ if (GGML_CANN)
883886
message(STATUS "CANN: CANN_INCLUDE_DIRS = ${CANN_INCLUDE_DIRS}")
884887
message(STATUS "CANN: CANN_LIBRARIES = ${CANN_LIBRARIES}")
885888
886-
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} ${CANN_LIBRARIES} )
887-
set(GGML_EXTRA_INCLUDES ${GGML_EXTRA_INCLUDES} ${CANN_INCLUDE_DIRS})
888-
set(GGML_EXTRA_LIBDIRS ${GGML_EXTRA_LIBDIRS} ${CANN_INSTALL_DIR}/lib64)
889+
list(APPEND GGML_EXTRA_LIBS ${CANN_LIBRARIES} )
890+
list(APPEND GGML_EXTRA_INCLUDES ${CANN_INCLUDE_DIRS})
891+
list(APPEND GGML_EXTRA_LIBDIRS ${CANN_INSTALL_DIR}/lib64)
892+
889893
list(APPEND GGML_CDEF_PUBLIC GGML_USE_CANN)
890894
endif()
891895
else()
@@ -1322,12 +1326,14 @@ if (EMSCRIPTEN)
13221326
set_target_properties(ggml PROPERTIES COMPILE_FLAGS "-msimd128")
13231327
endif()
13241328

1325-
target_compile_definitions(ggml PUBLIC ${GGML_CDEF_PUBLIC})
1326-
target_include_directories(ggml PUBLIC ../include)
1329+
target_compile_definitions(ggml PUBLIC ${GGML_CDEF_PUBLIC})
1330+
target_include_directories(ggml PUBLIC ../include)
13271331
target_include_directories(ggml PRIVATE . ${GGML_EXTRA_INCLUDES})
1328-
target_link_directories(ggml PRIVATE ${GGML_EXTRA_LIBDIRS})
1332+
target_link_directories (ggml PRIVATE ${GGML_EXTRA_LIBDIRS})
13291333
target_compile_features (ggml PRIVATE c_std_11) # don't bump
13301334

1335+
list(REMOVE_DUPLICATES GGML_EXTRA_LIBS)
1336+
13311337
target_link_libraries(ggml PRIVATE Threads::Threads ${GGML_EXTRA_LIBS})
13321338

13331339
find_library(MATH_LIBRARY m)

0 commit comments

Comments
 (0)