Skip to content
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

build: add support for nesting the Swift subdirectory #860

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
81 changes: 32 additions & 49 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@

cmake_minimum_required(VERSION 3.15.1)
cmake_minimum_required(VERSION 3.26...3.29)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)

# NOTE(compnerd) enable CMP0091 - select MSVC runtime based on
# CMAKE_MSVC_RUNTIME_LIBRARY. Requires CMake 3.15 or newer.
if(POLICY CMP0091)
cmake_policy(SET CMP0091 NEW)
endif()

project(dispatch
VERSION 1.3
LANGUAGES C CXX)

if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
include(ClangClCompileRules)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
if(NOT MINGW)
include(DispatchWindowsSupport)
dispatch_windows_arch_spelling(${CMAKE_SYSTEM_PROCESSOR} DISPATCH_MSVC_ARCH)
dispatch_windows_include_for_arch(${DISPATCH_MSVC_ARCH} DISPATCH_INCLUDES)
include_directories(BEFORE SYSTEM ${DISPATCH_INCLUDES})
dispatch_windows_lib_for_arch(${CMAKE_SYSTEM_PROCESSOR} DISPATCH_LIBDIR)
link_directories(${DISPATCH_LIBDIR})
endif()

if(WIN32)
include(CheckCSourceCompiles)
include(CheckSymbolExists)

Expand Down Expand Up @@ -110,6 +91,11 @@ set(CMAKE_C_VISIBILITY_INLINES_HIDDEN YES)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

set(CMAKE_POSITION_INDEPENDENT_CODE YES)
if(NOT APPLE)
set(CMAKE_INSTALL_RPATH "$ORIGIN")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I want to override the RPATH?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question - how would you do that today? I don't think that this was something that was previously available.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this?

if(NOT APPLE AND NOT DEFINED CMAKE_INSTALL_RPATH)
  set(CMAKE_INSTALL_RPATH "$ORIGIN")
endif()

What is the CMake default? Nothing?

Alternatively, maybe something like this?

if(NOT APPLE)
  list(APPEND CMAKE_INSTALL_RPATH "$ORIGIN")
endif()

I think I prefer the former though.

No, it wasn't previously available, but as long as we're fixing all the things, might as well fix this too.

endif()

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
if(ANDROID)
Expand All @@ -131,7 +117,6 @@ include(DispatchAppleOptions)
include(DispatchSanitization)
include(DispatchCompilerWarnings)
include(DTrace)
include(SwiftSupport)

# NOTE(abdulras) this is the CMake supported way to control whether we generate
# shared or static libraries. This impacts the behaviour of `add_library` in
Expand All @@ -145,7 +130,7 @@ set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR})

option(ENABLE_DTRACE "enable dtrace support" "")

if(CMAKE_SYSTEM_NAME STREQUAL Darwin OR CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
if(APPLE OR BSD)
set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT OFF)
else()
set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT ON)
Expand All @@ -170,6 +155,14 @@ option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as
option(ENABLE_SWIFT "enable libdispatch swift overlay" OFF)
if(ENABLE_SWIFT)
enable_language(Swift)

include(PlatformInfo)

option(Dispatch_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" NO)
set(Dispatch_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${Dispatch_PLATFORM_SUBDIR}$<$<BOOL:${Dispatch_INSTALL_NESTED_SUBDIR}>:/${Dispatch_ARCH_SUBDIR}>")
set(Dispatch_INSTALL_SWIFTMODULEDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${Dispatch_PLATFORM_SUBDIR}$<$<BOOL:${Dispatch_INSTALL_NESTED_SUBDIR}>:/${Dispatch_ARCH_SUBDIR}>")
else()
set(Dispatch_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
endif()

option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via _Thread_local" ON)
Expand All @@ -189,7 +182,7 @@ if(__BUILTIN_TRAP)
set(HAVE_NORETURN_BUILTIN_TRAP 1)
endif()

if(NOT CMAKE_SYSTEM_NAME STREQUAL Android)
if(NOT ANDROID)
find_package(LibRT)
endif()

Expand Down Expand Up @@ -244,12 +237,12 @@ if(HAVE_MACH)
else()
set(USE_MACH_SEM 0)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:USE_WIN32_SEM>)
if(WIN32)
add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:USE_WIN32_SEM>)
endif()
check_library_exists(pthread sem_init "" USE_POSIX_SEM)
# NOTE: android has not always provided a libpthread, but uses the pthreads API
if(CMAKE_SYSTEM_NAME STREQUAL Android)
if(ANDROID)
set(USE_POSIX_SEM 1)
endif()

Expand All @@ -275,16 +268,16 @@ check_symbol_exists(VQ_FREE_SPACE_CHANGE "sys/mount.h" HAVE_DECL_VQ_FREE_SPACE_C
check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY)
check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
if (HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:_GNU_SOURCE=1>)
add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:_GNU_SOURCE=1>)
endif()
check_symbol_exists(__printflike "bsd/sys/cdefs.h" HAVE_PRINTFLIKE)

if(CMAKE_SYSTEM_NAME STREQUAL Android)
if(ANDROID)
set(ENABLE_DTRACE_DEFAULT OFF)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:_WITH_DPRINTF>)
if(BSD)
add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:_WITH_DPRINTF>)
endif()

if(ENABLE_DTRACE)
Expand All @@ -295,9 +288,9 @@ if(ENABLE_DTRACE)
endif()

if(dtrace_EXECUTABLE)
add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:DISPATCH_USE_DTRACE=1>)
add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:DISPATCH_USE_DTRACE=1>)
else()
add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:DISPATCH_USE_DTRACE=0>)
add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:DISPATCH_USE_DTRACE=0>)
endif()

find_program(leaks_EXECUTABLE leaks)
Expand All @@ -306,34 +299,24 @@ if(leaks_EXECUTABLE)
endif()


if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/darwin/module.modulemap>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/private/darwin/module.modulemap>)
if(APPLE)
add_compile_options($<:$<COMPILE_LANGUAGE:C,CXX>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/darwin/module.modulemap>
$<:$<COMPILE_LANGUAGE:C,CXX>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/private/darwin/module.modulemap>)
else()
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/generic/module.modulemap>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/private/generic/module.modulemap>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/generic/module.modulemap>
$<$<COMPILE_LANGUAGE:C,CXX>:-fmodule-map-file=${PROJECT_SOURCE_DIR}/private/generic/module.modulemap>)
endif()

configure_file("${PROJECT_SOURCE_DIR}/cmake/config.h.in"
"${PROJECT_BINARY_DIR}/config/config_ac.h")
add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:HAVE_CONFIG_H>)
add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:HAVE_CONFIG_H>)


if(ENABLE_SWIFT)
if(NOT SWIFT_SYSTEM_NAME)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(SWIFT_SYSTEM_NAME macosx)
else()
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
endif()
endif()

set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SWIFT_SYSTEM_NAME}" CACHE PATH "Path where the libraries will be installed")
set(INSTALL_DISPATCH_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
set(INSTALL_BLOCK_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
set(INSTALL_OS_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/os" CACHE PATH "Path where the os/ headers will be installed")
else()
set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}" CACHE PATH "Path where the libraries will be installed")
set(INSTALL_DISPATCH_HEADERS_DIR "include/dispatch" CACHE PATH "Path where the headers will be installed")
set(INSTALL_BLOCK_HEADERS_DIR "include" CACHE PATH "Path where the headers will be installed for the blocks runtime")
set(INSTALL_OS_HEADERS_DIR "include/os" CACHE PATH "Path where the headers will be installed")
Expand Down
3 changes: 3 additions & 0 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,6 @@

/* Define if using Darwin $NOCANCEL */
#cmakedefine __DARWIN_NON_CANCELABLE

/* Define to 1 if you have the `strlcpy` function. */
#cmakedefine01 HAVE_STRLCPY
5 changes: 0 additions & 5 deletions cmake/modules/ClangClCompileRules.cmake

This file was deleted.

Loading