2010-02-13 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / prj2make / CsprojInfo.cs
blob2f4e5a1d5167cafeea32c2e48a8ca67a178cfe9d
1 // Copyright (c) 2004 Francisco T. Martinez <paco@mfcon.com>
2 // All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Library General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 using System;
19 using System.Collections;
20 using System.IO;
21 using System.Text;
22 using System.Text.RegularExpressions;
23 using System.Xml;
24 using System.Xml.Serialization;
27 namespace Mfconsulting.General.Prj2Make
29 class CsprojInfo
31 public readonly string name;
32 public readonly string guid;
33 public readonly string csprojpath;
34 public string makename;
35 public string makename_ext;
36 public string assembly_name;
37 public string res;
38 public string resgen;
39 public string src;
40 private bool m_bAllowUnsafeCode;
41 private Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject m_projObject;
43 public string ext_refs = "";
44 public string switches = "";
46 public bool AllowUnsafeCode
48 get { return m_bAllowUnsafeCode; }
51 public ArrayList libdirs = new ArrayList();
53 public Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject Proyecto
55 get { return m_projObject; }
58 // Project desirialization
59 protected Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject LoadPrjFromFile (string strIn)
61 FileStream fs = new FileStream (strIn, FileMode.Open, FileAccess.Read);
63 XmlSerializer xmlSer = new XmlSerializer (typeof(Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject));
64 Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject prjObj = (Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject) xmlSer.Deserialize (fs);
66 fs.Close();
68 return (prjObj);
71 // Character to quote
73 static char [] quotable = new char [] { ' ', '(', ')' };
75 static public string Quote (string s)
77 if (s.IndexOfAny (quotable) == -1)
78 return s;
79 else {
80 StringBuilder sb = new StringBuilder ();
81 foreach (char c in s){
82 switch (c){
83 case ' ': case '(': case ')':
84 sb.Append ('\\');
85 break;
87 sb.Append (c);
89 return sb.ToString ();
93 public CsprojInfo(bool isUnixMode, bool isMcsMode, string name, string guid, string csprojpath)
95 this.name = name;
96 this.guid = guid;
97 this.csprojpath = csprojpath;
99 makename = name.Replace('.','_').ToUpper();
100 makename_ext = makename + "_EXT";
101 m_bAllowUnsafeCode = false;
103 // convert backslashes to slashes
104 csprojpath = csprojpath.Replace("\\", "/");
106 // loads the file in order to deserialize and
107 // build the object graph
108 try
110 m_projObject = LoadPrjFromFile (csprojpath);
112 catch (Exception exc)
114 Console.WriteLine("CurrentDirectory={0}", Directory.GetCurrentDirectory());
115 Console.WriteLine (
116 "Could not load the file {0}\n{1}: {2}\n{3}",
117 csprojpath,
118 exc.GetType().Name,
119 exc.Message,
120 exc.StackTrace
122 return;
125 // Establish if the allow unsafe code flag is true
126 foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.Config cf in m_projObject.CSHARP.Build.Settings.Config)
128 if(cf.AllowUnsafeBlocks == true)
129 m_bAllowUnsafeCode = true;
132 switch (m_projObject.CSHARP.Build.Settings.OutputType)
134 case "Library":
135 makename_ext = makename + "_DLL";
136 assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".dll";
137 switches += " -target:library";
138 break;
140 case "Exe":
141 makename_ext = makename + "_EXE";
142 assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";
143 switches += " -target:exe";
144 break;
146 case "WinExe":
147 makename_ext = makename + "_EXE";
148 assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";
149 switches += " -target:winexe";
150 break;
152 default:
153 throw new NotSupportedException("Unsupported OutputType: " + m_projObject.CSHARP.Build.Settings.OutputType);
157 src = "";
158 string basePath = Path.GetDirectoryName(csprojpath);
159 string s;
161 foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)
163 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.Compile)
165 if (src != "")
167 src += " \\\n\t";
170 string filePath = fl.Link;
171 if (filePath == null)
172 filePath = fl.RelPath;
174 s = System.IO.Path.Combine(basePath, filePath);
175 s = s.Replace("\\", "/");
176 if (SlnMaker.slash != "/")
177 s = s.Replace("/", SlnMaker.slash);
179 // Test for spaces
180 if (isUnixMode == false) {
181 // We are in win32 using a cmd.exe or other
182 // DOS shell
183 if(s.IndexOf(' ') > -1) {
184 src += String.Format("\"{0}\"", s);
185 } else {
186 src += s;
188 } else {
189 // We are in *NIX or some other
190 // GNU like shell
191 src += s.Replace(" ", "\\ ");
196 res = "";
197 resgen = "";
198 string rootNS = m_projObject.CSHARP.Build.Settings.RootNamespace;
199 string relPath;
200 foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)
202 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.EmbeddedResource)
204 if (res != "") {
205 res += " \\\n\t";
208 relPath = fl.RelPath.Replace("\\", "/");
209 s = System.IO.Path.Combine(basePath, relPath);
210 if (Path.GetExtension (s) == ".resx") {
211 string path = s;
212 path = path.Replace (@"\", "/");
213 if (SlnMaker.slash != "/")
214 path = path.Replace("/", SlnMaker.slash);
215 resgen += String.Format ("{0} ", Quote (path));
216 s = Path.ChangeExtension (s, ".resources");
217 relPath = Path.ChangeExtension (relPath, ".resources");
219 s = s.Replace("\\", "/");
220 string rp = relPath.Replace ("/", ".").Replace ("\\", "/");
221 s = String.Format("-resource:{0},{1}", Quote (s), rootNS + "." + rp);
222 if (SlnMaker.slash != "/")
223 s = s.Replace("/", SlnMaker.slash);
224 res += s;