merge -r 58060:58217
[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                 public CsprojInfo(bool isUnixMode, bool isMcsMode, string name, string guid, string csprojpath)
72                 {
73                         this.name = name;
74                         this.guid = guid;
75                         this.csprojpath = csprojpath;
76     
77                         makename = name.Replace('.','_').ToUpper();
78                         makename_ext = makename + "_EXT";
79                         m_bAllowUnsafeCode = false;
80     
81                         // convert backslashes to slashes               
82                         csprojpath = csprojpath.Replace("\\", "/");
83
84                         // loads the file in order to deserialize and
85                         // build the object graph
86                         try 
87                         {
88                                 m_projObject = LoadPrjFromFile (csprojpath);
89                         } 
90                         catch (Exception exc) 
91                         {
92                                 Console.WriteLine("CurrentDirectory={0}", Directory.GetCurrentDirectory());
93                                 Console.WriteLine (
94                                         "Could not load the file {0}\n{1}: {2}\n{3}",
95                                         csprojpath,
96                                         exc.GetType().Name,
97                                         exc.Message,
98                                         exc.StackTrace
99                                         );
100                                 return;                 
101                         }
102
103                         // Establish if the allow unsafe code flag is true
104                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.Config cf in m_projObject.CSHARP.Build.Settings.Config)
105                         {
106                                 if(cf.AllowUnsafeBlocks == true)
107                                         m_bAllowUnsafeCode = true;
108                         }
109                 
110                         switch (m_projObject.CSHARP.Build.Settings.OutputType)
111                         {
112                                 case "Library":
113                                         makename_ext = makename + "_DLL";
114                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".dll";
115                                         switches += " -target:library";
116                                         break;
117     
118                                 case "Exe":
119                                         makename_ext = makename + "_EXE";
120                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";
121                                         switches += " -target:exe";
122                                         break;
123     
124                                 case "WinExe":
125                                         makename_ext = makename + "_EXE";
126                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";
127                                         switches += " -target:winexe";
128                                         break;
129     
130                                 default:
131                                         throw new NotSupportedException("Unsupported OutputType: " + m_projObject.CSHARP.Build.Settings.OutputType);
132                         
133                         }
134     
135                         src = "";    
136                         string basePath = Path.GetDirectoryName(csprojpath);
137                         string s;
138     
139                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)
140                         {
141                                 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.Compile)
142                                 {
143                                         if (src != "")
144                                         {
145                                                 src += " \\\n\t";
146                                         }
147                                 
148                                         string filePath = fl.Link;
149                                         if (filePath == null)
150                                                 filePath = fl.RelPath;
151                                 
152                                         s = System.IO.Path.Combine(basePath, filePath);
153                                         s = s.Replace("\\", "/");
154                                         if (SlnMaker.slash != "/")
155                                                 s = s.Replace("/", SlnMaker.slash);
156         
157                                         // Test for spaces
158                                         if (isUnixMode == false) {
159                                                 // We are in win32 using a cmd.exe or other
160                                                 // DOS shell
161                                                 if(s.IndexOf(' ') > -1) {
162                                                         src += String.Format("\"{0}\"", s);
163                                                 } else {
164                                                         src += s;
165                                                 }
166                                         } else {
167                                                 // We are in *NIX or some other
168                                                 // GNU like shell
169                                                 src += s.Replace(" ", "\\ ");
170                                         }
171                                 }                       
172                         }
173                 
174                         res = "";
175                         resgen = "";
176                         string rootNS = m_projObject.CSHARP.Build.Settings.RootNamespace;
177                         string relPath;
178                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)
179                         {
180                                 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.EmbeddedResource)
181                                 {
182                                         if (res != "") {
183                                                 res += " \\\n\t";
184                                         }
185                         
186                                         relPath = fl.RelPath.Replace("\\", "/");
187                                         s = System.IO.Path.Combine(basePath, relPath);
188                                         if (Path.GetExtension (s) == ".resx") {
189                                                 string path = s;
190                                                 path = path.Replace (@"\", "/");
191                                                 if (SlnMaker.slash != "/")
192                                                         path = path.Replace("/", SlnMaker.slash);
193                                                 resgen += String.Format ("{0} ", path);
194                                                 s = Path.ChangeExtension (s, ".resources");
195                                                 relPath = Path.ChangeExtension (relPath, ".resources");
196                                         }
197                                         s = String.Format("-resource:{0},{1}", s, rootNS + "." + relPath.Replace("/", "."));
198                                         s = s.Replace("\\", "/");
199                                         if (SlnMaker.slash != "/")
200                                                 s = s.Replace("/", SlnMaker.slash);
201                                         res += s;
202                                 }
203                         }               
204                 }
205         }    
206 }