Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a235eff1a00711e5fa2724d25c5b2b1de654b05c (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
/*******************************************************************************
 * 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.core;

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

import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IModificationStatus;
import org.eclipse.cdt.managedbuilder.core.IResourceInfo;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

public class ToolListModificationInfo {
	private ToolInfo[] fResultingTools;
	private ToolInfo[] fAddedTools;
	private ToolInfo[] fRemovedTools;
	private IResourceInfo fRcInfo;

	ToolListModificationInfo(IResourceInfo rcInfo, ToolInfo[] resultingTools, ToolInfo[] added, ToolInfo[] removed,
			ToolInfo[] remaining) {
		fResultingTools = resultingTools;
		fRemovedTools = removed;
		fAddedTools = added;
		fRcInfo = rcInfo;
	}

	public IResourceInfo getResourceInfo() {
		return fRcInfo;
	}

	public List<ITool> getResultingToolList(List<ITool> list) {
		if (list == null)
			list = new ArrayList<ITool>(fResultingTools.length);

		for (int i = 0; i < fResultingTools.length; i++) {
			list.add(fResultingTools[i].getResultingTool());
		}

		return list;
	}

	public ITool[] getResultingTools() {
		ITool[] tools = new ITool[fResultingTools.length];

		for (int i = 0; i < fResultingTools.length; i++) {
			tools[i] = fResultingTools[i].getResultingTool();
		}

		return tools;
	}

	public ITool[] getRemovedTools() {
		return toToolArray(fRemovedTools, true);
	}

	public ITool[] getAddedTools(boolean resulting) {
		return toToolArray(fAddedTools, !resulting);
	}

	public ITool[] getRemainedTools() {
		return toToolArray(fAddedTools, true);
	}

	private static ITool[] toToolArray(ToolInfo[] infos, boolean initialTools) {
		ITool[] tools = new ITool[infos.length];

		for (int i = 0; i < infos.length; i++) {
			tools[i] = initialTools ? infos[i].getInitialTool() : infos[i].getResultingTool();
		}

		return tools;
	}

	private static ITool[][] toToolArray(ToolInfo[][] infos, boolean initialTools) {
		ITool[][] tools = new ITool[infos.length][];

		for (int i = 0; i < infos.length; i++) {
			tools[i] = toToolArray(infos[i], initialTools);
		}

		return tools;
	}

	public MultiStatus getModificationStatus() {
		List<IModificationStatus> statusList = new ArrayList<IModificationStatus>();

		ToolInfo[][] conflictInfos = calculateConflictingTools(fResultingTools);
		ITool[][] conflicting = toToolArray(conflictInfos, true);

		Map<String, String> unspecifiedRequiredProps = new HashMap<String, String>();
		Map<String, String> unspecifiedProps = new HashMap<String, String>();
		Set<String> undefinedSet = new HashSet<String>();
		IConfiguration cfg = fRcInfo.getParent();
		ITool[] nonManagedTools = null;
		if (cfg.isManagedBuildOn() && cfg.supportsBuild(true)) {
			List<ITool> list = new ArrayList<ITool>();
			for (int i = 0; i < fResultingTools.length; i++) {
				if (!fResultingTools[i].getInitialTool().supportsBuild(true)) {
					list.add(fResultingTools[i].getInitialTool());
				}
			}
			if (list.size() != 0) {
				nonManagedTools = list.toArray(new Tool[list.size()]);
			}
		}

		IModificationStatus status = new ModificationStatus(unspecifiedRequiredProps, unspecifiedProps, undefinedSet,
				conflicting, nonManagedTools);

		if (status.getSeverity() != IStatus.OK)
			statusList.add(status);

		for (int i = 0; i < fResultingTools.length; i++) {
			status = fResultingTools[i].getModificationStatus();
			if (status.getSeverity() != IStatus.OK)
				statusList.add(status);
		}

		if (statusList.size() != 0)
			return new MultiStatus(ManagedBuilderCorePlugin.getUniqueIdentifier(), IStatus.INFO, "", null); //$NON-NLS-1$
		return new MultiStatus(ManagedBuilderCorePlugin.getUniqueIdentifier(), IStatus.ERROR, "", null); //$NON-NLS-1$
	}

	private ToolInfo[][] calculateConflictingTools(ToolInfo[] infos) {
		infos = filterInfos(infos);

		return doCalculateConflictingTools(infos);
	}

	private ToolInfo[] filterInfos(ToolInfo[] infos) {
		if (fRcInfo instanceof FolderInfo) {
			Map<ITool, ToolInfo> map = createInitialToolToToolInfoMap(infos);
			ITool[] tools = new ArrayList<ITool>(map.keySet()).toArray(new ITool[map.size()]);

			tools = ((FolderInfo) fRcInfo).filterTools(tools, fRcInfo.getParent().getManagedProject());

			if (tools.length < infos.length) {
				infos = new ToolInfo[tools.length];
				for (int i = 0; i < infos.length; i++) {
					infos[i] = map.get(tools[i]);
				}
			}
		}

		return infos;
	}

	private static Map<ITool, ToolInfo> createInitialToolToToolInfoMap(ToolInfo[] infos) {
		Map<ITool, ToolInfo> map = new LinkedHashMap<ITool, ToolInfo>();
		for (int i = 0; i < infos.length; i++) {
			map.put(infos[i].getInitialTool(), infos[i]);
		}

		return map;
	}

	private ToolInfo[][] doCalculateConflictingTools(ToolInfo[] infos) {
		HashSet<ToolInfo> set = new HashSet<ToolInfo>();
		set.addAll(Arrays.asList(infos));
		List<ToolInfo[]> result = new ArrayList<ToolInfo[]>();
		for (Iterator<ToolInfo> iter = set.iterator(); iter.hasNext();) {
			ToolInfo ti = iter.next();
			ITool t = ti.getInitialTool();
			iter.remove();
			@SuppressWarnings("unchecked")
			HashSet<ToolInfo> tmp = (HashSet<ToolInfo>) set.clone();
			List<ITool> list = new ArrayList<ITool>();
			for (Iterator<ToolInfo> tmpIt = tmp.iterator(); tmpIt.hasNext();) {
				ToolInfo otherTi = tmpIt.next();
				ITool other = otherTi.getInitialTool();
				String conflicts[] = getConflictingInputExts(t, other);
				if (conflicts.length != 0) {
					list.add(other);
					tmpIt.remove();
				}
			}

			if (list.size() != 0) {
				list.add(t);
				ToolInfo[] arr = list.toArray(new ToolInfo[list.size()]);
				result.add(arr);
			}
			set = tmp;
			iter = set.iterator();
		}

		return result.toArray(new ToolInfo[result.size()][]);
	}

	private String[] getConflictingInputExts(ITool tool1, ITool tool2) {
		IProject project = fRcInfo.getParent().getOwner().getProject();
		String ext1[] = ((Tool) tool1).getAllInputExtensions(project);
		String ext2[] = ((Tool) tool2).getAllInputExtensions(project);
		Set<String> set1 = new HashSet<String>(Arrays.asList(ext1));
		Set<String> result = new HashSet<String>();
		for (int i = 0; i < ext2.length; i++) {
			if (set1.remove(ext2[i]))
				result.add(ext2[i]);
		}
		return result.toArray(new String[result.size()]);
	}

	public void apply() {
		((ResourceInfo) fRcInfo).doApply(this);
	}
}

Back to the top