Skip to content

revive the Windows port #344

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

Merged
merged 3 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ project(dispatch
LANGUAGES C CXX)
enable_testing()

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

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED YES)

Expand Down Expand Up @@ -262,10 +266,6 @@ if (HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
endif()
check_symbol_exists(__printflike "bsd/sys/cdefs.h" HAVE_PRINTFLIKE)

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
add_definitions(-DTARGET_OS_WIN32)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Android)
set(ENABLE_DTRACE_DEFAULT OFF)
endif()
Expand Down Expand Up @@ -321,6 +321,13 @@ configure_file("${CMAKE_SOURCE_DIR}/cmake/config.h.in"
"${CMAKE_BINARY_DIR}/config/config_ac.h")
add_definitions(-DHAVE_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})
endif()

add_subdirectory(dispatch)
add_subdirectory(man)
add_subdirectory(os)
Expand Down
8 changes: 8 additions & 0 deletions cmake/modules/ClangClCompileRules.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# clang-cl interprets paths starting with /U as macro undefines, so we need to
# put a -- before the input file path to force it to be treated as a path.
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}")
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT}")

set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")

1 change: 1 addition & 0 deletions cmake/modules/DispatchCompilerWarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ else()
add_compile_options(-Wno-int-conversion)
add_compile_options(-Wno-shorten-64-to-32)
endif()
add_compile_options(-Wno-error=assign-enum)
endmacro()
endif()
74 changes: 74 additions & 0 deletions cmake/modules/DispatchWindowsSupport.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

function(dispatch_windows_arch_spelling arch var)
if(${arch} STREQUAL i686)
set(${var} x86 PARENT_SCOPE)
elseif(${arch} STREQUAL x86_64)
set(${var} x64 PARENT_SCOPE)
elseif(${arch} STREQUAL armv7)
set(${var} arm PARENT_SCOPE)
elseif(${arch} STREQUAL aarch64)
set(${var} arm64 PARENT_SCOPE)
else()
message(FATAL_ERROR "do not know MSVC spelling for ARCH: `${arch}`")
endif()
endfunction()

function(dispatch_verify_windows_environment_variables)
set(VCToolsInstallDir $ENV{VCToolsInstallDir})
set(UniversalCRTSdkDir $ENV{UniversalCRTSdkDir})
set(UCRTVersion $ENV{UCRTVersion})

if("${VCToolsInstallDir}" STREQUAL "")
message(SEND_ERROR "VCToolsInstallDir environment variable must be set")
endif()
if("${UniversalCRTSdkDir}" STREQUAL "")
message(SEND_ERROR "UniversalCRTSdkDir environment variable must be set")
endif()
if("${UCRTVersion}" STREQUAL "")
message(SEND_ERROR "UCRTVersion environment variable must be set")
endif()
endfunction()

function(dispatch_windows_include_for_arch arch var)
dispatch_verify_windows_environment_variables()

set(paths
"$ENV{VCToolsInstallDir}/include"
"$ENV{UniversalCRTSdkDir}/Include/$ENV{UCRTVersion}/ucrt"
"$ENV{UniversalCRTSdkDir}/Include/$ENV{UCRTVersion}/shared"
"$ENV{UniversalCRTSdkDir}/Include/$ENV{UCRTVersion}/um")
set(${var} ${paths} PARENT_SCOPE)
endfunction()

function(dispatch_windows_lib_for_arch arch var)
dispatch_verify_windows_environment_variables()
dispatch_windows_arch_spelling(${arch} ARCH)

set(paths)
if(${ARCH} STREQUAL x86)
list(APPEND paths "$ENV{VCToolsInstallDir}/Lib")
else()
list(APPEND paths "$ENV{VCToolsInstallDir}/Lib/${ARCH}")
endif()
list(APPEND paths
"$ENV{UniversalCRTSdkDir}/Lib/$ENV{UCRTVersion}/ucrt/${ARCH}"
"$ENV{UniversalCRTSdkDir}/Lib/$ENV{UCRTVersion}/um/${ARCH}")
set(${var} ${paths} PARENT_SCOPE)
endfunction()

function(dispatch_windows_generate_sdk_vfs_overlay flags)
dispatch_verify_windows_environment_variables()

get_filename_component(VCToolsInstallDir $ENV{VCToolsInstallDir} ABSOLUTE)
get_filename_component(UniversalCRTSdkDir $ENV{UniversalCRTSdkDir} ABSOLUTE)
set(UCRTVersion $ENV{UCRTVersion})

# TODO(compnerd) use a target to avoid re-creating this file all the time
configure_file("${CMAKE_SOURCE_DIR}/utils/WindowsSDKVFSOverlay.yaml.in"
"${CMAKE_BINARY_DIR}/windows-sdk-vfs-overlay.yaml"
@ONLY)

set(${flags}
-ivfsoverlay;"${CMAKE_BINARY_DIR}/windows-sdk-vfs-overlay.yaml"
PARENT_SCOPE)
endfunction()
21 changes: 13 additions & 8 deletions dispatch/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,20 @@
#endif
#endif

#if TARGET_OS_WIN32 && defined(__DISPATCH_BUILDING_DISPATCH__) && \
defined(__cplusplus)
#define DISPATCH_EXPORT extern "C" extern __declspec(dllexport)
#elif TARGET_OS_WIN32 && defined(__DISPATCH_BUILDING_DISPATCH__)
#if defined(_WIN32)
#if defined(__DISPATCH_BUILDING_DISPATCH__)
#if defined(__cplusplus)
#define DISPATCH_EXPORT extern "C" __declspec(dllexport)
#else
#define DISPATCH_EXPORT extern __declspec(dllexport)
#elif TARGET_OS_WIN32 && defined(__cplusplus)
#define DISPATCH_EXPORT extern "C" extern __declspec(dllimport)
#elif TARGET_OS_WIN32
#endif
#else
#if defined(__cplusplus)
#define DISPATCH_EXPORT extern "C" __declspec(dllimport)
#else
#define DISPATCH_EXPORT extern __declspec(dllimport)
#endif
#endif
#elif __GNUC__
#define DISPATCH_EXPORT extern __attribute__((visibility("default")))
#else
Expand Down Expand Up @@ -203,7 +208,7 @@
#endif
#endif

#if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
#if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums) || defined(_WIN32)
#define DISPATCH_ENUM(name, type, ...) \
typedef enum : type { __VA_ARGS__ } name##_t
#else
Expand Down
5 changes: 0 additions & 5 deletions dispatch/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,10 @@ DISPATCH_EXPORT struct dispatch_data_s _dispatch_data_empty;
#define DISPATCH_DATA_DESTRUCTOR_DEFAULT NULL

#ifdef __BLOCKS__
#if !TARGET_OS_WIN32
/*! @parseOnly */
#define DISPATCH_DATA_DESTRUCTOR_TYPE_DECL(name) \
DISPATCH_EXPORT const dispatch_block_t _dispatch_data_destructor_##name
#else
#define DISPATCH_DATA_DESTRUCTOR_TYPE_DECL(name) \
DISPATCH_EXPORT dispatch_block_t _dispatch_data_destructor_##name
#endif
#else
#define DISPATCH_DATA_DESTRUCTOR_TYPE_DECL(name) \
DISPATCH_EXPORT const dispatch_function_t \
_dispatch_data_destructor_##name
Expand Down
4 changes: 3 additions & 1 deletion dispatch/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <os/availability.h>
#include <TargetConditionals.h>
#include <os/base.h>
#elif defined(__linux__) || defined(__FreeBSD__)
#elif defined(_WIN32)
#include <os/generic_win_base.h>
#elif defined(__unix__)
#include <os/generic_unix_base.h>
#endif

Expand Down
4 changes: 4 additions & 0 deletions dispatch/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ __BEGIN_DECLS
* @typedef dispatch_fd_t
* Native file descriptor type for the platform.
*/
#if defined(_WIN32)
typedef intptr_t dispatch_fd_t;
#else
typedef int dispatch_fd_t;
#endif

/*!
* @functiongroup Dispatch I/O Convenience API
Expand Down
8 changes: 1 addition & 7 deletions dispatch/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ typedef union {
#ifndef DISPATCH_DATA_DECL
#define DISPATCH_DATA_DECL(name) OS_OBJECT_DECL_SWIFT(name)
#endif // DISPATCH_DATA_DECL
#elif !TARGET_OS_WIN32
#else
/*! @parseOnly */
#define DISPATCH_SOURCE_DECL(name) \
DISPATCH_DECL(name);
Expand All @@ -131,12 +131,6 @@ typedef union {
#define DISPATCH_SOURCE_TYPE_DECL(name) \
DISPATCH_EXPORT const struct dispatch_source_type_s \
_dispatch_source_type_##name
#else
#define DISPATCH_SOURCE_DECL(name) \
DISPATCH_DECL(name);
#define DISPATCH_SOURCE_TYPE_DECL(name) \
DISPATCH_EXPORT struct dispatch_source_type_s _dispatch_source_type_##name
#define DISPATCH_DATA_DECL(name) DISPATCH_DECL(name)
#endif

#ifdef __BLOCKS__
Expand Down
2 changes: 1 addition & 1 deletion dispatch/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <mach/message.h>
#endif

#if !TARGET_OS_WIN32
#if !defined(_WIN32)
#include <sys/signal.h>
#endif

Expand Down
132 changes: 132 additions & 0 deletions os/generic_win_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2015 Apple Inc. and the Swift project authors
*
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See http://swift.org/LICENSE.txt for license information
* See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
*
*/

#ifndef __OS_GENERIC_WIN_BASE__
#define __OS_GENERIC_WIN_BASE__

// Unices provide `roundup` via sys/param.h
#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
// Unices provide `MAX` via sys/param.h
#define MAX(a,b) (((a)>(b))?(a):(b))
// Unices provide `MIN` via sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
// Unices provide `howmany` via sys/param.h
#define howmany(x, y) (((x) + ((y) - 1)) / (y))

typedef int mode_t;
typedef void pthread_attr_t;

#if defined(__cplusplus)
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS }
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif

#ifndef API_AVAILABLE
#define API_AVAILABLE(...)
#endif
#ifndef API_DEPRECATED
#define API_DEPRECATED(...)
#endif
#ifndef API_UNAVAILABLE
#define API_UNAVAILABLE(...)
#endif
#ifndef API_DEPRECATED_WITH_REPLACEMENT
#define API_DEPRECATED_WITH_REPLACEMENT(...)
#endif

#if !defined(__has_attribute)
#define __has_attribute(attibute) 0
#endif

#if !defined(__has_builtin)
#define __has_builtin(builtin) 0
#endif

#if !defined(__has_feature)
#define __has_feature(feature) 0
#endif

#if __has_builtin(__builtin_expect)
#define OS_EXPECT(expression, value) __builtin_expect((expression), (value))
#else
#define OS_EXPECT(expression, value) (expression)
#endif

#if __has_attribute(__unused__)
#define OS_UNUSED __attribute__((__unused__))
#else
#define OS_UNUSED
#endif

#ifndef os_likely
#define os_likely(expression) OS_EXPECT(!!(expression), 1)
#endif
#ifndef os_unlikely
#define os_unlikely(expression) OS_EXPECT(!!(expression), 0)
#endif

#if __has_feature(assume_nonnull)
#define OS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
#define OS_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
#else
#define OS_ASSUME_NONNULL_BEGIN
#define OS_ASSUME_NONNULL_END
#endif

#if __has_builtin(__builtin_assume)
#define OS_COMPILER_CAN_ASSUME(expr) __builtin_assume(expr)
#else
#define OS_COMPILER_CAN_ASSUME(expr) ((void)(expr))
#endif

#if __has_feature(attribute_availability_swift)
// equivalent to __SWIFT_UNAVAILABLE from Availability.h
#define OS_SWIFT_UNAVAILABLE(msg) \
__attribute__((__availability__(swift, unavailable, message = msg)))
#else
#define OS_SWIFT_UNAVAILABLE(msg)
#endif

#define __OS_STRINGIFY(s) #s
#define OS_STRINGIFY(s) __OS_STRINGIFY(s)

#if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
#define OS_ENUM(name, type, ...) typedef enum : type { __VA_ARGS__ } name##_t
#else
#define OS_ENUM(name, type, ...) \
enum { __VA_ARGS__ }; \
typedef type name##_t
#endif

#ifdef OS_EXPORT
#undef OS_EXPORT
#endif
#define OS_EXPORT __declspec(dllexport)

#ifdef OS_WARN_RESULT_NEEDS_RELEASE
#undef OS_WARN_RESULT_NEEDS_RELEASE
#endif

#ifdef OS_WARN_RESULT
#undef OS_WARN_RESULT
#endif
#define OS_WARN_RESULT

#ifdef OS_NOTHROW
#undef OS_NOTHROW
#endif
#define OS_NOTHROW

#endif
4 changes: 3 additions & 1 deletion os/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <os/availability.h>
#include <TargetConditionals.h>
#include <os/base.h>
#elif defined(__linux__) || defined(__FreeBSD__)
#elif defined(_WIN32)
#include <os/generic_win_base.h>
#elif defined(__unix__)
#include <os/generic_unix_base.h>
#endif

Expand Down
6 changes: 0 additions & 6 deletions private/data_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,9 @@ dispatch_data_make_memory_entry(dispatch_data_t data);
*/
typedef const struct dispatch_data_format_type_s *dispatch_data_format_type_t;

#if !TARGET_OS_WIN32
#define DISPATCH_DATA_FORMAT_TYPE_DECL(name) \
DISPATCH_EXPORT const struct dispatch_data_format_type_s \
_dispatch_data_format_type_##name
#else
#define DISPATCH_DATA_FORMAT_TYPE_DECL(name) \
DISPATCH_EXPORT struct dispatch_data_format_type_s \
_dispatch_data_format_type_##name
#endif

/*!
* @const DISPATCH_DATA_FORMAT_TYPE_NONE
Expand Down
Loading