/** | |
******************************************************************************** Copyright (c) 2017-2020 Robert Bosch GmbH and others. This program and the accompanying materials are made available | |
* under the terms of the Eclipse Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0/ | |
* SPDX-License-Identifier: EPL-2.0 Contributors: Robert Bosch GmbH - initial API and implementation | |
*/ | |
package org.eclipse.app4mc.sca2amalthea.llvm.headless; | |
import org.apache.commons.cli.BasicParser; | |
import org.apache.commons.cli.CommandLine; | |
import org.apache.commons.cli.CommandLineParser; | |
import org.apache.commons.cli.HelpFormatter; | |
import org.apache.commons.cli.Option; | |
import org.apache.commons.cli.Options; | |
import org.apache.commons.cli.ParseException; | |
import org.eclipse.app4mc.sca.logging.manager.Logmanager; | |
import org.eclipse.app4mc.sca2amalthea.llvm.Activator; | |
import org.eclipse.app4mc.sca2amalthea.llvm.util.SCA2AmaltheaHeadlessConstants; | |
/** | |
* Command line parser class. Creates a wrapper out of the arguments passed in the headless command line prompt. | |
*/ | |
public class CmdLineArgumentParser { | |
/** | |
* Method that parses the given set of command line arguments. The arguments are then used to create a wrapper | |
* property | |
* | |
* @param args {@link String} | |
* @return {@link SCA2AMALTHEAStarterProperties} | |
*/ | |
public SCA2AMALTHEAStarterProperties parseCommandLineParameters(final String[] args) { | |
CommandLineParser parser = new BasicParser(); | |
CommandLine cmd = null; | |
Options helpOptions = new Options(); | |
helpOptions.addOption("h", "help", false, "show help"); | |
Options options = new Options(); | |
addOptions(options); | |
try { | |
cmd = parser.parse(helpOptions, args, true); | |
if (cmd.hasOption("h")) { | |
HelpFormatter helpformatter = new HelpFormatter(); | |
helpformatter.printHelp("Generate Amalthea from c sources", options); | |
return null; | |
} | |
// now parse main options | |
cmd = parser.parse(options, args); | |
String cProjectPath = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.PDIR); | |
String astpPath = null; | |
if (cmd.hasOption(SCA2AmaltheaHeadlessConstants.ASTP)) { | |
astpPath = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.ASTP); | |
} | |
String buildLogFilePath = null; | |
if (cmd.hasOption(SCA2AmaltheaHeadlessConstants.BLOG)) { | |
buildLogFilePath = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.BLOG); | |
} | |
String hFilePathList = null; | |
if (cmd.hasOption(SCA2AmaltheaHeadlessConstants.HDIR_LIST)) { | |
hFilePathList = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.HDIR_LIST); | |
} | |
String taskinfo = null; | |
if (cmd.hasOption(SCA2AmaltheaHeadlessConstants.TASKINFO)) { | |
taskinfo = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.TASKINFO); | |
} | |
String lockinfo = null; | |
if (cmd.hasOption(SCA2AmaltheaHeadlessConstants.LOCKINFO)) { | |
lockinfo = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.LOCKINFO); | |
} | |
String enableStructMembers = null; | |
if (cmd.hasOption(SCA2AmaltheaHeadlessConstants.ENABLE_STRUCT_MEMBER)) { | |
enableStructMembers = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.ENABLE_STRUCT_MEMBER); | |
} | |
String outdir = null; | |
if (cmd.hasOption(SCA2AmaltheaHeadlessConstants.OUTDIR)) { | |
outdir = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.OUTDIR); | |
} | |
String xmlCallTreePath = null; | |
if (cmd.hasOption(SCA2AmaltheaHeadlessConstants.XMLCALLTREE_PATH)) { | |
xmlCallTreePath = cmd.getOptionValue(SCA2AmaltheaHeadlessConstants.XMLCALLTREE_PATH); | |
} | |
SCA2AMALTHEAStarterProperties sca2amaltheaStarterProperties = new SCA2AMALTHEAStarterProperties(); | |
sca2amaltheaStarterProperties.setProjectPath(cProjectPath); | |
sca2amaltheaStarterProperties.getLlvmStarterProperties().setTraverseAstFile(astpPath); | |
sca2amaltheaStarterProperties.setOutPutPath(outdir); | |
sca2amaltheaStarterProperties.setTaskListFile(taskinfo); | |
sca2amaltheaStarterProperties.setHDirFilePath(hFilePathList); | |
sca2amaltheaStarterProperties.setBuildLogFile(buildLogFilePath); | |
sca2amaltheaStarterProperties.setLockinfoFile(lockinfo); | |
sca2amaltheaStarterProperties.setIsStructMemberEnabled(Boolean.valueOf(enableStructMembers)); | |
sca2amaltheaStarterProperties.setPathToxmlCallTree(xmlCallTreePath); | |
return sca2amaltheaStarterProperties; | |
} | |
catch (ParseException e) { | |
Logmanager.getInstance().logException(this.getClass(), e, Activator.PLUGIN_ID); | |
HelpFormatter helpformatter = new HelpFormatter(); | |
helpformatter.printHelp("Generate Amalthea from c sources", options); | |
return null; | |
} | |
} | |
/** | |
* @param options {@link Option} | |
*/ | |
private void addOptions(final Options options) { | |
options.addOption("h", "help", false, "show help"); | |
Option pDirOption = new Option(SCA2AmaltheaHeadlessConstants.PDIR, "cproject_directory", true, | |
SCA2AmaltheaHeadlessConstants.C_PROJECT_DIR_HELP); | |
pDirOption.setRequired(true); | |
options.addOption(pDirOption); | |
Option astOption = new Option(SCA2AmaltheaHeadlessConstants.ASTP, "astparser", true, | |
SCA2AmaltheaHeadlessConstants.AST_PARSER_HELP); | |
options.addOption(astOption); | |
Option xmlCallTreeOption = new Option(SCA2AmaltheaHeadlessConstants.XMLCALLTREE_PATH, "xmlCallTree_path", true, | |
SCA2AmaltheaHeadlessConstants.XMLCALLTREE_HELP); | |
xmlCallTreeOption.setRequired(false); | |
options.addOption(xmlCallTreeOption); | |
Option buildLogOption = | |
new Option(SCA2AmaltheaHeadlessConstants.BLOG, "build_log_file", true, SCA2AmaltheaHeadlessConstants.BLOG_HELP); | |
buildLogOption.setRequired(false); | |
options.addOption(buildLogOption); | |
Option hDirOption = new Option(SCA2AmaltheaHeadlessConstants.HDIR_LIST, "hdir_directories", true, | |
SCA2AmaltheaHeadlessConstants.HDIR_LIST_HELP); | |
hDirOption.setRequired(false); | |
options.addOption(hDirOption); | |
Option taskInfoOption = new Option(SCA2AmaltheaHeadlessConstants.TASKINFO, "task_info_file", true, | |
SCA2AmaltheaHeadlessConstants.TASKINFO_HELP); | |
taskInfoOption.setRequired(false); | |
options.addOption(taskInfoOption); | |
Option lockinfoOption = new Option(SCA2AmaltheaHeadlessConstants.LOCKINFO, "bsw_lock_functions", true, | |
SCA2AmaltheaHeadlessConstants.LOCKINFO_HELP); | |
lockinfoOption.setRequired(false); | |
options.addOption(lockinfoOption); | |
Option enableStructMember = new Option(SCA2AmaltheaHeadlessConstants.ENABLE_STRUCT_MEMBER, "enableStructMember", | |
false, SCA2AmaltheaHeadlessConstants.ENABLE_STRUCT_MEMBER_HELP); | |
enableStructMember.setRequired(false); | |
Option outDirOption = new Option(SCA2AmaltheaHeadlessConstants.OUTDIR, "output_directory", true, | |
SCA2AmaltheaHeadlessConstants.OUTDIR_HELP); | |
outDirOption.setRequired(false); | |
options.addOption(outDirOption); | |
options.addOption(enableStructMember); | |
} | |
} |