Skip to content

Commit bfc7730

Browse files
author
jantje
committed
#493 generic implementation to handle define strings with spaces in
command line.
1 parent 05fe8b9 commit bfc7730

File tree

3 files changed

+100
-153
lines changed

3 files changed

+100
-153
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
#fix for due, zero, Esplora, Leonardo, Robot Control, Robot Motor, Yun, Micro and LillyPad USB
2-
build.usb_flags.windows=-DUSB_VID={build.vid} -DUSB_PID={build.pid} -DUSBCON -DUSB_MANUFACTURER=\"{build.usb_manufacturer}\" -DUSB_PRODUCT=\"{build.usb_product}\"
3-

it.baeyens.arduino.core/src/it/baeyens/arduino/toolchain/ArduinoGnuMakefileGenerator.java

+40-124
Original file line numberDiff line numberDiff line change
@@ -1759,19 +1759,9 @@ protected boolean addRuleForTool(ITool tool, StringBuffer buffer, boolean bTarge
17591759
// TODO report error
17601760
flags = EMPTY_STRING_ARRAY;
17611761
}
1762-
String command = tool.getToolCommand();
1763-
try {
1764-
// try to resolve the build macros in the tool command
1765-
String resolvedCommand = ManagedBuildManager.getBuildMacroProvider()
1766-
.resolveValueToMakefileFormat(command, EMPTY_STRING, WHITESPACE,
1767-
IBuildMacroProvider.CONTEXT_FILE, new FileContextData(null, null, null, tool))
1768-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
1769-
if ((resolvedCommand = resolvedCommand.trim()).length() > 0)
1770-
command = resolvedCommand;
1771-
1772-
} catch (BuildMacroException e) {// JABA is not going to write this
1773-
// code
1774-
}
1762+
1763+
String command = resolveCommand(true, tool.getToolCommand(), null, null, tool);
1764+
17751765
String[] cmdInputs = inputs.toArray(new String[inputs.size()]);
17761766
IManagedCommandLineGenerator gen = tool.getCommandLineGenerator();
17771767
IManagedCommandLineInfo cmdLInfo = gen.generateCommandLineInfo(tool, command, flags, outflag, outputPrefix,
@@ -1794,17 +1784,7 @@ IBuildMacroProvider.CONTEXT_FILE, new FileContextData(null, null, null, tool))
17941784

17951785
// resolve any remaining macros in the command after it has been
17961786
// generated
1797-
try {
1798-
String resolvedCommand = ManagedBuildManager.getBuildMacroProvider()
1799-
.resolveValueToMakefileFormat(buildCmd, EMPTY_STRING, WHITESPACE,
1800-
IBuildMacroProvider.CONTEXT_FILE, new FileContextData(null, null, null, tool))
1801-
.replaceFirst(" -w ", " "); //$NON-NLS-1$//$NON-NLS-2$
1802-
if ((resolvedCommand = resolvedCommand.trim()).length() > 0)
1803-
buildCmd = resolvedCommand;
1804-
1805-
} catch (BuildMacroException e) {// JABA is not going to write this
1806-
// code
1807-
}
1787+
buildCmd = resolveCommand(true, buildCmd, null, null, tool);
18081788

18091789
// buffer.append(TAB + AT + escapedEcho(buildCmd));
18101790
// buffer.append(TAB + AT + buildCmd);
@@ -2375,40 +2355,17 @@ protected void addToBuildVar(LinkedHashMap<String, String> buildVarToRuleStringM
23752355
}
23762356
}
23772357

2378-
@SuppressWarnings({ "static-method" })
2358+
@SuppressWarnings({ "static-method", "nls" })
23792359
private IManagedCommandLineInfo generateToolCommandLineInfo(ITool tool, String sourceExtension, String[] flags,
23802360
String outputFlag, String outputPrefix, String outputName, String[] inputResources, IPath inputLocation,
23812361
IPath outputLocation) {
23822362

23832363
String cmd = tool.getToolCommand();
23842364
// try to resolve the build macros in the tool command
2385-
try {
2386-
String resolvedCommand = null;
23872365

2388-
if ((inputLocation != null && inputLocation.toString().indexOf(" ") != -1) || //$NON-NLS-1$
2389-
(outputLocation != null && outputLocation.toString().indexOf(" ") != -1)) //$NON-NLS-1$
2390-
{
2391-
resolvedCommand = ManagedBuildManager.getBuildMacroProvider()
2392-
.resolveValue(cmd, "", //$NON-NLS-1$
2393-
" ", //$NON-NLS-1$
2394-
IBuildMacroProvider.CONTEXT_FILE,
2395-
new FileContextData(inputLocation, outputLocation, null, tool))
2396-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
2397-
}
2398-
2399-
else {
2400-
resolvedCommand = ManagedBuildManager.getBuildMacroProvider()
2401-
.resolveValueToMakefileFormat(cmd, "", //$NON-NLS-1$
2402-
" ", //$NON-NLS-1$
2403-
IBuildMacroProvider.CONTEXT_FILE,
2404-
new FileContextData(inputLocation, outputLocation, null, tool))
2405-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
2406-
}
2407-
if ((resolvedCommand = resolvedCommand.trim()).length() > 0)
2408-
cmd = resolvedCommand;
2409-
2410-
} catch (BuildMacroException e) {// JABA is not going to write this code
2411-
}
2366+
boolean useMakeFileFormat = !((inputLocation != null && inputLocation.toString().indexOf(" ") != -1)
2367+
|| (outputLocation != null && outputLocation.toString().indexOf(" ") != -1));
2368+
cmd = resolveCommand(useMakeFileFormat, cmd, inputLocation, outputLocation, tool);
24122369

24132370
IManagedCommandLineGenerator gen = tool.getCommandLineGenerator();
24142371
return gen.generateCommandLineInfo(tool, cmd, flags, outputFlag, outputPrefix, outputName, inputResources,
@@ -2614,28 +2571,7 @@ && containsSpecialCharacters(sourceLocation.toString()))
26142571
// Get and resolve the command
26152572
String cmd = tool.getToolCommand();
26162573

2617-
try {
2618-
String resolvedCommand = null;
2619-
if (!needExplicitRuleForFile) {
2620-
resolvedCommand = ManagedBuildManager.getBuildMacroProvider()
2621-
.resolveValueToMakefileFormat(cmd, EMPTY_STRING, WHITESPACE, IBuildMacroProvider.CONTEXT_FILE,
2622-
new FileContextData(sourceLocation, outputLocation, null, tool))
2623-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
2624-
} else {
2625-
// if we need an explicit rule then don't use any builder
2626-
// variables, resolve everything
2627-
// to explicit strings
2628-
resolvedCommand = ManagedBuildManager.getBuildMacroProvider()
2629-
.resolveValue(cmd, EMPTY_STRING, WHITESPACE, IBuildMacroProvider.CONTEXT_FILE,
2630-
new FileContextData(sourceLocation, outputLocation, null, tool))
2631-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
2632-
}
2633-
2634-
if ((resolvedCommand = resolvedCommand.trim()).length() > 0)
2635-
cmd = resolvedCommand;
2636-
2637-
} catch (BuildMacroException e) {// JABA is not going to write this code
2638-
}
2574+
cmd = resolveCommand(!needExplicitRuleForFile, cmd, sourceLocation, outputLocation, tool);
26392575

26402576
String defaultOutputName = EMPTY_STRING;
26412577
String primaryDependencyName = EMPTY_STRING;
@@ -2765,31 +2701,9 @@ && containsSpecialCharacters(sourceLocation.toString()))
27652701
String[] preToolCommands = depCommands.getPreToolDependencyCommands();
27662702
if (preToolCommands != null && preToolCommands.length > 0) {
27672703
for (String preCmd : preToolCommands) {
2768-
try {
2769-
String resolvedCommand;
2770-
IBuildMacroProvider provider = ManagedBuildManager.getBuildMacroProvider();
2771-
if (!needExplicitRuleForFile) {
2772-
resolvedCommand = provider
2773-
.resolveValueToMakefileFormat(preCmd, EMPTY_STRING, WHITESPACE,
2774-
IBuildMacroProvider.CONTEXT_FILE,
2775-
new FileContextData(sourceLocation, outputLocation, null, tool))
2776-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
2777-
} else {
2778-
// if we need an explicit rule then don't use
2779-
// any builder
2780-
// variables, resolve everything to explicit
2781-
// strings
2782-
resolvedCommand = provider
2783-
.resolveValue(preCmd, EMPTY_STRING, WHITESPACE,
2784-
IBuildMacroProvider.CONTEXT_FILE,
2785-
new FileContextData(sourceLocation, outputLocation, null, tool))
2786-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
2787-
}
2788-
if (resolvedCommand != null)
2789-
buffer.append(resolvedCommand + NEWLINE);
2790-
} catch (BuildMacroException e) {// JABA is not going to
2791-
// write this code
2792-
}
2704+
buffer.append(
2705+
resolveCommand(!needExplicitRuleForFile, preCmd, sourceLocation, outputLocation, tool)
2706+
+ NEWLINE);
27932707
}
27942708
}
27952709
}
@@ -2872,32 +2786,7 @@ && containsSpecialCharacters(sourceLocation.toString()))
28722786
+ outputPrefix + OUT_MACRO + otherPrimaryOutputs + WHITESPACE + IN_MACRO;
28732787
}
28742788

2875-
// resolve any remaining macros in the command after it has been
2876-
// generated
2877-
try {
2878-
String resolvedCommand;
2879-
IBuildMacroProvider provider = ManagedBuildManager.getBuildMacroProvider();
2880-
if (!needExplicitRuleForFile) {
2881-
resolvedCommand = provider
2882-
.resolveValueToMakefileFormat(buildCmd, EMPTY_STRING, WHITESPACE,
2883-
IBuildMacroProvider.CONTEXT_FILE,
2884-
new FileContextData(sourceLocation, outputLocation, null, tool))
2885-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
2886-
} else {
2887-
// if we need an explicit rule then don't use any builder
2888-
// variables, resolve everything to explicit strings
2889-
resolvedCommand = provider
2890-
.resolveValue(buildCmd, EMPTY_STRING, WHITESPACE, IBuildMacroProvider.CONTEXT_FILE,
2891-
new FileContextData(sourceLocation, outputLocation, null, tool))
2892-
.replaceFirst(" -w ", " "); //$NON-NLS-1$ //$NON-NLS-2$
2893-
}
2894-
2895-
if ((resolvedCommand = resolvedCommand.trim()).length() > 0)
2896-
buildCmd = resolvedCommand;
2897-
2898-
} catch (BuildMacroException e) {// JABA is not going to write this
2899-
// code
2900-
}
2789+
buildCmd = resolveCommand(!needExplicitRuleForFile, buildCmd, sourceLocation, outputLocation, tool);
29012790

29022791
// buffer.append(TAB + AT + escapedEcho(buildCmd));
29032792
// buffer.append(TAB + AT + buildCmd);
@@ -4951,4 +4840,31 @@ private ToolInfoHolder getToolInfo(IPath path, boolean create) {
49514840
}
49524841
return h;
49534842
}
4843+
4844+
@SuppressWarnings("nls")
4845+
private static String resolveCommand(boolean makeFormat, String inCommand, IPath inputLocation,
4846+
IPath outputLocation, IBuildObject tool) {
4847+
String returnedCommand = inCommand;
4848+
try {
4849+
if (makeFormat) {
4850+
returnedCommand = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(inCommand,
4851+
"", " ", IBuildMacroProvider.CONTEXT_FILE,
4852+
new FileContextData(inputLocation, outputLocation, null, tool));
4853+
} else {
4854+
returnedCommand = ManagedBuildManager.getBuildMacroProvider().resolveValue(inCommand, "", " ",
4855+
IBuildMacroProvider.CONTEXT_FILE,
4856+
new FileContextData(inputLocation, outputLocation, null, tool));
4857+
}
4858+
4859+
} catch (BuildMacroException e) {// JABA is not going to write this
4860+
// code
4861+
}
4862+
returnedCommand = returnedCommand.replaceFirst(" -w ", " ").trim();
4863+
if (returnedCommand.isEmpty()) {
4864+
return inCommand;
4865+
4866+
}
4867+
return ArduinoLanguageProvider.adaptCompilerCommand(returnedCommand);
4868+
4869+
}
49544870
}

it.baeyens.arduino.core/src/it/baeyens/arduino/toolchain/ArduinoLanguageProvider.java

+60-26
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import org.eclipse.core.resources.IFolder;
1818
import org.eclipse.core.runtime.CoreException;
1919
import org.eclipse.core.runtime.IStatus;
20+
import org.eclipse.core.runtime.Platform;
2021
import org.eclipse.core.runtime.Status;
2122

2223
import it.baeyens.arduino.common.Common;
2324
import it.baeyens.arduino.common.Const;
2425

25-
public class ArduinoLanguageProvider extends ToolchainBuiltinSpecsDetector implements ILanguageSettingsEditableProvider {
26+
public class ArduinoLanguageProvider extends ToolchainBuiltinSpecsDetector
27+
implements ILanguageSettingsEditableProvider {
2628
// ID must match the tool-chain definition in
2729
// org.eclipse.cdt.managedbuilder.core.buildDefinitions extension point
2830
private static final String GCC_TOOLCHAIN_ID = "cdt.managedbuild.toolchain.gnu.base"; //$NON-NLS-1$
@@ -108,7 +110,8 @@ protected List<String> parseOptions(String lineIn) {
108110
}
109111

110112
@Override
111-
public void startup(ICConfigurationDescription cfgDescription, IWorkingDirectoryTracker cwdTracker1) throws CoreException {
113+
public void startup(ICConfigurationDescription cfgDescription, IWorkingDirectoryTracker cwdTracker1)
114+
throws CoreException {
112115
super.startup(cfgDescription, cwdTracker1);
113116

114117
this.state = State.NONE;
@@ -131,18 +134,16 @@ public ArduinoLanguageProvider clone() throws CloneNotSupportedException {
131134
return (ArduinoLanguageProvider) super.clone();
132135
}
133136

137+
@SuppressWarnings("nls")
134138
@Override
135139
protected String getCompilerCommand(String languageId) {
136140
String compilerCommand = Const.EMPTY_STRING;
137-
// ArduinoProperties arduinoProperties = new
138-
// ArduinoProperties(currentProject);
141+
139142
ICProjectDescription prjDesc = CoreModel.getDefault().getProjectDescription(this.currentProject);
140143
if (prjDesc == null)
141144
return compilerCommand;
142145

143146
IEnvironmentVariableManager envManager = CCorePlugin.getDefault().getBuildEnvironmentManager();
144-
// IContributedEnvironment contribEnv =
145-
// envManager.getContributedEnvironment();
146147
ICConfigurationDescription confDesc = prjDesc.getActiveConfiguration();
147148

148149
// Bug fix for CDT 8.1 fixed in 8.2
@@ -151,52 +152,85 @@ protected String getCompilerCommand(String languageId) {
151152
try {
152153
buildFolder.create(true, true, null);
153154
} catch (CoreException e) {
154-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to create folder " + confDesc.getName(), e)); //$NON-NLS-1$
155+
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID,
156+
"failed to create folder " + confDesc.getName(), e));
155157
}
156158
}
157159
// End of Bug fix for CDT 8.1 fixed in 8.2
158-
if (languageId.equals("org.eclipse.cdt.core.gcc")) { //$NON-NLS-1$
160+
if (languageId.equals("org.eclipse.cdt.core.gcc")) {
159161
try {
160-
compilerCommand = envManager.getVariable(Const.get_ENV_KEY_RECIPE(Const.ACTION_C_to_O), confDesc, true).getValue().replace(" -o ", //$NON-NLS-1$
161-
" "); //$NON-NLS-1$
162+
compilerCommand = envManager.getVariable(Const.get_ENV_KEY_RECIPE(Const.ACTION_C_to_O), confDesc, true)
163+
.getValue().replace(" -o ", " "); //$NON-NLS-2$
162164
} catch (Exception e) {
163165
compilerCommand = Const.EMPTY_STRING;
164166
}
165-
IEnvironmentVariable op1 = envManager.getVariable(Const.ENV_KEY_JANTJE_ADDITIONAL_COMPILE_OPTIONS, confDesc, true);
166-
IEnvironmentVariable op2 = envManager.getVariable(Const.ENV_KEY_JANTJE_ADDITIONAL_C_COMPILE_OPTIONS, confDesc, true);
167+
IEnvironmentVariable op1 = envManager.getVariable(Const.ENV_KEY_JANTJE_ADDITIONAL_COMPILE_OPTIONS, confDesc,
168+
true);
169+
IEnvironmentVariable op2 = envManager.getVariable(Const.ENV_KEY_JANTJE_ADDITIONAL_C_COMPILE_OPTIONS,
170+
confDesc, true);
167171
if (op1 != null) {
168172
compilerCommand = compilerCommand + ' ' + op1.getValue();
169173
}
170174
if (op2 != null) {
171175
compilerCommand = compilerCommand + ' ' + op2.getValue();
172176
}
173-
compilerCommand = compilerCommand + " -D" + Const.DEFINE_IN_ECLIPSE + "=1"; //$NON-NLS-1$ //$NON-NLS-2$
174-
} else if (languageId.equals("org.eclipse.cdt.core.g++")) { //$NON-NLS-1$
177+
compilerCommand = compilerCommand + " -D" + Const.DEFINE_IN_ECLIPSE + "=1";
178+
} else if (languageId.equals("org.eclipse.cdt.core.g++")) {
175179
try {
176-
compilerCommand = envManager.getVariable(Const.get_ENV_KEY_RECIPE(Const.ACTION_CPP_to_O), confDesc, true).getValue().replace(" -o ", //$NON-NLS-1$
177-
" "); //$NON-NLS-1$
180+
compilerCommand = envManager
181+
.getVariable(Const.get_ENV_KEY_RECIPE(Const.ACTION_CPP_to_O), confDesc, true).getValue()
182+
.replace(" -o ", " ");
178183
} catch (Exception e) {
179184
compilerCommand = Const.EMPTY_STRING;
180185
}
181-
IEnvironmentVariable op1 = envManager.getVariable(Const.ENV_KEY_JANTJE_ADDITIONAL_COMPILE_OPTIONS, confDesc, true);
182-
IEnvironmentVariable op2 = envManager.getVariable(Const.ENV_KEY_JANTJE_ADDITIONAL_CPP_COMPILE_OPTIONS, confDesc, true);
186+
IEnvironmentVariable op1 = envManager.getVariable(Const.ENV_KEY_JANTJE_ADDITIONAL_COMPILE_OPTIONS, confDesc,
187+
true);
188+
IEnvironmentVariable op2 = envManager.getVariable(Const.ENV_KEY_JANTJE_ADDITIONAL_CPP_COMPILE_OPTIONS,
189+
confDesc, true);
183190
if (op1 != null) {
184191
compilerCommand = compilerCommand + ' ' + op1.getValue();
185192
}
186193
if (op2 != null) {
187194
compilerCommand = compilerCommand + ' ' + op2.getValue();
188195
}
189-
compilerCommand = compilerCommand + " -D" + Const.DEFINE_IN_ECLIPSE + "=1"; //$NON-NLS-1$ //$NON-NLS-2$
196+
compilerCommand = compilerCommand + " -D" + Const.DEFINE_IN_ECLIPSE + "=1";
190197
} else {
191-
ManagedBuilderCorePlugin.error("Unable to find compiler command for language " + languageId + " in toolchain=" + getToolchainId()); //$NON-NLS-1$ //$NON-NLS-2$
198+
ManagedBuilderCorePlugin.error(
199+
"Unable to find compiler command for language " + languageId + " in toolchain=" + getToolchainId());
192200
}
193201

194-
String ret = compilerCommand.replaceAll(" -MMD ", " ").replaceAll("[^\\\\]\"\"", Const.EMPTY_STRING) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
195-
.replaceAll(" ", " "); // remove //$NON-NLS-1$ //$NON-NLS-2$
196-
// "" except \""
197-
// and
198-
// double
199-
// blanks
202+
return adaptCompilerCommand(compilerCommand.replaceAll(" -MMD ", " "));
203+
}
204+
205+
/*
206+
* due to the way arduino and cdt work some conversions are needed her.
207+
* replaceAll(" -MMD ", " ") CDT adds -MMD so we delete them
208+
*
209+
* replaceAll("[^\\\\]\"\"", Const.EMPTY_STRING I can't recall what this one
210+
* is for but it removes "" except \""
211+
*
212+
* For the os dependent stuff see
213+
* https://github.com./jantje/arduino-eclipse-plugin/issues/493
214+
*
215+
* replaceAll(" ", " ") due to the above replacements there can be multiple
216+
* spaces. this cause(s/d) problems so I re^lace them with 1 space. note
217+
* that -with the current implementation- this means that is you define a
218+
* string to a define and the string has multiple spaces there will only be
219+
* one left. This one has to be the last replacement !!
220+
*/
221+
@SuppressWarnings("nls")
222+
public static String adaptCompilerCommand(String environmentReceivedRecipe) {
223+
String ret = environmentReceivedRecipe.replaceAll("[^\\\\]\"\"", Const.EMPTY_STRING);
224+
225+
String replaceString = "'-D$1=\"$2\"'"; // linux and mac
226+
if (Platform.getOS().equals(Platform.OS_WIN32)) {
227+
replaceString = " \"-D$1=\\\\\"$2\\\\\"\""; // windows
228+
}
229+
230+
ret = ret.replaceAll(" '?-D(\\S+)=\\\\?\"(.+?)\\\\?\"'?", replaceString);
231+
232+
ret = ret.replaceAll(" ", " ");
233+
200234
return ret;
201235
}
202236

0 commit comments

Comments
 (0)