NETBuildSystem.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/Exception.h>
  23. #include <Poco/Environment.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include "../ToolEvents.h"
  27. #include "../ToolSystem.h"
  28. #include "../ToolEnvironment.h"
  29. #include "../Subprocess/SubprocessSystem.h"
  30. #include "../Project/Project.h"
  31. #include "NETProjectSystem.h"
  32. #include "NETProjectGen.h"
  33. #include "NETBuildSystem.h"
  34. namespace ToolCore
  35. {
  36. NETBuild::NETBuild(Context* context, const String& solutionPath, const String& platform, const String& configuration) :
  37. Object(context),
  38. solutionPath_(solutionPath),
  39. platform_(platform),
  40. configuration_(configuration),
  41. status_(NETBUILD_PENDING)
  42. {
  43. }
  44. NETBuildSystem::NETBuildSystem(Context* context) :
  45. Object(context)
  46. {
  47. SubscribeToEvent(E_TOOLUPDATE, HANDLER(NETBuildSystem, HandleToolUpdate));
  48. SubscribeToEvent(E_NETBUILDATOMICPROJECT, HANDLER(NETBuildSystem, HandleBuildAtomicProject));
  49. }
  50. NETBuildSystem::~NETBuildSystem()
  51. {
  52. }
  53. void NETBuildSystem::HandleSubprocessOutput(StringHash eventType, VariantMap& eventData)
  54. {
  55. if (curBuild_.Null())
  56. {
  57. LOGERRORF("NETBuildSystem::HandleSubprocessOutput - output received without current build");
  58. return;
  59. }
  60. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  61. // LOGINFOF(text.CString());
  62. curBuild_->output_ += text;
  63. }
  64. void NETBuildSystem::HandleCompileProcessComplete(StringHash eventType, VariantMap& eventData)
  65. {
  66. UnsubscribeFromEvent(E_SUBPROCESSCOMPLETE);
  67. UnsubscribeFromEvent(E_SUBPROCESSOUTPUT);
  68. if (curBuild_.Null())
  69. {
  70. LOGERROR("NETBuildSystem::HandleCompileProcessComplete - called with no current build");
  71. return;
  72. }
  73. curBuild_->status_ = NETBUILD_COMPLETE;
  74. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  75. using namespace NETBuildResult;
  76. VariantMap buildEventData;
  77. buildEventData[P_BUILD] = curBuild_;
  78. bool success = true;
  79. String errorMsg;
  80. if (!code)
  81. {
  82. if (curBuild_->project_.NotNull())
  83. {
  84. // Copy compiled assembly to resource path
  85. String srcAssembly = AddTrailingSlash(curBuild_->project_->GetProjectPath()) + "AtomicNET/Bin/";
  86. srcAssembly += curBuild_->configuration_;
  87. srcAssembly += "/AtomicProject.dll";
  88. String dstAssembly = AddTrailingSlash(curBuild_->project_->GetResourcePath()) + "AtomicProject.dll";
  89. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  90. bool result = fileSystem->Copy(srcAssembly, dstAssembly);
  91. if (!result)
  92. {
  93. success = false;
  94. errorMsg = ToString("BuildBase::BuildCopyFile: Unable to copy assembly %s -> %s", srcAssembly.CString(), dstAssembly.CString());
  95. errorMsg += ToString("\nCompilation Command: %s", curBuild_->allArgs_.CString());
  96. }
  97. }
  98. }
  99. else
  100. {
  101. success = false;
  102. errorMsg = curBuild_->output_;
  103. errorMsg += ToString("\nCompilation Command: %s", curBuild_->allArgs_.CString());
  104. }
  105. buildEventData[P_SUCCESS] = success;
  106. if (!success)
  107. {
  108. buildEventData[P_ERRORTEXT] = errorMsg;
  109. }
  110. curBuild_->SendEvent(E_NETBUILDRESULT, buildEventData);
  111. curBuild_ = nullptr;
  112. }
  113. void NETBuildSystem::CurrentBuildError(String errorText)
  114. {
  115. if (curBuild_.Null())
  116. {
  117. LOGERRORF("NETBuildSystem::CurrentBuildError - Error %s with no current build", errorText.CString());
  118. return;
  119. }
  120. using namespace NETBuildResult;
  121. VariantMap buildEventData;
  122. buildEventData[P_BUILD] = curBuild_;
  123. buildEventData[P_SUCCESS] = false;
  124. buildEventData[P_ERRORTEXT] = errorText;
  125. curBuild_->SendEvent(E_NETBUILDRESULT, buildEventData);
  126. curBuild_ = nullptr;
  127. }
  128. void NETBuildSystem::HandleToolUpdate(StringHash eventType, VariantMap& eventData)
  129. {
  130. if (curBuild_.Null() && !builds_.Size())
  131. return;
  132. if (curBuild_.Null())
  133. {
  134. // kick off a new build
  135. curBuild_ = builds_.Front();
  136. builds_.PopFront();
  137. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  138. // Ensure solution still exists
  139. if (!fileSystem->FileExists(curBuild_->solutionPath_))
  140. {
  141. CurrentBuildError(ToString("Solution does not exist(%s)", curBuild_->solutionPath_.CString()));
  142. return;
  143. }
  144. String solutionPath = curBuild_->solutionPath_;
  145. String ext = GetExtension(solutionPath);
  146. bool requiresNuGet = true;
  147. if (ext == ".atomic")
  148. {
  149. if (curBuild_->project_.Null() || curBuild_->project_.Expired())
  150. {
  151. CurrentBuildError(ToString("Error loading project (%s), project expired", solutionPath.CString()));
  152. }
  153. NETProjectSystem* projectSystem = GetSubsystem<NETProjectSystem>();
  154. solutionPath = projectSystem->GetSolutionPath();
  155. // TODO: handle projects that require nuget
  156. requiresNuGet = false;
  157. if (!fileSystem->FileExists(solutionPath))
  158. {
  159. CurrentBuildError(ToString("Generated solution does not exist (%s : %s)", curBuild_->solutionPath_.CString(), solutionPath.CString()));
  160. return;
  161. }
  162. }
  163. else if (ext == ".json")
  164. {
  165. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  166. gen->SetScriptPlatform(curBuild_->platform_);
  167. if (!gen->LoadProject(solutionPath))
  168. {
  169. CurrentBuildError(ToString("Error loading project (%s)", solutionPath.CString()));
  170. return;
  171. }
  172. if (!gen->Generate())
  173. {
  174. CurrentBuildError(ToString("Error generating project (%s)", solutionPath.CString()));
  175. return;
  176. }
  177. solutionPath = gen->GetSolution()->GetOutputFilename();
  178. requiresNuGet = gen->GetRequiresNuGet();
  179. if (!fileSystem->FileExists(solutionPath))
  180. {
  181. CurrentBuildError(ToString("Generated solution does not exist (%s : %s)", curBuild_->solutionPath_.CString(), solutionPath.CString()));
  182. return;
  183. }
  184. }
  185. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  186. const String& nugetBinary = tenv->GetAtomicNETNuGetBinary();
  187. if (requiresNuGet && !fileSystem->FileExists(nugetBinary))
  188. {
  189. CurrentBuildError(ToString("NuGet binary is missing (%s)", nugetBinary.CString()));
  190. return;
  191. }
  192. String cmdToolsPath = Poco::Environment::get("VS140COMNTOOLS").c_str();
  193. if (!cmdToolsPath.Length())
  194. {
  195. CurrentBuildError("VS140COMNTOOLS environment variable not found, cannot proceed");
  196. return;
  197. }
  198. String vcvars64 = ToString("%s..\\..\\VC\\bin\\amd64\\vcvars64.bat", cmdToolsPath.CString());
  199. const String configuration = curBuild_->configuration_;
  200. String cmd = "cmd";
  201. Vector<String> args;
  202. args.Push("/A");
  203. args.Push("/C");
  204. // vcvars bat
  205. String compile = ToString("\"\"%s\" ", vcvars64.CString());
  206. if (requiresNuGet)
  207. {
  208. compile += ToString("&& \"%s\" restore \"%s\" ", nugetBinary.CString(), solutionPath.CString());
  209. }
  210. compile += ToString("&& msbuild \"%s\" /p:Configuration=%s /p:Platform=\"Any CPU\"\"", solutionPath.CString(), configuration.CString());
  211. args.Push(compile);
  212. curBuild_->allArgs_.Join(args, " ");
  213. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  214. Subprocess* subprocess = nullptr;
  215. try
  216. {
  217. subprocess = subs->Launch(cmd, args, "C:\\");
  218. }
  219. catch (Poco::SystemException)
  220. {
  221. subprocess = nullptr;
  222. }
  223. if (!subprocess)
  224. {
  225. CurrentBuildError(ToString("NETCompile::Compile - Unable to launch MSBuild subprocess\n%s", curBuild_->allArgs_.CString()));
  226. return;
  227. }
  228. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(NETBuildSystem, HandleCompileProcessComplete));
  229. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(NETBuildSystem, HandleSubprocessOutput));
  230. curBuild_->status_ = NETBUILD_BUILDING;
  231. }
  232. }
  233. NETBuild* NETBuildSystem::GetBuild(const String& solutionPath, const String& platform, const String& configuration)
  234. {
  235. List<SharedPtr<NETBuild>>::ConstIterator itr = builds_.Begin();
  236. while (itr != builds_.End())
  237. {
  238. NETBuild* build = *itr;
  239. if (build->solutionPath_ == solutionPath && build->platform_ == platform && build->configuration_ == configuration)
  240. return build;
  241. itr++;
  242. }
  243. return nullptr;
  244. }
  245. NETBuild* NETBuildSystem::BuildAtomicProject(Project* project)
  246. {
  247. String platform;
  248. String configuration;
  249. #ifdef ATOMIC_PLATFORM_WINDOWS
  250. platform = "WINDOWS";
  251. #elif ATOMIC_PLATFORM_OSX
  252. platform = "MACOSX";
  253. #else
  254. platform = "LINUX";
  255. #endif
  256. #ifdef _DEBUG
  257. configuration = "Debug";
  258. #else
  259. configuration = "Release";
  260. #endif
  261. String projectPath = project->GetProjectFilePath();
  262. NETBuild* build = Build(projectPath, platform, configuration);
  263. if (build)
  264. {
  265. build->project_ = project;
  266. }
  267. LOGINFOF("Received build for project %s", project->GetProjectFilePath().CString());
  268. return build;
  269. }
  270. void NETBuildSystem::HandleBuildAtomicProject(StringHash eventType, VariantMap& eventData)
  271. {
  272. using namespace NETBuildAtomicProject;
  273. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  274. if (!project)
  275. {
  276. LOGERROR("NETBuildSystem::HandleBuildAtomicProject - null project");
  277. return;
  278. }
  279. BuildAtomicProject(project);
  280. }
  281. NETBuild* NETBuildSystem::Build(const String& solutionPath, const String& platform, const String& configuration)
  282. {
  283. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  284. if (!fileSystem->FileExists(solutionPath))
  285. {
  286. LOGERRORF("NETBuildSystem::Build - Solution does not exist (%s)", solutionPath.CString());
  287. return 0;
  288. }
  289. // Get existing build
  290. SharedPtr<NETBuild> build(GetBuild(solutionPath, platform, configuration));
  291. if (build.NotNull())
  292. return build;
  293. // Create a new build
  294. build = new NETBuild(context_, solutionPath, platform, configuration);
  295. builds_.Push(build);
  296. return build;
  297. }
  298. }