Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/com.ibm.jpp.preprocessor/com/ibm/jpp/commandline/CommandlineBuilder.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 1999, 2021 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.jpp.commandline;
23
24
import java.io.File;
25
import java.io.FileNotFoundException;
26
import java.util.HashMap;
27
import java.util.Map;
28
import java.util.Set;
29
import java.util.TreeSet;
30
31
import com.ibm.jpp.om.ConfigObject;
32
import com.ibm.jpp.om.ConfigurationRegistry;
33
import com.ibm.jpp.om.Logger;
34
import com.ibm.jpp.om.MetaRegistry;
35
import com.ibm.jpp.xml.XMLException;
36
37
/**
38
* J9 JCL Preprocessor Command Line Builder
39
*/
40
public class CommandlineBuilder {
41
42
/**
43
* Interface to the JPP command line builder
44
*
45
* @param args the command line arguments
46
*/
47
public static void main(String[] args) {
48
String xml = null;
49
String config = null;
50
String baseDir = null;
51
String srcRoot = null;
52
String destDir = null;
53
boolean incremental = false;
54
/* [PR 117967] idea 491: Automatically create the jars required for test bootpath */
55
boolean testsBootpathProject = false;
56
/* [PR 121491] Use correct build method and correct commandline input values for preprocessing jobs */
57
boolean testsProject = false;
58
/* [PR 121584]Add option to jcl-builder to use testsBootPath JavaDoc */
59
boolean useTestBootpathJavaDoc = false;
60
61
Map<String, String> options = new HashMap<>();
62
ConfigurationRegistry registry;
63
64
if (args.length == 0 || args[0].equalsIgnoreCase("-h") || args[0].equalsIgnoreCase("--help")) {
65
Logger logger = new CommandlineLogger();
66
logger.log(getArgInfo(), 1);
67
System.exit(1);
68
}
69
70
for (int i = 0; i < args.length; ++i) {
71
String arg = args[i];
72
String nextArg = i < args.length - 1 ? args[i + 1] : null;
73
74
if (isSupportedArg(arg)) {
75
if (arg.equalsIgnoreCase("-config")) {
76
if (nextArg == null) {
77
missingValueFor(arg);
78
}
79
config = nextArg;
80
i += 1;
81
} else if (arg.equalsIgnoreCase("-baseDir")) {
82
if (nextArg == null) {
83
missingValueFor(arg);
84
}
85
baseDir = nextArg;
86
i += 1;
87
} else if (arg.equalsIgnoreCase("-srcRoot")) {
88
if (nextArg == null) {
89
missingValueFor(arg);
90
}
91
srcRoot = nextArg;
92
i += 1;
93
} else if (arg.equalsIgnoreCase("-dest")) {
94
if (nextArg == null) {
95
missingValueFor(arg);
96
}
97
destDir = nextArg;
98
i += 1;
99
} else if (arg.equalsIgnoreCase("-xml")) {
100
if (nextArg == null) {
101
missingValueFor(arg);
102
}
103
xml = nextArg;
104
i += 1;
105
} else if (arg.equalsIgnoreCase("-incremental")) {
106
incremental = true;
107
/* [PR 117967] idea 491: Automatically create the jars required for test bootpath */
108
} else if (arg.equalsIgnoreCase("-isTestsBoot")) {
109
testsBootpathProject = true;
110
/* [PR 121491] Use correct build method and correct commandline input values for preprocessing jobs */
111
} else if (arg.equalsIgnoreCase("-isTests")) {
112
testsProject = true;
113
/* [PR 121584]Add option to jcl-builder to use testsBootPath JavaDoc */
114
} else if (arg.equalsIgnoreCase("-useTestBootpathJavaDoc")) {
115
useTestBootpathJavaDoc = true;
116
} else {
117
String name = arg.substring(1).toLowerCase();
118
String value = "true";
119
120
if (nextArg != null && nextArg.charAt(0) != '-') {
121
value = nextArg;
122
i += 1;
123
}
124
125
options.put(name, value);
126
}
127
} else {
128
System.err.println("Unrecognized option: " + arg + "\nCould not preprocess configuration");
129
System.exit(1);
130
}
131
}
132
133
try {
134
registry = MetaRegistry.getRegistry(baseDir, srcRoot, xml);
135
ConfigObject configuration = registry.getConfiguration(config);
136
137
if (configuration != null) {
138
// Overwrites the options defined in the XML with those from the command line
139
for (Map.Entry<String, String> entry : options.entrySet()) {
140
configuration.addOption(entry.getKey(), entry.getValue());
141
}
142
/* [PR 117967] idea 491: Automatically create the jars required for test bootpath */
143
// add bootpath required flag to preprocess for bootpathproject
144
if (testsBootpathProject) {
145
/* [PR 120411] Use a javadoc tag instead of TestBootpath preprocessor tag */
146
/* [PR 121584]Add option to jcl-builder to use testsBootPath JavaDoc */
147
if (!useTestBootpathJavaDoc) {
148
configuration.addRequiredIncludeFlag("TestBootpath");
149
}
150
configuration.addFlag("TestBootpath");
151
}
152
/* [PR 117967] idea 491: Automatically create the jars required for test bootpath */
153
// Overwrites the destDir defined in the XML with those from the command line
154
configuration.setOutputPathforJCLBuilder(destDir);
155
boolean result;
156
/* [PR 119500] Design 955 Core.JCL : Support bootpath JCL testing */
157
if (testsBootpathProject) {
158
// create the output dir even if there is no file to put it in it,
159
new File(destDir).mkdirs();
160
result = configuration.buildTestBootpath(incremental, options.containsKey("nowarn"), useTestBootpathJavaDoc);
161
/* [PR 121491] Use correct build method and correct commandline input values for preprocessing jobs */
162
} else if (testsProject) {
163
// create the output dir even if there is no file to put it in it,
164
new File(destDir).mkdirs();
165
result = configuration.buildTests(incremental, options.containsKey("nowarn"));
166
} else {
167
result = configuration.build(incremental, options.containsKey("nowarn"));
168
}
169
if (result) {
170
System.exit(0);
171
}
172
} else {
173
StringBuilder msg = new StringBuilder("No configuration or non-existant configuration specified (Configurations are case sensitive)");
174
msg.append("\nPREPROCESS WAS NOT SUCCESSFUL");
175
System.err.println(msg.toString());
176
}
177
} catch (FileNotFoundException e) {
178
System.err.println("Cannot open configuration file: " + xml);
179
} catch (NullPointerException e) {
180
if (destDir == null) {
181
System.err.println("No destination directory defined.");
182
} else if (xml == null) {
183
System.err.println("Could not find configuration files. You must define valid a JPP XML configuration file.");
184
} else {
185
e.printStackTrace();
186
}
187
} catch (XMLException e) {
188
System.err.println("\nERROR: " + e.getMessage());
189
}
190
System.exit(1);
191
}
192
193
private static void missingValueFor(String arg) {
194
System.err.println("Option: " + arg + " requires a value.");
195
System.exit(1);
196
}
197
198
private static final Set<String> validArgs;
199
200
static {
201
validArgs = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
202
203
validArgs.add("-updateAllCopyrights");
204
validArgs.add("-xml");
205
validArgs.add("-config");
206
validArgs.add("-baseDir");
207
validArgs.add("-srcRoot");
208
validArgs.add("-dest");
209
validArgs.add("-verdict");
210
validArgs.add("-includeifunsure");
211
validArgs.add("-nowarn");
212
validArgs.add("-nowarnincludeif");
213
validArgs.add("-nowarninvalidflags");
214
validArgs.add("-incremental");
215
validArgs.add("-force");
216
validArgs.add("-macro:define");
217
validArgs.add("-tag:define");
218
validArgs.add("-isTestsBoot");
219
/* [PR 121491] Use correct build method and correct commandline input values for preprocessing jobs */
220
validArgs.add("-isTests");
221
/* [PR 120038] -tag:remove preprocessor option is rejected */
222
validArgs.add("-tag:remove");
223
/* [PR 121584]Add option to jcl-builder to use testsBootPath JavaDoc */
224
validArgs.add("-useTestBootpathJavaDoc");
225
}
226
227
/**
228
* Identifies all of the arguments supported by the command line builder.
229
*
230
* @param arg the argument to be tested
231
* @return <code>true</code> if the argument is supported, <code>false</code> otherwise
232
*/
233
private static boolean isSupportedArg(String arg) {
234
return validArgs.contains(arg);
235
}
236
237
private static String getArgInfo() {
238
StringBuilder temp = new StringBuilder();
239
temp.append("J9 JCL Preprocessor, Version 4.2.0");
240
temp.append("\n\nUsage: CommandLineBuilder -config <config name> -xml <config file> -dest <destination> [options]");
241
temp.append("\n\n[options] ");
242
temp.append("\n -baseDir <base dir> Prepends base dir to all paths in xml");
243
temp.append("\n -srcRoot <source dir> Appends source dir to base dir for use with local src/output paths");
244
temp.append("\n -verdict Print information on preprocessor success or failure");
245
temp.append("\n -xmlVerbose Print information on loaded configurations");
246
temp.append("\n -includeIfUnsure Include in preprocessor if not sure");
247
temp.append("\n -noWarn Do not print any preprocessor warnings");
248
temp.append("\n -force Unconditionally update output files");
249
temp.append("\n -incremental Do only an incremental preprocess");
250
temp.append("\n -macro:define Define a preprocessor macro");
251
temp.append("\n -tag:define Define a preprocessor tag");
252
temp.append("\n -isTestsBoot Preprocess for TestBootPath project with required tag TestBootPath");
253
temp.append("\n -updateAllCopyrights Update copyright on all files to current year instead of the files last modified year");
254
temp.append("\n");
255
return temp.toString();
256
}
257
}
258
259