SourceKit CMake: use the C compiler and linker flags computed by the Swift build system
diff --git a/tools/SourceKit/CMakeLists.txt b/tools/SourceKit/CMakeLists.txt
index 9a8a6be..6e7d4f7 100644
--- a/tools/SourceKit/CMakeLists.txt
+++ b/tools/SourceKit/CMakeLists.txt
@@ -50,6 +50,43 @@
   endif()
 endfunction()
 
+# Add default compiler and linker flags to 'target'.
+#
+# FIXME: this is a HACK.  All SourceKit CMake code using this function
+# should be rewritten to use 'add_swift_library'.
+function(add_sourcekit_default_compiler_flags target)
+  set(sdk "${SWIFT_HOST_VARIANT_SDK}")
+  set(arch "${SWIFT_HOST_VARIANT_ARCH}")
+  set(c_compile_flags)
+  set(link_flags)
+
+  # Add variant-specific flags.
+  set(build_type "${CMAKE_BUILD_TYPE}")
+  set(enable_assertions "${LLVM_ENABLE_ASSERTIONS}")
+  _add_variant_c_compile_flags(
+      "${sdk}"
+      "${arch}"
+      "${build_type}"
+      "${enable_assertions}"
+      c_compile_flags)
+  _add_variant_link_flags(
+      "${sdk}"
+      "${arch}"
+      "${build_type}"
+      "${enable_assertions}"
+      link_flags)
+
+  # Convert variables to space-separated strings.
+  _list_escape_for_shell("${c_compile_flags}" c_compile_flags)
+  _list_escape_for_shell("${link_flags}" link_flags)
+
+  # Set compilation and link flags.
+  set_property(TARGET "${target}" APPEND_STRING PROPERTY
+      COMPILE_FLAGS " ${c_compile_flags}")
+  set_property(TARGET "${target}" APPEND_STRING PROPERTY
+      LINK_FLAGS " ${link_flags}")
+endfunction()
+
 # Add a new SourceKit library.
 #
 # Usage:
@@ -152,6 +189,7 @@
       ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}"
       RUNTIME DESTINATION "bin")
   set_target_properties(${name} PROPERTIES FOLDER "SourceKit libraries")
+  add_sourcekit_default_compiler_flags("${name}")
 endmacro()
 
 # Add a new SourceKit executable.
@@ -198,6 +236,7 @@
         LINK_FLAGS "-Wl,-exported_symbol,_main")
     endif()
   endif()
+  add_sourcekit_default_compiler_flags("${name}")
 endmacro()
 
 # Add a new SourceKit framework.
@@ -313,6 +352,7 @@
         COMMAND ${CMAKE_COMMAND} -E copy "${hdr}" "${framework_location}/Headers/${hdrname}")
     endforeach()
   endif()
+  add_sourcekit_default_compiler_flags("${name}")
 endmacro(add_sourcekit_framework)
 
 # Add a new SourceKit XPC service to a framework.
@@ -384,6 +424,7 @@
         LINK_FLAGS "-Wl,-exported_symbol,_main")
     endif()
   endif()
+  add_sourcekit_default_compiler_flags("${name}")
 endmacro()
 
 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")