Skip to content
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
3 changes: 3 additions & 0 deletions cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ function(handle_swift_sources
if (NOT SWIFTSOURCES_IS_MAIN)
list(APPEND swift_compile_flags "-module-link-name" "${name}")
endif()
if("${SWIFTSOURCES_SDK}" STREQUAL "CYGWIN")
list(APPEND swift_compile_flags -DCYGWIN)
endif()

if(swift_sources)
# Special-case hack to create Swift.o for the core standard library.
Expand Down
6 changes: 3 additions & 3 deletions stdlib/private/SwiftPrivateLibcExtras/Subprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//
//===----------------------------------------------------------------------===//

// Spawn is not available on Android.
#if !defined(__ANDROID__)
// posix_spawn is not available on Android or Windows (MSVC).
#if !defined(__ANDROID__) && (!defined(_WIN32) || defined(__CYGWIN__))

#include "swift/Runtime/Config.h"

Expand Down Expand Up @@ -65,5 +65,5 @@ char ***swift_SwiftPrivateLibcExtras_NSGetEnviron(void) {
return _NSGetEnviron();
}
#endif // defined(__APPLE__)
#endif // defined(__ANDROID__)
#endif // !defined(__ANDROID__) && (!defined(_WIN32) || defined(__CGYWIN__))

5 changes: 4 additions & 1 deletion stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import Glibc
#endif


// posix_spawn is not available on Android.
#if !os(Windows) || CYGWIN
// posix_spawn is not available on Android or Windows.
#if !os(Android)
// swift_posix_spawn isn't available in the public watchOS SDK, we sneak by the
// unavailable attribute declaration here of the APIs that we need.
Expand Down Expand Up @@ -293,3 +294,5 @@ internal func _getEnviron() -> UnsafeMutablePointer<UnsafeMutablePointer<CChar>?
return __environ
#endif
}
#endif

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Darwin
import Glibc
#endif

#if !os(Windows) || CYGWIN
public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt {
#if os(Android)
preconditionFailure("mkstemps doesn't work on Android")
Expand All @@ -32,6 +33,7 @@ public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CIn
return fd
#endif
}
#endif

public var _stdlib_FD_SETSIZE: CInt {
return 1024
Expand Down Expand Up @@ -81,6 +83,7 @@ public struct _stdlib_fd_set {
}
}

#if !os(Windows) || CYGWIN
public func _stdlib_select(
_ readfds: inout _stdlib_fd_set, _ writefds: inout _stdlib_fd_set,
_ errorfds: inout _stdlib_fd_set, _ timeout: UnsafeMutablePointer<timeval>?
Expand All @@ -104,6 +107,7 @@ public func _stdlib_select(
}
}
}
#endif

//
// Functions missing in `Darwin` module.
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ add_swift_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX
DEPENDS glibc_modulemap)

add_swift_library(swiftMSVCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
msvcrt.swift
${swift_platform_sources}
TARGET_SDKS WINDOWS)

set(glibc_modulemap_target_list)
foreach(sdk ${SWIFT_SDKS})
if("${sdk}" STREQUAL "LINUX" OR
Expand Down
18 changes: 17 additions & 1 deletion stdlib/public/Platform/Misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,33 @@
//===----------------------------------------------------------------------===//

#include <fcntl.h>
#if !defined(_WIN32) || defined(__CYGWIN__)
#include <semaphore.h>
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <io.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#if !defined(_WIN32) || defined(__CYGWIN__)
#include <sys/ioctl.h>
#endif

#include "swift/Runtime/Config.h"

#if !defined(_WIN32) || defined(__CYGWIN__)
SWIFT_CC(swift)
extern int _swift_Platform_open(const char *path, int oflag, mode_t mode) {
return open(path, oflag, mode);
}
#else
SWIFT_CC(swift)
extern int _swift_Platform_open(const char *path, int oflag, int mode) {
return _open(path, oflag, mode);
}
#endif

#if !defined(_WIN32) || defined(__CYGWIN__)
SWIFT_CC(swift)
extern int _swift_Platform_openat(int fd, const char *path, int oflag,
mode_t mode) {
Expand Down Expand Up @@ -61,7 +76,8 @@ extern int
_swift_Platform_ioctlPtr(int fd, unsigned long int request, void* ptr) {
return ioctl(fd, request, ptr);
}

#endif

#if defined(__APPLE__)
#define _REENTRANT
#include <math.h>
Expand Down
100 changes: 87 additions & 13 deletions stdlib/public/Platform/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,29 @@ public var errno : Int32 {
get {
Copy link
Contributor

@gribozavr gribozavr Jul 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The #ifs in errno overlay are a mess :( As a separate PR, would you mind changing our implementation strategy to be more portable? We could have a C function that reads errno, and another one that writes it. These functions will use the normal errno macro, so they will be portable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good idea, will do.

#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) || os(FreeBSD) || os(PS4)
return __error().pointee
// FIXME: os(Windows) should be replaced, such as triple(Cygwin)
#elseif os(Android) || os(Windows)
#elseif os(Android)
return __errno().pointee
#elseif os(Windows)
#if CYGWIN
return __errno().pointee
#else
return _errno().pointee
#endif
#else
return __errno_location().pointee
#endif
}
set(val) {
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) || os(FreeBSD) || os(PS4)
return __error().pointee = val
#elseif os(Android) || os(Windows)
#elseif os(Android)
return __errno().pointee = val
#elseif os(Windows)
#if CYGWIN
return __errno().pointee = val
#else
return _errno().pointee = val
#endif
#else
return __errno_location().pointee = val
#endif
Expand Down Expand Up @@ -165,20 +176,31 @@ public func snprintf(ptr: UnsafeMutablePointer<Int8>, _ len: Int, _ format: Unsa
// fcntl.h
//===----------------------------------------------------------------------===//

#if !os(Windows) || CYGWIN
@_silgen_name("_swift_Platform_open")
func _swift_Platform_open(
_ path: UnsafePointer<CChar>,
_ oflag: Int32,
_ mode: mode_t
) -> Int32
#else
@_silgen_name("_swift_Platform_open")
func _swift_Platform_open(
_ path: UnsafePointer<CChar>,
_ oflag: Int32,
_ mode: Int32
) -> Int32
#endif

#if !os(Windows) || CYGWIN
@_silgen_name("_swift_Platform_openat")
func _swift_Platform_openat(
_ fd: Int32,
_ path: UnsafePointer<CChar>,
_ oflag: Int32,
_ mode: mode_t
) -> Int32
#endif

public func open(
_ path: UnsafePointer<CChar>,
Expand All @@ -187,6 +209,7 @@ public func open(
return _swift_Platform_open(path, oflag, 0)
}

#if !os(Windows) || CYGWIN
public func open(
_ path: UnsafePointer<CChar>,
_ oflag: Int32,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've shimmed open() with the mode on Windows, why did you have to hide this overload?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because theres no openat equivalent atm. This is possible to do and unhide this, but, Im trying to get an immediate port going. It would be possible to get an implementation of openat and then unhide this in a follow up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is not openat, this is open with a file mode...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant, that because it calls openat. Now, arguably that is a bug, and we should fix that :-).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, I see what you mean. This was supposed to have an alternate implementation with the signature change. Ill fix this in the upload that Im about to do.

Expand All @@ -211,7 +234,17 @@ public func openat(
) -> Int32 {
return _swift_Platform_openat(fd, path, oflag, mode)
}
#else
public func open(
_ path: UnsafePointer<CChar>,
_ oflag: Int32,
_ mode: Int32
) -> Int32 {
return _swift_Platform_open(path, oflag, mode)
}
#endif

#if !os(Windows) || CYGWIN
@_silgen_name("_swift_Platform_fcntl")
internal func _swift_Platform_fcntl(
_ fd: Int32,
Expand Down Expand Up @@ -248,7 +281,9 @@ public func fcntl(
) -> Int32 {
return _swift_Platform_fcntlPtr(fd, cmd, ptr)
}
#endif

#if !os(Windows) || CYGWIN
public var S_IFMT: mode_t { return mode_t(0o170000) }
public var S_IFIFO: mode_t { return mode_t(0o010000) }
public var S_IFCHR: mode_t { return mode_t(0o020000) }
Expand Down Expand Up @@ -286,11 +321,24 @@ public var S_IREAD: mode_t { return S_IRUSR }
public var S_IWRITE: mode_t { return S_IWUSR }
public var S_IEXEC: mode_t { return S_IXUSR }
#endif
#else
public var S_IFMT: Int32 { return Int32(0xf000) }

public var S_IFREG: Int32 { return Int32(0x8000) }
public var S_IFDIR: Int32 { return Int32(0x4000) }
public var S_IFCHR: Int32 { return Int32(0x2000) }
public var S_IFIFO: Int32 { return Int32(0x1000) }

public var S_IREAD: Int32 { return Int32(0x0100) }
public var S_IWRITE: Int32 { return Int32(0x0080) }
public var S_IEXEC: Int32 { return Int32(0x0040) }
Copy link
Contributor

@gribozavr gribozavr Jul 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows does not have mode_t? This is painful.

The definitions in this block look identical otherwise. We actually also have S_IFMT, S_IFREG, S_IFDIR, S_IFCHR, S_IFIFO on Apple platforms.

Could we gyb this somehow? Or maybe emulate mode_t on Windows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree its painful. Windows does not have a mode_t unfortunately. Personally, I like the gyb idea. However, I think that would be beyond the scope of this change. Could do that as a follow up?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

#endif

//===----------------------------------------------------------------------===//
// ioctl.h
//===----------------------------------------------------------------------===//

#if !os(Windows) || CYGWIN
@_silgen_name("_swift_Platform_ioctl")
internal func _swift_Platform_ioctl(
_ fd: CInt,
Expand Down Expand Up @@ -327,8 +375,8 @@ public func ioctl(
) -> CInt {
return _swift_Platform_ioctl(fd, request, 0)
}
#endif

//===----------------------------------------------------------------------===//
// unistd.h
//===----------------------------------------------------------------------===//
Expand All @@ -354,14 +402,22 @@ public var SIG_DFL: sig_t? { return nil }
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
public var SIG_HOLD: sig_t { return unsafeBitCast(5, to: sig_t.self) }
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Windows)
#if os(Windows)
// In Cygwin, the below SIG_* have the same value with Linux.
// Verified with libstdc++6 v5.3.0 in Cygwin v2.4.1 64bit.
public typealias sighandler_t = _sig_func_ptr
#else
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
public typealias sighandler_t = __sighandler_t

public var SIG_DFL: sighandler_t? { return nil }
public var SIG_IGN: sighandler_t {
return unsafeBitCast(1, to: sighandler_t.self)
}
public var SIG_ERR: sighandler_t {
return unsafeBitCast(-1, to: sighandler_t.self)
}
public var SIG_HOLD: sighandler_t {
return unsafeBitCast(2, to: sighandler_t.self)
}
#elseif os(Windows)
#if CYGWIN
public typealias sighandler_t = __sighandler_t
#endif

public var SIG_DFL: sighandler_t? { return nil }
public var SIG_IGN: sighandler_t {
Expand All @@ -374,21 +430,38 @@ public var SIG_HOLD: sighandler_t {
return unsafeBitCast(2, to: sighandler_t.self)
}
#else
public var SIG_DFL: _crt_signal_t? { return nil }
public var SIG_IGN: _crt_signal_t {
return unsafeBitCast(1, to: _crt_signal_t.self)
}
public var SIG_ERR: _crt_signal_t {
return unsafeBitCast(-1, to: _crt_signal_t.self)
}
#endif
#else
internal var _ignore = _UnsupportedPlatformError()
#endif

//===----------------------------------------------------------------------===//
// semaphore.h
//===----------------------------------------------------------------------===//

#if !os(Windows) || CYGWIN
/// The value returned by `sem_open()` in the case of failure.
public var SEM_FAILED: UnsafeMutablePointer<sem_t>? {
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
// The value is ABI. Value verified to be correct for OS X, iOS, watchOS, tvOS.
return UnsafeMutablePointer<sem_t>(bitPattern: -1)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Windows)
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
// The value is ABI. Value verified to be correct on Glibc.
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
#elseif os(Windows)
#if CYGWIN
// The value is ABI. Value verified to be correct on Glibc.
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
#else
_UnsupportedPlatformError()
#endif
#else
_UnsupportedPlatformError()
#endif
Expand Down Expand Up @@ -423,6 +496,7 @@ public func sem_open(
) -> UnsafeMutablePointer<sem_t>? {
return _swift_Platform_sem_open4(name, oflag, mode, value)
}
#endif

//===----------------------------------------------------------------------===//
// Misc.
Expand Down
15 changes: 15 additions & 0 deletions stdlib/public/Platform/msvcrt.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
//===----------------------------------------------------------------------===//

@_exported import ucrt // Clang module
@_exported import visualc // Clang module

Loading