Gomuks SDK Rewrite #2
3 changed files with 90 additions and 109 deletions
wip go 3
commit
33239c02c0
|
|
@ -5,91 +5,6 @@ import "package:path/path.dart";
|
||||||
import "package:ffigen/ffigen.dart";
|
import "package:ffigen/ffigen.dart";
|
||||||
|
|
||||||
Future<void> main(List<String> args) => build(args, (input, output) async {
|
Future<void> main(List<String> args) => build(args, (input, output) async {
|
||||||
final targetOS = input.config.code.targetOS;
|
|
||||||
final targetArch = input.config.code.targetArchitecture;
|
|
||||||
String libFileName;
|
|
||||||
switch (targetOS) {
|
|
||||||
case OS.linux:
|
|
||||||
libFileName = "libgomuks.so";
|
|
||||||
break;
|
|
||||||
case OS.macOS:
|
|
||||||
libFileName = "libgomuks.dylib";
|
|
||||||
break;
|
|
||||||
case OS.windows:
|
|
||||||
libFileName = "libgomuks.dll";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw UnsupportedError("Unsupported OS: $targetOS");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Where we put the Gomuks repo and compiled library
|
|
||||||
final buildDir = Directory.fromUri(
|
|
||||||
input.outputDirectoryShared.resolve("gomuks/"),
|
|
||||||
);
|
|
||||||
if (!await buildDir.exists()) await buildDir.create(recursive: true);
|
|
||||||
|
|
||||||
final repoDir = Directory(join(buildDir.path, "source"));
|
|
||||||
|
|
||||||
bool skipBuild = false;
|
|
||||||
|
|
||||||
final generatedSourcePath = "src/third_party/gomuks.g.dart";
|
|
||||||
final generatedLibPath = input.packageRoot.resolve(
|
|
||||||
join("lib", generatedSourcePath),
|
|
||||||
);
|
|
||||||
final bindingsFile = File(generatedLibPath.toFilePath());
|
|
||||||
|
|
||||||
if (await bindingsFile.exists() &&
|
|
||||||
await File(join(buildDir.path, libFileName)).exists() &&
|
|
||||||
await repoDir.exists()) {
|
|
||||||
final result = await Process.run("git", [
|
|
||||||
"fetch",
|
|
||||||
"--dry-run",
|
|
||||||
], workingDirectory: repoDir.path);
|
|
||||||
|
|
||||||
if ((result.stdout as String).trim().isEmpty) {
|
|
||||||
skipBuild = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (skipBuild) {
|
|
||||||
return print(
|
|
||||||
"Gomuks build skipped: bindings and library exist and repo is up to date.",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (await repoDir.exists()) await repoDir.delete(recursive: true);
|
|
||||||
|
|
||||||
print("Cloning Gomuks repository...");
|
|
||||||
final cloneResult = await Process.run("git", [
|
|
||||||
"clone",
|
|
||||||
"--depth",
|
|
||||||
"1",
|
|
||||||
"--branch",
|
|
||||||
"tulir/ffi",
|
|
||||||
"https://mau.dev/gomuks/gomuks",
|
|
||||||
repoDir.path,
|
|
||||||
]);
|
|
||||||
if (cloneResult.exitCode != 0) {
|
|
||||||
throw Exception(
|
|
||||||
"Failed to clone Gomuks repository: \n${cloneResult.stderr}",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
final libFile = File(join(buildDir.path, libFileName));
|
|
||||||
|
|
||||||
print("Building Gomuks shared library for $targetOS/$targetArch...");
|
|
||||||
final result = await Process.run("go", [
|
|
||||||
"build",
|
|
||||||
"-o",
|
|
||||||
libFile.path,
|
|
||||||
"-buildmode=c-shared",
|
|
||||||
], workingDirectory: join(repoDir.path, "pkg/ffi"));
|
|
||||||
|
|
||||||
if (result.exitCode != 0) {
|
|
||||||
throw Exception("Failed to build Gomuks shared library\n${result.stderr}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the library as a code asset so Dart can find it
|
|
||||||
output.assets.code.add(
|
output.assets.code.add(
|
||||||
CodeAsset(
|
CodeAsset(
|
||||||
package: "nexus",
|
package: "nexus",
|
||||||
|
|
@ -99,13 +14,4 @@ Future<void> main(List<String> args) => build(args, (input, output) async {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
output.dependencies.add(libFile.uri);
|
output.dependencies.add(libFile.uri);
|
||||||
|
|
||||||
print("Generating FFI Bindings...");
|
|
||||||
FfiGenerator(
|
|
||||||
output: Output(dartFile: generatedLibPath),
|
|
||||||
headers: Headers(
|
|
||||||
entryPoints: [File(join(buildDir.path, "libgomuks.h")).uri],
|
|
||||||
),
|
|
||||||
functions: Functions.includeAll,
|
|
||||||
).generate();
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -5,21 +5,6 @@ import "dart:typed_data";
|
||||||
import "package:ffi/ffi.dart";
|
import "package:ffi/ffi.dart";
|
||||||
import "package:nexus/src/third_party/gomuks.g.dart";
|
import "package:nexus/src/third_party/gomuks.g.dart";
|
||||||
|
|
||||||
extension GomuksBufferX on GomuksBuffer {
|
|
||||||
/// Safely converts the Go buffer into a Dart `Uint8List`
|
|
||||||
Uint8List toBytes() {
|
|
||||||
if (base == nullptr || length <= 0) return Uint8List(0);
|
|
||||||
return base.asTypedList(length);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Decodes the bytes as JSON
|
|
||||||
Map<String, dynamic> toJson() {
|
|
||||||
final bytes = toBytes();
|
|
||||||
if (bytes.isEmpty) return {};
|
|
||||||
return jsonDecode(utf8.decode(bytes));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension JsonToGomuksBuffer on Map<String, dynamic> {
|
extension JsonToGomuksBuffer on Map<String, dynamic> {
|
||||||
GomuksBuffer toGomuksBuffer() {
|
GomuksBuffer toGomuksBuffer() {
|
||||||
final jsonString = json.encode(this);
|
final jsonString = json.encode(this);
|
||||||
|
|
|
||||||
90
scripts/generate.dart
Normal file
90
scripts/generate.dart
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
import "dart:io";
|
||||||
|
import "package:ffigen/ffigen.dart";
|
||||||
|
import "package:path/path.dart";
|
||||||
|
|
||||||
|
void main(List<String> args) async {
|
||||||
|
// Where we put the Gomuks repo and compiled library
|
||||||
|
final buildDir = Directory.fromUri(
|
||||||
|
Platform.script.resolve(join("..", "build", "gomuks")),
|
||||||
|
);
|
||||||
|
if (!await buildDir.exists()) await buildDir.create(recursive: true);
|
||||||
|
|
||||||
|
final repoDir = Directory(join(buildDir.path, "source"));
|
||||||
|
|
||||||
|
bool skipBuild = false;
|
||||||
|
|
||||||
|
final generatedSourcePath = join("src", "third_party", "gomuks.g.dart");
|
||||||
|
final generatedLibPath = Platform.script.resolve(
|
||||||
|
join("..", "lib", generatedSourcePath),
|
||||||
|
);
|
||||||
|
final bindingsFile = File(generatedLibPath.toFilePath());
|
||||||
|
|
||||||
|
if (await bindingsFile.exists() &&
|
||||||
|
await repoDir.exists() &&
|
||||||
|
!args.contains("--rebuild")) {
|
||||||
|
final result = await Process.run("git", [
|
||||||
|
"fetch",
|
||||||
|
"--dry-run",
|
||||||
|
], workingDirectory: repoDir.path);
|
||||||
|
|
||||||
|
if ((result.stdout as String).trim().isEmpty) {
|
||||||
|
skipBuild = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (skipBuild) {
|
||||||
|
return print(
|
||||||
|
"Gomuks build skipped: bindings and library exist and repo is up to date.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await repoDir.exists()) await repoDir.delete(recursive: true);
|
||||||
|
|
||||||
|
print("Cloning Gomuks repository...");
|
||||||
|
final cloneResult = await Process.run("git", [
|
||||||
|
"clone",
|
||||||
|
"--branch",
|
||||||
|
"tulir/ffi",
|
||||||
|
"--depth",
|
||||||
|
"1",
|
||||||
|
"https://mau.dev/gomuks/gomuks",
|
||||||
|
repoDir.path,
|
||||||
|
]);
|
||||||
|
if (cloneResult.exitCode != 0) {
|
||||||
|
throw Exception(
|
||||||
|
"Failed to clone Gomuks repository: \n${cloneResult.stderr}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (final name in ["libgomuks.so", "libgomuks.dylib", "libgomuks.dll"]) {
|
||||||
|
final libFile = File(join(buildDir.path, name));
|
||||||
|
|
||||||
|
print("Building Gomuks shared library $name...");
|
||||||
|
final result = await Process.run("go", [
|
||||||
|
"build",
|
||||||
|
"-o",
|
||||||
|
libFile.path,
|
||||||
|
"-buildmode=c-shared",
|
||||||
|
], workingDirectory: join(repoDir.path, "pkg/ffi"));
|
||||||
|
|
||||||
|
if (result.exitCode != 0) {
|
||||||
|
throw Exception(
|
||||||
|
"Failed to build Gomuks shared library\n${result.stderr}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print("Generating FFI Bindings...");
|
||||||
|
final packageRoot = Platform.script.resolve("../");
|
||||||
|
FfiGenerator(
|
||||||
|
output: Output(
|
||||||
|
dartFile: packageRoot.resolve("lib/src/third_party/gomuks.g.dart"),
|
||||||
|
),
|
||||||
|
headers: Headers(
|
||||||
|
entryPoints: [File(join(buildDir.path, "libgomuks.h")).uri],
|
||||||
|
compilerOptions: ["-I${String.fromEnvironment("CPATH")}"],
|
||||||
|
),
|
||||||
|
functions: Functions.includeAll,
|
||||||
|
).generate();
|
||||||
|
print("Done!");
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue