Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2d5ae707461bf72759c9739206f9a8f533d32b71 (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
/**********************************************************************
 * Copyright (c) 2006, 2011 QNX Software Systems 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:
 *     QNX Software Systems - Initial API and implementation
 *     Anton Leherbauer (Wind River Systems)
 *     Markus Schorn (Wind River Systems)
 **********************************************************************/

package org.eclipse.cdt.managedbuilder.pdomdepgen;

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

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexInclude;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IndexLocationFactory;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.managedbuilder.core.IBuildObject;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.makegen.IManagedDependencyCalculator;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;

/**
 * @author Doug Schaefer
 *
 * @noextend This class is not intended to be subclassed by clients.
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public class PDOMDependencyCalculator implements IManagedDependencyCalculator {

	private final IPath source;
	private final IResource resource;
	private final IBuildObject buildContext;
	private final ITool tool;
	private final IPath topBuildDirectory;
	private IPath[] dependencies;

	public PDOMDependencyCalculator(IPath source, IResource resource, IBuildObject buildContext, ITool tool,
			IPath topBuildDirectory) {
		this.source = source;
		this.resource = resource;
		this.buildContext = buildContext;
		this.tool = tool;
		this.topBuildDirectory = topBuildDirectory;
	}

	@Override
	public IPath[] getAdditionalTargets() {
		return null;
	}

	@Override
	public IPath[] getDependencies() {
		if (dependencies == null) {
			if (resource != null) {
				ICProject project = CoreModel.getDefault().create(resource.getProject());
				try {
					IIndex index = CCorePlugin.getIndexManager().getIndex(project, IIndexManager.ADD_DEPENDENCIES);
					index.acquireReadLock();
					try {
						IIndexFile[] files = index.getFiles(IndexLocationFactory.getWorkspaceIFL((IFile) resource));
						if (files.length > 0) {
							IIndexInclude[] includes = index.findIncludes(files[0], IIndex.DEPTH_INFINITE);

							List<IPath> list = new ArrayList<IPath>();
							for (IIndexInclude inc : includes) {
								if (inc.isResolved()) {
									list.add(IndexLocationFactory.getAbsolutePath(inc.getIncludesLocation()));
								}
							}
							dependencies = list.toArray(new IPath[list.size()]);
						} else
							dependencies = new IPath[0];
					} finally {
						index.releaseReadLock();
					}
				} catch (CoreException e) {
					//					Activator.getDefault().getLog().log(e.getStatus());
					dependencies = new IPath[0];
				} catch (InterruptedException e) {
					dependencies = new IPath[0];
				}
			} else
				dependencies = new IPath[0];
		}

		return dependencies;
	}

	@Override
	public IBuildObject getBuildContext() {
		return buildContext;
	}

	@Override
	public IPath getSource() {
		return source;
	}

	@Override
	public ITool getTool() {
		return tool;
	}

	@Override
	public IPath getTopBuildDirectory() {
		return topBuildDirectory;
	}

}

Back to the top