Path: blob/master/sourcetools/com.ibm.jpp.preprocessor/com/ibm/jpp/commandline/CommandlineBuilder.java
6004 views
/*******************************************************************************1* Copyright (c) 1999, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package com.ibm.jpp.commandline;2223import java.io.File;24import java.io.FileNotFoundException;25import java.util.HashMap;26import java.util.Map;27import java.util.Set;28import java.util.TreeSet;2930import com.ibm.jpp.om.ConfigObject;31import com.ibm.jpp.om.ConfigurationRegistry;32import com.ibm.jpp.om.Logger;33import com.ibm.jpp.om.MetaRegistry;34import com.ibm.jpp.xml.XMLException;3536/**37* J9 JCL Preprocessor Command Line Builder38*/39public class CommandlineBuilder {4041/**42* Interface to the JPP command line builder43*44* @param args the command line arguments45*/46public static void main(String[] args) {47String xml = null;48String config = null;49String baseDir = null;50String srcRoot = null;51String destDir = null;52boolean incremental = false;53/* [PR 117967] idea 491: Automatically create the jars required for test bootpath */54boolean testsBootpathProject = false;55/* [PR 121491] Use correct build method and correct commandline input values for preprocessing jobs */56boolean testsProject = false;57/* [PR 121584]Add option to jcl-builder to use testsBootPath JavaDoc */58boolean useTestBootpathJavaDoc = false;5960Map<String, String> options = new HashMap<>();61ConfigurationRegistry registry;6263if (args.length == 0 || args[0].equalsIgnoreCase("-h") || args[0].equalsIgnoreCase("--help")) {64Logger logger = new CommandlineLogger();65logger.log(getArgInfo(), 1);66System.exit(1);67}6869for (int i = 0; i < args.length; ++i) {70String arg = args[i];71String nextArg = i < args.length - 1 ? args[i + 1] : null;7273if (isSupportedArg(arg)) {74if (arg.equalsIgnoreCase("-config")) {75if (nextArg == null) {76missingValueFor(arg);77}78config = nextArg;79i += 1;80} else if (arg.equalsIgnoreCase("-baseDir")) {81if (nextArg == null) {82missingValueFor(arg);83}84baseDir = nextArg;85i += 1;86} else if (arg.equalsIgnoreCase("-srcRoot")) {87if (nextArg == null) {88missingValueFor(arg);89}90srcRoot = nextArg;91i += 1;92} else if (arg.equalsIgnoreCase("-dest")) {93if (nextArg == null) {94missingValueFor(arg);95}96destDir = nextArg;97i += 1;98} else if (arg.equalsIgnoreCase("-xml")) {99if (nextArg == null) {100missingValueFor(arg);101}102xml = nextArg;103i += 1;104} else if (arg.equalsIgnoreCase("-incremental")) {105incremental = true;106/* [PR 117967] idea 491: Automatically create the jars required for test bootpath */107} else if (arg.equalsIgnoreCase("-isTestsBoot")) {108testsBootpathProject = true;109/* [PR 121491] Use correct build method and correct commandline input values for preprocessing jobs */110} else if (arg.equalsIgnoreCase("-isTests")) {111testsProject = true;112/* [PR 121584]Add option to jcl-builder to use testsBootPath JavaDoc */113} else if (arg.equalsIgnoreCase("-useTestBootpathJavaDoc")) {114useTestBootpathJavaDoc = true;115} else {116String name = arg.substring(1).toLowerCase();117String value = "true";118119if (nextArg != null && nextArg.charAt(0) != '-') {120value = nextArg;121i += 1;122}123124options.put(name, value);125}126} else {127System.err.println("Unrecognized option: " + arg + "\nCould not preprocess configuration");128System.exit(1);129}130}131132try {133registry = MetaRegistry.getRegistry(baseDir, srcRoot, xml);134ConfigObject configuration = registry.getConfiguration(config);135136if (configuration != null) {137// Overwrites the options defined in the XML with those from the command line138for (Map.Entry<String, String> entry : options.entrySet()) {139configuration.addOption(entry.getKey(), entry.getValue());140}141/* [PR 117967] idea 491: Automatically create the jars required for test bootpath */142// add bootpath required flag to preprocess for bootpathproject143if (testsBootpathProject) {144/* [PR 120411] Use a javadoc tag instead of TestBootpath preprocessor tag */145/* [PR 121584]Add option to jcl-builder to use testsBootPath JavaDoc */146if (!useTestBootpathJavaDoc) {147configuration.addRequiredIncludeFlag("TestBootpath");148}149configuration.addFlag("TestBootpath");150}151/* [PR 117967] idea 491: Automatically create the jars required for test bootpath */152// Overwrites the destDir defined in the XML with those from the command line153configuration.setOutputPathforJCLBuilder(destDir);154boolean result;155/* [PR 119500] Design 955 Core.JCL : Support bootpath JCL testing */156if (testsBootpathProject) {157// create the output dir even if there is no file to put it in it,158new File(destDir).mkdirs();159result = configuration.buildTestBootpath(incremental, options.containsKey("nowarn"), useTestBootpathJavaDoc);160/* [PR 121491] Use correct build method and correct commandline input values for preprocessing jobs */161} else if (testsProject) {162// create the output dir even if there is no file to put it in it,163new File(destDir).mkdirs();164result = configuration.buildTests(incremental, options.containsKey("nowarn"));165} else {166result = configuration.build(incremental, options.containsKey("nowarn"));167}168if (result) {169System.exit(0);170}171} else {172StringBuilder msg = new StringBuilder("No configuration or non-existant configuration specified (Configurations are case sensitive)");173msg.append("\nPREPROCESS WAS NOT SUCCESSFUL");174System.err.println(msg.toString());175}176} catch (FileNotFoundException e) {177System.err.println("Cannot open configuration file: " + xml);178} catch (NullPointerException e) {179if (destDir == null) {180System.err.println("No destination directory defined.");181} else if (xml == null) {182System.err.println("Could not find configuration files. You must define valid a JPP XML configuration file.");183} else {184e.printStackTrace();185}186} catch (XMLException e) {187System.err.println("\nERROR: " + e.getMessage());188}189System.exit(1);190}191192private static void missingValueFor(String arg) {193System.err.println("Option: " + arg + " requires a value.");194System.exit(1);195}196197private static final Set<String> validArgs;198199static {200validArgs = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);201202validArgs.add("-updateAllCopyrights");203validArgs.add("-xml");204validArgs.add("-config");205validArgs.add("-baseDir");206validArgs.add("-srcRoot");207validArgs.add("-dest");208validArgs.add("-verdict");209validArgs.add("-includeifunsure");210validArgs.add("-nowarn");211validArgs.add("-nowarnincludeif");212validArgs.add("-nowarninvalidflags");213validArgs.add("-incremental");214validArgs.add("-force");215validArgs.add("-macro:define");216validArgs.add("-tag:define");217validArgs.add("-isTestsBoot");218/* [PR 121491] Use correct build method and correct commandline input values for preprocessing jobs */219validArgs.add("-isTests");220/* [PR 120038] -tag:remove preprocessor option is rejected */221validArgs.add("-tag:remove");222/* [PR 121584]Add option to jcl-builder to use testsBootPath JavaDoc */223validArgs.add("-useTestBootpathJavaDoc");224}225226/**227* Identifies all of the arguments supported by the command line builder.228*229* @param arg the argument to be tested230* @return <code>true</code> if the argument is supported, <code>false</code> otherwise231*/232private static boolean isSupportedArg(String arg) {233return validArgs.contains(arg);234}235236private static String getArgInfo() {237StringBuilder temp = new StringBuilder();238temp.append("J9 JCL Preprocessor, Version 4.2.0");239temp.append("\n\nUsage: CommandLineBuilder -config <config name> -xml <config file> -dest <destination> [options]");240temp.append("\n\n[options] ");241temp.append("\n -baseDir <base dir> Prepends base dir to all paths in xml");242temp.append("\n -srcRoot <source dir> Appends source dir to base dir for use with local src/output paths");243temp.append("\n -verdict Print information on preprocessor success or failure");244temp.append("\n -xmlVerbose Print information on loaded configurations");245temp.append("\n -includeIfUnsure Include in preprocessor if not sure");246temp.append("\n -noWarn Do not print any preprocessor warnings");247temp.append("\n -force Unconditionally update output files");248temp.append("\n -incremental Do only an incremental preprocess");249temp.append("\n -macro:define Define a preprocessor macro");250temp.append("\n -tag:define Define a preprocessor tag");251temp.append("\n -isTestsBoot Preprocess for TestBootPath project with required tag TestBootPath");252temp.append("\n -updateAllCopyrights Update copyright on all files to current year instead of the files last modified year");253temp.append("\n");254return temp.toString();255}256}257258259