From 8c41e7f62e6ac81101335ca7b96fb8a7ec7792d3 Mon Sep 17 00:00:00 2001 From: Martijn van Beurden Date: Tue, 1 Aug 2023 09:04:16 +0200 Subject: [PATCH] Add multithreading to CMake --- CMakeLists.txt | 11 +++++++++++ config.cmake.h.in | 3 +++ src/libFLAC/CMakeLists.txt | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb23b7d9..a81ff6f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ option(INSTALL_PKGCONFIG_MODULES "Install PkgConfig modules" ON) option(INSTALL_CMAKE_CONFIG_MODULE "Install CMake package-config module" ON) option(WITH_OGG "ogg support (default: test for libogg)" ON) option(BUILD_SHARED_LIBS "Build shared instead of static libraries" OFF) +option(ENABLE_MULTITHREADING "Enable multithreading if pthreads is available" ON) set(VERSION ${PROJECT_VERSION}) @@ -113,6 +114,7 @@ check_include_file("inttypes.h" HAVE_INTTYPES_H) check_include_file("stdint.h" HAVE_STDINT_H) check_include_file("stdbool.h" HAVE_STDBOOL_H) check_include_file("arm_neon.h" FLAC__HAS_NEONINTRIN) +check_include_file("semaphore.h" HAVE_SEMAPHORE_H) if(NOT HAVE_STDINT_H OR NOT HAVE_STDBOOL_H) message(SEND_ERROR "Header stdint.h and/or stdbool.h not found") @@ -198,6 +200,15 @@ if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo) add_definitions(-DFLAC__OVERFLOW_DETECT) endif() +if(ENABLE_MULTITHREADING AND HAVE_SEMAPHORE_H) + set(CMAKE_THREAD_PREFER_PTHREAD TRUE) + set(THREADS_PREFER_PTHREAD_FLAG TRUE) + find_package(Threads) + if(CMAKE_USE_PTHREADS_INIT) + set(HAVE_PTHREAD 1) + endif() +endif() + add_subdirectory("src") add_subdirectory("microbench") if(BUILD_DOCS) diff --git a/config.cmake.h.in b/config.cmake.h.in index acc73f08..1aeaffc8 100644 --- a/config.cmake.h.in +++ b/config.cmake.h.in @@ -88,6 +88,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_MEMORY_H +/* Define if multithreading is enabled */ +#cmakedefine HAVE_PTHREAD 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_STDINT_H diff --git a/src/libFLAC/CMakeLists.txt b/src/libFLAC/CMakeLists.txt index cf7368f6..6319da6f 100644 --- a/src/libFLAC/CMakeLists.txt +++ b/src/libFLAC/CMakeLists.txt @@ -108,6 +108,10 @@ else() endif() endif() +if(HAVE_PTHREAD) + target_link_libraries(FLAC PUBLIC Threads::Threads) +endif() + add_library(FLAC::FLAC ALIAS FLAC) install(TARGETS FLAC EXPORT targets