Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fbd6182c35b5793d26bd70c95f5d0d341ba23d26 (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
/*******************************************************************************
 * Copyright (c) 2007, 2009 Symbian 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:
 *     Andrew Ferguson (Symbian) - Initial implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.export;

import java.io.File;
import com.ibm.icu.text.MessageFormat;
import java.util.Map;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndexLocationConverter;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.export.IExportProjectProvider;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.pdom.WritablePDOM;
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;

/**
 * An ISafeRunnable which
 * <ul>
 * <li>Creates a project for export
 * <li>Exports the PDOM
 * <li>Writes new properties to the PDOM
 * <ul>
 */
public class GeneratePDOM {
	protected IExportProjectProvider pm;
	protected String[] applicationArguments;
	protected File targetLocation;
	protected String indexerID;
	protected boolean deleteOnExit;
	
	public GeneratePDOM(IExportProjectProvider pm, String[] applicationArguments, File targetLocation, String indexerID) {
		this.pm= pm;
		this.applicationArguments= applicationArguments;
		this.targetLocation= targetLocation;
		this.indexerID= indexerID;
	}
	
	/**
	 * When set, the project created by the associated {@link IExportProjectProvider} will
	 * be deleted after {@link #run()} completes. By default this is not set.
	 * @param deleteOnExit
	 */
	public void setDeleteOnExit(boolean deleteOnExit) {
		this.deleteOnExit= deleteOnExit;
	}

	/**
	 * Executes the PDOM generation 
	 * @return {@link IStatus#OK} if the generated content is complete, {@link IStatus#ERROR} otherwise.
	 * @throws CoreException if an internal or invalid configuration error occurs
	 */
	public final IStatus run() throws CoreException {
		boolean isContentSynced= false;
		
		// create the project
		pm.setApplicationArguments(applicationArguments);
		final ICProject cproject = pm.createProject();
		if(cproject==null) {
			fail(MessageFormat.format(Messages.GeneratePDOM_ProjectProviderReturnedNullCProject,
					new Object [] {pm.getClass().getName()}));
			return null; // cannot be reached, inform the compiler
		}
		
		IIndexLocationConverter converter= pm.getLocationConverter(cproject);
		if(converter==null) {
			fail(MessageFormat.format(Messages.GeneratePDOM_NullLocationConverter,
					new Object [] {pm.getClass().getName()}));
		}
		
		// index the project
		IndexerPreferences.set(cproject.getProject(), IndexerPreferences.KEY_INDEXER_ID, indexerID);
		
		try {
			final IIndexManager im = CCorePlugin.getIndexManager();
			for (int i = 0; i < 20; i++) {
				if(CCoreInternals.getPDOMManager().isProjectRegistered(cproject)) {
					im.joinIndexer(Integer.MAX_VALUE, new NullProgressMonitor());
					if (!im.isIndexerSetupPostponed(cproject)) {
						break;
					}
				}
				Thread.sleep(200);
			}
		
			// check status
			isContentSynced= CCoreInternals.getPDOMManager().isProjectContentSynced(cproject);
			
			if(isContentSynced) {
				// export a .pdom file
				CCoreInternals.getPDOMManager().exportProjectPDOM(cproject, targetLocation, converter);

				// write properties to exported PDOM
				WritablePDOM exportedPDOM= new WritablePDOM(targetLocation, converter, LanguageManager.getInstance().getPDOMLinkageFactoryMappings());
				exportedPDOM.acquireWriteLock(0);
				try {
					Map<String,String> exportProperties= pm.getExportProperties();
					if(exportProperties!=null) {
						for(Map.Entry<String,String> entry : exportProperties.entrySet()) {
							exportedPDOM.setProperty(entry.getKey(), entry.getValue());
						}
					}
					exportedPDOM.close();
				}
				finally {
					exportedPDOM.releaseWriteLock();
				}
			}
			
		} catch(InterruptedException ie) {
			String msg= MessageFormat.format(Messages.GeneratePDOM_GenericGenerationFailed, new Object[] {ie.getMessage()});
			throw new CoreException(CCorePlugin.createStatus(msg, ie));
		} finally {
			if(deleteOnExit) {
				cproject.getProject().delete(true, new NullProgressMonitor());
			}
		}
		
		return isContentSynced ?
			  new Status(IStatus.OK, CCorePlugin.PLUGIN_ID, Messages.GeneratePDOM_Success)
			: new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, Messages.GeneratePDOM_Incomplete);
	}
	
	private void fail(String message) throws CoreException {
		GeneratePDOMApplication.fail(message);
	}
}

Back to the top