7
7
import java .util .HashSet ;
8
8
import java .util .LinkedHashMap ;
9
9
import java .util .LinkedHashSet ;
10
- import java .util .List ;
11
10
import java .util .Map ;
12
11
import java .util .Set ;
13
12
14
13
import org .eclipse .cdt .managedbuilder .core .IConfiguration ;
15
- import org .eclipse .cdt .managedbuilder .core .IFileInfo ;
16
14
import org .eclipse .cdt .managedbuilder .core .IInputType ;
17
15
import org .eclipse .cdt .managedbuilder .core .IOutputType ;
18
- import org .eclipse .cdt .managedbuilder .core .IResourceInfo ;
19
16
import org .eclipse .cdt .managedbuilder .core .ITool ;
20
17
import org .eclipse .cdt .managedbuilder .internal .core .ManagedMakeMessages ;
21
18
import org .eclipse .core .resources .IContainer ;
31
28
public class SubDirMakeGenerator {
32
29
private ArduinoGnuMakefileGenerator caller ;
33
30
private Set <MakeRule > myMakeRules = new LinkedHashSet <>();
34
- private IContainer myModule ;
31
+ private IFile myMakefile ;
35
32
36
33
SubDirMakeGenerator (ArduinoGnuMakefileGenerator theCaller , IContainer module ) {
37
34
caller = theCaller ;
38
- myModule = module ;
39
- getMakeRules ();
35
+ IProject project = getProject ();
36
+ IPath buildRoot = getBuildWorkingDir ();
37
+ if (buildRoot == null ) {
38
+ return ;
39
+ }
40
+ IPath moduleOutputPath = buildRoot .append (module .getProjectRelativePath ());
41
+ myMakefile = project .getFile (moduleOutputPath .append (MODFILE_NAME ));
42
+ getMakeRulesFromSourceFiles (module );
40
43
}
41
44
42
45
public Set <String > getDependecyMacros () {
@@ -63,6 +66,10 @@ public Set<String> getTargetMacros() {
63
66
return ret ;
64
67
}
65
68
69
+ public Set <MakeRule > getMakeRules () {
70
+ return myMakeRules ;
71
+ }
72
+
66
73
public Set <String > getPrerequisiteMacros () {
67
74
HashSet <String > ret = new LinkedHashSet <>();
68
75
for (MakeRule curMakeRule : myMakeRules ) {
@@ -87,8 +94,8 @@ public Set<IFile> getDependencyFiles() {
87
94
return ret ;
88
95
}
89
96
90
- public Map <IOutputType , List <IFile >> getTargets () {
91
- Map <IOutputType , List <IFile >> ret = new LinkedHashMap <>();
97
+ public Map <IOutputType , Set <IFile >> getTargets () {
98
+ Map <IOutputType , Set <IFile >> ret = new LinkedHashMap <>();
92
99
for (MakeRule curMakeRule : myMakeRules ) {
93
100
ret .putAll (curMakeRule .getTargets ());
94
101
}
@@ -119,22 +126,13 @@ private IProject getProject() {
119
126
* generated for each project directory/subdirectory that contains source files.
120
127
*/
121
128
public void generateMakefile () throws CoreException {
122
- //create the parent folder on disk and file in eclispe
123
- IProject project = getProject ();
124
- IPath buildRoot = getBuildWorkingDir ();
125
- if (buildRoot == null ) {
126
- return ;
127
- }
128
- IPath moduleOutputPath = buildRoot .append (myModule .getProjectRelativePath ());
129
- IFile modMakefile = project .getFile (moduleOutputPath .append (MODFILE_NAME ));
130
-
131
129
//generate the file content
132
130
StringBuffer makeBuf = addDefaultHeader ();
133
131
makeBuf .append (GenerateMacros ());
134
132
makeBuf .append (GenerateRules (getConfig ()));
135
133
136
134
// Save the files
137
- save (makeBuf , modMakefile );
135
+ save (makeBuf , myMakefile );
138
136
}
139
137
140
138
private StringBuffer GenerateMacros () {
@@ -179,85 +177,65 @@ private StringBuffer GenerateRules(IConfiguration config) {
179
177
}
180
178
181
179
//Get the rules for the source files
182
- private void getMakeRules ( ) {
180
+ private void getMakeRulesFromSourceFiles ( IContainer module ) {
183
181
myMakeRules .clear ();
184
182
IConfiguration config = getConfig ();
185
183
IProject project = getProject ();
186
184
187
185
// Visit the resources in this folder
188
186
try {
189
- for (IResource resource : myModule .members ()) {
187
+ for (IResource resource : module .members ()) {
190
188
if (resource .getType () != IResource .FILE ) {
191
189
//only handle files
192
190
continue ;
193
191
}
194
192
IFile inputFile = (IFile ) resource ;
193
+ String ext = inputFile .getFileExtension ();
194
+ if (ext == null || ext .isBlank ()) {
195
+ continue ;
196
+ }
195
197
IPath rcProjRelPath = inputFile .getProjectRelativePath ();
196
198
if (!caller .isSource (rcProjRelPath )) {
197
199
// this resource is excluded from build
198
200
continue ;
199
201
}
200
- IResourceInfo rcInfo = config .getResourceInfo (rcProjRelPath , false );
201
- String ext = rcProjRelPath .getFileExtension ();
202
202
203
- ITool tool = null ;
204
- //try to find tool
205
- if (rcInfo instanceof IFileInfo ) {
206
- IFileInfo fi = (IFileInfo ) rcInfo ;
207
- ITool [] tools = fi .getToolsToInvoke ();
208
- if (tools != null && tools .length > 0 ) {
209
- tool = tools [0 ];
210
- }
211
- }
212
-
213
- //No tool found yet try other way
214
- if (tool == null ) {
215
- ToolInfoHolder h = ToolInfoHolder .getToolInfo (caller , rcInfo .getPath ());
216
- ITool buildTools [] = h .buildTools ;
217
- h = ToolInfoHolder .getToolInfo (caller , Path .EMPTY );
218
- buildTools = h .buildTools ;
219
- for (ITool buildTool : buildTools ) {
220
- if (buildTool .buildsFileType (ext )) {
221
- tool = buildTool ;
222
- break ;
203
+ for (ITool tool : config .getTools ()) {
204
+ for (IInputType inputType : tool .getInputTypes ()) {
205
+ if (!inputType .isSourceExtension (tool , ext )) {
206
+ continue ;
207
+ }
208
+ for (IOutputType outputType : tool .getOutputTypes ()) {
209
+ IManagedOutputNameProviderJaba nameProvider = getJABANameProvider (outputType );
210
+ if (nameProvider == null ) {
211
+ continue ;
212
+ }
213
+ IPath outputFile = nameProvider .getOutputName (getProject (), config , tool ,
214
+ resource .getProjectRelativePath ());
215
+ if (outputFile == null ) {
216
+ continue ;
217
+ }
218
+ //We found a tool that provides a outputfile for our source file
219
+ //TOFIX if this is a multiple to one we should only create one MakeRule
220
+ IPath correctOutputPath = new Path (config .getName ()).append (outputFile );
221
+ MakeRule newMakeRule = new MakeRule (tool , inputType , inputFile , outputType ,
222
+ project .getFile (correctOutputPath ));
223
+ newMakeRule .addDependencies (caller );
224
+
225
+ myMakeRules .add (newMakeRule );
223
226
}
224
227
}
225
228
}
226
229
227
- //We found a tool get the other info
228
- //TOFIX we should simply loop over all available tools
229
- if (tool == null ) {
230
- continue ;
231
- }
232
-
233
- // Generate the rule to build this source file
234
- //TOFIX check wether this tool can handle this file
235
- IInputType inputType = tool .getPrimaryInputType ();
236
- if (inputType == null ) {
237
- inputType = tool .getInputType (ext );
238
- }
239
-
240
- for (IOutputType outputType : tool .getOutputTypes ()) {
241
- IManagedOutputNameProviderJaba nameProvider = getJABANameProvider (outputType );
242
- if (nameProvider == null ) {
243
- continue ;
244
- }
245
- IPath outputFile = nameProvider .getOutputName (getProject (), config , tool ,
246
- resource .getProjectRelativePath ());
247
- if (outputFile != null ) {
248
- //We found a tool that provides a outputfile for our source file
249
- //TOFIX if this is a multiple to one we should only create one MakeRule
250
- IPath correctOutputPath = new Path (config .getName ()).append (outputFile );
251
- MakeRule newMakeRule = new MakeRule (caller , tool , inputType , inputFile , outputType ,
252
- project .getFile (correctOutputPath ));
253
-
254
- myMakeRules .add (newMakeRule );
255
- }
256
- }
257
230
}
258
231
} catch (CoreException e ) {
259
232
// TODO Auto-generated catch block
260
233
e .printStackTrace ();
261
234
}
262
235
}
236
+
237
+ public boolean isEmpty () {
238
+ return myMakeRules .size () == 0 ;
239
+ }
240
+
263
241
}
0 commit comments