Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 22c4518841c42218a9b3aa83557aa7b2885fd32c (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
/*******************************************************************************
 * Copyright (c) 2015, 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.internal.meson.core;

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

import org.eclipse.cdt.core.CommandLauncherManager;
import org.eclipse.cdt.core.ConsoleOutputStream;
import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.ICommandLauncher;
import org.eclipse.cdt.core.build.CBuildConfiguration;
import org.eclipse.cdt.core.build.ICBuildCommandLauncher;
import org.eclipse.cdt.core.build.IToolChain;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.cdt.core.resources.IConsole;
import org.eclipse.cdt.meson.core.Activator;
import org.eclipse.cdt.meson.core.IMesonConstants;
import org.eclipse.cdt.meson.core.IMesonToolChainFile;
import org.eclipse.cdt.meson.core.IMesonToolChainManager;
import org.eclipse.cdt.utils.Platform;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

import com.google.gson.Gson;

public class MesonBuildConfiguration extends CBuildConfiguration {

	private static final String TOOLCHAIN_FILE = "cdt.meson.toolchainfile"; //$NON-NLS-1$

	private IMesonToolChainFile toolChainFile;

	public MesonBuildConfiguration(IBuildConfiguration config, String name) throws CoreException {
		super(config, name);

		IMesonToolChainManager manager = Activator.getService(IMesonToolChainManager.class);
		Preferences settings = getSettings();
		String pathStr = settings.get(TOOLCHAIN_FILE, ""); //$NON-NLS-1$
		if (!pathStr.isEmpty()) {
			Path path = Paths.get(pathStr);
			toolChainFile = manager.getToolChainFile(path);
		} else {
			toolChainFile = manager.getToolChainFileFor(getToolChain());
			if (toolChainFile != null) {
				saveToolChainFile();
			}
		}
	}

	public MesonBuildConfiguration(IBuildConfiguration config, String name, IToolChain toolChain) {
		this(config, name, toolChain, null, "run"); //$NON-NLS-1$
	}

	public MesonBuildConfiguration(IBuildConfiguration config, String name, IToolChain toolChain,
			IMesonToolChainFile toolChainFile, String launchMode) {
		super(config, name, toolChain, launchMode);

		this.toolChainFile = toolChainFile;
		if (toolChainFile != null) {
			saveToolChainFile();
		}
	}

	private void saveToolChainFile() {
		Preferences settings = getSettings();
		settings.put(TOOLCHAIN_FILE, toolChainFile.getPath().toString());
		try {
			settings.flush();
		} catch (BackingStoreException e) {
			Activator.log(e);
		}
	}

	private boolean isLocal() throws CoreException {
		IToolChain toolchain = getToolChain();
		return Platform.getOS().equals(toolchain.getProperty(IToolChain.ATTR_OS))
				&& Platform.getOSArch().equals(toolchain.getProperty(IToolChain.ATTR_ARCH));
	}

	@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.MesonBuildConfiguration_BuildingIn, buildDir.toString()));

			// Make sure we have a toolchain file if cross
			if (toolChainFile == null && !isLocal()) {
				IMesonToolChainManager manager = Activator.getService(IMesonToolChainManager.class);
				toolChainFile = manager.getToolChainFileFor(getToolChain());
				
				if (toolChainFile == null) {
					// error
					console.getErrorStream().write(Messages.MesonBuildConfiguration_NoToolchainFile);
					return null;
				}
			}

			boolean runMeson = !Files.exists(buildDir.resolve("build.ninja")); //$NON-NLS-1$
			if (runMeson) { // $NON-NLS-1$
				Path path = findCommand("meson"); //$NON-NLS-1$
				if (path == null) {
					path = Paths.get("meson"); //$NON-NLS-1
				}

				List<String> argsList = new ArrayList<>();
				
				String userArgs = getProperty(IMesonConstants.MESON_ARGUMENTS);
				if (userArgs != null) {
					argsList.addAll(Arrays.asList(userArgs.trim().split("\\s+"))); //$NON-NLS-1$
				}
				
				argsList.add(getBuildDirectory().toString());

				String envStr = getProperty(IMesonConstants.MESON_ENV);
				String[] env = new String[0];
				if (envStr != null) {
					env = envStr.split(IMesonConstants.MESON_ENV_SEPARATOR); //$NON-NLS-1$
				}
				ICommandLauncher launcher = CommandLauncherManager.getInstance().getCommandLauncher(this);
				
				launcher.setProject(getProject());
				if (launcher instanceof ICBuildCommandLauncher) {
					((ICBuildCommandLauncher)launcher).setBuildConfiguration(this);
				}

				monitor.subTask(Messages.MesonBuildConfiguration_RunningMeson);
				
				org.eclipse.core.runtime.Path mesonPath = new org.eclipse.core.runtime.Path(path.toString());
				outStream.write(String.join(" ", envStr != null ? envStr : "", //$NON-NLS-1$ //$NON-NLS-2$ 
						mesonPath.toString(), userArgs, "\n")); //$NON-NLS-1$
				outStream.write(getBuildDirectory() + "\n"); //$NON-NLS-1$
				org.eclipse.core.runtime.Path workingDir = new org.eclipse.core.runtime.Path(getBuildDirectory().getParent().getParent().toString());
				
				launcher.execute(mesonPath, argsList.toArray(new String[0]), env, workingDir, monitor);
				if (launcher.waitAndRead(outStream, outStream, SubMonitor.convert(monitor)) != ICommandLauncher.OK) {
					String errMsg = launcher.getErrorMessage();
					console.getErrorStream().write(String.format(Messages.MesonBuildConfiguration_RunningMesonFailure, errMsg));
					return null;
				};
			}

			try (ErrorParserManager epm = new ErrorParserManager(project, getBuildDirectoryURI(), this,
					getToolChain().getErrorParserIds())) {
				epm.setOutputStream(console.getOutputStream());
				
				String buildCommand = getProperty(IMesonConstants.BUILD_COMMAND);
				if (buildCommand == null || buildCommand.isEmpty()) {
					buildCommand = "ninja";
				}

				
				String envStr = getProperty(IMesonConstants.MESON_ENV);
				String[] env = new String[0];
				if (envStr != null) {
					env = envStr.split(envStr);
				}
				ICommandLauncher launcher = CommandLauncherManager.getInstance().getCommandLauncher(this);
				
				launcher.setProject(getProject());
				if (launcher instanceof ICBuildCommandLauncher) {
					((ICBuildCommandLauncher)launcher).setBuildConfiguration(this);
				}

				monitor.subTask(Messages.MesonBuildConfiguration_RunningMeson);
				
				org.eclipse.core.runtime.Path ninjaPath = new org.eclipse.core.runtime.Path(buildCommand);
				outStream.write(String.join(" ", ninjaPath.toString() + '\n')); //$NON-NLS-1$
				org.eclipse.core.runtime.Path workingDir = new org.eclipse.core.runtime.Path(getBuildDirectory().toString());
				
				launcher.execute(ninjaPath, new String[0], env, workingDir, monitor);
				if (launcher.waitAndRead(epm.getOutputStream(), epm.getOutputStream(), SubMonitor.convert(monitor)) != ICommandLauncher.OK) {
					String errMsg = launcher.getErrorMessage();
					console.getErrorStream().write(String.format(Messages.MesonBuildConfiguration_RunningNinjaFailure, errMsg));
					return null;
				};
			}

			project.refreshLocal(IResource.DEPTH_INFINITE, monitor);

			// Load compile_commands.json file
			processCompileCommandsFile(monitor);

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

			return new IProject[] { project };
		} catch (IOException e) {
			throw new CoreException(Activator.errorStatus(String.format(Messages.MesonBuildConfiguration_Building, project.getName()), e));
		}
	}

	@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();

			Path buildDir = getBuildDirectory();

			try (ErrorParserManager epm = new ErrorParserManager(project, getBuildDirectoryURI(), this,
					getToolChain().getErrorParserIds())) {
				epm.setOutputStream(console.getOutputStream());


				String cleanCommand = getProperty(IMesonConstants.CLEAN_COMMAND);
				if (cleanCommand == null) {
					cleanCommand = "ninja clean"; //$NON-NLS-1$
				}
				String[] command = cleanCommand.split(" "); //$NON-NLS-1$

				Path cmdPath = findCommand(command[0]);
				if (cmdPath != null) {
					command[0] = cmdPath.toString();
				}

				IPath cmd = new org.eclipse.core.runtime.Path(command[0]);
				ICommandLauncher launcher = CommandLauncherManager.getInstance().getCommandLauncher(this);

				launcher.setProject(getProject());
				if (launcher instanceof ICBuildCommandLauncher) {
					((ICBuildCommandLauncher)launcher).setBuildConfiguration(this);
				}

				String[] commandargs = new String[0];
				if (command.length > 1) {
					commandargs = Arrays.copyOfRange(command, 1, command.length);
				}
				org.eclipse.core.runtime.Path workingDir = new org.eclipse.core.runtime.Path(buildDir.toString());

				String[] env = new String[0];

				outStream.write(String.join(" ", command) + '\n'); //$NON-NLS-1$
				launcher.execute(cmd, commandargs, env, workingDir, monitor);
				if (launcher.waitAndRead(epm.getOutputStream(), epm.getOutputStream(), SubMonitor.convert(monitor)) != ICommandLauncher.OK) {
					String errMsg = launcher.getErrorMessage();
					console.getErrorStream().write(String.format(Messages.MesonBuildConfiguration_RunningNinjaFailure, errMsg));
					return;
				};
			}

			project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
		} catch (IOException e) {
			throw new CoreException(Activator.errorStatus(String.format(Messages.MesonBuildConfiguration_Cleaning, project.getName()), e));
		}
	}

	private void processCompileCommandsFile(IProgressMonitor monitor) throws CoreException {
		IProject project = getProject();
		Path commandsFile = getBuildDirectory().resolve("compile_commands.json"); //$NON-NLS-1$
		if (Files.exists(commandsFile)) {
			monitor.setTaskName(Messages.MesonBuildConfiguration_ProcCompJson);
			try (FileReader reader = new FileReader(commandsFile.toFile())) {
				Gson gson = new Gson();
				CompileCommand[] commands = gson.fromJson(reader, CompileCommand[].class);
				Map<String, CompileCommand> dedupedCmds = new HashMap<>();
				for (CompileCommand command : commands) {
					dedupedCmds.put(command.getFile(), command);
				}
				for (CompileCommand command : dedupedCmds.values()) {
					processLine(command.getCommand());
				}
				shutdown();
			} catch (IOException e) {
				throw new CoreException(
						Activator.errorStatus(String.format(Messages.MesonBuildConfiguration_ProcCompCmds, project.getName()), e));
			}
		}
	}

}

Back to the top