NETBuildSystem.cpp 18 KB

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