cmGlobalFastbuildGenerator.cxx 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  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. std::string const workingDir =
  424. this->GetCMakeInstance()->GetHomeOutputDirectory();
  425. // Make "rebuild-bff" target up-to-date after the generation.
  426. // This is actually a noop, it just asks CMake to touch the generated file
  427. // so FASTBuild would consider the target as up-to-date.
  428. AskCMakeToMakeRebuildBFFUpToDate(workingDir);
  429. if (this->GlobalSettingIsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
  430. std::string output;
  431. ExecuteFastbuildTarget(workingDir, FASTBUILD_ALL_TARGET_NAME, output,
  432. { "-compdb" });
  433. }
  434. }
  435. void cmGlobalFastbuildGenerator::AskCMakeToMakeRebuildBFFUpToDate(
  436. std::string const& workingDir) const
  437. {
  438. // "restat" the generated build file.
  439. // The idea here is to mimic what Ninja's "restat" command does.
  440. // We need to make the "rebuild.bff" target up-to-date, so the regeneration
  441. // will only be triggered when CMake files have actually changed.
  442. // Tested in "RunCMake.Configure" test.
  443. cmsys::ofstream{
  444. cmStrCat(workingDir, '/', FASTBUILD_RESTAT_FILE).c_str(),
  445. std::ios::out | std::ios::binary
  446. } << cmStrCat(workingDir, '/', FASTBUILD_BUILD_FILE);
  447. std::string output;
  448. ExecuteFastbuildTarget(workingDir, FASTBUILD_REBUILD_BFF_TARGET_NAME,
  449. output);
  450. }
  451. void cmGlobalFastbuildGenerator::ExecuteFastbuildTarget(
  452. std::string const& dir, std::string const& target, std::string& output,
  453. std::vector<std::string> const& fbuildOptions) const
  454. {
  455. std::vector<std::string> command;
  456. command.emplace_back(this->FastbuildCommand);
  457. command.emplace_back("-config");
  458. std::string const file = cmStrCat(dir, '/', FASTBUILD_BUILD_FILE);
  459. command.emplace_back(file);
  460. command.emplace_back(target);
  461. if (!fbuildOptions.empty()) {
  462. command.emplace_back(cmJoin(fbuildOptions, " "));
  463. }
  464. int retVal = 0;
  465. if (!cmSystemTools::RunSingleCommand(command, &output, nullptr, &retVal,
  466. dir.c_str(),
  467. cmSystemTools::OUTPUT_NONE) ||
  468. retVal != 0) {
  469. cmSystemTools::Error(cmStrCat("Failed to run FASTBuild command:\n '",
  470. cmJoin(command, "' '"), "'\nOutput:\n",
  471. output));
  472. cmSystemTools::Stdout(output);
  473. std::exit(retVal);
  474. }
  475. }
  476. void cmGlobalFastbuildGenerator::WriteSettings()
  477. {
  478. // Define some placeholder
  479. WriteDivider();
  480. *this->BuildFileStream << "// Helper variables\n\n";
  481. WriteVariable("FB_INPUT_1_PLACEHOLDER", Quote("\"%1\""));
  482. WriteVariable("FB_INPUT_1_0_PLACEHOLDER", Quote("\"%1[0]\""));
  483. WriteVariable("FB_INPUT_1_1_PLACEHOLDER", Quote("\"%1[1]\""));
  484. WriteVariable("FB_INPUT_2_PLACEHOLDER", Quote("\"%2\""));
  485. WriteVariable("FB_INPUT_3_PLACEHOLDER", Quote("\"%3\""));
  486. std::string cacheDir;
  487. // If explicitly set from CMake.
  488. auto val = this->GetSafeGlobalSetting(FASTBUILD_CACHE_PATH);
  489. if (!val.empty()) {
  490. cacheDir = std::move(val);
  491. cmSystemTools::ConvertToOutputSlashes(cacheDir);
  492. }
  493. WriteDivider();
  494. *this->BuildFileStream << "// Settings\n\n";
  495. WriteCommand("Settings");
  496. *this->BuildFileStream << "{\n";
  497. if (!cacheDir.empty()) {
  498. WriteVariable("CachePath", Quote(cacheDir), 1);
  499. }
  500. // Concurrency groups.
  501. WriteStruct(
  502. FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME,
  503. { { "ConcurrencyGroupName", Quote(FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME) },
  504. { "ConcurrencyLimit", "1" } },
  505. 1);
  506. WriteArray("ConcurrencyGroups",
  507. { "." FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME }, 1);
  508. *this->BuildFileStream << "}\n";
  509. }
  510. void cmGlobalFastbuildGenerator::WriteEnvironment()
  511. {
  512. if (!LocalEnvironment.empty()) {
  513. WriteArray(FASTBUILD_ENV_VAR_NAME, Wrap(LocalEnvironment), 0);
  514. }
  515. }
  516. void cmGlobalFastbuildGenerator::WriteDivider()
  517. {
  518. *this->BuildFileStream << "// ======================================"
  519. "=======================================\n";
  520. }
  521. void cmGlobalFastbuildGenerator::Indent(int count)
  522. {
  523. for (int i = 0; i < count; ++i) {
  524. *this->BuildFileStream << " ";
  525. }
  526. }
  527. void cmGlobalFastbuildGenerator::WriteComment(std::string const& comment,
  528. int indent)
  529. {
  530. if (comment.empty()) {
  531. return;
  532. }
  533. std::string::size_type lpos = 0;
  534. std::string::size_type rpos;
  535. *this->BuildFileStream << "\n";
  536. Indent(indent);
  537. *this->BuildFileStream << "/////////////////////////////////////////////\n";
  538. while ((rpos = comment.find('\n', lpos)) != std::string::npos) {
  539. Indent(indent);
  540. *this->BuildFileStream << "// " << comment.substr(lpos, rpos - lpos)
  541. << "\n";
  542. lpos = rpos + 1;
  543. }
  544. Indent(indent);
  545. *this->BuildFileStream << "// " << comment.substr(lpos) << "\n\n";
  546. }
  547. void cmGlobalFastbuildGenerator::WriteVariable(std::string const& key,
  548. std::string const& value,
  549. int indent)
  550. {
  551. WriteVariable(key, value, "=", indent);
  552. }
  553. void cmGlobalFastbuildGenerator::WriteVariable(std::string const& key,
  554. std::string const& value,
  555. std::string const& op,
  556. int indent)
  557. {
  558. Indent(indent);
  559. *this->BuildFileStream << "." << key << " " + op + (value.empty() ? "" : " ")
  560. << value << "\n";
  561. }
  562. void cmGlobalFastbuildGenerator::WriteCommand(std::string const& command,
  563. std::string const& value,
  564. int indent)
  565. {
  566. Indent(indent);
  567. *this->BuildFileStream << command;
  568. if (!value.empty()) {
  569. *this->BuildFileStream << "(" << value << ")";
  570. }
  571. *this->BuildFileStream << "\n";
  572. }
  573. void cmGlobalFastbuildGenerator::WriteArray(
  574. std::string const& key, std::vector<std::string> const& values, int indent)
  575. {
  576. WriteArray(key, values, "=", indent);
  577. }
  578. void cmGlobalFastbuildGenerator::WriteArray(
  579. std::string const& key, std::vector<std::string> const& values,
  580. std::string const& op, int indent)
  581. {
  582. WriteVariable(key, "", op, indent);
  583. Indent(indent);
  584. *this->BuildFileStream << "{\n";
  585. char const* sep = "";
  586. for (std::string const& value : values) {
  587. *this->BuildFileStream << sep;
  588. sep = ",\n";
  589. Indent(indent + 1);
  590. *this->BuildFileStream << value;
  591. }
  592. *this->BuildFileStream << "\n";
  593. Indent(indent);
  594. *this->BuildFileStream << "}\n";
  595. }
  596. void cmGlobalFastbuildGenerator::WriteStruct(
  597. std::string const& name,
  598. std::vector<std::pair<std::string, std::string>> const& variables,
  599. int indent)
  600. {
  601. WriteVariable(name, "", "=", indent);
  602. Indent(indent);
  603. *this->BuildFileStream << "[\n";
  604. for (auto const& val : variables) {
  605. auto const& key = val.first;
  606. auto const& value = val.second;
  607. WriteVariable(key, value, "=", indent + 1);
  608. }
  609. Indent(indent);
  610. *this->BuildFileStream << "]\n";
  611. }
  612. std::string cmGlobalFastbuildGenerator::Quote(std::string const& str,
  613. std::string const& quotation)
  614. {
  615. std::string result = str;
  616. cmSystemTools::ReplaceString(result, quotation, "^" + quotation);
  617. cmSystemTools::ReplaceString(result, FASTBUILD_DOLLAR_TAG, "$");
  618. return quotation + result + quotation;
  619. }
  620. std::string cmGlobalFastbuildGenerator::QuoteIfHasSpaces(std::string str)
  621. {
  622. if (str.find(' ') != std::string::npos) {
  623. return '"' + str + '"';
  624. }
  625. return str;
  626. }
  627. struct WrapHelper
  628. {
  629. std::string Prefix;
  630. std::string Suffix;
  631. bool EscapeDollar;
  632. std::string operator()(std::string in)
  633. {
  634. // If we have ^ in env variable - need to escape it.
  635. cmSystemTools::ReplaceString(in, "^", "^^");
  636. // Those all are considered as line ends by FASTBuild.
  637. cmSystemTools::ReplaceString(in, "\n", "\\n");
  638. cmSystemTools::ReplaceString(in, "\r", "\\r");
  639. // Escaping of single quotes tested in "RunCMake.CompilerArgs" test.
  640. cmSystemTools::ReplaceString(in, "'", "^'");
  641. std::string result = Prefix + in + Suffix;
  642. if (EscapeDollar) {
  643. cmSystemTools::ReplaceString(result, "$", "^$");
  644. cmSystemTools::ReplaceString(result, FASTBUILD_DOLLAR_TAG, "$");
  645. }
  646. return result;
  647. }
  648. std::string operator()(FastbuildTargetDep const& in)
  649. {
  650. return (*this)(in.Name);
  651. }
  652. };
  653. template <class T>
  654. std::vector<std::string> cmGlobalFastbuildGenerator::Wrap(
  655. T const& in, std::string const& prefix, std::string const& suffix,
  656. bool const escape_dollar)
  657. {
  658. std::vector<std::string> result;
  659. WrapHelper helper = { prefix, suffix, escape_dollar };
  660. std::transform(in.begin(), in.end(), std::back_inserter(result), helper);
  661. return result;
  662. }
  663. void cmGlobalFastbuildGenerator::TopologicalSort(
  664. std::vector<FastbuildTargetPtrT>& nodes)
  665. {
  666. std::unordered_map<std::string, int> inDegree;
  667. std::unordered_map<std::string, std::set<std::string>> reverseDeps;
  668. std::unordered_map<std::string, std::size_t> originalIndex;
  669. // Track original positions
  670. for (std::size_t i = 0; i < nodes.size(); ++i) {
  671. auto const& node = nodes[i];
  672. inDegree[node->Name] = 0;
  673. originalIndex[node->Name] = i;
  674. }
  675. // Build reverse dependency graph and in-degree map
  676. for (auto const& node : nodes) {
  677. for (auto const& dep : node->PreBuildDependencies) {
  678. if (inDegree.count(dep.Name)) {
  679. reverseDeps[dep.Name].insert(node->Name);
  680. ++inDegree[node->Name];
  681. }
  682. }
  683. }
  684. // Min-heap based on original position
  685. auto const cmp = [&](std::string const& a, std::string const& b) {
  686. return originalIndex[a] > originalIndex[b];
  687. };
  688. std::priority_queue<std::string, std::vector<std::string>, decltype(cmp)>
  689. zeroInDegree(cmp);
  690. for (auto const& val : inDegree) {
  691. auto const& degree = val.second;
  692. auto const& name = val.first;
  693. if (degree == 0) {
  694. zeroInDegree.push(name);
  695. }
  696. }
  697. std::vector<std::string> sorted;
  698. while (!zeroInDegree.empty()) {
  699. std::string node = zeroInDegree.top();
  700. zeroInDegree.pop();
  701. sorted.push_back(node);
  702. for (auto const& dep : reverseDeps[node]) {
  703. if (--inDegree[dep] == 0) {
  704. zeroInDegree.push(dep);
  705. }
  706. }
  707. }
  708. if (sorted.size() != nodes.size()) {
  709. cmSystemTools::Error("Failed to sort (Cyclic dependency)");
  710. cmSystemTools::Error(cmStrCat("Sorted size: ", sorted.size()));
  711. cmSystemTools::Error(cmStrCat("nodes size: ", nodes.size()));
  712. for (auto const& node : nodes) {
  713. cmSystemTools::Error("Node: " + node->Name);
  714. for (auto const& dep : reverseDeps[node->Name]) {
  715. cmSystemTools::Error("\tReverse dep: " + dep);
  716. }
  717. for (auto const& child : node->PreBuildDependencies) {
  718. cmSystemTools::Error("\tChild: " + child.Name);
  719. }
  720. }
  721. for (auto const& node : sorted) {
  722. cmSystemTools::Error("Sorted: " + node);
  723. }
  724. for (auto const& node : nodes) {
  725. cmSystemTools::Error("In node: " + node->Name);
  726. }
  727. }
  728. // Reconstruct sorted nodes
  729. std::vector<FastbuildTargetPtrT> result;
  730. for (auto const& name : sorted) {
  731. auto it = std::find_if(
  732. nodes.begin(), nodes.end(), [&name](FastbuildTargetPtrT const& node) {
  733. return node /* the node might be in moved-from state*/ &&
  734. node->Name == name;
  735. });
  736. if (it != nodes.end()) {
  737. result.emplace_back(std::move(*it));
  738. }
  739. }
  740. std::swap(result, nodes);
  741. }
  742. void cmGlobalFastbuildGenerator::WriteDisclaimer()
  743. {
  744. *this->BuildFileStream << "// CMAKE generated file: DO NOT EDIT!\n"
  745. << "// Generated by \"" << this->GetName() << "\""
  746. << " Generator, CMake Version "
  747. << cmVersion::GetMajorVersion() << "."
  748. << cmVersion::GetMinorVersion() << "\n\n";
  749. }
  750. void cmGlobalFastbuildGenerator::OpenBuildFileStream()
  751. {
  752. // Compute Fastbuild's build file path.
  753. std::string buildFilePath =
  754. this->GetCMakeInstance()->GetHomeOutputDirectory();
  755. buildFilePath += "/";
  756. buildFilePath += FASTBUILD_BUILD_FILE;
  757. // Get a stream where to generate things.
  758. if (!this->BuildFileStream) {
  759. this->BuildFileStream = cm::make_unique<cmGeneratedFileStream>(
  760. buildFilePath, false, this->GetMakefileEncoding());
  761. if (!this->BuildFileStream) {
  762. // An error message is generated by the constructor if it cannot
  763. // open the file.
  764. return;
  765. }
  766. }
  767. // Write the do not edit header.
  768. this->WriteDisclaimer();
  769. // Write a comment about this file.
  770. *this->BuildFileStream
  771. << "// This file contains all the build statements\n\n";
  772. }
  773. void cmGlobalFastbuildGenerator::CloseBuildFileStream()
  774. {
  775. if (this->BuildFileStream) {
  776. this->BuildFileStream.reset();
  777. } else {
  778. cmSystemTools::Error("Build file stream was not open.");
  779. }
  780. }
  781. void cmGlobalFastbuildGenerator::WriteCompilers()
  782. {
  783. WriteDivider();
  784. *this->BuildFileStream << "// Compilers\n\n";
  785. for (auto const& val : Compilers) {
  786. auto const& compilerDef = val.second;
  787. std::string compilerPath = compilerDef.Executable;
  788. // Write out the compiler that has been configured
  789. WriteCommand("Compiler", Quote(compilerDef.Name));
  790. *this->BuildFileStream << "{\n";
  791. for (auto const& extra : compilerDef.ExtraVariables) {
  792. auto const& extraKey = extra.first;
  793. auto const& extraVal = extra.second;
  794. WriteVariable(extraKey, Quote(extraVal), 1);
  795. }
  796. WriteVariable("Executable", Quote(compilerPath), 1);
  797. WriteVariable("CompilerFamily", Quote(compilerDef.CompilerFamily), 1);
  798. if (this->GetCMakeInstance()->GetIsInTryCompile()) {
  799. WriteVariable("AllowCaching", "false", 1);
  800. WriteVariable("AllowDistribution", "false", 1);
  801. }
  802. if (compilerDef.UseLightCache && compilerDef.CompilerFamily == "msvc") {
  803. WriteVariable("UseLightCache_Experimental", "true", 1);
  804. }
  805. if (compilerDef.UseRelativePaths) {
  806. WriteVariable("UseRelativePaths_Experimental", "true", 1);
  807. }
  808. if (compilerDef.UseDeterministicPaths) {
  809. WriteVariable("UseDeterministicPaths_Experimental", "true", 1);
  810. }
  811. if (!compilerDef.SourceMapping.empty()) {
  812. WriteVariable("SourceMapping_Experimental",
  813. Quote(compilerDef.SourceMapping), 1);
  814. }
  815. auto const isClang = [&compilerDef] {
  816. return compilerDef.CompilerFamily == "clang" ||
  817. compilerDef.CompilerFamily == "clang-cl";
  818. };
  819. if (!compilerDef.ClangRewriteIncludes && isClang()) {
  820. WriteVariable("ClangRewriteIncludes", "false", 1);
  821. }
  822. if (compilerDef.ClangGCCUpdateXLanguageArg &&
  823. (isClang() || compilerDef.CompilerFamily == "gcc")) {
  824. WriteVariable("ClangGCCUpdateXLanguageArg", "true", 1);
  825. }
  826. if (compilerDef.AllowResponseFile) {
  827. WriteVariable("AllowResponseFile", "true", 1);
  828. }
  829. if (compilerDef.ForceResponseFile) {
  830. WriteVariable("ForceResponseFile", "true", 1);
  831. }
  832. if (compilerDef.DontUseEnv) {
  833. LogMessage("Not using system environment");
  834. } else {
  835. if (!LocalEnvironment.empty()) {
  836. WriteVariable("Environment", "." FASTBUILD_ENV_VAR_NAME, 1);
  837. }
  838. }
  839. if (!compilerDef.ExtraFiles.empty()) {
  840. // Do not escape '$' sign, CMAKE_${LANG}_FASTBUILD_EXTRA_FILES might
  841. // contain FB variables to be expanded (we do use some internally).
  842. // Besides a path cannot contain a '$'
  843. WriteArray("ExtraFiles", Wrap(compilerDef.ExtraFiles, "'", "'", false),
  844. 1);
  845. }
  846. *this->BuildFileStream << "}\n";
  847. auto const compilerId = compilerDef.Name;
  848. WriteVariable(compilerId, Quote(compilerDef.Name));
  849. *this->BuildFileStream << "\n";
  850. }
  851. // We need this because the Library command needs a compiler
  852. // even if don't compile anything
  853. if (!this->Compilers.empty()) {
  854. WriteVariable("Compiler_dummy",
  855. Quote(this->Compilers.begin()->second.Name));
  856. }
  857. }
  858. void cmGlobalFastbuildGenerator::AddCompiler(std::string const& language,
  859. cmMakefile* mf)
  860. {
  861. if (this->Compilers.find(FASTBUILD_COMPILER_PREFIX + language) !=
  862. this->Compilers.end()) {
  863. return;
  864. }
  865. // Calculate the root location of the compiler
  866. std::string const variableString = "CMAKE_" + language + "_COMPILER";
  867. std::string const compilerLocation = mf->GetSafeDefinition(variableString);
  868. if (compilerLocation.empty()) {
  869. return;
  870. }
  871. // Calculate the i18n number.
  872. std::string i18nNum = "1033";
  873. // Add the language to the compiler's name
  874. FastbuildCompiler compilerDef;
  875. compilerDef.ExtraVariables["Root"] =
  876. cmSystemTools::GetFilenamePath(compilerLocation);
  877. compilerDef.Name = FASTBUILD_COMPILER_PREFIX + language;
  878. compilerDef.Executable = compilerLocation;
  879. compilerDef.CmakeCompilerID =
  880. mf->GetSafeDefinition("CMAKE_" + language + "_COMPILER_ID");
  881. if (compilerDef.CmakeCompilerID == "Clang" &&
  882. mf->GetSafeDefinition("CMAKE_" + language +
  883. "_COMPILER_FRONTEND_VARIANT") == "MSVC") {
  884. compilerDef.CmakeCompilerID = "Clang-cl";
  885. }
  886. compilerDef.CmakeCompilerVersion =
  887. mf->GetSafeDefinition("CMAKE_" + language + "_COMPILER_VERSION");
  888. compilerDef.Language = language;
  889. cmExpandList(mf->GetSafeDefinition(FASTBUILD_COMPILER_EXTRA_FILES),
  890. compilerDef.ExtraFiles);
  891. if (supportedLanguages.find(language) != supportedLanguages.end()) {
  892. auto const iter =
  893. compilerIdToFastbuildFamily.find(compilerDef.CmakeCompilerID);
  894. if (iter != compilerIdToFastbuildFamily.end()) {
  895. compilerDef.CompilerFamily = iter->second;
  896. }
  897. }
  898. // Has to be called after we determined 'CompilerFamily'.
  899. ReadCompilerOptions(compilerDef, mf);
  900. // If FASTBUILD_COMPILER_EXTRA_FILES is not set - automatically add extra
  901. // files based on compiler (see
  902. // https://fastbuild.org/docs/functions/compiler.html)
  903. if (compilerDef.ExtraFiles.empty() &&
  904. (language == "C" || language == "CXX") &&
  905. compilerDef.CmakeCompilerID == "MSVC") {
  906. // https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html
  907. // Visual Studio 17 (19.30 to 19.39)
  908. // TODO
  909. // Visual Studio 16 (19.20 to 19.29)
  910. if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  911. compilerDef.CmakeCompilerVersion,
  912. "19.20")) {
  913. compilerDef.ExtraFiles.push_back("$Root$/c1.dll");
  914. compilerDef.ExtraFiles.push_back("$Root$/c1xx.dll");
  915. compilerDef.ExtraFiles.push_back("$Root$/c2.dll");
  916. compilerDef.ExtraFiles.push_back(
  917. "$Root$/atlprov.dll"); // Only needed if using ATL
  918. compilerDef.ExtraFiles.push_back("$Root$/msobj140.dll");
  919. compilerDef.ExtraFiles.push_back("$Root$/mspdb140.dll");
  920. compilerDef.ExtraFiles.push_back("$Root$/mspdbcore.dll");
  921. compilerDef.ExtraFiles.push_back("$Root$/mspdbsrv.exe");
  922. compilerDef.ExtraFiles.push_back("$Root$/mspft140.dll");
  923. compilerDef.ExtraFiles.push_back("$Root$/msvcp140.dll");
  924. compilerDef.ExtraFiles.push_back(
  925. "$Root$/msvcp140_atomic_wait.dll"); // Required circa 16.8.3
  926. // (14.28.29333)
  927. compilerDef.ExtraFiles.push_back(
  928. "$Root$/tbbmalloc.dll"); // Required as of 16.2 (14.22.27905)
  929. compilerDef.ExtraFiles.push_back("$Root$/vcruntime140.dll");
  930. compilerDef.ExtraFiles.push_back(
  931. "$Root$/vcruntime140_1.dll"); // Required as of 16.5.1 (14.25.28610)
  932. compilerDef.ExtraFiles.push_back("$Root$/" + i18nNum + "/clui.dll");
  933. compilerDef.ExtraFiles.push_back(
  934. "$Root$/" + i18nNum + "/mspft140ui.dll"); // Localized messages for
  935. // static analysis
  936. }
  937. // Visual Studio 15 (19.10 to 19.19)
  938. else if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  939. compilerDef.CmakeCompilerVersion,
  940. "19.10")) {
  941. compilerDef.ExtraFiles.push_back("$Root$/c1.dll");
  942. compilerDef.ExtraFiles.push_back("$Root$/c1xx.dll");
  943. compilerDef.ExtraFiles.push_back("$Root$/c2.dll");
  944. compilerDef.ExtraFiles.push_back(
  945. "$Root$/atlprov.dll"); // Only needed if using ATL
  946. compilerDef.ExtraFiles.push_back("$Root$/msobj140.dll");
  947. compilerDef.ExtraFiles.push_back("$Root$/mspdb140.dll");
  948. compilerDef.ExtraFiles.push_back("$Root$/mspdbcore.dll");
  949. compilerDef.ExtraFiles.push_back("$Root$/mspdbsrv.exe");
  950. compilerDef.ExtraFiles.push_back("$Root$/mspft140.dll");
  951. compilerDef.ExtraFiles.push_back("$Root$/msvcp140.dll");
  952. compilerDef.ExtraFiles.push_back("$Root$/vcruntime140.dll");
  953. compilerDef.ExtraFiles.push_back("$Root$/" + i18nNum + "/clui.dll");
  954. }
  955. }
  956. // TODO: Handle Intel compiler
  957. this->Compilers[compilerDef.Name] = std::move(compilerDef);
  958. }
  959. void cmGlobalFastbuildGenerator::AddLauncher(std::string const& prefix,
  960. std::string const& launcher,
  961. std::string const& language,
  962. std::string const& args)
  963. {
  964. if (this->Compilers.find(prefix + language) != this->Compilers.end()) {
  965. return;
  966. }
  967. LogMessage("Launcher: " + launcher);
  968. LogMessage("Launcher args: " + args);
  969. FastbuildCompiler compilerDef;
  970. compilerDef.Name = prefix + language;
  971. compilerDef.Args = args;
  972. if (cmSystemTools::FileIsFullPath(launcher)) {
  973. compilerDef.Executable = launcher;
  974. } else {
  975. // FASTBuild needs an absolute path to the executable.
  976. compilerDef.Executable = cmSystemTools::FindProgram(launcher);
  977. if (compilerDef.Executable.empty()) {
  978. cmSystemTools::Error("Failed to find path to " + launcher);
  979. return;
  980. }
  981. }
  982. // When CTest is used as a launcher, there is an interesting env variable
  983. // "CTEST_LAUNCH_LOGS" which is set by parent CTest process and is expected
  984. // to be read from global (sic!) env by the launched CTest process. So we
  985. // will need to make this global env available for CTest executable used as a
  986. // "launcher". Tested in RunCMake.ctest_labels_for_subprojects test..
  987. compilerDef.DontUseEnv = true;
  988. this->Compilers[compilerDef.Name] = std::move(compilerDef);
  989. }
  990. std::string cmGlobalFastbuildGenerator::ConvertToFastbuildPath(
  991. std::string const& path) const
  992. {
  993. cmLocalGenerator const* root = LocalGenerators[0].get();
  994. return root->MaybeRelativeToWorkDir(cmSystemTools::FileIsFullPath(path)
  995. ? cmSystemTools::CollapseFullPath(path)
  996. : path);
  997. }
  998. std::unique_ptr<cmLinkLineComputer>
  999. cmGlobalFastbuildGenerator::CreateLinkLineComputer(
  1000. cmOutputConverter* outputConverter,
  1001. cmStateDirectory const& /* stateDir */) const
  1002. {
  1003. return cm::make_unique<cmFastbuildLinkLineComputer>(
  1004. outputConverter,
  1005. this->LocalGenerators[0]->GetStateSnapshot().GetDirectory(), this);
  1006. }
  1007. void cmGlobalFastbuildGenerator::WriteExec(FastbuildExecNode const& Exec,
  1008. int indent)
  1009. {
  1010. auto const identPlus1 = indent + 1;
  1011. WriteCommand("Exec", Exec.Name.empty() ? std::string{} : Quote(Exec.Name),
  1012. indent);
  1013. Indent(indent);
  1014. *BuildFileStream << "{\n";
  1015. {
  1016. if (!Exec.PreBuildDependencies.empty()) {
  1017. WriteArray("PreBuildDependencies", Wrap(Exec.PreBuildDependencies),
  1018. identPlus1);
  1019. }
  1020. WriteVariable("ExecExecutable", Quote(Exec.ExecExecutable), identPlus1);
  1021. if (!Exec.ExecArguments.empty()) {
  1022. WriteVariable("ExecArguments", Quote(Exec.ExecArguments), identPlus1);
  1023. }
  1024. if (!Exec.ExecWorkingDir.empty()) {
  1025. WriteVariable("ExecWorkingDir", Quote(Exec.ExecWorkingDir), identPlus1);
  1026. }
  1027. if (!Exec.ExecInput.empty()) {
  1028. WriteArray("ExecInput", Wrap(Exec.ExecInput), identPlus1);
  1029. }
  1030. if (Exec.ExecUseStdOutAsOutput) {
  1031. WriteVariable("ExecUseStdOutAsOutput", "true", identPlus1);
  1032. }
  1033. if (!Exec.ExecInputPath.empty()) {
  1034. WriteArray("ExecInputPath", Wrap(Exec.ExecInputPath), identPlus1);
  1035. }
  1036. if (!Exec.ExecInputPattern.empty()) {
  1037. WriteArray("ExecInputPattern", Wrap(Exec.ExecInputPattern), identPlus1);
  1038. }
  1039. WriteVariable("ExecAlwaysShowOutput", "true", identPlus1);
  1040. WriteVariable("ExecOutput", Quote(Exec.ExecOutput), identPlus1);
  1041. WriteVariable("ExecAlways", Exec.ExecAlways ? "true" : "false",
  1042. identPlus1);
  1043. if (!Exec.ConcurrencyGroupName.empty()) {
  1044. WriteVariable("ConcurrencyGroupName", Quote(Exec.ConcurrencyGroupName),
  1045. identPlus1);
  1046. }
  1047. }
  1048. Indent(indent);
  1049. *BuildFileStream << "}\n";
  1050. static bool const verbose = GlobalSettingIsOn(FASTBUILD_VERBOSE_GENERATOR) ||
  1051. cmSystemTools::HasEnv(FASTBUILD_VERBOSE_GENERATOR);
  1052. // Those aliases are only used for troubleshooting the generated file.
  1053. if (verbose) {
  1054. WriteAlias(Exec.OutputsAlias);
  1055. WriteAlias(Exec.ByproductsAlias);
  1056. }
  1057. }
  1058. void cmGlobalFastbuildGenerator::WriteUnity(FastbuildUnityNode const& Unity)
  1059. {
  1060. WriteCommand("Unity", Quote(Unity.Name), 1);
  1061. Indent(1);
  1062. *BuildFileStream << "{\n";
  1063. {
  1064. WriteVariable("UnityOutputPath", Quote(Unity.UnityOutputPath), 2);
  1065. WriteVariable("UnityOutputPattern", Quote(Unity.UnityOutputPattern), 2);
  1066. WriteArray("UnityInputFiles", Wrap(Unity.UnityInputFiles), 2);
  1067. if (!Unity.UnityInputIsolatedFiles.empty()) {
  1068. WriteArray("UnityInputIsolatedFiles",
  1069. Wrap(Unity.UnityInputIsolatedFiles), 2);
  1070. }
  1071. if (UsingRelativePaths) {
  1072. WriteVariable("UseRelativePaths_Experimental", "true", 2);
  1073. }
  1074. }
  1075. Indent(1);
  1076. *BuildFileStream << "}\n";
  1077. }
  1078. void cmGlobalFastbuildGenerator::WriteObjectList(
  1079. FastbuildObjectListNode const& ObjectList, bool allowDistribution)
  1080. {
  1081. WriteCommand("ObjectList", Quote(ObjectList.Name), 1);
  1082. Indent(1);
  1083. *BuildFileStream << "{\n";
  1084. {
  1085. if (!allowDistribution) {
  1086. WriteVariable("AllowDistribution", "false", 2);
  1087. }
  1088. if (!ObjectList.PreBuildDependencies.empty()) {
  1089. WriteArray("PreBuildDependencies", Wrap(ObjectList.PreBuildDependencies),
  1090. 2);
  1091. }
  1092. WriteVariable("Compiler", ObjectList.Compiler, 2);
  1093. // If only PCH output is present - this node reuses existing PCH.
  1094. if (!ObjectList.PCHOutputFile.empty()) {
  1095. WriteVariable("PCHOutputFile", Quote(ObjectList.PCHOutputFile), 2);
  1096. }
  1097. // If PCHInputFile and PCHOptions are present - this node creates PCH.
  1098. if (!ObjectList.PCHInputFile.empty() && !ObjectList.PCHOptions.empty()) {
  1099. WriteVariable("PCHInputFile", Quote(ObjectList.PCHInputFile), 2);
  1100. WriteVariable("PCHOptions", Quote(ObjectList.PCHOptions), 2);
  1101. }
  1102. WriteVariable("CompilerOptions", Quote(ObjectList.CompilerOptions), 2);
  1103. WriteVariable("CompilerOutputPath", Quote(ObjectList.CompilerOutputPath),
  1104. 2);
  1105. WriteVariable("CompilerOutputExtension",
  1106. Quote(ObjectList.CompilerOutputExtension), 2);
  1107. WriteVariable("CompilerOutputKeepBaseExtension", "true", 2);
  1108. if (!ObjectList.CompilerInputUnity.empty()) {
  1109. WriteArray("CompilerInputUnity", Wrap(ObjectList.CompilerInputUnity), 2);
  1110. }
  1111. if (!ObjectList.CompilerInputFiles.empty()) {
  1112. WriteArray("CompilerInputFiles", Wrap(ObjectList.CompilerInputFiles), 2);
  1113. }
  1114. if (!ObjectList.AllowCaching) {
  1115. WriteVariable("AllowCaching", "false", 2);
  1116. }
  1117. if (!ObjectList.AllowDistribution) {
  1118. WriteVariable("AllowDistribution", "false", 2);
  1119. }
  1120. if (ObjectList.Hidden) {
  1121. WriteVariable("Hidden", "true", 2);
  1122. }
  1123. }
  1124. Indent(1);
  1125. *BuildFileStream << "}\n";
  1126. }
  1127. void cmGlobalFastbuildGenerator::WriteLinker(
  1128. FastbuildLinkerNode const& LinkerNode, bool allowDistribution)
  1129. {
  1130. WriteCommand(
  1131. LinkerNode.Type == FastbuildLinkerNode::EXECUTABLE ? "Executable"
  1132. : LinkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY ? "DLL"
  1133. : "Library",
  1134. (!LinkerNode.Name.empty() && LinkerNode.Name != LinkerNode.LinkerOutput)
  1135. ? Quote(LinkerNode.Name)
  1136. : "",
  1137. 1);
  1138. Indent(1);
  1139. *BuildFileStream << "{\n";
  1140. {
  1141. if (!LinkerNode.PreBuildDependencies.empty()) {
  1142. WriteArray("PreBuildDependencies", Wrap(LinkerNode.PreBuildDependencies),
  1143. 2);
  1144. }
  1145. if (!allowDistribution) {
  1146. WriteVariable("AllowDistribution", "false", 2);
  1147. }
  1148. if (!LinkerNode.Compiler.empty() &&
  1149. LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY) {
  1150. WriteVariable("Compiler", LinkerNode.Compiler, 2);
  1151. WriteVariable("CompilerOptions", Quote(LinkerNode.CompilerOptions), 2);
  1152. WriteVariable("CompilerOutputPath", Quote("."), 2);
  1153. }
  1154. if (!LocalEnvironment.empty()) {
  1155. WriteVariable("Environment", "." FASTBUILD_ENV_VAR_NAME, 2);
  1156. }
  1157. WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
  1158. ? "Librarian"
  1159. : "Linker",
  1160. Quote(LinkerNode.Linker), 2);
  1161. WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
  1162. ? "LibrarianOptions"
  1163. : "LinkerOptions",
  1164. Quote(LinkerNode.LinkerOptions), 2);
  1165. WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
  1166. ? "LibrarianOutput"
  1167. : "LinkerOutput",
  1168. Quote(LinkerNode.LinkerOutput), 2);
  1169. if (!LinkerNode.LibrarianAdditionalInputs.empty()) {
  1170. WriteArray(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
  1171. ? "LibrarianAdditionalInputs"
  1172. : "Libraries",
  1173. Wrap(LinkerNode.LibrarianAdditionalInputs), 2);
  1174. }
  1175. if (!LinkerNode.Libraries2.empty()) {
  1176. WriteArray("Libraries2", Wrap(LinkerNode.Libraries2), 2);
  1177. }
  1178. if (!LinkerNode.LibrarianAdditionalInputs.empty()) {
  1179. if (!LinkerNode.LinkerType.empty()) {
  1180. WriteVariable("LinkerType", Quote(LinkerNode.LinkerType), 2);
  1181. }
  1182. }
  1183. if (LinkerNode.Type == FastbuildLinkerNode::EXECUTABLE ||
  1184. LinkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY) {
  1185. WriteVariable("LinkerLinkObjects",
  1186. LinkerNode.LinkerLinkObjects ? "true" : "false", 2);
  1187. if (!LinkerNode.LinkerStampExe.empty()) {
  1188. WriteVariable("LinkerStampExe", Quote(LinkerNode.LinkerStampExe), 2);
  1189. if (!LinkerNode.LinkerStampExeArgs.empty()) {
  1190. WriteVariable("LinkerStampExeArgs",
  1191. Quote(LinkerNode.LinkerStampExeArgs), 2);
  1192. }
  1193. }
  1194. }
  1195. Indent(1);
  1196. *BuildFileStream << "}\n";
  1197. }
  1198. }
  1199. void cmGlobalFastbuildGenerator::WriteAlias(FastbuildAliasNode const& Alias,
  1200. int indent)
  1201. {
  1202. if (Alias.PreBuildDependencies.empty()) {
  1203. return;
  1204. }
  1205. auto const identPlus1 = indent + 1;
  1206. WriteCommand("Alias", Quote(Alias.Name), indent);
  1207. Indent(indent);
  1208. *BuildFileStream << "{\n";
  1209. WriteArray("Targets", Wrap(Alias.PreBuildDependencies), identPlus1);
  1210. if (Alias.Hidden) {
  1211. WriteVariable("Hidden", "true", identPlus1);
  1212. }
  1213. Indent(indent);
  1214. *BuildFileStream << "}\n";
  1215. }
  1216. void cmGlobalFastbuildGenerator::WriteCopy(FastbuildCopyNode const& Copy)
  1217. {
  1218. cmGlobalFastbuildGenerator::WriteCommand(
  1219. Copy.CopyDir ? "CopyDir" : "Copy",
  1220. cmGlobalFastbuildGenerator::Quote(Copy.Name), 1);
  1221. cmGlobalFastbuildGenerator::Indent(1);
  1222. *BuildFileStream << "{\n";
  1223. WriteVariable("PreBuildDependencies",
  1224. cmGlobalFastbuildGenerator::Quote(Copy.PreBuildDependencies),
  1225. 2);
  1226. WriteVariable(Copy.CopyDir ? "SourcePaths" : "Source",
  1227. cmGlobalFastbuildGenerator::Quote(Copy.Source), 2);
  1228. WriteVariable("Dest", cmGlobalFastbuildGenerator::Quote(Copy.Dest), 2);
  1229. cmGlobalFastbuildGenerator::Indent(1);
  1230. *BuildFileStream << "}\n";
  1231. }
  1232. void cmGlobalFastbuildGenerator::WriteTarget(FastbuildTarget const& target)
  1233. {
  1234. for (auto const& val : target.Variables) {
  1235. auto const& key = val.first;
  1236. auto const& value = val.second;
  1237. WriteVariable(key, cmGlobalFastbuildGenerator::Quote(value), 1);
  1238. }
  1239. // add_custom_commands(...)
  1240. for (auto const& alias : { target.ExecNodes }) {
  1241. this->WriteAlias(alias);
  1242. }
  1243. // -deps Alias.
  1244. this->WriteAlias(target.DependenciesAlias);
  1245. // PRE_BUILD.
  1246. for (auto const& alias : { target.PreBuildExecNodes }) {
  1247. this->WriteAlias(alias);
  1248. }
  1249. // Copy commands.
  1250. for (FastbuildCopyNode const& node : target.CopyNodes) {
  1251. this->WriteCopy(node);
  1252. }
  1253. // Unity.
  1254. for (FastbuildUnityNode const& unity : target.UnityNodes) {
  1255. this->WriteUnity(unity);
  1256. }
  1257. // Objects.
  1258. for (FastbuildObjectListNode const& objectList : target.ObjectListNodes) {
  1259. this->WriteObjectList(objectList, target.AllowDistribution);
  1260. }
  1261. if (!target.PreLinkExecNodes.Nodes.empty()) {
  1262. for (auto const& exec : target.PreLinkExecNodes.Nodes) {
  1263. this->WriteExec(exec);
  1264. }
  1265. this->WriteAlias(target.PreLinkExecNodes.Alias);
  1266. }
  1267. // Libraries / executables.
  1268. if (!target.LinkerNode.empty()) {
  1269. for (auto const& cudaDeviceLinkNode : target.CudaDeviceLinkNode) {
  1270. this->WriteLinker(cudaDeviceLinkNode, target.AllowDistribution);
  1271. }
  1272. for (auto const& linkerNode : target.LinkerNode) {
  1273. this->WriteLinker(linkerNode, target.AllowDistribution);
  1274. }
  1275. }
  1276. if (!target.PostBuildExecNodes.Nodes.empty()) {
  1277. for (auto const& exec : target.PostBuildExecNodes.Nodes) {
  1278. this->WriteExec(exec);
  1279. }
  1280. this->WriteAlias(target.PostBuildExecNodes.Alias);
  1281. }
  1282. // Aliases (if any).
  1283. for (FastbuildAliasNode const& alias : target.AliasNodes) {
  1284. this->WriteAlias(alias);
  1285. }
  1286. }
  1287. void cmGlobalFastbuildGenerator::WriteIDEProjects()
  1288. {
  1289. for (auto const& proj : IDEProjects) {
  1290. (void)proj;
  1291. // VS
  1292. #if defined(_WIN32)
  1293. auto const& VSProj = proj.second.first;
  1294. WriteCommand("VCXProject", Quote(VSProj.Alias));
  1295. *this->BuildFileStream << "{\n";
  1296. WriteVariable("ProjectOutput", Quote(VSProj.ProjectOutput), 1);
  1297. WriteIDEProjectConfig(VSProj.ProjectConfigs);
  1298. WriteVSBuildCommands();
  1299. WriteIDEProjectCommon(VSProj);
  1300. *this->BuildFileStream << "}\n\n";
  1301. // XCode
  1302. #elif defined(__APPLE__)
  1303. auto const& XCodeProj = proj.second.second;
  1304. WriteCommand("XCodeProject", Quote(XCodeProj.Alias), 0);
  1305. *this->BuildFileStream << "{\n";
  1306. WriteVariable("ProjectOutput", Quote(XCodeProj.ProjectOutput), 1);
  1307. WriteIDEProjectConfig(XCodeProj.ProjectConfigs);
  1308. WriteXCodeBuildCommands();
  1309. WriteIDEProjectCommon(XCodeProj);
  1310. *this->BuildFileStream << "}\n\n";
  1311. #endif
  1312. }
  1313. #if defined(_WIN32)
  1314. this->WriteSolution();
  1315. #elif defined(__APPLE__)
  1316. this->WriteXCodeTopLevelProject();
  1317. #endif
  1318. }
  1319. std::string cmGlobalFastbuildGenerator::GetIDEBuildArgs() const
  1320. {
  1321. cmValue const ideArgs = this->GetGlobalSetting(FASTBUILD_IDE_ARGS);
  1322. if (ideArgs) {
  1323. return cmStrCat(' ', ideArgs, ' ');
  1324. }
  1325. return FASTBUILD_DEFAULT_IDE_BUILD_ARGS;
  1326. }
  1327. void cmGlobalFastbuildGenerator::WriteVSBuildCommands()
  1328. {
  1329. std::string const ideArgs = this->GetIDEBuildArgs();
  1330. WriteVariable(
  1331. "ProjectBuildCommand",
  1332. Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX, this->FastbuildCommand,
  1333. ideArgs, " ^$(ProjectName)")),
  1334. 1);
  1335. WriteVariable(
  1336. "ProjectRebuildCommand",
  1337. Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX, this->FastbuildCommand,
  1338. ideArgs, "-clean ^$(ProjectName)")),
  1339. 1);
  1340. WriteVariable("ProjectCleanCommand",
  1341. Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX,
  1342. this->FastbuildCommand, ideArgs, " clean")),
  1343. 1);
  1344. }
  1345. void cmGlobalFastbuildGenerator::WriteXCodeBuildCommands()
  1346. {
  1347. std::string const ideArgs = this->GetIDEBuildArgs();
  1348. WriteVariable("XCodeBuildToolPath", Quote(this->FastbuildCommand), 1);
  1349. WriteVariable("XCodeBuildToolArgs",
  1350. Quote(cmStrCat(ideArgs, "^$(FASTBUILD_TARGET)")), 1);
  1351. WriteVariable("XCodeBuildWorkingDir",
  1352. Quote(this->CMakeInstance->GetHomeOutputDirectory()), 1);
  1353. }
  1354. void cmGlobalFastbuildGenerator::WriteIDEProjectCommon(
  1355. IDEProjectCommon const& project)
  1356. {
  1357. WriteVariable("ProjectBasePath", Quote(project.ProjectBasePath), 1);
  1358. // So Fastbuild will pick up files relative to CMakeLists.txt
  1359. WriteVariable("ProjectInputPaths", Quote(project.ProjectBasePath), 1);
  1360. }
  1361. void cmGlobalFastbuildGenerator::WriteIDEProjectConfig(
  1362. std::vector<IDEProjectConfig> const& configs, std::string const& keyName)
  1363. {
  1364. std::vector<std::string> allConfigVariables;
  1365. for (auto const& config : configs) {
  1366. std::string configName = "Config" + config.Config;
  1367. WriteVariable(configName, "", 1);
  1368. Indent(1);
  1369. *this->BuildFileStream << "[\n";
  1370. WriteVariable("Config", Quote(config.Config), 2);
  1371. if (!config.Target.empty()) {
  1372. WriteVariable("Target", Quote(config.Target), 2);
  1373. }
  1374. if (!config.Platform.empty()) {
  1375. WriteVariable("Platform", Quote(config.Platform), 2);
  1376. }
  1377. Indent(1);
  1378. *this->BuildFileStream << "]\n";
  1379. allConfigVariables.emplace_back(std::move(configName));
  1380. }
  1381. WriteArray(keyName, Wrap(allConfigVariables, ".", ""), 1);
  1382. }
  1383. void cmGlobalFastbuildGenerator::AddTargetAll()
  1384. {
  1385. FastbuildAliasNode allAliasNode;
  1386. allAliasNode.Name = FASTBUILD_ALL_TARGET_NAME;
  1387. for (auto const& targetBase : FastbuildTargets) {
  1388. if (targetBase->Type == FastbuildTargetType::LINK) {
  1389. auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
  1390. // Add non-global and non-excluded targets to "all"
  1391. if (!target.IsGlobal && !target.ExcludeFromAll) {
  1392. allAliasNode.PreBuildDependencies.emplace(target.Name);
  1393. }
  1394. } else if (targetBase->Type == FastbuildTargetType::ALIAS) {
  1395. auto const& target = static_cast<FastbuildAliasNode const&>(*targetBase);
  1396. if (!target.ExcludeFromAll) {
  1397. allAliasNode.PreBuildDependencies.emplace(target.Name);
  1398. }
  1399. }
  1400. }
  1401. if (allAliasNode.PreBuildDependencies.empty()) {
  1402. allAliasNode.PreBuildDependencies.emplace(FASTBUILD_NOOP_FILE_NAME);
  1403. }
  1404. this->AddTarget(std::move(allAliasNode));
  1405. }
  1406. void cmGlobalFastbuildGenerator::AddGlobCheckExec()
  1407. {
  1408. // Tested in "RunCMake.file" test.
  1409. std::string const globScript =
  1410. this->GetCMakeInstance()->GetGlobVerifyScript();
  1411. if (!globScript.empty()) {
  1412. FastbuildExecNode globCheck;
  1413. globCheck.Name = FASTBUILD_GLOB_CHECK_TARGET;
  1414. globCheck.ExecExecutable = cmSystemTools::GetCMakeCommand();
  1415. globCheck.ExecArguments =
  1416. cmStrCat("-P ", this->ConvertToFastbuildPath(globScript));
  1417. globCheck.ExecAlways = false;
  1418. globCheck.ExecUseStdOutAsOutput = false;
  1419. auto const cache = this->GetCMakeInstance()->GetGlobCacheEntries();
  1420. for (auto const& entry : cache) {
  1421. auto path = cmSystemTools::GetFilenamePath(entry.Expression);
  1422. auto expression = cmSystemTools::GetFilenameName(entry.Expression);
  1423. if (std::find(globCheck.ExecInputPath.begin(),
  1424. globCheck.ExecInputPath.end(),
  1425. path) == globCheck.ExecInputPath.end()) {
  1426. globCheck.ExecInputPath.emplace_back(std::move(path));
  1427. }
  1428. if (std::find(globCheck.ExecInputPattern.begin(),
  1429. globCheck.ExecInputPattern.end(),
  1430. expression) == globCheck.ExecInputPattern.end()) {
  1431. globCheck.ExecInputPattern.emplace_back(std::move(expression));
  1432. }
  1433. }
  1434. globCheck.ExecOutput = this->ConvertToFastbuildPath(
  1435. this->GetCMakeInstance()->GetGlobVerifyStamp());
  1436. this->AddTarget(std::move(globCheck));
  1437. }
  1438. }
  1439. void cmGlobalFastbuildGenerator::WriteSolution()
  1440. {
  1441. std::string const solutionName = LocalGenerators[0]->GetProjectName();
  1442. std::map<std::string /*folder*/, std::vector<std::string>> VSProjects;
  1443. std::vector<std::string> VSProjectsWithoutFolder;
  1444. for (auto const& IDEProj : IDEProjects) {
  1445. auto const VSProj = IDEProj.second.first;
  1446. VSProjects[VSProj.folder].emplace_back(VSProj.Alias);
  1447. }
  1448. WriteCommand("VSSolution", Quote("solution"));
  1449. *this->BuildFileStream << "{\n";
  1450. WriteVariable("SolutionOutput",
  1451. Quote(cmJoin({ "VisualStudio", solutionName + ".sln" }, "/")),
  1452. 1);
  1453. auto const& configs = IDEProjects.begin()->second.first.ProjectConfigs;
  1454. WriteIDEProjectConfig(configs, "SolutionConfigs");
  1455. int folderNumber = 0;
  1456. std::vector<std::string> folders;
  1457. for (auto& item : VSProjects) {
  1458. auto const& pathToFolder = item.first;
  1459. auto& projectsInFolder = item.second;
  1460. if (pathToFolder.empty()) {
  1461. std::move(projectsInFolder.begin(), projectsInFolder.end(),
  1462. std::back_inserter(VSProjectsWithoutFolder));
  1463. } else {
  1464. std::string folderName = cmStrCat("Folder_", ++folderNumber);
  1465. WriteStruct(
  1466. folderName,
  1467. { { "Path", Quote(pathToFolder) },
  1468. { "Projects",
  1469. cmStrCat("{", cmJoin(Wrap(projectsInFolder), ","), "}") } },
  1470. 1);
  1471. folders.emplace_back(std::move(folderName));
  1472. }
  1473. }
  1474. if (!folders.empty()) {
  1475. WriteArray("SolutionFolders ", Wrap(folders, ".", ""), 1);
  1476. }
  1477. if (!VSProjectsWithoutFolder.empty()) {
  1478. WriteArray("SolutionProjects", Wrap(VSProjectsWithoutFolder), 1);
  1479. }
  1480. *this->BuildFileStream << "}\n";
  1481. }
  1482. void cmGlobalFastbuildGenerator::WriteXCodeTopLevelProject()
  1483. {
  1484. std::string const projectName = LocalGenerators[0]->GetProjectName();
  1485. std::vector<std::string> XCodeProjects;
  1486. for (auto const& IDEProj : IDEProjects) {
  1487. auto const XCodeProj = IDEProj.second.second;
  1488. XCodeProjects.emplace_back(XCodeProj.Alias);
  1489. }
  1490. WriteCommand("XCodeProject", Quote("xcode"));
  1491. *this->BuildFileStream << "{\n";
  1492. WriteVariable(
  1493. "ProjectOutput",
  1494. Quote(
  1495. cmJoin({ "XCode", projectName + ".xcodeproj", "project.pbxproj" }, "/")),
  1496. 1);
  1497. WriteVariable("ProjectBasePath", Quote(FASTBUILD_XCODE_BASE_PATH), 1);
  1498. auto const& configs = IDEProjects.begin()->second.second.ProjectConfigs;
  1499. WriteIDEProjectConfig(configs);
  1500. WriteArray("ProjectFiles", Wrap(XCodeProjects), 1);
  1501. *this->BuildFileStream << "}\n";
  1502. }
  1503. void cmGlobalFastbuildGenerator::LogMessage(std::string const& m) const
  1504. {
  1505. static bool const verbose = GlobalSettingIsOn(FASTBUILD_VERBOSE_GENERATOR) ||
  1506. cmSystemTools::HasEnv(FASTBUILD_VERBOSE_GENERATOR);
  1507. if (verbose) {
  1508. cmSystemTools::Message(m);
  1509. }
  1510. }
  1511. void cmGlobalFastbuildGenerator::AddFileToClean(std::string const& file)
  1512. {
  1513. AllFilesToClean.insert(file);
  1514. }
  1515. std::string cmGlobalFastbuildGenerator::GetExternalShellExecutable()
  1516. {
  1517. // FindProgram is expensive - touches filesystem and makes syscalls, so cache
  1518. // it.
  1519. static std::string const cached =
  1520. #ifdef _WIN32
  1521. cmSystemTools::FindProgram(
  1522. "cmd.exe", std::vector<std::string>{ "C:\\Windows\\System32" });
  1523. #else
  1524. cmSystemTools::FindProgram("sh", std::vector<std::string>{ "/bin" });
  1525. #endif
  1526. return cached;
  1527. }
  1528. void cmGlobalFastbuildGenerator::WriteTargetRebuildBFF()
  1529. {
  1530. std::vector<std::string> implicitDeps;
  1531. for (auto& lg : LocalGenerators) {
  1532. std::vector<std::string> const& lf = lg->GetMakefile()->GetListFiles();
  1533. for (auto const& dep : lf) {
  1534. implicitDeps.push_back(this->ConvertToFastbuildPath(dep));
  1535. }
  1536. }
  1537. auto const* cmake = this->GetCMakeInstance();
  1538. std::string outDir = cmake->GetHomeOutputDirectory() + '/';
  1539. implicitDeps.push_back(outDir + "CMakeCache.txt");
  1540. FastbuildExecNode rebuildBFF;
  1541. rebuildBFF.Name = FASTBUILD_REBUILD_BFF_TARGET_NAME;
  1542. if (!this->GetCMakeInstance()->GetGlobVerifyScript().empty()) {
  1543. implicitDeps.emplace_back(this->GetCMakeInstance()->GetGlobVerifyStamp());
  1544. }
  1545. std::sort(implicitDeps.begin(), implicitDeps.end());
  1546. implicitDeps.erase(std::unique(implicitDeps.begin(), implicitDeps.end()),
  1547. implicitDeps.end());
  1548. std::string args =
  1549. cmStrCat("--regenerate-during-build",
  1550. (this->GetCMakeInstance()->GetIgnoreCompileWarningAsError()
  1551. ? " --compile-no-warning-as-error"
  1552. : ""),
  1553. (this->GetCMakeInstance()->GetIgnoreLinkWarningAsError()
  1554. ? " --link-no-warning-as-error"
  1555. : ""),
  1556. " -S", QuoteIfHasSpaces(cmake->GetHomeDirectory()), " -B",
  1557. QuoteIfHasSpaces(cmake->GetHomeOutputDirectory()));
  1558. rebuildBFF.ExecArguments = std::move(args);
  1559. rebuildBFF.ExecInput = implicitDeps;
  1560. rebuildBFF.ExecExecutable = cmSystemTools::GetCMakeCommand();
  1561. rebuildBFF.ExecWorkingDir = outDir;
  1562. rebuildBFF.ExecOutput = outDir + FASTBUILD_BUILD_FILE;
  1563. this->WriteExec(rebuildBFF, 0);
  1564. }
  1565. void cmGlobalFastbuildGenerator::WriteCleanScript()
  1566. {
  1567. std::string const path =
  1568. cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), '/',
  1569. FASTBUILD_CLEAN_SCRIPT_NAME);
  1570. cmsys::ofstream scriptFile(path.c_str(), std::ios::out | std::ios::binary);
  1571. if (!scriptFile.is_open()) {
  1572. cmSystemTools::Error("Failed to open: " FASTBUILD_CLEAN_SCRIPT_NAME);
  1573. return;
  1574. }
  1575. for (std::string const& file : AllFilesToClean) {
  1576. #if defined(_WIN32)
  1577. scriptFile << "del /f /q "
  1578. << cmSystemTools::ConvertToWindowsOutputPath(file) << "\n";
  1579. #else
  1580. scriptFile << "rm -f " << file << '\n';
  1581. #endif
  1582. }
  1583. }
  1584. void cmGlobalFastbuildGenerator::WriteTargetClean()
  1585. {
  1586. if (AllFilesToClean.empty()) {
  1587. FastbuildAliasNode clean;
  1588. clean.Name = FASTBUILD_CLEAN_TARGET_NAME;
  1589. clean.PreBuildDependencies.emplace(FASTBUILD_CLEAN_FILE_NAME);
  1590. WriteAlias(clean, 0);
  1591. return;
  1592. }
  1593. WriteCleanScript();
  1594. FastbuildExecNode clean;
  1595. clean.Name = FASTBUILD_CLEAN_TARGET_NAME;
  1596. clean.ExecExecutable = GetExternalShellExecutable();
  1597. clean.ExecArguments =
  1598. FASTBUILD_SCRIPT_FILE_ARG FASTBUILD_1_INPUT_PLACEHOLDER;
  1599. clean.ExecInput = { FASTBUILD_CLEAN_SCRIPT_NAME };
  1600. clean.ExecAlways = true;
  1601. clean.ExecUseStdOutAsOutput = true;
  1602. clean.ExecOutput = FASTBUILD_CLEAN_FILE_NAME;
  1603. clean.ExecWorkingDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  1604. WriteExec(clean, 0);
  1605. }
  1606. void cmGlobalFastbuildGenerator::WriteTargets()
  1607. {
  1608. std::string const outputDir = this->CMakeInstance->GetHomeOutputDirectory();
  1609. LogMessage("GetHomeOutputDirectory: " + outputDir);
  1610. // Noop file that 'all' can alias to if we don't have any other targets...
  1611. // The exact location of the "noop" file is verified in one of the tests in
  1612. // "RunCMake.CMakePresetsPackage" test suite.
  1613. cmSystemTools::Touch(cmStrCat(this->CMakeInstance->GetHomeOutputDirectory(),
  1614. '/', FASTBUILD_NOOP_FILE_NAME),
  1615. true);
  1616. cmSystemTools::Touch(cmStrCat(this->CMakeInstance->GetHomeOutputDirectory(),
  1617. '/', FASTBUILD_CLEAN_FILE_NAME),
  1618. true);
  1619. // Add "all" utility target before sorting, so we can correctly sort
  1620. // targets that depend on it
  1621. AddTargetAll();
  1622. TopologicalSort(FastbuildTargets);
  1623. AddGlobCheckExec();
  1624. for (auto const& targetBase : FastbuildTargets) {
  1625. this->WriteComment("Target definition: " + targetBase->Name);
  1626. // Target start.
  1627. *BuildFileStream << "{\n";
  1628. if (targetBase->Type == FastbuildTargetType::EXEC) {
  1629. this->WriteExec(static_cast<FastbuildExecNode const&>(*targetBase));
  1630. } else if (targetBase->Type == FastbuildTargetType::ALIAS) {
  1631. this->WriteAlias(static_cast<FastbuildAliasNode const&>(*targetBase));
  1632. } else if (targetBase->Type == FastbuildTargetType::LINK) {
  1633. auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
  1634. this->WriteTarget(target);
  1635. }
  1636. // Target end.
  1637. *BuildFileStream << "}\n";
  1638. }
  1639. if (!this->GetCMakeInstance()->GetIsInTryCompile()) {
  1640. if (!IDEProjects.empty()) {
  1641. this->WriteIDEProjects();
  1642. }
  1643. }
  1644. this->WriteTargetClean();
  1645. this->WriteTargetRebuildBFF();
  1646. }
  1647. std::string cmGlobalFastbuildGenerator::GetTargetName(
  1648. cmGeneratorTarget const* GeneratorTarget) const
  1649. {
  1650. std::string targetName =
  1651. GeneratorTarget->GetLocalGenerator()->GetCurrentBinaryDirectory();
  1652. targetName += "/";
  1653. targetName += GeneratorTarget->GetName();
  1654. targetName = this->ConvertToFastbuildPath(targetName);
  1655. return targetName;
  1656. }
  1657. cm::optional<FastbuildTarget>
  1658. cmGlobalFastbuildGenerator::GetTargetByOutputName(
  1659. std::string const& output) const
  1660. {
  1661. for (auto const& targetBase : FastbuildTargets) {
  1662. if (targetBase->Type == FastbuildTargetType::LINK) {
  1663. auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
  1664. if (std::any_of(target.LinkerNode.begin(), target.LinkerNode.end(),
  1665. [&output](FastbuildLinkerNode const& target_) {
  1666. return target_.LinkerOutput == output;
  1667. })) {
  1668. return target;
  1669. }
  1670. }
  1671. }
  1672. return cm::nullopt;
  1673. }
  1674. void cmGlobalFastbuildGenerator::AddIDEProject(FastbuildTarget const& target,
  1675. std::string const& config)
  1676. {
  1677. auto const& configs = GetConfigNames();
  1678. if (std::find(configs.begin(), configs.end(), config) == configs.end()) {
  1679. LogMessage("Config " + config + " doesn't exist, IDE projest for " +
  1680. target.Name + " won't be generated");
  1681. return;
  1682. }
  1683. auto& IDEProject = IDEProjects[target.BaseName];
  1684. auto const relativeSubdir = cmSystemTools::RelativePath(
  1685. this->GetCMakeInstance()->GetHomeDirectory(), target.BasePath);
  1686. // VS
  1687. auto& VSProject = IDEProject.first;
  1688. VSProject.Alias = target.BaseName + "-vcxproj";
  1689. VSProject.ProjectOutput = cmStrCat("VisualStudio/Projects/", relativeSubdir,
  1690. '/', target.BaseName + ".vcxproj");
  1691. VSProject.ProjectBasePath = target.BasePath;
  1692. VSProject.folder = relativeSubdir;
  1693. // XCode
  1694. auto& XCodeProject = IDEProject.second;
  1695. XCodeProject.Alias = target.BaseName + "-xcodeproj";
  1696. XCodeProject.ProjectOutput =
  1697. cmStrCat("XCode/Projects/", relativeSubdir, '/',
  1698. target.BaseName + ".xcodeproj/project.pbxproj");
  1699. XCodeProject.ProjectBasePath = target.BasePath;
  1700. IDEProjectConfig VSConfig;
  1701. VSConfig.Platform = "X64";
  1702. IDEProjectConfig XCodeConfig;
  1703. VSConfig.Target = XCodeConfig.Target = target.Name;
  1704. VSConfig.Config = XCodeConfig.Config = config.empty() ? "DEFAULT" : config;
  1705. VSProject.ProjectConfigs.emplace_back(std::move(VSConfig));
  1706. XCodeProject.ProjectConfigs.emplace_back(std::move(XCodeConfig));
  1707. }
  1708. bool cmGlobalFastbuildGenerator::IsExcluded(cmGeneratorTarget* target)
  1709. {
  1710. return cmGlobalGenerator::IsExcluded(LocalGenerators[0].get(), target);
  1711. }
  1712. std::vector<std::string> const& cmGlobalFastbuildGenerator::GetConfigNames()
  1713. const
  1714. {
  1715. return static_cast<cmLocalFastbuildGenerator const*>(
  1716. this->LocalGenerators.front().get())
  1717. ->GetConfigNames();
  1718. }
  1719. bool cmGlobalFastbuildGenerator::Open(std::string const& bindir,
  1720. std::string const& projectName,
  1721. bool dryRun)
  1722. {
  1723. #ifdef _WIN32
  1724. std::string sln = bindir + "/VisualStudio/" + projectName + ".sln";
  1725. if (dryRun) {
  1726. return cmSystemTools::FileExists(sln, true);
  1727. }
  1728. sln = cmSystemTools::ConvertToOutputPath(sln);
  1729. auto OpenSolution = [](std::string pathToSolution) {
  1730. HRESULT comInitialized =
  1731. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  1732. if (FAILED(comInitialized)) {
  1733. return false;
  1734. }
  1735. HINSTANCE hi = ShellExecuteA(NULL, "open", pathToSolution.c_str(), NULL,
  1736. NULL, SW_SHOWNORMAL);
  1737. CoUninitialize();
  1738. return reinterpret_cast<intptr_t>(hi) > 32;
  1739. };
  1740. return std::async(std::launch::async, OpenSolution, sln).get();
  1741. #else
  1742. return cmGlobalCommonGenerator::Open(bindir, projectName, dryRun);
  1743. #endif
  1744. }