Merge pull request #268 from ktopley-apple/dispatch-sync-fixup-overlay

Fix warnings in DispatchQueue.sync() implementation when using a comp…
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1593569..f6b078e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -31,6 +31,10 @@
 
 # TODO(compnerd) swift options
 
+option(BUILD_SHARED_LIBS "build shared libraries" ON)
+
+option(ENABLE_TESTING "build libdispatch tests" ON)
+
 if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
    CMAKE_SYSTEM_NAME STREQUAL Android)
   set(USE_GOLD_LINKER_DEFAULT ON)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 4ce0e33..2ec2691 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -188,6 +188,15 @@
                  -fuse-ld=gold)
 endif()
 
+# Temporary staging; the various swift projects that depend on libdispatch
+# all expect libdispatch.so to be in src/.libs/libdispatch.so
+# So for now, make a copy so we don't have to do a coordinated commit across
+# all the swift projects to change this assumption.
+add_custom_command(TARGET dispatch POST_BUILD
+                   COMMAND cmake -E make_directory .libs
+                   COMMAND cmake -E copy $<TARGET_FILE:dispatch> .libs
+                   COMMENT "Copying libdispatch to .libs")
+
 install(TARGETS
           dispatch
         DESTINATION
diff --git a/src/swift/DispatchStubs.cc b/src/swift/DispatchStubs.cc
index de309c7..1eaf4bd 100644
--- a/src/swift/DispatchStubs.cc
+++ b/src/swift/DispatchStubs.cc
@@ -199,8 +199,19 @@
 #endif
 SOURCE(WRITE)
 
-// See comment in CFFuntime.c explaining why objc_retainAutoreleasedReturnValue is needed.
+#if !USE_OBJC
+
+// For CF functions with 'Get' semantics, the compiler currently assumes that
+// the result is autoreleased and must be retained. It does so on all platforms
+// by emitting a call to objc_retainAutoreleasedReturnValue. On Darwin, this is
+// implemented by the ObjC runtime. On non-ObjC platforms, there is no runtime,
+// and therefore we have to stub it out here ourselves. The compiler will
+// eventually call swift_release to balance the retain below. This is a
+// workaround until the compiler no longer emits this callout on non-ObjC
+// platforms.
 extern "C" void swift_retain(void *);
+
+SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
 extern "C" void * objc_retainAutoreleasedReturnValue(void *obj) {
     if (obj) {
         swift_retain(obj);
@@ -208,3 +219,5 @@
     }
     else return NULL;
 }
+
+#endif // !USE_OBJC
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 37ab002..b9d7acd 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -39,6 +39,13 @@
                           ${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)
   set(single_value_args)
@@ -77,6 +84,12 @@
                           PRIVATE
                             ${BSD_OVERLAY_LDFLAGS})
   endif()
+  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()
   target_link_libraries(${name} PRIVATE bsdtests)
   add_test(NAME ${name}
            COMMAND bsdtestharness $<TARGET_FILE:${name}>)