tools/builder/ABuilder.cs
changeset 388 4c346c316a49
child 403 fe1c5a3a4ba2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/builder/ABuilder.cs	Sat Jan 27 03:10:31 2007 +0000
     1.3 @@ -0,0 +1,165 @@
     1.4 +using System;
     1.5 +using System.IO;
     1.6 +using System.Diagnostics;
     1.7 +
     1.8 +namespace builder
     1.9 +{
    1.10 +	public abstract class ABuilder
    1.11 +	{
    1.12 +		public Config cfg;
    1.13 +
    1.14 +		public ABuilder()
    1.15 +		{
    1.16 +		}
    1.17 +
    1.18 +		public abstract bool BuildLibrary(Package pkg, Library lib, ref string _binName, ref string _binPath);
    1.19 +
    1.20 +		public void UpdateRevisionInfo(Package pkg, Library lib)
    1.21 +		{
    1.22 +			string path = Config.PathFormat("{0}/{1}", cfg.SourceBase, lib.LocalPath);
    1.23 +			string file = Config.PathFormat("{0}/svn_version.h", path);
    1.24 +
    1.25 +			if (File.Exists(file))
    1.26 +			{
    1.27 +				UpdateRevisionInfo(path, file);
    1.28 +			}
    1.29 +		}
    1.30 +
    1.31 +		public void CopyFolder(Package pkg, string source, string dest, string [] omits)
    1.32 +		{
    1.33 +			string from_base = Config.PathFormat("{0}/{1}", cfg.SourceBase, source);
    1.34 +			string to_base = Config.PathFormat("{0}/{1}/{2}", 
    1.35 +				cfg.OutputBase, 
    1.36 +				pkg.GetBaseFolder(), 
    1.37 +				dest);
    1.38 +
    1.39 +			string [] files = Directory.GetFiles(from_base);
    1.40 +			string file;
    1.41 +
    1.42 +			for (int i=0; i<files.Length; i++)
    1.43 +			{
    1.44 +				file = Path.GetFileName(files[i]);
    1.45 +
    1.46 +				if (omits != null)
    1.47 +				{
    1.48 +					bool skip = false;
    1.49 +					for (int j=0; j<omits.Length; j++)
    1.50 +					{
    1.51 +						if (file.CompareTo(omits[j]) == 0)
    1.52 +						{
    1.53 +							skip = true;
    1.54 +							break;
    1.55 +						}
    1.56 +					}
    1.57 +					if (skip)
    1.58 +					{
    1.59 +						continue;
    1.60 +					}
    1.61 +				}
    1.62 +				dest = Config.PathFormat("{0}/{1}", to_base, file);
    1.63 +				File.Copy(files[i], dest, true);
    1.64 +			}
    1.65 +		}
    1.66 +
    1.67 +		public void UpdateRevisionInfo(string path, string file)
    1.68 +		{
    1.69 +			ProcessStartInfo info = new ProcessStartInfo();
    1.70 +
    1.71 +			info.WorkingDirectory = path;
    1.72 +			info.FileName = cfg.SVNVersion;
    1.73 +			info.Arguments = "--committed " + path;
    1.74 +			info.UseShellExecute = false;
    1.75 +			info.RedirectStandardOutput = true;
    1.76 +
    1.77 +			Process p = Process.Start(info);
    1.78 +			string output = p.StandardOutput.ReadToEnd();
    1.79 +			p.WaitForExit();
    1.80 +			p.Close();
    1.81 +
    1.82 +			string [] revs = output.Split(":".ToCharArray(), 2);
    1.83 +			if (revs.Length < 1)
    1.84 +			{
    1.85 +				return;
    1.86 +			}
    1.87 +
    1.88 +			string rev = null;
    1.89 +			if (revs.Length == 1)
    1.90 +			{
    1.91 +				rev = revs[0];
    1.92 +			} 
    1.93 +			else 
    1.94 +			{
    1.95 +				rev = revs[1];
    1.96 +			}
    1.97 +
    1.98 +			rev = rev.Trim();
    1.99 +			rev = rev.Replace("M", "");
   1.100 +			rev = rev.Replace("S", "");
   1.101 +
   1.102 +			string vers = cfg.ProductVersion.Replace(".", ",");
   1.103 +
   1.104 +			File.Delete(file);
   1.105 +			StreamWriter sw = File.CreateText(file);
   1.106 +
   1.107 +			sw.WriteLine("/** This file is autogenerated by build scripts */");
   1.108 +			sw.WriteLine("");
   1.109 +			sw.WriteLine("#ifndef _INCLUDE_SVN_VERSION_H_");
   1.110 +			sw.WriteLine("#define _INCLUDE_SVN_VERSION_H_");
   1.111 +			sw.WriteLine("");
   1.112 +			sw.WriteLine("#define SVN_REVISION			{0}", rev);
   1.113 +			sw.WriteLine("#define SVN_REVISION_STRING	\"{0}\"", rev);
   1.114 +			sw.WriteLine("#define SVN_FILE_VERSION		{0},{1}", vers, rev);
   1.115 +			sw.WriteLine("");
   1.116 +			sw.WriteLine("#endif //_INCLUDE_SVN_VERSION_H_");
   1.117 +			sw.WriteLine("");
   1.118 +
   1.119 +			sw.Close();
   1.120 +		}
   1.121 +
   1.122 +		public void BuildPackage(Package pkg)
   1.123 +		{
   1.124 +			string path = Config.PathFormat("{0}/{1}", cfg.OutputBase, pkg.GetBaseFolder());
   1.125 +
   1.126 +			if (!Directory.Exists(path))
   1.127 +			{
   1.128 +				Directory.CreateDirectory(path);
   1.129 +			}
   1.130 +
   1.131 +			/* Create all dirs */
   1.132 +			string [] paths = pkg.GetFolders();
   1.133 +			for (int i=0; i<paths.GetLength(0); i++)
   1.134 +			{
   1.135 +				path = Config.PathFormat("{0}/{1}/{2}", cfg.OutputBase, pkg.GetBaseFolder(), paths[i]);
   1.136 +				if (!Directory.Exists(path))
   1.137 +				{
   1.138 +					Directory.CreateDirectory(path);
   1.139 +				}
   1.140 +			}
   1.141 +
   1.142 +			/* Do primitive copies */
   1.143 +			pkg.OnCopyFolders(this);
   1.144 +			pkg.OnCopyFiles();
   1.145 +
   1.146 +			/* Do libraries */
   1.147 +			Library [] libs = pkg.GetLibraries();
   1.148 +			string bin = null, binpath = null;
   1.149 +			for (int i=0; i<libs.Length; i++)
   1.150 +			{
   1.151 +				UpdateRevisionInfo(pkg, libs[i]);
   1.152 +				if (BuildLibrary(pkg, libs[i], ref bin, ref binpath))
   1.153 +				{
   1.154 +					path = Config.PathFormat("{0}/{1}/{2}/{3}",
   1.155 +						cfg.OutputBase,
   1.156 +						pkg.GetBaseFolder(),
   1.157 +						libs[i].Destination,
   1.158 +						bin);
   1.159 +					File.Copy(binpath, path, true);
   1.160 +				}
   1.161 +				else 
   1.162 +				{
   1.163 +					throw new System.Exception("Failed to compile library: " + libs[i].Name);
   1.164 +				}
   1.165 +			}
   1.166 +		}
   1.167 +	}
   1.168 +}