1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 17:26:01 +02:00

Bug 279633 - Add ability to override command-generation for a build-option

This commit is contained in:
Chris Recoskie 2011-03-09 02:24:52 +00:00
parent e170d35e58
commit 71900424e1
9 changed files with 278 additions and 2 deletions

View file

@ -4934,6 +4934,70 @@
owner="cdt.test.assignToOption.exeLinker"/>
</tool>
<!-- Project type for testing custom option command-generation -->
<projectType
id="cdt.test.customOptionCommand.ProjectType"
isAbstract="false"
isTest="true"
name="Custom Option Command Type">
<configuration
id="cdt.test.customOptionCommand.one"
name="One">
<toolChain isSystem="true"
id="cdt.test.customOptionCommand.Toolchain"
isAbstract="false"
name="Tools">
<tool
command="xxx"
id="cdt.test.customOptionCommand.Tool"
isAbstract="false"
name="Tool"
natureFilter="both"
outputFlag="-o">
<optionCategory
id="cdt.test.customOptionCommand.optionCategory1"
name="Test Options"
owner="cdt.test.customOptionCommand.Tool"/>
<option
category="cdt.test.customOptionCommand.optionCategory1"
command="-opt1="
id="cdt.test.customOptionCommand.option1"
name="Option1"
commandGenerator="org.eclipse.cdt.managedbuilder.core.tests.CustomOptionCommandGenerator"
valueType="stringList"/>
<option
category="cdt.test.customOptionCommand.optionCategory1"
command="-opt2="
id="cdt.test.customOptionCommand.option2"
name="Option2"
commandGenerator="org.eclipse.cdt.managedbuilder.core.tests.CustomOptionCommandGenerator"
valueType="string"/>
<option
category="cdt.test.customOptionCommand.optionCategory1"
command="-opt3 ${value}"
id="cdt.test.customOptionCommand.option3"
name="Option3"
commandGenerator="org.eclipse.cdt.managedbuilder.core.tests.CustomOptionCommandGenerator"
valueType="string"/>
<option
category="cdt.test.customOptionCommand.optionCategory1"
command="-opt4="
id="cdt.test.customOptionCommand.option4"
name="Option4"
valueType="string"/>
</tool>
<builder
id="cdt.test.customOptionCommand.builder"
name="Builder"
command="make"
arguments="-k"
buildfileGenerator="org.eclipse.cdt.managedbuilder.makegen.gnu.GnuMakefileGenerator">
</builder>
</toolChain>
</configuration>
</projectType>
<!-- Project type for testing option command ${VALUE} -->
<projectType

View file

@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2011 Texas Instruments and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Baltasar Belyavsky (Texas Instruments) - [279633] Custom command-generator support
*******************************************************************************/
package org.eclipse.cdt.managedbuilder.core.tests;
import java.util.List;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionCommandGenerator;
import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver;
import org.eclipse.cdt.utils.cdtvariables.IVariableSubstitutor;
public class CustomOptionCommandGenerator implements IOptionCommandGenerator
{
/* (non-Javadoc)
* @see org.eclipse.cdt.managedbuilder.core.IOptionCommandGenerator#generateCommand(org.eclipse.cdt.managedbuilder.core.IOption, org.eclipse.cdt.utils.cdtvariables.IVariableSubstitutor)
*/
public String generateCommand(IOption option, IVariableSubstitutor macroSubstitutor) {
Object value = option.getValue();
if(value instanceof List) {
try {
String[] list = CdtVariableResolver.resolveStringListValues(option.getBasicStringListValue(), macroSubstitutor, true);
if(list != null) {
StringBuilder sb = new StringBuilder();
for(String entry : list) {
sb.append(entry + ';');
}
return option.getCommand() + '\"' + sb.toString() + '\"';
}
}
catch(Exception x) {
}
}
return null;
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2006 Intel Corporation and others.
* Copyright (c) 2004, 2011 Intel Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
*
* Contributors:
* Intel Corporation - Initial API and implementation
* Baltasar Belyavsky (Texas Instruments) - [279633] Custom command-generator support
*******************************************************************************/
package org.eclipse.cdt.managedbuilder.core.tests;
@ -173,6 +174,47 @@ public class ManagedCommandLineGeneratorTest extends TestCase {
assertEquals("TestBuildFile.mak", name);
}
public final void testCustomOptionCommandGenerator() {
try{
IProject project = ManagedBuildTestHelper.createProject("COCG", null, (IPath)null, "cdt.test.customOptionCommand.ProjectType");
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
IConfiguration config = info.getDefaultConfiguration();
ITool[] tools = config.getToolsBySuperClassId("cdt.test.customOptionCommand.Tool");
assertEquals(tools.length, 1);
ITool tool = tools[0];
IOption option1 = tool.getOptionBySuperClassId("cdt.test.customOptionCommand.option1");
IOption option2 = tool.getOptionBySuperClassId("cdt.test.customOptionCommand.option2");
IOption option3 = tool.getOptionBySuperClassId("cdt.test.customOptionCommand.option3");
IOption option4 = tool.getOptionBySuperClassId("cdt.test.customOptionCommand.option4");
assertTrue(option1.getCommandGenerator() instanceof CustomOptionCommandGenerator);
assertTrue(option2.getCommandGenerator() instanceof CustomOptionCommandGenerator);
assertTrue(option3.getCommandGenerator() instanceof CustomOptionCommandGenerator);
assertNull(option4.getCommandGenerator());
option1 = config.setOption(tool, option1, new String[] {"val1", "val2", "${ProjName}"});
option2 = config.setOption(tool, option2, "${ProjName}");
option3 = config.setOption(tool, option3, "${ProjName}");
option4 = config.setOption(tool, option4, "${ProjName}");
/* Expected results
* option1: custom command-generator concatenates list-entries into quoted semicolon-separated list.
* option2/3: custom command-generator returns 'null' causing CDT to fall-back to default behaviour.
* option4: no custom command-generator contributed - CDT falls back to default behaviour.
*/
String command = tool.getToolCommandFlagsString(null, null);
assertEquals("-opt1=\"val1;val2;COCG;\" -opt2=COCG -opt3 COCG -opt4=COCG", command);
ManagedBuildTestHelper.removeProject("COCG");
}
catch(Exception e){
fail("Test failed on project creation: " + e.getLocalizedMessage());
}
}
public final void testDollarValue() {
try{
IProject project = ManagedBuildTestHelper.createProject("CDV", null, (IPath)null, "cdt.test.dollarValue.ProjectType");

View file

@ -1341,6 +1341,16 @@ Additional special types exist to flag options of special relevance to the build
</documentation>
</annotation>
</attribute>
<attribute name="commandGenerator" type="string">
<annotation>
<documentation>
Optional class which can be used to override the default command-generation logic for an option. This class must impelment the IOptionCommandGenerator interface. If no generator is specified then the &apos;comand&apos; attribute is used to generate the option&apos;s command.
</documentation>
<appInfo>
<meta.attribute kind="java" basedOn=":org.eclipse.cdt.managedbuilder.core.IOptionCommandGenerator"/>
</appInfo>
</annotation>
</attribute>
<attribute name="commandFalse" type="string">
<annotation>
<documentation>

View file

@ -10,6 +10,7 @@
* ARM Ltd. - basic tooltip support
* James Blackburn (Broadcom Corp.)
* Petri Tuononen - [321040] Get Library Search Paths
* Baltasar Belyavsky (Texas Instruments) - [279633] Custom command-generator support
*******************************************************************************/
package org.eclipse.cdt.managedbuilder.core;
@ -85,6 +86,8 @@ public interface IOption extends IBuildObject {
public static final String CATEGORY = "category"; //$NON-NLS-1$
public static final String COMMAND = "command"; //$NON-NLS-1$
public static final String COMMAND_FALSE = "commandFalse"; //$NON-NLS-1$
/** @since 8.0 */
public static final String COMMAND_GENERATOR = "commandGenerator"; //$NON-NLS-1$
public static final String TOOL_TIP = "tip"; //$NON-NLS-1$
public static final String CONTEXT_ID = "contextId"; //$NON-NLS-1$
public static final String DEFAULT_VALUE = "defaultValue"; //$NON-NLS-1$
@ -246,6 +249,12 @@ public interface IOption extends IBuildObject {
*/
public String getCommand();
/**
* @return an instance of the class that overrides the default command generation for the option
* @since 8.0
*/
public IOptionCommandGenerator getCommandGenerator();
/**
* Sets a <code>String</code> containing the actual command line
* option associated with the option

View file

@ -0,0 +1,38 @@
/*****************************************************************
* Copyright (c) 2010, 2011 Texas Instruments and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Texas Instruments - Initial API and implementation
*****************************************************************/
package org.eclipse.cdt.managedbuilder.core;
import org.eclipse.cdt.utils.cdtvariables.IVariableSubstitutor;
/**
* This interface can be implemented by clients to contribute custom command-generator
* for a build-option.
*
* The custom command-generator class should be referenced in the <option>/commandGenerator
* attribute of the org.eclipse.cdt.managedbuilder.core.buildDefinitions extension-point.
*
* @since 8.0
*/
public interface IOptionCommandGenerator
{
/**
* Generate the command for the given option.
*
* @param option the underlying build-option
* @param macroSubstitutor to be used for expanding macros in option's value
* @return the generated build-option command. May return {@code null} to fall
* back to the default command generation logic.
*/
String generateCommand(IOption option, IVariableSubstitutor macroSubstitutor);
}

View file

@ -9,6 +9,7 @@
* IBM - Initial API and implementation
* ARM Ltd. - basic tooltip support
* Petri Tuononen - [321040] Get Library Search Paths
* Baltasar Belyavsky (Texas Instruments) - [279633] Custom command-generator support
*******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.core;
@ -29,6 +30,7 @@ import org.eclipse.cdt.managedbuilder.core.IManagedOptionValueHandler;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionApplicability;
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
import org.eclipse.cdt.managedbuilder.core.IOptionCommandGenerator;
import org.eclipse.cdt.managedbuilder.core.IProjectType;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
@ -63,6 +65,8 @@ public class Option extends BuildObject implements IOption, IBuildPropertiesRest
private IOptionCategory category;
private String categoryId;
private String command;
private IConfigurationElement commandGeneratorElement;
private IOptionCommandGenerator commandGenerator;
private String commandFalse;
private String tip;
private String contextId;
@ -277,6 +281,10 @@ public class Option extends BuildObject implements IOption, IBuildPropertiesRest
}
category = option.category;
commandGeneratorElement = option.commandGeneratorElement;
commandGenerator = option.commandGenerator;
applicabilityCalculatorElement = option.applicabilityCalculatorElement;
applicabilityCalculator = option.applicabilityCalculator;
@ -333,6 +341,12 @@ public class Option extends BuildObject implements IOption, IBuildPropertiesRest
// Get the command defined for the option
command = element.getAttribute(COMMAND);
// Get the command-generator, if any
String commandGeneratorStr = element.getAttribute(COMMAND_GENERATOR);
if (commandGeneratorStr != null && element instanceof DefaultManagedConfigElement) {
commandGeneratorElement = ((DefaultManagedConfigElement)element).getConfigurationElement();
}
// Get the command defined for a Boolean option when the value is False
commandFalse = element.getAttribute(COMMAND_FALSE);
@ -1179,6 +1193,29 @@ public class Option extends BuildObject implements IOption, IBuildPropertiesRest
return command;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.managedbuilder.core.IOption#getCommandGenerator()
*/
public IOptionCommandGenerator getCommandGenerator() {
if (commandGenerator == null) {
if (commandGeneratorElement != null) {
try {
if (commandGeneratorElement.getAttribute(COMMAND_GENERATOR) != null) {
commandGenerator = (IOptionCommandGenerator) commandGeneratorElement
.createExecutableExtension(COMMAND_GENERATOR);
}
} catch (CoreException e) {
ManagedBuilderCorePlugin.log(e);
}
}
else if(superClass != null) {
commandGenerator = superClass.getCommandGenerator();
}
}
return commandGenerator;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.managedbuilder.core.IOption#getCommandFalse()
*/

View file

@ -9,6 +9,7 @@
* IBM - Initial API and implementation
* ARM Ltd. - basic tooltip support
* Petri Tuononen - [321040] Get Library Search Paths
* Baltasar Belyavsky (Texas Instruments) - [279633] Custom command-generator support
*******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.core;
@ -23,6 +24,7 @@ import org.eclipse.cdt.managedbuilder.core.IManagedOptionValueHandler;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionApplicability;
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
import org.eclipse.cdt.managedbuilder.core.IOptionCommandGenerator;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.OptionStringValue;
import org.eclipse.cdt.managedbuilder.macros.IOptionContextData;
@ -311,6 +313,13 @@ public class OptionReference implements IOption {
return option.getCommand();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.managedbuilder.core.IOption#getCommandGenerator()
*/
public IOptionCommandGenerator getCommandGenerator() {
return option.getCommandGenerator();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.build.managed.IOption#getCommandFalse()
*/

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2003, 2010 IBM Corporation and others.
* Copyright (c) 2003, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
*
* Contributors:
* IBM - Initial API and implementation
* Baltasar Belyavsky (Texas Instruments) - [279633] Custom option command-generator support
*******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.core;
@ -47,6 +48,7 @@ import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionApplicability;
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
import org.eclipse.cdt.managedbuilder.core.IOptionCommandGenerator;
import org.eclipse.cdt.managedbuilder.core.IOptionPathConverter;
import org.eclipse.cdt.managedbuilder.core.IOutputType;
import org.eclipse.cdt.managedbuilder.core.IProjectType;
@ -2529,6 +2531,20 @@ public class Tool extends HoldsOptions implements ITool, IOptionCategory, IMatch
}
try{
boolean generateDefaultCommand = true;
IOptionCommandGenerator commandGenerator = option.getCommandGenerator();
if(commandGenerator != null) {
IMacroContextInfo info = provider.getMacroContextInfo(BuildMacroProvider.CONTEXT_FILE, new FileContextData(inputFileLocation, outputFileLocation, option, this));
if(info != null) {
macroSubstitutor.setMacroContextInfo(info);
String command = commandGenerator.generateCommand(option, macroSubstitutor);
if(command != null) {
sb.append(command);
generateDefaultCommand = false;
}
}
}
if(generateDefaultCommand) {
switch (option.getValueType()) {
case IOption.BOOLEAN :
String boolCmd;
@ -2612,6 +2628,7 @@ public class Tool extends HoldsOptions implements ITool, IOptionCategory, IMatch
default :
break;
}
}
if (sb.toString().trim().length() > 0)
flags.add(sb.toString().trim());