-
Notifications
You must be signed in to change notification settings - Fork 200
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
Swift binding integration #23
Changes from all commits
49091b0
7a865b4
7a6303d
f5f306f
a4b59bd
82f3eb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Usage: find_package(Swiftc) | ||
# | ||
# If successful the following variables will be defined | ||
# SWIFTC_FOUND | ||
# SWIFTC_EXECUTABLE | ||
|
||
find_program(SWIFTC_EXECUTABLE | ||
NAMES swiftc | ||
DOC "Path to 'swiftc' executable") | ||
|
||
# Handle REQUIRED and QUIET arguments, this will also set SWIFTC_FOUND to true | ||
# if SWIFTC_EXECUTABLE exists. | ||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(Swiftc | ||
"Failed to locate 'swiftc' executable" | ||
SWIFTC_EXECUTABLE) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import llbuild | ||
|
||
typealias Compute = [Int] -> Int | ||
|
||
class SimpleTask: Task { | ||
let inputs: [Key] | ||
var values: [Int] | ||
let compute: Compute | ||
|
||
init(_ inputs: [Key], compute: Compute) { | ||
self.inputs = inputs | ||
values = [Int](repeating: 0, count: inputs.count) | ||
self.compute = compute | ||
} | ||
|
||
func start(_ engine: TaskBuildEngine) { | ||
for (idx, input) in inputs.enumerated() { | ||
engine.taskNeedsInput(input, inputID: idx) | ||
} | ||
} | ||
|
||
func provideValue(_ engine: TaskBuildEngine, inputID: Int, value: Value) { | ||
values[inputID] = Int(value.toString())! | ||
} | ||
|
||
func inputsAvailable(_ engine: TaskBuildEngine) { | ||
let result = compute(values) | ||
engine.taskIsComplete(Value("\(result)"), forceChange: false) | ||
} | ||
} | ||
|
||
class SimpleBuildEngineDelegate: BuildEngineDelegate { | ||
var builtKeys = [Key]() | ||
|
||
func lookupRule(_ key: Key) -> Rule { | ||
switch key.toString() { | ||
case "A": | ||
return SimpleRule([]) { arr in | ||
precondition(self.builtKeys.isEmpty) | ||
self.builtKeys.append(key) | ||
return 2 | ||
} | ||
case "B": | ||
return SimpleRule([]) { arr in | ||
precondition(self.builtKeys.count == 1) | ||
self.builtKeys.append(key) | ||
return 3 | ||
} | ||
case "C": | ||
return SimpleRule([Key("A"), Key("B")]) { arr in | ||
precondition(self.builtKeys.count == 2) | ||
precondition(self.builtKeys[0].toString() == "A") | ||
precondition(self.builtKeys[1].toString() == "B") | ||
self.builtKeys.append(key) | ||
return arr[0] * arr[1] | ||
} | ||
default: fatalError("Unexpected key \(key) lookup") | ||
} | ||
} | ||
} | ||
|
||
class SimpleRule: Rule { | ||
let inputs: [Key] | ||
let compute: Compute | ||
init(_ inputs: [Key], compute: Compute) { | ||
self.inputs = inputs | ||
self.compute = compute | ||
} | ||
func createTask() -> Task { | ||
return SimpleTask(inputs, compute: compute) | ||
} | ||
} | ||
|
||
let delegate = SimpleBuildEngineDelegate() | ||
var engine = BuildEngine(delegate: delegate) | ||
|
||
// C depends on A and B | ||
var result = engine.build(key: Key("C")) | ||
print("\(result.toString())") | ||
|
||
precondition(result.toString() == "6") | ||
|
||
// Make sure building already built keys do not re-compute. | ||
delegate.builtKeys.removeAll() | ||
precondition(delegate.builtKeys.isEmpty) | ||
|
||
result = engine.build(key: Key("A")) | ||
precondition(result.toString() == "2") | ||
precondition(delegate.builtKeys.isEmpty) | ||
|
||
result = engine.build(key: Key("B")) | ||
precondition(result.toString() == "3") | ||
precondition(delegate.builtKeys.isEmpty) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
add_custom_target(swift-bindings | ||
DEPENDS libllbuild) | ||
# Set sources. | ||
set(SOURCES llbuild.swift) | ||
|
||
# Link C API. | ||
list(APPEND additional_args -import-underlying-module -lllbuild) | ||
list(APPEND additional_args -I ${CMAKE_CURRENT_SOURCE_DIR}/../libllbuild/public-api) | ||
|
||
# Add swift bindings target if swift compiler is present. | ||
if (SWIFTC_FOUND) | ||
add_swift_module(swift-bindings llbuild libllbuild "${SOURCES}" "${additional_args}") | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
config.suffixes = ['.llbuild'] | ||
config.suffixes = ['.txt', '.llbuild'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Check basic 'core' functionality of the swift bindings | ||
# | ||
# REQUIRES: has-swift=TRUE | ||
# RUN: env LD_LIBRARY_PATH=%{llbuild-lib-dir} %{swiftc} %{swiftc-platform-flags} %{srcroot}/examples/swift-bindings/core/basic.swift -I %{srcroot}/build/products/swift-bindings -I %{srcroot}/products/libllbuild/public-api -Xlinker %{llbuild-lib-dir}/swift-bindings.dylib -o %t.exe | ||
# RUN: env LD_LIBRARY_PATH=%{llbuild-lib-dir} %t.exe %s > %t.out | ||
# RUN: cat %t.out | ||
# RUN: %{FileCheck} %s --input-file %t.out | ||
# CHECK: 6 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This commit is missing one thing, there needs to be a dependency from the You can see this if you do a build from clean. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to check and record the TOOLCHAINS and SDKROOT in use... if not, then what will happen when run as:
is we will just find
/usr/bin/swiftc
as theswiftc
, but if we run again with a different TOOLCHAINS variable it won't work. I'd prefer if the configure step would bake in the value for TOOLCHAINS, I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed this by finding swiftc by
xcrun --find swiftc
on darwin