NETBuildSystem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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, ATOMIC_HANDLER(NETBuildSystem, HandleToolUpdate));
  48. SubscribeToEvent(E_NETBUILDATOMICPROJECT, ATOMIC_HANDLER(NETBuildSystem, HandleBuildAtomicProject));
  49. }
  50. NETBuildSystem::~NETBuildSystem()
  51. {
  52. }
  53. void NETBuildSystem::HandleSubprocessOutput(StringHash eventType, VariantMap& eventData)
  54. {
  55. if (curBuild_.Null())
  56. {
  57. ATOMIC_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. ATOMIC_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. ATOMIC_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. const String configuration = curBuild_->configuration_;
  193. Vector<String> args;
  194. #ifdef ATOMIC_PLATFORM_WINDOWS
  195. String cmdToolsPath = Poco::Environment::get("VS140COMNTOOLS").c_str();
  196. if (!cmdToolsPath.Length())
  197. {
  198. CurrentBuildError("VS140COMNTOOLS environment variable not found, cannot proceed");
  199. return;
  200. }
  201. String vcvars64 = ToString("%s..\\..\\VC\\bin\\amd64\\vcvars64.bat", cmdToolsPath.CString());
  202. String cmd = "cmd";
  203. args.Push("/A");
  204. args.Push("/C");
  205. // vcvars bat
  206. String compile = ToString("\"\"%s\" ", vcvars64.CString());
  207. if (requiresNuGet)
  208. {
  209. compile += ToString("&& \"%s\" restore \"%s\" ", nugetBinary.CString(), solutionPath.CString());
  210. }
  211. compile += ToString("&& msbuild \"%s\" /p:Configuration=%s /p:Platform=\"Any CPU\"\"", solutionPath.CString(), configuration.CString());
  212. args.Push(compile);
  213. #else
  214. String compile;
  215. String cmd = "bash";
  216. args.Push("-c");
  217. String xbuildBinary = tenv->GetMonoExecutableDir() + "xbuild";
  218. if (requiresNuGet)
  219. {
  220. compile += ToString("\"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  221. }
  222. compile += ToString("\"%s\" \"%s\" /p:Configuration=%s /p:Platform=\"Any CPU\"", xbuildBinary.CString(), solutionPath.CString(), configuration.CString());
  223. args.Push(compile);
  224. #endif
  225. curBuild_->allArgs_.Join(args, " ");
  226. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  227. Subprocess* subprocess = nullptr;
  228. try
  229. {
  230. subprocess = subs->Launch(cmd, args, "");
  231. }
  232. catch (Poco::SystemException)
  233. {
  234. subprocess = nullptr;
  235. }
  236. if (!subprocess)
  237. {
  238. CurrentBuildError(ToString("NETCompile::Compile - Unable to launch MSBuild subprocess\n%s", curBuild_->allArgs_.CString()));
  239. return;
  240. }
  241. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, ATOMIC_HANDLER(NETBuildSystem, HandleCompileProcessComplete));
  242. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, ATOMIC_HANDLER(NETBuildSystem, HandleSubprocessOutput));
  243. curBuild_->status_ = NETBUILD_BUILDING;
  244. }
  245. }
  246. NETBuild* NETBuildSystem::GetBuild(const String& solutionPath, const String& platform, const String& configuration)
  247. {
  248. List<SharedPtr<NETBuild>>::ConstIterator itr = builds_.Begin();
  249. while (itr != builds_.End())
  250. {
  251. NETBuild* build = *itr;
  252. if (build->solutionPath_ == solutionPath && build->platform_ == platform && build->configuration_ == configuration)
  253. return build;
  254. itr++;
  255. }
  256. return nullptr;
  257. }
  258. NETBuild* NETBuildSystem::BuildAtomicProject(Project* project)
  259. {
  260. String platform;
  261. String configuration;
  262. #ifdef ATOMIC_PLATFORM_WINDOWS
  263. platform = "WINDOWS";
  264. #elif ATOMIC_PLATFORM_OSX
  265. platform = "MACOSX";
  266. #else
  267. platform = "LINUX";
  268. #endif
  269. #ifdef ATOMIC_DEBUG
  270. configuration = "Debug";
  271. #else
  272. configuration = "Release";
  273. #endif
  274. String projectPath = project->GetProjectFilePath();
  275. NETBuild* build = Build(projectPath, platform, configuration);
  276. if (build)
  277. {
  278. build->project_ = project;
  279. }
  280. ATOMIC_LOGINFOF("Received build for project %s", project->GetProjectFilePath().CString());
  281. return build;
  282. }
  283. void NETBuildSystem::HandleBuildAtomicProject(StringHash eventType, VariantMap& eventData)
  284. {
  285. using namespace NETBuildAtomicProject;
  286. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  287. if (!project)
  288. {
  289. ATOMIC_LOGERROR("NETBuildSystem::HandleBuildAtomicProject - null project");
  290. return;
  291. }
  292. BuildAtomicProject(project);
  293. }
  294. NETBuild* NETBuildSystem::Build(const String& solutionPath, const String& platform, const String& configuration)
  295. {
  296. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  297. if (!fileSystem->FileExists(solutionPath))
  298. {
  299. ATOMIC_LOGERRORF("NETBuildSystem::Build - Solution does not exist (%s)", solutionPath.CString());
  300. return 0;
  301. }
  302. // Get existing build
  303. SharedPtr<NETBuild> build(GetBuild(solutionPath, platform, configuration));
  304. if (build.NotNull())
  305. return build;
  306. // Create a new build
  307. build = new NETBuild(context_, solutionPath, platform, configuration);
  308. builds_.Push(build);
  309. return build;
  310. }
  311. }