Skip to content

Commit

Permalink
support for pod/dylib
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp committed Aug 28, 2023
1 parent c32b456 commit 8f96e6d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 17 deletions.
39 changes: 32 additions & 7 deletions build_tool/lib/src/artifacts_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,23 @@ class Artifact {
/// File system location of the artifact.
final String path;

/// Actual file name that the artifact should have in destination folder.ƒ
/// Actual file name that the artifact should have in destination folder.
final String finalFileName;

AritifactType get type {
if (finalFileName.endsWith('.dll') ||
finalFileName.endsWith('.dll.lib') ||
finalFileName.endsWith('.pdb') ||
finalFileName.endsWith('.so') ||
finalFileName.endsWith('.dylib')) {
return AritifactType.dylib;
} else if (finalFileName.endsWith('.lib') || finalFileName.endsWith('.a')) {
return AritifactType.staticlib;
} else {
throw Exception('Unknown artifact type for $finalFileName');
}
}

Artifact({
required this.path,
required this.finalFileName,
Expand Down Expand Up @@ -50,18 +64,29 @@ class ArtifactProvider {
for (final target in targets) {
final builder = RustBuilder(target: target, environment: environment);
builder.prepare(rustup);
final targetDir = await builder.build();
final artifactNames = getArtifactNames(
target: target,
libraryName: environment.libName,
remote: false,
);
_log.info('Building ${environment.libName} 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,
aritifactType: AritifactType.dylib,
remote: false,
),
...getArtifactNames(
target: target,
libraryName: environment.libName,
aritifactType: AritifactType.staticlib,
remote: false,
)
};
final artifacts = artifactNames
.map((artifactName) => Artifact(
path: path.join(targetDir, artifactName),
finalFileName: artifactName,
))
.where((element) => File(element.path).existsSync())
.toList();
result[target] = artifacts;
}
Expand Down
6 changes: 4 additions & 2 deletions build_tool/lib/src/build_cmake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ class BuildCMake {
final libs = artifacts[target]!;

for (final lib in libs) {
File(lib.path)
.copySync(path.join(Environment.outputDir, lib.finalFileName));
if (lib.type == AritifactType.dylib) {
File(lib.path)
.copySync(path.join(Environment.outputDir, lib.finalFileName));
}
}
}
}
4 changes: 3 additions & 1 deletion build_tool/lib/src/build_gradle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class BuildGradle {
Directory(outputDir).createSync(recursive: true);

for (final lib in libs) {
File(lib.path).copySync(path.join(outputDir, lib.finalFileName));
if (lib.type == AritifactType.dylib) {
File(lib.path).copySync(path.join(outputDir, lib.finalFileName));
}
}
}
}
Expand Down
46 changes: 39 additions & 7 deletions build_tool/lib/src/build_pod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,46 @@ class BuildPod {
]);
}

final finalTargetFile =
path.join(Environment.outputDir, "lib${environment.libName}.a");
Directory(Environment.outputDir).createSync(recursive: true);
final outputDir = Environment.outputDir;

final libraries = artifacts.values.map(
(e) => e.first.path,
);
Directory(outputDir).createSync(recursive: true);

performLipo(finalTargetFile, libraries);
final staticLibs = artifacts.values
.expand((element) => element)
.where((element) => element.type == AritifactType.staticlib)
.toList();
final dynamicLibs = artifacts.values
.expand((element) => element)
.where((element) => element.type == AritifactType.dylib)
.toList();

// If there is static lib, use it and link it with pod
if (staticLibs.isNotEmpty) {
final finalTargetFile =
path.join(outputDir, "lib${environment.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}',
];

for (final bundlePath in bundlePaths) {
final targetFile = path.join(outputDir, bundlePath);
if (File(targetFile).existsSync()) {
performLipo(targetFile, dynamicLibs.map((e) => e.path));

// Replace absolute id with @rpath one so that it works properly
// when moved to Frameworks.
runCommand("install_name_tool", [
'-id',
'@rpath/$bundlePath',
targetFile,
]);
}
}
throw Exception('Unable to find bundle for dynamic library');
}
}
}

0 comments on commit 8f96e6d

Please sign in to comment.