Skip to content

Commit 147cf31

Browse files
authored
feat(builder): add parallel build support for CMake (#29)
* feat(builder): add parallel build support for CMake Introduce `parallelJobs` and `parallelUseAllProcessors` fields to CMakeBuilder and RunCMakeBuilder to enable parallel compilation using CMake's `--parallel` flag. The feature is automatically enabled only when the detected CMake version is greater than 3.12.0, which introduced the `--parallel` option. * chore: bump version to 0.2.4 and update changelog.
1 parent 74e2d8f commit 147cf31

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# native_toolchain_cmake
22

3+
## 0.2.4
4+
5+
- new: add `parallelJobs` and `parallelUseAllProcessors` to support parallel build or set njobs explicitly.
6+
37
## 0.2.3
48

59
- new: support skipping generate if cached

lib/src/builder/builder.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ class CMakeBuilder implements Builder {
7474

7575
final bool useVcvars;
7676

77+
/// Number of parallel jobs to use for CMake build, i.e., --parallel [<jobs>].
78+
///
79+
/// This is added since CMake 3.12
80+
///
81+
/// https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-build-j
82+
///
83+
/// If [parallelUseAllProcessors] is true, [parallelJobs] will be set to
84+
/// [Platform.numberOfProcessors].
85+
final int? parallelJobs;
86+
87+
/// Whether to use all processors for parallel jobs.
88+
final bool parallelUseAllProcessors;
89+
7790
/// This constructor initializes a new build config from [sourceDir].
7891
///
7992
/// Parameters:
@@ -110,6 +123,8 @@ class CMakeBuilder implements Builder {
110123
this.androidArgs = const AndroidBuilderArgs(),
111124
this.appleArgs = const AppleBuilderArgs(),
112125
this.useVcvars = true,
126+
this.parallelJobs,
127+
this.parallelUseAllProcessors = false,
113128
}) : cmakeListsDir = sourceDir;
114129

115130
/// This constructor initializes a new build config by cloning the
@@ -160,6 +175,8 @@ class CMakeBuilder implements Builder {
160175
this.androidArgs = const AndroidBuilderArgs(),
161176
this.appleArgs = const AppleBuilderArgs(),
162177
this.useVcvars = true,
178+
this.parallelJobs,
179+
this.parallelUseAllProcessors = false,
163180
}) : cmakeListsDir = sourceDir {
164181
// Some platforms will error if directory does not exist, create it.
165182
cmakeListsDir = sourceDir.resolve('external/$name/').normalizePath();
@@ -308,6 +325,8 @@ class CMakeBuilder implements Builder {
308325
androidArgs: androidArgs,
309326
logLevel: logLevel,
310327
userConfig: userConfig,
328+
parallelJobs: parallelJobs,
329+
parallelUseAllProcessors: parallelUseAllProcessors,
311330
);
312331

313332
// Do not remove this line for potential extra variables in the future

lib/src/builder/run_builder.dart

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:change_case/change_case.dart';
1313
import 'package:code_assets/code_assets.dart';
1414
import 'package:hooks/hooks.dart';
1515
import 'package:logging/logging.dart';
16+
import 'package:pub_semver/pub_semver.dart';
1617

1718
import '../native_toolchain/android_ndk.dart';
1819
import '../native_toolchain/cmake.dart';
@@ -64,6 +65,10 @@ class RunCMakeBuilder {
6465
/// save the last generate status of native_toolchain_cmake
6566
final String lastGenStatusFile;
6667

68+
/// number of parallel jobs to use
69+
final int? parallelJobs;
70+
final bool parallelUseAllProcessors;
71+
6772
RunCMakeBuilder({
6873
required this.input,
6974
required this.codeConfig,
@@ -78,10 +83,13 @@ class RunCMakeBuilder {
7883
this.appleArgs = const AppleBuilderArgs(),
7984
this.logLevel = LogLevel.STATUS,
8085
this.lastGenStatusFile = 'ntc_last_generate_status.txt',
86+
this.parallelUseAllProcessors = false,
87+
int? parallelJobs,
8188
Uri? outputDir,
8289
UserConfig? userConfig,
8390
}) : outDir = outputDir ?? input.outputDirectory,
84-
userConfig = userConfig ?? UserConfig(targetOS: codeConfig.targetOS);
91+
userConfig = userConfig ?? UserConfig(targetOS: codeConfig.targetOS),
92+
parallelJobs = parallelJobs ?? (parallelUseAllProcessors ? Platform.numberOfProcessors : null);
8593

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

238246
Future<RunProcessResult> _build({Map<String, String>? environment}) async {
247+
final cmakeTools = await cmake.defaultResolver?.resolve(
248+
logger: logger,
249+
userConfig: userConfig,
250+
environment: environment,
251+
);
252+
final _cmake = cmakeTools?.first;
253+
if (_cmake == null) {
254+
throw Exception('Failed to resolve CMake path.');
255+
}
256+
257+
final _parallelJobs = <String>[];
258+
// https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-build-j
259+
// --parallel added in CMake 3.12.0
260+
if (parallelJobs != null && _cmake.version != null && _cmake.version! > Version.parse('3.12.0')) {
261+
_parallelJobs.addAll(['--parallel', parallelJobs.toString()]);
262+
}
263+
239264
return runProcess(
240-
executable: await cmakePath(environment: environment),
265+
executable: _cmake.uri,
241266
arguments: [
242267
'--build',
243268
outDir.normalizePath().toFilePath(),
244269
'--config',
245270
buildMode.name.toCapitalCase(),
246271
if (targets?.isNotEmpty ?? false) '--target',
247272
if (targets?.isNotEmpty ?? false) ...targets!,
273+
..._parallelJobs,
248274
],
249275
logger: logger,
250276
workingDirectory: outDir,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: native_toolchain_cmake
22
description: >-
33
A library to invoke and build CMake projects for Dart Native Assets.
4-
version: 0.2.3
4+
version: 0.2.4
55
repository: https://github.com/rainyl/native_toolchain_cmake
66

77
topics:

0 commit comments

Comments
 (0)