Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3c66105c2ed38b130de70a4a4b241d7c6a44ee5c (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
/*******************************************************************************
 * Copyright (c) 2009, 2011 Andrew Gvozdev 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 Gvozdev - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.managedbuilder.internal.language.settings.providers;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.AbstractExecutableExtensionBase;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.core.cdtvariables.ICdtVariableManager;
import org.eclipse.cdt.core.language.settings.providers.ILanguageSettingsBroadcastingProvider;
import org.eclipse.cdt.core.language.settings.providers.LanguageSettingsStorage;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICFileDescription;
import org.eclipse.cdt.core.settings.model.ICFolderDescription;
import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.ICPathEntry;
import org.eclipse.cdt.core.settings.model.ICResourceDescription;
import org.eclipse.cdt.core.settings.model.ICSettingBase;
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;

/**
 * Implementation of language settings provider for CDT Managed Build System.
 */
public class MBSLanguageSettingsProvider extends AbstractExecutableExtensionBase implements ILanguageSettingsBroadcastingProvider {
	@Override
	public List<ICLanguageSettingEntry> getSettingEntries(ICConfigurationDescription cfgDescription, IResource rc, String languageId) {

		IPath projectPath = rc.getProjectRelativePath();
		ICLanguageSetting[] languageSettings = null;

		if (rc instanceof IFile) {
			ICLanguageSetting ls = cfgDescription.getLanguageSettingForFile(projectPath, true);
			if (ls != null) {
				languageSettings = new ICLanguageSetting[] { ls };
			} else {
				return getSettingEntries(cfgDescription, rc.getParent(), languageId);
			}
		} else {
			ICResourceDescription rcDescription = cfgDescription.getResourceDescription(projectPath, false);
			languageSettings = getLanguageSettings(rcDescription);
		}

		List<ICLanguageSettingEntry> list = new ArrayList<ICLanguageSettingEntry>();

		if (languageSettings != null) {
			for (ICLanguageSetting langSetting : languageSettings) {
				if (langSetting != null) {
					String id = langSetting.getLanguageId();
					if (id == languageId || (id != null && id.equals(languageId))) {
						int kindsBits = langSetting.getSupportedEntryKinds();
						for (int kind=1; kind <= kindsBits; kind <<= 1) {
							if ((kindsBits & kind) != 0) {
								List<ICLanguageSettingEntry> additions = langSetting.getSettingEntriesList(kind);
								for (ICLanguageSettingEntry entry : additions) {
									if (entry instanceof ICPathEntry) {
										// have to use getName() rather than getLocation() and not use IPath operations to avoid collapsing ".."
										String pathStr = ((ICPathEntry) entry).getName();
										if (!new Path(pathStr).isAbsolute()) {
											// We need to add project-rooted entry for relative path as MBS counts it this way in some UI
											// The relative entry below also should be added for indexer to resolve from source file locations

											ICdtVariableManager varManager = CCorePlugin.getDefault().getCdtVariableManager();
											try {
												// Substitute build/environment variables
												String location = varManager.resolveValue(pathStr, "", null, cfgDescription); //$NON-NLS-1$
												if (!new Path(location).isAbsolute()) {
													IStringVariableManager mngr = VariablesPlugin.getDefault().getStringVariableManager();
													String projectRootedPath = mngr.generateVariableExpression("workspace_loc", rc.getProject().getName()) + Path.SEPARATOR + pathStr; //$NON-NLS-1$
													ICLanguageSettingEntry projectRootedEntry = (ICLanguageSettingEntry) CDataUtil.createEntry(kind, projectRootedPath, projectRootedPath, null, entry.getFlags());
													if (! list.contains(projectRootedEntry)) {
														list.add(projectRootedEntry);
													}
												}
											} catch (CdtVariableException e) {
												// Swallow exceptions but also log them
												ManagedBuilderCorePlugin.log(e);
											}
											
										}
									}
									if (! list.contains(entry)) {
										list.add(entry);
									}
								}
							}
						}
					}
				}
			}
		}
		return LanguageSettingsStorage.getPooledList(list);
	}

	/**
	 * Get language settings for resource description.
	 */
	private ICLanguageSetting[] getLanguageSettings(ICResourceDescription rcDescription) {
		ICLanguageSetting[] array = null;
		switch (rcDescription.getType()) {
		case ICSettingBase.SETTING_PROJECT:
		case ICSettingBase.SETTING_CONFIGURATION:
		case ICSettingBase.SETTING_FOLDER:
			ICFolderDescription foDes = (ICFolderDescription)rcDescription;
			array = foDes.getLanguageSettings();
			break;
		case ICSettingBase.SETTING_FILE:
			ICFileDescription fiDes = (ICFileDescription)rcDescription;
			ICLanguageSetting ls = fiDes.getLanguageSetting();
			if (ls != null) {
				array = new ICLanguageSetting[] { ls };
			}
		}
		if (array == null) {
			array = new ICLanguageSetting[0];
		}
		return array;
	}

	@Override
	public LanguageSettingsStorage copyStorage() {
		class PretendStorage extends LanguageSettingsStorage {
			@Override
			public boolean isEmpty() {
				return false;
			}
			@Override
			public LanguageSettingsStorage clone() throws CloneNotSupportedException {
				return this;
			}
			@Override
			public boolean equals(Object obj) {
				// Note that this always triggers change event even if nothing changed in MBS
				return false;
			}
		}
		return new PretendStorage();
	}

}

Back to the top