NETProjectGen.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Poco/UUID.h>
  23. #include <Poco/UUIDGenerator.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/File.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include "../ToolEnvironment.h"
  28. #include "../ToolSystem.h"
  29. #include "../Project/Project.h"
  30. #include "NETProjectGen.h"
  31. namespace ToolCore
  32. {
  33. NETProjectBase::NETProjectBase(Context* context, NETProjectGen* projectGen):
  34. Object(context), xmlFile_(new XMLFile(context)), projectGen_(projectGen)
  35. {
  36. }
  37. NETProjectBase::~NETProjectBase()
  38. {
  39. }
  40. void NETProjectBase::ReplacePathStrings(String& path)
  41. {
  42. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  43. ToolSystem* tsys = GetSubsystem<ToolSystem>();
  44. const String& atomicRoot = tenv->GetRootSourceDir();
  45. const String& scriptPlatform = projectGen_->GetScriptPlatform();
  46. path.Replace("$ATOMIC_ROOT$", atomicRoot, false);
  47. path.Replace("$SCRIPT_PLATFORM$", scriptPlatform, false);
  48. Project* project = tsys->GetProject();
  49. if (project)
  50. {
  51. path.Replace("$PROJECT_ROOT$", project->GetProjectPath(), false);
  52. }
  53. }
  54. NETCSProject::NETCSProject(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen)
  55. {
  56. }
  57. NETCSProject::~NETCSProject()
  58. {
  59. }
  60. void NETCSProject::CreateCompileItemGroup(XMLElement &projectRoot)
  61. {
  62. FileSystem* fs = GetSubsystem<FileSystem>();
  63. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  64. for (unsigned i = 0; i < sourceFolders_.Size(); i++)
  65. {
  66. const String& sourceFolder = sourceFolders_[i];
  67. Vector<String> result;
  68. fs->ScanDir(result, sourceFolder, "*.cs", SCAN_FILES, true);
  69. for (unsigned j = 0; j < result.Size(); j++)
  70. {
  71. XMLElement compile = igroup.CreateChild("Compile");
  72. compile.SetAttribute("Include", sourceFolder + result[j]);
  73. // put generated files into generated folder
  74. if (sourceFolder.Contains("Generated") && sourceFolder.Contains("CSharp") && sourceFolder.Contains("Packages") )
  75. {
  76. compile.CreateChild("Link").SetValue("Generated\\" + result[j]);
  77. }
  78. }
  79. }
  80. }
  81. void NETCSProject::CreateReferencesItemGroup(XMLElement &projectRoot)
  82. {
  83. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  84. FileSystem* fs = GetSubsystem<FileSystem>();
  85. const String& coreCLRAbsPath = tenv->GetNETCoreCLRAbsPath();
  86. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  87. XMLElement mscorlibref = igroup.CreateChild("Reference");
  88. mscorlibref.SetAttribute("Include", "mscorlib");
  89. mscorlibref.CreateChild("HintPath").SetValue(coreCLRAbsPath + "mscorlib.dll");
  90. mscorlibref.CreateChild("Private").SetValue("False");
  91. for (unsigned i = 0; i < references_.Size(); i++)
  92. {
  93. String ref = references_[i];
  94. String refpath = ref + ".dll";
  95. // we explicitly add mscorlib
  96. if (ref == "mscorlib")
  97. continue;
  98. XMLElement xref = igroup.CreateChild("Reference");
  99. xref.SetAttribute("Include", ref);
  100. // if we're a coreclr assembly, qualify it
  101. if (fs->FileExists(coreCLRAbsPath + refpath))
  102. {
  103. refpath = coreCLRAbsPath + refpath;
  104. }
  105. xref.CreateChild("HintPath").SetValue(refpath);
  106. xref.CreateChild("Private").SetValue("False");
  107. }
  108. }
  109. void NETCSProject::GetAssemblySearchPaths(String& paths)
  110. {
  111. paths.Clear();
  112. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  113. const String& coreCLRAbsPath = tenv->GetNETCoreCLRAbsPath();
  114. Vector<String> searchPaths;
  115. searchPaths.Push(coreCLRAbsPath);
  116. if (assemblySearchPaths_.Length())
  117. searchPaths.Push(assemblySearchPaths_);
  118. paths.Join(searchPaths, ";");
  119. }
  120. void NETCSProject::CreateReleasePropertyGroup(XMLElement &projectRoot)
  121. {
  122. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  123. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ");
  124. pgroup.CreateChild("DebugType").SetValue("full");
  125. pgroup.CreateChild("Optimize").SetValue("true");
  126. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_);
  127. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  128. pgroup.CreateChild("WarningLevel").SetValue("4");
  129. pgroup.CreateChild("ConsolePause").SetValue("false");
  130. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  131. pgroup.CreateChild("NoStdLib").SetValue("true");
  132. pgroup.CreateChild("NoConfig").SetValue("true");
  133. pgroup.CreateChild("NoCompilerStandardLib").SetValue("true");
  134. String assemblySearchPaths;
  135. GetAssemblySearchPaths(assemblySearchPaths);
  136. pgroup.CreateChild("AssemblySearchPaths").SetValue(assemblySearchPaths);
  137. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  138. const String& editorBinary = tenv->GetEditorBinary();
  139. if (!projectGen_->GetGameBuild())
  140. {
  141. XMLElement command = pgroup.CreateChild("CustomCommands").CreateChild("CustomCommands").CreateChild("Command");
  142. command.SetAttribute("type", "Execute");
  143. command.SetAttribute("command", editorBinary.CString());
  144. }
  145. }
  146. void NETCSProject::CreateDebugPropertyGroup(XMLElement &projectRoot)
  147. {
  148. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  149. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
  150. pgroup.CreateChild("DebugSymbols").SetValue("true");
  151. pgroup.CreateChild("DebugType").SetValue("full");
  152. pgroup.CreateChild("Optimize").SetValue("false");
  153. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_);
  154. pgroup.CreateChild("DefineConstants").SetValue("DEBUG;");
  155. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  156. pgroup.CreateChild("WarningLevel").SetValue("4");
  157. pgroup.CreateChild("ConsolePause").SetValue("false");
  158. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  159. pgroup.CreateChild("NoStdLib").SetValue("true");
  160. pgroup.CreateChild("NoConfig").SetValue("true");
  161. pgroup.CreateChild("NoCompilerStandardLib").SetValue("true");
  162. String assemblySearchPaths;
  163. GetAssemblySearchPaths(assemblySearchPaths);
  164. pgroup.CreateChild("AssemblySearchPaths").SetValue(assemblySearchPaths);
  165. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  166. const String& editorBinary = tenv->GetEditorBinary();
  167. if (!projectGen_->GetGameBuild())
  168. {
  169. XMLElement command = pgroup.CreateChild("CustomCommands").CreateChild("CustomCommands").CreateChild("Command");
  170. command.SetAttribute("type", "Execute");
  171. command.SetAttribute("command", editorBinary.CString());
  172. }
  173. }
  174. void NETCSProject::CreateMainPropertyGroup(XMLElement& projectRoot)
  175. {
  176. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  177. // Configuration
  178. XMLElement config = pgroup.CreateChild("Configuration");
  179. config.SetAttribute("Condition", " '$(Configuration)' == '' ");
  180. config.SetValue("Debug");
  181. // Platform
  182. XMLElement platform = pgroup.CreateChild("Platform");
  183. platform.SetAttribute("Condition", " '$(Platform)' == '' ");
  184. platform.SetValue("AnyCPU");
  185. // ProjectGuid
  186. XMLElement guid = pgroup.CreateChild("ProjectGuid");
  187. guid.SetValue(projectGuid_);
  188. // OutputType
  189. XMLElement outputType = pgroup.CreateChild("OutputType");
  190. outputType.SetValue(outputType_);
  191. // RootNamespace
  192. XMLElement rootNamespace = pgroup.CreateChild("RootNamespace");
  193. rootNamespace.SetValue(rootNamespace_);
  194. // AssemblyName
  195. XMLElement assemblyName = pgroup.CreateChild("AssemblyName");
  196. assemblyName.SetValue(assemblyName_);
  197. // TargetFrameworkVersion
  198. XMLElement targetFrameWork = pgroup.CreateChild("TargetFrameworkVersion");
  199. targetFrameWork.SetValue("4.5");
  200. }
  201. bool NETCSProject::Generate()
  202. {
  203. XMLElement project = xmlFile_->CreateRoot("Project");
  204. project.SetAttribute("DefaultTargets", "Build");
  205. project.SetAttribute("ToolsVersion", "4.0");
  206. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  207. CreateMainPropertyGroup(project);
  208. CreateDebugPropertyGroup(project);
  209. CreateReleasePropertyGroup(project);
  210. CreateReferencesItemGroup(project);
  211. CreateCompileItemGroup(project);
  212. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildBinPath)\\Microsoft.CSharp.targets");
  213. // on msbuild this seems to stop referencing of framework assemblies
  214. project.CreateChild("Target").SetAttribute("Name", "GetReferenceAssemblyPaths");
  215. project.CreateChild("Target").SetAttribute("Name", "GetFrameworkPaths");
  216. String projectSource = xmlFile_->ToString();
  217. NETSolution* solution = projectGen_->GetSolution();
  218. String projectPath = solution->GetOutputPath() + name_ + ".csproj";
  219. SharedPtr<File> output(new File(context_, projectPath, FILE_WRITE));
  220. output->Write(projectSource.CString(), projectSource.Length());
  221. return true;
  222. }
  223. bool NETCSProject::Load(const JSONValue& root)
  224. {
  225. bool gameBuild = projectGen_->GetGameBuild();
  226. name_ = root["name"].GetString();
  227. projectGuid_ = projectGen_->GenerateUUID();
  228. if (gameBuild)
  229. outputType_ = "Library";
  230. else
  231. outputType_ = root["outputType"].GetString();
  232. rootNamespace_ = root["rootNamespace"].GetString();
  233. assemblyName_ = root["assemblyName"].GetString();
  234. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  235. ReplacePathStrings(assemblyOutputPath_);
  236. assemblySearchPaths_ = root["assemblySearchPaths"].GetString();
  237. if (gameBuild)
  238. {
  239. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  240. const String& engineAssemblyPath = tenv->GetAtomicNETEngineAssemblyPath();
  241. if (assemblySearchPaths_.Length())
  242. {
  243. assemblySearchPaths_ += ";";
  244. assemblySearchPaths_ += engineAssemblyPath;
  245. }
  246. else
  247. {
  248. assemblySearchPaths_ = engineAssemblyPath;
  249. }
  250. }
  251. ReplacePathStrings(assemblySearchPaths_);
  252. const JSONArray& references = root["references"].GetArray();
  253. for (unsigned i = 0; i < references.Size(); i++)
  254. {
  255. String reference = references[i].GetString();
  256. ReplacePathStrings(reference);
  257. references_.Push(reference);
  258. }
  259. if (gameBuild)
  260. {
  261. references_.Push("AtomicNETEngine");
  262. }
  263. // msvc doesn't like including these
  264. if (projectGen_->GetMonoBuild())
  265. {
  266. references_.Push("System.Console");
  267. references_.Push("System.IO");
  268. references_.Push("System.IO.FileSystem");
  269. }
  270. const JSONArray& sources = root["sources"].GetArray();
  271. for (unsigned i = 0; i < sources.Size(); i++)
  272. {
  273. String source = sources[i].GetString();
  274. ReplacePathStrings(source);
  275. sourceFolders_.Push(AddTrailingSlash(source));
  276. }
  277. return true;
  278. }
  279. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen)
  280. {
  281. }
  282. NETSolution::~NETSolution()
  283. {
  284. }
  285. bool NETSolution::Generate()
  286. {
  287. String slnPath = outputPath_ + name_ + ".sln";
  288. GenerateXamarinStudio(slnPath);
  289. return true;
  290. }
  291. void NETSolution::GenerateXamarinStudio(const String &slnPath)
  292. {
  293. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  294. source += "# Visual Studio 2012\n";
  295. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  296. for (unsigned i = 0; i < projects.Size(); i++)
  297. {
  298. NETCSProject* p = projects.At(i);
  299. source += ToString("Project(\"{%s}\") = \"%s\", \"%s.csproj\", \"{%s}\"\n",
  300. p->GetProjectGUID().CString(), p->GetName().CString(), p->GetName().CString(),
  301. p->GetProjectGUID().CString());
  302. source += "EndProject\n";
  303. }
  304. source += "Global\n";
  305. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  306. source += " Debug|Any CPU = Debug|Any CPU\n";
  307. source += " Release|Any CPU = Release|Any CPU\n";
  308. source += " EndGlobalSection\n";
  309. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  310. for (unsigned i = 0; i < projects.Size(); i++)
  311. {
  312. NETCSProject* p = projects.At(i);
  313. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  314. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  315. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  316. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  317. }
  318. source += " EndGlobalSection\n";
  319. source += "EndGlobal\n";
  320. SharedPtr<File> output(new File(context_, slnPath, FILE_WRITE));
  321. output->Write(source.CString(), source.Length());
  322. output->Close();
  323. }
  324. bool NETSolution::Load(const JSONValue& root)
  325. {
  326. FileSystem* fs = GetSubsystem<FileSystem>();
  327. name_ = root["name"].GetString();
  328. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  329. ReplacePathStrings(outputPath_);
  330. // TODO: use poco mkdirs
  331. if (!fs->DirExists(outputPath_))
  332. fs->CreateDir(outputPath_);
  333. return true;
  334. }
  335. NETProjectGen::NETProjectGen(Context* context) : Object(context),
  336. monoBuild_(false), gameBuild_(false)
  337. {
  338. #ifndef ATOMIC_PLATFORM_WINDOWS
  339. monoBuild_ = true;
  340. #endif
  341. }
  342. NETProjectGen::~NETProjectGen()
  343. {
  344. }
  345. bool NETProjectGen::Generate()
  346. {
  347. solution_->Generate();
  348. for (unsigned i = 0; i < projects_.Size(); i++)
  349. {
  350. if (!projects_[i]->Generate())
  351. return false;
  352. }
  353. return true;
  354. }
  355. bool NETProjectGen::LoadProject(const JSONValue &root, bool gameBuild)
  356. {
  357. gameBuild_ = gameBuild;
  358. solution_ = new NETSolution(context_, this);
  359. solution_->Load(root["solution"]);
  360. const JSONValue& jprojects = root["projects"];
  361. if (!jprojects.IsArray() || ! jprojects.Size())
  362. return false;
  363. for (unsigned i = 0; i < jprojects.Size(); i++)
  364. {
  365. const JSONValue& jproject = jprojects[i];
  366. if (!jproject.IsObject())
  367. return false;
  368. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  369. if (!csProject->Load(jproject))
  370. return false;
  371. projects_.Push(csProject);
  372. }
  373. return true;
  374. }
  375. bool NETProjectGen::LoadProject(const String& projectPath, bool gameBuild)
  376. {
  377. SharedPtr<File> file(new File(context_));
  378. if (!file->Open(projectPath))
  379. return false;
  380. String json;
  381. file->ReadText(json);
  382. JSONValue jvalue;
  383. if (!JSONFile::ParseJSON(json, jvalue))
  384. return false;
  385. return LoadProject(jvalue, gameBuild);
  386. }
  387. String NETProjectGen::GenerateUUID()
  388. {
  389. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  390. Poco::UUID uuid(generator.create()); // time based
  391. return String(uuid.toString().c_str()).ToUpper();
  392. }
  393. }