NETBuildSystem.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #ifdef ATOMIC_PLATFORM_WINDOWS
  36. #include <Poco/WinRegistryKey.h>
  37. #endif
  38. namespace ToolCore
  39. {
  40. NETBuild::NETBuild(Context* context, const String& solutionPath, const StringVector& platforms, const StringVector& configurations) :
  41. Object(context),
  42. solutionPath_(solutionPath),
  43. status_(NETBUILD_PENDING)
  44. {
  45. for (unsigned i = 0; i < platforms.Size() ; i++)
  46. {
  47. platforms_.Push(platforms[i].ToLower());
  48. }
  49. for (unsigned i = 0; i < configurations.Size() ; i++)
  50. {
  51. String config = configurations[i];
  52. config.Replace("release", "Release");
  53. config.Replace("debug", "Debug");
  54. configurations_.Push(config);
  55. }
  56. }
  57. NETBuildSystem::NETBuildSystem(Context* context) :
  58. Object(context),
  59. verbose_(false)
  60. {
  61. SubscribeToEvent(E_TOOLUPDATE, ATOMIC_HANDLER(NETBuildSystem, HandleToolUpdate));
  62. SubscribeToEvent(E_NETBUILDATOMICPROJECT, ATOMIC_HANDLER(NETBuildSystem, HandleBuildAtomicProject));
  63. }
  64. NETBuildSystem::~NETBuildSystem()
  65. {
  66. }
  67. void NETBuildSystem::HandleSubprocessOutput(StringHash eventType, VariantMap& eventData)
  68. {
  69. if (curBuild_.Null())
  70. {
  71. ATOMIC_LOGERRORF("NETBuildSystem::HandleSubprocessOutput - output received without current build");
  72. return;
  73. }
  74. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  75. if (verbose_)
  76. ATOMIC_LOGINFOF(text.CString());
  77. curBuild_->output_ += text;
  78. }
  79. void NETBuildSystem::HandleCompileProcessComplete(StringHash eventType, VariantMap& eventData)
  80. {
  81. UnsubscribeFromEvent(E_SUBPROCESSCOMPLETE);
  82. UnsubscribeFromEvent(E_SUBPROCESSOUTPUT);
  83. if (curBuild_.Null())
  84. {
  85. ATOMIC_LOGERROR("NETBuildSystem::HandleCompileProcessComplete - called with no current build");
  86. return;
  87. }
  88. curBuild_->status_ = NETBUILD_COMPLETE;
  89. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  90. using namespace NETBuildResult;
  91. VariantMap buildEventData;
  92. buildEventData[P_BUILD] = curBuild_;
  93. bool success = true;
  94. String errorMsg;
  95. if (verbose_)
  96. {
  97. ATOMIC_LOGINFOF("AtomicNET Build Command: %s", curBuild_->allArgs_.CString());
  98. }
  99. if (!code)
  100. {
  101. }
  102. else
  103. {
  104. success = false;
  105. errorMsg = curBuild_->output_;
  106. errorMsg += ToString("\nCompilation Command: %s", curBuild_->allArgs_.CString());
  107. }
  108. buildEventData[P_SUCCESS] = success;
  109. if (!success)
  110. {
  111. buildEventData[P_ERRORTEXT] = errorMsg;
  112. }
  113. curBuild_->SendEvent(E_NETBUILDRESULT, buildEventData);
  114. curBuild_ = nullptr;
  115. }
  116. void NETBuildSystem::CurrentBuildError(String errorText)
  117. {
  118. if (curBuild_.Null())
  119. {
  120. ATOMIC_LOGERRORF("NETBuildSystem::CurrentBuildError - Error %s with no current build", errorText.CString());
  121. return;
  122. }
  123. using namespace NETBuildResult;
  124. VariantMap buildEventData;
  125. buildEventData[P_BUILD] = curBuild_;
  126. buildEventData[P_SUCCESS] = false;
  127. buildEventData[P_ERRORTEXT] = errorText;
  128. curBuild_->SendEvent(E_NETBUILDRESULT, buildEventData);
  129. curBuild_ = nullptr;
  130. }
  131. void NETBuildSystem::HandleToolUpdate(StringHash eventType, VariantMap& eventData)
  132. {
  133. if (curBuild_.Null() && !builds_.Size())
  134. return;
  135. if (curBuild_.Null())
  136. {
  137. // kick off a new build
  138. curBuild_ = builds_.Front();
  139. builds_.PopFront();
  140. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  141. // Ensure solution still exists
  142. if (!fileSystem->FileExists(curBuild_->solutionPath_))
  143. {
  144. CurrentBuildError(ToString("Solution does not exist(%s)", curBuild_->solutionPath_.CString()));
  145. return;
  146. }
  147. String solutionPath = curBuild_->solutionPath_;
  148. String ext = GetExtension(solutionPath);
  149. bool requiresNuGet = true;
  150. if (ext == ".sln")
  151. {
  152. // TODO: handle projects that require nuget
  153. requiresNuGet = false;
  154. if (!fileSystem->FileExists(solutionPath))
  155. {
  156. CurrentBuildError(ToString("Generated solution does not exist (%s : %s)", curBuild_->solutionPath_.CString(), solutionPath.CString()));
  157. return;
  158. }
  159. }
  160. else if (ext == ".json")
  161. {
  162. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  163. gen->SetSupportedPlatforms(curBuild_->platforms_);
  164. gen->SetRewriteSolution(true);
  165. if (!gen->LoadJSONProject(solutionPath))
  166. {
  167. CurrentBuildError(ToString("Error loading project (%s)", solutionPath.CString()));
  168. return;
  169. }
  170. if (!gen->Generate())
  171. {
  172. CurrentBuildError(ToString("Error generating project (%s)", solutionPath.CString()));
  173. return;
  174. }
  175. solutionPath = gen->GetSolution()->GetOutputFilename();
  176. requiresNuGet = gen->GetRequiresNuGet();
  177. if (!fileSystem->FileExists(solutionPath))
  178. {
  179. CurrentBuildError(ToString("Generated solution does not exist (%s : %s)", curBuild_->solutionPath_.CString(), solutionPath.CString()));
  180. return;
  181. }
  182. }
  183. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  184. const String& nugetBinary = tenv->GetAtomicNETNuGetBinary();
  185. if (requiresNuGet && !fileSystem->FileExists(nugetBinary))
  186. {
  187. CurrentBuildError(ToString("NuGet binary is missing (%s)", nugetBinary.CString()));
  188. return;
  189. }
  190. StringVector stringVector;
  191. String platforms;
  192. StringVector processedPlatforms;
  193. String configs;
  194. for (unsigned i = 0; i < curBuild_->configurations_.Size(); i++)
  195. {
  196. stringVector.Push(ToString("/p:Configuration=%s", curBuild_->configurations_[i].CString()));
  197. }
  198. configs = String::Joined(stringVector, " ");
  199. stringVector.Clear();
  200. for (unsigned i = 0; i < curBuild_->platforms_.Size(); i++)
  201. {
  202. // map platform
  203. String platform = curBuild_->platforms_[i];
  204. if (platform == "windows" || platform == "macosx" || platform == "linux")
  205. {
  206. ATOMIC_LOGINFOF("Platform \"%s\" mapped to \"desktop\"", platform.CString());
  207. platform = "desktop";
  208. }
  209. if (processedPlatforms.Contains(platform))
  210. {
  211. ATOMIC_LOGWARNINGF("Platform \"%s\" is duplicated, skipping", platform.CString());
  212. continue;
  213. }
  214. processedPlatforms.Push(platform);
  215. if (platform == "desktop" || platform == "android")
  216. {
  217. platform = "\"Any CPU\"";
  218. }
  219. else if (platform == "ios")
  220. {
  221. platform = "\"Any CPU\"";
  222. // TODO
  223. // platform = "iPhone";
  224. }
  225. else
  226. {
  227. ATOMIC_LOGERRORF("Unknown platform: %s, skipping", platform.CString());
  228. continue;
  229. }
  230. platform = ToString("/p:Platform=%s", platform.CString());
  231. if (stringVector.Contains(platform))
  232. {
  233. // This can happen when specifying Desktop + Android for example
  234. continue;
  235. }
  236. stringVector.Push(platform);
  237. }
  238. platforms = String::Joined(stringVector, " ");
  239. stringVector.Clear();
  240. Vector<String> args;
  241. #ifdef ATOMIC_PLATFORM_WINDOWS
  242. // VS2015
  243. String vs2015ToolsPath = Poco::Environment::get("VS140COMNTOOLS", "").c_str();
  244. // VS2017
  245. String vs2017ToolsPath;
  246. Poco::WinRegistryKey regKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\VisualStudio\\SxS\\VS7", true);
  247. if (regKey.exists() && regKey.exists("15.0"))
  248. vs2017ToolsPath = regKey.getString("15.0").c_str();
  249. if (vs2017ToolsPath.Length())
  250. {
  251. vs2017ToolsPath += "Common7\\Tools\\";
  252. }
  253. String cmdToolsPath;
  254. if (toolVersion_ == "VS2017" && vs2017ToolsPath.Length())
  255. {
  256. cmdToolsPath = vs2017ToolsPath;
  257. }
  258. else
  259. {
  260. cmdToolsPath = vs2015ToolsPath;
  261. }
  262. if (!cmdToolsPath.Length())
  263. {
  264. CurrentBuildError("VS140COMNTOOLS environment variable and VS2017 registry key not found, cannot proceed");
  265. return;
  266. }
  267. if (!cmdToolsPath.EndsWith("\\"))
  268. {
  269. cmdToolsPath += "\\";
  270. }
  271. String msbuildcmd = ToString("%sVsMSBuildCmd.bat", cmdToolsPath.CString());
  272. String cmd = "cmd";
  273. args.Push("/A");
  274. args.Push("/C");
  275. // vcvars bat
  276. String compile = ToString("\"\"%s\" ", msbuildcmd.CString());
  277. if (requiresNuGet)
  278. {
  279. compile += ToString("&& \"%s\" restore \"%s\" ", nugetBinary.CString(), solutionPath.CString());
  280. }
  281. compile += ToString("&& msbuild \"%s\" %s %s", solutionPath.CString(), platforms.CString(), configs.CString());
  282. if (curBuild_->targets_.Size()) {
  283. StringVector targets;
  284. for (unsigned i = 0; i < curBuild_->targets_.Size(); i++)
  285. {
  286. const char* tname = curBuild_->targets_[i].CString();
  287. targets.Push(ToString("/t:\"%s:Rebuild\"", tname));
  288. }
  289. compile += " " + String::Joined(targets, " ");
  290. }
  291. // close out quote
  292. compile += "\"";
  293. args.Push(compile);
  294. #else
  295. String compile;
  296. String cmd = "bash";
  297. args.Push("-c");
  298. String xbuildBinary = tenv->GetMonoExecutableDir() + "xbuild";
  299. if (requiresNuGet)
  300. {
  301. #ifdef ATOMIC_PLATFORM_OSX
  302. compile += ToString("\"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  303. #else
  304. compile += ToString("mono \"%s\" restore \"%s\" && ", nugetBinary.CString(), solutionPath.CString());
  305. #endif
  306. }
  307. compile += ToString("\"%s\" \"%s\" %s %s", xbuildBinary.CString(), solutionPath.CString(), platforms.CString(), configs.CString());
  308. if (curBuild_->targets_.Size()) {
  309. StringVector targets;
  310. for (unsigned i = 0; i < curBuild_->targets_.Size(); i++)
  311. {
  312. const char* tname = curBuild_->targets_[i].CString();
  313. targets.Push(ToString("%s:Rebuild", tname));
  314. }
  315. compile += " /target:\"" + String::Joined(targets, ";") + "\"";
  316. }
  317. args.Push(compile);
  318. #endif
  319. curBuild_->allArgs_.Join(args, " ");
  320. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  321. Subprocess* subprocess = nullptr;
  322. ATOMIC_LOGINFOF("%s : %s", cmd.CString(), curBuild_->allArgs_.CString());
  323. try
  324. {
  325. subprocess = subs->Launch(cmd, args, "");
  326. }
  327. catch (Poco::SystemException)
  328. {
  329. subprocess = nullptr;
  330. }
  331. if (!subprocess)
  332. {
  333. CurrentBuildError(ToString("NETCompile::Compile - Unable to launch MSBuild subprocess\n%s", curBuild_->allArgs_.CString()));
  334. return;
  335. }
  336. VariantMap buildBeginEventData;
  337. buildBeginEventData[NETBuildBegin::P_BUILD] = curBuild_;
  338. SendEvent(E_NETBUILDBEGIN, buildBeginEventData);
  339. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, ATOMIC_HANDLER(NETBuildSystem, HandleCompileProcessComplete));
  340. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, ATOMIC_HANDLER(NETBuildSystem, HandleSubprocessOutput));
  341. curBuild_->status_ = NETBUILD_BUILDING;
  342. }
  343. }
  344. NETBuild* NETBuildSystem::GetBuild(const String& solutionPath, const StringVector& platforms, const StringVector& configurations)
  345. {
  346. List<SharedPtr<NETBuild>>::ConstIterator itr = builds_.Begin();
  347. while (itr != builds_.End())
  348. {
  349. NETBuild* build = *itr;
  350. if (build->solutionPath_ == solutionPath && build->platforms_ == platforms && build->configurations_ == configurations)
  351. return build;
  352. itr++;
  353. }
  354. return nullptr;
  355. }
  356. NETBuild* NETBuildSystem::BuildAtomicProject(Project* project)
  357. {
  358. StringVector platforms;
  359. StringVector configurations;
  360. platforms.Push("desktop");
  361. #ifdef ATOMIC_DEBUG
  362. configurations.Push("Debug");
  363. #else
  364. configurations.Push("Release");
  365. #endif
  366. AtomicNETCopyAssemblies(context_, project->GetProjectPath() + "AtomicNET/Lib/");
  367. String solutionPath = project->GetProjectPath() + "AtomicNET/Solution/" + project->GetProjectSettings()->GetName() + ".sln";
  368. NETBuild* build = Build(solutionPath, platforms, configurations);
  369. if (build)
  370. {
  371. ProjectSettings* settings = project->GetProjectSettings();
  372. // This path is currently only hit when refreshing for desktop
  373. if (settings->GetSupportsAndroid() || settings->GetSupportsIOS())
  374. {
  375. // Build the PCL, which will get copied to Resources
  376. build->targets_.Push(project->GetProjectSettings()->GetName());
  377. // Build the Desktop executable, so we can run it
  378. // IMPORTANT NOTE: msbuild requires replacing '.' with '_' when in project name
  379. build->targets_.Push(project->GetProjectSettings()->GetName() + "_Desktop");
  380. }
  381. build->project_ = project;
  382. }
  383. ATOMIC_LOGINFOF("Received build for project %s", project->GetProjectFilePath().CString());
  384. return build;
  385. }
  386. void NETBuildSystem::HandleBuildAtomicProject(StringHash eventType, VariantMap& eventData)
  387. {
  388. using namespace NETBuildAtomicProject;
  389. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  390. if (!project)
  391. {
  392. ATOMIC_LOGERROR("NETBuildSystem::HandleBuildAtomicProject - null project");
  393. return;
  394. }
  395. BuildAtomicProject(project);
  396. }
  397. NETBuild* NETBuildSystem::Build(const String& solutionPath, const StringVector& platforms, const StringVector& configurations)
  398. {
  399. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  400. if (!fileSystem->FileExists(solutionPath))
  401. {
  402. ATOMIC_LOGERRORF("NETBuildSystem::Build - Solution does not exist (%s)", solutionPath.CString());
  403. return 0;
  404. }
  405. // Get existing build
  406. SharedPtr<NETBuild> build(GetBuild(solutionPath, platforms, configurations));
  407. if (build.NotNull())
  408. return build;
  409. // Create a new build
  410. build = new NETBuild(context_, solutionPath, platforms, configurations);
  411. builds_.Push(build);
  412. return build;
  413. }
  414. }