Skip to content

update CMake support #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,32 @@ set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

include(GNUInstallDirs)
include(ExternalProject)

set(WITH_BLOCKS_RUNTIME "" CACHE PATH "Path to blocks runtime")
set(WITH_PTHREAD_WORKQUEUES "" CACHE PATH "Path to pthread-workqueues")

include(DispatchAppleOptions)

option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" ON)
set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR})

# TODO(compnerd) swift options
option(BUILD_SHARED_LIBS "build shared libraries" ON)

option(USE_GOLD_LINKER "use the gold linker" ON)

# TODO(compnerd) consider adding a flag for USE_GOLD_LINKER. Currently, we
# expect the user to specify `-fuse-ld=gold`
# TODO(compnerd) swift options

option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via __thread" ON)
set(DISPATCH_USE_THREAD_LOCAL_STORAGE ${ENABLE_THREAD_LOCAL_STORAGE})

if(EXISTS "${CMAKE_SOURCE_DIR}/libpwq/CMakeLists.txt")
ExternalProject_Add(pwq
SOURCE_DIR
"${CMAKE_SOURCE_DIR}/libpwq"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
BUILD_BYPRODUCTS
<INSTALL_DIR>/${CMAKE_INSTALL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}pthread_workqueue${CMAKE_STATIC_LIBRARY_SUFFIX})
ExternalProject_Get_Property(pwq install_dir)
add_library(PTHREAD::workqueue UNKNOWN IMPORTED)
set_target_properties(PTHREAD::workqueue
PROPERTIES
IMPORTED_LOCATION ${install_dir}/${CMAKE_INSTALL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}pthread_workqueue${CMAKE_STATIC_LIBRARY_SUFFIX})
set(WITH_PTHREAD_WORKQUEUES "${install_dir}" CACHE PATH "Path to pthread-workqueues" FORCE)
set(HAVE_PTHREAD_WORKQUEUES 1)
# TODO: Real CMake logic for internal workqueues
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
CMAKE_SYSTEM_NAME STREQUAL Android OR
CMAKE_SYSTEM_NAME STREQUAL Windows)
set(DISPATCH_USE_INTERNAL_WORKQUEUE 1)
set(HAVE_PTHREAD_WORKQUEUES 0)
else()
# TODO(compnerd) support system installed pthread-workqueues
# find_package(pthread_workqueues REQUIRED)
# set(HAVE_PTHREAD_WORKQUEUES 1)
set(DISPATCH_USE_INTERNAL_WORKQUEUE 0)
set(HAVE_PTHREAD_WORKQUEUES 1)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
Expand Down Expand Up @@ -179,12 +166,13 @@ check_symbol_exists(VQ_VERYLOWDISK "sys/mount.h" HAVE_DECL_VQ_VERYLOWDISK)

check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)

find_program(dtrace_EXECUTABLE dtrace)
if(dtrace_EXECUTABLE)
find_program(DTRACE dtrace)
if(DTRACE)
add_definitions(-DDISPATCH_USE_DTRACE=1)
else()
add_definitions(-DDISPATCH_USE_DTRACE=0)
endif()
include(DTrace)

find_program(leaks_EXECUTABLE leaks)
if(leaks_EXECUTABLE)
Expand Down
5 changes: 2 additions & 3 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

/* Define if building pthread work queues from source */
#cmakedefine BUILD_OWN_PTHREAD_WORKQUEUES
/* Use libdispatch's own implementation of pthread workqueues */
#cmakedefine01 DISPATCH_USE_INTERNAL_WORKQUEUE

/* Enable usage of thread local storage via __thread */
#cmakedefine01 DISPATCH_USE_THREAD_LOCAL_STORAGE
Expand Down
10 changes: 10 additions & 0 deletions cmake/modules/DTrace.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MACRO (DTRACE_HEADER provider header)
if (DTRACE)
ADD_CUSTOM_COMMAND(
OUTPUT ${header}
COMMAND ${DTRACE} -h -s ${provider} -o ${header}
DEPENDS ${provider}
)
ENDIF()
ENDMACRO()

23 changes: 23 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@

include(SwiftSupport)

set(dispatch_DTRACE_HEADERS)
if(DTRACE)
list(APPEND dispatch_DTRACE_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/provider.h)
endif()
DTRACE_HEADER(${CMAKE_CURRENT_SOURCE_DIR}/provider.d ${CMAKE_CURRENT_BINARY_DIR}/provider.h)

set(dispatch_BLOCK_SOURCES block.cpp)
if(HAVE_OBJC)
list(APPEND dispatch_BLOCK_SOURCES data.m object.m)
endif()

set(dispatch_SWIFT_SOURCES)
if(CMAKE_SWIFT_COMPILER)
set(swift_optimization_flags)
Expand Down Expand Up @@ -39,7 +46,9 @@ if(CMAKE_SWIFT_COMPILER)
list(APPEND dispatch_SWIFT_SOURCES
swift/DispatchStubs.cc;${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o)
endif()

add_library(dispatch
${dispatch_DTRACE_HEADERS}
allocator.c
apply.c
benchmark.c
Expand Down Expand Up @@ -77,6 +86,8 @@ add_library(dispatch
event/event_epoll.c
event/event_internal.h
event/event_kevent.c
event/workqueue_internal.h
event/workqueue.c
firehose/firehose_internal.h
shims/android_stubs.h
shims/atomic.h
Expand All @@ -93,12 +104,24 @@ add_library(dispatch
shims/yield.h
${dispatch_BLOCK_SOURCES}
${dispatch_SWIFT_SOURCES})
# Temporary staging; the various swift projects that depend on libdispatch
# all expect libdispatch.so to be in src/.libs/libdispatch.so
# So make a symlink so we don't have to do a coordinated commit across all the
# swift projects to change this assumption.
add_custom_target(.libs ALL
COMMAND cmake -E create_symlink . .libs)
target_include_directories(dispatch
PRIVATE
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/private)
if (USE_GOLD_LINKER)
target_link_libraries(dispatch
PRIVATE
-fuse-ld=gold)
endif()
if(WITH_PTHREAD_WORKQUEUES)
target_include_directories(dispatch
SYSTEM BEFORE PRIVATE
Expand Down
8 changes: 7 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ if(BSD_OVERLAY_FOUND)
PRIVATE
${BSD_OVERLAY_LDFLAGS})
endif()
if(CMAKE_SWIFT_RUNTIME_LIBDIR)
target_link_libraries(bsdtestharness PRIVATE -L${CMAKE_SWIFT_RUNTIME_LIBDIR} -lswiftCore -lswiftSwiftOnoneSupport -Wl,-rpath -Wl,${CMAKE_SWIFT_RUNTIME_LIBDIR})
endif()

function(add_unit_test name)
set(options DISABLED_TEST)
Expand Down Expand Up @@ -86,11 +89,14 @@ function(add_unit_test name)
${BSD_OVERLAY_LDFLAGS})
endif()
target_link_libraries(${name} PRIVATE bsdtests)
if(CMAKE_SWIFT_RUNTIME_LIBDIR)
target_link_libraries(${name} PRIVATE -L${CMAKE_SWIFT_RUNTIME_LIBDIR} -lswiftCore -lswiftSwiftOnoneSupport -Wl,-rpath -Wl,${CMAKE_SWIFT_RUNTIME_LIBDIR})
endif()
add_test(NAME ${name}
COMMAND bsdtestharness $<TARGET_FILE:${name}>)
set_tests_properties(${name}
PROPERTIES
TIMEOUT 30
TIMEOUT 600
DEPENDS bsdtestharness
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(NOT leaks_EXECUTABLE)
Expand Down