Add Prj2Make from Francisco Martinez
[mono.git] / mcs / tools / prj2make / CsprojInfo.cs
1 // Copyright (c) 2004 Francisco T. Martinez <paco@mfcon.com>\r
2 // All rights reserved.\r
3 //\r
4 // This program is free software; you can redistribute it and/or modify\r
5 // it under the terms of the GNU General Public License as published by\r
6 // the Free Software Foundation; either version 2 of the License, or\r
7 // (at your option) any later version.\r
8 // \r
9 // This program is distributed in the hope that it will be useful,\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12 // GNU Library General Public License for more details.\r
13 // \r
14 // You should have received a copy of the GNU General Public License\r
15 // along with this program; if not, write to the Free Software\r
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
17 \r
18 using System;\r
19 using System.Collections;\r
20 using System.IO;\r
21 using System.Text;\r
22 using System.Text.RegularExpressions;\r
23 using System.Xml;\r
24 using System.Xml.Serialization;\r
25 \r
26 \r
27 namespace Mfconsulting.General.Prj2Make\r
28 {\r
29         class CsprojInfo\r
30         {\r
31                 public readonly string name;\r
32                 public readonly string guid;\r
33                 public readonly string csprojpath;\r
34                 public string makename;\r
35                 public string makename_ext;\r
36                 public string assembly_name;\r
37                 public string res;\r
38                 public string src;\r
39                 private bool m_bAllowUnsafeCode;\r
40                 private Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject m_projObject;\r
41     \r
42                 public string ext_refs = "";\r
43                 public string switches = "";\r
44 \r
45                 public bool AllowUnsafeCode\r
46                 {\r
47                         get { return m_bAllowUnsafeCode; }\r
48                 }\r
49     \r
50                 public ArrayList libdirs = new ArrayList();\r
51                 \r
52                 public Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject Proyecto \r
53                 {\r
54                         get { return m_projObject; }\r
55                 }\r
56     \r
57                 // Project desirialization\r
58                 protected Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject LoadPrjFromFile (string strIn)\r
59                 {\r
60                         FileStream fs = new FileStream (strIn, FileMode.Open);\r
61             \r
62                         XmlSerializer xmlSer = new XmlSerializer (typeof(Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject));\r
63                         Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject prjObj = (Mfconsulting.General.Prj2Make.Schema.Csproj.VisualStudioProject) xmlSer.Deserialize (fs);\r
64             \r
65                         fs.Close();\r
66             \r
67                         return (prjObj);\r
68                 }\r
69 \r
70                 public CsprojInfo(bool isUnixMode, bool isMcsMode, string name, string guid, string csprojpath)\r
71                 {\r
72                         this.name = name;\r
73                         this.guid = guid;\r
74                         this.csprojpath = csprojpath;\r
75     \r
76                         makename = name.Replace('.','_').ToUpper();\r
77                         makename_ext = makename + "_EXT";\r
78                         m_bAllowUnsafeCode = false;\r
79     \r
80                         // convert backslashes to slashes               \r
81                         csprojpath = csprojpath.Replace("\\", "/");\r
82 \r
83                         // loads the file in order to deserialize and\r
84                         // build the object graph\r
85                         try \r
86                         {\r
87                                 m_projObject = LoadPrjFromFile (csprojpath);\r
88                         } \r
89                         catch (Exception exc) \r
90                         {\r
91                                 Console.WriteLine("CurrentDirectory={0}", Directory.GetCurrentDirectory());\r
92                                 Console.WriteLine (\r
93                                         "Could not load the file {0}\n{1}: {2}\n{3}",\r
94                                         csprojpath,\r
95                                         exc.GetType().Name,\r
96                                         exc.Message,\r
97                                         exc.StackTrace\r
98                                         );\r
99                                 return;                 \r
100                         }\r
101 \r
102                         // Establish if the allow unsafe code flag is true\r
103                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.Config cf in m_projObject.CSHARP.Build.Settings.Config)\r
104                         {\r
105                                 if(cf.AllowUnsafeBlocks == true)\r
106                                         m_bAllowUnsafeCode = true;\r
107                         }\r
108                 \r
109                         switch (m_projObject.CSHARP.Build.Settings.OutputType)\r
110                         {\r
111                                 case "Library":\r
112                                         makename_ext = makename + "_DLL";\r
113                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".dll";\r
114                                         switches += " -target:library";\r
115                                         break;\r
116     \r
117                                 case "Exe":\r
118                                         makename_ext = makename + "_EXE";\r
119                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";\r
120                                         switches += " -target:exe";\r
121                                         break;\r
122     \r
123                                 case "WinExe":\r
124                                         makename_ext = makename + "_EXE";\r
125                                         assembly_name = m_projObject.CSHARP.Build.Settings.AssemblyName + ".exe";\r
126                                         switches += " -target:winexe";\r
127                                         break;\r
128     \r
129                                 default:\r
130                                         throw new NotSupportedException("Unsupported OutputType: " + m_projObject.CSHARP.Build.Settings.OutputType);\r
131                         \r
132                         }\r
133     \r
134                         src = "";    \r
135                         string basePath = Path.GetDirectoryName(csprojpath);\r
136                         string s;\r
137     \r
138                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)\r
139                         {\r
140                                 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.Compile)\r
141                                 {\r
142                                         if (src != "")\r
143                                         {\r
144                                                 src += " \\\n\t";\r
145                                         }\r
146                                 \r
147                                         string filePath = fl.Link;\r
148                                         if (filePath == null)\r
149                                                 filePath = fl.RelPath;\r
150                                 \r
151                                         s = System.IO.Path.Combine(basePath, filePath);\r
152                                         s = s.Replace("\\", "/");\r
153                                         if (SlnMaker.slash != "/")\r
154                                                 s = s.Replace("/", SlnMaker.slash);\r
155         \r
156                                         // Test for spaces\r
157                                         if (isUnixMode == false) {\r
158                                                 // We are in win32 using a cmd.exe or other\r
159                                                 // DOS shell\r
160                                                 if(s.IndexOf(' ') > -1) {\r
161                                                         src += String.Format("\"{0}\"", s);\r
162                                                 } else {\r
163                                                         src += s;\r
164                                                 }\r
165                                         } else {\r
166                                                 // We are in *NIX or some other\r
167                                                 // GNU like shell\r
168                                                 src += s.Replace(" ", "\\ ");\r
169                                         }\r
170                                 }                       \r
171                         }\r
172                 \r
173                         res = "";\r
174                         string rootNS = m_projObject.CSHARP.Build.Settings.RootNamespace;\r
175                         string relPath;\r
176                         foreach (Mfconsulting.General.Prj2Make.Schema.Csproj.File fl in m_projObject.CSHARP.Files.Include)\r
177                         {\r
178                                 if(fl.BuildAction == Mfconsulting.General.Prj2Make.Schema.Csproj.FileBuildAction.EmbeddedResource)\r
179                                 {\r
180                                         if (res != "") {\r
181                                                 res += " \\\n\t";\r
182                                         }\r
183                         \r
184                                         relPath = fl.RelPath.Replace("\\", "/");\r
185                                         s = System.IO.Path.Combine(basePath, relPath);\r
186                                         s = String.Format("-resource:{0},{1}", s, rootNS + "." + relPath.Replace("/", "."));\r
187                                         s = s.Replace("\\", "/");\r
188                                         if (SlnMaker.slash != "/")\r
189                                                 s = s.Replace("/", SlnMaker.slash);\r
190                                         res += s;\r
191                                 }\r
192                         }               \r
193                 }\r
194         }    \r
195 }\r