NETBuildSystem.cpp 14 KB

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