NETBuildSystem.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. if (!cmdToolsPath.EndsWith("\\"))
  228. {
  229. cmdToolsPath += "\\";
  230. }
  231. String msbuildcmd = ToString("%sVsMSBuildCmd.bat", cmdToolsPath.CString());
  232. String cmd = "cmd";
  233. args.Push("/A");
  234. args.Push("/C");
  235. // vcvars bat
  236. String compile = ToString("\"\"%s\" ", msbuildcmd.CString());
  237. if (requiresNuGet)
  238. {
  239. compile += ToString("&& \"%s\" restore \"%s\" ", nugetBinary.CString(), solutionPath.CString());
  240. }
  241. compile += ToString("&& msbuild \"%s\" %s %s", solutionPath.CString(), platforms.CString(), configs.CString());
  242. if (curBuild_->targets_.Size()) {
  243. StringVector targets;
  244. for (unsigned i = 0; i < curBuild_->targets_.Size(); i++)
  245. {
  246. const char* tname = curBuild_->targets_[i].CString();
  247. targets.Push(ToString("/t:\"%s:Rebuild\"", tname));
  248. }
  249. compile += " " + String::Joined(targets, " ");
  250. }
  251. // close out quote
  252. compile += "\"";
  253. args.Push(compile);
  254. #else
  255. String compile;
  256. String cmd = "bash";
  257. args.Push("-c");
  258. String xbuildBinary = tenv->GetMonoExecutableDir() + "xbuild";
  259. if (requiresNuGet)
  260. {
  261. #ifdef ATOMIC_PLATFORM_OSX
  262. compile += ToString("\"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  263. #else
  264. compile += ToString("mono \"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  265. #endif
  266. }
  267. compile += ToString("\"%s\" \"%s\" %s %s", xbuildBinary.CString(), solutionPath.CString(), platforms.CString(), configs.CString());
  268. args.Push(compile);
  269. #endif
  270. curBuild_->allArgs_.Join(args, " ");
  271. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  272. Subprocess* subprocess = nullptr;
  273. ATOMIC_LOGINFOF("%s : %s", cmd.CString(), curBuild_->allArgs_.CString());
  274. try
  275. {
  276. subprocess = subs->Launch(cmd, args, "");
  277. }
  278. catch (Poco::SystemException)
  279. {
  280. subprocess = nullptr;
  281. }
  282. if (!subprocess)
  283. {
  284. CurrentBuildError(ToString("NETCompile::Compile - Unable to launch MSBuild subprocess\n%s", curBuild_->allArgs_.CString()));
  285. return;
  286. }
  287. VariantMap buildBeginEventData;
  288. buildBeginEventData[NETBuildBegin::P_BUILD] = curBuild_;
  289. SendEvent(E_NETBUILDBEGIN, buildBeginEventData);
  290. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, ATOMIC_HANDLER(NETBuildSystem, HandleCompileProcessComplete));
  291. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, ATOMIC_HANDLER(NETBuildSystem, HandleSubprocessOutput));
  292. curBuild_->status_ = NETBUILD_BUILDING;
  293. }
  294. }
  295. NETBuild* NETBuildSystem::GetBuild(const String& solutionPath, const StringVector& platforms, const StringVector& configurations)
  296. {
  297. List<SharedPtr<NETBuild>>::ConstIterator itr = builds_.Begin();
  298. while (itr != builds_.End())
  299. {
  300. NETBuild* build = *itr;
  301. if (build->solutionPath_ == solutionPath && build->platforms_ == platforms && build->configurations_ == configurations)
  302. return build;
  303. itr++;
  304. }
  305. return nullptr;
  306. }
  307. NETBuild* NETBuildSystem::BuildAtomicProject(Project* project)
  308. {
  309. StringVector platforms;
  310. StringVector configurations;
  311. platforms.Push("desktop");
  312. #ifdef ATOMIC_DEBUG
  313. configurations.Push("Debug");
  314. #else
  315. configurations.Push("Release");
  316. #endif
  317. String solutionPath = project->GetProjectPath() + "AtomicNET/Solution/" + project->GetProjectSettings()->GetName() + ".sln";
  318. NETBuild* build = Build(solutionPath, platforms, configurations);
  319. if (build)
  320. {
  321. ProjectSettings* settings = project->GetProjectSettings();
  322. // This path is currently only hit when refreshing for desktop
  323. if (settings->GetSupportsAndroid() || settings->GetSupportsIOS())
  324. {
  325. // Build the PCL, which will get copied to Resources
  326. build->targets_.Push(project->GetProjectSettings()->GetName());
  327. // Build the Desktop executable, so we can run it
  328. // IMPORTANT NOTE: msbuild requires replacing '.' with '_' when in project name
  329. build->targets_.Push(project->GetProjectSettings()->GetName() + "_Desktop");
  330. }
  331. build->project_ = project;
  332. }
  333. ATOMIC_LOGINFOF("Received build for project %s", project->GetProjectFilePath().CString());
  334. return build;
  335. }
  336. void NETBuildSystem::HandleBuildAtomicProject(StringHash eventType, VariantMap& eventData)
  337. {
  338. using namespace NETBuildAtomicProject;
  339. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  340. if (!project)
  341. {
  342. ATOMIC_LOGERROR("NETBuildSystem::HandleBuildAtomicProject - null project");
  343. return;
  344. }
  345. BuildAtomicProject(project);
  346. }
  347. NETBuild* NETBuildSystem::Build(const String& solutionPath, const StringVector& platforms, const StringVector& configurations)
  348. {
  349. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  350. if (!fileSystem->FileExists(solutionPath))
  351. {
  352. ATOMIC_LOGERRORF("NETBuildSystem::Build - Solution does not exist (%s)", solutionPath.CString());
  353. return 0;
  354. }
  355. // Get existing build
  356. SharedPtr<NETBuild> build(GetBuild(solutionPath, platforms, configurations));
  357. if (build.NotNull())
  358. return build;
  359. // Create a new build
  360. build = new NETBuild(context_, solutionPath, platforms, configurations);
  361. builds_.Push(build);
  362. return build;
  363. }
  364. }