Skip to content
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

[Feature] Support Android Platform. #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ Carthage/Build

fastlane/report.xml
fastlane/screenshots
.idea
.DS_Store
35 changes: 20 additions & 15 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@

import PackageDescription

#if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(Android)

let package = Package(
name: "Signals",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Signals",
targets: ["Signals"]
)
],
targets: [
.target(
name: "Signals",
exclude: ["Signals.xcodeproj", "README.md", "Sources/Info.plist", "Sources/Signals.h", "Tests"]
),
]
name: "Signals",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Signals",
type: .dynamic,
targets: ["Signals"]
)
],
targets: [
.target(
name: "Signals",
exclude: ["Signals.xcodeproj", "README.md", "Sources/Info.plist", "Sources/Signals.h", "Tests"]
),
.target(
name: "Example",
dependencies: ["Signals"]
),
]
)

#else
Expand Down
33 changes: 19 additions & 14 deletions Package@swift-4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ import PackageDescription
#if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS)

let package = Package(
name: "Signals",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Signals",
targets: ["Signals"]
)
],
targets: [
.target(
name: "Signals",
exclude: ["Signals.xcodeproj", "README.md", "Sources/Info.plist", "Sources/Signals.h", "Tests"]
),
]
name: "Signals",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Signals",
type: .dynamic,
targets: ["Signals"]
)
],
targets: [
.target(
name: "Signals",
exclude: ["Signals.xcodeproj", "README.md", "Sources/Info.plist", "Sources/Signals.h", "Tests"]
),
.target(
name: "Example",
dependencies: ["Signals"]
),
]
)

#else
Expand Down
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
<a href="https://travis-ci.org/Kitura/BlueSignals">
<img src="https://travis-ci.org/Kitura/BlueSignals.svg?branch=master" alt="Build Status - Master">
</a>
<img src="https://img.shields.io/badge/os-macOS-green.svg?style=flat" alt="macOS">
<img src="https://img.shields.io/badge/os-iOS-green.svg?style=flat" alt="iOS">
<img src="https://img.shields.io/badge/os-linux-green.svg?style=flat" alt="Linux">
<img src="https://img.shields.io/badge/os-Android-green.svg?style=flat" alt="Android">
<img src="https://img.shields.io/badge/os-macOS-green.svg?style=flat" alt="macOS">
<img src="https://img.shields.io/badge/os-Linux-green.svg?style=flat" alt="Linux">
<img src="https://img.shields.io/badge/license-Apache2-blue.svg?style=flat" alt="Apache 2">
<a href="http://swift-at-ibm-slack.mybluemix.net/">
<img src="http://swift-at-ibm-slack.mybluemix.net/badge.svg" alt="Slack Status">
Expand Down Expand Up @@ -45,16 +46,64 @@ BlueSignals version 2.0 and above supports Swift 5.1+. See older versions of Bl
* Ubuntu 16.04 (or 16.10 but only tested on 16.04) and 18.04.
* One of the Swift Open Source toolchain listed above.

### Android

* macOS 10.15.7 (*Catalina*) or higher.
* Xcode Version 12.4 (12D4e) or higher using the included toolchain (*Recommended*).
* [Swift-android-toolchain-5.4.2-RELEASE](https://github.com/Guang1234567/swift_android_all_in_one/tree/swift_android_5.4.2_release) (**Recommended**)
* Android sdk(latest) and ndk(21.4.7075529)

## Build

To build Signals from the command line:

- other platform
```
% cd <path-to-clone>
% swift build
```

- android

```
#!/usr/bin/env bash

export ANDROID_HOME=$HOME/dev_kit/sdk/android_sdk
export ANDROID_NDK_HOME=$ANDROID_HOME/ndk/21.4.7075529

# clone from https://github.com/Guang1234567/swift_android_all_in_one/tree/swift_android_5.4.2_release
export SWIFT_ANDROID_HOME=$HOME/dev_kit/sdk/swift-android-5.4.2-release-ndk21

export SWIFT_ANDROID_ARCH=aarch64
#export SWIFT_ANDROID_ARCH=armv7
#export SWIFT_ANDROID_ARCH=x86_64
#export SWIFT_ANDROID_ARCH=x86
export SWIFT_ANDROID_API=23

cd Swift_Signals

echo -e "Running on macOS:\n=======================================\n"
swift run Example

echo -e "Running on androidOS:\n=======================================\n"

${SWIFT_ANDROID_HOME}/build-tools/1.9.7-swift5.4/swift-build --configuration debug -Xswiftc -DDEBUG -Xswiftc -g

echo -e "Copy ELF to real android device :\n"

adb push .build/aarch64-unknown-linux-android/debug/Example /data/local/tmp

echo -e "Copy swift runtime SO to real android device :\n"

adb push ${SWIFT_ANDROID_HOME}/toolchain/usr/lib/swift/android/${SWIFT_ANDROID_ARCH}/*.so /data/local/tmp

echo -e "Running on real android device :\n"

adb shell LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/Example

cd ..
```

## Using Signals

### Including in your project
Expand Down
62 changes: 62 additions & 0 deletions Sources/Example/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Foundation
import Signals

let reason = CommandLine.arguments.count == 2 ? CommandLine.arguments[1] : "unknown"

func A1() -> Void {
A2()
}

func A2() -> Void {
A3()
}

func A3() -> Void {
B1()
}

func B1() -> Void {
B2()
}

func B2() -> Void {
B3()
}

func B3() -> Void {
Van().drive()
}

class Van {
func drive() -> Void {

print("\n\n\n")
print("👇👇👇")
fatalError(reason) // raise SIGILL here !!!
print("👆👆👆")
print("\n\n\n")
}

func oilEmpty() throws -> Void {
throw CarError.oilEmpty(message: "!!! oil empty !!!")
}
}

enum CarError: Error {
case oilEmpty(message: String)
}

Signals.trap(signal: Signals.Signal.ill) { signal in

print("----> \(signal)")

if (signal == SIGILL) {
#if os(macOS)
exit(EXIT_FAILURE)
#endif
}
}

A1()


Loading