NETProjectGen.cpp 15 KB

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