OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /// This library generates Mojo bindings for a Dart package. | 5 /// This library generates Mojo bindings for a Dart package. |
6 | 6 |
7 library generate; | 7 library generate; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 import 'dart:collection'; | 10 import 'dart:collection'; |
11 import 'dart:convert'; | 11 import 'dart:convert'; |
12 import 'dart:developer' as dev; | 12 import 'dart:developer' as dev; |
13 import 'dart:io'; | 13 import 'dart:io'; |
14 | 14 |
15 import 'package:mojom/src/utils.dart'; | 15 import 'package:mojom/src/utils.dart'; |
16 import 'package:path/path.dart' as path; | 16 import 'package:path/path.dart' as path; |
17 import 'package:yaml/yaml.dart' as yaml; | 17 import 'package:yaml/yaml.dart' as yaml; |
18 | 18 |
19 part 'mojom_finder.dart'; | 19 part 'mojom_finder.dart'; |
20 | 20 |
21 const String mojoTestPackage = '_mojo_for_test_only'; | 21 const String mojoTestPackage = '_mojo_for_test_only'; |
22 | 22 |
| 23 // A class for running as many generation tasks in parallel as there are cores |
| 24 // on the machine. Supposing `tasks` is a list of functions of type |
| 25 // `Future f()`, used as: |
| 26 // var runner = new _GenerationTaskRunner(tasks); |
| 27 // await runner.run(); |
| 28 // No result is returned. |
| 29 class _GenerationTaskRunner { |
| 30 List<Function> _tasks; |
| 31 List<Future> _futures; |
| 32 Completer _completer; |
| 33 int _numCpus; |
| 34 int _nextTaskIndex; |
| 35 int _futuresOutstanding; |
| 36 |
| 37 _GenerationTaskRunner(this._tasks) { |
| 38 _numCpus = Platform.numberOfProcessors; |
| 39 _futures = new List<Future>(); |
| 40 _completer = new Completer(); |
| 41 _nextTaskIndex = 0; |
| 42 _futuresOutstanding = 0; |
| 43 } |
| 44 |
| 45 Future run() { |
| 46 while ((_nextTaskIndex < _numCpus) && (_nextTaskIndex < _tasks.length)) { |
| 47 int idx = _nextTaskIndex; |
| 48 _futures.add(_tasks[_nextTaskIndex]().then((_) { |
| 49 _addNewTask(idx); |
| 50 })); |
| 51 _nextTaskIndex++; |
| 52 _futuresOutstanding++; |
| 53 } |
| 54 return _completer.future; |
| 55 } |
| 56 |
| 57 void _addNewTask(int idx) { |
| 58 if (_nextTaskIndex < _tasks.length) { |
| 59 _futures[idx] = _tasks[_nextTaskIndex]().then((_) { |
| 60 _addNewTask(idx); |
| 61 }); |
| 62 _nextTaskIndex++; |
| 63 } else { |
| 64 _futuresOutstanding--; |
| 65 if (_futuresOutstanding == 0) { |
| 66 _completer.complete(null); |
| 67 } |
| 68 } |
| 69 } |
| 70 } |
| 71 |
23 class MojomGenerator { | 72 class MojomGenerator { |
24 static dev.Counter _genMs; | 73 static dev.Counter _genMs; |
25 final bool _errorOnDuplicate; | 74 final bool _errorOnDuplicate; |
26 final bool _dryRun; | 75 final bool _dryRun; |
27 final bool _force; | 76 final bool _force; |
28 final Directory _mojoSdk; | 77 final Directory _mojoSdk; |
29 | 78 |
30 Map<String, String> _duplicateDetection; | 79 Map<String, String> _duplicateDetection; |
31 int _generationMs; | 80 int _generationMs; |
32 | 81 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 int mojomDartCount = 0; | 113 int mojomDartCount = 0; |
65 if (mojomDartInfo != null) { | 114 if (mojomDartInfo != null) { |
66 oldestMojomDartTime = mojomDartInfo[0]; | 115 oldestMojomDartTime = mojomDartInfo[0]; |
67 mojomDartCount = mojomDartInfo[1]; | 116 mojomDartCount = mojomDartInfo[1]; |
68 } | 117 } |
69 | 118 |
70 // If we don't have enough .mojom.dart files, or if a .mojom file is | 119 // If we don't have enough .mojom.dart files, or if a .mojom file is |
71 // newer than the oldest .mojom.dart file, then regenerate. | 120 // newer than the oldest .mojom.dart file, then regenerate. |
72 if (_force || (mojomDartCount < mojomCount) || | 121 if (_force || (mojomDartCount < mojomCount) || |
73 _shouldRegenerate(newestMojomTime, oldestMojomDartTime)) { | 122 _shouldRegenerate(newestMojomTime, oldestMojomDartTime)) { |
| 123 var tasks = new List(); |
74 for (File mojom in info.mojomFiles) { | 124 for (File mojom in info.mojomFiles) { |
75 await _generateForMojom( | 125 tasks.add(() => _generateForMojom( |
76 mojom, info.importDir, info.packageDir, info.name); | 126 mojom, info.importDir, info.packageDir, info.name)); |
77 } | 127 } |
| 128 var runner = new _GenerationTaskRunner(tasks); |
| 129 await runner.run(); |
| 130 |
78 // Delete any .mojom.dart files that are still older than mojomTime. | 131 // Delete any .mojom.dart files that are still older than mojomTime. |
79 await _deleteOldMojomDart(info.packageDir, newestMojomTime); | 132 await _deleteOldMojomDart(info.packageDir, newestMojomTime); |
80 } | 133 } |
81 } | 134 } |
82 | 135 |
83 /// Under [package]/lib, returns the oldest modification time for a | 136 /// Under [package]/lib, returns the oldest modification time for a |
84 /// .mojom.dart file. | 137 /// .mojom.dart file. |
85 _findOldestMojomDart(Directory package) async { | 138 _findOldestMojomDart(Directory package) async { |
86 int mojomDartCount = 0; | 139 int mojomDartCount = 0; |
87 DateTime oldestModTime; | 140 DateTime oldestModTime; |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 '\t$dart $packRoot $scriptPath single -m $mojoSdk -r $root ' | 501 '\t$dart $packRoot $scriptPath single -m $mojoSdk -r $root ' |
449 '-p $packagePath $skips'); | 502 '-p $packagePath $skips'); |
450 _errors++; | 503 _errors++; |
451 return; | 504 return; |
452 } | 505 } |
453 } | 506 } |
454 } | 507 } |
455 | 508 |
456 bool _shouldSkip(File f) => containsPrefix(f.path, _skip); | 509 bool _shouldSkip(File f) => containsPrefix(f.path, _skip); |
457 } | 510 } |
OLD | NEW |