Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f0901661546835807c826f50cf1f85fa3456dd47 (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
/*******************************************************************************
 * Copyright (c) 2007, 2011 Intel Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.dataprovider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.cdt.core.ICDescriptorOperation;
import org.eclipse.cdt.core.cdtvariables.ICdtVariable;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.cdt.core.settings.model.ICConfigExtensionReference;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICExternalSetting;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICStorageElement;
import org.eclipse.cdt.core.settings.model.extension.CConfigurationData;
import org.eclipse.cdt.core.settings.model.extension.ICProjectConverter;
import org.eclipse.cdt.core.settings.model.util.PathEntryTranslator;
import org.eclipse.cdt.core.settings.model.util.PathEntryTranslator.ReferenceSettingsInfo;
import org.eclipse.cdt.internal.core.cdtvariables.ICoreVariableContextInfo;
import org.eclipse.cdt.internal.core.cdtvariables.StorableCdtVariables;
import org.eclipse.cdt.internal.core.cdtvariables.UserDefinedVariableSupplier;
import org.eclipse.cdt.make.core.IMakeTarget;
import org.eclipse.cdt.make.core.IMakeTargetManager;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.internal.core.Builder;
import org.eclipse.cdt.managedbuilder.internal.core.BuilderFactory;
import org.eclipse.cdt.managedbuilder.internal.core.Configuration;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedProject;
import org.eclipse.cdt.managedbuilder.internal.core.ToolChain;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
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;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.IOverwriteQuery;

public class ProjectConverter implements ICProjectConverter {
	private final static String OLD_MAKE_BUILDER_ID = "org.eclipse.cdt.make.core.makeBuilder"; //$NON-NLS-1$
	private final static String OLD_MAKE_NATURE_ID = "org.eclipse.cdt.make.core.makeNature"; //$NON-NLS-1$
	private final static String OLD_MNG_BUILDER_ID = "org.eclipse.cdt.managedbuilder.core.genmakebuilder"; //$NON-NLS-1$
	private final static String OLD_MNG_NATURE_ID = "org.eclipse.cdt.managedbuilder.core.managedBuildNature"; //$NON-NLS-1$
	private final static String OLD_DISCOVERY_MODULE_ID = "scannerConfiguration"; //$NON-NLS-1$
	private final static String OLD_BINARY_PARSER_ID = "org.eclipse.cdt.core.BinaryParser"; //$NON-NLS-1$
	private final static String OLD_ERROR_PARSER_ID = "org.eclipse.cdt.core.ErrorParser"; //$NON-NLS-1$
	private final static String OLD_PATH_ENTRY_ID = "org.eclipse.cdt.core.pathentry"; //$NON-NLS-1$
	private final static String OLD_DISCOVERY_NATURE_ID = "org.eclipse.cdt.make.core.ScannerConfigNature"; //$NON-NLS-1$
	private final static String OLD_DISCOVERY_BUILDER_ID = "org.eclipse.cdt.make.core.ScannerConfigBuilder"; //$NON-NLS-1$
	private final static String OLD_MAKE_TARGET_BUIDER_ID = "org.eclipse.cdt.make.MakeTargetBuilder"; //$NON-NLS-1$
	private final static String NEW_MAKE_TARGET_BUIDER_ID = "org.eclipse.cdt.build.MakeTargetBuilder"; //$NON-NLS-1$

	private static ResourcePropertyHolder PROPS = new ResourcePropertyHolder(true);

	private static String CONVERSION_FAILED_MSG_ID = "conversionFailed"; //$NON-NLS-1$

	@Override
	public boolean canConvertProject(IProject project, String oldOwnerId, ICProjectDescription oldDes) {
		try {
			if (oldOwnerId == null || oldDes == null)
				return false;

			IProjectDescription eDes = project.getDescription();
			Set<String> natureSet = new HashSet<String>(Arrays.asList(eDes.getNatureIds()));
			if (natureSet.contains(OLD_MAKE_NATURE_ID))
				return true;

			if (natureSet.contains(OLD_MNG_NATURE_ID))
				return true;

		} catch (CoreException e) {
		}

		return false;
		//		return ManagedBuildManager.canGetBuildInfo(project);
	}

	@Override
	public ICProjectDescription convertProject(IProject project, IProjectDescription eDes, String oldOwnerId,
			ICProjectDescription oldDes) throws CoreException {
		Set<String> natureSet = new HashSet<String>(Arrays.asList(eDes.getNatureIds()));
		CoreModel model = CoreModel.getDefault();
		ICProjectDescription newDes = null;
		IManagedBuildInfo info = null;
		String[] binErrParserIds = null;
		//		boolean convertMakeTargetInfo = false;

		if (natureSet.contains(OLD_MAKE_NATURE_ID)) {
			newDes = oldDes;
			ICConfigurationDescription des = newDes.getConfigurations()[0];
			ICConfigExtensionReference refs[] = des.get(OLD_BINARY_PARSER_ID);
			if (refs.length != 0) {
				binErrParserIds = new String[refs.length];
				for (int i = 0; i < refs.length; i++) {
					binErrParserIds[i] = refs[i].getID();
				}
			}
			info = ManagedBuildManager.createBuildInfo(project);
			ManagedProject mProj = new ManagedProject(newDes);
			info.setManagedProject(mProj);

			Configuration cfg = ConfigurationDataProvider.getClearPreference(des.getId());
			cfg.applyToManagedProject(mProj);
			cfg.setConfigurationDescription(des);

			des.setConfigurationData(ManagedBuildManager.CFG_DATA_PROVIDER_ID, cfg.getConfigurationData());
		} else if (natureSet.contains(OLD_MNG_NATURE_ID)) {
			try {
				if (PROPS.getProperty(project, CONVERSION_FAILED_MSG_ID) != null)
					throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
							DataProviderMessages.getString("ProjectConverter.0"))); //$NON-NLS-1$

				newDes = model.createProjectDescription(project, false);
				info = convertManagedBuildInfo(project, newDes);
			} catch (CoreException e) {
				displayInfo(project, CONVERSION_FAILED_MSG_ID, DataProviderMessages.getString("ProjectConverter.10"), //$NON-NLS-1$
						DataProviderMessages.getFormattedString("ProjectConverter.11", //$NON-NLS-1$
								new String[] { project.getName(), e.getLocalizedMessage() }));
				throw e;
			}
		}

		if (newDes == null || !newDes.isValid() || newDes.getConfigurations().length == 0) {
			newDes = null;
		} else {
			boolean changeEDes = false;
			if (natureSet.remove(OLD_MAKE_NATURE_ID))
				changeEDes = true;
			if (natureSet.remove(OLD_DISCOVERY_NATURE_ID))
				changeEDes = true;

			if (changeEDes)
				eDes.setNatureIds(natureSet.toArray(new String[natureSet.size()]));

			changeEDes = false;
			ICommand[] cmds = eDes.getBuildSpec();
			List<ICommand> list = new ArrayList<ICommand>(Arrays.asList(cmds));
			ICommand makeBuilderCmd = null;
			for (Iterator<ICommand> iter = list.iterator(); iter.hasNext();) {
				ICommand cmd = iter.next();
				if (OLD_MAKE_BUILDER_ID.equals(cmd.getBuilderName())) {
					makeBuilderCmd = cmd;
					iter.remove();
					changeEDes = true;
					//					convertMakeTargetInfo = true;
				} else if (OLD_DISCOVERY_BUILDER_ID.equals(cmd.getBuilderName())) {
					iter.remove();
					changeEDes = true;
				}
			}

			ICConfigurationDescription cfgDess[] = newDes.getConfigurations();
			for (int i = 0; i < cfgDess.length; i++) {
				ICConfigurationDescription cfgDes = cfgDess[i];
				BuildConfigurationData data = (BuildConfigurationData) cfgDes.getConfigurationData();
				IConfiguration cfg = ManagedBuildManager.getConfigurationForDescription(cfgDes);

				if (makeBuilderCmd != null)
					loadBuilderSettings(cfg, makeBuilderCmd);

				//				loadDiscoveryOptions(cfgDes, cfg);

				loadPathEntryInfo(project, cfgDes, data);

				if (binErrParserIds != null) {
					data.getTargetPlatformData().setBinaryParserIds(binErrParserIds);
					cfgDes.get(OLD_BINARY_PARSER_ID);
					//					ICConfigExtensionReference refs[] = cfgDes.get(OLD_BINARY_PARSER_ID);
					//					String ids[] = idsFromRefs(refs);
					//					data.getTargetPlatformData().setBinaryParserIds(ids);
					//
					//					refs = cfgDes.get(OLD_ERROR_PARSER_ID);
					//					ids = idsFromRefs(refs);
					//					data.getBuildData().setErrorParserIDs(ids);
				}

				try {
					ConfigurationDataProvider.writeConfiguration(cfgDes, data);
				} catch (CoreException e) {
				}
			}

			//			if(convertMakeTargetInfo){
			//				try {
			//					convertMakeTargetInfo(project, newDes, null);
			//				} catch (CoreException e){
			//					ManagedBuilderCorePlugin.log(e);
			//				}
			//			}

			if (changeEDes) {
				cmds = list.toArray(new ICommand[list.size()]);
				eDes.setBuildSpec(cmds);
			}

			info.setValid(true);

			try {
				ManagedBuildManager.setLoaddedBuildInfo(project, info);
			} catch (Exception e) {
			}
		}

		return newDes;
	}

	static void displayInfo(IProject proj, String id, String title, String message) {
		if (PROPS.getProperty(proj, id) == null) {
			openInformation(proj, id, title, message, false);
			PROPS.setProperty(proj, id, Boolean.TRUE);
		}
	}

	public static boolean getBooleanFromQueryAnswer(String answer) {
		if (IOverwriteQuery.ALL.equalsIgnoreCase(answer) || IOverwriteQuery.YES.equalsIgnoreCase(answer))
			return true;
		return false;
	}

	static public boolean openQuestion(final IResource rc, final String id, final String title, final String message,
			IOverwriteQuery query, final boolean multiple) {
		if (query != null)
			return getBooleanFromQueryAnswer(query.queryOverwrite(message));

		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window == null) {
			IWorkbenchWindow windows[] = PlatformUI.getWorkbench().getWorkbenchWindows();
			window = windows[0];
		}

		final Shell shell = window.getShell();
		final boolean[] answer = new boolean[1];
		shell.getDisplay().syncExec(new Runnable() {
			@Override
			public void run() {
				Object ob = PROPS.getProperty(rc, id);
				if (multiple || ob == null) {
					PROPS.setProperty(rc, id, Boolean.TRUE);
					answer[0] = MessageDialog.openQuestion(shell, title, message);
					PROPS.setProperty(rc, id, answer[0] ? Boolean.TRUE : Boolean.FALSE);
				} else {
					answer[0] = ((Boolean) ob).booleanValue();
				}
			}
		});
		return answer[0];
	}

	static private void openInformation(final IResource rc, final String id, final String title, final String message,
			final boolean multiple) {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window == null) {
			IWorkbenchWindow windows[] = PlatformUI.getWorkbench().getWorkbenchWindows();
			window = windows[0];
		}

		final Shell shell = window.getShell();
		shell.getDisplay().syncExec(new Runnable() {
			@Override
			public void run() {
				if (multiple || PROPS.getProperty(rc, id) == null) {
					PROPS.setProperty(rc, id, Boolean.TRUE);
					MessageDialog.openInformation(shell, title, message);
				}
			}
		});
	}

	private static void convertMakeTargetInfo(final IProject project, ICProjectDescription des,
			IProgressMonitor monitor) throws CoreException {
		if (monitor == null)
			monitor = new NullProgressMonitor();

		CCorePlugin.getDefault().getCDescriptorManager().runDescriptorOperation(project, des,
				new ICDescriptorOperation() {

					@Override
					public void execute(ICDescriptor descriptor, IProgressMonitor monitor) throws CoreException {
						final IMakeTargetManager mngr = MakeCorePlugin.getDefault().getTargetManager();

						project.accept(new IResourceVisitor() {

							@Override
							public boolean visit(IResource resource) throws CoreException {
								if (resource.getType() == IResource.FILE)
									return false;

								try {
									IContainer cr = (IContainer) resource;
									IMakeTarget targets[] = mngr.getTargets(cr);
									for (int i = 0; i < targets.length; i++) {
										IMakeTarget t = targets[i];
										if (!OLD_MAKE_TARGET_BUIDER_ID.equals(t.getTargetBuilderID()))
											continue;

										IMakeTarget newT = mngr.createTarget(project, t.getName(),
												NEW_MAKE_TARGET_BUIDER_ID);
										copySettings(t, newT);
										mngr.removeTarget(t);
										mngr.addTarget(cr, newT);
									}
								} catch (CoreException e) {
									ManagedBuilderCorePlugin.log(e);
								}
								return true;
							}

						});
					}

				}, monitor);
	}

	private static void copySettings(IMakeTarget fromTarget, IMakeTarget toTarget) throws CoreException {
		toTarget.setAppendEnvironment(fromTarget.appendEnvironment());
		toTarget.setAppendProjectEnvironment(fromTarget.appendProjectEnvironment());

		toTarget.setBuildAttribute(IMakeTarget.BUILD_LOCATION,
				fromTarget.getBuildAttribute(IMakeTarget.BUILD_LOCATION, null));
		toTarget.setBuildAttribute(IMakeTarget.BUILD_COMMAND,
				fromTarget.getBuildAttribute(IMakeTarget.BUILD_COMMAND, null));
		toTarget.setBuildAttribute(IMakeTarget.BUILD_ARGUMENTS,
				fromTarget.getBuildAttribute(IMakeTarget.BUILD_ARGUMENTS, null));
		toTarget.setBuildAttribute(IMakeTarget.BUILD_TARGET,
				fromTarget.getBuildAttribute(IMakeTarget.BUILD_TARGET, null));

		Map<String, String> fromMap = fromTarget.getEnvironment();
		if (fromMap != null)
			toTarget.setEnvironment(new HashMap<String, String>(fromMap));

		//			toTarget.setErrorParsers(fromTarget.getErrorParsers());

		toTarget.setRunAllBuilders(fromTarget.runAllBuilders());

		toTarget.setStopOnError(fromTarget.isStopOnError());

		toTarget.setUseDefaultBuildCmd(fromTarget.isDefaultBuildCmd());

		toTarget.setContainer(fromTarget.getContainer());

	}

	private void loadPathEntryInfo(IProject project, ICConfigurationDescription des, CConfigurationData data) {
		try {
			ICStorageElement el = des.getStorage(OLD_PATH_ENTRY_ID, false);
			if (el != null) {
				IPathEntry[] entries = PathEntryTranslator.decodePathEntries(project, el);
				if (entries.length != 0) {
					List<IPathEntry> list = new ArrayList<IPathEntry>(Arrays.asList(entries));
					for (Iterator<IPathEntry> iter = list.iterator(); iter.hasNext();) {
						IPathEntry entry = iter.next();
						if (entry.getEntryKind() == IPathEntry.CDT_CONTAINER) {
							iter.remove();
							continue;
						}
					}

					if (list.size() != 0) {
						PathEntryTranslator tr = new PathEntryTranslator(project, data);
						entries = list.toArray(new IPathEntry[list.size()]);
						ReferenceSettingsInfo refInfo = tr.applyPathEntries(entries, null,
								PathEntryTranslator.OP_REPLACE);
						ICExternalSetting extSettings[] = refInfo.getExternalSettings();
						des.removeExternalSettings();
						if (extSettings.length != 0) {
							ICExternalSetting setting;
							for (int i = 0; i < extSettings.length; i++) {
								setting = extSettings[i];
								des.createExternalSetting(setting.getCompatibleLanguageIds(),
										setting.getCompatibleContentTypeIds(), setting.getCompatibleExtensions(),
										setting.getEntries());
							}
						}

						IPath projPaths[] = refInfo.getReferencedProjectsPaths();
						if (projPaths.length != 0) {
							Map<String, String> map = new HashMap<String, String>(projPaths.length);
							for (int i = 0; i < projPaths.length; i++) {
								map.put(projPaths[i].segment(0), ""); //$NON-NLS-1$
							}
							des.setReferenceInfo(map);
						}
					}
				}
				des.removeStorage(OLD_PATH_ENTRY_ID);
			}
		} catch (CoreException e) {
			ManagedBuilderCorePlugin.log(e);
		}
	}

	//	private String[] idsFromRefs(ICConfigExtensionReference refs[]){
	//		String ids[] = new String[refs.length];
	//		for(int i = 0; i < ids.length; i++){
	//			ids[i] = refs[i].getID();
	//		}
	//		return ids;
	//	}

	//	private void loadDiscoveryOptions(ICConfigurationDescription des, IConfiguration cfg){
	//		try {
	//			ICStorageElement discoveryStorage = des.getStorage(OLD_DISCOVERY_MODULE_ID, false);
	//			if(discoveryStorage != null){
	//				Configuration config = (Configuration)cfg;
	//				IScannerConfigBuilderInfo2 scannerConfigInfo = ScannerConfigInfoFactory2.create(new CfgInfoContext(cfg), discoveryStorage, ScannerConfigProfileManager.NULL_PROFILE_ID);
	//				config.setPerRcTypeDiscovery(false);
	//				config.setScannerConfigInfo(scannerConfigInfo);
	//				des.removeStorage(OLD_DISCOVERY_MODULE_ID);
	//			}
	//		} catch (CoreException e) {
	//			ManagedBuilderCorePlugin.log(e);
	//		}
	//
	//
	//	}

	private void loadBuilderSettings(IConfiguration cfg, ICommand cmd) {
		Builder builder = (Builder) BuilderFactory.createBuilderFromCommand(cfg, cmd);
		if (builder.getCommand() != null && builder.getCommand().length() != 0) {
			String[] errParserIds = builder.getCustomizedErrorParserIds();
			builder.setCustomizedErrorParserIds(null);
			((ToolChain) cfg.getToolChain()).setBuilder(builder);
			if (errParserIds != null && errParserIds.length != 0) {
				cfg.setErrorParserList(errParserIds);
			}
		}
	}

	private static boolean convertOldStdMakeToNewStyle(final IProject project, boolean checkOnly,
			IProgressMonitor monitor, boolean throwExceptions) throws CoreException {
		try {
			//			ICDescriptor dr = CCorePlugin.getDefault().getCProjectDescription(project, false);
			//			if(dr == null){
			//				if(throwExceptions)
			//					throw new CoreException(new Status(IStatus.ERROR,
			//							ManagedBuilderCorePlugin.getUniqueIdentifier(),
			//							DataProviderMessages.getString("ProjectConverter.0"))); //$NON-NLS-1$
			//				return false;
			//			}
			//
			//			if(!MakeCorePlugin.MAKE_PROJECT_ID.equals(dr.getProjectOwner().getID())){
			//				if(throwExceptions)
			//					throw new CoreException(new Status(IStatus.ERROR,
			//							ManagedBuilderCorePlugin.getUniqueIdentifier(),
			//							DataProviderMessages.getString("ProjectConverter.1") + dr.getProjectOwner().getID())); //$NON-NLS-1$
			//				return false;
			//			}

			ICProjectDescription des = CCorePlugin.getDefault().getProjectDescription(project, false);

			if (des == null) {
				if (throwExceptions)
					throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
							DataProviderMessages.getString("ProjectConverter.9"))); //$NON-NLS-1$
				return false;
			}

			ICConfigurationDescription cfgs[] = des.getConfigurations();
			if (cfgs.length != 1) {
				if (throwExceptions)
					throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
							DataProviderMessages.getString("ProjectConverter.2") + cfgs.length)); //$NON-NLS-1$
				return false;
			}

			if (!CCorePlugin.DEFAULT_PROVIDER_ID.equals(cfgs[0].getBuildSystemId())) {
				if (throwExceptions)
					throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
							DataProviderMessages.getString("ProjectConverter.3") + cfgs.length)); //$NON-NLS-1$
				return false;
			}

			final IProjectDescription eDes = project.getDescription();
			String natureIds[] = eDes.getNatureIds();
			Set<String> set = new HashSet<String>(Arrays.asList(natureIds));
			if (!set.contains(OLD_MAKE_NATURE_ID)) {
				if (throwExceptions)
					throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
							DataProviderMessages.getString("ProjectConverter.4") + natureIds.toString())); //$NON-NLS-1$
				return false;
			}

			if (!checkOnly) {
				ProjectConverter instance = new ProjectConverter();
				ICProjectDescription oldDes = CCorePlugin.getDefault().getProjectDescription(project);
				if (!instance.canConvertProject(project, MakeCorePlugin.MAKE_PROJECT_ID, oldDes)) {
					if (throwExceptions)
						throw new CoreException(
								new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
										DataProviderMessages.getString("ProjectConverter.5"))); //$NON-NLS-1$
					return false;
				}

				final ICProjectDescription newDes = instance.convertProject(project, eDes,
						MakeCorePlugin.MAKE_PROJECT_ID, oldDes);
				if (newDes == null) {
					if (throwExceptions)
						throw new CoreException(
								new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
										DataProviderMessages.getString("ProjectConverter.6"))); //$NON-NLS-1$
					return false;
				}

				final IWorkspace wsp = ResourcesPlugin.getWorkspace();
				wsp.run(new IWorkspaceRunnable() {

					@Override
					public void run(IProgressMonitor monitor) throws CoreException {
						project.setDescription(eDes, monitor);
						CCorePlugin.getDefault().setProjectDescription(project, newDes);
						Job job = new Job(DataProviderMessages.getString("ProjectConverter.7")) { //$NON-NLS-1$

							@Override
							protected IStatus run(IProgressMonitor monitor) {
								try {
									ICProjectDescription des = CCorePlugin.getDefault().getProjectDescription(project);
									convertMakeTargetInfo(project, des, monitor);
									CCorePlugin.getDefault().setProjectDescription(project, des);
								} catch (CoreException e) {
									return e.getStatus();
								}
								return Status.OK_STATUS;
							}

						};

						job.setRule(wsp.getRoot());
						job.schedule();
					}

				}, wsp.getRoot(), IWorkspace.AVOID_UPDATE, monitor);
			}
			return true;
		} catch (CoreException e) {
			if (throwExceptions)
				throw e;
			ManagedBuilderCorePlugin.log(e);
		}
		if (throwExceptions)
			throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
					DataProviderMessages.getString("ProjectConverter.8"))); //$NON-NLS-1$
		return false;
	}

	public static boolean isOldStyleMakeProject(IProject project) {
		try {
			return convertOldStdMakeToNewStyle(project, true, null, false);
		} catch (CoreException e) {
			ManagedBuilderCorePlugin.log(e);
		}
		return false;
	}

	public static void convertOldStdMakeToNewStyle(IProject project, IProgressMonitor monitor) throws CoreException {
		convertOldStdMakeToNewStyle(project, false, monitor, true);
	}

	private IManagedBuildInfo convertManagedBuildInfo(IProject project, ICProjectDescription newDes)
			throws CoreException {
		IManagedBuildInfo info = ManagedBuildManager.getOldStyleBuildInfo(project);

		synchronized (PROPS) {
			if (info != null && info.isValid()) {
				IManagedProject mProj = info.getManagedProject();
				IConfiguration cfgs[] = mProj.getConfigurations();
				if (cfgs.length != 0) {
					Configuration cfg;
					CConfigurationData data;

					UserDefinedVariableSupplier usrSupplier = UserDefinedVariableSupplier.getInstance();

					for (int i = 0; i < cfgs.length; i++) {
						cfg = (Configuration) cfgs[i];
						data = cfg.getConfigurationData();
						//						try {
						ICConfigurationDescription cfgDes = newDes
								.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDER_ID, data);
						if (cfg.getConfigurationDescription() != null) {
							//copy cfg to avoid raise conditions
							cfg = ConfigurationDataProvider.copyCfg(cfg, cfgDes);
							cfgDes.setConfigurationData(ManagedBuildManager.CFG_DATA_PROVIDER_ID,
									cfg.getConfigurationData());
						}
						cfg.setConfigurationDescription(cfgDes);

						StorableCdtVariables vars = ((ToolChain) cfg.getToolChain()).getResetOldStyleProjectVariables();
						if (vars != null) {
							ICdtVariable vs[] = vars.getMacros();
							for (int k = 0; k < vs.length; k++) {
								usrSupplier.createMacro(vs[k], ICoreVariableContextInfo.CONTEXT_CONFIGURATION, cfgDes);
							}
						}
						//						} catch (WriteAccessException e) {
						//							ManagedBuilderCorePlugin.log(e);
						//						} catch (CoreException e) {
						//							ManagedBuilderCorePlugin.log(e);
						//						}
						cfg.exportArtifactInfo();
					}
				}
			} else {
				throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
						DataProviderMessages.getString("ProjectConverter.13"))); //$NON-NLS-1$
			}
		}
		return info;
	}

}

Back to the top