root/OpenSceneGraph/trunk/src/OpenThreads/pthreads/CMakeLists.txt @ 11286

Revision 11286, 4.0 kB (checked in by robert, 5 years ago)

Added a compile test for pthread_setaffinity_np to workaround problem with the FreeBSD pthread lib containing the function but the pthread.h header not containing it.

RevLine 
[6326]1# This file should only be included when using Pthreads
2
[7493]3INCLUDE (CheckFunctionExists)
4INCLUDE (CheckLibraryExists)
5INCLUDE (CheckSymbolExists)
6INCLUDE (CheckCXXSourceCompiles)
7
[6326]8SET(LIB_NAME OpenThreads)
9SET(LIB_PUBLIC_HEADERS ${OpenThreads_PUBLIC_HEADERS})
10
11ADD_LIBRARY(${LIB_NAME}
12    ${OPENTHREADS_USER_DEFINED_DYNAMIC_OR_STATIC}
[8211]13    ${LIB_PUBLIC_HEADERS}
[8358]14    PThread.c++
15    PThreadBarrier.c++
16    PThreadBarrierPrivateData.h
17    PThreadCondition.c++
18    PThreadConditionPrivateData.h
19    PThreadMutex.c++
20    PThreadMutexPrivateData.h
21    PThreadPrivateData.h
22    ../common/Version.cpp
[8493]23    ../common/Atomic.cpp
[6326]24)
25
[6707]26IF(OPENTHREADS_SONAMES)
27  SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES VERSION ${OPENTHREADS_VERSION} SOVERSION ${OPENTHREADS_SOVERSION})
[9949]28ENDIF()
[6707]29
[7493]30SET(CMAKE_REQUIRED_LIBRARIES_SAFE "${CMAKE_REQUIRED_LIBRARIES}")
[8358]31SET(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
[7493]32
[11286]33
[7493]34CHECK_FUNCTION_EXISTS(pthread_yield HAVE_PTHREAD_YIELD)
35IF(HAVE_PTHREAD_YIELD)
36  ADD_DEFINITIONS(-DHAVE_PTHREAD_YIELD)
[9949]37ELSE()
[7493]38  # sched_yield appears not in libc, pthreads or whatever on some systems
39  CHECK_FUNCTION_EXISTS(sched_yield HAVE_SCHED_YIELD)
40  IF(NOT HAVE_SCHED_YIELD)
41    CHECK_LIBRARY_EXISTS(rt sched_yield "" HAVE_SCHED_YIELD)
42    IF(HAVE_SCHED_YIELD)
43      SET(CMAKE_THREAD_LIBS_INIT "${CMAKE_THREAD_LIBS_INIT} -lrt")
[9949]44    ENDIF()
45  ENDIF()
[7493]46  IF(HAVE_SCHED_YIELD)
47    ADD_DEFINITIONS(-DHAVE_SCHED_YIELD)
[9949]48  ENDIF()
49ENDIF()
[7493]50
51IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
52  # need to have that for pthread_setaffinity_np on linux
53  ADD_DEFINITIONS(-D_GNU_SOURCE)
54  SET(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE")
[9949]55ENDIF()
[7493]56
57CHECK_FUNCTION_EXISTS(pthread_setconcurrency HAVE_PTHREAD_SETCONCURRENCY)
58IF(HAVE_PTHREAD_SETCONCURRENCY)
59  ADD_DEFINITIONS(-DHAVE_PTHREAD_SETCONCURRENCY)
[9949]60ENDIF()
[7493]61
62CHECK_FUNCTION_EXISTS(pthread_getconcurrency HAVE_PTHREAD_GETCONCURRENCY)
63IF(HAVE_PTHREAD_GETCONCURRENCY)
64  ADD_DEFINITIONS(-DHAVE_PTHREAD_GETCONCURRENCY)
[9949]65ENDIF()
[7493]66
67CHECK_FUNCTION_EXISTS(pthread_setaffinity_np HAVE_PTHREAD_SETAFFINITY_NP)
68IF(HAVE_PTHREAD_SETAFFINITY_NP)
[11286]69  # double check that pthread_setaffinity_np is available as FreeBSD header doesn't contain required function
70  CHECK_CXX_SOURCE_COMPILES("
71        #include <pthread.h>
72        int main() {
73        cpu_set_t cpumask;
74        CPU_ZERO( &cpumask );
75        CPU_SET( 0, &cpumask );
76        pthread_setaffinity_np( pthread_self(), sizeof(cpumask), &cpumask);
77        return 0;
78        }" COMPILES_PTHREAD_SETAFFINITY_NP)
79
80    IF (NOT COMPILES_PTHREAD_SETAFFINITY_NP)
81        SET(HAVE_PTHREAD_SETAFFINITY_NP OFF)
82    ENDIF()
83ENDIF()
84
85IF(HAVE_PTHREAD_SETAFFINITY_NP)
[7493]86  ADD_DEFINITIONS(-DHAVE_PTHREAD_SETAFFINITY_NP)
[9949]87ELSE()
[7493]88  CHECK_CXX_SOURCE_COMPILES("
[11286]89        #include <sched.h>
90        int main() {
91        cpu_set_t cpumask;
92        sched_setaffinity( 0, sizeof(cpumask), &cpumask );
93        return 0;
94        }" HAVE_THREE_PARAM_SCHED_SETAFFINITY)
[7493]95  IF(HAVE_THREE_PARAM_SCHED_SETAFFINITY)
96    ADD_DEFINITIONS(-DHAVE_THREE_PARAM_SCHED_SETAFFINITY)
[9949]97  ELSE()
[7493]98    CHECK_CXX_SOURCE_COMPILES("
99#include <sched.h>
100int main() {
101  cpu_set_t cpumask;
102  sched_setaffinity( 0, &cpumask );
103  return 0;
104}" HAVE_TWO_PARAM_SCHED_SETAFFINITY)
105    IF(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
106      ADD_DEFINITIONS(-DHAVE_TWO_PARAM_SCHED_SETAFFINITY)
[9949]107    ENDIF()
108  ENDIF()
109ENDIF()
[7493]110
111SET(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES_SAFE}")
112
[6326]113TARGET_LINK_LIBRARIES(${LIB_NAME}
[8358]114    ${CMAKE_THREAD_LIBS_INIT}
[6326]115)
116
117# Since we're building different platforms binaries in
118# their respective directories, we need to set the
119# link directory so it can find this location.
120LINK_DIRECTORIES(
[8358]121    ${CMAKE_CURRENT_BINARY_DIR}
[6326]122)
123
124INSTALL(
[8358]125    TARGETS OpenThreads
[9359]126    ARCHIVE DESTINATION lib${LIB_POSTFIX} COMPONENT libopenthreads-dev
[9339]127    LIBRARY DESTINATION lib${LIB_POSTFIX} COMPONENT libopenthreads
128    RUNTIME DESTINATION bin COMPONENT libopenthreads
[6326]129)
130INSTALL(
[8358]131    FILES ${OpenThreads_PUBLIC_HEADERS}
132    DESTINATION include/OpenThreads
[9339]133    COMPONENT libopenthreads-dev
[6326]134)
135
136#commented out# INCLUDE(ModuleInstall OPTIONAL)
Note: See TracBrowser for help on using the browser.