From 09071d20f303c12559475a1c923f34b24fff6a83 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 19 May 2023 00:57:27 -0400 Subject: [PATCH] Fix build for NetBSD (and presumably DragonFly BSD) These both use POSIX threads. I got a link error when cross-compiling using Nixpkgs (Linux -> NetBSD) that went away once I passed `-pthread`. The autoconf is crafted to have the same conditional as the C code itself. --- CMakeLists.txt | 12 ++++++++++++ configure.ac | 27 +++++++++++++++++++++++++++ libcpuid/CMakeLists.txt | 1 + 3 files changed, 40 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 209688f6..34eb2cf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,18 @@ option(LIBCPUID_TESTS "Enable building tests" OFF) set(CMAKE_CXX_STANDARD 11) set(CMAKE_C_STANDARD 99) +# pthreads library +if(${CMAKE_SYSTEM_NAME} STREQUAL "DragonFly" OR ${CMAKE_SYSTEM_NAME} STREQUAL "NetBSD") + find_package(Threads REQUIRED) +endif() + +# check if popcount64 is available +include(CheckSymbolExists) +check_symbol_exists(popcount64 "string.h" HAVE_POPCOUNT64) +if(HAVE_POPCOUNT64) + add_definitions(-DHAVE_POPCOUNT64) +endif(HAVE_POPCOUNT64) + # Global variables list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") diff --git a/configure.ac b/configure.ac index d2d92538..ccdbbbb9 100644 --- a/configure.ac +++ b/configure.ac @@ -62,18 +62,45 @@ fi AC_CANONICAL_HOST +AC_MSG_CHECKING([Whether we need POSIX threads]) +AC_COMPILE_IFELSE([ + AC_LANG_PROGRAM([ + #if defined(__NetBSD__) || defined(__DragonFly__) + #else + # error "no pthreads needed" + #endif + ]) +], [ + AC_MSG_RESULT([yes]) + AC_SUBST([LIBCPUID_LDFLAGS], ["-pthread"]) +], [ + AC_MSG_RESULT([no]) +]) + build_windows=no +build_netbsd=no +build_dragonflybsd=no case "${host_os}" in cygwin*|mingw*) build_windows=yes ;; + netbsd*) + build_netbsd=yes + ;; + dragonfly*) + build_dragonflybsd=yes + ;; esac if test "$build_windows" = "no"; then AM_CPPFLAGS="$AM_CPPFLAGS -D_GNU_SOURCE" fi +if test "$build_netbsd" = "yes" || test "$build_dragonflybsd" = "yes"; then + AM_LDFLAGS="$AM_LDFLAGS -pthread" +fi + AM_CONDITIONAL([WINDOWS], [test "$build_windows" = "yes"]) AC_SUBST(AM_CPPFLAGS) diff --git a/libcpuid/CMakeLists.txt b/libcpuid/CMakeLists.txt index 418be3fd..dcad1bf7 100644 --- a/libcpuid/CMakeLists.txt +++ b/libcpuid/CMakeLists.txt @@ -23,6 +23,7 @@ add_library(cpuid ${cpuid_sources}) set_property(TARGET cpuid PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON) target_include_directories(cpuid SYSTEM PUBLIC $) +target_link_libraries(cpuid ${CMAKE_THREAD_LIBS_INIT}) target_compile_definitions(cpuid PRIVATE VERSION="${PROJECT_VERSION}") set_target_properties(cpuid PROPERTIES VERSION "${LIBCPUID_CURRENT}.${LIBCPUID_AGE}.${LIBCPUID_REVISION}") set_target_properties(cpuid PROPERTIES SOVERSION "${LIBCPUID_CURRENT}")