Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
bin/setup.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
mahesh-hegde committed Jul 14, 2022
1 parent f820996 commit dd7dfef
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
125 changes: 125 additions & 0 deletions jni/bin/setup.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import 'dart:io';

import 'package:args/args.dart';

const _buildDir = "build-dir";
const _srcDir = "source-dir";
const _verbose = "verbose";
const _cmakeArgs = "cmake-args";
const _clean = "clean";

// Sets up input output channels and maintains state.
class CommandRunner {
CommandRunner({this.printCmds = false});
bool printCmds = false;
int? time;
// TODO: time commands
// TODO: Run all commands in single shell instance
Future<CommandRunner> run(
String exec, List<String> args, String workingDir) async {
if (printCmds) {
final cmd = "$exec ${args.join(" ")}";
stderr.writeln("+ [$workingDir] $cmd");
}
final process =
await Process.start(exec, args, workingDirectory: workingDir);
// stdout.addStream(process.stdout);
final exitCode = await process.exitCode;
if (exitCode != 0) {
stderr.writeln("command exited with $exitCode");
}
return this;
}
}

class Options {
Options(ArgResults arg)
: buildDir = arg[_buildDir],
srcDir = arg[_srcDir],
cmakeArgs = arg[_cmakeArgs],
verbose = arg[_verbose] ?? false,
clean = arg[_clean] ?? false;

String? buildDir, srcDir, cmakeArgs;
bool verbose, clean;
}

late Options options;
void log(String msg) {
if (options.verbose) {
stderr.writeln(msg);
}
}

void main(List<String> arguments) async {
final parser = ArgParser()
..addOption(_buildDir,
abbr: 'B', help: 'Directory to place built artifacts')
..addOption(_srcDir,
abbr: 'S', help: 'alternative path to package:jni sources')
..addFlag(_verbose, abbr: 'v', help: 'Enable verbose output')
..addFlag(_clean,
negatable: false,
abbr: 'C',
help: 'Clear built artifacts instead of running a build')
..addOption(_cmakeArgs,
abbr: 'm',
help: 'additional space separated arguments to pass to CMake');
final cli = parser.parse(arguments);
options = Options(cli);
final rest = cli.rest;

if (rest.isNotEmpty) {
stderr.writeln("one or more unrecognized arguments: $rest");
stderr.writeln("usage: dart run jni:setup <options>");
stderr.writeln(parser.usage);
exitCode = 1;
return;
}

final scriptUri = Platform.script;
log("scriptUri: $scriptUri");
final srcPath = options.srcDir ?? scriptUri.resolve("../src").toFilePath();
final srcDir = Directory(srcPath);
if (!await srcDir.exists()) {
throw 'Directory $srcPath does not exist';
}
log("srcPath: $srcPath");

final currentDirUri = Uri.file(".");
final buildPath =
options.buildDir ?? currentDirUri.resolve("src/build").toFilePath();
final buildDir = Directory(buildPath);
await buildDir.create(recursive: true);
log("buildPath: $buildPath");

if (buildDir.absolute.uri == srcDir.absolute.uri) {
stderr.writeln("Please build in a directory different than source.");
exit(2);
}

if (options.clean) {
await cleanup(options, srcDir.absolute.path, buildDir.absolute.path);
} else {
await build(options, srcDir.absolute.path, buildDir.absolute.path);
}
}

Future<void> build(Options options, String srcPath, String buildPath) async {
final runner = CommandRunner(printCmds: true);
var cmakeArgs = [srcPath];
if (options.cmakeArgs != null) {
cmakeArgs.addAll(options.cmakeArgs!.split(" "));
}
await runner.run("cmake", cmakeArgs, buildPath);
await runner.run("cmake", ["--build", "."], buildPath);
if (Platform.isWindows) {
await runner.run("move", ["Debug\\dartjni.dll", "."], buildPath);
}
}

Future<FileSystemEntity> cleanup(
Options options, String srcPath, String buildPath) async {
stderr.writeln("deleting $buildPath");
return Directory(buildPath).delete(recursive: true);
}
1 change: 1 addition & 0 deletions jni/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
plugin_platform_interface: ^2.0.2
ffi: ^2.0.0
path: ^1.8.0
args:

dev_dependencies:
## Temporarily linking to a personal fork of ffigen
Expand Down

0 comments on commit dd7dfef

Please sign in to comment.