run method

Future<void> run({
  1. required BuildOutput output,
})

Implementation

Future<void> run({required BuildOutput output}) async {
  final toolchain = this.toolchain ?? await RustToolchain.withName('stable');

  final manifestPath = buildConfig.packageRoot
      .resolve(cratePath)
      .makeDirectory()
      .resolve('Cargo.toml');
  final manifestInfo = CrateManifestInfo.load(manifestPath);
  final outDir =
      buildConfig.outputDirectory.resolve('native_toolchain_rust/');

  final dylibName =
      buildConfig.targetOS.dylibFileName(manifestInfo.libraryName);

  if (buildConfig.dryRun) {
    output.addAsset(NativeCodeAsset(
      package: package,
      name: assetName ?? manifestInfo.packageName,
      linkMode: DynamicLoadingBundled(),
      os: buildConfig.targetOS,
      file: Uri.file(dylibName),
    ));
    return;
  }

  final target = RustTarget.from(
    architecture: buildConfig.targetArchitecture!,
    os: buildConfig.targetOS,
    iosSdk: buildConfig.targetOS == OS.iOS ? buildConfig.targetIOSSdk : null,
  )!;

  await toolchain._checkTarget(target: target);
  if (useNativeManifest) {
    await toolchain._checkNativeManifest(buildConfig: buildConfig);
  }

  final effectiveBuildMode = release ? BuildMode.release : BuildMode.debug;

  if (!buildConfig.dryRun) {
    await toolchain.toolchain.rustup.runCommand(
      [
        'run',
        toolchain.name,
        'cargo',
        'build',
        '--manifest-path',
        manifestPath.toFilePath(),
        '-p',
        manifestInfo.packageName,
        if (effectiveBuildMode == BuildMode.release) '--release',
        '--verbose',
        '--target',
        target.triple,
        '--target-dir',
        outDir.toFilePath(),
        ...extraCargoArgs,
      ],
      environment: await _buildEnvironment(
        outDir,
        target,
        toolchain.toolchain,
      ),
      logger: logger,
    );
  }

  final effectiveOutDir = outDir
      .resolve('${target.triple}/')
      .resolve('${effectiveBuildMode.name}/');

  final asset = NativeCodeAsset(
    package: package,
    name: assetName ?? manifestInfo.packageName,
    os: buildConfig.targetOS,
    architecture: buildConfig.targetArchitecture,
    linkMode: DynamicLoadingBundled(),
    file: effectiveOutDir.resolve(dylibName),
  );
  output.addAsset(asset);
  if (!buildConfig.dryRun) {
    _addDependencies(
      output: output,
      effectiveOutDir: effectiveOutDir,
      dylibName: dylibName,
    );
  }
  for (final source in dartBuildFiles) {
    output.addDependency(
      buildConfig.packageRoot.resolve(source),
    );
  }
}