Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9fce12031fdb26a9377b73dd6341803f20c72d17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*******************************************************************************
 * Copyright (c) 2015, 2017, 2018 QNX Software Systems 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:
 * 		Red Hat Inc. - modified for use in Container build
 *******************************************************************************/
package org.eclipse.cdt.internal.docker.launcher.ui.launchbar;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CommandLauncherManager;
import org.eclipse.cdt.core.ICommandLauncher;
import org.eclipse.cdt.core.build.ICBuildCommandLauncher;
import org.eclipse.cdt.core.build.ICBuildConfiguration;
import org.eclipse.cdt.core.build.IToolChain;
import org.eclipse.cdt.core.build.IToolChain2;
import org.eclipse.cdt.core.build.IToolChainProvider;
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.parser.ExtendedScannerInfo;
import org.eclipse.cdt.core.parser.IExtendedScannerInfo;
import org.eclipse.cdt.core.resources.IConsole;
import org.eclipse.cdt.docker.launcher.ContainerCommandLauncher;
import org.eclipse.cdt.docker.launcher.ContainerTargetTypeProvider;
import org.eclipse.cdt.docker.launcher.DockerLaunchUIPlugin;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.linuxtools.docker.ui.Activator;

/**
 * The Container GCC toolchain. It represents a GCC that will run in a Docker
 * Container. It can be overridden to change environment variable settings.
 * 
 * @since 1.2
 */
public class ContainerGCCToolChain extends PlatformObject
		implements IToolChain, IToolChain2 {

	public static final String TYPE_ID = "org.eclipse.cdt.docker.launcher.gcc"; //$NON-NLS-1$

	private final IToolChainProvider provider;
	private final String id;
	private final Path path;
	private final IEnvironmentVariable[] envVars;
	private final Map<String, String> properties = new HashMap<>();

	private String cCommand;
	private String cppCommand;
	private String[] commands;


	public ContainerGCCToolChain(String id, IToolChainProvider provider,
			Map<String, String> properties,
			IEnvironmentVariable[] envVars) {
		this.provider = provider;
		this.path = new File("/usr/bin/gcc").toPath(); //$NON-NLS-1$

		// We include arch in the id since a compiler can support multiple arches.
		StringBuilder idBuilder = new StringBuilder("container-gcc-"); //$NON-NLS-1$
		idBuilder.append(properties.get(Platform.getOSArch()));
		idBuilder.append('-');
		idBuilder.append(path.toString());
		this.id = id;

		this.properties.putAll(properties);
		this.envVars = envVars;
	}

	@Override
	public String getTypeId() {
		return TYPE_ID;
	}

	public Path getPath() {
		return path;
	}

	@Override
	public IToolChainProvider getProvider() {
		return provider;
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public String getVersion() {
		return ""; //$NON-NLS-1$
	}

	@Override
	public String getName() {
		StringBuilder name = new StringBuilder(); // $NON-NLS-1$
		String os = getProperty(ATTR_OS);
		if (os != null) {
			name.append(os);
			name.append(' ');
		}

		String arch = getProperty(ATTR_ARCH);
		if (arch != null) {
			name.append(arch);
			name.append(' ');
		}

		if (path != null) {
			name.append(path.toString());
		}

		return name.toString();
	}

	@Override
	public String getProperty(String key) {
		String value = properties.get(key);
		if (value != null) {
			return value;
		}

		switch (key) {
		case ATTR_OS:
			return ContainerTargetTypeProvider.CONTAINER_LINUX;
		case ATTR_ARCH:
			return Platform.getOSArch();
		}

		return null;
	}

	public Map<String, String> getProperties() {
		return properties;
	}

	@Override
	public void setProperty(String key, String value) {
		properties.put(key, value);
	}
	
	@Override
	public String getBinaryParserId() {
		return CCorePlugin.PLUGIN_ID + ".ELF"; //$NON-NLS-1$
	}

	protected void addDiscoveryOptions(List<String> command) {
		command.add("-E"); //$NON-NLS-1$
		command.add("-P"); //$NON-NLS-1$
		command.add("-v"); //$NON-NLS-1$
		command.add("-dD"); //$NON-NLS-1$
	}

	@Override
	public IExtendedScannerInfo getScannerInfo(IBuildConfiguration buildConfig, List<String> commandStrings,
			IExtendedScannerInfo baseScannerInfo, IResource resource, URI buildDirectoryURI) {
		try {
			Path buildDirectory = Paths.get(buildDirectoryURI);

			int offset = 0;
			Path command = Paths.get(commandStrings.get(offset));

			// look for ccache being used
			if (command.toString().contains("ccache")) { //$NON-NLS-1$
				command = Paths.get(commandStrings.get(++offset));
			}

			List<String> commandLine = new ArrayList<>();
			if (command.isAbsolute()) {
				commandLine.add(command.toString());
			} else {
				commandLine.add(getCommandPath(command).toString());
			}

			if (baseScannerInfo != null) {
				if (baseScannerInfo.getIncludePaths() != null) {
					for (String includePath : baseScannerInfo.getIncludePaths()) {
						commandLine.add("-I" + includePath); //$NON-NLS-1$
					}
				}

				if (baseScannerInfo.getDefinedSymbols() != null) {
					for (Map.Entry<String, String> macro : baseScannerInfo.getDefinedSymbols().entrySet()) {
						if (macro.getValue() != null && !macro.getValue().isEmpty()) {
							commandLine.add("-D" + macro.getKey() + "=" + macro.getValue()); //$NON-NLS-1$
						} else {
							commandLine.add("-D" + macro.getKey()); //$NON-NLS-1$
						}
					}
				}
			}

			addDiscoveryOptions(commandLine);
			commandLine.addAll(
					commandStrings.subList(offset + 1, commandStrings.size()));

			// Strip surrounding quotes from the args on Windows
			if (Platform.OS_WIN32.equals(Platform.getOS())) {
				for (int i = 0; i < commandLine.size(); i++) {
					String arg = commandLine.get(i);
					if (arg.startsWith("\"") && arg.endsWith("\"")) { //$NON-NLS-1$ //$NON-NLS-2$
						commandLine.set(i, arg.substring(1, arg.length() - 1));
					}
				}
			}

			// Change output to stdout
			boolean haveOut = false;
			for (int i = 0; i < commandLine.size() - 1; ++i) {
				if (commandLine.get(i).equals("-o")) { //$NON-NLS-1$
					commandLine.set(i + 1, "-"); //$NON-NLS-1$
					haveOut = true;
					break;
				}
			}
			if (!haveOut) {
				commandLine.add("-o"); //$NON-NLS-1$
				commandLine.add("-"); //$NON-NLS-1$
			}

			// Change source file to a tmp file (needs to be empty)
			Path tmpFile = null;
			for (int i = 1; i < commandLine.size(); ++i) {
				String arg = commandLine.get(i);
				if (!arg.startsWith("-")) { //$NON-NLS-1$
					Path filePath;
					try {
						filePath = buildDirectory.resolve(commandLine.get(i)).normalize();
					} catch (InvalidPathException e) {
						continue;
					}
					IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(filePath.toUri());
					if (files.length > 0 && files[0].exists()) {
						// replace it with a temp file
						Path parentPath = filePath.getParent();
						String extension = files[0].getFileExtension();
						if (extension == null) {
							// Not sure if this is a reasonable choice when
							// there's
							// no extension
							extension = ".cpp"; //$NON-NLS-1$
						} else {
							extension = '.' + extension;
						}
						tmpFile = Files.createTempFile(parentPath, ".sc", extension); //$NON-NLS-1$
						commandLine.set(i, tmpFile.toString());
					}
				} else if (arg.equals("-o")) { //$NON-NLS-1$
					// skip over the next arg
					// TODO handle other args like this
					i++;
				}
			}
			if (tmpFile == null) {
				// Have to assume there wasn't a source file. Add one in the
				// resource's container
				// TODO really?
				IPath parentPath = resource instanceof IFile ? resource.getParent().getLocation()
						: resource.getLocation();
				if (parentPath.toFile().exists()) {
					tmpFile = Files.createTempFile(parentPath.toFile().toPath(), ".sc", ".cpp"); //$NON-NLS-1$ //$NON-NLS-2$
					commandLine.add(tmpFile.toString());
				}
			}

			return getScannerInfo(buildConfig, commandLine, buildDirectory, tmpFile);
		} catch (IOException e) {
			Activator.log(e);
			return null;
		}
	}

	@Override
	public IExtendedScannerInfo getDefaultScannerInfo(IBuildConfiguration buildConfig,
			IExtendedScannerInfo baseScannerInfo, ILanguage language, URI buildDirectoryURI) {
		try {
			String[] commands = getCompileCommands(language);
			if (commands == null || commands.length == 0) {
				// no default commands
				return null;
			}

			Path buildDirectory = Paths.get(buildDirectoryURI);

			// Pick the first one
			Path command = Paths.get(commands[0]);
			List<String> commandLine = new ArrayList<>();
			if (command.isAbsolute()) {
				commandLine.add(command.toString());
			} else {
				commandLine.add(getCommandPath(command).toString());
			}

			if (baseScannerInfo != null) {
				if (baseScannerInfo.getIncludePaths() != null) {
					for (String includePath : baseScannerInfo.getIncludePaths()) {
						commandLine.add("-I" + includePath); //$NON-NLS-1$
					}
				}
				
				if (baseScannerInfo.getDefinedSymbols() != null) {
					for (Map.Entry<String, String> macro : baseScannerInfo.getDefinedSymbols().entrySet()) {
						if (macro.getValue() != null && !macro.getValue().isEmpty()) {
							commandLine.add("-D" + macro.getKey() + "=" + macro.getValue()); //$NON-NLS-1$
						} else {
							commandLine.add("-D" + macro.getKey()); //$NON-NLS-1$
						}
					}
				}
			}

			addDiscoveryOptions(commandLine);

			// output to stdout
			commandLine.add("-o"); //$NON-NLS-1$
			commandLine.add("-"); //$NON-NLS-1$

			// Source is an empty tmp file
			String extension;
			if (GPPLanguage.ID.equals(language.getId())) {
				extension = ".cpp"; //$NON-NLS-1$
			} else if (GCCLanguage.ID.equals(language.getId())) {
				extension = ".c"; //$NON-NLS-1$
			} else {
				// In theory we shouldn't get here
				return null;
			}

			Path tmpFile = Files.createTempFile(buildDirectory, ".sc", extension); //$NON-NLS-1$
			commandLine.add(tmpFile.toString());

			return getScannerInfo(buildConfig, commandLine, buildDirectory, tmpFile);
		} catch (IOException e) {
			Activator.log(e);
			return null;
		}
	}

	private IExtendedScannerInfo getScannerInfo(IBuildConfiguration buildConfig, List<String> commandLine,
			Path buildDirectory, Path tmpFile) throws IOException {
		Files.createDirectories(buildDirectory);

		// Startup the command
		ContainerCommandLauncher commandLauncher = new ContainerCommandLauncher();
		ICBuildConfiguration cconfig = buildConfig
				.getAdapter(ICBuildConfiguration.class);
		commandLauncher.setBuildConfiguration(cconfig);
		commandLauncher.setProject(buildConfig.getProject());
		// CCorePlugin.getDefault().getBuildEnvironmentManager().setEnvironment(processBuilder.environment(),
		// buildConfig, true);
		org.eclipse.core.runtime.IPath commandPath = new org.eclipse.core.runtime.Path(
				commandLine.get(0));
		String[] args = commandLine.subList(1, commandLine.size())
				.toArray(new String[0]);
		org.eclipse.core.runtime.IPath workingDirectory = new org.eclipse.core.runtime.Path(
				buildDirectory.toString());

		Process process;
		try (ByteArrayOutputStream stdout = new ByteArrayOutputStream();
				ByteArrayOutputStream stderr = new ByteArrayOutputStream()) {
			process = commandLauncher.execute(commandPath, args, new String[0],
					workingDirectory, new NullProgressMonitor());
			if (process != null && commandLauncher.waitAndRead(stdout, stderr,
					new NullProgressMonitor()) != ICommandLauncher.OK) {
				String errMsg = commandLauncher.getErrorMessage();
				DockerLaunchUIPlugin.logErrorMessage(errMsg);
				return null;
			}

			// Scan for the scanner info
			Map<String, String> symbols = new HashMap<>();
			List<String> includePath = new ArrayList<>();
			Pattern definePattern = Pattern.compile("#define ([^\\s]*)\\s(.*)"); //$NON-NLS-1$
			boolean inIncludePaths = false;

			// concatenate stdout after stderr as stderr has the include paths
			// and stdout has the defines
			String[] outlines = stdout.toString(StandardCharsets.UTF_8.name())
					.split("\\r?\\n"); //$NON-NLS-1$
			String[] errlines = stderr.toString(StandardCharsets.UTF_8.name())
					.split("\\r?\\n"); //$NON-NLS-1$
			String[] lines = new String[errlines.length + outlines.length];
			System.arraycopy(errlines, 0, lines, 0, errlines.length);
			System.arraycopy(outlines, 0, lines, errlines.length,
					outlines.length);

			for (String line : lines) {
				line = line.trim();
				if (inIncludePaths) {
					if (line.equals("End of search list.")) { //$NON-NLS-1$
						inIncludePaths = false;
					} else {
						String include = line.trim();
						org.eclipse.core.runtime.IPath path = new org.eclipse.core.runtime.Path(
								include);
						if (!path.isAbsolute()) {
							org.eclipse.core.runtime.IPath newPath = workingDirectory
									.append(path);
							include = newPath.makeAbsolute().toPortableString();
						}
						includePath.add(include);
					}
				} else if (line.startsWith("#define ")) { //$NON-NLS-1$
					Matcher matcher = definePattern.matcher(line);
					if (matcher.matches()) {
						symbols.put(matcher.group(1), matcher.group(2));
					}
				} else if (line.equals("#include <...> search starts here:")) { //$NON-NLS-1$
					inIncludePaths = true;
				}
			}

			// Process include paths for scanner info and point to any copied
			// header directories
			includePath = CommandLauncherManager.getInstance()
					.processIncludePaths(cconfig, includePath);

			ExtendedScannerInfo info = new ExtendedScannerInfo(symbols,
					includePath.toArray(new String[includePath.size()]));
			return info;
		} catch (CoreException e1) {
			return null;
		} finally {
			Files.delete(tmpFile);
		}

	}

	@Override
	public String[] getErrorParserIds() {
		return new String[] { "org.eclipse.cdt.core.GCCErrorParser", //$NON-NLS-1$
				"org.eclipse.cdt.core.GASErrorParser", //$NON-NLS-1$
				"org.eclipse.cdt.core.GLDErrorParser", //$NON-NLS-1$
				"org.eclipse.cdt.core.GmakeErrorParser", //$NON-NLS-1$
				"org.eclipse.cdt.core.CWDLocator" //$NON-NLS-1$
		};
	}

	@Override
	public IEnvironmentVariable getVariable(String name) {
		if (envVars != null) {
			for (IEnvironmentVariable var : envVars) {
				if (var.getName().equals(name)) {
					return var;
				}
			}
		}
		return null;
	}

	@Override
	public IEnvironmentVariable[] getVariables() {
		return envVars;
	}

	@Override
	public Path getCommandPath(Path command) {
		if (command.isAbsolute()) {
			return command;
		}

		return new File("/usr/bin/" + command).toPath(); //$NON-NLS-1$
	}

	private void initCompileCommands() {
		if (commands == null) {
			cCommand = path.getFileName().toString();
			cppCommand = null;
			if (cCommand.contains("gcc")) { //$NON-NLS-1$
				cppCommand = cCommand.replace("gcc", "g++"); //$NON-NLS-1$ //$NON-NLS-2$
				// Also recognize c++ as an alias for g++
				commands = new String[] { cCommand, cppCommand, cCommand.replace("gcc", "cc"), //$NON-NLS-1$ //$NON-NLS-2$
						cCommand.replace("gcc", "c++") }; //$NON-NLS-1$ //$NON-NLS-2$
			} else if (cCommand.contains("clang")) { //$NON-NLS-1$
				cppCommand = cCommand.replace("clang", "clang++"); //$NON-NLS-1$ //$NON-NLS-2$
				commands = new String[] { cCommand, cppCommand };
			} else if (cCommand.contains("emcc")) { //$NON-NLS-1$
				// TODO Hack for emscripten. Can we generalize?
				cppCommand = cCommand.replace("emcc", "em++"); //$NON-NLS-1$ //$NON-NLS-2$
				commands = new String[] { cCommand, cppCommand };
			} else {
				commands = new String[] { cCommand };
			}
		}
	}

	@Override
	public String[] getCompileCommands() {
		initCompileCommands();
		return commands;
	}

	@Override
	public String[] getCompileCommands(ILanguage language) {
		initCompileCommands();
		if (GPPLanguage.ID.equals(language.getId())) {
			return new String[] { cppCommand != null ? cppCommand : cCommand };
		} else if (GCCLanguage.ID.equals(language.getId())) {
			return new String[] { cCommand };
		} else {
			return new String[0];
		}
	}

	@Override
	public IResource[] getResourcesFromCommand(List<String> cmd, URI buildDirectoryURI) {
		// Start at the back looking for arguments
		List<IResource> resources = new ArrayList<>();
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		for (int i = cmd.size() - 1; i >= 0; --i) {
			String arg = cmd.get(i);
			if (arg.startsWith("-")) { //$NON-NLS-1$
				// ran into an option, we're done.
				break;
			}
			if (i > 1 && cmd.get(i - 1).equals("-o")) { //$NON-NLS-1$
				// this is an output file
				--i;
				continue;
			}
			try {
				Path srcPath = Paths.get(arg);
				URI uri;
				if (srcPath.isAbsolute()) {
					uri = srcPath.toUri();
				} else {
					if (arg.startsWith("/") && Platform.getOS().equals(Platform.OS_WIN32)) { //$NON-NLS-1$
						String drive = srcPath.getName(0).toString();
						if (drive.length() == 1) {
							srcPath = Paths.get(drive + ":\\").resolve(srcPath.subpath(1, srcPath.getNameCount())); //$NON-NLS-1$
						}
					}
					uri = Paths.get(buildDirectoryURI).resolve(srcPath).toUri().normalize();
				}

				for (IFile resource : root.findFilesForLocationURI(uri)) {
					resources.add(resource);
				}
			} catch (IllegalArgumentException e) {
				// Bad URI
				continue;
			}
		}

		return resources.toArray(new IResource[resources.size()]);
	}

	@Override
	public List<String> stripCommand(List<String> command, IResource[] resources) {
		List<String> newCommand = new ArrayList<>();

		for (int i = 0; i < command.size() - resources.length; ++i) {
			String arg = command.get(i);
			if (arg.startsWith("-o")) { //$NON-NLS-1$
				if (arg.equals("-o")) { //$NON-NLS-1$
					i++;
				}
				continue;
			}
			newCommand.add(arg);
		}

		return newCommand;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ContainerGCCToolChain tc = (ContainerGCCToolChain) obj;
		if (tc.id != this.id)
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public Process startBuildProcess(ICBuildConfiguration config,
			List<String> command, String buildDirectory,
			IEnvironmentVariable[] envVars, IConsole console,
			IProgressMonitor monitor) throws CoreException, IOException {

		IPath cmdPath = new org.eclipse.core.runtime.Path("/usr/bin/env"); //$NON-NLS-1$

		List<String> argList = new ArrayList<>();
		for (IEnvironmentVariable var : envVars) {
			argList.add(var.getName() + "=" + var.getValue()); //$NON-NLS-1$
		}

		argList.add("sh"); //$NON-NLS-1$
		argList.add("-c"); //$NON-NLS-1$

		StringBuffer buf = new StringBuffer();
		for (String s : command) {
			buf.append(s);
			buf.append(" "); //$NON-NLS-1$
		}
		buf.deleteCharAt(buf.length() - 1); // remove last blank;
		argList.add(buf.toString());

		ICommandLauncher launcher = CommandLauncherManager.getInstance()
				.getCommandLauncher(config);

		launcher.setProject(config.getBuildConfiguration().getProject());
		if (launcher instanceof ICBuildCommandLauncher) {
			((ICBuildCommandLauncher) launcher).setBuildConfiguration(config);
			console.getOutputStream().write(
					((ICBuildCommandLauncher) launcher).getConsoleHeader());
		}

		org.eclipse.core.runtime.Path workingDir = new org.eclipse.core.runtime.Path(
				buildDirectory);

		Process p = launcher.execute(cmdPath, argList.toArray(new String[0]),
				new String[0], workingDir, monitor);

		return p;
	}

}

Back to the top