Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# native_toolchain_cmake

## 0.2.4

- new: add `parallelJobs` and `parallelUseAllProcessors` to support parallel build or set njobs explicitly.

## 0.2.3

- new: support skipping generate if cached
Expand Down
19 changes: 19 additions & 0 deletions lib/src/builder/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ class CMakeBuilder implements Builder {

final bool useVcvars;

/// Number of parallel jobs to use for CMake build, i.e., --parallel [<jobs>].
///
/// This is added since CMake 3.12
///
/// https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-build-j
///
/// If [parallelUseAllProcessors] is true, [parallelJobs] will be set to
/// [Platform.numberOfProcessors].
final int? parallelJobs;

/// Whether to use all processors for parallel jobs.
final bool parallelUseAllProcessors;

/// This constructor initializes a new build config from [sourceDir].
///
/// Parameters:
Expand Down Expand Up @@ -110,6 +123,8 @@ class CMakeBuilder implements Builder {
this.androidArgs = const AndroidBuilderArgs(),
this.appleArgs = const AppleBuilderArgs(),
this.useVcvars = true,
this.parallelJobs,
this.parallelUseAllProcessors = false,
}) : cmakeListsDir = sourceDir;

/// This constructor initializes a new build config by cloning the
Expand Down Expand Up @@ -160,6 +175,8 @@ class CMakeBuilder implements Builder {
this.androidArgs = const AndroidBuilderArgs(),
this.appleArgs = const AppleBuilderArgs(),
this.useVcvars = true,
this.parallelJobs,
this.parallelUseAllProcessors = false,
}) : cmakeListsDir = sourceDir {
// Some platforms will error if directory does not exist, create it.
cmakeListsDir = sourceDir.resolve('external/$name/').normalizePath();
Expand Down Expand Up @@ -308,6 +325,8 @@ class CMakeBuilder implements Builder {
androidArgs: androidArgs,
logLevel: logLevel,
userConfig: userConfig,
parallelJobs: parallelJobs,
parallelUseAllProcessors: parallelUseAllProcessors,
);

// Do not remove this line for potential extra variables in the future
Expand Down
30 changes: 28 additions & 2 deletions lib/src/builder/run_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:change_case/change_case.dart';
import 'package:code_assets/code_assets.dart';
import 'package:hooks/hooks.dart';
import 'package:logging/logging.dart';
import 'package:pub_semver/pub_semver.dart';

import '../native_toolchain/android_ndk.dart';
import '../native_toolchain/cmake.dart';
Expand Down Expand Up @@ -64,6 +65,10 @@ class RunCMakeBuilder {
/// save the last generate status of native_toolchain_cmake
final String lastGenStatusFile;

/// number of parallel jobs to use
final int? parallelJobs;
final bool parallelUseAllProcessors;

RunCMakeBuilder({
required this.input,
required this.codeConfig,
Expand All @@ -78,10 +83,13 @@ class RunCMakeBuilder {
this.appleArgs = const AppleBuilderArgs(),
this.logLevel = LogLevel.STATUS,
this.lastGenStatusFile = 'ntc_last_generate_status.txt',
this.parallelUseAllProcessors = false,
int? parallelJobs,
Uri? outputDir,
UserConfig? userConfig,
}) : outDir = outputDir ?? input.outputDirectory,
userConfig = userConfig ?? UserConfig(targetOS: codeConfig.targetOS);
userConfig = userConfig ?? UserConfig(targetOS: codeConfig.targetOS),
parallelJobs = parallelJobs ?? (parallelUseAllProcessors ? Platform.numberOfProcessors : null);

Future<Uri> cmakePath({Map<String, String>? environment}) async {
final cmakeTools = await cmake.defaultResolver?.resolve(
Expand Down Expand Up @@ -236,15 +244,33 @@ class RunCMakeBuilder {
}

Future<RunProcessResult> _build({Map<String, String>? environment}) async {
final cmakeTools = await cmake.defaultResolver?.resolve(
logger: logger,
userConfig: userConfig,
environment: environment,
);
final _cmake = cmakeTools?.first;
if (_cmake == null) {
throw Exception('Failed to resolve CMake path.');
}

final _parallelJobs = <String>[];
// https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-build-j
// --parallel added in CMake 3.12.0
if (parallelJobs != null && _cmake.version != null && _cmake.version! > Version.parse('3.12.0')) {
_parallelJobs.addAll(['--parallel', parallelJobs.toString()]);
}

return runProcess(
executable: await cmakePath(environment: environment),
executable: _cmake.uri,
arguments: [
'--build',
outDir.normalizePath().toFilePath(),
'--config',
buildMode.name.toCapitalCase(),
if (targets?.isNotEmpty ?? false) '--target',
if (targets?.isNotEmpty ?? false) ...targets!,
..._parallelJobs,
],
logger: logger,
workingDirectory: outDir,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: native_toolchain_cmake
description: >-
A library to invoke and build CMake projects for Dart Native Assets.
version: 0.2.3
version: 0.2.4
repository: https://github.com/rainyl/native_toolchain_cmake

topics:
Expand Down
Loading