Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 40fc3058b57604987f7efb117b93f7b9aa0cdbc0 (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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
/*******************************************************************************
 * Copyright (c) 2004, 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
 * James Blackburn (Broadcom Corp.)
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.testplugin;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.ZipFile;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.ResourceHelper;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IAdditionalInput;
import org.eclipse.cdt.managedbuilder.core.IBuildObject;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IHoldsOptions;
import org.eclipse.cdt.managedbuilder.core.IInputType;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IProjectType;
import org.eclipse.cdt.managedbuilder.core.IResourceConfiguration;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ui.dialogs.IOverwriteQuery;
import org.eclipse.ui.internal.ide.filesystem.FileSystemStructureProvider;
import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;

public class ManagedBuildTestHelper {
	private static final String rcbsToolId = "org.eclipse.cdt.managedbuilder.ui.rcbs"; //$NON-NLS-1$
	private static final String rcbsToolName = "Resource Custom Build Step"; //$NON-NLS-1$
	private static final String rcbsToolInputTypeId = "org.eclipse.cdt.managedbuilder.ui.rcbs.inputtype"; //$NON-NLS-1$
	private static final String rcbsToolInputTypeName = "Resource Custom Build Step Input Type"; //$NON-NLS-1$
	private static final String rcbsToolOutputTypeId = "org.eclipse.cdt.managedbuilder.ui.rcbs.outputtype"; //$NON-NLS-1$
	private static final String rcbsToolOutputTypeName = "Resource Custom Build Step Output Type"; //$NON-NLS-1$

	/* (non-Javadoc)
	 * Create a new project named <code>name</code> or return the project in
	 * the workspace of the same name if it exists.
	 *
	 * @param name The name of the project to create or retrieve.
	 * @return
	 * @throws CoreException
	 */
	static public IProject createProject(final String name, final IPath location, final String projectId,
			final String projectTypeId) throws CoreException {
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		final IProject newProjectHandle = root.getProject(name);
		IProject project = null;

		if (!newProjectHandle.exists()) {
			IWorkspace workspace = ResourcesPlugin.getWorkspace();
			if (projectId.equals(ManagedBuilderCorePlugin.MANAGED_MAKE_PROJECT_ID)) {
				createNewManagedProject(newProjectHandle, name, location, projectId, projectTypeId);
				project = newProjectHandle;
			} else {
				IWorkspaceDescription workspaceDesc = workspace.getDescription();
				workspaceDesc.setAutoBuilding(false);
				workspace.setDescription(workspaceDesc);
				IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName());
				//description.setLocation(root.getLocation());
				project = CCorePlugin.getDefault().createCProject(description, newProjectHandle,
						new NullProgressMonitor(), ManagedBuilderCorePlugin.MANAGED_MAKE_PROJECT_ID);
			}
		} else {
			IWorkspace workspace = ResourcesPlugin.getWorkspace();
			IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
				@Override
				public void run(IProgressMonitor monitor) throws CoreException {
					newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, monitor);
				}
			};
			NullProgressMonitor monitor = new NullProgressMonitor();
			workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
			project = newProjectHandle;
		}

		// Open the project if we have to
		if (!project.isOpen()) {
			project.open(new NullProgressMonitor());
			// CDT opens the Project with BACKGROUND_REFRESH enabled which causes the
			// refresh manager to refresh the project 200ms later.  This Job interferes
			// with the resource change handler firing see: bug 271264
			try {
				// CDT opens the Project with BACKGROUND_REFRESH enabled which causes the
				// refresh manager to refresh the project 200ms later.  This Job interferes
				// with the resource change handler firing see: bug 271264
				Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, null);
			} catch (Exception e) {
				// Ignore
			}
		}

		return project;
	}

	static public IProject createProject(final String name, final String projectTypeId) {
		try {
			return createProject(name, null, ManagedBuilderCorePlugin.MANAGED_MAKE_PROJECT_ID, projectTypeId);
		} catch (CoreException e) {
			TestCase.fail(e.getLocalizedMessage());
		}
		return null;
	}

	static public IFile createFile(IProject project, String name) {
		return createFile(project, name, new ByteArrayInputStream(new byte[0]));
	}

	static public IFile createFile(IProject project, String name, String contents) {
		return createFile(project, name, new ByteArrayInputStream(contents.getBytes()));
	}

	static public IFile createFile(IProject project, String name, InputStream contents) {
		IFile file = project.getFile(name);
		if (!file.exists()) {
			try {
				IPath dirPath = file.getFullPath().removeLastSegments(1).removeFirstSegments(1);
				if (dirPath.segmentCount() > 0) {
					IFolder rc = project.getFolder(dirPath);
					if (!rc.exists()) {
						rc.create(true, true, null);
					}
				}

				//				file.create( new ByteArrayInputStream( "#include <stdio.h>\n extern void bar(); \n int main() { \nprintf(\"Hello, World!!\"); \n bar();\n return 0; }".getBytes() ), false, null );
				file.create(contents, false, null);
			} catch (CoreException e) {
				TestCase.fail(e.getLocalizedMessage());
			}
		}
		return file;
	}

	static public IFolder createFolder(IProject project, String name) {
		IFolder folder = project.getFolder(name);
		if (!folder.exists()) {
			try {
				IPath dirPath = folder.getFullPath().removeLastSegments(1).removeFirstSegments(1);
				if (dirPath.segmentCount() > 0) {
					IFolder rc = project.getFolder(dirPath);
					if (!rc.exists()) {
						rc.create(true, true, null);
					}
				}

				//				file.create( new ByteArrayInputStream( "#include <stdio.h>\n extern void bar(); \n int main() { \nprintf(\"Hello, World!!\"); \n bar();\n return 0; }".getBytes() ), false, null );
				folder.create(true, false, null);
			} catch (CoreException e) {
				TestCase.fail(e.getLocalizedMessage());
			}
		}
		return folder;
	}

	/**
	 * Remove the <code>IProject</code> with the name specified in the argument from the
	 * receiver's workspace.
	 *
	 * @param name
	 */
	static public void removeProject(String name) {
		ResourceHelper.joinIndexerBeforeCleanup(ManagedBuildTestHelper.class.getName());
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		final IProject project = root.getProject(name);
		if (project.exists()) {
			IWorkspace workspace = ResourcesPlugin.getWorkspace();
			IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
				@Override
				public void run(IProgressMonitor monitor) throws CoreException {
					System.gc();
					System.runFinalization();
					project.delete(true, true, null);
				}
			};
			NullProgressMonitor monitor = new NullProgressMonitor();
			try {
				workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
			} catch (CoreException e2) {
				Assert.fail("failed to remove the project: " + e2.getLocalizedMessage());
			}
		}
	}

	/**
	 * Import a test project into the test workspace.
	 * Project template must be located under:
	 * <ul>
	 * <li>resources/{path}/{name}/{name}.zip</li>
	 * <li>resources/{path}/{name}.zip</li>
	 * <li>resources/{path}/{name}</li>
	 * </ul>
	 * Where name is either an expanded or zip compressed project.
	 * @param name - name of the project to import
	 * @param path - path relative to /resources
	 * @return IProject or throws AssertionFailedError on failure
	 */
	public static IProject loadProject(String name, String path) {
		IPath zipPath = new Path("resources").append(path).append(name).append(name).addFileExtension("zip");
		File zipFile = CTestPlugin.getFileInPlugin(zipPath);
		if (zipFile == null) {
			zipPath = new Path("resources").append(path).append(name).addFileExtension("zip");
			zipFile = CTestPlugin.getFileInPlugin(zipPath);
		}
		// Try loading as a project directory
		if (zipFile == null) {
			zipPath = new Path("resources").append(path).append(name);
			zipFile = CTestPlugin.getFileInPlugin(zipPath);
		}

		if (zipFile == null) {
			Assert.fail("zip file " + zipPath.toString() + " is missing.");
			/* never get here */
			return null;
		}

		try {
			return createProject(name, zipFile, null, null);
		} catch (Exception e) {
			Assert.fail("fail to create the project: " + e.getLocalizedMessage());
		}
		/* never get here */
		return null;
	}

	static public IProject createProject(String projectName, File importFrom, IPath location, String projectTypeId)
			throws CoreException, InvocationTargetException, IOException {
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IProject project = root.getProject(projectName);
		if (project.exists())
			removeProject(projectName);

		IPath destPath = (location != null) ? location : project.getFullPath();

		if (importFrom != null) {
			IImportStructureProvider importStructure;
			Object importRoot;
			if (importFrom.isFile()) {
				importStructure = new ZipFileStructureProvider(new ZipFile(importFrom));
				importRoot = ((ZipFileStructureProvider) importStructure).getRoot();
			} else {
				importStructure = new FileSystemStructureProvider();
				importRoot = importFrom;
			}

			try {
				ImportOperation op = new ImportOperation(destPath, importRoot, importStructure, new IOverwriteQuery() {
					@Override
					public String queryOverwrite(String file) {
						return ALL;
					}
				});
				op.setCreateContainerStructure(false);
				op.run(null);
			} catch (InterruptedException e) {
				// should not happen
				Assert.assertTrue(false);
			}
		}

		return createProject(projectName, location, ManagedBuilderCorePlugin.MANAGED_MAKE_PROJECT_ID, projectTypeId);
	}

	static public IProject createNewManagedProject(IProject newProjectHandle, final String name, final IPath location,
			final String projectId, final String projectTypeId) throws CoreException {
		final IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot root = workspace.getRoot();
		final IProject project = newProjectHandle;
		IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				// Create the base project
				IWorkspaceDescription workspaceDesc = workspace.getDescription();
				workspaceDesc.setAutoBuilding(false);
				workspace.setDescription(workspaceDesc);
				IProjectDescription description = workspace.newProjectDescription(project.getName());
				if (location != null) {
					description.setLocation(location);
				}
				CCorePlugin.getDefault().createCProject(description, project, new NullProgressMonitor(), projectId);
				// Add the managed build nature and builder
				addManagedBuildNature(project);

				// Find the base project type definition
				IProjectType projType = ManagedBuildManager.getProjectType(projectTypeId);
				Assert.assertNotNull(projType);

				// Create the managed-project (.cdtbuild) for our project that builds an executable.
				IManagedProject newProject = null;
				try {
					newProject = ManagedBuildManager.createManagedProject(project, projType);
				} catch (Exception e) {
					Assert.fail("Failed to create managed project for: " + project.getName());
					return;
				}
				Assert.assertEquals(newProject.getName(), projType.getName());
				Assert.assertFalse(newProject.equals(projType));
				ManagedBuildManager.setNewProjectVersion(project);
				// Copy over the configs
				IConfiguration defaultConfig = null;
				IConfiguration[] configs = projType.getConfigurations();
				for (int i = 0; i < configs.length; ++i) {
					// Make the first configuration the default
					if (i == 0) {
						defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
					} else {
						newProject.createConfiguration(configs[i], projType.getId() + "." + i);
					}
				}
				ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);

				IConfiguration cfgs[] = newProject.getConfigurations();
				for (int i = 0; i < cfgs.length; i++) {
					cfgs[i].setArtifactName(newProject.getDefaultArtifactName());
				}

				ManagedBuildManager.getBuildInfo(project).setValid(true);
			}
		};
		NullProgressMonitor monitor = new NullProgressMonitor();
		try {
			workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
		} catch (CoreException e2) {
			Assert.fail(e2.getLocalizedMessage());
		}
		// CDT opens the Project with BACKGROUND_REFRESH enabled which causes the
		// refresh manager to refresh the project 200ms later.  This Job interferes
		// with the resource change handler firing see: bug 271264
		try {
			// CDT opens the Project with BACKGROUND_REFRESH enabled which causes the
			// refresh manager to refresh the project 200ms later.  This Job interferes
			// with the resource change handler firing see: bug 271264
			Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, null);
		} catch (Exception e) {
			// Ignore
		}

		// Initialize the path entry container
		IStatus initResult = ManagedBuildManager.initBuildInfoContainer(project);
		if (initResult.getCode() != IStatus.OK) {
			Assert.fail("Initializing build information failed for: " + project.getName() + " because: "
					+ initResult.getMessage());
		}
		return project;
	}

	static public void addManagedBuildNature(IProject project) {
		// Create the buildinformation object for the project
		IManagedBuildInfo info = ManagedBuildManager.createBuildInfo(project);
		Assert.assertNotNull(info);
		//		info.setValid(true);

		// Add the managed build nature
		try {
			ManagedCProjectNature.addManagedNature(project, new NullProgressMonitor());
			ManagedCProjectNature.addManagedBuilder(project, new NullProgressMonitor());
		} catch (CoreException e) {
			Assert.fail("Test failed on adding managed build nature or builder: " + e.getLocalizedMessage());
		}

		// Associate the project with the managed builder so the clients can get proper information
		ICDescriptor desc = null;
		try {
			desc = CCorePlugin.getDefault().getCProjectDescription(project, true);
			desc.remove(CCorePlugin.BUILD_SCANNER_INFO_UNIQ_ID);
			desc.create(CCorePlugin.BUILD_SCANNER_INFO_UNIQ_ID, ManagedBuildManager.INTERFACE_IDENTITY);
		} catch (CoreException e) {
			Assert.fail("Test failed on adding managed builder as scanner info provider: " + e.getLocalizedMessage());
			return;
		}
		try {
			desc.saveProjectData();
		} catch (CoreException e) {
			Assert.fail("Test failed on saving the ICDescriptor data: " + e.getLocalizedMessage());
		}
	}

	static public boolean compareBenchmarks(final IProject project, IPath testLocationBase, IPath[] files,
			IPath benchmarkLocationBase) {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
			}
		};
		try {
			NullProgressMonitor monitor = new NullProgressMonitor();
			workspace.run(runnable, workspace.getRoot(), IWorkspace.AVOID_UPDATE, monitor);
		} catch (Exception e) {
			Assert.fail("File " + files[0].lastSegment() + " - project refresh failed.");
		}
		for (int i = 0; i < files.length; i++) {
			IPath testFileLocation = testLocationBase.append(files[i]);
			IPath benchmarkFileLocation = benchmarkLocationBase.append("Benchmarks").append(files[i]);
			if (isMakefile(testFileLocation)) {
				if (compareMakefiles(testFileLocation, benchmarkFileLocation)) {
					continue;
				}
			}
			if (isDependencyFile(testFileLocation)) {
				if (compareDependencyFiles(testFileLocation, benchmarkFileLocation)) {
					continue;
				}
			}
			StringBuffer testBuffer = readContentsStripLineEnds(project, testFileLocation);
			StringBuffer benchmarkBuffer = readContentsStripLineEnds(project, benchmarkFileLocation);
			if (!testBuffer.toString().equals(benchmarkBuffer.toString())) {
				StringBuffer buffer = new StringBuffer();
				buffer.append("File ").append(testFileLocation.lastSegment())
						.append(" does not match its benchmark.\n ");
				buffer.append("expected:\n ");
				buffer.append('"').append(benchmarkBuffer).append('"');
				buffer.append("\n\n ");
				buffer.append("but was:\n ");
				buffer.append('"').append(testBuffer).append('"');
				buffer.append("\n\n ");

				buffer.append(">>>>>>>>>>>>>>>start diff: \n");
				String location1 = benchmarkFileLocation.isAbsolute() ? benchmarkFileLocation.toString()
						: getFileLocation(project, benchmarkFileLocation);
				String location2 = testFileLocation.isAbsolute() ? testFileLocation.toString()
						: getFileLocation(project, testFileLocation);
				String diff = DiffUtil.getInstance().diff(location1, location2);
				if (diff == null)
					diff = "!diff failed!";
				buffer.append(diff);
				buffer.append("\n<<<<<<<<<<<end diff");
				buffer.append("\n\n ");

				Assert.fail(buffer.toString());
			}
		}
		return true;
	}

	private static boolean isMakefile(IPath file) {
		String ext = file.getFileExtension();
		if (ext == null) {
			String name = file.lastSegment();
			return name != null && (name.equals("makefile") || name.equals("Makefile") || name.equals("GNUmakefile"));
		}
		return ext.equals("mk");
	}

	private static boolean isDependencyFile(IPath file) {
		String ext = file.getFileExtension();
		return "d".equals(ext);
	}

	/**
	 * Compare makefiles using a bunch of heuristics to avoid ordering mismatches
	 *
	 * @param testFile - location of actual makefile
	 * @param benchmarkFile - location of the benchmark file
	 * @return {@code true} if matches, {@code false} otherwise
	 */
	private static boolean compareMakefiles(IPath testFile, IPath benchmarkFile) {
		final String ECHO_INVOKING_PATTERN = "	@echo 'Invoking: .* C\\+\\+ .*'";
		final String IFNEQ_PATTERN = "ifneq \\(\\$\\(strip \\$\\(.*\\)\\),\\)";
		final String INCLUDE_PATTERN = "-include \\$\\(.*\\)";
		final String MACRO_PATTERN = "\\S* [:+]=.*";
		final String EMPTY_MACRO_PATTERN = "\\S* :=";
		final String WORKSPACE_DIR_STR = CdtVariableResolver
				.createVariableReference(CdtVariableResolver.VAR_WORKSPACE_DIR_PATH);
		ArrayList<String> testArray = mergeContinuationLines(getContents(testFile));
		ArrayList<String> benchmarkArray = mergeContinuationLines(getContents(benchmarkFile));

		Set<String> testNotMatchingLines = new TreeSet<String>();
		Set<String> benchNotMatchingLines = new TreeSet<String>();
		Set<String> extraLines = new TreeSet<String>();
		for (int i = 0; i < benchmarkArray.size() || i < testArray.size(); i++) {
			if (!(i < benchmarkArray.size())) {
				System.err.println(testFile.lastSegment() + ": extra line =[" + testArray.get(i)
						+ "] not in benchmark. File " + testFile);
				System.err.println(testFile.lastSegment() + ": benchmark file " + benchmarkFile);
				extraLines.add(testArray.get(i));
				continue;
			}
			if (!(i < testArray.size())) {
				System.err.println(testFile.lastSegment() + ": missing line =[" + benchmarkArray.get(i)
						+ "] comparing to benchmark. File " + testFile);
				System.err.println(testFile.lastSegment() + ": benchmark file " + benchmarkFile);
				extraLines.add(benchmarkArray.get(i));
				continue;
			}
			String testLine = testArray.get(i);
			String benchmarkLine = benchmarkArray.get(i);
			if (!testLine.equals(benchmarkLine)) {
				if (testLine.startsWith("	-$(RM) ")) {
					// accommodate to arbitrary order of 'rm' parameters
					final String DELIMITERS = "[ $]";
					String[] testMacros = new TreeSet<String>(Arrays.asList(testLine.split(DELIMITERS)))
							.toArray(new String[0]);
					String[] benchMacros = new TreeSet<String>(Arrays.asList(benchmarkLine.split(DELIMITERS)))
							.toArray(new String[0]);
					if (testMacros.length != benchMacros.length) {
						return false;
					}
					for (int j = 0; j < testMacros.length; j++) {
						if (!testMacros[j].equals(benchMacros[j])) {
							return false;
						}
					}
				} else if (testLine.matches(ECHO_INVOKING_PATTERN) && benchmarkLine.matches(ECHO_INVOKING_PATTERN)) {
					// accommodate for variable linker name (GCC vs. Cygwin)
					continue;
				} else if (testLine.matches(IFNEQ_PATTERN) && benchmarkLine.matches(IFNEQ_PATTERN)) {
					// accommodate for variable order of different macro's blocks (see also INCLUDE_PATTERN)
					// ifneq ($(strip $(CXX_DEPS)),)
					// -include $(CXX_DEPS)
					// endif
					testNotMatchingLines.add(testLine);
					benchNotMatchingLines.add(benchmarkLine);
				} else if (testLine.matches(INCLUDE_PATTERN) && benchmarkLine.matches(INCLUDE_PATTERN)) {
					// accommodate for variable order of different macro's blocks (see IFNEQ_PATTERN)
					testNotMatchingLines.add(testLine);
					benchNotMatchingLines.add(benchmarkLine);
				} else if (testLine.matches(MACRO_PATTERN) && benchmarkLine.matches(MACRO_PATTERN)) {
					// accommodate for variable order of macros
					testNotMatchingLines.add(testLine);
					benchNotMatchingLines.add(benchmarkLine);
				} else if (benchmarkLine.contains(WORKSPACE_DIR_STR)) {
					String[] benchmarkSubstrings = benchmarkLine.split(" ");
					String[] testSubstrings = testLine.split(" ");
					if (testSubstrings.length != benchmarkSubstrings.length) {
						System.err.println("Following lines do not match (" + testFile.lastSegment() + "):");
						System.err.println("actual  : [" + testLine + "], file " + testFile);
						System.err.println("expected: [" + benchmarkLine + "], file " + benchmarkFile);
						return false;
					}

					final IWorkspace workspace = ResourcesPlugin.getWorkspace();
					final IWorkspaceRoot root = workspace.getRoot();
					final String workspaceLocation = root.getLocation().toOSString();
					final String platformFileSeparator = System.getProperty("file.separator", //$NON-NLS-1$
							Character.toString(IPath.SEPARATOR));

					for (int j = 0; j < testSubstrings.length; j++) {
						String testSubstring = testSubstrings[j];
						String benchmarkSubstring = benchmarkSubstrings[j];
						if (benchmarkSubstring.contains(WORKSPACE_DIR_STR)) {
							benchmarkSubstring = benchmarkSubstring.replace("/", platformFileSeparator)
									.replace(WORKSPACE_DIR_STR, workspaceLocation);
						}
						if (!testSubstring.equals(benchmarkSubstring)) {
							System.err.println("Following lines do not match (" + testFile.lastSegment() + "):");
							System.err.println("actual  : [" + testLine + "], file " + testFile);
							System.err.println("expected: [" + benchmarkLine + "], file " + benchmarkFile);
							System.err.println("substring actual : [" + testSubstring + "], file " + testFile);
							System.err
									.println("substring expected: [" + benchmarkSubstring + "], file " + benchmarkFile);
							return false;
						}
					}
				} else {
					System.err.println("Following lines do not match (" + testFile.lastSegment() + "):");
					System.err.println("actual  : [" + testLine + "], file " + testFile);
					System.err.println("expected: [" + benchmarkLine + "], file " + benchmarkFile);
					return false;
				}
			}
		}
		if (testArray.size() != benchmarkArray.size()) {
			System.err.println(
					"testArray.size=" + testArray.size() + " while benchmarkArray.size=" + benchmarkArray.size());
			// Ignore benign differences
			for (String line : extraLines) {
				if (line == null || line.length() == 0) {
					continue;
				}
				if (line.matches(EMPTY_MACRO_PATTERN)) {
					continue;
				}
				return false;
			}
		}

		// Check if all collected lines match irrespective of order
		String[] testNotMatchingLinesArray = testNotMatchingLines.toArray(new String[0]);
		String[] benchNotMatchingLinesArray = benchNotMatchingLines.toArray(new String[0]);
		for (int i = 0; i < testNotMatchingLinesArray.length; i++) {
			if (!testNotMatchingLinesArray[i].equals(benchNotMatchingLinesArray[i])) {
				System.err.println("Following line is missing (" + testFile.lastSegment() + "):");
				if (testNotMatchingLinesArray[i].compareTo(benchNotMatchingLinesArray[i]) < 0) {
					System.err.println("line [" + testNotMatchingLinesArray[i] + "], missing in file " + benchmarkFile);
				} else {
					System.err.println("line [" + benchNotMatchingLinesArray[i] + "], missing in file " + testFile);
				}
				return false;
			}
		}

		return true;
	}

	/**
	 * Compare dependency files *.d using some of heuristics to avoid mismatches
	 * related to different gcc versions
	 *
	 * @param testFile - location of actual file
	 * @param benchmarkFile - location of the benchmark file
	 * @return {@code true} if matches, {@code false} otherwise
	 */
	private static boolean compareDependencyFiles(IPath testFile, IPath benchmarkFile) {
		boolean result = true;
		ArrayList<String> testArray = getContents(testFile);
		ArrayList<String> benchmarkArray = getContents(benchmarkFile);

		for (int i = 0; i < benchmarkArray.size() || i < testArray.size(); i++) {
			if (!(i < benchmarkArray.size())) {
				System.err.println(testFile.lastSegment() + ": extra line =[" + testArray.get(i)
						+ "] not in benchmark. File " + testFile);
				System.err.println(testFile.lastSegment() + ": benchmark file " + benchmarkFile);
				continue;
			}
			if (!(i < testArray.size())) {
				System.err.println(testFile.lastSegment() + ": missing line =[" + benchmarkArray.get(i)
						+ "] comparing to benchmark. File " + testFile);
				System.err.println(testFile.lastSegment() + ": benchmark file " + benchmarkFile);
				continue;
			}
			String testLine = testArray.get(i);
			String benchmarkLine = benchmarkArray.get(i);
			if (!testLine.equals(benchmarkLine)) {
				// Accommodate for differences in amount of leading  and trailing spaces
				if (testLine.startsWith(" ") && benchmarkLine.startsWith(" ")) {
					String testLineTrimmed = testLine;
					String benchmarkLineTrimmed = benchmarkLine;
					if (testLineTrimmed.endsWith("\\") && benchmarkLineTrimmed.endsWith("\\")) {
						// trim ending "\"
						testLineTrimmed = testLineTrimmed.substring(0, testLineTrimmed.length() - 1);
						benchmarkLineTrimmed = benchmarkLineTrimmed.substring(0, benchmarkLineTrimmed.length() - 1);
					}
					if (testLineTrimmed.trim().equals(benchmarkLineTrimmed.trim())) {
						continue;
					}
					System.err.println("Following lines do not match (" + testFile.lastSegment() + "):");
					System.err.println("actual  : [" + testLine + "], file " + testFile);
					System.err.println("expected: [" + benchmarkLine + "], file " + benchmarkFile);
					result = false;
				}
			}
		}

		return result;
	}

	private static ArrayList<String> getContents(IPath fullPath) {
		ArrayList<String> lines = new ArrayList<String>();
		try {
			BufferedReader in = new BufferedReader(new FileReader(fullPath.toFile()));
			String line;
			do {
				line = in.readLine();
				if (line != null) {
					lines.add(line);
				}
			} while (line != null);
		} catch (IOException e) {
			Assert.fail("File " + fullPath.toString() + " could not be read: " + e.getLocalizedMessage());
		}
		return lines;
	}

	private static ArrayList<String> mergeContinuationLines(ArrayList<String> lines) {
		for (int i = 0; i < lines.size();) {
			String line = lines.get(i);
			if (line.endsWith("\\") && i + 1 < lines.size()) {
				lines.set(i, line.substring(0, line.length() - 1) + lines.get(i + 1));
				lines.remove(i + 1);
				// do not advance i and check the renewed line again
				continue;
			}
			i++;
		}
		return lines;
	}

	static public boolean compareBenchmarks(final IProject project, IPath testDir, String[] fileNames) {
		return compareBenchmarks(project, testDir, new Path("benchmarks").append(testDir), fileNames);
	}

	static public boolean compareBenchmarks(final IProject project, IPath testDir, IPath benchmarkDir,
			String[] fileNames) {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
			}
		};
		try {
			NullProgressMonitor monitor = new NullProgressMonitor();
			workspace.run(runnable, workspace.getRoot(), IWorkspace.AVOID_UPDATE, monitor);
		} catch (Exception e) {
			Assert.fail("File " + fileNames[0] + " - project refresh failed.");
		}

		IFolder testFolder = (IFolder) project.findMember(testDir);
		IFolder bmFolder = (IFolder) project.findMember(benchmarkDir);

		return compareBenchmarks(testFolder, bmFolder, fileNames);
	}

	static public boolean compareBenchmarks(IFolder testFolder, IFolder bmFolder, String[] fileNames) {
		Assert.assertNotNull(testFolder);
		Assert.assertNotNull(bmFolder);

		for (int i = 0; i < fileNames.length; i++) {
			IFile tFile = testFolder.getFile(fileNames[i]);
			IFile bmFile = bmFolder.getFile(fileNames[i]);
			if (!tFile.exists() && !bmFile.exists())
				continue;

			compareBenchmarks(tFile, bmFile);
		}

		return true;
	}

	static public boolean compareBenchmarks(IFile tFile, IFile bmFile) {
		IPath tFileLocation = tFile.getLocation();
		IPath bmFileLocation = bmFile.getLocation();
		if (isMakefile(tFileLocation)) {
			if (compareMakefiles(tFileLocation, bmFileLocation)) {
				return true;
			}
		}
		if (isDependencyFile(tFileLocation)) {
			if (compareDependencyFiles(tFileLocation, bmFileLocation)) {
				return true;
			}
		}
		StringBuffer testBuffer = readContentsStripLineEnds(tFile);
		StringBuffer benchmarkBuffer = readContentsStripLineEnds(bmFile);
		if (!testBuffer.toString().equals(benchmarkBuffer.toString())) {
			StringBuffer buffer = new StringBuffer();
			buffer.append("File ").append(tFile.getName()).append(" does not match its benchmark.\n ");
			buffer.append("expected:\n ");
			buffer.append('"').append(benchmarkBuffer).append('"');
			buffer.append("\n\n ");
			buffer.append("but was:\n ");
			buffer.append('"').append(testBuffer).append('"');
			buffer.append("\n\n ");

			buffer.append(">>>>>>>>>>>>>>>start diff: \n");
			String location1 = getFileLocation(bmFile.getProject(), bmFile.getProjectRelativePath());
			String location2 = getFileLocation(tFile.getProject(), tFile.getProjectRelativePath());
			String diff = DiffUtil.getInstance().diff(location1, location2);
			if (diff == null)
				diff = "!diff failed!";
			buffer.append(diff);
			buffer.append("\n<<<<<<<<<<<end diff");
			buffer.append("\n\n ");

			Assert.fail(buffer.toString());
		}
		return true;
	}

	static public boolean verifyFilesDoNotExist(final IProject project, IPath testDir, IPath[] files) {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
			}
		};
		try {
			NullProgressMonitor monitor = new NullProgressMonitor();
			workspace.run(runnable, workspace.getRoot(), IWorkspace.AVOID_UPDATE, monitor);
		} catch (Exception e) {
			Assert.fail("File " + files[0].lastSegment() + " - project refresh failed.");
		}
		for (int i = 0; i < files.length; i++) {
			IPath testFile = testDir.append(files[i]);
			IPath fullPath = project.getLocation().append(testFile);
			try {
				if (fullPath.toFile().exists()) {
					Assert.fail("File " + testFile.lastSegment() + " unexpectedly found.");
					return false;
				}
			} catch (Exception e) {
				Assert.fail("File " + fullPath.toString() + " could not be referenced.");
			}
		}
		return true;
	}

	static public String getFileLocation(IProject project, IPath path) {
		return project.getLocation().append(path).toString();
	}

	static public StringBuffer readContentsStripLineEnds(IFile file) {
		return readContentsStripLineEnds(file.getProject(), file.getProjectRelativePath());
	}

	static public StringBuffer readContentsStripLineEnds(IProject project, IPath path) {
		StringBuffer buff = new StringBuffer();
		IPath fullPath = path;
		if (!fullPath.isAbsolute()) {
			fullPath = project.getLocation().append(path);
		}
		try {
			FileReader input = null;
			try {
				input = new FileReader(fullPath.toFile());
			} catch (Exception e) {
				Assert.fail("File " + fullPath.toString() + " could not be read: " + e.getLocalizedMessage());
			}
			//InputStream input = file.getContents(true);   // A different way to read the file...
			int c;
			do {
				c = input.read();
				if (c == -1)
					break;
				if (c != '\r' && c != '\n') {
					buff.append((char) c);
				}
			} while (c != -1);
			input.close();
		} catch (Exception e) {
			Assert.fail("File " + fullPath.toString() + " could not be read.");
		}
		return buff;
	}

	static public IPath copyFilesToTempDir(IPath srcDir, IPath tmpRootDir, IPath tmpSubDir, IPath[] files) {
		IPath tmpSrcDir = null;
		tmpSrcDir = tmpRootDir.append(tmpSubDir);
		if (tmpRootDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
			Assert.fail("Temporary sub-directory cannot be the empty string.");
		} else {
			File tmpSrcDirFile = tmpSrcDir.toFile();
			if (tmpSrcDirFile.exists()) {
				//  Make sure that this is the expected directory before we delete it...
				if (tmpSrcDir.lastSegment().equals(tmpSubDir.lastSegment())) {
					deleteDirectory(tmpSrcDirFile);
				} else {
					Assert.fail("Temporary directory " + tmpSrcDirFile.toString() + " already exists.");
				}
			}
			tmpSrcDirFile.mkdir();
			if (!tmpSrcDirFile.exists()) {
				Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString());
			}
			for (int i = 0; i < files.length; i++) {
				IPath file = files[i];
				IPath srcFile = srcDir.append(file);
				FileReader srcReader = null;
				try {
					srcReader = new FileReader(srcFile.toFile());
				} catch (Exception e) {
					Assert.fail("File " + file.toString() + " could not be read.");
					return null;
				}
				if (file.segmentCount() > 1) {
					IPath newDir = tmpSrcDir;
					do {
						IPath dir = file.uptoSegment(1);
						newDir = newDir.append(dir);
						file = file.removeFirstSegments(1);
						newDir.toFile().mkdir();
						if (!newDir.toFile().exists()) {
							Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString());
						}
					} while (file.segmentCount() > 1);
				}
				IPath destFile = tmpSrcDir.append(files[i]);
				FileWriter writer = null;
				try {
					writer = new FileWriter(destFile.toFile());
				} catch (Exception e) {
					Assert.fail("File " + files[i].toString() + " could not be written.");
					return null;
				}
				try {
					int c;
					do {
						c = srcReader.read();
						if (c == -1)
							break;
						writer.write(c);
					} while (c != -1);
					srcReader.close();
					writer.close();
				} catch (Exception e) {
					Assert.fail("File " + file.toString() + " could not be copied.");
				}
			}
		}

		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot root = workspace.getRoot();
		for (IFile rc : root.findFilesForLocation(tmpSrcDir)) {
			try {
				rc.refreshLocal(IFile.DEPTH_INFINITE, null);
			} catch (CoreException e) {
				// ignore exception
			}
		}
		return tmpSrcDir;
	}

	static public void deleteTempDir(IPath tmpRootDir, IPath tmpSubDir, IPath[] files) {
		IPath tmpSrcDir = null;
		tmpSrcDir = tmpRootDir.append(tmpSubDir);
		if (tmpRootDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
			Assert.fail("Temporary sub-directory cannot be the empty string.");
		} else {
			File tmpSrcDirFile = tmpSrcDir.toFile();
			if (tmpSrcDirFile.exists()) {
				for (int i = 0; i < files.length; i++) {
					// Delete the file
					IPath thisFile = tmpSrcDir.append(files[i]);
					thisFile.toFile().delete();
				}
				// Delete the dir
				tmpSrcDirFile.delete();
			}

			tmpRootDir.toFile().delete();
		}
	}

	/*
	 * Cloned from core CProjectHelper
	 */
	public static void delete(ICProject cproject) {
		try {
			cproject.getProject().delete(true, true, null);
		} catch (CoreException e) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
			} finally {
				try {
					System.gc();
					System.runFinalization();
					cproject.getProject().delete(true, true, null);
				} catch (CoreException e2) {
					Assert.fail(getMessage(e2.getStatus()));
				}
			}
		}
	}

	/*
	 * Cloned from core CProjectHelper
	 */
	private static String getMessage(IStatus status) {
		StringBuffer message = new StringBuffer("[");
		message.append(status.getMessage());
		if (status.isMultiStatus()) {
			IStatus children[] = status.getChildren();
			for (int i = 0; i < children.length; i++) {
				message.append(getMessage(children[i]));
			}
		}
		message.append("]");
		return message.toString();
	}

	static private void deleteDirectory(File dir) {
		File[] toDelete = dir.listFiles();
		for (int i = 0; i < toDelete.length; i++) {
			File fileToDelete = toDelete[i];
			if (fileToDelete.isDirectory()) {
				deleteDirectory(fileToDelete);
			}
			fileToDelete.delete();
		}
		dir.delete();
	}

	public static ITool createRcbsTool(IConfiguration cfg, String file, String inputs, String outputs, String cmds) {
		IProject project = cfg.getOwner().getProject();
		IResource f = project.findMember(file);

		Assert.assertTrue("file does not exist", f != null);
		Assert.assertEquals("resource is not a file", f.getType(), IResource.FILE);

		return createRcbsTool(cfg, (IFile) f, inputs, outputs, cmds);
	}

	public static ITool createRcbsTool(IConfiguration cfg, IFile file, String inputs, String outputs, String cmds) {
		IResourceConfiguration rcCfg = cfg.getResourceConfiguration(file.getFullPath().toString());
		if (rcCfg == null)
			rcCfg = cfg.createResourceConfiguration(file);

		Assert.assertTrue("failed to create resource configuration", rcCfg != null);

		ITool tool = getRcbsTool(rcCfg, true);

		setRcbsInputs(tool, inputs);
		setRcbsOutputs(tool, outputs);
		tool.setToolCommand(cmds);
		tool.setAnnouncement("default test rcbs announcement");

		rcCfg.setRcbsApplicability(IResourceConfiguration.KIND_APPLY_RCBS_TOOL_AS_OVERRIDE);
		return tool;
	}

	public static ITool setRcbsInputs(ITool tool, String inputs) {
		tool.getInputTypes()[0].getAdditionalInputs()[0].setPaths(inputs);
		return tool;
	}

	public static ITool setRcbsOutputs(ITool tool, String outputs) {
		tool.getOutputTypes()[0].setOutputNames(outputs);
		return tool;
	}

	public static ITool getRcbsTool(IResourceConfiguration rcConfig, boolean create) {
		ITool rcbsTools[] = getRcbsTools(rcConfig);
		ITool rcbsTool = null;
		if (rcbsTools != null)
			rcbsTool = rcbsTools[0];
		else if (create) {
			rcbsTool = rcConfig.createTool(null, rcbsToolId + "." + ManagedBuildManager.getRandomNumber(), rcbsToolName, //$NON-NLS-1$
					false);
			rcbsTool.setCustomBuildStep(true);
			IInputType rcbsToolInputType = rcbsTool.createInputType(null,
					rcbsToolInputTypeId + "." + ManagedBuildManager.getRandomNumber(), rcbsToolInputTypeName, false); //$NON-NLS-1$
			IAdditionalInput rcbsToolInputTypeAdditionalInput = rcbsToolInputType.createAdditionalInput(""); //$NON-NLS-1$
			rcbsToolInputTypeAdditionalInput.setKind(IAdditionalInput.KIND_ADDITIONAL_INPUT_DEPENDENCY);
			rcbsTool.createOutputType(null, rcbsToolOutputTypeId + "." + ManagedBuildManager.getRandomNumber(), //$NON-NLS-1$
					rcbsToolOutputTypeName, false);
		}
		return rcbsTool;
	}

	public static ITool[] getRcbsTools(IResourceConfiguration rcConfig) {
		List<ITool> list = new ArrayList<ITool>();
		ITool tools[] = rcConfig.getTools();
		for (int i = 0; i < tools.length; i++) {
			ITool tool = tools[i];
			if (tool.getCustomBuildStep() && !tool.isExtensionElement()) {
				list.add(tool);
			}
		}
		if (list.size() != 0)
			return list.toArray(new ITool[list.size()]);
		return null;
	}

	public static boolean setObjs(IConfiguration cfg, String[] objs) {
		return setOption(cfg, IOption.OBJECTS, objs);
	}

	public static boolean setLibs(IConfiguration cfg, String[] objs) {
		return setOption(cfg, IOption.LIBRARIES, objs);
	}

	public static boolean setOption(IConfiguration cfg, int type, Object value) {
		return setOption(cfg.getFilteredTools(), type, value);
	}

	public static boolean setOption(IResourceConfiguration rcCfg, int type, Object value) {
		return setOption(rcCfg.getToolsToInvoke()[0], type, value);
	}

	public static boolean setOption(ITool tools[], int type, Object value) {
		for (int i = 0; i < tools.length; i++) {
			if (setOption(tools[i], type, value))
				return true;
		}
		return false;
	}

	public static IBuildObject[] getOption(IConfiguration cfg, int type) {
		return getOption(cfg.getFilteredTools(), type);
	}

	public static IBuildObject[] getOption(IResourceConfiguration rcCfg, int type, Object value) {
		return getOption(new ITool[] { rcCfg.getToolsToInvoke()[0] }, type);
	}

	public static IBuildObject[] getOption(ITool tools[], int type) {
		for (int i = 0; i < tools.length; i++) {
			IOption option = getOption(tools[i], type);
			if (option != null)
				return new IBuildObject[] { tools[i], option };
		}
		return null;
	}

	public static IOption getOption(IHoldsOptions tool, int type) {
		IOption opts[] = tool.getOptions();

		for (int i = 0; i < opts.length; i++) {
			IOption option = opts[i];
			try {
				if (option.getValueType() == type) {
					return option;
				}
			} catch (BuildException e) {
			}
		}
		return null;
	}

	public static boolean setOption(ITool tool, int type, Object value) {
		IOption option = getOption(tool, type);

		if (option == null)
			return false;
		IBuildObject obj = tool.getParent();
		IConfiguration cfg = null;
		IResourceConfiguration rcCfg = null;
		if (obj instanceof IToolChain)
			cfg = ((IToolChain) obj).getParent();
		else
			rcCfg = (IResourceConfiguration) obj;

		try {
			if (option.getValueType() == type) {
				switch (type) {
				case IOption.BOOLEAN: {
					boolean val = ((Boolean) value).booleanValue();
					if (rcCfg != null)
						rcCfg.setOption(tool, option, val);
					else
						cfg.setOption(tool, option, val);
				}
					return true;
				case IOption.ENUMERATED:
				case IOption.STRING: {
					String val = (String) value;
					if (rcCfg != null)
						rcCfg.setOption(tool, option, val);
					else
						cfg.setOption(tool, option, val);
				}
					return true;
				case IOption.STRING_LIST:
				case IOption.INCLUDE_PATH:
				case IOption.PREPROCESSOR_SYMBOLS:
				case IOption.LIBRARIES:
				case IOption.OBJECTS:
				case IOption.INCLUDE_FILES:
				case IOption.LIBRARY_PATHS:
				case IOption.LIBRARY_FILES:
				case IOption.MACRO_FILES: {
					String val[] = (String[]) value;
					if (rcCfg != null)
						rcCfg.setOption(tool, option, val);
					else
						cfg.setOption(tool, option, val);
				}
					return true;
				default:
					Assert.fail("wrong option type passed");
				}
			}
		} catch (BuildException e) {
		}
		return false;
	}

}

Back to the top