cmGlobalFastbuildGenerator.cxx 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmGlobalFastbuildGenerator.h"
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <initializer_list>
  7. #include <iterator>
  8. #include <queue>
  9. #include <sstream>
  10. #include <cm/memory>
  11. #include "cmsys/FStream.hxx"
  12. #include "cmsys/RegularExpression.hxx"
  13. #include "cmFastbuildLinkLineComputer.h"
  14. #include "cmFastbuildTargetGenerator.h" // IWYU pragma: keep
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmGlobCacheEntry.h"
  18. #include "cmGlobalGenerator.h"
  19. #include "cmGlobalGeneratorFactory.h"
  20. #include "cmList.h"
  21. #include "cmLocalFastbuildGenerator.h"
  22. #include "cmLocalGenerator.h"
  23. #include "cmMakefile.h"
  24. #include "cmMessageType.h"
  25. #include "cmState.h"
  26. #include "cmStateDirectory.h"
  27. #include "cmStateSnapshot.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmValue.h"
  31. #include "cmVersion.h"
  32. #include "cmake.h"
  33. #if defined(_WIN32)
  34. # include <future>
  35. # include <objbase.h>
  36. # include <shellapi.h>
  37. #endif
  38. class cmLinkLineComputer;
  39. #define FASTBUILD_REBUILD_BFF_TARGET_NAME "rebuild-bff"
  40. #define FASTBUILD_GLOB_CHECK_TARGET "glob-check"
  41. #define FASTBUILD_ENV_VAR_NAME "LocalEnv"
  42. // IDE support
  43. #define FASTBUILD_XCODE_BASE_PATH "XCode/Projects"
  44. #define FASTBUILD_VS_BASE_PATH "VisualStudio/Projects"
  45. #define FASTBUILD_IDE_VS_COMMAND_PREFIX "cd ^$(SolutionDir).. && "
  46. #define FASTBUILD_DEFAULT_IDE_BUILD_ARGS " -ide -cache -summary -dist "
  47. constexpr auto FASTBUILD_CAPTURE_SYSTEM_ENV =
  48. "CMAKE_FASTBUILD_CAPTURE_SYSTEM_ENV";
  49. constexpr auto FASTBUILD_ENV_OVERRIDES = "CMAKE_FASTBUILD_ENV_OVERRIDES";
  50. // Inherits from "CMAKE_FASTBUILD_VERBOSE_GENERATOR" env variable.
  51. constexpr auto FASTBUILD_VERBOSE_GENERATOR =
  52. "CMAKE_FASTBUILD_VERBOSE_GENERATOR";
  53. constexpr auto FASTBUILD_CACHE_PATH = "CMAKE_FASTBUILD_CACHE_PATH";
  54. // Compiler settings.
  55. constexpr auto FASTBUILD_COMPILER_EXTRA_FILES =
  56. "CMAKE_FASTBUILD_COMPILER_EXTRA_FILES";
  57. constexpr auto FASTBUILD_USE_LIGHTCACHE = "CMAKE_FASTBUILD_USE_LIGHTCACHE";
  58. constexpr auto FASTBUILD_USE_RELATIVE_PATHS =
  59. "CMAKE_FASTBUILD_USE_RELATIVE_PATHS";
  60. constexpr auto FASTBUILD_USE_DETERMINISTIC_PATHS =
  61. "CMAKE_FASTBUILD_USE_DETERMINISTIC_PATHS";
  62. constexpr auto FASTBUILD_SOURCE_MAPPING = "CMAKE_FASTBUILD_SOURCE_MAPPING";
  63. constexpr auto FASTBUILD_CLANG_REWRITE_INCLUDES =
  64. "CMAKE_FASTBUILD_CLANG_REWRITE_INCLUDES";
  65. constexpr auto FASTBUILD_CLANG_GCC_UPDATE_XLANG_ARG =
  66. "CMAKE_FASTBUILD_CLANG_GCC_UPDATE_XLANG_ARG";
  67. constexpr auto FASTBUILD_ALLOW_RESPONSE_FILE =
  68. "CMAKE_FASTBUILD_ALLOW_RESPONSE_FILE";
  69. constexpr auto FASTBUILD_FORCE_RESPONSE_FILE =
  70. "CMAKE_FASTBUILD_FORCE_RESPONSE_FILE";
  71. constexpr auto FASTBUILD_IDE_ARGS = "CMAKE_FASTBUILD_IDE_ARGS";
  72. static std::map<std::string, std::string> const compilerIdToFastbuildFamily = {
  73. { "MSVC", "msvc" }, { "Clang", "clang" }, { "AppleClang", "clang" },
  74. { "GNU", "gcc" }, { "NVIDIA", "cuda-nvcc" }, { "Clang-cl", "clang-cl" },
  75. };
  76. static std::set<std::string> const supportedLanguages = { "C", "CXX", "CUDA",
  77. "OBJC", "OBJCXX" };
  78. template <class T>
  79. FastbuildAliasNode generateAlias(std::string const& name, char const* postfix,
  80. T const& nodes)
  81. {
  82. FastbuildAliasNode alias;
  83. alias.Name = name + postfix;
  84. for (auto const& node : nodes) {
  85. alias.PreBuildDependencies.emplace(node.Name);
  86. }
  87. return alias;
  88. }
  89. void FastbuildTarget::GenerateAliases()
  90. {
  91. // -deps
  92. this->DependenciesAlias.Name =
  93. this->Name + FASTBUILD_DEPS_ARTIFACTS_ALIAS_POSTFIX;
  94. for (auto const& dep : this->PreBuildDependencies) {
  95. if (dep.Type != FastbuildTargetDepType::ORDER_ONLY) {
  96. this->DependenciesAlias.PreBuildDependencies.emplace(dep);
  97. }
  98. }
  99. // PRE/POST/REST
  100. if (!this->PreBuildExecNodes.PreBuildDependencies.empty()) {
  101. this->PreBuildExecNodes.Name =
  102. this->Name + FASTBUILD_PRE_BUILD_ALIAS_POSTFIX;
  103. }
  104. if (!this->PreLinkExecNodes.Nodes.empty()) {
  105. this->PreLinkExecNodes.Alias =
  106. generateAlias(this->Name, FASTBUILD_PRE_LINK_ALIAS_POSTFIX,
  107. this->PreLinkExecNodes.Nodes);
  108. }
  109. if (!this->PostBuildExecNodes.Alias.PreBuildDependencies.empty()) {
  110. this->PostBuildExecNodes.Alias.Name =
  111. this->Name + FASTBUILD_POST_BUILD_ALIAS_POSTFIX;
  112. }
  113. if (!this->ExecNodes.PreBuildDependencies.empty()) {
  114. this->ExecNodes.Name = this->Name + FASTBUILD_CUSTOM_COMMAND_ALIAS_POSTFIX;
  115. }
  116. // If we don't have any node that we can build by name (e.g. no static /
  117. // dynamic lib or executable) -> create an alias so that we can build this
  118. // target by name.
  119. if (LinkerNode.empty()) {
  120. FastbuildAliasNode alias;
  121. alias.Name = this->Name;
  122. if (LinkerNode.empty()) {
  123. for (FastbuildObjectListNode const& objListNode : ObjectListNodes) {
  124. alias.PreBuildDependencies.emplace(objListNode.Name);
  125. }
  126. } else {
  127. for (FastbuildLinkerNode const& linkerNode : LinkerNode) {
  128. alias.PreBuildDependencies.emplace(linkerNode.Name);
  129. }
  130. }
  131. AliasNodes.emplace_back(std::move(alias));
  132. }
  133. // Link artifacts (should not be added to all
  134. // since on Windows it might contain Import Lib and FASTBuild doesn't know
  135. // how to create it, so "-all" will fail).
  136. AliasNodes.emplace_back(generateAlias(
  137. this->Name, FASTBUILD_OBJECTS_ALIAS_POSTFIX, this->ObjectListNodes));
  138. for (auto const& linkerNode : this->LinkerNode) {
  139. if (linkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY ||
  140. linkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY ||
  141. linkerNode.Type == FastbuildLinkerNode::EXECUTABLE) {
  142. std::string postfix = FASTBUILD_LINK_ARTIFACTS_ALIAS_POSTFIX;
  143. if (!linkerNode.Arch.empty()) {
  144. postfix += cmStrCat('-', linkerNode.Arch);
  145. }
  146. #ifdef _WIN32
  147. // On Windows DLL and Executables must be linked via Import Lib file
  148. // (.lib).
  149. if (linkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY ||
  150. linkerNode.Type == FastbuildLinkerNode::EXECUTABLE) {
  151. FastbuildAliasNode linkAlias;
  152. linkAlias.Name = this->Name + FASTBUILD_LINK_ARTIFACTS_ALIAS_POSTFIX;
  153. linkAlias.PreBuildDependencies.emplace(
  154. FASTBUILD_DOLLAR_TAG "TargetOutputImplib" FASTBUILD_DOLLAR_TAG);
  155. AliasNodes.emplace_back(std::move(linkAlias));
  156. continue;
  157. }
  158. #endif
  159. FastbuildAliasNode alias;
  160. alias.Name = this->Name + postfix;
  161. alias.PreBuildDependencies.emplace(linkerNode.LinkerOutput);
  162. AliasNodes.emplace_back(std::move(alias));
  163. }
  164. }
  165. }
  166. cmGlobalFastbuildGenerator::cmGlobalFastbuildGenerator(cmake* cm)
  167. : cmGlobalCommonGenerator(cm)
  168. , BuildFileStream(nullptr)
  169. {
  170. #ifdef _WIN32
  171. cm->GetState()->SetWindowsShell(true);
  172. #endif
  173. this->FindMakeProgramFile = "CMakeFastbuildFindMake.cmake";
  174. cm->GetState()->SetFastbuildMake(true);
  175. cm->GetState()->SetIsGeneratorMultiConfig(false);
  176. }
  177. void cmGlobalFastbuildGenerator::ReadCompilerOptions(
  178. FastbuildCompiler& compiler, cmMakefile* mf)
  179. {
  180. if (compiler.CompilerFamily == "custom") {
  181. return;
  182. }
  183. if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_USE_LIGHTCACHE))) {
  184. compiler.UseLightCache = true;
  185. }
  186. if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_USE_RELATIVE_PATHS))) {
  187. compiler.UseRelativePaths = true;
  188. UsingRelativePaths = true;
  189. }
  190. if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_USE_DETERMINISTIC_PATHS))) {
  191. compiler.UseDeterministicPaths = true;
  192. }
  193. std::string sourceMapping = mf->GetSafeDefinition(FASTBUILD_SOURCE_MAPPING);
  194. if (!sourceMapping.empty()) {
  195. compiler.SourceMapping = std::move(sourceMapping);
  196. }
  197. auto const clangRewriteIncludesDef =
  198. mf->GetDefinition(FASTBUILD_CLANG_REWRITE_INCLUDES);
  199. if (clangRewriteIncludesDef.IsSet() && clangRewriteIncludesDef.IsOff()) {
  200. compiler.ClangRewriteIncludes = false;
  201. }
  202. if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_CLANG_GCC_UPDATE_XLANG_ARG))) {
  203. compiler.ClangGCCUpdateXLanguageArg = true;
  204. }
  205. if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_ALLOW_RESPONSE_FILE))) {
  206. compiler.AllowResponseFile = true;
  207. }
  208. if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_FORCE_RESPONSE_FILE))) {
  209. compiler.ForceResponseFile = true;
  210. }
  211. }
  212. void cmGlobalFastbuildGenerator::ProcessEnvironment()
  213. {
  214. bool const CaptureSystemEnv =
  215. !this->GetGlobalSetting(FASTBUILD_CAPTURE_SYSTEM_ENV).IsSet() ||
  216. this->GetGlobalSetting(FASTBUILD_CAPTURE_SYSTEM_ENV).IsOn();
  217. // On Windows environment is needed for MSVC, but preserve ability to discard
  218. // it from the generated file if requested.
  219. if (CaptureSystemEnv) {
  220. LocalEnvironment = cmSystemTools::GetEnvironmentVariables();
  221. }
  222. // FASTBuild strips off "-isysroot" command line option (see :
  223. // https://github.com/fastbuild/fastbuild/issues/1066).
  224. // If 'SDK_ROOT' is not set via env and '-isysroot' is absent, AppleClang
  225. // seems to use MacOS SDK by default (even though FBuild flattens includes
  226. // before compiling). It breaks cross-compilation for iOS. Tested in
  227. // "RunCMake.Framework" test.
  228. std::string const osxRoot = this->GetSafeGlobalSetting("CMAKE_OSX_SYSROOT");
  229. if (!osxRoot.empty()) {
  230. LocalEnvironment.emplace_back("SDKROOT=" + osxRoot);
  231. }
  232. auto const EnvOverrides =
  233. this->GetSafeGlobalSetting(FASTBUILD_ENV_OVERRIDES);
  234. if (!EnvOverrides.empty()) {
  235. auto const overrideEnvVar = [this](std::string const& prefix,
  236. std::string val) {
  237. auto const iter =
  238. std::find_if(LocalEnvironment.begin(), LocalEnvironment.end(),
  239. [&prefix](std::string const& value) {
  240. return cmSystemTools::StringStartsWith(value.c_str(),
  241. prefix.c_str());
  242. });
  243. if (iter != LocalEnvironment.end()) {
  244. *iter = std::move(val);
  245. } else {
  246. LocalEnvironment.emplace_back(std::move(val));
  247. }
  248. };
  249. for (auto const& val : cmList{ EnvOverrides }) {
  250. auto const pos = val.find('=');
  251. if (pos != std::string::npos && ((pos + 1) < val.size())) {
  252. overrideEnvVar(val.substr(0, pos + 1), val);
  253. }
  254. }
  255. }
  256. // Empty strings are not allowed.
  257. LocalEnvironment.erase(
  258. std::remove_if(LocalEnvironment.begin(), LocalEnvironment.end(),
  259. [](std::string const& s) { return s.empty(); }),
  260. LocalEnvironment.end());
  261. }
  262. std::unique_ptr<cmGlobalGeneratorFactory>
  263. cmGlobalFastbuildGenerator::NewFactory()
  264. {
  265. return std::unique_ptr<cmGlobalGeneratorFactory>(
  266. new cmGlobalGeneratorSimpleFactory<cmGlobalFastbuildGenerator>());
  267. }
  268. void cmGlobalFastbuildGenerator::EnableLanguage(
  269. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  270. {
  271. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  272. for (std::string const& l : lang) {
  273. if (l == "NONE") {
  274. continue;
  275. }
  276. this->ResolveLanguageCompiler(l, mf, optional);
  277. }
  278. }
  279. bool cmGlobalFastbuildGenerator::FindMakeProgram(cmMakefile* mf)
  280. {
  281. if (!cmGlobalGenerator::FindMakeProgram(mf)) {
  282. return false;
  283. }
  284. if (auto fastbuildCommand = mf->GetDefinition("CMAKE_MAKE_PROGRAM")) {
  285. this->FastbuildCommand = *fastbuildCommand;
  286. std::vector<std::string> command;
  287. command.push_back(this->FastbuildCommand);
  288. command.emplace_back("-version");
  289. std::string version;
  290. std::string error;
  291. if (!cmSystemTools::RunSingleCommand(command, &version, &error, nullptr,
  292. nullptr,
  293. cmSystemTools::OUTPUT_NONE)) {
  294. mf->IssueMessage(MessageType::FATAL_ERROR,
  295. "Running\n '" + cmJoin(command, "' '") +
  296. "'\n"
  297. "failed with:\n " +
  298. error);
  299. cmSystemTools::SetFatalErrorOccurred();
  300. return false;
  301. }
  302. cmsys::RegularExpression versionRegex(R"(^FASTBuild v([0-9]+\.[0-9]+))");
  303. versionRegex.find(version);
  304. this->FastbuildVersion = versionRegex.match(1);
  305. }
  306. return true;
  307. }
  308. std::unique_ptr<cmLocalGenerator>
  309. cmGlobalFastbuildGenerator::CreateLocalGenerator(cmMakefile* makefile)
  310. {
  311. return std::unique_ptr<cmLocalGenerator>(
  312. cm::make_unique<cmLocalFastbuildGenerator>(this, makefile));
  313. }
  314. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  315. cmGlobalFastbuildGenerator::GenerateBuildCommand(
  316. std::string const& makeProgram, std::string const& /*projectName*/,
  317. std::string const& projectDir, std::vector<std::string> const& targetNames,
  318. std::string const& /*config*/, int /*jobs*/, bool verbose,
  319. cmBuildOptions /*buildOptions*/, std::vector<std::string> const& makeOptions,
  320. BuildTryCompile isInTryCompile)
  321. {
  322. GeneratedMakeCommand makeCommand;
  323. this->FastbuildCommand = this->SelectMakeProgram(makeProgram);
  324. makeCommand.Add(this->FastbuildCommand);
  325. // A build command for fastbuild looks like this:
  326. // fbuild.exe [make-options] [-config projectName.bff] <target>
  327. std::string configFile = cmStrCat(projectDir, '/', FASTBUILD_BUILD_FILE);
  328. // Push in the make options
  329. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  330. if (!configFile.empty()) {
  331. makeCommand.Add("-config", configFile);
  332. }
  333. // Tested in "RunCMake.SymlinkTrees" test.
  334. makeCommand.Add("-continueafterdbmove");
  335. // Tested in RunCMake.LinkWhatYouUse on Linux. (We need to see output of
  336. // LinkerStampExe process).
  337. // In general, it might be useful to see output of external processes
  338. // regardless of their outcome.
  339. makeCommand.Add("-showcmdoutput");
  340. // Add the target-config to the command
  341. for (auto const& tname : targetNames) {
  342. if (!tname.empty()) {
  343. makeCommand.Add(tname);
  344. }
  345. }
  346. if (verbose) {
  347. makeCommand.Add("-verbose");
  348. }
  349. // Don't do extra work during "TryCompile".
  350. if (isInTryCompile == BuildTryCompile::Yes) {
  351. return { std::move(makeCommand) };
  352. }
  353. // Make "rebuild-bff" target up-to-date before running the build.
  354. std::string output;
  355. ExecuteFastbuildTarget(projectDir, FASTBUILD_REBUILD_BFF_TARGET_NAME, output,
  356. { "-why" });
  357. // If fbuild.bff was re-generated we need to "restat" it.
  358. if (output.find("Need to build") != std::string::npos) {
  359. // Let the user know that re-generation happened (and why it
  360. // happened).
  361. cmSystemTools::Stdout(output);
  362. // FASTBuild will consider the target out-of-date in case some of the
  363. // inputs have changes after re-generation which might happen if, for
  364. // example, configuration depends on some files generated during
  365. // the configuration itself.
  366. AskCMakeToMakeRebuildBFFUpToDate(projectDir);
  367. }
  368. return { std::move(makeCommand) };
  369. }
  370. void cmGlobalFastbuildGenerator::ComputeTargetObjectDirectory(
  371. cmGeneratorTarget* gt) const
  372. {
  373. // Compute full path to object file directory for this target.
  374. std::string dir =
  375. cmStrCat(gt->GetSupportDirectory(), '/', this->GetCMakeCFGIntDir(), '/');
  376. gt->ObjectDirectory = std::move(dir);
  377. }
  378. void cmGlobalFastbuildGenerator::AppendDirectoryForConfig(
  379. std::string const& prefix, std::string const& config,
  380. std::string const& suffix, std::string& dir)
  381. {
  382. if (!config.empty() && this->IsMultiConfig()) {
  383. dir += cmStrCat(prefix, config, suffix);
  384. }
  385. }
  386. cmDocumentationEntry cmGlobalFastbuildGenerator::GetDocumentation()
  387. {
  388. return { cmGlobalFastbuildGenerator::GetActualName(),
  389. "Generates fbuild.bff files." };
  390. }
  391. void cmGlobalFastbuildGenerator::Generate()
  392. {
  393. // Check minimum Fastbuild version.
  394. if (cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  395. this->FastbuildVersion,
  396. RequiredFastbuildVersion())) {
  397. std::ostringstream msg;
  398. msg << "The detected version of Fastbuild (" << this->FastbuildVersion;
  399. msg << ") is less than the version of Fastbuild required by CMake (";
  400. msg << this->RequiredFastbuildVersion() << ").";
  401. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  402. msg.str());
  403. return;
  404. }
  405. this->ProcessEnvironment();
  406. this->OpenBuildFileStream();
  407. this->WriteSettings();
  408. this->WriteEnvironment();
  409. // Execute the standard generate process
  410. cmGlobalGenerator::Generate();
  411. // Write compilers
  412. this->WriteCompilers();
  413. this->WriteTargets();
  414. this->CloseBuildFileStream();
  415. if (cmSystemTools::GetErrorOccurredFlag()) {
  416. return;
  417. }
  418. this->RemoveUnknownClangTidyExportFixesFiles();
  419. if (this->GetCMakeInstance()->GetRegenerateDuringBuild() ||
  420. this->GetCMakeInstance()->GetIsInTryCompile()) {
  421. return;
  422. }
  423. // Make "rebuild-bff" target up-to-date after the generation.
  424. // This is actually a noop, it just asks CMake to touch the generated file
  425. // so FASTBuild would consider the target as up-to-date.
  426. AskCMakeToMakeRebuildBFFUpToDate(
  427. this->GetCMakeInstance()->GetHomeOutputDirectory());
  428. }
  429. void cmGlobalFastbuildGenerator::AskCMakeToMakeRebuildBFFUpToDate(
  430. std::string const& workingDir) const
  431. {
  432. // "restat" the generated build file.
  433. // The idea here is to mimic what Ninja's "restat" command does.
  434. // We need to make the "rebuild.bff" target up-to-date, so the regeneration
  435. // will only be triggered when CMake files have actually changed.
  436. // Tested in "RunCMake.Configure" test.
  437. cmsys::ofstream{
  438. cmStrCat(workingDir, '/', FASTBUILD_RESTAT_FILE).c_str(),
  439. std::ios::out | std::ios::binary
  440. } << cmStrCat(workingDir, '/', FASTBUILD_BUILD_FILE);
  441. std::string output;
  442. ExecuteFastbuildTarget(workingDir, FASTBUILD_REBUILD_BFF_TARGET_NAME,
  443. output);
  444. }
  445. void cmGlobalFastbuildGenerator::ExecuteFastbuildTarget(
  446. std::string const& dir, std::string const& target, std::string& output,
  447. std::vector<std::string> const& fbuildOptions) const
  448. {
  449. std::vector<std::string> command;
  450. command.emplace_back(this->FastbuildCommand);
  451. command.emplace_back("-config");
  452. std::string const file = cmStrCat(dir, '/', FASTBUILD_BUILD_FILE);
  453. command.emplace_back(file);
  454. command.emplace_back(target);
  455. if (!fbuildOptions.empty()) {
  456. command.emplace_back(cmJoin(fbuildOptions, " "));
  457. }
  458. int retVal = 0;
  459. if (!cmSystemTools::RunSingleCommand(command, &output, nullptr, &retVal,
  460. dir.c_str(),
  461. cmSystemTools::OUTPUT_NONE) ||
  462. retVal != 0) {
  463. cmSystemTools::Error(cmStrCat("Failed to run FASTBuild command:\n '",
  464. cmJoin(command, "' '"), "'\nOutput:\n",
  465. output));
  466. cmSystemTools::Stdout(output);
  467. std::exit(retVal);
  468. }
  469. }
  470. void cmGlobalFastbuildGenerator::WriteSettings()
  471. {
  472. // Define some placeholder
  473. WriteDivider();
  474. *this->BuildFileStream << "// Helper variables\n\n";
  475. WriteVariable("FB_INPUT_1_PLACEHOLDER", Quote("\"%1\""));
  476. WriteVariable("FB_INPUT_1_0_PLACEHOLDER", Quote("\"%1[0]\""));
  477. WriteVariable("FB_INPUT_1_1_PLACEHOLDER", Quote("\"%1[1]\""));
  478. WriteVariable("FB_INPUT_2_PLACEHOLDER", Quote("\"%2\""));
  479. WriteVariable("FB_INPUT_3_PLACEHOLDER", Quote("\"%3\""));
  480. std::string cacheDir;
  481. // If explicitly set from CMake.
  482. auto val = this->GetSafeGlobalSetting(FASTBUILD_CACHE_PATH);
  483. if (!val.empty()) {
  484. cacheDir = std::move(val);
  485. cmSystemTools::ConvertToOutputSlashes(cacheDir);
  486. }
  487. WriteDivider();
  488. *this->BuildFileStream << "// Settings\n\n";
  489. WriteCommand("Settings");
  490. *this->BuildFileStream << "{\n";
  491. if (!cacheDir.empty()) {
  492. WriteVariable("CachePath", Quote(cacheDir), 1);
  493. }
  494. // Concurrency groups.
  495. WriteStruct(
  496. FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME,
  497. { { "ConcurrencyGroupName", Quote(FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME) },
  498. { "ConcurrencyLimit", "1" } },
  499. 1);
  500. WriteArray("ConcurrencyGroups",
  501. { "." FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME }, 1);
  502. *this->BuildFileStream << "}\n";
  503. }
  504. void cmGlobalFastbuildGenerator::WriteEnvironment()
  505. {
  506. if (!LocalEnvironment.empty()) {
  507. WriteArray(FASTBUILD_ENV_VAR_NAME, Wrap(LocalEnvironment), 0);
  508. }
  509. }
  510. void cmGlobalFastbuildGenerator::WriteDivider()
  511. {
  512. *this->BuildFileStream << "// ======================================"
  513. "=======================================\n";
  514. }
  515. void cmGlobalFastbuildGenerator::Indent(int count)
  516. {
  517. for (int i = 0; i < count; ++i) {
  518. *this->BuildFileStream << " ";
  519. }
  520. }
  521. void cmGlobalFastbuildGenerator::WriteComment(std::string const& comment,
  522. int indent)
  523. {
  524. if (comment.empty()) {
  525. return;
  526. }
  527. std::string::size_type lpos = 0;
  528. std::string::size_type rpos;
  529. *this->BuildFileStream << "\n";
  530. Indent(indent);
  531. *this->BuildFileStream << "/////////////////////////////////////////////\n";
  532. while ((rpos = comment.find('\n', lpos)) != std::string::npos) {
  533. Indent(indent);
  534. *this->BuildFileStream << "// " << comment.substr(lpos, rpos - lpos)
  535. << "\n";
  536. lpos = rpos + 1;
  537. }
  538. Indent(indent);
  539. *this->BuildFileStream << "// " << comment.substr(lpos) << "\n\n";
  540. }
  541. void cmGlobalFastbuildGenerator::WriteVariable(std::string const& key,
  542. std::string const& value,
  543. int indent)
  544. {
  545. WriteVariable(key, value, "=", indent);
  546. }
  547. void cmGlobalFastbuildGenerator::WriteVariable(std::string const& key,
  548. std::string const& value,
  549. std::string const& op,
  550. int indent)
  551. {
  552. Indent(indent);
  553. *this->BuildFileStream << "." << key << " " + op + (value.empty() ? "" : " ")
  554. << value << "\n";
  555. }
  556. void cmGlobalFastbuildGenerator::WriteCommand(std::string const& command,
  557. std::string const& value,
  558. int indent)
  559. {
  560. Indent(indent);
  561. *this->BuildFileStream << command;
  562. if (!value.empty()) {
  563. *this->BuildFileStream << "(" << value << ")";
  564. }
  565. *this->BuildFileStream << "\n";
  566. }
  567. void cmGlobalFastbuildGenerator::WriteArray(
  568. std::string const& key, std::vector<std::string> const& values, int indent)
  569. {
  570. WriteArray(key, values, "=", indent);
  571. }
  572. void cmGlobalFastbuildGenerator::WriteArray(
  573. std::string const& key, std::vector<std::string> const& values,
  574. std::string const& op, int indent)
  575. {
  576. WriteVariable(key, "", op, indent);
  577. Indent(indent);
  578. *this->BuildFileStream << "{\n";
  579. char const* sep = "";
  580. for (std::string const& value : values) {
  581. *this->BuildFileStream << sep;
  582. sep = ",\n";
  583. Indent(indent + 1);
  584. *this->BuildFileStream << value;
  585. }
  586. *this->BuildFileStream << "\n";
  587. Indent(indent);
  588. *this->BuildFileStream << "}\n";
  589. }
  590. void cmGlobalFastbuildGenerator::WriteStruct(
  591. std::string const& name,
  592. std::vector<std::pair<std::string, std::string>> const& variables,
  593. int indent)
  594. {
  595. WriteVariable(name, "", "=", indent);
  596. Indent(indent);
  597. *this->BuildFileStream << "[\n";
  598. for (auto const& val : variables) {
  599. auto const& key = val.first;
  600. auto const& value = val.second;
  601. WriteVariable(key, value, "=", indent + 1);
  602. }
  603. Indent(indent);
  604. *this->BuildFileStream << "]\n";
  605. }
  606. std::string cmGlobalFastbuildGenerator::Quote(std::string const& str,
  607. std::string const& quotation)
  608. {
  609. std::string result = str;
  610. cmSystemTools::ReplaceString(result, quotation, "^" + quotation);
  611. cmSystemTools::ReplaceString(result, FASTBUILD_DOLLAR_TAG, "$");
  612. return quotation + result + quotation;
  613. }
  614. std::string cmGlobalFastbuildGenerator::QuoteIfHasSpaces(std::string str)
  615. {
  616. if (str.find(' ') != std::string::npos) {
  617. return '"' + str + '"';
  618. }
  619. return str;
  620. }
  621. struct WrapHelper
  622. {
  623. std::string Prefix;
  624. std::string Suffix;
  625. bool EscapeDollar;
  626. std::string operator()(std::string in)
  627. {
  628. // If we have ^ in env variable - need to escape it.
  629. cmSystemTools::ReplaceString(in, "^", "^^");
  630. // Those all are considered as line ends by FASTBuild.
  631. cmSystemTools::ReplaceString(in, "\n", "\\n");
  632. cmSystemTools::ReplaceString(in, "\r", "\\r");
  633. // Escaping of single quotes tested in "RunCMake.CompilerArgs" test.
  634. cmSystemTools::ReplaceString(in, "'", "^'");
  635. std::string result = Prefix + in + Suffix;
  636. if (EscapeDollar) {
  637. cmSystemTools::ReplaceString(result, "$", "^$");
  638. cmSystemTools::ReplaceString(result, FASTBUILD_DOLLAR_TAG, "$");
  639. }
  640. return result;
  641. }
  642. std::string operator()(FastbuildTargetDep const& in)
  643. {
  644. return (*this)(in.Name);
  645. }
  646. };
  647. template <class T>
  648. std::vector<std::string> cmGlobalFastbuildGenerator::Wrap(
  649. T const& in, std::string const& prefix, std::string const& suffix,
  650. bool const escape_dollar)
  651. {
  652. std::vector<std::string> result;
  653. WrapHelper helper = { prefix, suffix, escape_dollar };
  654. std::transform(in.begin(), in.end(), std::back_inserter(result), helper);
  655. return result;
  656. }
  657. void cmGlobalFastbuildGenerator::TopologicalSort(
  658. std::vector<FastbuildTargetPtrT>& nodes)
  659. {
  660. std::unordered_map<std::string, int> inDegree;
  661. std::unordered_map<std::string, std::set<std::string>> reverseDeps;
  662. std::unordered_map<std::string, std::size_t> originalIndex;
  663. // Track original positions
  664. for (std::size_t i = 0; i < nodes.size(); ++i) {
  665. auto const& node = nodes[i];
  666. inDegree[node->Name] = 0;
  667. originalIndex[node->Name] = i;
  668. }
  669. // Build reverse dependency graph and in-degree map
  670. for (auto const& node : nodes) {
  671. for (auto const& dep : node->PreBuildDependencies) {
  672. if (inDegree.count(dep.Name)) {
  673. reverseDeps[dep.Name].insert(node->Name);
  674. ++inDegree[node->Name];
  675. }
  676. }
  677. }
  678. // Min-heap based on original position
  679. auto const cmp = [&](std::string const& a, std::string const& b) {
  680. return originalIndex[a] > originalIndex[b];
  681. };
  682. std::priority_queue<std::string, std::vector<std::string>, decltype(cmp)>
  683. zeroInDegree(cmp);
  684. for (auto const& val : inDegree) {
  685. auto const& degree = val.second;
  686. auto const& name = val.first;
  687. if (degree == 0) {
  688. zeroInDegree.push(name);
  689. }
  690. }
  691. std::vector<std::string> sorted;
  692. while (!zeroInDegree.empty()) {
  693. std::string node = zeroInDegree.top();
  694. zeroInDegree.pop();
  695. sorted.push_back(node);
  696. for (auto const& dep : reverseDeps[node]) {
  697. if (--inDegree[dep] == 0) {
  698. zeroInDegree.push(dep);
  699. }
  700. }
  701. }
  702. if (sorted.size() != nodes.size()) {
  703. cmSystemTools::Error("Failed to sort (Cyclic dependency)");
  704. cmSystemTools::Error(cmStrCat("Sorted size: ", sorted.size()));
  705. cmSystemTools::Error(cmStrCat("nodes size: ", nodes.size()));
  706. for (auto const& node : nodes) {
  707. cmSystemTools::Error("Node: " + node->Name);
  708. for (auto const& dep : reverseDeps[node->Name]) {
  709. cmSystemTools::Error("\tReverse dep: " + dep);
  710. }
  711. for (auto const& child : node->PreBuildDependencies) {
  712. cmSystemTools::Error("\tChild: " + child.Name);
  713. }
  714. }
  715. for (auto const& node : sorted) {
  716. cmSystemTools::Error("Sorted: " + node);
  717. }
  718. for (auto const& node : nodes) {
  719. cmSystemTools::Error("In node: " + node->Name);
  720. }
  721. }
  722. // Reconstruct sorted nodes
  723. std::vector<FastbuildTargetPtrT> result;
  724. for (auto const& name : sorted) {
  725. auto it = std::find_if(
  726. nodes.begin(), nodes.end(), [&name](FastbuildTargetPtrT const& node) {
  727. return node /* the node might be in moved-from state*/ &&
  728. node->Name == name;
  729. });
  730. if (it != nodes.end()) {
  731. result.emplace_back(std::move(*it));
  732. }
  733. }
  734. std::swap(result, nodes);
  735. }
  736. void cmGlobalFastbuildGenerator::WriteDisclaimer()
  737. {
  738. *this->BuildFileStream << "// CMAKE generated file: DO NOT EDIT!\n"
  739. << "// Generated by \"" << this->GetName() << "\""
  740. << " Generator, CMake Version "
  741. << cmVersion::GetMajorVersion() << "."
  742. << cmVersion::GetMinorVersion() << "\n\n";
  743. }
  744. void cmGlobalFastbuildGenerator::OpenBuildFileStream()
  745. {
  746. // Compute Fastbuild's build file path.
  747. std::string buildFilePath =
  748. this->GetCMakeInstance()->GetHomeOutputDirectory();
  749. buildFilePath += "/";
  750. buildFilePath += FASTBUILD_BUILD_FILE;
  751. // Get a stream where to generate things.
  752. if (!this->BuildFileStream) {
  753. this->BuildFileStream = cm::make_unique<cmGeneratedFileStream>(
  754. buildFilePath, false, this->GetMakefileEncoding());
  755. if (!this->BuildFileStream) {
  756. // An error message is generated by the constructor if it cannot
  757. // open the file.
  758. return;
  759. }
  760. }
  761. // Write the do not edit header.
  762. this->WriteDisclaimer();
  763. // Write a comment about this file.
  764. *this->BuildFileStream
  765. << "// This file contains all the build statements\n\n";
  766. }
  767. void cmGlobalFastbuildGenerator::CloseBuildFileStream()
  768. {
  769. if (this->BuildFileStream) {
  770. this->BuildFileStream.reset();
  771. } else {
  772. cmSystemTools::Error("Build file stream was not open.");
  773. }
  774. }
  775. void cmGlobalFastbuildGenerator::WriteCompilers()
  776. {
  777. WriteDivider();
  778. *this->BuildFileStream << "// Compilers\n\n";
  779. for (auto const& val : Compilers) {
  780. auto const& compilerDef = val.second;
  781. std::string compilerPath = compilerDef.Executable;
  782. // Write out the compiler that has been configured
  783. WriteCommand("Compiler", Quote(compilerDef.Name));
  784. *this->BuildFileStream << "{\n";
  785. for (auto const& extra : compilerDef.ExtraVariables) {
  786. auto const& extraKey = extra.first;
  787. auto const& extraVal = extra.second;
  788. WriteVariable(extraKey, Quote(extraVal), 1);
  789. }
  790. WriteVariable("Executable", Quote(compilerPath), 1);
  791. WriteVariable("CompilerFamily", Quote(compilerDef.CompilerFamily), 1);
  792. if (compilerDef.UseLightCache && compilerDef.CompilerFamily == "msvc") {
  793. WriteVariable("UseLightCache_Experimental", "true", 1);
  794. }
  795. if (compilerDef.UseRelativePaths) {
  796. WriteVariable("UseRelativePaths_Experimental", "true", 1);
  797. }
  798. if (compilerDef.UseDeterministicPaths) {
  799. WriteVariable("UseDeterministicPaths_Experimental", "true", 1);
  800. }
  801. if (!compilerDef.SourceMapping.empty()) {
  802. WriteVariable("SourceMapping_Experimental",
  803. Quote(compilerDef.SourceMapping), 1);
  804. }
  805. auto const isClang = [&compilerDef] {
  806. return compilerDef.CompilerFamily == "clang" ||
  807. compilerDef.CompilerFamily == "clang-cl";
  808. };
  809. if (!compilerDef.ClangRewriteIncludes && isClang()) {
  810. WriteVariable("ClangRewriteIncludes", "false", 1);
  811. }
  812. if (compilerDef.ClangGCCUpdateXLanguageArg &&
  813. (isClang() || compilerDef.CompilerFamily == "gcc")) {
  814. WriteVariable("ClangGCCUpdateXLanguageArg", "true", 1);
  815. }
  816. if (compilerDef.AllowResponseFile) {
  817. WriteVariable("AllowResponseFile", "true", 1);
  818. }
  819. if (compilerDef.ForceResponseFile) {
  820. WriteVariable("ForceResponseFile", "true", 1);
  821. }
  822. if (compilerDef.DontUseEnv) {
  823. LogMessage("Not using system environment");
  824. } else {
  825. if (!LocalEnvironment.empty()) {
  826. WriteVariable("Environment", "." FASTBUILD_ENV_VAR_NAME, 1);
  827. }
  828. }
  829. if (!compilerDef.ExtraFiles.empty()) {
  830. // Do not escape '$' sign, CMAKE_${LANG}_FASTBUILD_EXTRA_FILES might
  831. // contain FB variables to be expanded (we do use some internally).
  832. // Besides a path cannot contain a '$'
  833. WriteArray("ExtraFiles", Wrap(compilerDef.ExtraFiles, "'", "'", false),
  834. 1);
  835. }
  836. *this->BuildFileStream << "}\n";
  837. auto const compilerId = compilerDef.Name;
  838. WriteVariable(compilerId, Quote(compilerDef.Name));
  839. *this->BuildFileStream << "\n";
  840. }
  841. // We need this because the Library command needs a compiler
  842. // even if don't compile anything
  843. if (!this->Compilers.empty()) {
  844. WriteVariable("Compiler_dummy",
  845. Quote(this->Compilers.begin()->second.Name));
  846. }
  847. }
  848. void cmGlobalFastbuildGenerator::AddCompiler(std::string const& language,
  849. cmMakefile* mf)
  850. {
  851. if (this->Compilers.find(FASTBUILD_COMPILER_PREFIX + language) !=
  852. this->Compilers.end()) {
  853. return;
  854. }
  855. // Calculate the root location of the compiler
  856. std::string const variableString = "CMAKE_" + language + "_COMPILER";
  857. std::string const compilerLocation = mf->GetSafeDefinition(variableString);
  858. if (compilerLocation.empty()) {
  859. return;
  860. }
  861. // Calculate the i18n number.
  862. std::string i18nNum = "1033";
  863. // Add the language to the compiler's name
  864. FastbuildCompiler compilerDef;
  865. compilerDef.ExtraVariables["Root"] =
  866. cmSystemTools::GetFilenamePath(compilerLocation);
  867. compilerDef.Name = FASTBUILD_COMPILER_PREFIX + language;
  868. compilerDef.Executable = compilerLocation;
  869. compilerDef.CmakeCompilerID =
  870. mf->GetSafeDefinition("CMAKE_" + language + "_COMPILER_ID");
  871. if (compilerDef.CmakeCompilerID == "Clang" &&
  872. mf->GetSafeDefinition("CMAKE_" + language +
  873. "_COMPILER_FRONTEND_VARIANT") == "MSVC") {
  874. compilerDef.CmakeCompilerID = "Clang-cl";
  875. }
  876. compilerDef.CmakeCompilerVersion =
  877. mf->GetSafeDefinition("CMAKE_" + language + "_COMPILER_VERSION");
  878. compilerDef.Language = language;
  879. cmExpandList(mf->GetSafeDefinition(FASTBUILD_COMPILER_EXTRA_FILES),
  880. compilerDef.ExtraFiles);
  881. if (supportedLanguages.find(language) != supportedLanguages.end()) {
  882. auto const iter =
  883. compilerIdToFastbuildFamily.find(compilerDef.CmakeCompilerID);
  884. if (iter != compilerIdToFastbuildFamily.end()) {
  885. compilerDef.CompilerFamily = iter->second;
  886. }
  887. }
  888. // Has to be called after we determined 'CompilerFamily'.
  889. ReadCompilerOptions(compilerDef, mf);
  890. // If FASTBUILD_COMPILER_EXTRA_FILES is not set - automatically add extra
  891. // files based on compiler (see
  892. // https://fastbuild.org/docs/functions/compiler.html)
  893. if (compilerDef.ExtraFiles.empty() &&
  894. (language == "C" || language == "CXX") &&
  895. compilerDef.CmakeCompilerID == "MSVC") {
  896. // https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html
  897. // Visual Studio 17 (19.30 to 19.39)
  898. // TODO
  899. // Visual Studio 16 (19.20 to 19.29)
  900. if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  901. compilerDef.CmakeCompilerVersion,
  902. "19.20")) {
  903. compilerDef.ExtraFiles.push_back("$Root$/c1.dll");
  904. compilerDef.ExtraFiles.push_back("$Root$/c1xx.dll");
  905. compilerDef.ExtraFiles.push_back("$Root$/c2.dll");
  906. compilerDef.ExtraFiles.push_back(
  907. "$Root$/atlprov.dll"); // Only needed if using ATL
  908. compilerDef.ExtraFiles.push_back("$Root$/msobj140.dll");
  909. compilerDef.ExtraFiles.push_back("$Root$/mspdb140.dll");
  910. compilerDef.ExtraFiles.push_back("$Root$/mspdbcore.dll");
  911. compilerDef.ExtraFiles.push_back("$Root$/mspdbsrv.exe");
  912. compilerDef.ExtraFiles.push_back("$Root$/mspft140.dll");
  913. compilerDef.ExtraFiles.push_back("$Root$/msvcp140.dll");
  914. compilerDef.ExtraFiles.push_back(
  915. "$Root$/msvcp140_atomic_wait.dll"); // Required circa 16.8.3
  916. // (14.28.29333)
  917. compilerDef.ExtraFiles.push_back(
  918. "$Root$/tbbmalloc.dll"); // Required as of 16.2 (14.22.27905)
  919. compilerDef.ExtraFiles.push_back("$Root$/vcruntime140.dll");
  920. compilerDef.ExtraFiles.push_back(
  921. "$Root$/vcruntime140_1.dll"); // Required as of 16.5.1 (14.25.28610)
  922. compilerDef.ExtraFiles.push_back("$Root$/" + i18nNum + "/clui.dll");
  923. compilerDef.ExtraFiles.push_back(
  924. "$Root$/" + i18nNum + "/mspft140ui.dll"); // Localized messages for
  925. // static analysis
  926. }
  927. // Visual Studio 15 (19.10 to 19.19)
  928. else if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  929. compilerDef.CmakeCompilerVersion,
  930. "19.10")) {
  931. compilerDef.ExtraFiles.push_back("$Root$/c1.dll");
  932. compilerDef.ExtraFiles.push_back("$Root$/c1xx.dll");
  933. compilerDef.ExtraFiles.push_back("$Root$/c2.dll");
  934. compilerDef.ExtraFiles.push_back(
  935. "$Root$/atlprov.dll"); // Only needed if using ATL
  936. compilerDef.ExtraFiles.push_back("$Root$/msobj140.dll");
  937. compilerDef.ExtraFiles.push_back("$Root$/mspdb140.dll");
  938. compilerDef.ExtraFiles.push_back("$Root$/mspdbcore.dll");
  939. compilerDef.ExtraFiles.push_back("$Root$/mspdbsrv.exe");
  940. compilerDef.ExtraFiles.push_back("$Root$/mspft140.dll");
  941. compilerDef.ExtraFiles.push_back("$Root$/msvcp140.dll");
  942. compilerDef.ExtraFiles.push_back("$Root$/vcruntime140.dll");
  943. compilerDef.ExtraFiles.push_back("$Root$/" + i18nNum + "/clui.dll");
  944. }
  945. }
  946. // TODO: Handle Intel compiler
  947. this->Compilers[compilerDef.Name] = std::move(compilerDef);
  948. }
  949. void cmGlobalFastbuildGenerator::AddLauncher(std::string const& prefix,
  950. std::string const& launcher,
  951. std::string const& language,
  952. std::string const& args)
  953. {
  954. if (this->Compilers.find(prefix + language) != this->Compilers.end()) {
  955. return;
  956. }
  957. LogMessage("Launcher: " + launcher);
  958. LogMessage("Launcher args: " + args);
  959. FastbuildCompiler compilerDef;
  960. compilerDef.Name = prefix + language;
  961. compilerDef.Args = args;
  962. if (cmSystemTools::FileIsFullPath(launcher)) {
  963. compilerDef.Executable = launcher;
  964. } else {
  965. // FASTBuild needs an absolute path to the executable.
  966. compilerDef.Executable = cmSystemTools::FindProgram(launcher);
  967. if (compilerDef.Executable.empty()) {
  968. cmSystemTools::Error("Failed to find path to " + launcher);
  969. return;
  970. }
  971. }
  972. // When CTest is used as a launcher, there is an interesting env variable
  973. // "CTEST_LAUNCH_LOGS" which is set by parent CTest process and is expected
  974. // to be read from global (sic!) env by the launched CTest process. So we
  975. // will need to make this global env available for CTest executable used as a
  976. // "launcher". Tested in RunCMake.ctest_labels_for_subprojects test..
  977. compilerDef.DontUseEnv = true;
  978. this->Compilers[compilerDef.Name] = std::move(compilerDef);
  979. }
  980. std::string cmGlobalFastbuildGenerator::ConvertToFastbuildPath(
  981. std::string const& path) const
  982. {
  983. cmLocalGenerator const* root = LocalGenerators[0].get();
  984. return root->MaybeRelativeToWorkDir(cmSystemTools::FileIsFullPath(path)
  985. ? cmSystemTools::CollapseFullPath(path)
  986. : path);
  987. }
  988. std::unique_ptr<cmLinkLineComputer>
  989. cmGlobalFastbuildGenerator::CreateLinkLineComputer(
  990. cmOutputConverter* outputConverter,
  991. cmStateDirectory const& /* stateDir */) const
  992. {
  993. return cm::make_unique<cmFastbuildLinkLineComputer>(
  994. outputConverter,
  995. this->LocalGenerators[0]->GetStateSnapshot().GetDirectory(), this);
  996. }
  997. void cmGlobalFastbuildGenerator::WriteExec(FastbuildExecNode const& Exec,
  998. int indent)
  999. {
  1000. auto const identPlus1 = indent + 1;
  1001. WriteCommand("Exec", Exec.Name.empty() ? std::string{} : Quote(Exec.Name),
  1002. indent);
  1003. Indent(indent);
  1004. *BuildFileStream << "{\n";
  1005. {
  1006. if (!Exec.PreBuildDependencies.empty()) {
  1007. WriteArray("PreBuildDependencies", Wrap(Exec.PreBuildDependencies),
  1008. identPlus1);
  1009. }
  1010. WriteVariable("ExecExecutable", Quote(Exec.ExecExecutable), identPlus1);
  1011. if (!Exec.ExecArguments.empty()) {
  1012. WriteVariable("ExecArguments", Quote(Exec.ExecArguments), identPlus1);
  1013. }
  1014. if (!Exec.ExecWorkingDir.empty()) {
  1015. WriteVariable("ExecWorkingDir", Quote(Exec.ExecWorkingDir), identPlus1);
  1016. }
  1017. if (!Exec.ExecInput.empty()) {
  1018. WriteArray("ExecInput", Wrap(Exec.ExecInput), identPlus1);
  1019. }
  1020. if (Exec.ExecUseStdOutAsOutput) {
  1021. WriteVariable("ExecUseStdOutAsOutput", "true", identPlus1);
  1022. }
  1023. if (!Exec.ExecInputPath.empty()) {
  1024. WriteArray("ExecInputPath", Wrap(Exec.ExecInputPath), identPlus1);
  1025. }
  1026. if (!Exec.ExecInputPattern.empty()) {
  1027. WriteArray("ExecInputPattern", Wrap(Exec.ExecInputPattern), identPlus1);
  1028. }
  1029. WriteVariable("ExecAlwaysShowOutput", "true", identPlus1);
  1030. WriteVariable("ExecOutput", Quote(Exec.ExecOutput), identPlus1);
  1031. WriteVariable("ExecAlways", Exec.ExecAlways ? "true" : "false",
  1032. identPlus1);
  1033. if (!Exec.ConcurrencyGroupName.empty()) {
  1034. WriteVariable("ConcurrencyGroupName", Quote(Exec.ConcurrencyGroupName),
  1035. identPlus1);
  1036. }
  1037. }
  1038. Indent(indent);
  1039. *BuildFileStream << "}\n";
  1040. static bool const verbose = GlobalSettingIsOn(FASTBUILD_VERBOSE_GENERATOR) ||
  1041. cmSystemTools::HasEnv(FASTBUILD_VERBOSE_GENERATOR);
  1042. // Those aliases are only used for troubleshooting the generated file.
  1043. if (verbose) {
  1044. WriteAlias(Exec.OutputsAlias);
  1045. WriteAlias(Exec.ByproductsAlias);
  1046. }
  1047. }
  1048. void cmGlobalFastbuildGenerator::WriteUnity(FastbuildUnityNode const& Unity)
  1049. {
  1050. WriteCommand("Unity", Quote(Unity.Name), 1);
  1051. Indent(1);
  1052. *BuildFileStream << "{\n";
  1053. {
  1054. WriteVariable("UnityOutputPath", Quote(Unity.UnityOutputPath), 2);
  1055. WriteVariable("UnityOutputPattern", Quote(Unity.UnityOutputPattern), 2);
  1056. WriteArray("UnityInputFiles", Wrap(Unity.UnityInputFiles), 2);
  1057. if (!Unity.UnityInputIsolatedFiles.empty()) {
  1058. WriteArray("UnityInputIsolatedFiles",
  1059. Wrap(Unity.UnityInputIsolatedFiles), 2);
  1060. }
  1061. if (UsingRelativePaths) {
  1062. WriteVariable("UseRelativePaths_Experimental", "true", 2);
  1063. }
  1064. }
  1065. Indent(1);
  1066. *BuildFileStream << "}\n";
  1067. }
  1068. void cmGlobalFastbuildGenerator::WriteObjectList(
  1069. FastbuildObjectListNode const& ObjectList, bool allowDistribution)
  1070. {
  1071. WriteCommand("ObjectList", Quote(ObjectList.Name), 1);
  1072. Indent(1);
  1073. *BuildFileStream << "{\n";
  1074. {
  1075. if (!allowDistribution) {
  1076. WriteVariable("AllowDistribution", "false", 2);
  1077. }
  1078. if (!ObjectList.PreBuildDependencies.empty()) {
  1079. WriteArray("PreBuildDependencies", Wrap(ObjectList.PreBuildDependencies),
  1080. 2);
  1081. }
  1082. WriteVariable("Compiler", ObjectList.Compiler, 2);
  1083. // If only PCH output is present - this node reuses existing PCH.
  1084. if (!ObjectList.PCHOutputFile.empty()) {
  1085. WriteVariable("PCHOutputFile", Quote(ObjectList.PCHOutputFile), 2);
  1086. }
  1087. // If PCHInputFile and PCHOptions are present - this node creates PCH.
  1088. if (!ObjectList.PCHInputFile.empty() && !ObjectList.PCHOptions.empty()) {
  1089. WriteVariable("PCHInputFile", Quote(ObjectList.PCHInputFile), 2);
  1090. WriteVariable("PCHOptions", Quote(ObjectList.PCHOptions), 2);
  1091. }
  1092. WriteVariable("CompilerOptions", Quote(ObjectList.CompilerOptions), 2);
  1093. WriteVariable("CompilerOutputPath", Quote(ObjectList.CompilerOutputPath),
  1094. 2);
  1095. WriteVariable("CompilerOutputExtension",
  1096. Quote(ObjectList.CompilerOutputExtension), 2);
  1097. WriteVariable("CompilerOutputKeepBaseExtension", "true", 2);
  1098. if (!ObjectList.CompilerInputUnity.empty()) {
  1099. WriteArray("CompilerInputUnity", Wrap(ObjectList.CompilerInputUnity), 2);
  1100. }
  1101. if (!ObjectList.CompilerInputFiles.empty()) {
  1102. WriteArray("CompilerInputFiles", Wrap(ObjectList.CompilerInputFiles), 2);
  1103. }
  1104. if (!ObjectList.AllowCaching) {
  1105. WriteVariable("AllowCaching", "false", 2);
  1106. }
  1107. if (!ObjectList.AllowDistribution) {
  1108. WriteVariable("AllowDistribution", "false", 2);
  1109. }
  1110. if (ObjectList.Hidden) {
  1111. WriteVariable("Hidden", "true", 2);
  1112. }
  1113. }
  1114. Indent(1);
  1115. *BuildFileStream << "}\n";
  1116. }
  1117. void cmGlobalFastbuildGenerator::WriteLinker(
  1118. FastbuildLinkerNode const& LinkerNode, bool allowDistribution)
  1119. {
  1120. WriteCommand(
  1121. LinkerNode.Type == FastbuildLinkerNode::EXECUTABLE ? "Executable"
  1122. : LinkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY ? "DLL"
  1123. : "Library",
  1124. (!LinkerNode.Name.empty() && LinkerNode.Name != LinkerNode.LinkerOutput)
  1125. ? Quote(LinkerNode.Name)
  1126. : "",
  1127. 1);
  1128. Indent(1);
  1129. *BuildFileStream << "{\n";
  1130. {
  1131. if (!LinkerNode.PreBuildDependencies.empty()) {
  1132. WriteArray("PreBuildDependencies", Wrap(LinkerNode.PreBuildDependencies),
  1133. 2);
  1134. }
  1135. if (!allowDistribution) {
  1136. WriteVariable("AllowDistribution", "false", 2);
  1137. }
  1138. if (!LinkerNode.Compiler.empty() &&
  1139. LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY) {
  1140. WriteVariable("Compiler", LinkerNode.Compiler, 2);
  1141. WriteVariable("CompilerOptions", Quote(LinkerNode.CompilerOptions), 2);
  1142. WriteVariable("CompilerOutputPath", Quote("."), 2);
  1143. }
  1144. if (!LocalEnvironment.empty()) {
  1145. WriteVariable("Environment", "." FASTBUILD_ENV_VAR_NAME, 2);
  1146. }
  1147. WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
  1148. ? "Librarian"
  1149. : "Linker",
  1150. Quote(LinkerNode.Linker), 2);
  1151. WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
  1152. ? "LibrarianOptions"
  1153. : "LinkerOptions",
  1154. Quote(LinkerNode.LinkerOptions), 2);
  1155. WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
  1156. ? "LibrarianOutput"
  1157. : "LinkerOutput",
  1158. Quote(LinkerNode.LinkerOutput), 2);
  1159. if (!LinkerNode.LibrarianAdditionalInputs.empty()) {
  1160. WriteArray(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
  1161. ? "LibrarianAdditionalInputs"
  1162. : "Libraries",
  1163. Wrap(LinkerNode.LibrarianAdditionalInputs), 2);
  1164. }
  1165. if (!LinkerNode.Libraries2.empty()) {
  1166. WriteArray("Libraries2", Wrap(LinkerNode.Libraries2), 2);
  1167. }
  1168. if (!LinkerNode.LibrarianAdditionalInputs.empty()) {
  1169. if (!LinkerNode.LinkerType.empty()) {
  1170. WriteVariable("LinkerType", Quote(LinkerNode.LinkerType), 2);
  1171. }
  1172. }
  1173. if (LinkerNode.Type == FastbuildLinkerNode::EXECUTABLE ||
  1174. LinkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY) {
  1175. WriteVariable("LinkerLinkObjects",
  1176. LinkerNode.LinkerLinkObjects ? "true" : "false", 2);
  1177. if (!LinkerNode.LinkerStampExe.empty()) {
  1178. WriteVariable("LinkerStampExe", Quote(LinkerNode.LinkerStampExe), 2);
  1179. if (!LinkerNode.LinkerStampExeArgs.empty()) {
  1180. WriteVariable("LinkerStampExeArgs",
  1181. Quote(LinkerNode.LinkerStampExeArgs), 2);
  1182. }
  1183. }
  1184. }
  1185. Indent(1);
  1186. *BuildFileStream << "}\n";
  1187. }
  1188. }
  1189. void cmGlobalFastbuildGenerator::WriteAlias(FastbuildAliasNode const& Alias,
  1190. int indent)
  1191. {
  1192. if (Alias.PreBuildDependencies.empty()) {
  1193. return;
  1194. }
  1195. auto const identPlus1 = indent + 1;
  1196. WriteCommand("Alias", Quote(Alias.Name), indent);
  1197. Indent(indent);
  1198. *BuildFileStream << "{\n";
  1199. WriteArray("Targets", Wrap(Alias.PreBuildDependencies), identPlus1);
  1200. if (Alias.Hidden) {
  1201. WriteVariable("Hidden", "true", identPlus1);
  1202. }
  1203. Indent(indent);
  1204. *BuildFileStream << "}\n";
  1205. }
  1206. void cmGlobalFastbuildGenerator::WriteCopy(FastbuildCopyNode const& Copy)
  1207. {
  1208. cmGlobalFastbuildGenerator::WriteCommand(
  1209. Copy.CopyDir ? "CopyDir" : "Copy",
  1210. cmGlobalFastbuildGenerator::Quote(Copy.Name), 1);
  1211. cmGlobalFastbuildGenerator::Indent(1);
  1212. *BuildFileStream << "{\n";
  1213. WriteVariable("PreBuildDependencies",
  1214. cmGlobalFastbuildGenerator::Quote(Copy.PreBuildDependencies),
  1215. 2);
  1216. WriteVariable(Copy.CopyDir ? "SourcePaths" : "Source",
  1217. cmGlobalFastbuildGenerator::Quote(Copy.Source), 2);
  1218. WriteVariable("Dest", cmGlobalFastbuildGenerator::Quote(Copy.Dest), 2);
  1219. cmGlobalFastbuildGenerator::Indent(1);
  1220. *BuildFileStream << "}\n";
  1221. }
  1222. void cmGlobalFastbuildGenerator::WriteTarget(FastbuildTarget const& target)
  1223. {
  1224. for (auto const& val : target.Variables) {
  1225. auto const& key = val.first;
  1226. auto const& value = val.second;
  1227. WriteVariable(key, cmGlobalFastbuildGenerator::Quote(value), 1);
  1228. }
  1229. // add_custom_commands(...)
  1230. for (auto const& alias : { target.ExecNodes }) {
  1231. this->WriteAlias(alias);
  1232. }
  1233. // -deps Alias.
  1234. this->WriteAlias(target.DependenciesAlias);
  1235. // PRE_BUILD.
  1236. for (auto const& alias : { target.PreBuildExecNodes }) {
  1237. this->WriteAlias(alias);
  1238. }
  1239. // Copy commands.
  1240. for (FastbuildCopyNode const& node : target.CopyNodes) {
  1241. this->WriteCopy(node);
  1242. }
  1243. // Unity.
  1244. for (FastbuildUnityNode const& unity : target.UnityNodes) {
  1245. this->WriteUnity(unity);
  1246. }
  1247. // Objects.
  1248. for (FastbuildObjectListNode const& objectList : target.ObjectListNodes) {
  1249. this->WriteObjectList(objectList, target.AllowDistribution);
  1250. }
  1251. if (!target.PreLinkExecNodes.Nodes.empty()) {
  1252. for (auto const& exec : target.PreLinkExecNodes.Nodes) {
  1253. this->WriteExec(exec);
  1254. }
  1255. this->WriteAlias(target.PreLinkExecNodes.Alias);
  1256. }
  1257. // Libraries / executables.
  1258. if (!target.LinkerNode.empty()) {
  1259. for (auto const& linkerNode : target.LinkerNode) {
  1260. this->WriteLinker(linkerNode, target.AllowDistribution);
  1261. }
  1262. }
  1263. if (!target.PostBuildExecNodes.Nodes.empty()) {
  1264. for (auto const& exec : target.PostBuildExecNodes.Nodes) {
  1265. this->WriteExec(exec);
  1266. }
  1267. this->WriteAlias(target.PostBuildExecNodes.Alias);
  1268. }
  1269. // Aliases (if any).
  1270. for (FastbuildAliasNode const& alias : target.AliasNodes) {
  1271. this->WriteAlias(alias);
  1272. }
  1273. }
  1274. void cmGlobalFastbuildGenerator::WriteIDEProjects()
  1275. {
  1276. for (auto const& proj : IDEProjects) {
  1277. (void)proj;
  1278. // VS
  1279. #if defined(_WIN32)
  1280. auto const& VSProj = proj.second.first;
  1281. WriteCommand("VCXProject", Quote(VSProj.Alias));
  1282. *this->BuildFileStream << "{\n";
  1283. WriteVariable("ProjectOutput", Quote(VSProj.ProjectOutput), 1);
  1284. WriteIDEProjectConfig(VSProj.ProjectConfigs);
  1285. WriteVSBuildCommands();
  1286. WriteIDEProjectCommon(VSProj);
  1287. *this->BuildFileStream << "}\n\n";
  1288. // XCode
  1289. #elif defined(__APPLE__)
  1290. auto const& XCodeProj = proj.second.second;
  1291. WriteCommand("XCodeProject", Quote(XCodeProj.Alias), 0);
  1292. *this->BuildFileStream << "{\n";
  1293. WriteVariable("ProjectOutput", Quote(XCodeProj.ProjectOutput), 1);
  1294. WriteIDEProjectConfig(XCodeProj.ProjectConfigs);
  1295. WriteXCodeBuildCommands();
  1296. WriteIDEProjectCommon(XCodeProj);
  1297. *this->BuildFileStream << "}\n\n";
  1298. #endif
  1299. }
  1300. #if defined(_WIN32)
  1301. this->WriteSolution();
  1302. #elif defined(__APPLE__)
  1303. this->WriteXCodeTopLevelProject();
  1304. #endif
  1305. }
  1306. std::string cmGlobalFastbuildGenerator::GetIDEBuildArgs() const
  1307. {
  1308. cmValue const ideArgs = this->GetGlobalSetting(FASTBUILD_IDE_ARGS);
  1309. if (ideArgs) {
  1310. return cmStrCat(' ', ideArgs, ' ');
  1311. }
  1312. return FASTBUILD_DEFAULT_IDE_BUILD_ARGS;
  1313. }
  1314. void cmGlobalFastbuildGenerator::WriteVSBuildCommands()
  1315. {
  1316. std::string const ideArgs = this->GetIDEBuildArgs();
  1317. WriteVariable(
  1318. "ProjectBuildCommand",
  1319. Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX, this->FastbuildCommand,
  1320. ideArgs, " ^$(ProjectName)")),
  1321. 1);
  1322. WriteVariable(
  1323. "ProjectRebuildCommand",
  1324. Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX, this->FastbuildCommand,
  1325. ideArgs, "-clean ^$(ProjectName)")),
  1326. 1);
  1327. WriteVariable("ProjectCleanCommand",
  1328. Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX,
  1329. this->FastbuildCommand, ideArgs, " clean")),
  1330. 1);
  1331. }
  1332. void cmGlobalFastbuildGenerator::WriteXCodeBuildCommands()
  1333. {
  1334. std::string const ideArgs = this->GetIDEBuildArgs();
  1335. WriteVariable("XCodeBuildToolPath", Quote(this->FastbuildCommand), 1);
  1336. WriteVariable("XCodeBuildToolArgs",
  1337. Quote(cmStrCat(ideArgs, "^$(FASTBUILD_TARGET)")), 1);
  1338. WriteVariable("XCodeBuildWorkingDir",
  1339. Quote(this->CMakeInstance->GetHomeOutputDirectory()), 1);
  1340. }
  1341. void cmGlobalFastbuildGenerator::WriteIDEProjectCommon(
  1342. IDEProjectCommon const& project)
  1343. {
  1344. WriteVariable("ProjectBasePath", Quote(project.ProjectBasePath), 1);
  1345. // So Fastbuild will pick up files relative to CMakeLists.txt
  1346. WriteVariable("ProjectInputPaths", Quote(project.ProjectBasePath), 1);
  1347. }
  1348. void cmGlobalFastbuildGenerator::WriteIDEProjectConfig(
  1349. std::vector<IDEProjectConfig> const& configs, std::string const& keyName)
  1350. {
  1351. std::vector<std::string> allConfigVariables;
  1352. for (auto const& config : configs) {
  1353. std::string configName = "Config" + config.Config;
  1354. WriteVariable(configName, "", 1);
  1355. Indent(1);
  1356. *this->BuildFileStream << "[\n";
  1357. WriteVariable("Config", Quote(config.Config), 2);
  1358. if (!config.Target.empty()) {
  1359. WriteVariable("Target", Quote(config.Target), 2);
  1360. }
  1361. if (!config.Platform.empty()) {
  1362. WriteVariable("Platform", Quote(config.Platform), 2);
  1363. }
  1364. Indent(1);
  1365. *this->BuildFileStream << "]\n";
  1366. allConfigVariables.emplace_back(std::move(configName));
  1367. }
  1368. WriteArray(keyName, Wrap(allConfigVariables, ".", ""), 1);
  1369. }
  1370. void cmGlobalFastbuildGenerator::AddTargetAll()
  1371. {
  1372. FastbuildAliasNode allAliasNode;
  1373. allAliasNode.Name = FASTBUILD_ALL_TARGET_NAME;
  1374. for (auto const& targetBase : FastbuildTargets) {
  1375. if (targetBase->Type == FastbuildTargetType::LINK) {
  1376. auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
  1377. // Add non-global and non-excluded targets to "all"
  1378. if (!target.IsGlobal && !target.ExcludeFromAll) {
  1379. allAliasNode.PreBuildDependencies.emplace(target.Name);
  1380. }
  1381. } else if (targetBase->Type == FastbuildTargetType::ALIAS) {
  1382. auto const& target = static_cast<FastbuildAliasNode const&>(*targetBase);
  1383. if (!target.ExcludeFromAll) {
  1384. allAliasNode.PreBuildDependencies.emplace(target.Name);
  1385. }
  1386. }
  1387. }
  1388. if (allAliasNode.PreBuildDependencies.empty()) {
  1389. allAliasNode.PreBuildDependencies.emplace(FASTBUILD_NOOP_FILE_NAME);
  1390. }
  1391. this->AddTarget(std::move(allAliasNode));
  1392. }
  1393. void cmGlobalFastbuildGenerator::AddGlobCheckExec()
  1394. {
  1395. // Tested in "RunCMake.file" test.
  1396. std::string const globScript =
  1397. this->GetCMakeInstance()->GetGlobVerifyScript();
  1398. if (!globScript.empty()) {
  1399. FastbuildExecNode globCheck;
  1400. globCheck.Name = FASTBUILD_GLOB_CHECK_TARGET;
  1401. globCheck.ExecExecutable = cmSystemTools::GetCMakeCommand();
  1402. globCheck.ExecArguments =
  1403. cmStrCat("-P ", this->ConvertToFastbuildPath(globScript));
  1404. globCheck.ExecAlways = false;
  1405. globCheck.ExecUseStdOutAsOutput = false;
  1406. auto const cache = this->GetCMakeInstance()->GetGlobCacheEntries();
  1407. for (auto const& entry : cache) {
  1408. auto path = cmSystemTools::GetFilenamePath(entry.Expression);
  1409. auto expression = cmSystemTools::GetFilenameName(entry.Expression);
  1410. if (std::find(globCheck.ExecInputPath.begin(),
  1411. globCheck.ExecInputPath.end(),
  1412. path) == globCheck.ExecInputPath.end()) {
  1413. globCheck.ExecInputPath.emplace_back(std::move(path));
  1414. }
  1415. if (std::find(globCheck.ExecInputPattern.begin(),
  1416. globCheck.ExecInputPattern.end(),
  1417. expression) == globCheck.ExecInputPattern.end()) {
  1418. globCheck.ExecInputPattern.emplace_back(std::move(expression));
  1419. }
  1420. }
  1421. globCheck.ExecOutput = this->ConvertToFastbuildPath(
  1422. this->GetCMakeInstance()->GetGlobVerifyStamp());
  1423. this->AddTarget(std::move(globCheck));
  1424. }
  1425. }
  1426. void cmGlobalFastbuildGenerator::WriteSolution()
  1427. {
  1428. std::string const solutionName = LocalGenerators[0]->GetProjectName();
  1429. std::map<std::string /*folder*/, std::vector<std::string>> VSProjects;
  1430. std::vector<std::string> VSProjectsWithoutFolder;
  1431. for (auto const& IDEProj : IDEProjects) {
  1432. auto const VSProj = IDEProj.second.first;
  1433. VSProjects[VSProj.folder].emplace_back(VSProj.Alias);
  1434. }
  1435. WriteCommand("VSSolution", Quote("solution"));
  1436. *this->BuildFileStream << "{\n";
  1437. WriteVariable("SolutionOutput",
  1438. Quote(cmJoin({ "VisualStudio", solutionName + ".sln" }, "/")),
  1439. 1);
  1440. auto const& configs = IDEProjects.begin()->second.first.ProjectConfigs;
  1441. WriteIDEProjectConfig(configs, "SolutionConfigs");
  1442. int folderNumber = 0;
  1443. std::vector<std::string> folders;
  1444. for (auto& item : VSProjects) {
  1445. auto const& pathToFolder = item.first;
  1446. auto& projectsInFolder = item.second;
  1447. if (pathToFolder.empty()) {
  1448. std::move(projectsInFolder.begin(), projectsInFolder.end(),
  1449. std::back_inserter(VSProjectsWithoutFolder));
  1450. } else {
  1451. std::string folderName = cmStrCat("Folder_", ++folderNumber);
  1452. WriteStruct(
  1453. folderName,
  1454. { { "Path", Quote(pathToFolder) },
  1455. { "Projects",
  1456. cmStrCat("{", cmJoin(Wrap(projectsInFolder), ","), "}") } },
  1457. 1);
  1458. folders.emplace_back(std::move(folderName));
  1459. }
  1460. }
  1461. if (!folders.empty()) {
  1462. WriteArray("SolutionFolders ", Wrap(folders, ".", ""), 1);
  1463. }
  1464. if (!VSProjectsWithoutFolder.empty()) {
  1465. WriteArray("SolutionProjects", Wrap(VSProjectsWithoutFolder), 1);
  1466. }
  1467. *this->BuildFileStream << "}\n";
  1468. }
  1469. void cmGlobalFastbuildGenerator::WriteXCodeTopLevelProject()
  1470. {
  1471. std::string const projectName = LocalGenerators[0]->GetProjectName();
  1472. std::vector<std::string> XCodeProjects;
  1473. for (auto const& IDEProj : IDEProjects) {
  1474. auto const XCodeProj = IDEProj.second.second;
  1475. XCodeProjects.emplace_back(XCodeProj.Alias);
  1476. }
  1477. WriteCommand("XCodeProject", Quote("xcode"));
  1478. *this->BuildFileStream << "{\n";
  1479. WriteVariable(
  1480. "ProjectOutput",
  1481. Quote(
  1482. cmJoin({ "XCode", projectName + ".xcodeproj", "project.pbxproj" }, "/")),
  1483. 1);
  1484. WriteVariable("ProjectBasePath", Quote(FASTBUILD_XCODE_BASE_PATH), 1);
  1485. auto const& configs = IDEProjects.begin()->second.second.ProjectConfigs;
  1486. WriteIDEProjectConfig(configs);
  1487. WriteArray("ProjectFiles", Wrap(XCodeProjects), 1);
  1488. *this->BuildFileStream << "}\n";
  1489. }
  1490. void cmGlobalFastbuildGenerator::LogMessage(std::string const& m) const
  1491. {
  1492. static bool const verbose = GlobalSettingIsOn(FASTBUILD_VERBOSE_GENERATOR) ||
  1493. cmSystemTools::HasEnv(FASTBUILD_VERBOSE_GENERATOR);
  1494. if (verbose) {
  1495. cmSystemTools::Message(m);
  1496. }
  1497. }
  1498. void cmGlobalFastbuildGenerator::AddFileToClean(std::string const& file)
  1499. {
  1500. AllFilesToClean.insert(file);
  1501. }
  1502. std::string cmGlobalFastbuildGenerator::GetExternalShellExecutable()
  1503. {
  1504. // FindProgram is expensive - touches filesystem and makes syscalls, so cache
  1505. // it.
  1506. static std::string const cached =
  1507. #ifdef _WIN32
  1508. cmSystemTools::FindProgram(
  1509. "cmd.exe", std::vector<std::string>{ "C:\\Windows\\System32" });
  1510. #else
  1511. cmSystemTools::FindProgram("sh", std::vector<std::string>{ "/bin" });
  1512. #endif
  1513. return cached;
  1514. }
  1515. void cmGlobalFastbuildGenerator::WriteTargetRebuildBFF()
  1516. {
  1517. std::vector<std::string> implicitDeps;
  1518. for (auto& lg : LocalGenerators) {
  1519. std::vector<std::string> const& lf = lg->GetMakefile()->GetListFiles();
  1520. for (auto const& dep : lf) {
  1521. implicitDeps.push_back(this->ConvertToFastbuildPath(dep));
  1522. }
  1523. }
  1524. auto const* cmake = this->GetCMakeInstance();
  1525. std::string outDir = cmake->GetHomeOutputDirectory() + '/';
  1526. implicitDeps.push_back(outDir + "CMakeCache.txt");
  1527. FastbuildExecNode rebuildBFF;
  1528. rebuildBFF.Name = FASTBUILD_REBUILD_BFF_TARGET_NAME;
  1529. if (!this->GetCMakeInstance()->GetGlobVerifyScript().empty()) {
  1530. implicitDeps.emplace_back(this->GetCMakeInstance()->GetGlobVerifyStamp());
  1531. }
  1532. std::sort(implicitDeps.begin(), implicitDeps.end());
  1533. implicitDeps.erase(std::unique(implicitDeps.begin(), implicitDeps.end()),
  1534. implicitDeps.end());
  1535. std::string args =
  1536. cmStrCat("--regenerate-during-build",
  1537. (this->GetCMakeInstance()->GetIgnoreCompileWarningAsError()
  1538. ? " --compile-no-warning-as-error"
  1539. : ""),
  1540. (this->GetCMakeInstance()->GetIgnoreLinkWarningAsError()
  1541. ? " --link-no-warning-as-error"
  1542. : ""),
  1543. " -S", QuoteIfHasSpaces(cmake->GetHomeDirectory()), " -B",
  1544. QuoteIfHasSpaces(cmake->GetHomeOutputDirectory()));
  1545. rebuildBFF.ExecArguments = std::move(args);
  1546. rebuildBFF.ExecInput = implicitDeps;
  1547. rebuildBFF.ExecExecutable = cmSystemTools::GetCMakeCommand();
  1548. rebuildBFF.ExecWorkingDir = outDir;
  1549. rebuildBFF.ExecOutput = outDir + FASTBUILD_BUILD_FILE;
  1550. this->WriteExec(rebuildBFF, 0);
  1551. }
  1552. void cmGlobalFastbuildGenerator::WriteCleanScript()
  1553. {
  1554. std::string const path =
  1555. cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), '/',
  1556. FASTBUILD_CLEAN_SCRIPT_NAME);
  1557. cmsys::ofstream scriptFile(path.c_str(), std::ios::out | std::ios::binary);
  1558. if (!scriptFile.is_open()) {
  1559. cmSystemTools::Error("Failed to open: " FASTBUILD_CLEAN_SCRIPT_NAME);
  1560. return;
  1561. }
  1562. for (std::string const& file : AllFilesToClean) {
  1563. #if defined(_WIN32)
  1564. scriptFile << "del /f /q "
  1565. << cmSystemTools::ConvertToWindowsOutputPath(file) << "\n";
  1566. #else
  1567. scriptFile << "rm -f " << file << '\n';
  1568. #endif
  1569. }
  1570. }
  1571. void cmGlobalFastbuildGenerator::WriteTargetClean()
  1572. {
  1573. if (AllFilesToClean.empty()) {
  1574. FastbuildAliasNode clean;
  1575. clean.Name = FASTBUILD_CLEAN_TARGET_NAME;
  1576. clean.PreBuildDependencies.emplace(FASTBUILD_CLEAN_FILE_NAME);
  1577. WriteAlias(clean, 0);
  1578. return;
  1579. }
  1580. WriteCleanScript();
  1581. FastbuildExecNode clean;
  1582. clean.Name = FASTBUILD_CLEAN_TARGET_NAME;
  1583. clean.ExecExecutable = GetExternalShellExecutable();
  1584. clean.ExecArguments =
  1585. FASTBUILD_SCRIPT_FILE_ARG FASTBUILD_1_INPUT_PLACEHOLDER;
  1586. clean.ExecInput = { FASTBUILD_CLEAN_SCRIPT_NAME };
  1587. clean.ExecAlways = true;
  1588. clean.ExecUseStdOutAsOutput = true;
  1589. clean.ExecOutput = FASTBUILD_CLEAN_FILE_NAME;
  1590. clean.ExecWorkingDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  1591. WriteExec(clean, 0);
  1592. }
  1593. void cmGlobalFastbuildGenerator::WriteTargets()
  1594. {
  1595. std::string const outputDir = this->CMakeInstance->GetHomeOutputDirectory();
  1596. LogMessage("GetHomeOutputDirectory: " + outputDir);
  1597. // Noop file that 'all' can alias to if we don't have any other targets...
  1598. // The exact location of the "noop" file is verified in one of the tests in
  1599. // "RunCMake.CMakePresetsPackage" test suite.
  1600. cmSystemTools::Touch(cmStrCat(this->CMakeInstance->GetHomeOutputDirectory(),
  1601. '/', FASTBUILD_NOOP_FILE_NAME),
  1602. true);
  1603. cmSystemTools::Touch(cmStrCat(this->CMakeInstance->GetHomeOutputDirectory(),
  1604. '/', FASTBUILD_CLEAN_FILE_NAME),
  1605. true);
  1606. // Add "all" utility target before sorting, so we can correctly sort
  1607. // targets that depend on it
  1608. AddTargetAll();
  1609. TopologicalSort(FastbuildTargets);
  1610. AddGlobCheckExec();
  1611. for (auto const& targetBase : FastbuildTargets) {
  1612. this->WriteComment("Target definition: " + targetBase->Name);
  1613. // Target start.
  1614. *BuildFileStream << "{\n";
  1615. if (targetBase->Type == FastbuildTargetType::EXEC) {
  1616. this->WriteExec(static_cast<FastbuildExecNode const&>(*targetBase));
  1617. } else if (targetBase->Type == FastbuildTargetType::ALIAS) {
  1618. this->WriteAlias(static_cast<FastbuildAliasNode const&>(*targetBase));
  1619. } else if (targetBase->Type == FastbuildTargetType::LINK) {
  1620. auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
  1621. this->WriteTarget(target);
  1622. }
  1623. // Target end.
  1624. *BuildFileStream << "}\n";
  1625. }
  1626. if (!this->GetCMakeInstance()->GetIsInTryCompile()) {
  1627. if (!IDEProjects.empty()) {
  1628. this->WriteIDEProjects();
  1629. }
  1630. }
  1631. this->WriteTargetClean();
  1632. this->WriteTargetRebuildBFF();
  1633. }
  1634. std::string cmGlobalFastbuildGenerator::GetTargetName(
  1635. cmGeneratorTarget const* GeneratorTarget) const
  1636. {
  1637. std::string targetName =
  1638. GeneratorTarget->GetLocalGenerator()->GetCurrentBinaryDirectory();
  1639. targetName += "/";
  1640. targetName += GeneratorTarget->GetName();
  1641. targetName = this->ConvertToFastbuildPath(targetName);
  1642. return targetName;
  1643. }
  1644. cm::optional<FastbuildTarget>
  1645. cmGlobalFastbuildGenerator::GetTargetByOutputName(
  1646. std::string const& output) const
  1647. {
  1648. for (auto const& targetBase : FastbuildTargets) {
  1649. if (targetBase->Type == FastbuildTargetType::LINK) {
  1650. auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
  1651. if (std::any_of(target.LinkerNode.begin(), target.LinkerNode.end(),
  1652. [&output](FastbuildLinkerNode const& target_) {
  1653. return target_.LinkerOutput == output;
  1654. })) {
  1655. return target;
  1656. }
  1657. }
  1658. }
  1659. return cm::nullopt;
  1660. }
  1661. void cmGlobalFastbuildGenerator::AddIDEProject(FastbuildTarget const& target,
  1662. std::string const& config)
  1663. {
  1664. auto const& configs = GetConfigNames();
  1665. if (std::find(configs.begin(), configs.end(), config) == configs.end()) {
  1666. LogMessage("Config " + config + " doesn't exist, IDE projest for " +
  1667. target.Name + " won't be generated");
  1668. return;
  1669. }
  1670. auto& IDEProject = IDEProjects[target.BaseName];
  1671. auto const relativeSubdir = cmSystemTools::RelativePath(
  1672. this->GetCMakeInstance()->GetHomeDirectory(), target.BasePath);
  1673. // VS
  1674. auto& VSProject = IDEProject.first;
  1675. VSProject.Alias = target.BaseName + "-vcxproj";
  1676. VSProject.ProjectOutput = cmStrCat("VisualStudio/Projects/", relativeSubdir,
  1677. '/', target.BaseName + ".vcxproj");
  1678. VSProject.ProjectBasePath = target.BasePath;
  1679. VSProject.folder = relativeSubdir;
  1680. // XCode
  1681. auto& XCodeProject = IDEProject.second;
  1682. XCodeProject.Alias = target.BaseName + "-xcodeproj";
  1683. XCodeProject.ProjectOutput =
  1684. cmStrCat("XCode/Projects/", relativeSubdir, '/',
  1685. target.BaseName + ".xcodeproj/project.pbxproj");
  1686. XCodeProject.ProjectBasePath = target.BasePath;
  1687. IDEProjectConfig VSConfig;
  1688. VSConfig.Platform = "X64";
  1689. IDEProjectConfig XCodeConfig;
  1690. VSConfig.Target = XCodeConfig.Target = target.Name;
  1691. VSConfig.Config = XCodeConfig.Config = config.empty() ? "DEFAULT" : config;
  1692. VSProject.ProjectConfigs.emplace_back(std::move(VSConfig));
  1693. XCodeProject.ProjectConfigs.emplace_back(std::move(XCodeConfig));
  1694. }
  1695. bool cmGlobalFastbuildGenerator::IsExcluded(cmGeneratorTarget* target)
  1696. {
  1697. return cmGlobalGenerator::IsExcluded(LocalGenerators[0].get(), target);
  1698. }
  1699. std::vector<std::string> const& cmGlobalFastbuildGenerator::GetConfigNames()
  1700. const
  1701. {
  1702. return static_cast<cmLocalFastbuildGenerator const*>(
  1703. this->LocalGenerators.front().get())
  1704. ->GetConfigNames();
  1705. }
  1706. bool cmGlobalFastbuildGenerator::Open(std::string const& bindir,
  1707. std::string const& projectName,
  1708. bool dryRun)
  1709. {
  1710. #ifdef _WIN32
  1711. std::string sln = bindir + "/VisualStudio/" + projectName + ".sln";
  1712. if (dryRun) {
  1713. return cmSystemTools::FileExists(sln, true);
  1714. }
  1715. sln = cmSystemTools::ConvertToOutputPath(sln);
  1716. auto OpenSolution = [](std::string pathToSolution) {
  1717. HRESULT comInitialized =
  1718. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  1719. if (FAILED(comInitialized)) {
  1720. return false;
  1721. }
  1722. HINSTANCE hi = ShellExecuteA(NULL, "open", pathToSolution.c_str(), NULL,
  1723. NULL, SW_SHOWNORMAL);
  1724. CoUninitialize();
  1725. return reinterpret_cast<intptr_t>(hi) > 32;
  1726. };
  1727. return std::async(std::launch::async, OpenSolution, sln).get();
  1728. #else
  1729. return cmGlobalCommonGenerator::Open(bindir, projectName, dryRun);
  1730. #endif
  1731. }