svn path=/branches/mono-1-1-9/mcs/; revision=51207
[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 src;
39                 private bool m_bAllowUnsafeCode;
40                 private Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject m_projObject;
41     
42                 public string ext_refs = "";
43                 public string switches = "";
44
45                 public bool AllowUnsafeCode
46                 {
47                         get { return m_bAllowUnsafeCode; }
48                 }
49     
50                 public ArrayList libdirs = new ArrayList();
51                 
52                 public Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject Proyecto 
53                 {
54                         get { return m_projObject; }
55                 }
56     
57                 // Project desirialization
58                 protected Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject LoadPrjFromFile (string strIn)
59                 {
60                         FileStream fs = new FileStream (strIn, FileMode.Open, FileAccess.Read);
61             
62                         XmlSerializer xmlSer = new XmlSerializer (typeof(Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject));
63                         Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject prjObj = (Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject) xmlSer.Deserialize (fs);
64             
65                         fs.Close();
66             
67                         return (prjObj);
68                 }
69
70                 public CsprojInfo(bool isUnixMode, bool isMcsMode, string name, string guid, string csprojpath)
71                 {
72                         this.name = name;
73                         this.guid = guid;
74                         this.csprojpath = csprojpath;
75     
76                         makename = name.Replace('.','_').ToUpper();
77                         makename_ext = makename + "_EXT";
78                         m_bAllowUnsafeCode = false;
79     
80                         // convert backslashes to slashes               
81                         csprojpath = csprojpath.Replace("\\", "/");
82
83                         // loads the file in order to deserialize and
84                         // build the object graph
85                         try 
86                         {
87                                 m_projObject = LoadPrjFromFile (csprojpath);
88                         } 
89                         catch (Exception exc) 
90                         {
91                                 Console.WriteLine("CurrentDirectory={0}", Directory.GetCurrentDirectory());
92                                 Console.WriteLine (
93                                         "Could not load the file {0}\n{1}: {2}\n{3}",
94                                         csprojpath,
95                                         exc.GetType().Name,
96                                         exc.Message,
97                                         exc.StackTrace
98                                         );
99                                 return;                 
100                         }
101
102                         // Establish if the allow unsafe code flag is true
103                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.Config cf in m_projObject.CSHARP.Build.Settings.Config)
104                         {
105                                 if(cf.AllowUnsafeBlocks == true)
106                                         m_bAllowUnsafeCode = true;
107                         }
108                 
109                         switch (m_projObject.CSHARP.Build.Settings.OutputType)
110                         {
111                                 case "Library":
112                                         makename_ext = makename + "_DLL";
113                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".dll";
114                                         switches += " -target:library";
115                                         break;
116     
117                                 case "Exe":
118                                         makename_ext = makename + "_EXE";
119                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";
120                                         switches += " -target:exe";
121                                         break;
122     
123                                 case "WinExe":
124                                         makename_ext = makename + "_EXE";
125                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";
126                                         switches += " -target:winexe";
127                                         break;
128     
129                                 default:
130                                         throw new NotSupportedException("Unsupported OutputType: " + m_projObject.CSHARP.Build.Settings.OutputType);
131                         
132                         }
133     
134                         src = "";    
135                         string basePath = Path.GetDirectoryName(csprojpath);
136                         string s;
137     
138                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)
139                         {
140                                 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.Compile)
141                                 {
142                                         if (src != "")
143                                         {
144                                                 src += " \\\n\t";
145                                         }
146                                 
147                                         string filePath = fl.Link;
148                                         if (filePath == null)
149                                                 filePath = fl.RelPath;
150                                 
151                                         s = System.IO.Path.Combine(basePath, filePath);
152                                         s = s.Replace("\\", "/");
153                                         if (SlnMaker.slash != "/")
154                                                 s = s.Replace("/", SlnMaker.slash);
155         
156                                         // Test for spaces
157                                         if (isUnixMode == false) {
158                                                 // We are in win32 using a cmd.exe or other
159                                                 // DOS shell
160                                                 if(s.IndexOf(' ') > -1) {
161                                                         src += String.Format("\"{0}\"", s);
162                                                 } else {
163                                                         src += s;
164                                                 }
165                                         } else {
166                                                 // We are in *NIX or some other
167                                                 // GNU like shell
168                                                 src += s.Replace(" ", "\\ ");
169                                         }
170                                 }                       
171                         }
172                 
173                         res = "";
174                         string rootNS = m_projObject.CSHARP.Build.Settings.RootNamespace;
175                         string relPath;
176                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)
177                         {
178                                 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.EmbeddedResource)
179                                 {
180                                         if (res != "") {
181                                                 res += " \\\n\t";
182                                         }
183                         
184                                         relPath = fl.RelPath.Replace("\\", "/");
185                                         s = System.IO.Path.Combine(basePath, relPath);
186                                         s = String.Format("-resource:{0},{1}", s, rootNS + "." + relPath.Replace("/", "."));
187                                         s = s.Replace("\\", "/");
188                                         if (SlnMaker.slash != "/")
189                                                 s = s.Replace("/", SlnMaker.slash);
190                                         res += s;
191                                 }
192                         }               
193                 }
194         }    
195 }