org.eclipse.jdt.core/scripts/GenerateBuildScript.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (download) (annotate)
Tue Mar 28 20:33:44 2006 UTC (3 years, 8 months ago) by oliviert
Branch: MAIN
CVS Tags: v_780_R33x, v_676_R32x, v_943, v_942, v_793_R33x, v_883b_R341_patches, v_675_R32x, v_720, v_886_R34x, v_974_R35x, v_838, v_969_R35x, v_782_R33x, v_729, v_A03, v_A02, v_A01, v_A00, v_A07, v_A06, v_A05, v_A04, v_749a, v_A09, v_A08, v_883a_R341_patches, v_770, v_771, v_686_R32x, v_788_R33x, v_731a, v_968_R35x, v_671, v_670, v_736a, v_777_R33x, v_772a_R33x, v_958, v_959, v_813, v_812, v_815, v_814, v_817, v_816, v_950, v_951, v_952, v_953, v_954, v_955, v_956, v_957, v_752, v_881_R34x, v_971_R35x, v_754, v_725, v_929, v_928, v_925, v_924, v_927, v_926, v_921, v_920, v_923, v_922, v_680_R32x, v_774_R33x, v_688_R32x, v_687_Startup_R32x, v_661, v_667, v_658a, v_900b_R34x, v_978_R35x, v_852_BEFORE_CONCURRENT_CHANGES, v_705, v_890_R34x, v_772_R33x, v_748, v_741, v_740, v_743, v_742, v_776_R33x, v_744, v_747, v_746, v_679_R32x, v_884_R34x, v_790_R33x, v_797_R33x, v_775_R33x, v_895_R34x, v_961, v_960, v_963, v_962, R3_3_1_1, v_848, v_849, v_846, v_847, v_844, v_884a_R341_patches, v_842, v_843, v_840, v_841, v_964_R35x, v_794_R33x, v_677_R32x, v_704, v_877_R34x, v_977_R35x, v_795_R33x, v_714, v_875_R34x, R3_4, R3_5, v_A18, v_A19, R3_2, R3_3, v_A14, v_A15, v_A16, v_A10, v_A11, v_A12, v_A13, v_899_R34x, v_712, v_716, v_668, v_669, v_710, v_826, v_662, v_663, v_660, v_711, v_666, v_693_R32x, v_664, v_665, pre_R3_3, v_717, v_966_R35x, v_684_R32x, v_719, v_802, v_803, v_800, v_801, v_806, v_807, v_804, v_805, v_808, v_809, v_718, v_877a_R34x, v_715, v_884b_R341_patches, v_749, v_674_R32x, v_882_R34x, v_887_R34x, v_655a, v_671a_R32x, v_713, v_938, v_939, v_936, v_937, v_934, v_935, v_932, v_933, v_930, v_931, v_745, v_677a_R32x, v_690_R32x, v_853_BEFORE_182360, v_970_R35x, v_A21, v_A20, v_A23, v_A22, v_758, v_759, v_787_R33x, v_753, v_750, v_751, v_756, v_757, v_885_R34x, v_755, v_852_BEFORE_102780, v_975_R35x, v_876_IES_R34x, v_746a, v_689_R32x, v_873, v_872, v_871, v_870, v_683_R32x, v_874, v_976_R35x, v_879_R34x, v_898_R34x, v_779_R33x, v_666a, v_883c_R341_patches, v_888_R34x, v_791_R33x, v_678_R32x, v_897_R34x, v_876_R34x, v_878_R34x, v_783_R33x, v_900a, v_685_R32x, v_900c, v_880_R34x, v_672_R32x, v_792_R33x, v_834, v_657, v_656, v_655, v_654, v_653, v_652, v_651, v_891_R34x, v_659, v_658, v_967_R35x, v_796_R33x, v_900b, v_811, v_810, v_707, v_965_R35x, v_706, v_701, v_700, v_723, v_722, v_721, v_845, v_727, v_726, v_839, v_724, v_837, v_836, v_835, v_728, v_833, v_832, v_831, v_830, v_819, v_818, v_783a_R33x, v_709, v_708, v_773_R33x, v_909, v_908, v_903, v_902, v_901, v_900, v_907, v_906, v_905, v_904, R3_4_2, R3_4_1, v_784_R33x, v_734, v_821, v_736, v_703, v_823, v_824, v_825, v_732, v_733, v_883_R34x, v_702, v_894_R34x, v_A21d, v_A21a, I20060605-1430, R3_2_1, R3_2_2, jsr308_A22, v_767, v_766, v_765, v_764, v_763, v_762, v_761, v_760, v_769, v_768, v_786_R33x, v_972_R35x, v_789_R33x, v_709a, v_864, v_865, v_866, v_867, v_860, v_861, v_949, v_948, v_947, v_946, v_945, v_944, v_868, v_869, v_941, v_940, v_781_R33x, v_681_R32x, v_889_R34x, v_838a_before_196200, v_682_R32x, v_686a_R32x, v_687_R32x, v_671a, v_691_R32x, v_673_R32x, R3_5_1, v_839a_before_182537, v_911, v_A17b, v_A17c, v_896_R34x, v_828, v_829, v_738, v_739, v_820, v_735, v_822, v_737, v_730, v_731, v_778_R33x, v_827, R3_3_1, R3_3_2, v_973_R35x, v_914, v_915, v_916, v_917, v_910, v_862, v_912, v_913, v_892_R34x, v_863, v_918, v_919, v_785_R33x, v_859, v_858, v_893_R34x, v_855, v_854, v_857, v_856, v_851, v_850, v_853, v_852, HEAD
Branch point for: Startup_R32x, R3_4_1_maintenance_patches, JSR_308, R3_4_maintenance_patches, R3_2_1_maintenance, R3_4_IES_maintenance, R3_3_maintenance, TARGET_321, R3_4_maintenance, R3_5_maintenance, R3_2_1_patches, R3_2_maintenance
Changes since 1.4: +1 -1 lines
HEAD - Fix existing or missing copyrights
/*******************************************************************************
 * Copyright (c) 2005, 2006 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 Corporation - initial API and implementation
 *******************************************************************************/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.MessageFormat;
import java.util.ArrayList;

public class GenerateBuildScript {

	private static final String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
	private static final String HEADER=
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + //$NON-NLS-1$
		"<project name=\"export-executable\" default=\"build_executable\">" +LINE_SEPARATOR + //$NON-NLS-1$
		"    <target name=\"build_executable\">" + LINE_SEPARATOR; //$NON-NLS-1$

	private static final String SOURCE_FILES =
		"	    <echo message=\"compiling sources      -> .o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"        <apply failonerror=\"true\" executable=\"$'{'gcc-path'}'/bin/{0}\" dest=\"{1}\" parallel=\"false\">" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"--verbose\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"--classpath={1}\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-O2\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-c\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-fassume-compiled\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-march=pentium4\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-mfpmath=sse\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <srcfile/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <targetfile/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <fileset dir=\"{1}\" includes=\"**/*.java\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <mapper type=\"glob\" from=\"*.java\" to=\"*.o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"        </apply>" + LINE_SEPARATOR + LINE_SEPARATOR; //$NON-NLS-1$
	private static final String FOOTER =
		"        <echo message=\"linking .o -> $'{'binaryname'}'\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"        <apply failonerror=\"true\" executable=\"$'{'gcc-path'}'/bin/{0}\" parallel=\"true\">" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"--verbose\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg line =\"-o $'{'dest'}'/$'{'binaryname'}'\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-fassume-compiled\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-march=pentium4\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-mfpmath=sse\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg line=\"--main=org.eclipse.jdt.internal.compiler.batch.Main\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <fileset dir=\"{1}\" includes=\"**/*.o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"       </apply>" + LINE_SEPARATOR + //$NON-NLS-1$
		"    </target>" + LINE_SEPARATOR + //$NON-NLS-1$
		"</project>" + LINE_SEPARATOR; //$NON-NLS-1$

	private static void collectAllFiles(File root, ArrayList collector, FileFilter fileFilter) {
		File[] files = root.listFiles(fileFilter);
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				collectAllFiles(files[i], collector, fileFilter);
			} else {
				String newElement = files[i].getAbsolutePath();
				newElement = newElement.replace('\\', '/');
				collector.add(newElement);
			}
		}
	}

	private static void dumpAllProperties(Writer writer, File sourceDir, ArrayList collector, String gcj_exe, String dest_dir) throws IOException {
		writer.write("        <echo message=\"compiling resources   -> .o\"/>" + LINE_SEPARATOR); //$NON-NLS-1$
		for (int i = 0, max = collector.size(); i < max; i++) {
			String absolutePath = (String) collector.get(i);
			String fileName = absolutePath.substring(sourceDir.getAbsolutePath().length() + 1); 
			writer.write(MessageFormat.format("  		<exec dir=\"{1}\" executable=\"$'{'gcc-path'}'/bin/{0}\">" + LINE_SEPARATOR, new Object[] { gcj_exe, dest_dir})); //$NON-NLS-1$
			writer.write("  		<arg line=\"--resource "); //$NON-NLS-1$
			writer.write(fileName + " " + fileName + " -c -o " + getObjectName(fileName) + "\"/>" + LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			writer.write("  		</exec>" + LINE_SEPARATOR); //$NON-NLS-1$
		}
	}

	private static void dumpAllClassFiles(Writer writer, File sourceDir, ArrayList collector, String gcj_exe, String dest_dir) throws IOException {
		writer.write("        <echo message=\"compiling class files   -> .o\"/>" + LINE_SEPARATOR); //$NON-NLS-1$
		writer.write(
				MessageFormat.format(
		"        <apply failonerror=\"true\" executable=\"$'{'gcc-path'}'/bin/{0}\" dest=\"{1}\" parallel=\"false\">" + LINE_SEPARATOR + //$NON-NLS-1$
		"  			 <arg value=\"--verbose\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"--classpath={1}\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-O2\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-c\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-fassume-compiled\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-march=pentium4\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-mfpmath=sse\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <srcfile/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <arg value=\"-o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <targetfile/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <fileset dir=\"{1}\" includes=\"**/*.class\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"            <mapper type=\"glob\" from=\"*.class\" to=\"*.o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
		"        </apply>" + LINE_SEPARATOR + LINE_SEPARATOR,//$NON-NLS-1$
		new Object[] {
				gcj_exe,
				dest_dir
		}));
	}

	private static String getObjectName(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf('.')) + ".o"; //$NON-NLS-1$
	}
			
	public static void main(String[] args) {
		if (args.length != 5) {
			System.out.println("Usage: script_name directory gcj_exe_name dest_dir source/bin"); //$NON-NLS-1$
			return;
		}
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(args[0])));
			writer.write(HEADER);
			File sourceDir = new File(args[1]);
			if (sourceDir.exists()) { 
				ArrayList collector = new ArrayList();
				collectAllFiles(sourceDir, collector, new FileFilter() {
					public boolean accept(File pathname) {
						String fileName = pathname.getAbsolutePath();
						return pathname.isDirectory() || fileName.endsWith(".rsc") || fileName.endsWith(".properties"); //$NON-NLS-1$ //$NON-NLS-2$
					}
				});
				dumpAllProperties(writer, sourceDir, collector, args[2], args[3]);
				if ("source".equals(args[4])) { //$NON-NLS-1$
					writer.write(MessageFormat.format(SOURCE_FILES, new Object[] {args[2], args[3]}));
				} else {
					collector = new ArrayList();
					collectAllFiles(sourceDir, collector, new FileFilter() {
						public boolean accept(File pathname) {
							String fileName = pathname.getAbsolutePath();
							return pathname.isDirectory() || fileName.endsWith(".class"); //$NON-NLS-1$
						}
					});
					dumpAllClassFiles(writer, sourceDir, collector, args[2], args[3]);				
				}
			}
			writer.write(MessageFormat.format(FOOTER, new Object[] {args[2], args[3]}));
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}