Skip to content

Commit

Permalink
feat: infer library name from Cargo.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp committed Aug 30, 2023
1 parent 47b4637 commit a267aa7
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 50 deletions.
3 changes: 0 additions & 3 deletions build_pod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ export CARGOKIT_CONFIGURATION=$CONFIGURATION
# Path to directory containing Cargo.toml.
export CARGOKIT_MANIFEST_DIR=$PODS_TARGET_SRCROOT/$1

# Name of Rust library being built.
export CARGOKIT_LIB_NAME=$2

# Temporary directory for build artifacts.
export CARGOKIT_TARGET_TEMP_DIR=$TARGET_TEMP_DIR

Expand Down
8 changes: 4 additions & 4 deletions build_tool/lib/src/artifacts_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ class ArtifactProvider {
for (final target in targets) {
final builder = RustBuilder(target: target, environment: environment);
builder.prepare(rustup);
_log.info('Building ${environment.libName} for $target');
_log.info('Building ${environment.crateInfo.packageName} for $target');
final targetDir = await builder.build();
// For local build accept both static and dynamic libraries.
final artifactNames = <String>{
...getArtifactNames(
target: target,
libraryName: environment.libName,
libraryName: environment.crateInfo.packageName,
aritifactType: AritifactType.dylib,
remote: false,
),
...getArtifactNames(
target: target,
libraryName: environment.libName,
libraryName: environment.crateInfo.packageName,
aritifactType: AritifactType.staticlib,
remote: false,
)
Expand Down Expand Up @@ -119,7 +119,7 @@ class ArtifactProvider {
for (final target in targets) {
final requiredArtifacts = getArtifactNames(
target: target,
libraryName: environment.libName,
libraryName: environment.crateInfo.packageName,
remote: true,
);
final artifactsForTarget = <Artifact>[];
Expand Down
9 changes: 5 additions & 4 deletions build_tool/lib/src/build_pod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ class BuildPod {
.where((element) => element.type == AritifactType.dylib)
.toList();

final libName = environment.crateInfo.packageName;

// If there is static lib, use it and link it with pod
if (staticLibs.isNotEmpty) {
final finalTargetFile =
path.join(outputDir, "lib${environment.libName}.a");
final finalTargetFile = path.join(outputDir, "lib$libName.a");
performLipo(finalTargetFile, staticLibs.map((e) => e.path));
} else {
// Otherwise try to replace bundle dylib with our dylib
final bundlePaths = [
'${environment.libName}.framework/Versions/A/${environment.libName}',
'${environment.libName}.framework/${environment.libName}',
'$libName.framework/Versions/A/$libName',
'$libName.framework/$libName',
];

for (final bundlePath in bundlePaths) {
Expand Down
24 changes: 5 additions & 19 deletions build_tool/lib/src/build_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ class PrecompileBinariesCommand extends Command {
mandatory: true,
help: 'Directory containing Cargo.toml',
)
..addOption(
'lib-name',
mandatory: true,
help: 'Name of the library to build.',
)
..addMultiOption('target',
help: 'Rust target triple of artifact to build.\n'
'Can be specified multiple times or omitted in which case\n'
Expand Down Expand Up @@ -192,7 +187,6 @@ class PrecompileBinariesCommand extends Command {
final precompileBinaries = PrecompileBinaries(
privateKey: PrivateKey(privateKey),
githubToken: githubToken,
libName: argResults!['lib-name'] as String,
manifestDir: manifestDir,
repositorySlug: RepositorySlug.full(argResults!['repository'] as String),
targets: targets,
Expand All @@ -208,17 +202,11 @@ class PrecompileBinariesCommand extends Command {

class VerifyBinariesCommand extends Command {
VerifyBinariesCommand() {
argParser
..addOption(
'manifest-dir',
mandatory: true,
help: 'Directory containing Cargo.toml',
)
..addOption(
'lib-name',
mandatory: true,
help: 'Name of the library to build.',
);
argParser.addOption(
'manifest-dir',
mandatory: true,
help: 'Directory containing Cargo.toml',
);
}

@override
Expand All @@ -232,10 +220,8 @@ class VerifyBinariesCommand extends Command {
@override
Future<void> run() async {
final manifestDir = argResults!['manifest-dir'] as String;
final libName = argResults!['lib-name'] as String;
final verifyBinaries = VerifyBinaries(
manifestDir: manifestDir,
libraryName: libName,
);
await verifyBinaries.run();
}
Expand Down
10 changes: 6 additions & 4 deletions build_tool/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:collection/collection.dart';
import 'package:path/path.dart' as path;

import 'android_environment.dart';
import 'cargo.dart';
import 'environment.dart';
import 'options.dart';
import 'rustup.dart';
Expand Down Expand Up @@ -39,7 +40,7 @@ class BuildEnvironment {
final CargokitCrateOptions crateOptions;
final String targetTempDir;
final String manifestDir;
final String libName;
final CrateInfo crateInfo;

final bool isAndroid;
final String? androidSdkPath;
Expand All @@ -52,7 +53,7 @@ class BuildEnvironment {
required this.crateOptions,
required this.targetTempDir,
required this.manifestDir,
required this.libName,
required this.crateInfo,
required this.isAndroid,
this.androidSdkPath,
this.androidNdkVersion,
Expand All @@ -75,12 +76,13 @@ class BuildEnvironment {
final crateOptions = CargokitCrateOptions.load(
manifestDir: manifestDir,
);
final crateInfo = CrateInfo.load(manifestDir);
return BuildEnvironment(
configuration: buildConfiguration,
crateOptions: crateOptions,
targetTempDir: Environment.targetTempDir,
manifestDir: manifestDir,
libName: Environment.libName,
crateInfo: crateInfo,
isAndroid: isAndroid,
androidSdkPath: isAndroid ? Environment.sdkPath : null,
androidNdkVersion: isAndroid ? Environment.ndkVersion : null,
Expand Down Expand Up @@ -135,7 +137,7 @@ class RustBuilder {
'--manifest-path',
manifestPath,
'-p',
environment.libName,
environment.crateInfo.packageName,
if (!environment.configuration.isDebug) '--release',
'--target',
target.rust,
Expand Down
45 changes: 45 additions & 0 deletions build_tool/lib/src/cargo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:toml/toml.dart';

class ManifestException {
ManifestException(this.message, {required this.fileName});

final String? fileName;
final String message;

@override
String toString() {
if (fileName != null) {
return 'Failed to parse package manifest at $fileName: $message';
} else {
return 'Failed to parse package manifest: $message';
}
}
}

class CrateInfo {
CrateInfo({required this.packageName});

final String packageName;

static CrateInfo parseManifest(String manifest, {final String? fileName}) {
final toml = TomlDocument.parse(manifest);
final package = toml.toMap()['package'];
if (package == null) {
throw ManifestException('Missing package section', fileName: fileName);
}
final name = package['name'];
if (name == null) {
throw ManifestException('Missing package name', fileName: fileName);
}
return CrateInfo(packageName: name);
}

static CrateInfo load(String manifestDir) {
final manifestFile = File(path.join(manifestDir, 'Cargo.toml'));
final manifest = manifestFile.readAsStringSync();
return parseManifest(manifest, fileName: manifestFile.path);
}
}
3 changes: 0 additions & 3 deletions build_tool/lib/src/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ class Environment {
/// Path to the crate manifest (containing Cargo.toml).
static String get manifestDir => _getEnvPath('CARGOKIT_MANIFEST_DIR');

/// Crate library name.
static String get libName => _getEnv('CARGOKIT_LIB_NAME');

/// Directory inside root project. Not necessarily root folder. Symlinks are
/// not resolved on purpose.
static String get rootProjectDir => _getEnv('CARGOKIT_ROOT_PROJECT_DIR');
Expand Down
15 changes: 8 additions & 7 deletions build_tool/lib/src/precompile_binaries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:path/path.dart' as path;

import 'artifacts_provider.dart';
import 'builder.dart';
import 'cargo.dart';
import 'crate_hash.dart';
import 'options.dart';
import 'rustup.dart';
Expand All @@ -20,7 +21,6 @@ class PrecompileBinaries {
required this.githubToken,
required this.repositorySlug,
required this.manifestDir,
required this.libName,
required this.targets,
this.androidSdkLocation,
this.androidNdkVersion,
Expand All @@ -32,7 +32,6 @@ class PrecompileBinaries {
final String githubToken;
final RepositorySlug repositorySlug;
final String manifestDir;
final String libName;
final List<Target> targets;
final String? androidSdkLocation;
final String? androidNdkVersion;
Expand All @@ -48,6 +47,8 @@ class PrecompileBinaries {
}

Future<void> run() async {
final crateInfo = CrateInfo.load(manifestDir);

final targets = List.of(this.targets);
if (targets.isEmpty) {
targets.addAll([
Expand All @@ -68,7 +69,7 @@ class PrecompileBinaries {
final release = await _getOrCreateRelease(
repo: repo,
tagName: tagName,
libName: libName,
packageName: crateInfo.packageName,
hash: hash,
);

Expand All @@ -87,7 +88,7 @@ class PrecompileBinaries {
crateOptions: crateOptions,
targetTempDir: tempDir.path,
manifestDir: manifestDir,
libName: libName,
crateInfo: crateInfo,
isAndroid: androidSdkLocation != null,
androidSdkPath: androidSdkLocation,
androidNdkVersion: androidNdkVersion,
Expand All @@ -99,7 +100,7 @@ class PrecompileBinaries {
for (final target in targets) {
final artifactNames = getArtifactNames(
target: target,
libraryName: libName,
libraryName: crateInfo.packageName,
remote: true,
);

Expand Down Expand Up @@ -172,7 +173,7 @@ class PrecompileBinaries {
Future<Release> _getOrCreateRelease({
required RepositoriesService repo,
required String tagName,
required String libName,
required String packageName,
required String hash,
}) async {
Release release;
Expand All @@ -189,7 +190,7 @@ class PrecompileBinaries {
targetCommitish: null,
isDraft: false,
isPrerelease: false,
body: 'Precompiled binaries for crate $libName, '
body: 'Precompiled binaries for crate $packageName, '
'crate hash $hash.',
));
}
Expand Down
7 changes: 4 additions & 3 deletions build_tool/lib/src/verify_binaries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:ed25519_edwards/ed25519_edwards.dart';
import 'package:http/http.dart';

import 'artifacts_provider.dart';
import 'cargo.dart';
import 'crate_hash.dart';
import 'options.dart';
import 'precompile_binaries.dart';
Expand All @@ -12,13 +13,13 @@ import 'target.dart';
class VerifyBinaries {
VerifyBinaries({
required this.manifestDir,
required this.libraryName,
});

final String manifestDir;
final String libraryName;

Future<void> run() async {
final crateInfo = CrateInfo.load(manifestDir);

final config = CargokitCrateOptions.load(manifestDir: manifestDir);
final precompiledBinaries = config.precompiledBinaries;
if (precompiledBinaries == null) {
Expand All @@ -34,7 +35,7 @@ class VerifyBinaries {

final artifacts = getArtifactNames(
target: target,
libraryName: libraryName,
libraryName: crateInfo.packageName,
remote: true,
);

Expand Down
16 changes: 16 additions & 0 deletions build_tool/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.0"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: cb3798bef7fc021ac45b308f4b51208a152792445cce0448c9a4ba5879dd8750
url: "https://pub.dev"
source: hosted
version: "5.4.0"
pool:
dependency: transitive
description:
Expand Down Expand Up @@ -377,6 +385,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.5.6"
toml:
dependency: "direct main"
description:
name: toml
sha256: "157c5dca5160fced243f3ce984117f729c788bb5e475504f3dbcda881accee44"
url: "https://pub.dev"
source: hosted
version: "0.14.0"
typed_data:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion build_tool/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A sample command-line application.
version: 1.0.0

environment:
sdk: '>=3.0.0 <4.0.0'
sdk: ">=3.0.0 <4.0.0"

# Add regular dependencies here.
dependencies:
Expand All @@ -22,6 +22,7 @@ dependencies:
crypto: 3.0.3
convert: 3.1.1
http: 1.1.0
toml: 0.14.0

dev_dependencies:
lints: ^2.1.0
Expand Down
Loading

0 comments on commit a267aa7

Please sign in to comment.