Skip to content

Commit f3a36c0

Browse files
authored
Swift 5.7 - Build test binary and execute on emulator (#15)
1 parent 3cb014b commit f3a36c0

29 files changed

+751
-138
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Package.resolved
1818

1919
## User settings
2020
xcuserdata/
21-
swift-builder.xcscheme
21+
.swiftpm/xcode/xcshareddata/xcschemes/swift-builder-Package.xcscheme
2222

2323
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
2424
*.xcscmblueprint
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1420"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "SwiftBuilder"
18+
BuildableName = "SwiftBuilder"
19+
BlueprintName = "SwiftBuilder"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "YES"
27+
buildForArchiving = "YES"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "Shell"
32+
BuildableName = "Shell"
33+
BlueprintName = "Shell"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
</BuildActionEntries>
38+
</BuildAction>
39+
<TestAction
40+
buildConfiguration = "Debug"
41+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
42+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
43+
shouldUseLaunchSchemeArgsEnv = "YES">
44+
<Testables>
45+
</Testables>
46+
</TestAction>
47+
<LaunchAction
48+
buildConfiguration = "Debug"
49+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
50+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
51+
launchStyle = "0"
52+
useCustomWorkingDirectory = "NO"
53+
ignoresPersistentStateOnLaunch = "NO"
54+
debugDocumentVersioning = "YES"
55+
debugServiceExtension = "internal"
56+
allowLocationSimulation = "YES"
57+
viewDebuggingEnabled = "No">
58+
<BuildableProductRunnable
59+
runnableDebuggingMode = "0">
60+
<BuildableReference
61+
BuildableIdentifier = "primary"
62+
BlueprintIdentifier = "SwiftBuilder"
63+
BuildableName = "SwiftBuilder"
64+
BlueprintName = "SwiftBuilder"
65+
ReferencedContainer = "container:">
66+
</BuildableReference>
67+
</BuildableProductRunnable>
68+
<CommandLineArguments>
69+
<CommandLineArgument
70+
argument = "--working-folder"
71+
isEnabled = "YES">
72+
</CommandLineArgument>
73+
<CommandLineArgument
74+
argument = "/Users/nikolaydzhulay/ws/SwiftAndroid_working"
75+
isEnabled = "YES">
76+
</CommandLineArgument>
77+
<CommandLineArgument
78+
argument = "--android-sdk"
79+
isEnabled = "YES">
80+
</CommandLineArgument>
81+
<CommandLineArgument
82+
argument = "/Users/nikolaydzhulay/Library/Android/sdk"
83+
isEnabled = "YES">
84+
</CommandLineArgument>
85+
<CommandLineArgument
86+
argument = "--source-root"
87+
isEnabled = "YES">
88+
</CommandLineArgument>
89+
<CommandLineArgument
90+
argument = "/Users/nikolaydzhulay/ws/swift-toolchain-for-android-on-macos"
91+
isEnabled = "YES">
92+
</CommandLineArgument>
93+
</CommandLineArguments>
94+
<EnvironmentVariables>
95+
<EnvironmentVariable
96+
key = "CMAKE_PATH"
97+
value = "/Users/nikolaydzhulay/Library/Android/sdk/cmake/3.22.1/bin"
98+
isEnabled = "NO">
99+
</EnvironmentVariable>
100+
</EnvironmentVariables>
101+
</LaunchAction>
102+
<ProfileAction
103+
buildConfiguration = "Release"
104+
shouldUseLaunchSchemeArgsEnv = "YES"
105+
savedToolIdentifier = ""
106+
useCustomWorkingDirectory = "NO"
107+
debugDocumentVersioning = "YES">
108+
<BuildableProductRunnable
109+
runnableDebuggingMode = "0">
110+
<BuildableReference
111+
BuildableIdentifier = "primary"
112+
BlueprintIdentifier = "SwiftBuilder"
113+
BuildableName = "SwiftBuilder"
114+
BlueprintName = "SwiftBuilder"
115+
ReferencedContainer = "container:">
116+
</BuildableReference>
117+
</BuildableProductRunnable>
118+
</ProfileAction>
119+
<AnalyzeAction
120+
buildConfiguration = "Debug">
121+
</AnalyzeAction>
122+
<ArchiveAction
123+
buildConfiguration = "Release"
124+
revealArchiveInOrganizer = "YES">
125+
</ArchiveAction>
126+
</Scheme>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1430"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "swift-on-android-test"
18+
BuildableName = "swift-on-android-test"
19+
BlueprintName = "swift-on-android-test"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
shouldAutocreateTestPlan = "YES">
31+
</TestAction>
32+
<LaunchAction
33+
buildConfiguration = "Debug"
34+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
35+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
36+
launchStyle = "0"
37+
useCustomWorkingDirectory = "NO"
38+
ignoresPersistentStateOnLaunch = "NO"
39+
debugDocumentVersioning = "YES"
40+
debugServiceExtension = "internal"
41+
allowLocationSimulation = "YES"
42+
viewDebuggingEnabled = "No">
43+
<BuildableProductRunnable
44+
runnableDebuggingMode = "0">
45+
<BuildableReference
46+
BuildableIdentifier = "primary"
47+
BlueprintIdentifier = "swift-on-android-test"
48+
BuildableName = "swift-on-android-test"
49+
BlueprintName = "swift-on-android-test"
50+
ReferencedContainer = "container:">
51+
</BuildableReference>
52+
</BuildableProductRunnable>
53+
</LaunchAction>
54+
<ProfileAction
55+
buildConfiguration = "Release"
56+
shouldUseLaunchSchemeArgsEnv = "YES"
57+
savedToolIdentifier = ""
58+
useCustomWorkingDirectory = "NO"
59+
debugDocumentVersioning = "YES">
60+
<BuildableProductRunnable
61+
runnableDebuggingMode = "0">
62+
<BuildableReference
63+
BuildableIdentifier = "primary"
64+
BlueprintIdentifier = "swift-on-android-test"
65+
BuildableName = "swift-on-android-test"
66+
BlueprintName = "swift-on-android-test"
67+
ReferencedContainer = "container:">
68+
</BuildableReference>
69+
</BuildableProductRunnable>
70+
</ProfileAction>
71+
<AnalyzeAction
72+
buildConfiguration = "Debug">
73+
</AnalyzeAction>
74+
<ArchiveAction
75+
buildConfiguration = "Release"
76+
revealArchiveInOrganizer = "YES">
77+
</ArchiveAction>
78+
</Scheme>

Package.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ let package = Package(
1212
name: "SwiftBuilder",
1313
targets: [
1414
"SwiftBuilder",
15-
"Shell",
1615
]),
1716
.executable(
1817
name: "swiftc-android",
1918
targets: [
20-
"Shell",
2119
"SwiftcAndroid",
2220
]),
21+
.executable(
22+
name: "swift-on-android-test",
23+
targets: [
24+
"SampleAndroidBinary"
25+
]),
2326
],
2427
dependencies: [
2528
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0"),
@@ -41,6 +44,7 @@ let package = Package(
4144
]),
4245
.target(name: "HostConfig",
4346
dependencies: ["FoundationExtension"]),
47+
.executableTarget(name: "SampleAndroidBinary"),
4448
.target(
4549
name: "Shell",
4650
dependencies: [

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,32 @@ Swift toolchain for android on macos
77
- Recent xcode vetsion installed on mac
88
- Install NDK and CMake. [Instructions](https://developer.android.com/studio/projects/install-ndk)
99

10-
## Execute
10+
## Use toolchain
11+
12+
Here are steps to compile package
13+
14+
- `$ export PATH="<toolchain_path>/usr/bin:$PATH"`
15+
- `$ export SWIFT_EXEC_MANIFEST=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc`
16+
- `export SWIFT_EXEC=<toolchain_path>/usr/bin/swiftc-android` and `swiftc-android` should be executable
17+
- `cd <package_path>`
18+
-
19+
```
20+
swift build --triple aarch64-unknown-linux-android \
21+
--emit-swift-module-separately \
22+
--sdk <toolchain_path> \
23+
--toolchain <toolchain_path>/usr/bin \
24+
-Xswiftc -emit-library \
25+
-Xswiftc -emit-module \
26+
-Xcc -I/Users/$USER/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include \
27+
-Xcc -I/Users/$USER/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/aarch64-linux-android \
28+
-v
29+
```
30+
31+
## Build toolchain
1132
```
1233
$ swift run SwiftBuilder \
1334
--working-folder <WORKING_FOLDER> \
14-
--android-sdk <PATH_TO_ANDROID_SDK>
35+
--android-sdk <PATH_TO_ANDROID_SDK> \
1536
--source-root "$PWD"
1637
```
1738
For example, values might be:
@@ -34,3 +55,6 @@ If you want start from scratch - just delete file from working folder. Alternati
3455
2. Follow steps [here](./Sources/SwiftBuilder/Repos/HowToGetCommitHashes.md) to update default checkout hashes
3556
3. Consider changing `CMAKE_OSX_DEPLOYMENT_TARGET`
3657
4. Execute and fix issue by issue...
58+
59+
60+

Sources/HostConfig/AndroidSDK.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@ import FoundationExtension
33

44
public struct AndroidSDK {
55

6+
public let sdkRootUrl: URL
7+
68
public let ndk: NDK
79
public let cmake: CMake
810

911
/// On init, AndroidSDK will search for valid cmake and ndk
1012
public init(path: String) throws {
13+
14+
self.sdkRootUrl = URL(filePath: path, directoryHint: .isDirectory)
15+
16+
let fileManager = FileManager.default
17+
18+
guard fileManager.folderExists(at: sdkRootUrl) else {
19+
throw SimpleError("Android SDK folder doesn't exist at \(path)")
20+
}
21+
1122
let ndksFolderPath = path + "/ndk"
1223

1324
self.ndk = try Self.create(itemsFolderPath: ndksFolderPath, name: "ndk", create: NDK.init)

Sources/HostConfig/NDK.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ public struct NDK {
1515

1616
public let toolchain: URL
1717

18+
public var toolchainPath: String {
19+
toolchain.path()
20+
}
21+
22+
public var sysrootLib: URL {
23+
toolchain.appending(path: "/sysroot/usr/lib", directoryHint: .isDirectory)
24+
}
25+
26+
public var sysrootLibPath: String {
27+
sysrootLib.path()
28+
}
29+
30+
public var sysrootIncludePath: String {
31+
toolchainPath + "/sysroot/usr/include"
32+
}
33+
1834
public init(folderPath: String) throws {
1935
let url = URL(filePath: folderPath, directoryHint: .isDirectory)
2036
try self.init(folderUrl: url)
@@ -28,7 +44,7 @@ public struct NDK {
2844
let version = try Version(ndkVersionString)
2945

3046
guard "\(version.major)" == Self.version else {
31-
throw SimpleError("Currently we workign only with NDK v\(version)")
47+
throw SimpleError("Currently we workign only with NDK v\(version.versionString)")
3248
}
3349

3450
self.toolchain = folderUrl.appending(path: "/toolchains/llvm/prebuilt/darwin-x86_64", directoryHint: .isDirectory)

Sources/HostConfig/Version.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import FoundationExtension
33

44
/// Version, were all elements are integers, might contain build, if it is integer
5-
struct Version: Comparable, Equatable {
5+
struct Version: Comparable, Equatable, CustomStringConvertible {
66

77
let major: Int
88
let minor: Int
@@ -44,6 +44,10 @@ struct Version: Comparable, Equatable {
4444
return true
4545
}
4646

47+
var description: String {
48+
versionString
49+
}
50+
4751
// MARK: Private
4852

4953
private var numbers: [Int] { [major, minor, patch] }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Foundation
2+
3+
print("Hello world")

0 commit comments

Comments
 (0)