NETBuildSystem.cpp 13 KB

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