Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3919f34371b6bed8ec49c69d2d361ce90553be48 (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
/*******************************************************************************
 * Copyright (c) 2016 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
 *******************************************************************************/
package org.eclipse.cdt.core.build;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ConsoleOutputStream;
import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.IConsoleParser;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.cdt.core.resources.IConsole;
import org.eclipse.cdt.internal.core.build.Messages;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
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.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;

/**
 * A Standard Build Configuration that simply calls a specified command for
 * build and clean. By default, it calls 'make all' and 'make clean'.
 * 
 * @since 6.2
 */
public class StandardBuildConfiguration extends CBuildConfiguration {

	/**
	 * @since 6.4
	 */
	public static final String BUILD_CONTAINER = "stdbuild.build.container"; //$NON-NLS-1$
	/**
	 * @since 6.4
	 */
	public static final String BUILD_COMMAND = "stdbuild.build.command"; //$NON-NLS-1$
	/**
	 * @since 6.4
	 */
	public static final String CLEAN_COMMAND = "stdbuild.clean.command"; //$NON-NLS-1$

	private String[] buildCommand;
	private String[] cleanCommand;
	private IContainer buildContainer;

	public StandardBuildConfiguration(IBuildConfiguration config, String name) throws CoreException {
		super(config, name);
		applyProperties();
	}

	public StandardBuildConfiguration(IBuildConfiguration config, String name, IToolChain toolChain,
			String launchMode) {
		super(config, name, toolChain);
	}

	private void applyProperties() {
		String container = getProperty(BUILD_CONTAINER);
		if (container != null && !container.trim().isEmpty()) {
			IPath containerLoc = new org.eclipse.core.runtime.Path(container);
			if (containerLoc.segmentCount() == 1) {
				buildContainer = ResourcesPlugin.getWorkspace().getRoot().getProject(containerLoc.segment(0));
			} else {
				buildContainer = ResourcesPlugin.getWorkspace().getRoot().getFolder(containerLoc);
			}
		}

		String buildCmd = getProperty(BUILD_COMMAND);
		if (buildCmd != null && !buildCmd.trim().isEmpty()) {
			buildCommand = buildCmd.split(" "); //$NON-NLS-1$
		}

		String cleanCmd = getProperty(CLEAN_COMMAND);
		if (cleanCmd != null && !cleanCmd.trim().isEmpty()) {
			cleanCommand = cleanCmd.split(" "); //$NON-NLS-1$
		}
	}

	public void setBuildContainer(IContainer buildContainer) {
		this.buildContainer = buildContainer;
		setProperty(BUILD_CONTAINER, buildContainer.getFullPath().toString());
	}

	public void setBuildCommand(String[] buildCommand) {
		this.buildCommand = buildCommand;
		setProperty(BUILD_COMMAND, String.join(" ", buildCommand)); //$NON-NLS-1$
	}

	public void setCleanCommand(String[] cleanCommand) {
		this.cleanCommand = cleanCommand;
	}

	private void createBuildContainer(IContainer container, IProgressMonitor monitor) throws CoreException {
		IContainer parent = container.getParent();
		if (!(parent instanceof IProject) && !parent.exists()) {
			createBuildContainer(parent, monitor);
		}

		if (container instanceof IFolder) {
			((IFolder) container).create(IResource.FORCE | IResource.DERIVED, true, monitor);
		}
	}

	@Override
	public IContainer getBuildContainer() throws CoreException {
		if (buildContainer == null) {
			return super.getBuildContainer();
		} else {
			if (!(buildContainer instanceof IProject) && !buildContainer.exists()) {
				createBuildContainer(buildContainer, new NullProgressMonitor());
			}
		}
		return buildContainer != null ? buildContainer : super.getBuildContainer();
	}

	/**
	 * @since 6.4
	 */
	public IContainer getDefaultBuildContainer() throws CoreException {
		return super.getBuildContainer();
	}

	@Override
	public String getProperty(String name) {
		String prop = super.getProperty(name);
		if (prop != null) {
			return prop;
		}

		switch (name) {
		case BUILD_CONTAINER:
			try {
				return getBuildContainer().getFullPath().toString();
			} catch (CoreException e) {
				CCorePlugin.log(e.getStatus());
			}
		}

		return null;
	}

	@Override
	public boolean setProperties(Map<String, String> properties) {
		if (!super.setProperties(properties)) {
			return false;
		}
		applyProperties();
		return true;
	}

	@Override
	public IProject[] build(int kind, Map<String, String> args, IConsole console, IProgressMonitor monitor)
			throws CoreException {
		IProject project = getProject();
		try {
			project.deleteMarkers(ICModelMarker.C_MODEL_PROBLEM_MARKER, false, IResource.DEPTH_INFINITE);

			ConsoleOutputStream outStream = console.getOutputStream();

			Path buildDir = getBuildDirectory();

			outStream.write(String.format(Messages.StandardBuildConfiguration_0, buildDir.toString()));

			List<String> command;
			if (buildCommand != null) {
				command = Arrays.asList(buildCommand);
				Path make = findCommand(buildCommand[0]);
				command.set(0, make.toString());
			} else {
				command = new ArrayList<>();
				command.add(findCommand("make").toString()); //$NON-NLS-1$
				if (!getBuildContainer().equals(getProject())) {
					Path makefile = Paths.get(getProject().getFile("Makefile").getLocationURI()); //$NON-NLS-1$
					Path relative = getBuildDirectory().relativize(makefile);
					command.add("-f"); //$NON-NLS-1$
					command.add(relative.toString());
				}
				command.add("all"); //$NON-NLS-1$
			}

			try (ErrorParserManager epm = new ErrorParserManager(project, getProject().getLocationURI(), this,
					getToolChain().getErrorParserIds())) {
				epm.setOutputStream(console.getOutputStream());
				// run make
				console.getOutputStream().write(String.format("%s\n", String.join(" ", command))); //$NON-NLS-1$ //$NON-NLS-2$
				ProcessBuilder processBuilder = new ProcessBuilder(command)
						.directory(getBuildDirectory().toFile());
				setBuildEnvironment(processBuilder.environment());
				Process process = processBuilder.start();
				IConsoleParser[] consoleParsers = new IConsoleParser[] { epm, this };
				watchProcess(process, consoleParsers);
			}

			project.refreshLocal(IResource.DEPTH_INFINITE, monitor);

			outStream.write(String.format(Messages.StandardBuildConfiguration_1, buildDir.toString()));

			return new IProject[] { project };
		} catch (IOException e) {
			throw new CoreException(
					new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, "Building " + project.getName(), e)); //$NON-NLS-1$
		}
	}

	@Override
	public void clean(IConsole console, IProgressMonitor monitor) throws CoreException {
		IProject project = getProject();
		try {
			project.deleteMarkers(ICModelMarker.C_MODEL_PROBLEM_MARKER, false, IResource.DEPTH_INFINITE);

			ConsoleOutputStream outStream = console.getOutputStream();

			List<String> command;
			if (cleanCommand != null) {
				command = Arrays.asList(cleanCommand);
			} else {
				command = new ArrayList<>();
				command.add(findCommand("make").toString()); //$NON-NLS-1$
				if (!getBuildContainer().equals(getProject())) {
					Path makefile = Paths.get(getProject().getFile("Makefile").getLocationURI()); //$NON-NLS-1$
					Path relative = getBuildDirectory().relativize(makefile);
					command.add("-f"); //$NON-NLS-1$
					command.add(relative.toString());
				}
				command.add("clean"); //$NON-NLS-1$
			}

			// run make
			outStream.write(String.format("%s\n", String.join(" ", command))); //$NON-NLS-1$ //$NON-NLS-2$
			ProcessBuilder processBuilder = new ProcessBuilder(command)
					.directory(getBuildDirectory().toFile());
			setBuildEnvironment(processBuilder.environment());
			Process process = processBuilder.start();
			watchProcess(process, console);

			project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
		} catch (IOException e) {
			throw new CoreException(
					new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, "Building " + project.getName(), e)); //$NON-NLS-1$
		}
	}

}

Back to the top