Commit c385c068 authored by Drew's avatar Drew

Merge pull request #70 from AnarchyTools/dynamic-library

Add support for dynamic libraries
parents 4440150f 308ebfc3
Pipeline #1347 passed with stage
......@@ -116,5 +116,8 @@ extension String {
func write(toFile path: String, atomically useAuxiliaryFile:Bool, encoding enc: NSStringEncoding) throws {
return try self.writeToFile(path, atomically: useAuxiliaryFile, encoding: enc)
}
func replacingOccurrences(of str: String, with: String) -> String {
return self.stringByReplacingOccurrencesOfString(str, withString: with)
}
}
#endif
\ No newline at end of file
......@@ -19,7 +19,9 @@ import Foundation
let SDKPath = "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk"
let SwiftCPath = "/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swiftc"
let SwiftBuildToolpath = "/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift-build-tool"
let DynamicLibraryExtension = ".dylib"
#elseif os(Linux)
let SwiftCPath = "/usr/local/bin/swiftc"
let SwiftBuildToolpath = "/usr/local/bin/swift-build-tool"
let DynamicLibraryExtension = ".so"
#endif
\ No newline at end of file
......@@ -57,6 +57,7 @@ final class ATllbuild : Tool {
enum OutputType {
case Executable
case StaticLibrary
case DynamicLibrary
}
enum ModuleMapType {
......@@ -105,7 +106,7 @@ final class ATllbuild : Tool {
switch(outputType) {
case .Executable:
break
case .StaticLibrary:
case .StaticLibrary, .DynamicLibrary:
yaml += " is-library: true\n" //I have no idea what the effect of this is, but swiftPM does it, so I'm including it.
}
......@@ -166,6 +167,23 @@ final class ATllbuild : Tool {
yaml += " args: \(args)\n"
yaml += " description: \"Linking Library: \(libPath)\""
return yaml
case .DynamicLibrary:
yaml += " tool: shell\n"
var llbuild_inputs = ["<atllbuild-swiftc>"]
llbuild_inputs.append(contentsOf: objects)
let builtProducts = linkWithProduct.map {workdir+"products/"+$0}
llbuild_inputs.append(contentsOf: builtProducts)
yaml += " inputs: \(llbuild_inputs)\n"
let libPath = productPath + modulename + DynamicLibraryExtension
yaml += " outputs: [\"<atllbuild>\", \"\(libPath)\"]\n"
var args = [swiftCPath, "-o", libPath, "-emit-library"]
args.append(contentsOf: objects)
args.append(contentsOf: builtProducts)
args.append(contentsOf: linkOptions)
yaml += " args: \(args)\n"
yaml += " description: \"Linking Library: \(libPath)\""
return yaml
}
}
......@@ -254,12 +272,19 @@ final class ATllbuild : Tool {
//parse arguments
var linkWithProduct: [String] = []
if let arr = task[Options.LinkWithProduct.rawValue]?.vector {
if let arr_ = task[Options.LinkWithProduct.rawValue] {
guard let arr = arr_.vector else {
fatalError("Non-vector link directive \(arr_)")
}
for product in arr {
guard let p = product.string else { fatalError("non-string product \(product)") }
guard var p = product.string else { fatalError("non-string product \(product)") }
if p.hasSuffix(".dynamic") {
p = p.replacingOccurrences(of: ".dynamic", with: DynamicLibraryExtension)
}
linkWithProduct.append(p)
}
}
let outputType: OutputType
if task[Options.OutputType.rawValue]?.string == "static-library" {
outputType = .StaticLibrary
......@@ -267,6 +292,9 @@ final class ATllbuild : Tool {
else if task[Options.OutputType.rawValue]?.string == "executable" {
outputType = .Executable
}
else if task[Options.OutputType.rawValue]?.string == "dynamic-library" {
outputType = .DynamicLibrary
}
else {
fatalError("Unknown \(Options.OutputType.rawValue) \(task["outputType"])")
}
......@@ -419,15 +447,15 @@ final class ATllbuild : Tool {
try! copyByOverwriting("\(workDirectory)/products/\(name)", toPath: "bin/\(name)")
case .StaticLibrary:
try! copyByOverwriting("\(workDirectory)/products/\(name).a", toPath: "bin/\(name).a")
case .DynamicLibrary:
try! copyByOverwriting("\(workDirectory)/products/\(name).\(DynamicLibraryExtension)", toPath: "bin/\(name).\(DynamicLibraryExtension)")
}
switch moduleMap {
case .None:
break
case .Synthesized:
try! copyByOverwriting("\(workDirectory)/products/\(name).modulemap", toPath: "bin/\(name).modulemap")
}
}
if task[Options.WholeModuleOptimization.rawValue]?.bool == true && !wmoHack {
......
(package
:name "dynamic_library"
:tasks {
:foo {
:tool "atllbuild"
:output-type "dynamic-library"
:name "dynamicFoo"
:sources ["src/foo.swift"]
}
:bar {
:tool "atllbuild"
:output-type "dynamic-library"
:name "dynamicBar"
:sources ["src/bar.swift"]
:link-with ["dynamicFoo.dynamic"]
}
:default {
:dependencies ["foo" "bar"]
:tool "atllbuild"
:output-type "executable"
:name "dynamic_library_tester"
:sources ["src/main.swift"]
:link-with ["dynamicBar.dynamic" "dynamicFoo.dynamic"]
}
}
)
\ No newline at end of file
import dynamicFoo
public class Bar : Foo {
public override init() {
super.init()
}
}
\ No newline at end of file
public class Foo {
public init() { }
}
\ No newline at end of file
import dynamicBar
let b = Bar()
print("dynamicBar works")
\ No newline at end of file
......@@ -10,6 +10,11 @@ pwd
echo "****************SELF-HOSTING TEST**************"
$ATBUILD atbuild
echo "****************DYNAMIC LIBRARY TEST**************"
cd $DIR/tests/fixtures/dynamic_library
$ATBUILD
.atllbuild/products/dynamic_library_tester
echo "****************WMO TEST**************"
cd $DIR/tests/fixtures/wmo
$ATBUILD
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment