Skip to content

Commit

Permalink
Fix build for NetBSD (and presumably DragonFly BSD)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ericson2314 authored and TheTumultuousUnicornOfDarkness committed Sep 23, 2023
1 parent f1c96e1 commit 09071d2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
27 changes: 27 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions libcpuid/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

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}")
Expand Down

0 comments on commit 09071d2

Please sign in to comment.