Skip to content

Commit b43626a

Browse files
authored
chore: migrate to cargokit (#28)
* remove toolbox * Squashed 'super_native_extensions/cargokit/' content from commit 5939b92 git-subtree-dir: super_native_extensions/cargokit git-subtree-split: 5939b92685aa779b1740b6f6501e6f708a12bf3d * Squashed 'super_native_extensions/cargokit/' changes from 5939b92..2ebfade 2ebfade fix problem with accessing gradle plugin properties too early git-subtree-dir: super_native_extensions/cargokit git-subtree-split: 2ebfade1d84c7e73e2f00a504277efa81b38d22f * migrate to cargokit * Squashed 'super_native_extensions/cargokit/' changes from 2ebfade..c4fc1fc c4fc1fc fix regression for static libraries in cocoapods git-subtree-dir: super_native_extensions/cargokit git-subtree-split: c4fc1fcf9b621430e1a298b162f9ae02533a0fce * migrate to cargokit (linux) * migrate to cargokit (windows)
1 parent 19b2625 commit b43626a

26 files changed

+384
-302
lines changed

super_drag_and_drop/example/ios/Podfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ EXTERNAL SOURCES:
2121
SPEC CHECKSUMS:
2222
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
2323
irondash_engine_context: 3458bf979b90d616ffb8ae03a150bafe2e860cc9
24-
super_native_extensions: 55d087ec425f0a4af81a899705181ee51636a12d
24+
super_native_extensions: 9d1f9d92e81e884c1d211d7c631de7abefb95f32
2525

2626
PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3
2727

super_drag_and_drop/example/macos/Podfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ EXTERNAL SOURCES:
2121
SPEC CHECKSUMS:
2222
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
2323
irondash_engine_context: da62996ee25616d2f01bbeb85dc115d813359478
24-
super_native_extensions: 4325f59d4bf41d417ca9567420b37f278c71cd83
24+
super_native_extensions: 639855da5aaa8ef5d63f26e0889423d201ec5e19
2525

2626
PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7
2727

super_native_extensions/android/build.gradle

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ android {
3434
}
3535
}
3636

37-
apply from: "../toolbox/gradle/plugin.gradle"
3837

39-
toolbox {
38+
apply from: "../cargokit/gradle/plugin.gradle"
39+
40+
cargokit {
4041
manifestDir = "../rust"
4142
libname = "super_native_extensions"
4243
}
File renamed without changes.

super_native_extensions/toolbox/build_pod.sh super_native_extensions/cargokit/build_pod.sh

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ NEW_PATH=`echo $PATH | tr ":" "\n" | grep -v "Contents/Developer/" | tr "\n" ":"
88

99
export PATH=${NEW_PATH%?} # remove trailing :
1010

11-
export TOOLBOX_PLATFORM_NAME=$PLATFORM_NAME
12-
export TOOLBOX_ARCHS=$ARCHS
13-
export TOOLBOX_CONFIGURATION=$CONFIGURATION
14-
export TOOLBOX_SRCROOT=$PODS_TARGET_SRCROOT
15-
export TOOLBOX_TEMP_DIR=$TARGET_TEMP_DIR
16-
export TOOLBOX_PRODUCT_NAME=$PRODUCT_NAME
17-
export TOOLBOX_TARGET_DIR=$PODS_CONFIGURATION_BUILD_DIR
18-
export TOOLBOX_TOOL_TEMP_DIR=$TARGET_TEMP_DIR/rust_tool
11+
export CARGOKIT_PLATFORM_NAME=$PLATFORM_NAME
12+
export CARGOKIT_ARCHS=$ARCHS
13+
export CARGOKIT_CONFIGURATION=$CONFIGURATION
14+
export CARGOKIT_SRCROOT=$PODS_TARGET_SRCROOT
15+
export CARGOKIT_TEMP_DIR=$TARGET_TEMP_DIR
16+
export CARGOKIT_PRODUCT_NAME=$PRODUCT_NAME
17+
export CARGOKIT_TARGET_DIR=$PODS_CONFIGURATION_BUILD_DIR
18+
export CARGOKIT_TOOL_TEMP_DIR=$TARGET_TEMP_DIR/rust_tool
1919

2020
$BASEDIR/run_rust_tool.sh build_pod $@
2121

2222
# Make a symlink from built framework to phony file, which will be used as input to
2323
# build script. This should force rebuild (podspec currently doesn't support alwaysOutOfDate
2424
# attribute on custom build phase)
25-
ln -Fs "${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}" "${TARGET_TEMP_DIR}/toolbox_phony"
25+
ln -Fs "${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}" "${TARGET_TEMP_DIR}/cargokit_phony"
26+
ln -Fs "${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}" "${TARGET_TEMP_DIR}/cargokit_phony_out"

super_native_extensions/toolbox/build_tool/Cargo.toml super_native_extensions/cargokit/build_tool/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Toolbox will get symlinked to project, but it's not part of workspace
1+
# CargoKit will get symlinked to project, but it's not part of workspace
22
[workspace]
33

44
[package]

super_native_extensions/toolbox/build_tool/src/build_android.rs super_native_extensions/cargokit/build_tool/src/build_android.rs

+36-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
use std::{fmt::Debug, fs, io::Write, path::Path, process::Command};
2-
3-
use anyhow::Result;
1+
use std::{
2+
fmt::Debug,
3+
fs,
4+
io::Write,
5+
path::{Path, PathBuf},
6+
process::Command,
7+
};
8+
9+
use anyhow::{Context, Result};
410
use log::{debug, info};
511
use semver::Version;
612

@@ -72,7 +78,7 @@ impl Target {
7278
}
7379

7480
fn get_targets() -> Vec<Target> {
75-
let platforms = std::env::var("TOOLBOX_TARGET_PLATFORMS")
81+
let platforms = std::env::var("CARGOKIT_TARGET_PLATFORMS")
7682
.ok()
7783
.unwrap_or_else(|| "".into());
7884
platforms
@@ -83,7 +89,7 @@ fn get_targets() -> Vec<Target> {
8389
}
8490

8591
fn is_release() -> bool {
86-
let configuration = std::env::var("TOOLBOX_BUILD_MODE")
92+
let configuration = std::env::var("CARGOKIT_BUILD_MODE")
8793
.ok()
8894
.unwrap_or_else(|| "release".into());
8995
configuration != "debug"
@@ -105,7 +111,7 @@ const CLANG_TOOL_EXTENSION: &str = "";
105111
// Workaround for libgcc missing in NDK23, inspired by cargo-ndk
106112
fn libgcc_workaround(build_dir: &Path, ndk_version: &Version) -> Result<String> {
107113
let workaround_dir = build_dir
108-
.join("toolbox")
114+
.join("cargokit")
109115
.join("libgcc_workaround")
110116
.join(ndk_version.major.to_string());
111117
fs::create_dir_all(&workaround_dir)?;
@@ -137,20 +143,28 @@ fn libgcc_workaround(build_dir: &Path, ndk_version: &Version) -> Result<String>
137143
Ok(rustflags)
138144
}
139145

146+
fn pick_existing(paths: Vec<PathBuf>) -> Option<PathBuf> {
147+
paths.into_iter().find(|p| p.exists())
148+
}
149+
140150
fn build_for_target(target: &Target) -> Result<()> {
141-
let min_version = string_from_env("TOOLBOX_MIN_SDK_VERSION")?;
151+
let min_version = string_from_env("CARGOKIT_MIN_SDK_VERSION")?;
142152
let min_version: i32 = min_version.parse()?;
143153
let min_version = min_version.max(target.min_sdk_version());
144154

145-
let toolchain_path = path_from_env("TOOLBOX_NDK_DIR")?
155+
let toolchain_path = path_from_env("CARGOKIT_NDK_DIR")?
146156
.join("toolchains")
147157
.join("llvm")
148158
.join("prebuilt")
149159
.join(ARCH)
150160
.join("bin");
151161

152162
let ar_key = format!("AR_{}", target.rust_target());
153-
let ar_value = toolchain_path.join(format!("{}-ar", target.ndk_prefix()));
163+
let ar_value = pick_existing(vec![
164+
toolchain_path.join(format!("{}-ar", target.ndk_prefix())),
165+
toolchain_path.join("llvm-ar"),
166+
])
167+
.expect("Did not find ar tool");
154168

155169
let cc_key = format!("CC_{}", target.rust_target());
156170
let cc_value = toolchain_path.join(format!(
@@ -177,10 +191,10 @@ fn build_for_target(target: &Target) -> Result<()> {
177191
.to_ascii_uppercase();
178192
let linker_value = cc_value.clone();
179193

180-
let ndk_version = string_from_env("TOOLBOX_NDK_VERSION")?;
181-
let build_dir = path_from_env("TOOLBOX_BUILD_DIR")?;
182-
let output_dir = path_from_env("TOOLBOX_OUTPUT_DIR")?;
183-
let lib_name = string_from_env("TOOLBOX_LIB_NAME")?;
194+
let ndk_version = string_from_env("CARGOKIT_NDK_VERSION")?;
195+
let build_dir = path_from_env("CARGOKIT_BUILD_DIR")?;
196+
let output_dir = path_from_env("CARGOKIT_OUTPUT_DIR")?;
197+
let lib_name = string_from_env("CARGOKIT_LIB_NAME")?;
184198

185199
let ndk_version = Version::parse(&ndk_version)?;
186200
let rust_flags_value = libgcc_workaround(&build_dir, &ndk_version)?;
@@ -194,7 +208,7 @@ fn build_for_target(target: &Target) -> Result<()> {
194208
let mut cmd = Command::new("cargo");
195209
cmd.arg("build");
196210
cmd.arg("--manifest-path");
197-
cmd.arg(path_from_env("TOOLBOX_MANIFEST_DIR")?.join("Cargo.toml"));
211+
cmd.arg(path_from_env("CARGOKIT_MANIFEST_DIR")?.join("Cargo.toml"));
198212
cmd.arg("-p");
199213
cmd.arg(&lib_name);
200214
if is_release() {
@@ -217,13 +231,14 @@ fn build_for_target(target: &Target) -> Result<()> {
217231
fs::create_dir_all(&output_dir)?;
218232

219233
let lib_name_full = format!("lib{}.so", lib_name);
220-
fs::copy(
221-
build_dir
222-
.join(target.rust_target())
223-
.join(if is_release() { "release" } else { "debug" })
224-
.join(&lib_name_full),
225-
output_dir.join(&lib_name_full),
226-
)?;
234+
let src = build_dir
235+
.join(target.rust_target())
236+
.join(if is_release() { "release" } else { "debug" })
237+
.join(&lib_name_full);
238+
let dst = output_dir.join(&lib_name_full);
239+
fs::copy(&src, &dst)
240+
.with_context(|| format!("dst: {:?}", dst))
241+
.with_context(|| format!("src: {:?}", src))?;
227242

228243
Ok(())
229244
}

super_native_extensions/toolbox/build_tool/src/build_cmake.rs super_native_extensions/cargokit/build_tool/src/build_cmake.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ impl Target {
3131
}
3232

3333
fn is_release() -> bool {
34-
let configuration = std::env::var("TOOLBOX_CONFIGURATION")
34+
let configuration = std::env::var("CARGOKIT_CONFIGURATION")
3535
.ok()
3636
.map(|s| s.to_ascii_lowercase())
3737
.unwrap_or_else(|| "release".into());
3838
configuration != "debug"
3939
}
4040

4141
pub fn build_cmake() -> Result<()> {
42-
let target = string_from_env("TOOLBOX_TARGET_PLATFORM")?;
42+
let target = string_from_env("CARGOKIT_TARGET_PLATFORM")?;
4343
let target = Target::from_flutter_target(&target)
4444
.ok_or_else(|| anyhow::anyhow!("Invalid target {:?}", target))?;
4545
let installed_targets = installed_targets()?;
@@ -48,10 +48,10 @@ pub fn build_cmake() -> Result<()> {
4848
install_target(target.rust_target())?;
4949
}
5050

51-
let build_dir = path_from_env("TOOLBOX_BUILD_DIR")?;
52-
let output_dir = path_from_env("TOOLBOX_TARGET_DIR")?;
53-
let lib_name = string_from_env("TOOLBOX_LIB_NAME")?;
54-
let manifest_path = path_from_env("TOOLBOX_MANIFEST_DIR")?
51+
let build_dir = path_from_env("CARGOKIT_BUILD_DIR")?;
52+
let output_dir = path_from_env("CARGOKIT_TARGET_DIR")?;
53+
let lib_name = string_from_env("CARGOKIT_LIB_NAME")?;
54+
let manifest_path = path_from_env("CARGOKIT_MANIFEST_DIR")?
5555
.join("Cargo.toml")
5656
.canonicalize()?;
5757

super_native_extensions/toolbox/build_tool/src/build_pod.rs super_native_extensions/cargokit/build_tool/src/build_pod.rs

+63-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
use anyhow::Result;
22
use log::info;
3-
use std::{env::Args, path::PathBuf, process::Command};
3+
use std::{
4+
env::Args,
5+
path::{Path, PathBuf},
6+
process::Command,
7+
};
48

59
use super::utils::*;
610

711
fn get_archs() -> Vec<String> {
8-
let archs = std::env::var("TOOLBOX_ARCHS")
12+
let archs = std::env::var("CARGOKIT_ARCHS")
913
.ok()
1014
.unwrap_or_else(|| "x86_64".into());
1115
archs.split(' ').into_iter().map(String::from).collect()
1216
}
1317

1418
fn target_for_arch(arch: &str) -> Result<String> {
15-
let platform_name = string_from_env("TOOLBOX_PLATFORM_NAME")?;
19+
let platform_name = string_from_env("CARGOKIT_PLATFORM_NAME")?;
1620
let suffix = match platform_name.as_str() {
1721
"macosx" => "apple-darwin",
1822
"iphonesimulator" => "apple-ios-sim",
@@ -37,27 +41,27 @@ fn target_for_arch(arch: &str) -> Result<String> {
3741
}
3842

3943
fn is_release() -> bool {
40-
let configuration = std::env::var("TOOLBOX_CONFIGURATION")
44+
let configuration = std::env::var("CARGOKIT_CONFIGURATION")
4145
.ok()
4246
.unwrap_or_else(|| "Release".into());
4347
configuration != "Debug"
4448
}
4549

4650
fn manifest_path(src_path: &str) -> Result<PathBuf> {
47-
let src_root = path_from_env("TOOLBOX_SRCROOT")?;
51+
let src_root = path_from_env("CARGOKIT_SRCROOT")?;
4852
// Resolve symlink so that crates with relative paths work properly
4953
// when using as flutter plugin (which itself is symlinked)
5054
Ok(src_root.join(src_path).join("Cargo.toml").canonicalize()?)
5155
}
5256

5357
fn temp_target_dir() -> Result<PathBuf> {
54-
let target_dir = string_from_env("TOOLBOX_TEMP_DIR")?;
58+
let target_dir = string_from_env("CARGOKIT_TEMP_DIR")?;
5559
Ok(target_dir.into())
5660
}
5761

5862
fn final_target_dir() -> Result<PathBuf> {
59-
let product_name = string_from_env("TOOLBOX_PRODUCT_NAME")?;
60-
let target_path = path_from_env("TOOLBOX_TARGET_DIR")?;
63+
let product_name = string_from_env("CARGOKIT_PRODUCT_NAME")?;
64+
let target_path = path_from_env("CARGOKIT_TARGET_DIR")?;
6165
Ok(target_path.join(product_name))
6266
}
6367

@@ -92,21 +96,63 @@ pub fn build_pod(mut args: Args) -> Result<()> {
9296
run_command(cmd)?;
9397
}
9498

95-
{
96-
let lib_name = format!("lib{}.a", lib_name);
99+
let lib_path = |lib_name: &str, arch: &str| -> Result<PathBuf> {
100+
let path = temp_target_dir()?
101+
.join(target_for_arch(arch)?)
102+
.join(if is_release() { "release" } else { "debug" })
103+
.join(lib_name);
104+
Ok(path)
105+
};
106+
107+
let perform_lipo = |target_file: &Path, lib_name: &str| -> Result<()> {
97108
let mut cmd = Command::new("lipo");
98109
cmd.arg("-create");
99110
for arch in &archs {
100-
let path = temp_target_dir()?
101-
.join(target_for_arch(arch)?)
102-
.join(if is_release() { "release" } else { "debug" })
103-
.join(&lib_name);
111+
let path = lib_path(lib_name, arch)?;
104112
cmd.arg(path);
105113
}
106114
cmd.arg("-output");
107-
cmd.arg(final_target_dir()?.join(lib_name));
115+
cmd.arg(target_file);
108116
run_command(cmd)?;
109-
}
117+
Ok(())
118+
};
119+
120+
let static_lib_name = format!("lib{}.a", lib_name);
121+
let is_static_lib = archs.iter().any(|a| {
122+
lib_path(&static_lib_name, a)
123+
.map(|a| a.exists())
124+
.unwrap_or(false)
125+
});
110126

111-
Ok(())
127+
// If the crate builds both staticlib and cdylib, we use static lib, assuming
128+
// it will be linked with the rest of pod.
129+
if is_static_lib {
130+
let target_file = final_target_dir()?.join(&static_lib_name);
131+
perform_lipo(&target_file, &static_lib_name)?;
132+
Ok(())
133+
} else {
134+
let bundle_paths = &[
135+
format!("{}.framework/Versions/A/{}", lib_name, lib_name),
136+
format!("{}.framework/{}", lib_name, lib_name), // iOS
137+
];
138+
139+
// Dynamic library in a bundle. Replace bundle dylib with lipoed rust dylib.
140+
for bundle_path in bundle_paths {
141+
let target_file = final_target_dir()?.join(bundle_path);
142+
if target_file.exists() {
143+
let lib_name = format!("lib{}.dylib", lib_name);
144+
perform_lipo(&target_file, &lib_name)?;
145+
146+
// Replace absolute id with @rpath one so that it works properly
147+
// when moved to Frameworks.
148+
let mut cmd = Command::new("install_name_tool");
149+
cmd.arg("-id");
150+
cmd.arg(format!("@rpath/{}", bundle_path));
151+
cmd.arg(&target_file);
152+
run_command(cmd)?;
153+
return Ok(());
154+
}
155+
}
156+
Err(anyhow::anyhow!("Unable to find bundle for dynamic library"))
157+
}
112158
}

super_native_extensions/toolbox/build_tool/src/build_tool.rs super_native_extensions/cargokit/build_tool/src/build_tool.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod logger;
1010
mod utils;
1111

1212
fn init_logging() -> Result<()> {
13-
let silent = std::env::var("TOOLBOX_SILENT")
13+
let silent = std::env::var("CARGOKIT_SILENT")
1414
.ok()
1515
.unwrap_or_else(|| "".into());
1616
let level = if silent == "1" {
@@ -24,9 +24,9 @@ fn init_logging() -> Result<()> {
2424
}
2525

2626
fn dump_environment() -> Result<()> {
27-
debug!("Toolbox environment:");
27+
debug!("CargoKit environment:");
2828
for var in std::env::vars() {
29-
if var.0.to_ascii_lowercase().starts_with("toolbox_") {
29+
if var.0.to_ascii_lowercase().starts_with("cargokit_") {
3030
debug!("{}={}", var.0, var.1);
3131
}
3232
}
@@ -55,7 +55,7 @@ fn run() -> Result<()> {
5555
fn main() {
5656
let res = run();
5757
if let Err(error) = res {
58-
eprintln!("Build tool failed:\n{}", error);
58+
eprintln!("Build tool failed:\n{:?}", error);
5959
exit(1);
6060
}
6161
}

0 commit comments

Comments
 (0)