summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/YoctoCMakeMakefileGenerator.java
blob: b77ae9e824754963eb19f96adc8433cfbf793ef5 (plain)
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
/*******************************************************************************
 * Copyright (c) 2013 BMW Car IT GmbH.
 * 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:
 * BMW Car IT - initial API and implementation
 *******************************************************************************/
package org.yocto.cmake.managedbuilder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.cdt.managedbuilder.core.IBuilder;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderMakefileGenerator2;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
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.MultiStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.yocto.cmake.managedbuilder.job.ExecuteConfigureJob;
import org.yocto.sdk.ide.utils.YoctoSDKUtils;

public class YoctoCMakeMakefileGenerator implements IManagedBuilderMakefileGenerator2 {

	private static final String TOOLCHAINCMAKE_FILE_NAME = "toolchain.cmake"; //$NON-NLS-1$
	private static final String MAKEFILE_NAME = "Makefile"; //$NON-NLS-1$
	private static final String CMAKE_FILE_NAME = "CMakeLists.txt"; //$NON-NLS-1$
	private static final String CMAKECACHE_FILE_NAME = "CMakeCache.txt"; //$NON-NLS-1$

	private IProject project;
	private int lastBuildInfoChecksum = 0;
	private IConfiguration configuration;
	private IProgressMonitor monitor;
	private IPath buildDir = null;

	@Override
	public String getMakefileName() {
		return MAKEFILE_NAME;
	}

	@Override
	public IPath getBuildWorkingDir() {
		IPath buildWorkingDir = null;

		if (buildDir != null) {
			buildWorkingDir = buildDir.removeFirstSegments(1);
		}

		return buildWorkingDir;
	}

	@Override
	public boolean isGeneratedResource(IResource resource) {
		return false;
	}

	@Override
	public void initialize(IProject project, IManagedBuildInfo info,
			IProgressMonitor monitor) {
		this.project = project;
		this.configuration = info.getDefaultConfiguration();
		this.monitor = monitor;

		if (info.getDefaultConfiguration() != null) {
			buildDir = project.getFolder(info.getConfigurationName()).getFullPath();
		}
	}

	@Override
	public void initialize(int buildKind, IConfiguration configuration, IBuilder builder,
			IProgressMonitor monitor) {
		this.project = configuration.getOwner().getProject();
		this.configuration = configuration;
		this.monitor = monitor;
		this.buildDir = project.getFolder(configuration.getName()).getFullPath();
	}

	@Override
	public void generateDependencies() throws CoreException {
		// nothing to do here
	}

	@Override
	public void regenerateDependencies(boolean force) throws CoreException {
		generateDependencies();
	}

	@Override
	public MultiStatus generateMakefiles(IResourceDelta delta)
			throws CoreException {
		int currentBuildInfoChecksum = ManagedBuildManager.getBuildInfo(project).hashCode();

		IFile cmakeFile = project.getFile(CMAKE_FILE_NAME);
		IResourceDelta cmakeDelta = delta.findMember(cmakeFile.getProjectRelativePath());
		IResourceDelta[] deltas = delta
				.getAffectedChildren(IResourceDelta.ADDED | IResourceDelta.REMOVED);

		if (deltas.length > 0 || cmakeDelta != null || currentBuildInfoChecksum != lastBuildInfoChecksum) {
			lastBuildInfoChecksum = currentBuildInfoChecksum;
			return regenerateMakefiles();
		} else {
			// CMake is not needed to run prior to building
			// just return that makefile generation is completed
			return new MultiStatus(
					ManagedBuilderCorePlugin.getUniqueIdentifier(), IStatus.OK,
					new String(YoctoCMakeMessages.getString("YoctoCMakeMakefileGenerator.ok.makefilesStillValid")), null); //$NON-NLS-1$
		}
	}

	@Override
	public MultiStatus regenerateMakefiles() throws CoreException {
		String taskName =
				YoctoCMakeMessages.getString("YoctoCMakeMakefileGenerator.configure.creatingMakefiles"); //$NON-NLS-1$
		monitor.beginTask(taskName, 20);

		IFile cmakeFile = project.getFile(CMAKE_FILE_NAME);
		if (!cmakeFile.exists()) {
			return new MultiStatus(ManagedBuilderCorePlugin.getUniqueIdentifier(), IStatus.CANCEL,
					new String(YoctoCMakeMessages.getString("YoctoCMakeMakefileGenerator.cancel.missingCMakeList")), null); //$NON-NLS-1$
		}

		// Retrieve Build directory
		IPath workingDir = getBuildWorkingDir();
		IPath location = project.getLocation().append(workingDir);
		monitor.worked(1);

		// Create build directory if it doesn't exist
		if (!location.toFile().exists()) {
			monitor.subTask(
					YoctoCMakeMessages.getString("YoctoCMakeMakefileGenerator.creatingBuildDirectory")); //$NON-NLS-1$
			location.toFile().mkdirs();
		} else {
			monitor.subTask(
					YoctoCMakeMessages.getString("YoctoCMakeMakefileGenerator.removingCacheFiles")); //$NON-NLS-1$
			IFile cmakeCache = project.getFile(workingDir.append(CMAKECACHE_FILE_NAME));
			cmakeCache.delete(true, monitor);
		}
		monitor.setTaskName(taskName);

		createToolchainCMakeFile(workingDir);

		// Create the Makefiles by executing cmake
		ExecuteConfigureJob job =
				new ExecuteConfigureJob(
						YoctoCMakeMessages.getString("YoctoCMakeMakefileGenerator.configureJob.name"), //$NON-NLS-1$
						project, configuration, location);
		job.setPriority(Job.BUILD);
		job.setUser(false);

		job.schedule();
		try {
			job.join();
			monitor.done();
			return new MultiStatus(
					Activator.PLUGIN_ID, job.getResult().getSeverity(),
					job.getResult().getMessage(), null);
		} catch (InterruptedException e) {
			return new MultiStatus(
					Activator.PLUGIN_ID,
					IStatus.ERROR, new String(
							YoctoCMakeMessages.getString("YoctoCMakeMakefileGenerator.error.makeFileGenerationFailed")), null); //$NON-NLS-1$
		}
	}

	private String createCMakeSetStatement(String variable, String value, String cacheOption) {
		String setStatement = "set("; //$NON-NLS-1$
		setStatement += variable + " " + value; //$NON-NLS-1$
		if(cacheOption != null && !cacheOption.equals("")) { //$NON-NLS-1$
			setStatement += " " + cacheOption; //$NON-NLS-1$
		}
		setStatement += ")\n"; //$NON-NLS-1$
		return setStatement;
	}

	// Considered poky's cmake.bbclass for this method
	private void createToolchainCMakeFile(IPath workingDir) {
		String toolchainCMakeFileContentAsString = "# CMake system name must be something like \"Linux\".\n" + //$NON-NLS-1$
				"# This is important for cross-compiling.\n"; //$NON-NLS-1$

		String targetArchValue = YoctoSDKUtils.getEnvValue(project, "TARGET_ARCH"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_SYSTEM_PROCESSOR", targetArchValue, null); //$NON-NLS-1$

		String oeCMakeCCompilerValue = 	YoctoSDKUtils.getEnvValue(project, "OECMAKE_C_COMPILER"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_C_COMPILER", oeCMakeCCompilerValue, null); //$NON-NLS-1$

		String oeCMakeCXXCompilerValue = YoctoSDKUtils.getEnvValue(project, "OECMAKE_CXX_COMPILER"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_CXX_COMPILER", oeCMakeCXXCompilerValue, null); //$NON-NLS-1$

		String oeCMakeCFlagsValue = YoctoSDKUtils.getEnvValue(project, "OECMAKE_C_FLAGS"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_C_FLAGS", //$NON-NLS-1$
				"\"" + oeCMakeCFlagsValue + "\"", "CACHE STRING \"CFLAGS\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		String oeCMakeCXXFlagsValue = YoctoSDKUtils.getEnvValue(project, "OECMAKE_CXX_FLAGS"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_CXX_FLAGS", //$NON-NLS-1$
				"\"" + oeCMakeCXXFlagsValue + "\"", "CACHE STRING \"CXXFLAGS\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		String oeCMakeCFlagsReleaseValue = YoctoSDKUtils.getEnvValue(project, "OECMAKE_C_FLAGS_RELEASE"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_C_FLAGS_RELEASE", //$NON-NLS-1$
				"\"" + oeCMakeCFlagsReleaseValue + "\"", "CACHE STRING \"CFLAGS for release\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		String oeCMakeCXXFlagsReleaseValue = YoctoSDKUtils.getEnvValue(project, "OECMAKE_CXX_FLAGS_RELEASE"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_CXX_FLAGS_RELEASE", //$NON-NLS-1$
				"\"" + oeCMakeCXXFlagsReleaseValue + "\"", "CACHE STRING \"CXXFLAGS for release\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		String oeCMakeCLinkFlagsValue = YoctoSDKUtils.getEnvValue(project, "OECMAKE_C_LINK_FLAGS"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_C_LINK_FLAGS", //$NON-NLS-1$
				"\"" + oeCMakeCLinkFlagsValue + "\"", "CACHE STRING \"LDFLAGS\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		String oeCMakeCXXLinkFlagsValue = YoctoSDKUtils.getEnvValue(project, "OECMAKE_CXX_LINK_FLAGS"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_CXX_LINK_FLAGS", //$NON-NLS-1$
				"\"" + oeCMakeCXXLinkFlagsValue + "\"", "CACHE STRING \"LDFLAGS\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		toolchainCMakeFileContentAsString += "\n"; //$NON-NLS-1$
		toolchainCMakeFileContentAsString += "# only search in the paths provided so cmake doesnt pick\n"; //$NON-NLS-1$
		toolchainCMakeFileContentAsString += "# up libraries and tools from the native build machine\n"; //$NON-NLS-1$

		String findRootPathValue = YoctoSDKUtils.getEnvValue(project, "STAGING_DIR_HOST"); //$NON-NLS-1$
		findRootPathValue += " "; //$NON-NLS-1$
		findRootPathValue += YoctoSDKUtils.getEnvValue(project, "STAGING_DIR_NATIVE"); //$NON-NLS-1$
		findRootPathValue += " "; //$NON-NLS-1$
		findRootPathValue += YoctoSDKUtils.getEnvValue(project, "CROSS_DIR"); //$NON-NLS-1$
		findRootPathValue += " "; //$NON-NLS-1$
		findRootPathValue += YoctoSDKUtils.getEnvValue(project, "OECMAKE_PERLNATIVE_DIR"); //$NON-NLS-1$
		findRootPathValue += " "; //$NON-NLS-1$
		findRootPathValue += YoctoSDKUtils.getEnvValue(project, "OECMAKE_EXTRA_ROOT_PATH"); //$NON-NLS-1$
		findRootPathValue += " "; //$NON-NLS-1$
		findRootPathValue += YoctoSDKUtils.getEnvValue(project, "EXTERNAL_TOOLCHAIN"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_FIND_ROOT_PATH", findRootPathValue, null); //$NON-NLS-1$

		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_FIND_ROOT_PATH_MODE_PROGRAM", "ONLY", null); //$NON-NLS-1$ //$NON-NLS-2$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_FIND_ROOT_PATH_MODE_LIBRARY", "ONLY", null); //$NON-NLS-1$ //$NON-NLS-2$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_FIND_ROOT_PATH_MODE_INCLUDE", "ONLY", null); //$NON-NLS-1$ //$NON-NLS-2$
		toolchainCMakeFileContentAsString += "\n"; //$NON-NLS-1$

		toolchainCMakeFileContentAsString += "# Use qt.conf settings\n"; //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("ENV{QT_CONF_PATH}", "qt.conf", null); //$NON-NLS-1$ //$NON-NLS-2$
		toolchainCMakeFileContentAsString += "\n"; //$NON-NLS-1$

		toolchainCMakeFileContentAsString += "# We need to set the rpath to the correct directory as cmake does not provide any\n"; //$NON-NLS-1$
		toolchainCMakeFileContentAsString += "# directory as rpath by default\n"; //$NON-NLS-1$

		String oeCMakeRPathValue = YoctoSDKUtils.getEnvValue(project, "OECMAKE_RPATH"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_INSTALL_RPATH", oeCMakeRPathValue, null); //$NON-NLS-1$

		toolchainCMakeFileContentAsString += "\n"; //$NON-NLS-1$
		toolchainCMakeFileContentAsString += "# Use native cmake modules\n"; //$NON-NLS-1$

		String stagingDatadirValue = YoctoSDKUtils.getEnvValue(project, "STAGING_DATADIR"); //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_MODULE_PATH", //$NON-NLS-1$
				stagingDatadirValue + "/cmake/Modules/", null); //$NON-NLS-1$

		toolchainCMakeFileContentAsString += "\n"; //$NON-NLS-1$
		toolchainCMakeFileContentAsString += "# add for non /usr/lib libdir, e.g. /usr/lib64\n"; //$NON-NLS-1$
		toolchainCMakeFileContentAsString += createCMakeSetStatement("CMAKE_LIBRARY_PATH", //$NON-NLS-1$
				"${libdir} ${base_libdir}", null); //$NON-NLS-1$

		InputStream toolchainCMakeFileContent = new ByteArrayInputStream(toolchainCMakeFileContentAsString.getBytes());

		IFile toolchainCMakeFile = project.getFile(TOOLCHAINCMAKE_FILE_NAME);
		try {
			if (toolchainCMakeFile.exists()) {
				toolchainCMakeFile.delete(true, monitor);
			}
			toolchainCMakeFile.create(toolchainCMakeFileContent, true, monitor);
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}
}