2007-10-17 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / tools / prj2make / PrjxInfo.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 PrjxInfo
30     {
31         public readonly string name;
32         public readonly string csprojpath;
33         public string makename;
34         public string makename_ext;
35         public string assembly_name;
36         public string res;
37         public string src;
38                 private bool m_bAllowUnsafeCode;
39         private Mfconsulting.General.Prj2Make.Schema.Prjx.Project m_projObject;
40     
41         public string ext_refs = "";
42         public string switches = "";
43
44                 public bool AllowUnsafeCode
45                 {
46                         get { return m_bAllowUnsafeCode; }
47                 }
48         
49         public Mfconsulting.General.Prj2Make.Schema.Prjx.Project Proyecto {
50                 get { return m_projObject; }
51         }
52     
53         // Project desirialization
54                 protected Mfconsulting.General.Prj2Make.Schema.Prjx.Project LoadPrjFromFile (string strIn)
55                 {
56                         FileStream fs = new FileStream (strIn, FileMode.Open);
57             
58                         XmlSerializer xmlSer = new XmlSerializer (typeof(Mfconsulting.General.Prj2Make.Schema.Prjx.Project));
59                         Mfconsulting.General.Prj2Make.Schema.Prjx.Project prjObj = (Mfconsulting.General.Prj2Make.Schema.Prjx.Project) xmlSer.Deserialize (fs);
60             
61                         fs.Close();
62             
63                         return (prjObj);
64                 }
65
66                 public PrjxInfo(bool isUnixMode, bool isMcsMode, string csprojpath)
67                 {
68                         Mfconsulting.General.Prj2Make.Schema.Prjx.Configuration activeConf = null;
69                 this.csprojpath = csprojpath;
70
71                 // convert backslashes to slashes               
72                 csprojpath = csprojpath.Replace("\\", "/");
73     
74                 m_bAllowUnsafeCode = false;
75                 
76                 // loads the file in order to deserialize and
77                 // build the object graph
78                 try {
79                         m_projObject = LoadPrjFromFile (csprojpath);
80                         } catch (Exception exc) {
81                         
82                                 Console.WriteLine ("Could not load the file {0}\n{1}: {2}",
83                                                 csprojpath,
84                                                 exc.GetType().Name,
85                                                 exc.Message
86                                         );
87                                 return;                 
88                         }
89
90                 this.name = m_projObject.name;
91
92                 makename = name.Replace('.','_').ToUpper();
93                 makename_ext = makename + "_EXT";
94
95                         // Get the configuration to be used and
96                         // copy it to a local configuration object
97                         foreach(Mfconsulting.General.Prj2Make.Schema.Prjx.Configuration cnfObj in m_projObject.Configurations.Configuration)
98                         {
99                                 if(cnfObj.name.CompareTo(m_projObject.Configurations.active) == 0)
100                                 {
101                                         // Assign the active configuration
102                                         activeConf = cnfObj;
103                                         break;
104                                 }
105                         }
106
107                         // Establish if the allow unsafe code flag is true
108                         if(activeConf.CodeGeneration.unsafecodeallowed == Mfconsulting.General.Prj2Make.Schema.Prjx.CodeGenerationUnsafecodeallowed.True)
109                         {
110                                 m_bAllowUnsafeCode = true;
111                         }
112                         
113                 switch (activeConf.CodeGeneration.target)
114                 {
115                         case "Library":
116                                 makename_ext = makename + "_DLL";
117                                 assembly_name = activeConf.Output.assembly + ".dll";
118                                 switches += " -target:library";
119                                 break;
120     
121                         case "Exe":
122                                 makename_ext = makename + "_EXE";
123                                 assembly_name = activeConf.Output.assembly + ".exe";
124                                 switches += " -target:exe";
125                                 break;
126     
127                         case "WinExe":
128                                 makename_ext = makename + "_EXE";
129                                 assembly_name = activeConf.Output.assembly + ".exe";
130                                 switches += " -target:winexe";
131                                 break;
132     
133                         default:
134                                 throw new NotSupportedException("Unsupported OutputType: " + activeConf.CodeGeneration.target);
135                         
136                 }
137     
138                 src = "";    
139                 string basePath = Path.GetDirectoryName(csprojpath);
140                 string s;
141                 
142                         // Process Source code files for compiling
143                 foreach (Mfconsulting.General.Prj2Make.Schema.Prjx.File f in m_projObject.Contents)
144                 {
145                         if(f.buildaction == Mfconsulting.General.Prj2Make.Schema.Prjx.FileBuildaction.Compile)
146                         {
147                                 if (src != "") {
148                                         src += " \\\n\t";
149                                 }
150                         
151                                 s = System.IO.Path.Combine(basePath, f.name);
152                                 s = s.Replace("\\", "/");
153                                 if (CmbxMaker.slash != "/")
154                                         s = s.Replace("/", CmbxMaker.slash);
155
156                                         // Test for spaces
157                                         if (isUnixMode == false)
158                                         {
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 += CsprojInfo.Quote (s);
170                                         }                       
171                         }
172                 }
173                 
174                 // Process resources for embedding
175                 res = "";
176                 string rootNS = this.name; 
177                 string relPath;
178                 foreach (Mfconsulting.General.Prj2Make.Schema.Prjx.File f in m_projObject.Contents)
179                 {
180                         if(f.buildaction == Mfconsulting.General.Prj2Make.Schema.Prjx.FileBuildaction.EmbedAsResource)
181                         {
182                                 if (src != "") {
183                                         src += " \\\n\t";
184                                 }
185                         
186                                 relPath = f.name.Replace("\\", "/");
187                                 s = System.IO.Path.Combine(basePath, relPath);
188                                 s = String.Format(" -resource:{0},{1}", s, System.IO.Path.GetFileName(relPath));
189                                 s = s.Replace("\\", "/");
190                                 if (CmbxMaker.slash != "/")
191                                         s = s.Replace("/", CmbxMaker.slash);
192                                 res += s;
193                         }
194                 }
195         }
196     }    
197 }