Merge pull request #245 from compnerd/windows-cleanups

Windows cleanups
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 08f6fc1..755f3c6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -154,6 +154,10 @@
 check_include_files("objc/objc-internal.h" HAVE_OBJC)
 
 check_library_exists(pthread sem_init "" USE_POSIX_SEM)
+if(CMAKE_SYSTEM_NAME STREQUAL Windows)
+  add_definitions(-DTARGET_OS_WIN32)
+  add_definitions(-DUSE_WIN32_SEM)
+endif()
 
 check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME)
 check_symbol_exists(CLOCK_UPTIME_FAST "time.h" HAVE_DECL_CLOCK_UPTIME_FAST)
diff --git a/dispatch/dispatch.h b/dispatch/dispatch.h
index 585940c..a0622e6 100644
--- a/dispatch/dispatch.h
+++ b/dispatch/dispatch.h
@@ -39,13 +39,17 @@
 #endif
 #endif // __APPLE__
 
+#if HAVE_SYS_CDEFS_H
 #include <sys/cdefs.h>
+#endif
 #include <sys/types.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <stdarg.h>
+#if HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <fcntl.h>
 
 #if defined(__linux__) && defined(__has_feature)
diff --git a/os/object.h b/os/object.h
index b0b4705..b98a93e 100644
--- a/os/object.h
+++ b/os/object.h
@@ -26,10 +26,10 @@
 #include <TargetConditionals.h>
 #include <os/availability.h>
 #endif
-#ifndef __linux__
-#include <os/base.h>
-#else
+#ifdef __linux__
 #include <os/linux_base.h>
+#else
+#include <os/base.h>
 #endif
 
 /*!
diff --git a/os/object_private.h b/os/object_private.h
index 36a807c..ebaf854 100644
--- a/os/object_private.h
+++ b/os/object_private.h
@@ -27,7 +27,9 @@
 #ifndef __OS_OBJECT_PRIVATE__
 #define __OS_OBJECT_PRIVATE__
 
+#if HAVE_SYS_CDEFS_H
 #include <sys/cdefs.h>
+#endif
 #include <stddef.h>
 #include <os/object.h>
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 1c4d963..8bc572b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -109,24 +109,47 @@
                              SYSTEM BEFORE PRIVATE
                                "${WITH_BLOCKS_RUNTIME}")
 endif()
-# TODO(compnerd) make this portable
-target_compile_options(dispatch PRIVATE -fno-exceptions)
+if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
+  target_compile_options(dispatch PRIVATE /EHsc-)
+else()
+  target_compile_options(dispatch PRIVATE -fno-exceptions)
+endif()
 if(DISPATCH_ENABLE_ASSERTS)
   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)
+endif()
 if(BSD_OVERLAY_FOUND)
   target_compile_options(dispatch
                          PRIVATE
                            ${BSD_OVERLAY_CFLAGS})
 endif()
-# FIXME(compnerd) add check for -momit-leaf-frame-pointer?
-target_compile_options(dispatch
-                       PRIVATE
-                         -Wall
-                         -fblocks
-                         -momit-leaf-frame-pointer)
+if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
+  target_compile_options(dispatch
+                         PRIVATE
+                           /W3)
+else()
+  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()
+  # FIXME(compnerd) add check for -momit-leaf-frame-pointer?
+  target_compile_options(dispatch
+                         PRIVATE
+                           -fblocks
+                           -momit-leaf-frame-pointer)
+endif()
 if(BSD_OVERLAY_FOUND)
   target_link_libraries(dispatch PRIVATE ${BSD_OVERLAY_LDFLAGS})
 endif()