forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_experimental_flags.dart
125 lines (101 loc) · 3.88 KB
/
generate_experimental_flags.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io' show File, Platform;
import 'package:yaml/yaml.dart' show YamlMap, loadYaml;
void main() {
YamlMap yaml = loadYaml(File.fromUri(computeYamlFile()).readAsStringSync());
final currentVersion = getAsVersionNumber(yaml['current-version'])!;
final enumNames = StringBuffer();
final featureValues = StringBuffer();
final featureNames = StringBuffer();
YamlMap features = yaml['features'];
for (var entry in features.entries) {
final category = (entry.value as YamlMap)['category'];
if (category == null || category == "vm" || category == "language") {
final version = getAsVersionNumber((entry.value as YamlMap)['enabledIn']);
if (version != null) {
final value = isGreaterOrEqualVersion(currentVersion, version);
final name = entry.key.replaceAll('-', '_');
enumNames.write(' $name,\n');
featureValues.write(' $value,\n');
featureNames.write(' "${entry.key}",\n');
}
}
}
final h = '''
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// NOTE: THIS FILE IS GENERATED. DO NOT EDIT.
//
// Instead modify 'tools/experimental_features.yaml' and run
// 'dart tools/generate_experimental_flags.dart' to update.
//
// Current version: ${currentVersion.join('.')}
#ifndef RUNTIME_VM_EXPERIMENTAL_FEATURES_H_
#define RUNTIME_VM_EXPERIMENTAL_FEATURES_H_
namespace dart {
enum class ExperimentalFeature {
$enumNames};
bool GetExperimentalFeatureDefault(ExperimentalFeature feature);
const char* GetExperimentalFeatureName(ExperimentalFeature feature);
} // namespace dart
#endif // RUNTIME_VM_EXPERIMENTAL_FEATURES_H_
''';
final cc = '''
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// NOTE: THIS FILE IS GENERATED. DO NOT EDIT.
//
// Instead modify 'tools/experimental_features.yaml' and run
// 'dart tools/generate_experimental_flags.dart' to update.
//
// Current version: ${currentVersion.join('.')}
#include "vm/experimental_features.h"
#include <cstring>
#include "platform/assert.h"
#include "vm/globals.h"
namespace dart {
bool GetExperimentalFeatureDefault(ExperimentalFeature feature) {
constexpr bool kFeatureValues[] = {
$featureValues };
ASSERT(static_cast<size_t>(feature) < ARRAY_SIZE(kFeatureValues));
return kFeatureValues[static_cast<int>(feature)];
}
const char* GetExperimentalFeatureName(ExperimentalFeature feature) {
constexpr const char* kFeatureNames[] = {
$featureNames };
ASSERT(static_cast<size_t>(feature) < ARRAY_SIZE(kFeatureNames));
return kFeatureNames[static_cast<int>(feature)];
}
} // namespace dart
''';
File.fromUri(computeHFile()).writeAsStringSync(h);
File.fromUri(computeCcFile()).writeAsStringSync(cc);
}
Uri computeYamlFile() {
return Platform.script.resolve("experimental_features.yaml");
}
Uri computeCcFile() {
return Platform.script.resolve("../runtime/vm/experimental_features.cc");
}
Uri computeHFile() {
return Platform.script.resolve("../runtime/vm/experimental_features.h");
}
List<num>? getAsVersionNumber(dynamic value) {
if (value == null) return null;
final version = List.of("$value".split(".").map(int.parse));
while (version.length < 3) {
version.add(0);
}
return version;
}
bool isGreaterOrEqualVersion(List<num> left, List<num> right) {
assert(left.length == right.length);
for (var i = 0; i < left.length; ++i) {
if (left[i] != right[i]) return left[i] > right[i];
}
return true;
}