Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/com.ibm.uma/com/ibm/j9/uma/configuration/ConfigurationImpl.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2018 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9.uma.configuration;
23
24
import java.io.BufferedReader;
25
import java.io.File;
26
import java.io.FileInputStream;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29
import java.io.InputStreamReader;
30
import java.util.Calendar;
31
import java.util.GregorianCalendar;
32
import java.util.Hashtable;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Properties;
36
import java.util.Set;
37
import java.util.Vector;
38
import java.util.regex.Matcher;
39
import java.util.regex.Pattern;
40
41
import com.ibm.j9.uma.configuration.freemarker.Features;
42
import com.ibm.j9.uma.configuration.freemarker.Source;
43
import com.ibm.j9.uma.platform.PlatformOSX;
44
import com.ibm.j9.uma.platform.PlatformUnix;
45
import com.ibm.j9.uma.platform.PlatformWindows;
46
import com.ibm.j9.uma.platform.PlatformZOS;
47
import com.ibm.j9tools.om.BuildInfo;
48
import com.ibm.j9tools.om.BuildSpec;
49
import com.ibm.j9tools.om.ConfigDirectory;
50
import com.ibm.j9tools.om.Flag;
51
import com.ibm.j9tools.om.FlagDefinitions;
52
import com.ibm.j9tools.om.InvalidBuildSpecException;
53
import com.ibm.j9tools.om.InvalidConfigurationException;
54
import com.ibm.j9tools.om.OMObjectException;
55
import com.ibm.j9tools.om.ObjectFactory;
56
import com.ibm.j9tools.om.Property;
57
import com.ibm.uma.IConfiguration;
58
import com.ibm.uma.IPlatform;
59
import com.ibm.uma.ISinglePredicateEvaluator;
60
import com.ibm.uma.UMABadPhaseNameException;
61
import com.ibm.uma.UMAException;
62
import com.ibm.uma.om.Artifact;
63
import com.ibm.uma.util.Logger;
64
65
import freemarker.template.SimpleSequence;
66
import freemarker.template.TemplateModel;
67
import freemarker.template.TemplateModelIterator;
68
69
public class ConfigurationImpl implements IConfiguration, ISinglePredicateEvaluator {
70
71
String[] phaseList;
72
String[] excludeArtifacts;
73
74
public int numberOfPhases() {
75
return phaseList.length;
76
}
77
78
public String phaseName(int phase) {
79
if (phase >= numberOfPhases()) {
80
return "base phase identifier [" + phase + "]";
81
}
82
return phaseList[phase];
83
}
84
85
public int phaseFromString(String phase) throws UMABadPhaseNameException {
86
for (int i = 0; i < phaseList.length; i++) {
87
if (phase.equalsIgnoreCase(phaseList[i])) {
88
return i;
89
}
90
}
91
throw new UMABadPhaseNameException("Unknown phase label: " + phase);
92
}
93
94
public String[] phases() {
95
return phaseList;
96
}
97
98
BuildSpec buildSpec;
99
FlagDefinitions flagDefs;
100
IPlatform platform;
101
BuildInfo buildInfo;
102
Hashtable<String, String> macroMap = new Hashtable<String, String>();
103
104
public BuildInfo getBuildInfo() {
105
return buildInfo;
106
}
107
108
public ConfigurationImpl(String configPath, String buildSpecId, String buildId, String buildDate, String buildTag, String jitVersionFile, String excludeArtifacts) throws UMAException {
109
try {
110
ConfigDirectory configDirectory = new ConfigDirectory(configPath);
111
ObjectFactory factory = new ObjectFactory(configDirectory);
112
factory.initialize();
113
buildSpec = factory.getBuildSpec(buildSpecId);
114
if (buildSpec == null) {
115
throw new UMAException("Could not find " + buildSpecId + ".spec file.");
116
}
117
flagDefs = factory.getFlagDefinitions();
118
buildInfo = factory.getBuildInfo();
119
120
macroMap.put("buildid", buildId);
121
macroMap.put("build_date", buildDate);
122
macroMap.put("vm_build_tag", buildTag);
123
macroMap.put("gc_build_tag", buildTag);
124
macroMap.put("jit_build_tag", getJITVersionTag(jitVersionFile, buildTag));
125
126
this.excludeArtifacts = excludeArtifacts.split(",");
127
128
processProperties(configPath);
129
130
/*
131
* define phases from properties
132
*/
133
String phases = macroMap.get("phase_list");
134
if (phases == null) {
135
throw new UMAException("phase_list is not defined in makelib/uma.properties.");
136
}
137
phaseList = phases.split(" ");
138
139
/*
140
* Verify that we have a mapping from JCL name to JCL artifact name
141
*/
142
for (String jcl : buildInfo.getJCLs()) {
143
if (macroMap.get(jcl) == null) {
144
throw new UMAException("Error: JCL [" + jcl + "] does not contain a mapping to it's artifact name in makelib/uma.properties.");
145
}
146
}
147
148
dumpFlags();
149
} catch (OMObjectException e) {
150
throw new UMAException(e);
151
} catch (InvalidConfigurationException e) {
152
throw new UMAException(e);
153
}
154
}
155
156
void processProperties(String path) throws UMAException {
157
try {
158
Properties properties = new Properties();
159
// Path is assumed to be in the shape {root}/buildspecs
160
properties.load(new FileInputStream(path + "/../makelib/uma.properties"));
161
for (Object key : properties.keySet()) {
162
String property = properties.getProperty((String) key);
163
macroMap.put((String) key, property);
164
}
165
} catch (FileNotFoundException e) {
166
// No uma.properties file, no problem.
167
Logger.getLogger().println(Logger.WarningLog, "Warning: uma.properties file not found.");
168
} catch (IOException e) {
169
throw new UMAException(e);
170
}
171
}
172
173
public String getMetadataFilename() {
174
return "module.xml";
175
}
176
177
public String getConfigurationName() {
178
return buildSpec.getId();
179
}
180
181
public boolean isValidJcl(String jcl) {
182
for (String validJcl : buildInfo.getJCLs()) {
183
if (jcl.equals(validJcl)) {
184
return true;
185
}
186
}
187
return false;
188
}
189
190
public String getDefaultJcl() {
191
return buildSpec.getDefaultJCL().getId();
192
}
193
194
public Map<String, Flag> getFlags() {
195
return buildSpec.getFlags();
196
}
197
198
public BuildSpec getBuildSpec() {
199
return buildSpec;
200
}
201
202
private void dumpFlags() {
203
for (String flagId : buildSpec.getFlags().keySet()) {
204
if (isFlagSet(flagId)) {
205
Logger.getLogger().println(Logger.InformationL2Log, "flag: " + flagId + " is on");
206
} else {
207
Logger.getLogger().println(Logger.InformationL2Log, "flag: " + flagId + " is off");
208
}
209
}
210
}
211
212
// TODO note this is a big hack until we can convert module.xmls to use this new version of the flags.
213
String transformFlag(String oldFlag) {
214
if (!oldFlag.startsWith("J9VM_")) {
215
return oldFlag;
216
}
217
218
// hack some special cases in
219
if (oldFlag.equals("J9VM_ENV_BUILD_SHARED_LIB_VM")) {
220
return "env_buildSharedLibVM";
221
}
222
if (oldFlag.equals("J9VM_ENV_BUILD_STATIC_VM")) {
223
return "env_buildStaticVM";
224
}
225
if (oldFlag.equals("J9VM_IVE_JXE_OERELOCATOR")) {
226
return "ive_jxeOERelocator";
227
}
228
229
String newFlag = oldFlag.substring(5); // trim off the J9VM_
230
int pos = newFlag.indexOf("_") + 1;
231
String flag = newFlag.substring(0, pos).toLowerCase();
232
boolean makeUpper = false;
233
while (true) {
234
int last = pos;
235
pos = newFlag.indexOf("_", last);
236
if (pos < 0) {
237
if (last >= newFlag.length()) {
238
break;
239
}
240
pos = newFlag.length();
241
}
242
if (makeUpper) {
243
flag += newFlag.substring(last, ++last).toUpperCase();
244
} else {
245
makeUpper = true;
246
}
247
flag += newFlag.substring(last, pos).toLowerCase();
248
pos += 1;
249
}
250
return flag;
251
}
252
253
public boolean isFlagValid(String flag) {
254
flag = transformFlag(flag);
255
if (flagDefs.getFlagDefinition(flag) == null) {
256
return false;
257
}
258
return true;
259
}
260
261
public boolean isFlagSet(String flagId) {
262
flagId = transformFlag(flagId);
263
if (isFlagValid(flagId)) {
264
Flag flag = buildSpec.getFlags().get(flagId);
265
if (flag != null) {
266
return flag.getState();
267
}
268
}
269
return false;
270
}
271
272
public Set<String> getAllFlags() {
273
return buildSpec.getFlags().keySet();
274
}
275
276
public void defineMacro(String key, String macro) {
277
macroMap.put(key, macro);
278
}
279
280
public String replaceMacro(String macro) {
281
Property property = buildSpec.getProperty(macro);
282
if (property == null) {
283
return macroMap.get(macro);
284
}
285
return property.getValue();
286
}
287
288
public IPlatform getPlatform() throws UMAException {
289
String configurationName = buildSpec.getId();
290
if (platform == null) {
291
if (configurationName.startsWith("win")) {
292
platform = new PlatformWindows(this);
293
} else if (configurationName.startsWith("zos")) {
294
platform = new PlatformZOS(this);
295
} else if (configurationName.startsWith("aix")
296
|| configurationName.startsWith("linux")
297
|| configurationName.startsWith("ose")
298
|| configurationName.startsWith("qnx")) {
299
platform = new PlatformUnix(this);
300
} else if (configurationName.startsWith("osx")) {
301
platform = new PlatformOSX(this);
302
}
303
304
}
305
if (platform == null) {
306
throw new UMAException("No platform for " + configurationName);
307
}
308
return platform;
309
}
310
311
String rawCopyrightNotice;
312
313
public String getRawCopyrightNotice() throws UMAException {
314
if (rawCopyrightNotice == null) {
315
GregorianCalendar calendar = new GregorianCalendar();
316
rawCopyrightNotice = "Copyright (c) 2000, " + calendar.get(Calendar.YEAR) + " IBM Corp. and others";
317
}
318
return rawCopyrightNotice;
319
}
320
321
String makefileCopyrightNotice;
322
323
public String getMakefileCopyrightNotice() throws UMAException {
324
if (makefileCopyrightNotice == null) {
325
String majorVersion = buildInfo.getMajorVersion();
326
String minorVersion = buildInfo.getMinorVersion();
327
if (majorVersion == null) {
328
majorVersion = "0";
329
}
330
if (minorVersion == null) {
331
minorVersion = "0";
332
}
333
makefileCopyrightNotice =
334
"#\n" +
335
"# " + getRawCopyrightNotice() + "\n" +
336
"#\n" +
337
"# This program and the accompanying materials are made available under\n" +
338
"# the terms of the Eclipse Public License 2.0 which accompanies this\n" +
339
"# distribution and is available at https://www.eclipse.org/legal/epl-2.0/\n" +
340
"# or the Apache License, Version 2.0 which accompanies this distribution and\n" +
341
"# is available at https://www.apache.org/licenses/LICENSE-2.0.\n" +
342
"#\n" +
343
"# This Source Code may also be made available under the following\n" +
344
"# Secondary Licenses when the conditions for such availability set\n" +
345
"# forth in the Eclipse Public License, v. 2.0 are satisfied: GNU\n" +
346
"# General Public License, version 2 with the GNU Classpath\n" +
347
"# Exception [1] and GNU General Public License, version 2 with the\n" +
348
"# OpenJDK Assembly Exception [2].\n" +
349
"#\n" +
350
"# [1] https://www.gnu.org/software/classpath/license.html\n" +
351
"# [2] http://openjdk.java.net/legal/assembly-exception.html\n" +
352
"#\n" +
353
"# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception\n" +
354
"\n" +
355
"# File generated in stream: " + majorVersion + "." + minorVersion + "\n" +
356
"#\n" +
357
"# Autogenerated Code\n";
358
}
359
return makefileCopyrightNotice;
360
}
361
362
String cCopyrightNotice;
363
364
public String getcCopyrightNotice() throws UMAException {
365
if (cCopyrightNotice == null) {
366
cCopyrightNotice =
367
"/*\n" +
368
" * " + getRawCopyrightNotice() + "\n" +
369
" * This program and the accompanying materials are made available under\n" +
370
" * the terms of the Eclipse Public License 2.0 which accompanies this\n" +
371
" * distribution and is available at https://www.eclipse.org/legal/epl-2.0/\n" +
372
" * or the Apache License, Version 2.0 which accompanies this distribution and\n" +
373
" * is available at https://www.apache.org/licenses/LICENSE-2.0.\n" +
374
" *\n" +
375
" * This Source Code may also be made available under the following\n" +
376
" * Secondary Licenses when the conditions for such availability set\n" +
377
" * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU\n" +
378
" * General Public License, version 2 with the GNU Classpath\n" +
379
" * Exception [1] and GNU General Public License, version 2 with the\n" +
380
" * OpenJDK Assembly Exception [2].\n" +
381
" *\n" +
382
" * [1] https://www.gnu.org/software/classpath/license.html\n" +
383
" * [2] http://openjdk.java.net/legal/assembly-exception.html\n" +
384
" *\n" +
385
" * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception\n" +
386
" */\n";
387
}
388
return cCopyrightNotice;
389
}
390
391
public boolean evaluateSinglePredicate(String predicate) throws UMAException {
392
if (predicate.startsWith("spec.")) {
393
if (predicate.startsWith("flags.", "spec.".length())) {
394
String flag = predicate.substring("spec.flags.".length());
395
if (!isFlagValid(flag)) {
396
throw new UMAException("Error: Invalid flag used: " + predicate);
397
}
398
return isFlagSet(flag);
399
} else if (predicate.startsWith("java", "spec.".length())) {
400
// spec.javaNN -> JAVA_SPEC_VERSION >= NN
401
String testVersion = predicate.substring("spec.java".length());
402
int testVersionVal;
403
404
try {
405
testVersionVal = Integer.parseInt(testVersion);
406
} catch (NumberFormatException e) {
407
throw new UMAException("Error: Non-numeric spec.java: " + testVersion);
408
}
409
410
String specVersion = macroMap.get("JAVA_SPEC_VERSION");
411
int specVersionVal;
412
413
if (specVersion == null) {
414
throw new UMAException("Error: JAVA_SPEC_VERSION is not defined");
415
}
416
417
try {
418
specVersionVal = Integer.parseInt(specVersion);
419
} catch (NumberFormatException e) {
420
throw new UMAException("Error: Non-numeric JAVA_SPEC_VERSION: " + specVersion);
421
}
422
423
return specVersionVal >= testVersionVal;
424
} else if (predicate.startsWith("jcl.", "spec.".length())) {
425
String defaultJcl = predicate.substring("spec.jcl.".length());
426
if (!isValidJcl(defaultJcl)) {
427
throw new UMAException("Error: Invalid flag used: " + predicate);
428
}
429
430
return defaultJcl.matches(getDefaultJcl());
431
} else {
432
// must be a platform identifier
433
String name = getConfigurationName();
434
String spec = predicate.substring("spec.".length());
435
return spec.equalsIgnoreCase(name) || name.matches(spec);
436
}
437
}
438
439
throw new UMAException("Error: Unknown predicate condition " + predicate);
440
}
441
442
public TemplateModel getDataModelExtension(String prefixTag, String extensionTag) throws UMAException {
443
if (prefixTag.equals(com.ibm.uma.UMA.FREEMARKER_UMA)) {
444
if (extensionTag.equals("buildinfo")) {
445
return new com.ibm.j9.uma.configuration.freemarker.BuildInfo(this);
446
}
447
} else if (prefixTag.startsWith("uma.spec.flags.")) {
448
return new com.ibm.j9.uma.configuration.freemarker.Flag(prefixTag.substring("uma.spec.flags.".length()), extensionTag, this);
449
} else if (prefixTag.equals("uma.spec")) {
450
if (extensionTag.equalsIgnoreCase("owners")) {
451
return new SimpleSequence(buildSpec.getOwnerIds());
452
} else if (extensionTag.equalsIgnoreCase("features")) {
453
return new Features(this);
454
} else if (extensionTag.equalsIgnoreCase("source")) {
455
return new Source(this);
456
}
457
return new com.ibm.j9.uma.configuration.freemarker.SimpleSpec(extensionTag, this);
458
}
459
return null;
460
}
461
462
public TemplateModelIterator getPropertiesIterator() throws UMAException {
463
return new com.ibm.j9.uma.configuration.freemarker.PropertiesIterator(this);
464
}
465
466
protected String getJITVersionTag(String jitVersionFileName, String buildTag) {
467
/* Looking to parse out the version from a line that looks like:
468
* #define TR_LEVEL_NAME "dev_20130104_30951"
469
* */
470
Pattern pattern = Pattern.compile("#define TR_LEVEL_NAME \"(.*)\"");
471
472
String jitVersionString = "jitVersionCouldNotBeParsed";
473
if (jitVersionFileName.equals("")) {
474
System.out.print("jitVersionFile not set. Proceeding as a developer build.\n");
475
jitVersionString = buildTag;
476
} else {
477
try {
478
File jitVersionFile = new File(jitVersionFileName);
479
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(jitVersionFile)));
480
String line = reader.readLine();
481
while (line != null) {
482
Matcher matcher = pattern.matcher(line);
483
if (matcher.matches()) {
484
jitVersionString = matcher.group(1);
485
break;
486
}
487
line = reader.readLine();
488
}
489
reader.close();
490
} catch (FileNotFoundException e) {
491
jitVersionString = "jitVersionFileNotFound";
492
System.out.println("jitVersionFileNotFound: " + jitVersionFileName);
493
System.exit(-1);
494
} catch (IOException e) {
495
jitVersionString = "jitVersionFileReadError";
496
System.out.println("jitVersionFileReadError: " + jitVersionFileName);
497
System.exit(-1);
498
}
499
}
500
return jitVersionString;
501
}
502
503
public List<String> getExcludedArtifacts() {
504
Vector<String> listOfExcludeArtifacts = new Vector<String>();
505
506
for (String excludeArtifact : excludeArtifacts) {
507
listOfExcludeArtifacts.add(excludeArtifact);
508
}
509
return listOfExcludeArtifacts;
510
}
511
512
public String getAdditionalIncludesForArtifact(Artifact artifact) throws UMAException {
513
if ((buildInfo.getMinorVersion().equalsIgnoreCase("4") || buildInfo.getMinorVersion().equalsIgnoreCase("6"))
514
&& buildInfo.getMajorVersion().equalsIgnoreCase("2")) {
515
return "$(UMA_PATH_TO_ROOT)include $(UMA_PATH_TO_ROOT)oti ";
516
}
517
return "";
518
}
519
520
public void verify() throws UMAException {
521
try {
522
buildSpec.verify(flagDefs, buildInfo);
523
} catch (InvalidBuildSpecException e) {
524
throw new UMAException(e);
525
}
526
}
527
528
}
529
530