NETBuildSystem.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. verbose_(false)
  57. {
  58. SubscribeToEvent(E_TOOLUPDATE, ATOMIC_HANDLER(NETBuildSystem, HandleToolUpdate));
  59. SubscribeToEvent(E_NETBUILDATOMICPROJECT, ATOMIC_HANDLER(NETBuildSystem, HandleBuildAtomicProject));
  60. }
  61. NETBuildSystem::~NETBuildSystem()
  62. {
  63. }
  64. void NETBuildSystem::HandleSubprocessOutput(StringHash eventType, VariantMap& eventData)
  65. {
  66. if (curBuild_.Null())
  67. {
  68. ATOMIC_LOGERRORF("NETBuildSystem::HandleSubprocessOutput - output received without current build");
  69. return;
  70. }
  71. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  72. if (verbose_)
  73. ATOMIC_LOGINFOF(text.CString());
  74. curBuild_->output_ += text;
  75. }
  76. void NETBuildSystem::HandleCompileProcessComplete(StringHash eventType, VariantMap& eventData)
  77. {
  78. UnsubscribeFromEvent(E_SUBPROCESSCOMPLETE);
  79. UnsubscribeFromEvent(E_SUBPROCESSOUTPUT);
  80. if (curBuild_.Null())
  81. {
  82. ATOMIC_LOGERROR("NETBuildSystem::HandleCompileProcessComplete - called with no current build");
  83. return;
  84. }
  85. curBuild_->status_ = NETBUILD_COMPLETE;
  86. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  87. using namespace NETBuildResult;
  88. VariantMap buildEventData;
  89. buildEventData[P_BUILD] = curBuild_;
  90. bool success = true;
  91. String errorMsg;
  92. if (verbose_)
  93. {
  94. ATOMIC_LOGINFOF("AtomicNET Build Command: %s", curBuild_->allArgs_.CString());
  95. }
  96. if (!code)
  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 == ".sln")
  148. {
  149. // TODO: handle projects that require nuget
  150. requiresNuGet = false;
  151. if (!fileSystem->FileExists(solutionPath))
  152. {
  153. CurrentBuildError(ToString("Generated solution does not exist (%s : %s)", curBuild_->solutionPath_.CString(), solutionPath.CString()));
  154. return;
  155. }
  156. }
  157. else if (ext == ".json")
  158. {
  159. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  160. gen->SetSupportedPlatforms(curBuild_->platforms_);
  161. gen->SetRewriteSolution(true);
  162. if (!gen->LoadJSONProject(solutionPath))
  163. {
  164. CurrentBuildError(ToString("Error loading project (%s)", solutionPath.CString()));
  165. return;
  166. }
  167. if (!gen->Generate())
  168. {
  169. CurrentBuildError(ToString("Error generating project (%s)", solutionPath.CString()));
  170. return;
  171. }
  172. solutionPath = gen->GetSolution()->GetOutputFilename();
  173. requiresNuGet = gen->GetRequiresNuGet();
  174. if (!fileSystem->FileExists(solutionPath))
  175. {
  176. CurrentBuildError(ToString("Generated solution does not exist (%s : %s)", curBuild_->solutionPath_.CString(), solutionPath.CString()));
  177. return;
  178. }
  179. }
  180. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  181. const String& nugetBinary = tenv->GetAtomicNETNuGetBinary();
  182. if (requiresNuGet && !fileSystem->FileExists(nugetBinary))
  183. {
  184. CurrentBuildError(ToString("NuGet binary is missing (%s)", nugetBinary.CString()));
  185. return;
  186. }
  187. StringVector stringVector;
  188. String platforms;
  189. String configs;
  190. for (unsigned i = 0; i < curBuild_->configurations_.Size(); i++)
  191. {
  192. stringVector.Push(ToString("/p:Configuration=%s", curBuild_->configurations_[i].CString()));
  193. }
  194. configs = String::Joined(stringVector, " ");
  195. stringVector.Clear();
  196. for (unsigned i = 0; i < curBuild_->platforms_.Size(); i++)
  197. {
  198. // map platform
  199. String platform = curBuild_->platforms_[i];
  200. if (platform == "desktop" || platform == "android")
  201. {
  202. platform = "\"Any CPU\"";
  203. }
  204. else if (platform == "ios")
  205. {
  206. platform = "\"Any CPU\"";
  207. // TODO
  208. // platform = "iPhone";
  209. }
  210. else
  211. {
  212. ATOMIC_LOGERRORF("Unknown platform: %s, skipping", platform.CString());
  213. continue;
  214. }
  215. platform = ToString("/p:Platform=%s", platform.CString());
  216. if (stringVector.Contains(platform))
  217. {
  218. // This can happen when specifying Desktop + Android for example
  219. continue;
  220. }
  221. stringVector.Push(platform);
  222. }
  223. platforms = String::Joined(stringVector, " ");
  224. stringVector.Clear();
  225. Vector<String> args;
  226. #ifdef ATOMIC_PLATFORM_WINDOWS
  227. String cmdToolsPath = Poco::Environment::get("VS140COMNTOOLS", "").c_str();
  228. if (!cmdToolsPath.Length())
  229. {
  230. CurrentBuildError("VS140COMNTOOLS environment variable not found, cannot proceed");
  231. return;
  232. }
  233. if (!cmdToolsPath.EndsWith("\\"))
  234. {
  235. cmdToolsPath += "\\";
  236. }
  237. String msbuildcmd = ToString("%sVsMSBuildCmd.bat", cmdToolsPath.CString());
  238. String cmd = "cmd";
  239. args.Push("/A");
  240. args.Push("/C");
  241. // vcvars bat
  242. String compile = ToString("\"\"%s\" ", msbuildcmd.CString());
  243. if (requiresNuGet)
  244. {
  245. compile += ToString("&& \"%s\" restore \"%s\" ", nugetBinary.CString(), solutionPath.CString());
  246. }
  247. compile += ToString("&& msbuild \"%s\" %s %s", solutionPath.CString(), platforms.CString(), configs.CString());
  248. if (curBuild_->targets_.Size()) {
  249. StringVector targets;
  250. for (unsigned i = 0; i < curBuild_->targets_.Size(); i++)
  251. {
  252. const char* tname = curBuild_->targets_[i].CString();
  253. targets.Push(ToString("/t:\"%s:Rebuild\"", tname));
  254. }
  255. compile += " " + String::Joined(targets, " ");
  256. }
  257. // close out quote
  258. compile += "\"";
  259. args.Push(compile);
  260. #else
  261. String compile;
  262. String cmd = "bash";
  263. args.Push("-c");
  264. String xbuildBinary = tenv->GetMonoExecutableDir() + "xbuild";
  265. if (requiresNuGet)
  266. {
  267. #ifdef ATOMIC_PLATFORM_OSX
  268. compile += ToString("\"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  269. #else
  270. compile += ToString("mono \"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  271. #endif
  272. }
  273. compile += ToString("\"%s\" \"%s\" %s %s", xbuildBinary.CString(), solutionPath.CString(), platforms.CString(), configs.CString());
  274. if (curBuild_->targets_.Size()) {
  275. StringVector targets;
  276. for (unsigned i = 0; i < curBuild_->targets_.Size(); i++)
  277. {
  278. const char* tname = curBuild_->targets_[i].CString();
  279. targets.Push(ToString("%s:Rebuild", tname));
  280. }
  281. compile += " /target:\"" + String::Joined(targets, ";") + "\"";
  282. }
  283. args.Push(compile);
  284. #endif
  285. curBuild_->allArgs_.Join(args, " ");
  286. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  287. Subprocess* subprocess = nullptr;
  288. ATOMIC_LOGINFOF("%s : %s", cmd.CString(), curBuild_->allArgs_.CString());
  289. try
  290. {
  291. subprocess = subs->Launch(cmd, args, "");
  292. }
  293. catch (Poco::SystemException)
  294. {
  295. subprocess = nullptr;
  296. }
  297. if (!subprocess)
  298. {
  299. CurrentBuildError(ToString("NETCompile::Compile - Unable to launch MSBuild subprocess\n%s", curBuild_->allArgs_.CString()));
  300. return;
  301. }
  302. VariantMap buildBeginEventData;
  303. buildBeginEventData[NETBuildBegin::P_BUILD] = curBuild_;
  304. SendEvent(E_NETBUILDBEGIN, buildBeginEventData);
  305. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, ATOMIC_HANDLER(NETBuildSystem, HandleCompileProcessComplete));
  306. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, ATOMIC_HANDLER(NETBuildSystem, HandleSubprocessOutput));
  307. curBuild_->status_ = NETBUILD_BUILDING;
  308. }
  309. }
  310. NETBuild* NETBuildSystem::GetBuild(const String& solutionPath, const StringVector& platforms, const StringVector& configurations)
  311. {
  312. List<SharedPtr<NETBuild>>::ConstIterator itr = builds_.Begin();
  313. while (itr != builds_.End())
  314. {
  315. NETBuild* build = *itr;
  316. if (build->solutionPath_ == solutionPath && build->platforms_ == platforms && build->configurations_ == configurations)
  317. return build;
  318. itr++;
  319. }
  320. return nullptr;
  321. }
  322. NETBuild* NETBuildSystem::BuildAtomicProject(Project* project)
  323. {
  324. StringVector platforms;
  325. StringVector configurations;
  326. platforms.Push("desktop");
  327. #ifdef ATOMIC_DEBUG
  328. configurations.Push("Debug");
  329. #else
  330. configurations.Push("Release");
  331. #endif
  332. String solutionPath = project->GetProjectPath() + "AtomicNET/Solution/" + project->GetProjectSettings()->GetName() + ".sln";
  333. NETBuild* build = Build(solutionPath, platforms, configurations);
  334. if (build)
  335. {
  336. ProjectSettings* settings = project->GetProjectSettings();
  337. // This path is currently only hit when refreshing for desktop
  338. if (settings->GetSupportsAndroid() || settings->GetSupportsIOS())
  339. {
  340. // Build the PCL, which will get copied to Resources
  341. build->targets_.Push(project->GetProjectSettings()->GetName());
  342. // Build the Desktop executable, so we can run it
  343. // IMPORTANT NOTE: msbuild requires replacing '.' with '_' when in project name
  344. build->targets_.Push(project->GetProjectSettings()->GetName() + "_Desktop");
  345. }
  346. build->project_ = project;
  347. }
  348. ATOMIC_LOGINFOF("Received build for project %s", project->GetProjectFilePath().CString());
  349. return build;
  350. }
  351. void NETBuildSystem::HandleBuildAtomicProject(StringHash eventType, VariantMap& eventData)
  352. {
  353. using namespace NETBuildAtomicProject;
  354. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  355. if (!project)
  356. {
  357. ATOMIC_LOGERROR("NETBuildSystem::HandleBuildAtomicProject - null project");
  358. return;
  359. }
  360. BuildAtomicProject(project);
  361. }
  362. NETBuild* NETBuildSystem::Build(const String& solutionPath, const StringVector& platforms, const StringVector& configurations)
  363. {
  364. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  365. if (!fileSystem->FileExists(solutionPath))
  366. {
  367. ATOMIC_LOGERRORF("NETBuildSystem::Build - Solution does not exist (%s)", solutionPath.CString());
  368. return 0;
  369. }
  370. // Get existing build
  371. SharedPtr<NETBuild> build(GetBuild(solutionPath, platforms, configurations));
  372. if (build.NotNull())
  373. return build;
  374. // Create a new build
  375. build = new NETBuild(context_, solutionPath, platforms, configurations);
  376. builds_.Push(build);
  377. return build;
  378. }
  379. }