NETBuildSystem.cpp 12 KB

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