Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 787f35f79796d8f9901d731468ae7b96c195183e (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
/*******************************************************************************
 * Copyright (c) 2007, 2016 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
 * IBM Corporation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.core;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.settings.model.ICSettingBase;
import org.eclipse.cdt.core.settings.model.ICStorageElement;
import org.eclipse.cdt.core.settings.model.extension.CResourceData;
import org.eclipse.cdt.internal.core.SafeStringInterner;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IBuildObject;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IFileInfo;
import org.eclipse.cdt.managedbuilder.core.IFolderInfo;
import org.eclipse.cdt.managedbuilder.core.IHoldsOptions;
import org.eclipse.cdt.managedbuilder.core.IManagedConfigElement;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IResourceConfiguration;
import org.eclipse.cdt.managedbuilder.core.IResourceInfo;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.core.OptionStringValue;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

public abstract class ResourceInfo extends BuildObject implements IResourceInfo {
	private Configuration config;
	private IPath path;
	boolean isDirty;
	boolean needsRebuild;
	private ResourceInfoContainer rcInfo;
	private CResourceData resourceData;

	ResourceInfo(IConfiguration cfg, IManagedConfigElement element, boolean hasBody) {
		config = (Configuration) cfg;
		if (hasBody)
			loadFromManifest(element);
	}

	ResourceInfo(IConfiguration cfg, ResourceInfo base, String id) {
		config = (Configuration) cfg;
		path = normalizePath(base.path);

		setId(id);
		setName(base.getName());

		if (id.equals(base.getId())) {
			isDirty = base.isDirty;
			needsRebuild = base.needsRebuild;
		} else {
			needsRebuild = true;
			isDirty = true;
		}
	}

	public boolean isRoot() {
		return path.segmentCount() == 0;
	}

	ResourceInfo(IConfiguration cfg, IPath path, String id, String name) {
		config = (Configuration) cfg;
		path = normalizePath(path);
		this.path = path;

		setId(id);
		setName(name);
	}

	ResourceInfo(IFileInfo base, IPath path, String id, String name) {
		config = (Configuration) base.getParent();

		setId(id);
		setName(name);
		path = normalizePath(path);

		this.path = path;
		needsRebuild = true;
		isDirty = true;
	}

	ResourceInfo(FolderInfo base, IPath path, String id, String name) {
		config = (Configuration) base.getParent();

		setId(id);
		setName(name);
		path = normalizePath(path);

		this.path = path;
		needsRebuild = true;
		isDirty = true;
	}

	ResourceInfo(IConfiguration cfg, ICStorageElement element, boolean hasBody) {
		config = (Configuration) cfg;
		if (hasBody)
			loadFromProject(element);
	}

	private void loadFromManifest(IManagedConfigElement element) {

		// id
		setId(SafeStringInterner.safeIntern(element.getAttribute(ID)));

		// Get the name
		setName(SafeStringInterner.safeIntern(element.getAttribute(NAME)));

		// resourcePath
		String tmp = element.getAttribute(RESOURCE_PATH);
		if (tmp != null) {
			path = new Path(tmp);
			if (IResourceConfiguration.RESOURCE_CONFIGURATION_ELEMENT_NAME.equals(element.getName())) {
				path = path.removeFirstSegments(1);
			}
			path = normalizePath(path);
		} else {
			Status status = new Status(IStatus.ERROR, ManagedBuilderCorePlugin.PLUGIN_ID,
					"ResourceInfo.loadFromManifest() : resourcePath=NULL", null); //$NON-NLS-1$
			ManagedBuilderCorePlugin.log(status);
		}

		// exclude
		String excludeStr = element.getAttribute(EXCLUDE);
		if (excludeStr != null) {
			config.setExcluded(getPath(), isFolderInfo(), (Boolean.parseBoolean(excludeStr)));
		}
	}

	private void loadFromProject(ICStorageElement element) {

		// id (unique, do not intern)
		setId(element.getAttribute(ID));

		// name
		if (element.getAttribute(NAME) != null) {
			setName(SafeStringInterner.safeIntern(element.getAttribute(NAME)));
		}

		// resourcePath
		if (element.getAttribute(RESOURCE_PATH) != null) {
			String tmp = element.getAttribute(RESOURCE_PATH);
			if (tmp != null) {
				path = new Path(tmp);
				if (IResourceConfiguration.RESOURCE_CONFIGURATION_ELEMENT_NAME.equals(element.getName())) {
					path = path.removeFirstSegments(1);
				}
				path = normalizePath(path);
			} else {
				Status status = new Status(IStatus.ERROR, ManagedBuilderCorePlugin.PLUGIN_ID,
						"ResourceInfo.loadFromProject() : resourcePath=NULL", null); //$NON-NLS-1$
				ManagedBuilderCorePlugin.log(status);
			}
		}

		// exclude
		if (element.getAttribute(EXCLUDE) != null) {
			String excludeStr = element.getAttribute(EXCLUDE);
			if (excludeStr != null) {
				config.setExcluded(getPath(), isFolderInfo(), (Boolean.parseBoolean(excludeStr)));
			}
		}
	}

	@Override
	public IConfiguration getParent() {
		return config;
	}

	@Override
	public IPath getPath() {
		return normalizePath(path);
	}

	@Override
	public boolean isDirty() {
		return isDirty;
	}

	@Override
	public boolean isExcluded() {
		return config.isExcluded(getPath());
	}

	@Override
	public boolean needsRebuild() {
		return needsRebuild;
	}

	@Override
	public void setDirty(boolean dirty) {
		isDirty = dirty;
	}

	@Override
	public void setExclude(boolean excluded) {
		if (isExcluded() == excluded)
			return;

		config.setExcluded(getPath(), isFolderInfo(), excluded);

		setDirty(true);
		setRebuildState(true);
	}

	@Override
	public boolean canExclude(boolean exclude) {
		return config.canExclude(getPath(), isFolderInfo(), exclude);
	}

	public abstract boolean isFolderInfo();

	@Override
	public void setPath(IPath p) {
		p = normalizePath(p);
		if (path == null)
			path = p;
		else if (!p.equals(normalizePath(this.path))) {
			ResourceInfoContainer info = getRcInfo();
			info.changeCurrentPath(p, true);
			this.path = p;
			setDirty(true);
			setRebuildState(true);
		}

	}

	private ResourceInfoContainer getRcInfo() {
		if (rcInfo == null)
			rcInfo = (config).getRcInfoContainer(this);
		return rcInfo;
	}

	@Override
	public void setRebuildState(boolean rebuild) {
		needsRebuild = rebuild;
	}

	void serialize(ICStorageElement element) {
		element.setAttribute(IBuildObject.ID, id);

		if (name != null) {
			element.setAttribute(IBuildObject.NAME, name);
		}

		if (path != null) {
			element.setAttribute(IResourceInfo.RESOURCE_PATH, path.toString());
		}
	}

	void resolveReferences() {
	}

	@Override
	public CResourceData getResourceData() {
		return resourceData;
	}

	protected void setResourceData(CResourceData data) {
		resourceData = data;
	}

	void removed() {
		config = null;
	}

	@Override
	public boolean isValid() {
		return config != null;
	}

	private void propagate(IHoldsOptions parent, IOption option, Object oldValue, Object value) {
		if (!(parent instanceof ITool))
			return;
		ITool tool = (ITool) parent;
		String sup = option.getId();
		IOption op = option;
		while (op.getSuperClass() != null) {
			op = op.getSuperClass();
			sup = op.getId();
		}
		for (IResourceInfo ri : getChildResourceInfos()) {
			for (ITool t : ri.getTools()) {
				if (t.getDefaultInputExtension() != tool.getDefaultInputExtension())
					continue;
				op = t.getOptionBySuperClassId(sup);
				if (op == null)
					continue;
				try {
					if (value instanceof Boolean) {
						boolean b = ((Boolean) oldValue).booleanValue();
						if (b == op.getBooleanValue() && b != ((Boolean) value).booleanValue())
							ri.setOption(t, op, ((Boolean) value).booleanValue());
					} else if (value instanceof String) {
						String s = (String) oldValue;
						if (s.equals(op.getStringValue()) && !s.equals(value))
							ri.setOption(t, op, (String) value);
					} else if (value instanceof String[]) {
						String[] s = (String[]) oldValue;
						if (Arrays.equals(s, op.getStringListValue()) && !Arrays.equals(s, (String[]) value))
							ri.setOption(t, op, (String[]) value);
					} else if (value instanceof OptionStringValue[]) {
						OptionStringValue[] s = (OptionStringValue[]) oldValue;
						if (Arrays.equals(s, op.getBasicStringListValueElements())
								&& !Arrays.equals(s, (OptionStringValue[]) value))
							ri.setOption(t, op, (OptionStringValue[]) value);
					}
					break;
				} catch (BuildException e) {
				}
			}
		}

	}

	@Override
	public IOption setOption(IHoldsOptions parent, IOption option, boolean value) throws BuildException {
		// Is there a change?
		IOption retOpt = option;
		boolean oldVal = option.getBooleanValue();
		if (oldVal != value) {
			retOpt = parent.getOptionToSet(option, false);
			retOpt.setValue(value);
			propagate(parent, option, (oldVal ? Boolean.TRUE : Boolean.FALSE), (value ? Boolean.TRUE : Boolean.FALSE));
			NotificationManager.getInstance().optionChanged(this, parent, option, Boolean.valueOf(oldVal));
		}
		return retOpt;
	}

	@Override
	public IOption setOption(IHoldsOptions parent, IOption option, String value) throws BuildException {
		IOption retOpt = option;
		String oldValue;
		oldValue = option.getStringValue();
		if (oldValue != null && !oldValue.equals(value)) {
			retOpt = parent.getOptionToSet(option, false);
			retOpt.setValue(value);
			propagate(parent, option, oldValue, value);
			NotificationManager.getInstance().optionChanged(this, parent, option, oldValue);
		}
		return retOpt;
	}

	@Override
	public IOption setOption(IHoldsOptions parent, IOption option, String[] value) throws BuildException {
		IOption retOpt = option;
		// Is there a change?
		String[] oldValue;
		switch (option.getBasicValueType()) {
		case IOption.STRING_LIST:
			oldValue = option.getBasicStringListValue();
			break;
		default:
			oldValue = new String[0];
			break;
		}
		if (!Arrays.equals(value, oldValue)) {
			retOpt = parent.getOptionToSet(option, false);
			retOpt.setValue(value);
			propagate(parent, option, oldValue, value);
			NotificationManager.getInstance().optionChanged(this, parent, option, oldValue);
		}
		return retOpt;
	}

	@Override
	public IOption setOption(IHoldsOptions parent, IOption option, OptionStringValue[] value) throws BuildException {
		IOption retOpt = option;
		// Is there a change?
		OptionStringValue[] oldValue;
		switch (option.getBasicValueType()) {
		case IOption.STRING_LIST:
			oldValue = ((Option) option).getBasicStringListValueElements();
			break;
		default:
			oldValue = new OptionStringValue[0];
			break;
		}
		if (!Arrays.equals(value, oldValue)) {
			retOpt = parent.getOptionToSet(option, false);
			((Option) retOpt).setValue(value);
			propagate(parent, option, oldValue, value);
			NotificationManager.getInstance().optionChanged(this, parent, option, oldValue);
		}
		return retOpt;
	}

	public void propertiesChanged() {
		if (isExtensionElement())
			return;

		ITool tools[] = getTools();
		for (ITool tool : tools) {
			((Tool) tool).propertiesChanged();
		}
	}

	@Override
	public abstract boolean isExtensionElement();

	public abstract Set<String> contributeErrorParsers(Set<String> set);

	protected Set<String> contributeErrorParsers(ITool[] tools, Set<String> set) {
		for (ITool tool : tools) {
			set = ((Tool) tool).contributeErrorParsers(set);
		}
		return set;
	}

	public abstract void resetErrorParsers();

	protected void resetErrorParsers(ITool tools[]) {
		for (ITool tool : tools) {
			((Tool) tool).resetErrorParsers();
		}
	}

	abstract void removeErrorParsers(Set<String> set);

	protected void removeErrorParsers(ITool tools[], Set<String> set) {
		for (ITool tool : tools) {
			((Tool) tool).removeErrorParsers(set);
		}
	}

	public ITool getToolById(String id) {
		ITool[] tools = getTools();
		for (ITool tool : tools) {
			if (id.equals(tool.getId()))
				return tool;
		}
		return null;
	}

	public static IPath normalizePath(IPath path) {
		return path.makeRelative();
	}

	public ResourceInfo getParentResourceInfo() {
		if (isRoot())
			return null;

		IPath path = getPath();
		path = path.removeLastSegments(1);
		return (ResourceInfo) getParent().getResourceInfo(path, false);
	}

	public IFolderInfo getParentFolderInfo() {
		ResourceInfo parentRc = getParentResourceInfo();
		for (; parentRc != null && !parentRc.isFolderInfo(); parentRc = parentRc.getParentResourceInfo()) {
			// empty body, loop is to find parent only
		}

		return (IFolderInfo) parentRc;
	}

	abstract void resolveProjectReferences(boolean onLoad);

	abstract public boolean hasCustomSettings();

	public ToolListModificationInfo getToolListModificationInfo(ITool[] tools) {
		ITool[] curTools = getTools();
		return ToolChainModificationHelper.getModificationInfo(this, curTools, tools);
	}

	static ITool[][] getRealPairs(ITool[] tools) {
		ITool[][] pairs = new ITool[tools.length][];
		for (int i = 0; i < tools.length; i++) {
			ITool[] pair = new ITool[2];
			pair[0] = ManagedBuildManager.getRealTool(tools[i]);
			if (pair[0] == null)
				pair[0] = tools[i];
			pair[1] = tools[i];
			pairs[i] = pair;
		}
		return pairs;
	}

	abstract void applyToolsInternal(ITool[] resultingTools, ToolListModificationInfo info);

	void doApply(ToolListModificationInfo info) {
		ITool[] resulting = info.getResultingTools();

		ITool[] removed = info.getRemovedTools();

		BuildSettingsUtil.disconnectDepentents(getParent(), removed);

		applyToolsInternal(resulting, info);

		performPostModificationAdjustments(info);
	}

	void performPostModificationAdjustments(ToolListModificationInfo info) {
		propertiesChanged();
	}

	public IResourceInfo[] getDirectChildResourceInfos() {
		ResourceInfoContainer cr = getRcInfo();
		return cr.getDirectChildResourceInfos();
	}

	public IResourceInfo[] getChildResourceInfos() {
		ResourceInfoContainer cr = getRcInfo();
		return cr.getResourceInfos();
	}

	public List<IResourceInfo> getChildResourceInfoList(boolean includeCurrent) {
		return getRcInfo().getRcInfoList(ICSettingBase.SETTING_FILE | ICSettingBase.SETTING_FOLDER, includeCurrent);
	}

}

Back to the top