Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 565ba82df294abcd274d6e6f3afe2a5c74066ae5 (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
/*******************************************************************************
 * Copyright (c) 2006, 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
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.buildmodel;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.managedbuilder.buildmodel.IBuildDescription;
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildIOType;
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildResource;
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildStep;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class BuildResource implements IBuildResource {
	private List<BuildIOType> fDepArgs = new ArrayList<BuildIOType>();
	private BuildIOType fProducerArg;
	private boolean fNeedsRebuild;
	private boolean fIsRemoved;
	private IPath fFullWorkspacePath;
	private boolean fIsProjectRc;
	private BuildDescription fInfo;
	private URI fLocationURI;

	protected BuildResource(BuildDescription info, IResource rc){
		this(info, info.calcResourceLocation(rc), rc.getLocationURI());
	}

	protected BuildResource(BuildDescription info, IPath fullWorkspacePath, URI locationURI){

		if(locationURI == null)
			throw new IllegalArgumentException(); // must point to somewhere!

		fLocationURI = locationURI;

		fFullWorkspacePath = fullWorkspacePath;
		fInfo = info;

		fIsProjectRc = (fullWorkspacePath != null);

		info.resourceCreated(this);

		if(DbgUtil.DEBUG)
			DbgUtil.trace("resource " + fullWorkspacePath + " created");	//$NON-NLS-1$	//$NON-NLS-2$
	}
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.builddescription.IBuildResource#getLocation()
	 */
	@Override
	public IPath getLocation() {
		if(fFullWorkspacePath == null) {
			return new Path(fLocationURI.getPath());
		}

		IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(fFullWorkspacePath);
		if(resource == null) {
			return new Path(fLocationURI.getPath());
		}

		if(resource.getLocation() != null)
			return resource.getLocation();
		else
			return new Path(fLocationURI.getPath());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.builddescription.IBuildResource#getFullPath()
	 */
	@Override
	public IPath getFullPath() {
		return fFullWorkspacePath;
		//return new Path(getLocationURI().getPath().toString());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.builddescription.IBuildResource#getProducerIOType()
	 */
	@Override
	public IBuildIOType getProducerIOType() {
		return fProducerArg;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.builddescription.IBuildResource#getDependentIOTypes()
	 */
	@Override
	public IBuildIOType[] getDependentIOTypes() {
		return fDepArgs.toArray(new BuildIOType[fDepArgs.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.builddescription.IBuildResource#needsRebuild()
	 */
	@Override
	public boolean needsRebuild() {
		return fNeedsRebuild;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.builddescription.IBuildResource#isRemoved()
	 */
	@Override
	public boolean isRemoved() {
		return fIsRemoved;
	}

	public void setRemoved(boolean removed) {
		if(DbgUtil.DEBUG){
			if(removed)
				DbgUtil.trace("REMOVED state: resource " + DbgUtil.resourceName(this)); //$NON-NLS-1$
		}
		fIsRemoved = removed;
		if(fIsRemoved)
			fNeedsRebuild = false;
	}

	public void setRebuildState(boolean rebuild){
		fNeedsRebuild = rebuild;
	}

	protected void addToArg(BuildIOType arg){
		if(arg.isInput()){
			fDepArgs.add(arg);
		} else {
			if(fProducerArg == null){
				fProducerArg = arg;
			} else if(fProducerArg.getStep() == fInfo.getInputStep()) {
				BuildStep inStep = (BuildStep)fInfo.getInputStep();
				inStep.removeResource(fProducerArg, this, true);
				fProducerArg = arg;
			} else {
				// Bug 461628
				// Check if this resource is an output produced by a Tool that support merging in which case
				// a producer can already be defined by a previous step. This is supported.
				
				IBuildStep step = fProducerArg.getStep();
				ITool tool = step instanceof BuildStep ? ((BuildStep)(step)).getTool() : null;
				if(tool != null && !arg.isInput()) {
					return;
				}
				String err = "ProducerArgument not null!!!\n";	//$NON-NLS-1$

				String rcName = DbgUtil.resourceName(this);
				String step1Name = DbgUtil.stepName(fProducerArg.getStep());
				String step2Name = DbgUtil.stepName(arg.getStep());
				String rcs[] = new String[]{rcName, step1Name, step2Name};

				String externalizedErr = BuildModelMessages.getFormattedString("BuildResource.0", rcs); //$NON-NLS-1$

				if(DbgUtil.DEBUG){
					err = err + externalizedErr + "curent producer: " + DbgUtil.dumpStep(fProducerArg.getStep()) + "\n producer attempt: " + DbgUtil.dumpStep(arg.getStep());	//$NON-NLS-1$	//$NON-NLS-2$
				}
				throw new IllegalArgumentException(externalizedErr);
			}
		}
	}

	void removeFromArg(BuildIOType arg){
		if(arg.isInput()){
			fDepArgs.remove(arg);
		} else {
			if(fProducerArg == arg){
				fProducerArg = null;
			}else
				throw new IllegalArgumentException("Resource is not produced by this arg!!!");	//$NON-NLS-1$
		}
	}

	@Override
	public boolean isProjectResource() {
		return fIsProjectRc;
	}

	BuildIOType[][] clear(){
		BuildIOType types[][] = new BuildIOType[2][];
		types[0] = new BuildIOType[1];
		types[0][0] = fProducerArg;
		BuildIOType outs[] = (BuildIOType[])getDependentIOTypes();
		types[1] = outs;

		if(fProducerArg != null)
			fProducerArg.removeResource(this);
		for(int i = 0; i < outs.length; i++){
			outs[i].removeResource(this);
		}

		return types;
	}

	BuildIOType[][] remove(){
		BuildIOType types[][] = clear();

		if(DbgUtil.DEBUG)
			DbgUtil.trace("resource " + DbgUtil.resourceName(this) + " removed");	//$NON-NLS-1$	//$NON-NLS-2$

		fInfo.resourceRemoved(this);
		fInfo = null;

		return types;
	}

	@Override
	public IBuildDescription getBuildDescription(){
		return fInfo;
	}

	@Override
	public IBuildStep[] getDependentSteps() {
		Set<IBuildStep> set = new HashSet<IBuildStep>();
		for(Iterator<BuildIOType> iter = fDepArgs.iterator(); iter.hasNext();){
			set.add(iter.next().getStep());
		}
		return set.toArray(new BuildStep[set.size()]);
	}

	@Override
	public IBuildStep getProducerStep() {
		if(fProducerArg != null)
			return fProducerArg.getStep();
		return null;
	}

	@Override
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append("BR "); //$NON-NLS-1$
		IPath fullPath = getFullPath();
		if(fullPath != null)
			buf.append("WSP|").append(fullPath); //$NON-NLS-1$
		else
			buf.append("FS|").append(getLocation()); //$NON-NLS-1$

		return buf.toString();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.buildmodel.IBuildResource#getLocationURI()
	 */
	@Override
	public URI getLocationURI() {
		return fLocationURI;
	}

}

Back to the top