Skip to content

Commit

Permalink
Merge pull request #16 from apple/update
Browse files Browse the repository at this point in the history
Sync with SwiftPM trunk
  • Loading branch information
aciidgh authored Dec 11, 2019
2 parents 05e8dd5 + 5238e5a commit c7f2fbe
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 7 deletions.
1 change: 0 additions & 1 deletion Sources/TSCBasic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ target_link_libraries(TSCBasic PUBLIC
set_target_properties(TSCBasic PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

set_property(GLOBAL APPEND PROPERTY TSC_EXPORTS TSCBasic)

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
install(TARGETS TSCBasic
Expand Down
7 changes: 7 additions & 0 deletions Sources/TSCBasic/PathShims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public func resolveSymlinks(_ path: AbsolutePath) -> AbsolutePath {
// that implements readlink and not realpath.
if let resultPtr = TSCLibc.realpath(pathStr, nil) {
let result = String(cString: resultPtr)
// If `resolved_path` is specified as NULL, then `realpath` uses
// malloc(3) to allocate a buffer [...]. The caller should deallocate
// this buffer using free(3).
//
// String.init(cString:) creates a new string by copying the
// null-terminated UTF-8 data referenced by the given pointer.
resultPtr.deallocate()
// FIXME: We should measure if it's really more efficient to compare the strings first.
return result == pathStr ? path : AbsolutePath(result)
}
Expand Down
1 change: 0 additions & 1 deletion Sources/TSCLibc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ target_link_libraries(TSCLibc PUBLIC
set_target_properties(TSCLibc PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

set_property(GLOBAL APPEND PROPERTY TSC_EXPORTS TSCLibc)

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
install(TARGETS TSCLibc
Expand Down
43 changes: 43 additions & 0 deletions Sources/TSCLibc/libc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,46 @@
#endif

@_exported import TSCclibc

#if os(Windows)
// char *realpath(const char *path, char *resolved_path);
public func realpath(
_ path: String,
_ resolvedPath: UnsafeMutablePointer<CChar>?
) -> UnsafeMutablePointer<CChar>? {
fatalError("realpath is unimplemented")
}

// char *mkdtemp(char *template);
public func mkdtemp(
_ template: UnsafeMutablePointer<CChar>?
) -> UnsafeMutablePointer<CChar>? {
fatalError("mkdtemp is unimplemented")
}

// int mkstemps(char *template, int suffixlen);
public func mkstemps(
_ template: UnsafeMutablePointer<CChar>?,
_ suffixlen: Int32
) -> Int32 {
guard let template = template else { return -EINVAL }
return String(cString: template).withCString(encodedAs: UTF16.self) {
let capacity: Int = wcslen($0) + 1
return $0.withMemoryRebound(to: wchar_t.self, capacity: capacity) {
guard _wmktemp_s(UnsafeMutablePointer(mutating: $0), capacity) == 0 else {
return -EINVAL
}

var fd: Int32 = -1
_wsopen_s(&fd, $0, _O_RDWR | _O_CREAT | _O_BINARY | _O_NOINHERIT,
_SH_DENYNO, _S_IREAD | _S_IWRITE)

String(decodingCString: $0, as: UTF16.self).utf8CString.withUnsafeBytes {
template.assign(from: $0.bindMemory(to: CChar.self).baseAddress!,
count: $0.count)
}
return fd
}
}
}
#endif
2 changes: 0 additions & 2 deletions Sources/TSCUtility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,3 @@ target_link_libraries(TSCUtility PUBLIC
# NOTE(compnerd) workaround for CMake not setting up include flags yet
set_target_properties(TSCUtility PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

set_property(GLOBAL APPEND PROPERTY TSC_EXPORTS TSCUtility)
2 changes: 0 additions & 2 deletions Sources/TSCclibc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ if(NOT BUILD_SHARED_LIBS)
install(TARGETS TSCclibc
ARCHIVE DESTINATION lib)
endif()

set_property(GLOBAL APPEND PROPERTY TSC_EXPORTS TSCclibc)
4 changes: 4 additions & 0 deletions Tests/TSCBasicTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ class FileSystemTests: XCTestCase {

// Check read/write against root.
XCTAssertThrows(FileSystemError.ioError) {
#if os(Android)
_ = try fs.readFileContents(AbsolutePath("/system/"))
#else
_ = try fs.readFileContents(AbsolutePath("/"))
#endif
}
XCTAssertThrows(FileSystemError.isDirectory) {
try fs.writeFileContents(AbsolutePath("/"), bytes: [])
Expand Down
16 changes: 15 additions & 1 deletion Tests/TSCBasicTests/PathShimTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,30 @@ class PathShimTests : XCTestCase {
class WalkTests : XCTestCase {

func testNonRecursive() {
#if os(Android)
let root = "/system"
var expected = [
AbsolutePath("\(root)/usr"),
AbsolutePath("\(root)/bin"),
AbsolutePath("\(root)/xbin")
]
#else
let root = ""
var expected = [
AbsolutePath("/usr"),
AbsolutePath("/bin"),
AbsolutePath("/sbin")
]
for x in try! walk(AbsolutePath("/"), recursively: false) {
#endif
for x in try! walk(AbsolutePath("\(root)/"), recursively: false) {
if let i = expected.firstIndex(of: x) {
expected.remove(at: i)
}
#if os(Android)
XCTAssertEqual(3, x.components.count)
#else
XCTAssertEqual(2, x.components.count)
#endif
}
XCTAssertEqual(expected.count, 0)
}
Expand Down

0 comments on commit c7f2fbe

Please sign in to comment.