Merge pull request #527 from apple/revert-526-revert-519-swift-support

Revert "Revert "build: port to new Swift support""
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 46e3aa5..10a0e46 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,22 +1,38 @@
 
-cmake_minimum_required(VERSION 3.4.3)
+cmake_minimum_required(VERSION 3.15.1)
 
-list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
+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)
+  VERSION 1.3
+  LANGUAGES C CXX)
 
 if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
   include(ClangClCompileRules)
 endif()
 
+if(CMAKE_SYSTEM_NAME STREQUAL Windows)
+  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()
+
 set(CMAKE_C_STANDARD 11)
 set(CMAKE_C_STANDARD_REQUIRED YES)
 
 set(CMAKE_CXX_STANDARD 11)
 
 set(CMAKE_C_VISIBILITY_PRESET hidden)
+set(CMAKE_C_VISIBILITY_INLINES_HIDDEN YES)
 
 # NOTE(compnerd) this is a horrible workaround for Windows to ensure that the
 # tests can run as there is no rpath equivalent and `PATH` is used to lookup the
@@ -28,74 +44,37 @@
 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
 find_package(Threads REQUIRED)
 
+include(CheckCCompilerFlag)
 include(CheckCSourceCompiles)
 include(CheckFunctionExists)
 include(CheckIncludeFiles)
 include(CheckLibraryExists)
 include(CheckSymbolExists)
 include(GNUInstallDirs)
-include(SwiftSupport)
 include(CTest)
 
-set(SWIFT_LIBDIR "lib" CACHE PATH "Library folder name, defined by swift main buildscript")
-set(INSTALL_LIBDIR "${SWIFT_LIBDIR}" CACHE PATH "Path where the libraries should be installed")
-
 include(DispatchAppleOptions)
 include(DispatchSanitization)
-
 include(DispatchCompilerWarnings)
-dispatch_common_warnings()
-
-option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" ON)
-set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR})
+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
 # what type of library it generates.
 option(BUILD_SHARED_LIBS "build shared libraries" ON)
 
-option(ENABLE_SWIFT "enable libdispatch swift overlay" OFF)
-if(ENABLE_SWIFT)
-  if(NOT CMAKE_SWIFT_COMPILER)
-    message(FATAL_ERROR "CMAKE_SWIFT_COMPILER must be defined to enable swift")
-  endif()
-
-  string(TOLOWER ${CMAKE_SYSTEM_NAME} swift_os)
-  get_swift_host_arch(swift_arch)
-
-  if(BUILD_SHARED_LIBS)
-    set(swift_dir swift)
-  else()
-    set(swift_dir swift_static)
-  endif()
-
-  set(INSTALL_TARGET_DIR "${INSTALL_LIBDIR}/${swift_dir}/${swift_os}" CACHE PATH "Path where the libraries will be installed")
-  set(INSTALL_DISPATCH_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
-  set(INSTALL_BLOCK_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
-  set(INSTALL_OS_HEADERS_DIR "${INSTALL_LIBDIR}/${swift_dir}/os" CACHE PATH "Path where the os/ headers will be installed")
-endif()
-
-if(NOT ENABLE_SWIFT)
-  set(INSTALL_TARGET_DIR "${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")
-endif()
-
 option(DISPATCH_ENABLE_ASSERTS "enable debug assertions" FALSE)
 
+option(ENABLE_DISPATCH_INIT_CONSTRUCTOR "enable libdispatch_init as a constructor" ON)
+set(USE_LIBDISPATCH_INIT_CONSTRUCTOR ${ENABLE_DISPATCH_INIT_CONSTRUCTOR})
+
 option(ENABLE_DTRACE "enable dtrace support" "")
 
-option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via _Thread_local" ON)
-set(DISPATCH_USE_THREAD_LOCAL_STORAGE ${ENABLE_THREAD_LOCAL_STORAGE})
-
-if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
-   CMAKE_SYSTEM_NAME STREQUAL Android OR
-   CMAKE_SYSTEM_NAME STREQUAL FreeBSD OR
-   CMAKE_SYSTEM_NAME STREQUAL Windows)
-  set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT ON)
-else()
+if(CMAKE_SYSTEM_NAME STREQUAL Darwin OR CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
   set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT OFF)
+else()
+  set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT ON)
 endif()
 option(ENABLE_INTERNAL_PTHREAD_WORKQUEUES "use libdispatch's own implementation of pthread workqueues" ${ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT})
 if(ENABLE_INTERNAL_PTHREAD_WORKQUEUES)
@@ -114,6 +93,15 @@
 
 option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as the public ones" OFF)
 
+option(ENABLE_SWIFT "enable libdispatch swift overlay" OFF)
+if(ENABLE_SWIFT)
+  enable_language(Swift)
+endif()
+
+option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via _Thread_local" ON)
+set(DISPATCH_USE_THREAD_LOCAL_STORAGE ${ENABLE_THREAD_LOCAL_STORAGE})
+
+
 check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)
 if(_GNU_SOURCE)
   set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE)
@@ -144,8 +132,6 @@
 check_function_exists(sysconf HAVE_SYSCONF)
 check_function_exists(arc4random HAVE_ARC4RANDOM)
 
-find_package(Threads REQUIRED)
-
 check_include_files("TargetConditionals.h" HAVE_TARGETCONDITIONALS_H)
 check_include_files("dlfcn.h" HAVE_DLFCN_H)
 check_include_files("fcntl.h" HAVE_FCNTL_H)
@@ -181,7 +167,7 @@
   set(USE_MACH_SEM 0)
 endif()
 if(CMAKE_SYSTEM_NAME STREQUAL Windows)
-  add_definitions(-DUSE_WIN32_SEM)
+  add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE: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
@@ -211,7 +197,7 @@
 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_definitions(-D_GNU_SOURCE=1)
+  add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:_GNU_SOURCE=1>)
 endif()
 check_symbol_exists(__printflike "bsd/sys/cdefs.h" HAVE_PRINTFLIKE)
 
@@ -220,24 +206,20 @@
 endif()
 
 if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
-  add_definitions(-D_WITH_DPRINTF)
+  add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:_WITH_DPRINTF>)
 endif()
 
-if(ENABLE_DTRACE STREQUAL "")
+if(ENABLE_DTRACE)
   find_program(dtrace_EXECUTABLE dtrace)
-  if(dtrace_EXECUTABLE)
-    add_definitions(-DDISPATCH_USE_DTRACE=1)
-  else()
-    add_definitions(-DDISPATCH_USE_DTRACE=0)
-  endif()
-elseif(ENABLE_DTRACE)
-  find_program(dtrace_EXECUTABLE dtrace)
-  if(NOT dtrace_EXECUTABLE)
+  if(NOT dtrace_EXECUTABLE AND NOT ENABLE_DTRACE STREQUAL "")
     message(FATAL_ERROR "dtrace not found but explicitly requested")
   endif()
-  add_definitions(-DDISPATCH_USE_DTRACE=1)
+endif()
+
+if(dtrace_EXECUTABLE)
+  add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:DISPATCH_USE_DTRACE=1>)
 else()
-  add_definitions(-DDISPATCH_USE_DTRACE=0)
+  add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:DISPATCH_USE_DTRACE=0>)
 endif()
 
 find_program(leaks_EXECUTABLE leaks)
@@ -245,6 +227,7 @@
   set(HAVE_LEAKS TRUE)
 endif()
 
+
 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
   add_custom_command(OUTPUT
                        "${PROJECT_SOURCE_DIR}/dispatch/module.modulemap"
@@ -266,19 +249,25 @@
                   DEPENDS
                      "${PROJECT_SOURCE_DIR}/dispatch/module.modulemap"
                      "${PROJECT_SOURCE_DIR}/private/module.modulemap")
+
 configure_file("${PROJECT_SOURCE_DIR}/cmake/config.h.in"
                "${PROJECT_BINARY_DIR}/config/config_ac.h")
-add_definitions(-DHAVE_CONFIG_H)
+add_compile_definitions($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:HAVE_CONFIG_H>)
 
-if(CMAKE_SYSTEM_NAME STREQUAL Windows)
-  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})
+
+if(ENABLE_SWIFT)
+  set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/$<LOWER_CASE:${CMAKE_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")
 endif()
 
+
 add_subdirectory(dispatch)
 add_subdirectory(man)
 add_subdirectory(os)
@@ -287,4 +276,3 @@
 if(BUILD_TESTING)
   add_subdirectory(tests)
 endif()
-
diff --git a/cmake/modules/DispatchCompilerWarnings.cmake b/cmake/modules/DispatchCompilerWarnings.cmake
index d568c72..6ef9d31 100644
--- a/cmake/modules/DispatchCompilerWarnings.cmake
+++ b/cmake/modules/DispatchCompilerWarnings.cmake
@@ -1,79 +1,75 @@
 
 if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
   # TODO: someone needs to provide the msvc equivalent warning flags
-  macro(dispatch_common_warnings)
-  endmacro()
 else()
-  macro(dispatch_common_warnings)
-    add_compile_options(-Werror)
-    add_compile_options(-Wall)
-    add_compile_options(-Wextra)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Werror>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wall>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wextra>)
 
-    add_compile_options(-Warray-bounds-pointer-arithmetic)
-    add_compile_options(-Wassign-enum)
-    add_compile_options(-Watomic-properties)
-    add_compile_options(-Wcomma)
-    add_compile_options(-Wconditional-uninitialized)
-    add_compile_options(-Wconversion)
-    add_compile_options(-Wcovered-switch-default)
-    add_compile_options(-Wdate-time)
-    add_compile_options(-Wdeprecated)
-    add_compile_options(-Wdocumentation)
-    add_compile_options(-Wdouble-promotion)
-    add_compile_options(-Wduplicate-enum)
-    add_compile_options(-Wexpansion-to-defined)
-    add_compile_options(-Wfloat-equal)
-    add_compile_options(-Widiomatic-parentheses)
-    add_compile_options(-Winfinite-recursion)
-    add_compile_options(-Wmissing-prototypes)
-    add_compile_options(-Wnewline-eof)
-    add_compile_options(-Wnullable-to-nonnull-conversion)
-    add_compile_options(-Wobjc-interface-ivars)
-    add_compile_options(-Wover-aligned)
-    add_compile_options(-Wpacked)
-    add_compile_options(-Wpointer-arith)
-    add_compile_options(-Wselector)
-    add_compile_options(-Wshadow)
-    add_compile_options(-Wshorten-64-to-32)
-    add_compile_options(-Wsign-conversion)
-    add_compile_options(-Wstatic-in-inline)
-    add_compile_options(-Wsuper-class-method-mismatch)
-    add_compile_options(-Wswitch)
-    add_compile_options(-Wunguarded-availability)
-    add_compile_options(-Wunreachable-code)
-    add_compile_options(-Wunused)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Warray-bounds-pointer-arithmetic>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wassign-enum>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Watomic-properties>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wcomma>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wconditional-uninitialized>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wconversion>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wcovered-switch-default>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wdate-time>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wdeprecated>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wdocumentation>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wdouble-promotion>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wduplicate-enum>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wexpansion-to-defined>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wfloat-equal>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Widiomatic-parentheses>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Winfinite-recursion>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wmissing-prototypes>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wnewline-eof>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wnullable-to-nonnull-conversion>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wobjc-interface-ivars>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wover-aligned>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wpacked>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wpointer-arith>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wselector>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wshadow>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wshorten-64-to-32>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wsign-conversion>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wstatic-in-inline>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wsuper-class-method-mismatch>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wswitch>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wunguarded-availability>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wunreachable-code>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wunused>)
 
-    add_compile_options(-Wno-unknown-warning-option)
-    add_compile_options(-Wno-trigraphs)
-    add_compile_options(-Wno-four-char-constants)
-    add_compile_options(-Wno-disabled-macro-expansion)
-    add_compile_options(-Wno-pedantic)
-    add_compile_options(-Wno-bad-function-cast)
-    add_compile_options(-Wno-c++-compat)
-    add_compile_options(-Wno-c++98-compat)
-    add_compile_options(-Wno-c++98-compat-pedantic)
-    add_compile_options(-Wno-cast-align)
-    add_compile_options(-Wno-cast-qual)
-    add_compile_options(-Wno-documentation-unknown-command)
-    add_compile_options(-Wno-format-nonliteral)
-    add_compile_options(-Wno-missing-variable-declarations)
-    add_compile_options(-Wno-old-style-cast)
-    add_compile_options(-Wno-padded)
-    add_compile_options(-Wno-reserved-id-macro)
-    add_compile_options(-Wno-shift-sign-overflow)
-    add_compile_options(-Wno-undef)
-    add_compile_options(-Wno-unreachable-code-aggressive)
-    add_compile_options(-Wno-unused-macros)
-    add_compile_options(-Wno-used-but-marked-unused)
-    add_compile_options(-Wno-vla)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-unknown-warning-option>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-trigraphs>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-four-char-constants>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-disabled-macro-expansion>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-pedantic>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-bad-function-cast>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-c++-compat>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-c++98-compat>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-c++98-compat-pedantic>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-cast-align>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-cast-qual>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-documentation-unknown-command>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-format-nonliteral>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-missing-variable-declarations>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-old-style-cast>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-padded>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-reserved-id-macro>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-shift-sign-overflow>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-undef>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-unreachable-code-aggressive>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-unused-macros>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-used-but-marked-unused>)
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-vla>)
 
-    if(CMAKE_SYSTEM_NAME STREQUAL Android)
-      add_compile_options(-Wno-incompatible-function-pointer-types)
-      add_compile_options(-Wno-implicit-function-declaration)
-      add_compile_options(-Wno-conversion)
-      add_compile_options(-Wno-int-conversion)
-      add_compile_options(-Wno-shorten-64-to-32)
-    endif()
-    add_compile_options(-Wno-error=assign-enum)
-  endmacro()
+  if(CMAKE_SYSTEM_NAME STREQUAL Android)
+    add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-incompatible-function-pointer-types>)
+    add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-implicit-function-declaration>)
+    add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-conversion>)
+    add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-int-conversion>)
+    add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-shorten-64-to-32>)
+  endif()
+  add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Wno-error=assign-enum>)
 endif()
diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake
index da7a201..4310b54 100644
--- a/cmake/modules/SwiftSupport.cmake
+++ b/cmake/modules/SwiftSupport.cmake
@@ -1,221 +1,4 @@
 
-include(CMakeParseArguments)
-
-function(add_swift_target target)
-  set(options LIBRARY;SHARED;STATIC)
-  set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT;TARGET)
-  set(multiple_value_options CFLAGS;DEPENDS;LINK_FLAGS;RESOURCES;SOURCES;SWIFT_FLAGS)
-
-  cmake_parse_arguments(AST "${options}" "${single_value_options}" "${multiple_value_options}" ${ARGN})
-
-  set(compile_flags ${CMAKE_SWIFT_FLAGS})
-  set(link_flags ${CMAKE_SWIFT_LINK_FLAGS})
-
-  if(AST_TARGET)
-    list(APPEND compile_flags -target;${AST_TARGET})
-    list(APPEND link_flags -target;${AST_TARGET})
-  endif()
-  if(AST_MODULE_NAME)
-    list(APPEND compile_flags -module-name;${AST_MODULE_NAME})
-  else()
-    list(APPEND compile_flags -module-name;${target})
-  endif()
-  if(AST_MODULE_LINK_NAME)
-    list(APPEND compile_flags -module-link-name;${AST_MODULE_LINK_NAME})
-  endif()
-  if(AST_MODULE_CACHE_PATH)
-    list(APPEND compile_flags -module-cache-path;${AST_MODULE_CACHE_PATH})
-  endif()
-  if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
-    list(APPEND compile_flags -g)
-  endif()
-  if(AST_SWIFT_FLAGS)
-    foreach(flag ${AST_SWIFT_FLAGS})
-      list(APPEND compile_flags ${flag})
-    endforeach()
-  endif()
-  if(AST_CFLAGS)
-    foreach(flag ${AST_CFLAGS})
-      list(APPEND compile_flags -Xcc;${flag})
-    endforeach()
-  endif()
-  if(AST_LINK_FLAGS)
-    foreach(flag ${AST_LINK_FLAGS})
-      list(APPEND link_flags ${flag})
-    endforeach()
-  endif()
-  if(AST_LIBRARY)
-    if(AST_STATIC AND AST_SHARED)
-      message(SEND_ERROR "add_swift_target asked to create library as STATIC and SHARED")
-    elseif(AST_STATIC OR NOT BUILD_SHARED_LIBS)
-      set(library_kind STATIC)
-    elseif(AST_SHARED OR BUILD_SHARED_LIBS)
-      set(library_kind SHARED)
-    endif()
-  else()
-    if(AST_STATIC OR AST_SHARED)
-      message(SEND_ERROR "add_swift_target asked to create executable as STATIC or SHARED")
-    endif()
-  endif()
-  if(NOT AST_OUTPUT)
-    if(AST_LIBRARY)
-      if(AST_SHARED OR BUILD_SHARED_LIBS)
-        set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
-      else()
-        set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_STATIC_LIBRARY_PREFIX}${target}${CMAKE_STATIC_LIBRARY_SUFFIX})
-      endif()
-    else()
-      set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target}${CMAKE_EXECUTABLE_SUFFIX})
-    endif()
-  endif()
-  if(CMAKE_SYSTEM_NAME STREQUAL Windows)
-    if(AST_SHARED OR BUILD_SHARED_LIBS)
-      set(IMPORT_LIBRARY ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_IMPORT_LIBRARY_PREFIX}${target}${CMAKE_IMPORT_LIBRARY_SUFFIX})
-    endif()
-  endif()
-
-  set(sources)
-  foreach(source ${AST_SOURCES})
-    get_filename_component(location ${source} PATH)
-    if(IS_ABSOLUTE ${location})
-      list(APPEND sources ${source})
-    else()
-      list(APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/${source})
-    endif()
-  endforeach()
-
-  set(objs)
-  set(mods)
-  set(docs)
-  set(i 0)
-  foreach(source ${sources})
-    get_filename_component(name ${source} NAME)
-
-    set(obj ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}${CMAKE_C_OUTPUT_EXTENSION})
-    set(mod ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftmodule)
-    set(doc ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${name}.swiftdoc)
-
-    set(all_sources ${sources})
-    list(INSERT all_sources ${i} -primary-file)
-
-    add_custom_command(OUTPUT
-                         ${obj}
-                         ${mod}
-                         ${doc}
-                       DEPENDS
-                         ${source}
-                         ${AST_DEPENDS}
-                       COMMAND
-                         ${CMAKE_SWIFT_COMPILER} -frontend ${compile_flags} -emit-module-path ${mod} -emit-module-doc-path ${doc} -o ${obj} -c ${all_sources})
-
-    list(APPEND objs ${obj})
-    list(APPEND mods ${mod})
-    list(APPEND docs ${doc})
-
-    math(EXPR i "${i}+1")
-  endforeach()
-
-  if(AST_LIBRARY)
-    get_filename_component(module_directory ${AST_MODULE_PATH} DIRECTORY)
-
-    set(module ${AST_MODULE_PATH})
-    set(documentation ${module_directory}/${AST_MODULE_NAME}.swiftdoc)
-
-    add_custom_command(OUTPUT
-                         ${module}
-                         ${documentation}
-                       DEPENDS
-                         ${mods}
-                         ${docs}
-                         ${AST_DEPENDS}
-                       COMMAND
-                         ${CMAKE_SWIFT_COMPILER} -frontend ${compile_flags} -sil-merge-partial-modules -emit-module ${mods} -o ${module} -emit-module-doc-path ${documentation})
-  endif()
-
-  if(AST_LIBRARY)
-    if(CMAKE_SYSTEM_NAME STREQUAL Windows OR CMAKE_SYSTEM_NAME STREQUAL Darwin)
-      set(emit_library -emit-library)
-    else()
-      set(emit_library -emit-library -Xlinker -soname -Xlinker ${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
-    endif()
-  endif()
-  if(NOT AST_LIBRARY OR library_kind STREQUAL SHARED)
-    add_custom_command(OUTPUT
-                         ${AST_OUTPUT}
-                       DEPENDS
-                         ${objs}
-                         ${AST_DEPENDS}
-                       COMMAND
-                         ${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs})
-    add_custom_target(${target}
-                      ALL
-                      DEPENDS
-                         ${AST_OUTPUT}
-                         ${module}
-                         ${documentation})
-  else()
-    add_library(${target}-static STATIC ${objs})
-    add_dependencies(${target}-static ${AST_DEPENDS})
-    get_filename_component(ast_output_bn ${AST_OUTPUT} NAME)
-    if(NOT CMAKE_STATIC_LIBRARY_PREFIX STREQUAL "")
-      string(REGEX REPLACE "^${CMAKE_STATIC_LIBRARY_PREFIX}" "" ast_output_bn ${ast_output_bn})
-    endif()
-    if(NOT CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL "")
-      string(REGEX REPLACE "${CMAKE_STATIC_LIBRARY_SUFFIX}$" "" ast_output_bn ${ast_output_bn})
-    endif()
-    get_filename_component(ast_output_dn ${AST_OUTPUT} DIRECTORY)
-    set_target_properties(${target}-static
-                          PROPERTIES
-                            LINKER_LANGUAGE C
-                            ARCHIVE_OUTPUT_DIRECTORY ${ast_output_dn}
-                            OUTPUT_DIRECTORY ${ast_output_dn}
-                            OUTPUT_NAME ${ast_output_bn})
-    add_custom_target(${target}
-                      ALL
-                      DEPENDS
-                         ${target}-static
-                         ${module}
-                         ${documentation})
-  endif()
-
-  if(AST_RESOURCES)
-    add_custom_command(TARGET
-                         ${target}
-                       POST_BUILD
-                       COMMAND
-                         ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${target}
-                       COMMAND
-                         ${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${target}
-                       COMMAND
-                         ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${target}/Resources
-                       COMMAND
-                         ${CMAKE_COMMAND} -E copy ${AST_RESOURCES} ${CMAKE_CURRENT_BINARY_DIR}/${target}/Resources)
-  else()
-    add_custom_command(TARGET
-                         ${target}
-                       POST_BUILD
-                       COMMAND
-                         ${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR})
-    if(CMAKE_SYSTEM_NAME STREQUAL Windows)
-      if(AST_SHARED OR BUILD_SHARED_LIBS)
-        add_custom_command(TARGET
-                             ${target}
-                           POST_BUILD
-                           COMMAND
-                             ${CMAKE_COMMAND} -E copy ${IMPORT_LIBRARY} ${CMAKE_CURRENT_BINARY_DIR})
-      endif()
-    endif()
-  endif()
-endfunction()
-
-function(add_swift_library library)
-  add_swift_target(${library} LIBRARY ${ARGN})
-endfunction()
-
-function(add_swift_executable executable)
-  add_swift_target(${executable} ${ARGN})
-endfunction()
-
 # Returns the current achitecture name in a variable
 #
 # Usage:
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 58419d4..1207987 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,297 +1,165 @@
-include(CheckCCompilerFlag)
 
-include(SwiftSupport)
-include(DTrace)
-
-add_subdirectory(BlocksRuntime)
+if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
+  add_subdirectory(BlocksRuntime)
+endif()
 
 add_library(dispatch
-              allocator.c
-              apply.c
-              benchmark.c
-              data.c
-              init.c
-              introspection.c
-              io.c
-              mach.c
-              object.c
-              once.c
-              queue.c
-              semaphore.c
-              source.c
-              time.c
-              transform.c
-              voucher.c
-              shims.c
-              protocol.defs
-              provider.d
-              allocator_internal.h
-              data_internal.h
-              inline_internal.h
-              internal.h
-              introspection_internal.h
-              io_internal.h
-              mach_internal.h
-              object_internal.h
-              queue_internal.h
-              semaphore_internal.h
-              shims.h
-              source_internal.h
-              trace.h
-              voucher_internal.h
-              event/event.c
-              event/event_config.h
-              event/event_epoll.c
-              event/event_internal.h
-              event/event_kevent.c
-              event/event_windows.c
-              firehose/firehose_internal.h
-              shims/android_stubs.h
-              shims/atomic.h
-              shims/atomic_sfb.h
-              shims/getprogname.h
-              shims/hw_config.h
-              shims/lock.c
-              shims/lock.h
-              shims/perfmon.h
-              shims/time.h
-              shims/tsd.h
-              shims/yield.c
-              shims/yield.h)
+  allocator.c
+  apply.c
+  benchmark.c
+  data.c
+  init.c
+  introspection.c
+  io.c
+  mach.c
+  object.c
+  once.c
+  queue.c
+  semaphore.c
+  source.c
+  time.c
+  transform.c
+  voucher.c
+  shims.c
+  protocol.defs
+  provider.d
+  allocator_internal.h
+  data_internal.h
+  inline_internal.h
+  internal.h
+  introspection_internal.h
+  io_internal.h
+  mach_internal.h
+  object_internal.h
+  queue_internal.h
+  semaphore_internal.h
+  shims.h
+  source_internal.h
+  trace.h
+  voucher_internal.h
+  event/event.c
+  event/event_config.h
+  event/event_epoll.c
+  event/event_internal.h
+  event/event_kevent.c
+  event/event_windows.c
+  firehose/firehose_internal.h
+  shims/android_stubs.h
+  shims/atomic.h
+  shims/atomic_sfb.h
+  shims/getprogname.h
+  shims/hw_config.h
+  shims/lock.c
+  shims/lock.h
+  shims/perfmon.h
+  shims/time.h
+  shims/tsd.h
+  shims/yield.c
+  shims/yield.h)
 
-set_target_properties(dispatch
-                      PROPERTIES
-                        POSITION_INDEPENDENT_CODE YES)
-
-if(WIN32)
-  target_sources(dispatch
-                 PRIVATE
-                   shims/generic_sys_queue.h
-                   shims/generic_win_stubs.c
-                   shims/generic_win_stubs.h
-                   shims/getprogname.c)
+if(CMAKE_SYSTEM_NAME STREQUAL Windows)
+  target_sources(dispatch PRIVATE
+    shims/generic_sys_queue.h
+    shims/generic_win_stubs.c
+    shims/generic_win_stubs.h
+    shims/getprogname.c)
 endif()
+
 if(DISPATCH_USE_INTERNAL_WORKQUEUE)
-  target_sources(dispatch
-                 PRIVATE
-                   event/workqueue.c
-                   event/workqueue_internal.h)
+  target_sources(dispatch PRIVATE
+    event/workqueue.c
+    event/workqueue_internal.h)
 endif()
-target_sources(dispatch
-               PRIVATE
-                 block.cpp)
+
+target_sources(dispatch PRIVATE
+  block.cpp)
+
+if(ENABLE_DTRACE)
+  dtrace_usdt_probe(${CMAKE_CURRENT_SOURCE_DIR}/provider.d OUTPUT_SOURCES
+    dispatch_dtrace_provider_headers)
+  target_sources(dispatch PRIVATE
+    ${dispatch_dtrace_provider_headers})
+endif()
+
 if(HAVE_OBJC)
   # TODO(compnerd) split DispatchStubs.cc into a separate component for the ObjC
   # registration and a separate component for the swift compiler's emission of a
   # call to the ObjC autorelease elision entry point.
-  target_sources(dispatch
-                 PRIVATE
-                   data.m
-                   object.m
-                   swift/DispatchStubs.cc)
+  target_sources(dispatch PRIVATE
+    data.m
+    object.m
+    swift/DispatchStubs.cc)
 endif()
-if(ENABLE_SWIFT)
-  set(swift_optimization_flags)
-  if(NOT CMAKE_BUILD_TYPE MATCHES Debug)
-    set(swift_optimization_flags -O)
-  endif()
 
-  # NOTE(compnerd) Today regardless of whether or not ObjC interop is enabled,
-  # swift will use an autoreleased return value convention for certain CF
-  # functions (including some that are used/related to dispatch). This means
-  # that the swift compiler in callers to such functions will call the function,
-  # and then pass the result of the function to
-  # objc_retainAutoreleasedReturnValue. In a context where we have ObjC interop
-  # disabled, we do not have access to the objc runtime so an implementation of
-  # objc_retainAutoreleasedReturnValue is not available. To work around this, we
-  # provide a shim for objc_retainAutoreleasedReturnValue in DispatchStubs.cc
-  # that just calls retain on the object. Once we fix the swift compiler to
-  # switch to a different model for handling these arguments with objc-interop
-  # disabled these shims can be eliminated.
-  add_library(DispatchStubs
-              STATIC
-                swift/DispatchStubs.cc)
-  target_include_directories(DispatchStubs
-                             PRIVATE
-                               ${PROJECT_SOURCE_DIR})
-  set_target_properties(DispatchStubs
-                        PROPERTIES
-                          POSITION_INDEPENDENT_CODE YES)
 
-  add_swift_library(swiftDispatch
-                    CFLAGS
-                      -fblocks
-                      -fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/module.modulemap
-                    DEPENDS
-                      module-maps
-                      DispatchStubs
-                    LINK_FLAGS
-                      -L $<TARGET_LINKER_FILE_DIR:DispatchStubs>
-                      -lDispatchStubs
-                      -L $<TARGET_LINKER_FILE_DIR:BlocksRuntime>
-                      -lBlocksRuntime
-                      -L $<TARGET_LINKER_FILE_DIR:dispatch>
-                      -ldispatch
-                      $<$<AND:$<PLATFORM_ID:Windows>,$<CONFIG:Debug>>:-lmsvcrtd>
-                      $<$<AND:$<PLATFORM_ID:Windows>,$<NOT:$<CONFIG:Debug>>>:-lmsvcrt>
-                    MODULE_NAME
-                      Dispatch
-                    MODULE_LINK_NAME
-                      swiftDispatch
-                    MODULE_PATH
-                      ${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
-                    SOURCES
-                      swift/Block.swift
-                      swift/Data.swift
-                      swift/Dispatch.swift
-                      swift/IO.swift
-                      swift/Private.swift
-                      swift/Queue.swift
-                      swift/Source.swift
-                      swift/Time.swift
-                      swift/Wrapper.swift
-                    SWIFT_FLAGS
-                      -I ${PROJECT_SOURCE_DIR}
-                      ${swift_optimization_flags}
-                      $<$<PLATFORM_ID:Windows>:-Xcc>
-                      $<$<PLATFORM_ID:Windows>:-D_MT>
-                      # TODO(compnerd) handle /MT builds
-                      $<$<PLATFORM_ID:Windows>:-Xcc>
-                      $<$<PLATFORM_ID:Windows>:-D_DLL>
-                    TARGET
-                      ${CMAKE_SWIFT_COMPILER_TARGET})
-endif()
-if(ENABLE_DTRACE)
-  dtrace_usdt_probe(${CMAKE_CURRENT_SOURCE_DIR}/provider.d
-                    OUTPUT_SOURCES
-                      dispatch_dtrace_provider_headers)
-  target_sources(dispatch
-                 PRIVATE
-                   ${dispatch_dtrace_provider_headers})
-endif()
-target_include_directories(dispatch
-                           PRIVATE
-                             ${PROJECT_BINARY_DIR}
-                             ${PROJECT_SOURCE_DIR}
-                             ${CMAKE_CURRENT_SOURCE_DIR}
-                             ${CMAKE_CURRENT_BINARY_DIR}
-                             ${PROJECT_SOURCE_DIR}/private)
-target_include_directories(dispatch
-                           SYSTEM BEFORE PRIVATE
-                             "${BlocksRuntime_INCLUDE_DIR}")
-if(WIN32)
-  target_compile_definitions(dispatch
-                             PRIVATE
-                               _CRT_NONSTDC_NO_WARNINGS)
-endif()
-if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
-  target_compile_options(dispatch PRIVATE /EHs-c-)
-else()
-  target_compile_options(dispatch PRIVATE -fno-exceptions)
+set_target_properties(dispatch PROPERTIES
+  POSITION_INDEPENDENT_CODE YES)
+
+target_include_directories(dispatch PRIVATE
+  ${PROJECT_BINARY_DIR}
+  ${PROJECT_SOURCE_DIR}
+  ${CMAKE_CURRENT_SOURCE_DIR}
+  ${CMAKE_CURRENT_BINARY_DIR}
+  ${PROJECT_SOURCE_DIR}/private)
+target_include_directories(dispatch SYSTEM BEFORE PRIVATE
+  "${BlocksRuntime_INCLUDE_DIR}")
+
+if(CMAKE_SYSTEM_NAME STREQUAL Windows)
+  target_compile_definitions(dispatch PRIVATE
+    _CRT_NONSTDC_NO_WARNINGS
+    _CRT_SECURE_NO_WARNINGS)
+elseif(CMAKE_SYSTEM_NAME STREQUAL Android)
+  target_compile_definitions(dispatch PRIVATE
+    -U_GNU_SOURCE)
 endif()
 if(DISPATCH_ENABLE_ASSERTS)
-  target_compile_definitions(dispatch
-                             PRIVATE
-                               -DDISPATCH_DEBUG=1)
+  target_compile_definitions(dispatch PRIVATE
+    -DDISPATCH_DEBUG=1)
 endif()
-if(CMAKE_SYSTEM_NAME STREQUAL Windows)
-  target_compile_definitions(dispatch
-                             PRIVATE
-                               -D_CRT_SECURE_NO_WARNINGS)
-elseif(CMAKE_SYSTEM_NAME STREQUAL Android)
-  target_compile_options(dispatch
-                         PRIVATE
-                           -U_GNU_SOURCE)
-endif()
+
 if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
-  target_compile_options(dispatch
-                         PRIVATE
-                           /W3)
+  target_compile_options(dispatch PRIVATE /EHs-c-)
+  target_compile_options(dispatch PRIVATE /W3)
 else()
-  target_compile_options(dispatch
-                         PRIVATE
-                           -Wall)
+  target_compile_options(dispatch PRIVATE -fno-exceptions)
+  target_compile_options(dispatch PRIVATE -Wall)
 endif()
+
 # FIXME(compnerd) add check for -fblocks?
-if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
-  target_compile_options(dispatch
-                         PRIVATE
-                           -Xclang -fblocks)
-else()
-  check_c_compiler_flag("-momit-leaf-frame-pointer -Werror -Wall -O3" C_SUPPORTS_OMIT_LEAF_FRAME_POINTER)
-  target_compile_options(dispatch PRIVATE -fblocks)
-  if (C_SUPPORTS_OMIT_LEAF_FRAME_POINTER)
-    target_compile_options(dispatch PRIVATE -momit-leaf-frame-pointer)
-  endif()
+target_compile_options(dispatch PRIVATE -fblocks)
+
+check_c_compiler_flag("-momit-leaf-frame-pointer -Werror -Wall -O3" C_SUPPORTS_OMIT_LEAF_FRAME_POINTER)
+if (C_SUPPORTS_OMIT_LEAF_FRAME_POINTER)
+  target_compile_options(dispatch PRIVATE -momit-leaf-frame-pointer)
 endif()
+
 if(LibRT_FOUND)
   target_link_libraries(dispatch PRIVATE RT::rt)
 endif()
-target_link_libraries(dispatch
-                      PRIVATE
-                        Threads::Threads
-                        BlocksRuntime::BlocksRuntime)
+target_link_libraries(dispatch PRIVATE
+  Threads::Threads
+  BlocksRuntime::BlocksRuntime)
 if(CMAKE_SYSTEM_NAME STREQUAL Windows)
-  target_link_libraries(dispatch
-                        PRIVATE
-                          ShLwApi
-                          WS2_32
-                          WinMM
-                          synchronization)
-endif()
-if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
-  set_property(TARGET dispatch
-               APPEND_STRING
-               PROPERTY LINK_FLAGS
-                 "-Xlinker -compatibility_version -Xlinker 1"
-                 "-Xlinker -current_version -Xlinker ${VERSION}"
-                 "-Xlinker -dead_strip"
-                 "-Xlinker -alias_list -Xlinker ${PROJECT_SOURCE_DIR}/xcodeconfig/libdispatch.aliases")
+  target_link_libraries(dispatch PRIVATE
+    ShLwApi
+    WS2_32
+    WinMM
+    synchronization)
 endif()
 
-install(TARGETS
-          dispatch
+if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
+  set_property(TARGET dispatch APPEND_STRING PROPERTY LINK_FLAGS
+    "-Xlinker -compatibility_version -Xlinker 1"
+    "-Xlinker -current_version -Xlinker ${VERSION}"
+    "-Xlinker -dead_strip"
+    "-Xlinker -alias_list -Xlinker ${PROJECT_SOURCE_DIR}/xcodeconfig/libdispatch.aliases")
+endif()
+
+if(ENABLE_SWIFT)
+  add_subdirectory(swift)
+endif()
+
+install(TARGETS dispatch
         ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
         LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
         RUNTIME DESTINATION bin)
-
-if(ENABLE_SWIFT)
-  install(FILES
-            ${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
-            ${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftdoc
-          DESTINATION
-            ${INSTALL_TARGET_DIR}/${swift_arch})
-
-  if(BUILD_SHARED_LIBS)
-    set(library_kind SHARED)
-  else()
-    set(library_kind STATIC)
-  endif()
-
-  set(swiftDispatch_OUTPUT_FILE
-      ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_${library_kind}_LIBRARY_PREFIX}swiftDispatch${CMAKE_${library_kind}_LIBRARY_SUFFIX})
-
-  if(CMAKE_SYSTEM_NAME STREQUAL Windows AND BUILD_SHARED_LIBS)
-    install(FILES
-              ${swiftDispatch_OUTPUT_FILE}
-            DESTINATION
-              bin)
-    install(FILES
-              ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_IMPORT_LIBRARY_PREFIX}swiftDispatch${CMAKE_IMPORT_LIBRARY_SUFFIX}
-            DESTINATION
-              ${INSTALL_TARGET_DIR})
-  else()
-    install(FILES
-              ${swiftDispatch_OUTPUT_FILE}
-            DESTINATION
-              ${INSTALL_TARGET_DIR})
-  endif()
-
-  if(NOT BUILD_SHARED_LIBS)
-    install(FILES
-              $<TARGET_FILE:DispatchStubs>
-            DESTINATION
-              ${INSTALL_TARGET_DIR})
-  endif()
-endif()
-
diff --git a/src/swift/CMakeLists.txt b/src/swift/CMakeLists.txt
new file mode 100644
index 0000000..a10d969
--- /dev/null
+++ b/src/swift/CMakeLists.txt
@@ -0,0 +1,56 @@
+
+# NOTE(compnerd) Today regardless of whether or not ObjC interop is enabled,
+# swift will use an autoreleased return value convention for certain CF
+# functions (including some that are used/related to dispatch). This means that
+# the swift compiler in callers to such functions will call the function, and
+# then pass the result of the function to objc_retainAutoreleasedReturnValue. In
+# a context where we have ObjC interop disabled, we do not have access to the
+# objc runtime so an implementation of objc_retainAutoreleasedReturnValue is not
+# available. To work around this, we provide a shim for
+# objc_retainAutoreleasedReturnValue in DispatchStubs.cc that just calls retain
+# on the object. Once we fix the swift compiler to switch to a different model
+# for handling these arguments with objc-interop disabled these shims can be
+# eliminated.
+add_library(DispatchStubs STATIC
+  DispatchStubs.cc)
+target_include_directories(DispatchStubs PRIVATE
+  ${PROJECT_SOURCE_DIR})
+set_target_properties(DispatchStubs PROPERTIES
+  POSITION_INDEPENDENT_CODE YES)
+
+add_library(swiftDispatch
+  Block.swift
+  Data.swift
+  Dispatch.swift
+  IO.swift
+  Private.swift
+  Queue.swift
+  Source.swift
+  Time.swift
+  Wrapper.swift)
+target_compile_options(swiftDispatch PRIVATE
+  "SHELL:-Xcc -fblocks"
+  "SHELL:-Xcc -fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/module.modulemap"
+  "SHELL:-Xcc -I${PROJECT_SOURCE_DIR}")
+set_target_properties(swiftDispatch PROPERTIES
+  Swift_MODULE_NAME Dispatch
+  Swift_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/swift)
+target_link_libraries(swiftDispatch PRIVATE
+  DispatchStubs
+  BlocksRuntime::BlocksRuntime
+  dispatch)
+add_dependencies(swiftDispatch module-maps)
+
+get_swift_host_arch(swift_arch)
+install(FILES
+  ${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
+  ${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftdoc
+  DESTINATION ${INSTALL_TARGET_DIR}/${swift_arch})
+install(TARGETS swiftDispatch
+  ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
+  LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
+  RUNTIME DESTINATION bin)
+if(NOT BUILD_SHARED_LIBS)
+  install(TARGETS DispatchStubs
+    DESTINATION ${INSTALL_TARGET_DIR})
+endif()