Skip to content

Commit

Permalink
✨ feat: Update copy script for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 11, 2024
1 parent ec909a9 commit b49b654
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
56 changes: 56 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# swc4j Rust Library

## Preparation

### Install Targets

Follow the [Cross-compilation](https://rust-lang.github.io/rustup/cross-compilation.html) to add the following [supported platforms](https://doc.rust-lang.org/nightly/rustc/platform-support.html).

```sh
# 64-bit MSVC (Windows 7+)
rustup target add x86_64-pc-windows-msvc

# 64-bit Linux (kernel 3.2+, glibc 2.17+)
rustup target add x86_64-unknown-linux-gnu
# ARM64 Linux (kernel 4.1, glibc 2.17+)
rustup target add aarch64-unknown-linux-gnu

# 64-bit macOS (10.12+, Sierra+)
rustup target add x86_64-apple-darwin
# ARM64 macOS (11.0+, Big Sur+)
rustup target add aarch64-apple-darwin

# 32-bit x86 Android
rustup target add i686-linux-android
# 64-bit x86 Android
rustup target add x86_64-linux-android
# ARMv7-A Android
rustup target add armv7-linux-androideabi
# ARM64 Android
rustup target add aarch64-linux-android
```

### Install Android NDK

Install a proper Android NDK.

## Build

```sh
# Windows
cargo build --release --target x86_64-pc-windows-msvc

# Linux
cargo build --release --target x86_64-unknown-linux-gnu
cargo build --release --target aarch64-unknown-linux-gnu

# MacOS
cargo build --release --target x86_64-apple-darwin
cargo build --release --target aarch64-apple-darwin

# Android
cargo build --release --target i686-linux-android
cargo build --release --target x86_64-linux-android
cargo build --release --target armv7-linux-androideabi
cargo build --release --target aarch64-linux-android
```
45 changes: 38 additions & 7 deletions scripts/ts/copy_swc4j_lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,66 @@ const OS_CONFIG_MAP = {
sourceName: NAME,
targetName: `lib${NAME}`,
ext: '.dll',
targets: {
'x86_64': 'x86_64-pc-windows-msvc',
}
},
'linux': {
sourceName: `lib${NAME}`,
targetName: `lib${NAME}`,
ext: '.so',
targets: {
'x86_64': 'x86_64-unknown-linux-gnu',
'arm64': 'aarch64-unknown-linux-gnu',
}
},
'android': {
sourceName: `lib${NAME}`,
targetName: `lib${NAME}`,
ext: '.so',
targets: {
'x86': 'i686-linux-android',
'x86_64': 'x86_64-linux-android',
'arm': 'armv7-linux-androideabi',
'arm64': 'aarch64-linux-android',
}
},
'macos': {
sourceName: `lib${NAME}`,
targetName: `lib${NAME}`,
ext: '.dylib',
targets: {
'x86_64': 'x86_64-apple-darwin',
'arm64': 'aarch64-apple-darwin',
}
},
}

function copy(debug: boolean = false, os: string = 'windows', arch: string = 'x86_64'): number {
if (!(os in OS_CONFIG_MAP)) {
console.error(`%c${os} is not supported.`, 'color: red')
return 1;
console.error(`%cOS ${os} is not supported.`, 'color: red')
return 1
}
const config = OS_CONFIG_MAP[os]
if (!(arch in config.targets)) {
console.error(`%cArch ${arch} is not supported.`, 'color: red')
return 1
}
const target = config.targets[arch]
const scriptDirPath = path.dirname(path.fromFileUrl(import.meta.url))
const sourceDirPath = path.join(scriptDirPath, '../../rust/target', debug ? 'debug' : 'release')
const sourceFilePath = path.join(sourceDirPath, `${config.sourceName}${config.ext}`)
let sourceDirPath = path.join(scriptDirPath, '../../rust/target', target, debug ? 'debug' : 'release')
let sourceFilePath = path.join(sourceDirPath, `${config.sourceName}${config.ext}`)
if (!fs.existsSync(sourceFilePath)) {
sourceDirPath = path.join(scriptDirPath, '../../rust/target', debug ? 'debug' : 'release')
sourceFilePath = path.join(sourceDirPath, `${config.sourceName}${config.ext}`)
}
if (!fs.existsSync(sourceFilePath)) {
console.error(`%c${sourceFilePath} is not found.`, 'color: red')
return 1;
return 1
}
const targetDirPath = path.join(scriptDirPath, '../../src/main/resources')
if (!fs.existsSync(targetDirPath)) {
Deno.mkdirSync(targetDirPath, { recursive: true });
Deno.mkdirSync(targetDirPath, { recursive: true })
}
const targetFilePath = path.join(targetDirPath, `${config.targetName}-${os}-${arch}.v.${VERSION}${config.ext}`)
console.info(`Copy from ${sourceFilePath} to ${targetFilePath}.`)
Expand Down Expand Up @@ -101,7 +132,7 @@ if (args.help) {
-a, --arch CPU arch [x86, x86_64, arm, arm64] (default: x86_64)
-d, --debug Copy the debug lib (default: false)
-h, --help Print this help page
-o, --os Operating system [windows, linux, macos] (default: windows)
-o, --os Operating system [windows, linux, android, macos] (default: windows)
-v, --version Print version
`)
} else if (args.version) {
Expand Down

0 comments on commit b49b654

Please sign in to comment.