Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit f08a74b

Browse files
Merge pull request #446 from xamarin/sandwich-sample
updated samples, QUICKSTART
2 parents bc5af77 + 9fd39ac commit f08a74b

File tree

10 files changed

+109
-11
lines changed

10 files changed

+109
-11
lines changed

Pack-Man/FilesToCopy.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ IEnumerable<(DirectoryPath Src, DirectoryPath Dest)> GetDirectoriesToBundle (Bui
105105
dl.Add (GetInfo (bi.XamGlue, $"{glue}/FinalProduct/XamGlue.framework", $"lib/SwiftInterop/{glue}/XamGlue.framework"));
106106

107107
// Get 'Sample' folders to copy
108-
var samples = new [] { "helloswift", "foreach", "piglatin", "propertybag", "sampler" };
108+
var samples = new [] { "helloswift", "piglatin", "propertybag", "sampler", "sandwiches" };
109109
foreach (var sample in samples)
110110
dl.Add (GetInfo (bi.BaseDir, $"samples/{sample}", $"samples/{sample}"));
111111

QUICKSTART.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ A typical set of commands to generate bindings is:
6868

6969
mono /path/to/tom-swifty.exe --swift-bin-path SWIFT_BIN_PATH --swift-lib-path SWIFT_LIB_PATH -o /path/to/output_directory -C /path/to/YOURLIBRARY.framework -C /path/to/binding-tools-for-swift/swiftglue/bin/Debug/PLATFORM/XamGlue.framework -module-name YOURLIBRARY
7070

71-
In this example `SWIFT_BIN_PATH` and `SWIFT_LIB_PATH` are paths to the Swift reflector build (see above). `YOULIBRARY` is the name of the library you’re trying to bind. `PLATFORM` is one of `appletv`, `iphone`, `mac`, or `watch`.
71+
In this example `SWIFT_BIN_PATH` and `SWIFT_LIB_PATH` are paths to the Swift reflector build (see above). `YOURLIBRARY` is the name of the library you’re trying to bind. `PLATFORM` is one of `appletv`, `iphone`, `mac`, or `watch`.
7272

7373
If you add the argument `--retain-swift-wrappers`, the binding tools will leave a directory that contains the source to the Swift wrappers written during the compilation. If you add the argument `--retain-xml-reflection`, the binding tools will leaves a directory that contains output of the reflector in XML format.
7474

@@ -81,5 +81,12 @@ The binding tools need the following to operate:
8181

8282
In addition, the tools can operator on separate `.swiftmodule` and `.dylib` files. This can be handled by using a `-M` argument for the `.swiftmodule` and `-L` for the library. It’s easier to use the `.framework` directory and a single `-C` argument.
8383

84+
## Building and Running Samples
85+
Samples source code is in the `samples` directory, but will not build and run there.
86+
Instead you need to make a packaged build, from the root of binding-tools-for-swift execute the command:
8487

88+
`make package`
89+
90+
this will leave a directory inside of the directory `Pack-Man` named `binding-tools-for-swift` which contains buildable samples.
91+
Most samples can be built by executing `make` in their directory. To try out a sample, do `make runit` for most of the samples.
8592

samples/foreach/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ libForeach.dylib: *.swift *.cs
1212
@cp ../../lib/binding-tools-for-swift/SwiftRuntimeLibrary.dll .
1313
@cp ../../lib/SwiftInterop/mac/XamGlue.framework/XamGlue .
1414
$(SWIFTC) $(SWIFTARGS) -module-name $(OUTPUT_MODULE) *.swift
15-
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . $(OUTPUT_MODULE)
15+
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . -module-name $(OUTPUT_MODULE)
16+
@xcrun install_name_tool -change "@rpath/XamGlue.framework/XamGlue" XamGlue libXamWrapping.dylib
1617
mcs -nowarn:CS0169 -lib:../lib -r:SwiftRuntimeLibrary -lib:. *.cs -out:$(OUTPUT_MODULE).exe
1718

1819
runit:
1920
@LD_LIBRARY_PATH=.:$(SWIFT_GLUE):$(SWIFT_LIB) mono --arch=64 $(OUTPUT_MODULE).exe
2021

2122
clean:
2223
@rm -f *.dylib *.swiftmodule *.swiftdoc *.dll *.exe XamGlue LooperForeach.cs
23-
@rm -rf XamWrappingSource x86_64 *.framework XmlReflection
24+
@rm -rf XamWrappingSource x86_64 *.framework XmlReflection bindings

samples/helloswift/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ libHello.dylib: *.swift *.cs
1111
@cp ../../lib/SwiftInterop/SwiftRuntimeLibrary.Mac.dll .
1212
@cp ../../lib/SwiftInterop/mac/XamGlue.framework/XamGlue .
1313
$(SWIFTC) $(SWIFTARGS) -module-name $(OUTPUT_MODULE) *.swift
14-
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . $(OUTPUT_MODULE)
14+
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . -module-name $(OUTPUT_MODULE)
15+
@xcrun install_name_tool -change "@rpath/XamGlue.framework/XamGlue" XamGlue libXamWrapping.dylib
1516
mcs -nowarn:CS0169 -lib:../lib -r:SwiftRuntimeLibrary.Mac -lib:. *.cs -out:$(OUTPUT_MODULE).exe
1617

1718
runit:

samples/helloworld/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ swift:
1212
$(MAKE) -C swiftsrc
1313

1414
tomswifty:
15-
mono $(TOM_SWIFTY) --retain-swift-wrappers --swift-bin-path $(SWIFT_BIN) --swift-lib-path $(SWIFT_LIB) -o . -C swiftsrc -C $(SWIFT_GLUE) HelloWorld
15+
mono $(TOM_SWIFTY) --retain-swift-wrappers --swift-bin-path $(SWIFT_BIN) --swift-lib-path $(SWIFT_LIB) -o . -C swiftsrc -C $(SWIFT_GLUE) -module-name HelloWorld
1616

1717
csharp: *.cs
1818
mcs -nowarn:CS0169 -lib:$(TOM_SWIFTY_LIB) -r:SwiftRuntimeLibrary.Mac -lib:swiftsrc *.cs -out:HelloWorld.exe
1919
clean:
2020
$(MAKE) -C swiftsrc clean
2121
rm -f TopLevelEntitiesHelloWorld.cs
2222
rm -f HelloWorld.exe
23+
rm -rf bindings
2324

24-
run:
25+
runit:
2526
LD_LIBRARY_PATH=.:swiftsrc:$(TOM_SWIFTY_LIB):$(SWIFT_GLUE) \
2627
mono HelloWorld.exe

samples/propertybag/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ libPropertyBag.dylib: *.swift
1515
@cp ../../lib/SwiftInterop/mac/XamGlue.framework/XamGlue .
1616
@cp ../../lib/binding-tools-for-swift/SwiftRuntimeLibrary.dll .
1717
$(SWIFTC) $(SWIFTARGS) -module-name $(OUTPUT_MODULE) *.swift
18-
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . $(OUTPUT_MODULE)
18+
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . -module-name $(OUTPUT_MODULE)
19+
@xcrun install_name_tool -change "@rpath/XamGlue.framework/XamGlue" XamGlue libXamWrapping.dylib
1920
mcs -nowarn:CS0169 -lib:../lib -r:SwiftRuntimeLibrary -lib:. *.cs -unsafe+ -out:$(OUTPUT_MODULE).exe
2021

2122
runit:
2223
@DYLD_LIBRARY_PATH=$(SWIFT_LIB):$(SWIFT_GLUE):. mono --arch=64 $(OUTPUT_MODULE).exe
2324

2425
clean:
2526
@rm -f *.dylib *.swiftmodule *.swiftdoc *.dll *.exe XamGlue PropertyBagProps.cs
26-
@rm -rf XamWrappingSource armv7 armv7s arm64 x86_64
27+
@rm -rf XamWrappingSource armv7 armv7s arm64 x86_64 bindings

samples/sampler/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ libSampler.dylib: *.swift
1313
@cp ../../lib/binding-tools-for-swift/SwiftRuntimeLibrary.dll .
1414
@cp ../../lib/SwiftInterop/mac/XamGlue.framework/XamGlue .
1515
$(SWIFTC) $(SWIFTARGS) -module-name $(OUTPUT_MODULE) *.swift
16-
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . $(OUTPUT_MODULE)
16+
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . -module-name $(OUTPUT_MODULE)
17+
@xcrun install_name_tool -change "@rpath/XamGlue.framework/XamGlue" XamGlue libXamWrapping.dylib
1718
mcs -nowarn:CS0169 -lib:../lib -r:SwiftRuntimeLibrary -lib:. *.cs -unsafe+ -out:$(OUTPUT_MODULE).exe
1819

1920
runit:
2021
@LD_LIBRARY_PATH=$(SWIFT_LIB) mono --arch=64 $(OUTPUT_MODULE).exe
2122

2223
clean:
2324
@rm -f *.dylib *.swiftmodule *.swiftdoc *.dll *.exe XamGlue AFinalClassSampler.cs NumberSampler.cs
24-
@rm -rf XamWrappingSource x86_64
25+
@rm -rf XamWrappingSource x86_64 bindings

samples/sandwiches/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
SWIFT_BIN = ../../bin/swift/bin
2+
SWIFT_LIB = ../../bin/swift/lib/swift/macosx/
3+
4+
5+
SWIFTC = $(SWIFT_BIN)/swiftc
6+
SDK = `xcrun --sdk macosx --show-sdk-path`
7+
SWIFTARGS = -v -sdk $(SDK) -L $(SWIFT_LIB) -emit-module -emit-library
8+
9+
BINDINGTOOLSFORSWIFT=../../binding-tools-for-swift
10+
11+
OUTPUT_MODULE=Sandwiches
12+
13+
libSandwiches.dylib: *.swift
14+
@rm -rf XamWrappingSource armv7 armv7s arm64 x86_64
15+
@mkdir XamGlue.framework
16+
@cp ../../lib/SwiftInterop/mac/XamGlue.framework/XamGlue .
17+
@cp ../../lib/binding-tools-for-swift/SwiftRuntimeLibrary.dll .
18+
$(SWIFTC) $(SWIFTARGS) -module-name $(OUTPUT_MODULE) *.swift
19+
$(BINDINGTOOLSFORSWIFT) --retain-swift-wrappers -o . -C . -module-name $(OUTPUT_MODULE)
20+
@xcrun install_name_tool -change "@rpath/XamGlue.framework/XamGlue" XamGlue libXamWrapping.dylib
21+
mcs -nowarn:CS0169 -lib:../lib -r:SwiftRuntimeLibrary -lib:. *.cs -unsafe+ -out:$(OUTPUT_MODULE).exe
22+
23+
runit:
24+
@DYLD_LIBRARY_PATH=$(SWIFT_LIB):$(SWIFT_GLUE):. mono --arch=64 $(OUTPUT_MODULE).exe
25+
26+
clean:
27+
@rm -f *.dylib *.swiftmodule *.swiftdoc *.dll *.exe TopLevelEntities.cs IBreadSandwiches.cs IFillingSandwiches.cs HamSandwiches.cs RyeSandwiches.cs XamGlue
28+
@rm -rf XamWrappingSource armv7 armv7s arm64 x86_64 bindings

samples/sandwiches/SandwichesMain.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
using System;
3+
using System.IO;
4+
using SwiftRuntimeLibrary;
5+
using SwiftRuntimeLibrary.SwiftMarshal;
6+
using System.Runtime.InteropServices;
7+
using Sandwiches;
8+
9+
namespace SandwichesCSharp
10+
{
11+
12+
public class WholeWheat : IBread {
13+
public SwiftString Name => (SwiftString)"whole wheat";
14+
public bool Sliced => true;
15+
}
16+
17+
public class SharpCheddar : IFilling {
18+
public SwiftString Stuff => (SwiftString)"sharp cheddar";
19+
}
20+
21+
public class SandwichesMain
22+
{
23+
public static void Main(string[] args)
24+
{
25+
TopLevelEntities.PrintSandwich (new WholeWheat (), new SharpCheddar ());
26+
}
27+
}
28+
}

samples/sandwiches/sandwiches.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
public protocol Filling {
3+
var stuff: String { get }
4+
}
5+
6+
public protocol Bread {
7+
var name: String { get }
8+
var sliced: Bool { get }
9+
}
10+
11+
public struct Rye : Bread {
12+
public init () { }
13+
public var name:String {
14+
get { return "rye" }
15+
}
16+
public var sliced:Bool {
17+
get { return true }
18+
}
19+
}
20+
21+
public struct Ham : Filling {
22+
public init () { }
23+
public var stuff: String {
24+
get { return "ham" }
25+
}
26+
}
27+
28+
public func printSandwich (of: Bread, with: Filling) {
29+
print ("\(with.stuff) on \(of.name)")
30+
}

0 commit comments

Comments
 (0)