New test.
[mono.git] / mcs / tools / prj2make / CsprojInfo.cs
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.
17
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;
25
26
27 namespace Mfconsulting.General.Prj2Make
28 {
29         class CsprojInfo
30         {
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;
42     
43                 public string ext_refs = "";
44                 public string switches = "";
45
46                 public bool AllowUnsafeCode
47                 {
48                         get { return m_bAllowUnsafeCode; }
49                 }
50     
51                 public ArrayList libdirs = new ArrayList();
52                 
53                 public Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject Proyecto 
54                 {
55                         get { return m_projObject; }
56                 }
57     
58                 // Project desirialization
59                 protected Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject LoadPrjFromFile (string strIn)
60                 {
61                         FileStream fs = new FileStream (strIn, FileMode.Open, FileAccess.Read);
62             
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);
65             
66                         fs.Close();
67             
68                         return (prjObj);
69                 }
70
71                 // Character to quote
72                 
73                 static char [] quotable = new char [] { ' ', '(', ')' };
74
75                 static public string Quote (string s)
76                 {
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;
86                                         }
87                                         sb.Append (c);
88                                 }
89                                 return sb.ToString ();
90                         }
91                 }
92                 
93                 public CsprojInfo(bool isUnixMode, bool isMcsMode, string name, string guid, string csprojpath)
94                 {
95                         this.name = name;
96                         this.guid = guid;
97                         this.csprojpath = csprojpath;
98     
99                         makename = name.Replace('.','_').ToUpper();
100                         makename_ext = makename + "_EXT";
101                         m_bAllowUnsafeCode = false;
102     
103                         // convert backslashes to slashes               
104                         csprojpath = csprojpath.Replace("\\", "/");
105
106                         // loads the file in order to deserialize and
107                         // build the object graph
108                         try 
109                         {
110                                 m_projObject = LoadPrjFromFile (csprojpath);
111                         } 
112                         catch (Exception exc) 
113                         {
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
121                                         );
122                                 return;                 
123                         }
124
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)
127                         {
128                                 if(cf.AllowUnsafeBlocks == true)
129                                         m_bAllowUnsafeCode = true;
130                         }
131                 
132                         switch (m_projObject.CSHARP.Build.Settings.OutputType)
133                         {
134                                 case "Library":
135                                         makename_ext = makename + "_DLL";
136                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".dll";
137                                         switches += " -target:library";
138                                         break;
139     
140                                 case "Exe":
141                                         makename_ext = makename + "_EXE";
142                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";
143                                         switches += " -target:exe";
144                                         break;
145     
146                                 case "WinExe":
147                                         makename_ext = makename + "_EXE";
148                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";
149                                         switches += " -target:winexe";
150                                         break;
151     
152                                 default:
153                                         throw new NotSupportedException("Unsupported OutputType: " + m_projObject.CSHARP.Build.Settings.OutputType);
154                         
155                         }
156     
157                         src = "";    
158                         string basePath = Path.GetDirectoryName(csprojpath);
159                         string s;
160     
161                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)
162                         {
163                                 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.Compile)
164                                 {
165                                         if (src != "")
166                                         {
167                                                 src += " \\\n\t";
168                                         }
169                                 
170                                         string filePath = fl.Link;
171                                         if (filePath == null)
172                                                 filePath = fl.RelPath;
173                                 
174                                         s = System.IO.Path.Combine(basePath, filePath);
175                                         s = s.Replace("\\", "/");
176                                         if (SlnMaker.slash != "/")
177                                                 s = s.Replace("/", SlnMaker.slash);
178         
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;
187                                                 }
188                                         } else {
189                                                 // We are in *NIX or some other
190                                                 // GNU like shell
191                                                 src += s.Replace(" ", "\\ ");
192                                         }
193                                 }                       
194                         }
195                 
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)
201                         {
202                                 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.EmbeddedResource)
203                                 {
204                                         if (res != "") {
205                                                 res += " \\\n\t";
206                                         }
207                         
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");
218                                         }
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;
225                                 }
226                         }               
227                 }
228         }    
229 }