NETBuildSystem.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. StringVector processedPlatforms;
  190. String configs;
  191. for (unsigned i = 0; i < curBuild_->configurations_.Size(); i++)
  192. {
  193. stringVector.Push(ToString("/p:Configuration=%s", curBuild_->configurations_[i].CString()));
  194. }
  195. configs = String::Joined(stringVector, " ");
  196. stringVector.Clear();
  197. for (unsigned i = 0; i < curBuild_->platforms_.Size(); i++)
  198. {
  199. // map platform
  200. String platform = curBuild_->platforms_[i];
  201. if (platform == "windows" || platform == "macosx" || platform == "linux")
  202. {
  203. ATOMIC_LOGINFOF("Platform \"%s\" mapped to \"desktop\"", platform.CString());
  204. platform = "desktop";
  205. }
  206. if (processedPlatforms.Contains(platform))
  207. {
  208. ATOMIC_LOGWARNINGF("Platform \"%s\" is duplicated, skipping", platform.CString());
  209. continue;
  210. }
  211. processedPlatforms.Push(platform);
  212. if (platform == "desktop" || platform == "android")
  213. {
  214. platform = "\"Any CPU\"";
  215. }
  216. else if (platform == "ios")
  217. {
  218. platform = "\"Any CPU\"";
  219. // TODO
  220. // platform = "iPhone";
  221. }
  222. else
  223. {
  224. ATOMIC_LOGERRORF("Unknown platform: %s, skipping", platform.CString());
  225. continue;
  226. }
  227. platform = ToString("/p:Platform=%s", platform.CString());
  228. if (stringVector.Contains(platform))
  229. {
  230. // This can happen when specifying Desktop + Android for example
  231. continue;
  232. }
  233. stringVector.Push(platform);
  234. }
  235. platforms = String::Joined(stringVector, " ");
  236. stringVector.Clear();
  237. Vector<String> args;
  238. #ifdef ATOMIC_PLATFORM_WINDOWS
  239. String cmdToolsPath = Poco::Environment::get("VS140COMNTOOLS", "").c_str();
  240. if (!cmdToolsPath.Length())
  241. {
  242. CurrentBuildError("VS140COMNTOOLS environment variable not found, cannot proceed");
  243. return;
  244. }
  245. if (!cmdToolsPath.EndsWith("\\"))
  246. {
  247. cmdToolsPath += "\\";
  248. }
  249. String msbuildcmd = ToString("%sVsMSBuildCmd.bat", cmdToolsPath.CString());
  250. String cmd = "cmd";
  251. args.Push("/A");
  252. args.Push("/C");
  253. // vcvars bat
  254. String compile = ToString("\"\"%s\" ", msbuildcmd.CString());
  255. if (requiresNuGet)
  256. {
  257. compile += ToString("&& \"%s\" restore \"%s\" ", nugetBinary.CString(), solutionPath.CString());
  258. }
  259. compile += ToString("&& msbuild \"%s\" %s %s", solutionPath.CString(), platforms.CString(), configs.CString());
  260. if (curBuild_->targets_.Size()) {
  261. StringVector targets;
  262. for (unsigned i = 0; i < curBuild_->targets_.Size(); i++)
  263. {
  264. const char* tname = curBuild_->targets_[i].CString();
  265. targets.Push(ToString("/t:\"%s:Rebuild\"", tname));
  266. }
  267. compile += " " + String::Joined(targets, " ");
  268. }
  269. // close out quote
  270. compile += "\"";
  271. args.Push(compile);
  272. #else
  273. String compile;
  274. String cmd = "bash";
  275. args.Push("-c");
  276. String xbuildBinary = tenv->GetMonoExecutableDir() + "xbuild";
  277. if (requiresNuGet)
  278. {
  279. #ifdef ATOMIC_PLATFORM_OSX
  280. compile += ToString("\"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  281. #else
  282. compile += ToString("mono \"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  283. #endif
  284. }
  285. compile += ToString("\"%s\" \"%s\" %s %s", xbuildBinary.CString(), solutionPath.CString(), platforms.CString(), configs.CString());
  286. if (curBuild_->targets_.Size()) {
  287. StringVector targets;
  288. for (unsigned i = 0; i < curBuild_->targets_.Size(); i++)
  289. {
  290. const char* tname = curBuild_->targets_[i].CString();
  291. targets.Push(ToString("%s:Rebuild", tname));
  292. }
  293. compile += " /target:\"" + String::Joined(targets, ";") + "\"";
  294. }
  295. args.Push(compile);
  296. #endif
  297. curBuild_->allArgs_.Join(args, " ");
  298. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  299. Subprocess* subprocess = nullptr;
  300. ATOMIC_LOGINFOF("%s : %s", cmd.CString(), curBuild_->allArgs_.CString());
  301. try
  302. {
  303. subprocess = subs->Launch(cmd, args, "");
  304. }
  305. catch (Poco::SystemException)
  306. {
  307. subprocess = nullptr;
  308. }
  309. if (!subprocess)
  310. {
  311. CurrentBuildError(ToString("NETCompile::Compile - Unable to launch MSBuild subprocess\n%s", curBuild_->allArgs_.CString()));
  312. return;
  313. }
  314. VariantMap buildBeginEventData;
  315. buildBeginEventData[NETBuildBegin::P_BUILD] = curBuild_;
  316. SendEvent(E_NETBUILDBEGIN, buildBeginEventData);
  317. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, ATOMIC_HANDLER(NETBuildSystem, HandleCompileProcessComplete));
  318. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, ATOMIC_HANDLER(NETBuildSystem, HandleSubprocessOutput));
  319. curBuild_->status_ = NETBUILD_BUILDING;
  320. }
  321. }
  322. NETBuild* NETBuildSystem::GetBuild(const String& solutionPath, const StringVector& platforms, const StringVector& configurations)
  323. {
  324. List<SharedPtr<NETBuild>>::ConstIterator itr = builds_.Begin();
  325. while (itr != builds_.End())
  326. {
  327. NETBuild* build = *itr;
  328. if (build->solutionPath_ == solutionPath && build->platforms_ == platforms && build->configurations_ == configurations)
  329. return build;
  330. itr++;
  331. }
  332. return nullptr;
  333. }
  334. NETBuild* NETBuildSystem::BuildAtomicProject(Project* project)
  335. {
  336. StringVector platforms;
  337. StringVector configurations;
  338. platforms.Push("desktop");
  339. #ifdef ATOMIC_DEBUG
  340. configurations.Push("Debug");
  341. #else
  342. configurations.Push("Release");
  343. #endif
  344. AtomicNETCopyAssemblies(context_, project->GetProjectPath() + "AtomicNET/Lib/");
  345. String solutionPath = project->GetProjectPath() + "AtomicNET/Solution/" + project->GetProjectSettings()->GetName() + ".sln";
  346. NETBuild* build = Build(solutionPath, platforms, configurations);
  347. if (build)
  348. {
  349. ProjectSettings* settings = project->GetProjectSettings();
  350. // This path is currently only hit when refreshing for desktop
  351. if (settings->GetSupportsAndroid() || settings->GetSupportsIOS())
  352. {
  353. // Build the PCL, which will get copied to Resources
  354. build->targets_.Push(project->GetProjectSettings()->GetName());
  355. // Build the Desktop executable, so we can run it
  356. // IMPORTANT NOTE: msbuild requires replacing '.' with '_' when in project name
  357. build->targets_.Push(project->GetProjectSettings()->GetName() + "_Desktop");
  358. }
  359. build->project_ = project;
  360. }
  361. ATOMIC_LOGINFOF("Received build for project %s", project->GetProjectFilePath().CString());
  362. return build;
  363. }
  364. void NETBuildSystem::HandleBuildAtomicProject(StringHash eventType, VariantMap& eventData)
  365. {
  366. using namespace NETBuildAtomicProject;
  367. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  368. if (!project)
  369. {
  370. ATOMIC_LOGERROR("NETBuildSystem::HandleBuildAtomicProject - null project");
  371. return;
  372. }
  373. BuildAtomicProject(project);
  374. }
  375. NETBuild* NETBuildSystem::Build(const String& solutionPath, const StringVector& platforms, const StringVector& configurations)
  376. {
  377. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  378. if (!fileSystem->FileExists(solutionPath))
  379. {
  380. ATOMIC_LOGERRORF("NETBuildSystem::Build - Solution does not exist (%s)", solutionPath.CString());
  381. return 0;
  382. }
  383. // Get existing build
  384. SharedPtr<NETBuild> build(GetBuild(solutionPath, platforms, configurations));
  385. if (build.NotNull())
  386. return build;
  387. // Create a new build
  388. build = new NETBuild(context_, solutionPath, platforms, configurations);
  389. builds_.Push(build);
  390. return build;
  391. }
  392. }