Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5058cc449122d09e9601d7aec68beb475ee6fea0 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 IBM Corporation 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:
 * IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.makegen.gnu;

import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IFolderInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedCommandLineGenerator;
import org.eclipse.cdt.managedbuilder.core.IManagedCommandLineInfo;
import org.eclipse.cdt.managedbuilder.core.IResourceConfiguration;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.internal.macros.FileContextData;
import org.eclipse.cdt.managedbuilder.macros.BuildMacroException;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider;
import org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderMakefileGenerator;
import org.eclipse.cdt.managedbuilder.makegen.IManagedDependencyGenerator;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;

/**
 * @since 2.0
 */
public class DefaultGCCDependencyCalculator implements IManagedDependencyGenerator {

	private static final String EMPTY_STRING = new String();
	private static final String[] EMPTY_STRING_ARRAY = new String[0];
	public final String WHITESPACE = " ";	//$NON-NLS-1$

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderDependencyCalculator#findDependencies(org.eclipse.core.resources.IResource)
	 */
	public IResource[] findDependencies(IResource resource, IProject project) {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderDependencyCalculator#getCalculatorType()
	 */
	public int getCalculatorType() {
		return TYPE_COMMAND;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderDependencyCalculator#getDependencyCommand()
	 */
	public String getDependencyCommand(IResource resource, IManagedBuildInfo info) {
		/*
		 * For a given input, <path>/<resource_name>.<ext>, return a string containing
		 * 	echo -n $(@:%.<out_ext>=%.d) '<path>/' >> $(@:%.<out_ext>=%.d) && \
		 * 	<tool_command> -P -MM -MG <tool_flags> $< >> $(@:%.<out_ext>=%.d)
		 * 
		 */
		StringBuffer buffer = new StringBuffer();

		// Get what we need to create the dependency generation command
		IConfiguration config = info.getDefaultConfiguration();

		//	We need to check whether we have any resource specific build information.
		IResourceConfiguration resConfig = null;
		if( config != null ) resConfig = config.getResourceConfiguration(resource.getFullPath().toString());

		String inputExtension = resource.getFileExtension();
		String outputExtension = info.getOutputExtension(inputExtension);
		
		// Work out the build-relative path for the output files
		IContainer resourceLocation = resource.getParent();
		String relativePath = new String();
		if (resourceLocation != null) {
			relativePath += resourceLocation.getProjectRelativePath().toString();
		}
		if (relativePath.length() > 0) {
			relativePath +=  IManagedBuilderMakefileGenerator.SEPARATOR;
		}
		
		// Calculate the dependency rule
		// <path>/$(@:%.<out_ext>=%.d)
		String depRule = "'$(@:%." + //$NON-NLS-1$
			outputExtension + 
			"=%." + //$NON-NLS-1$
			IManagedBuilderMakefileGenerator.DEP_EXT + 
			")'"; //$NON-NLS-1$
		
		// Add the rule that will actually create the right format for the dep 
		buffer.append(IManagedBuilderMakefileGenerator.TAB + 
				IManagedBuilderMakefileGenerator.ECHO + 
				IManagedBuilderMakefileGenerator.WHITESPACE + 
				"-n" + //$NON-NLS-1$
				IManagedBuilderMakefileGenerator.WHITESPACE +
				depRule + 
				IManagedBuilderMakefileGenerator.WHITESPACE + 
				"$(dir $@)" + //$NON-NLS-1$
				IManagedBuilderMakefileGenerator.WHITESPACE + 
				">" + //$NON-NLS-1$ 
				IManagedBuilderMakefileGenerator.WHITESPACE + 
				depRule + 
				IManagedBuilderMakefileGenerator.WHITESPACE + 
				IManagedBuilderMakefileGenerator.LOGICAL_AND + 
				IManagedBuilderMakefileGenerator.WHITESPACE + 
				IManagedBuilderMakefileGenerator.LINEBREAK);
		
		// Add the line that will do the work to calculate dependencies
		IManagedCommandLineInfo cmdLInfo = null;
		String buildCmd = null;
		String[] inputs= new String[1]; inputs[0] = IManagedBuilderMakefileGenerator.IN_MACRO;
		String outflag = "";  //$NON-NLS-1$
		String outputPrefix = "";   //$NON-NLS-1$
		String outputFile = "";   //$NON-NLS-1$
		ITool[] tools;
		if( resConfig != null && (tools = resConfig.getToolsToInvoke()) != null && tools.length > 0) {
			ITool tool = tools[0];
			String cmd = tool.getToolCommand();
			//try to resolve the build macros in the tool command
			try {
				String resolvedCommand = null;

				// does the resource have spaces in its name?
				if (resource.getProjectRelativePath().toString().indexOf(" ") != -1) { //$NON-NLS-1$
					// use fully qualified strings
					resolvedCommand = ManagedBuildManager
							.getBuildMacroProvider()
							.resolveValueToMakefileFormat(
									cmd,
									"", //$NON-NLS-1$
									" ", //$NON-NLS-1$
									IBuildMacroProvider.CONTEXT_FILE,
									new FileContextData(resource.getLocation(),
											null, null, tool));
				} else {
					// use builder variables
					resolvedCommand = ManagedBuildManager
							.getBuildMacroProvider()
							.resolveValueToMakefileFormat(
									cmd,
									"", //$NON-NLS-1$
									" ", //$NON-NLS-1$
									IBuildMacroProvider.CONTEXT_FILE,
									new FileContextData(resource.getLocation(),
											null, null, tool));
				}
				
				if((resolvedCommand = resolvedCommand.trim()).length() > 0)
					cmd = resolvedCommand;
					
			} catch (BuildMacroException e){
			}

			String[] toolFlags = null;
			try { 
				toolFlags = tool.getToolCommandFlags(resource.getLocation(),null);
			} catch( BuildException ex ) {
				// TODO add some routines to catch this
				toolFlags = EMPTY_STRING_ARRAY;
			}
			String[] flags = new String[toolFlags.length + 4];
			flags[0] = "-MM";  //$NON-NLS-1$
			flags[1] = "-MG";  //$NON-NLS-1$
			flags[2] = "-P";   //$NON-NLS-1$
			flags[3] = "-w";   //$NON-NLS-1$
			for (int i=0; i<toolFlags.length; i++) {
				flags[4+i] = toolFlags[i];
			}
			IManagedCommandLineGenerator cmdLGen = tool.getCommandLineGenerator();
			cmdLInfo = cmdLGen.generateCommandLineInfo( tool, cmd, flags, outflag, outputPrefix,
					outputFile, inputs, tool.getCommandLinePattern() );
			buildCmd = cmdLInfo.getCommandLine();
			
			// resolve any remaining macros in the command after it has been generated
			try {
				String resolvedCommand = null;

				// does the resource have spaces in its name?
				if (resource.getProjectRelativePath().toString().indexOf(" ") != -1) { //$NON-NLS-1$
					// use fully qualified strings
					resolvedCommand = ManagedBuildManager
							.getBuildMacroProvider()
							.resolveValueToMakefileFormat(
									buildCmd,
									"", //$NON-NLS-1$
									" ", //$NON-NLS-1$
									IBuildMacroProvider.CONTEXT_FILE,
									new FileContextData(resource.getLocation(),
											null, null, tool));
				} else {
					// use builder variables
					resolvedCommand = ManagedBuildManager
							.getBuildMacroProvider()
							.resolveValueToMakefileFormat(
									buildCmd,
									"", //$NON-NLS-1$
									" ", //$NON-NLS-1$
									IBuildMacroProvider.CONTEXT_FILE,
									new FileContextData(resource.getLocation(),
											null, null, tool));
				}
				if((resolvedCommand = resolvedCommand.trim()).length() > 0)
					buildCmd = resolvedCommand;
					
			} catch (BuildMacroException e){
			}
			
			
		} else {
			ITool tool = null;
			IFolderInfo foInfo = (IFolderInfo)config.getResourceInfo(resource.getProjectRelativePath(), false);
			tools = foInfo.getFilteredTools();
			for (int index = 0; index < tools.length; index++) {
				ITool tmp = tools[index];
				if (tmp.buildsFileType(inputExtension)) {
					tool = tmp;
					break;
				}
			}
			String cmd = tool != null ? tool.getToolCommand() : null;

			//try to resolve the build macros in the tool command
			try{
				String resolvedCommand = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(cmd,
						"", //$NON-NLS-1$
						" ", //$NON-NLS-1$
						IBuildMacroProvider.CONTEXT_FILE,
						new FileContextData(resource.getLocation(),null,null,tool));
				if((resolvedCommand = resolvedCommand.trim()).length() > 0)
					cmd = resolvedCommand;
					
			} catch (BuildMacroException e){
			}

			String buildFlags = "-MM -MG -P -w " + info.getToolFlagsForSource(inputExtension, resource.getLocation(), null); //$NON-NLS-1$
			String[] flags = buildFlags.split( "\\s" ); //$NON-NLS-1$
			cmdLInfo = info.generateToolCommandLineInfo( inputExtension, flags, outflag, outputPrefix, 
					outputFile, inputs, resource.getLocation(), null);
			// The command to build
			if( cmdLInfo == null ) buildCmd = 
				cmd + 
				IManagedBuilderMakefileGenerator.WHITESPACE +
				"-MM -MG -P -w " +  //$NON-NLS-1$
				buildFlags + 
				IManagedBuilderMakefileGenerator.WHITESPACE + 
				IManagedBuilderMakefileGenerator.IN_MACRO;
			else {
				buildCmd = cmdLInfo.getCommandLine();
			}
            
            // resolve any remaining macros in the command after it has been
            // generated
			try {
				String resolvedCommand = null;

				// does the resource have spaces in its name?
				if (resource.getProjectRelativePath().toString().indexOf(" ") != -1) { //$NON-NLS-1$
					// use fully qualified strings
					resolvedCommand = ManagedBuildManager
							.getBuildMacroProvider()
							.resolveValueToMakefileFormat(
									buildCmd,
									"", //$NON-NLS-1$
									" ", //$NON-NLS-1$
									IBuildMacroProvider.CONTEXT_FILE,
									new FileContextData(resource.getLocation(),
											null, null, tool));
				} else {
					// use builder variables
					resolvedCommand = ManagedBuildManager
							.getBuildMacroProvider()
							.resolveValueToMakefileFormat(
									buildCmd,
									"", //$NON-NLS-1$
									" ", //$NON-NLS-1$
									IBuildMacroProvider.CONTEXT_FILE,
									new FileContextData(resource.getLocation(),
											null, null, tool));
				}
                if ((resolvedCommand = resolvedCommand.trim()).length() > 0)
                    buildCmd = resolvedCommand;

            } catch (BuildMacroException e) {
            }
		}

		buffer.append(IManagedBuilderMakefileGenerator.TAB + 
				buildCmd +
				IManagedBuilderMakefileGenerator.WHITESPACE + 
				">>" +  //$NON-NLS-1$
				IManagedBuilderMakefileGenerator.WHITESPACE + depRule );

		return buffer.toString();
	}

}

Back to the top