cmLocalUnixMakefileGenerator3.cxx 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmLocalUnixMakefileGenerator3.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <functional>
  8. #include <sstream>
  9. #include <utility>
  10. #include <cm/memory>
  11. #include <cm/string_view>
  12. #include <cm/vector>
  13. #include <cmext/algorithm>
  14. #include <cmext/string_view>
  15. #include "cmsys/FStream.hxx"
  16. #include "cmsys/Terminal.h"
  17. #include "cmCMakePath.h"
  18. #include "cmCustomCommand.h" // IWYU pragma: keep
  19. #include "cmCustomCommandGenerator.h"
  20. #include "cmDependsCompiler.h"
  21. #include "cmFileTimeCache.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include "cmGeneratorExpression.h"
  24. #include "cmGeneratorTarget.h"
  25. #include "cmGlobalGenerator.h"
  26. #include "cmGlobalUnixMakefileGenerator3.h"
  27. #include "cmListFileCache.h"
  28. #include "cmLocalGenerator.h"
  29. #include "cmMakefile.h"
  30. #include "cmMakefileTargetGenerator.h"
  31. #include "cmOutputConverter.h"
  32. #include "cmRange.h"
  33. #include "cmRulePlaceholderExpander.h"
  34. #include "cmSourceFile.h"
  35. #include "cmState.h"
  36. #include "cmStateSnapshot.h"
  37. #include "cmStateTypes.h"
  38. #include "cmStringAlgorithms.h"
  39. #include "cmSystemTools.h"
  40. #include "cmTargetDepend.h"
  41. #include "cmValue.h"
  42. #include "cmVersion.h"
  43. #include "cmake.h"
  44. // Include dependency scanners for supported languages. Only the
  45. // C/C++ scanner is needed for bootstrapping CMake.
  46. #include "cmDependsC.h"
  47. #ifndef CMAKE_BOOTSTRAP
  48. # include "cmDependsFortran.h"
  49. # include "cmDependsJava.h"
  50. #endif
  51. namespace {
  52. // Helper function used below.
  53. std::string cmSplitExtension(std::string const& in, std::string& base)
  54. {
  55. std::string ext;
  56. std::string::size_type dot_pos = in.rfind('.');
  57. if (dot_pos != std::string::npos) {
  58. // Remove the extension first in case &base == &in.
  59. ext = in.substr(dot_pos);
  60. base = in.substr(0, dot_pos);
  61. } else {
  62. base = in;
  63. }
  64. return ext;
  65. }
  66. // Helper predicate for removing absolute paths that don't point to the
  67. // source or binary directory. It is used when CMAKE_DEPENDS_IN_PROJECT_ONLY
  68. // is set ON, to only consider in-project dependencies during the build.
  69. class NotInProjectDir
  70. {
  71. public:
  72. // Constructor with the source and binary directory's path
  73. NotInProjectDir(cm::string_view sourceDir, cm::string_view binaryDir)
  74. : SourceDir(sourceDir)
  75. , BinaryDir(binaryDir)
  76. {
  77. }
  78. // Operator evaluating the predicate
  79. bool operator()(const std::string& p) const
  80. {
  81. auto path = cmCMakePath(p).Normal();
  82. // Keep all relative paths:
  83. if (path.IsRelative()) {
  84. return false;
  85. }
  86. // If it's an absolute path, check if it starts with the source
  87. // directory:
  88. return !(cmCMakePath(this->SourceDir).IsPrefix(path) ||
  89. cmCMakePath(this->BinaryDir).IsPrefix(path));
  90. }
  91. private:
  92. // The path to the source directory
  93. cm::string_view SourceDir;
  94. // The path to the binary directory
  95. cm::string_view BinaryDir;
  96. };
  97. }
  98. cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3(
  99. cmGlobalGenerator* gg, cmMakefile* mf)
  100. : cmLocalCommonGenerator(gg, mf, WorkDir::CurBin)
  101. {
  102. this->MakefileVariableSize = 0;
  103. this->ColorMakefile = false;
  104. this->SkipPreprocessedSourceRules = false;
  105. this->SkipAssemblySourceRules = false;
  106. this->MakeCommandEscapeTargetTwice = false;
  107. this->BorlandMakeCurlyHack = false;
  108. }
  109. cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3() = default;
  110. std::string cmLocalUnixMakefileGenerator3::GetConfigName() const
  111. {
  112. auto const& configNames = this->GetConfigNames();
  113. assert(configNames.size() == 1);
  114. return configNames.front();
  115. }
  116. void cmLocalUnixMakefileGenerator3::Generate()
  117. {
  118. // Record whether some options are enabled to avoid checking many
  119. // times later.
  120. if (!this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) {
  121. if (this->Makefile->IsSet("CMAKE_COLOR_MAKEFILE")) {
  122. this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE");
  123. } else {
  124. this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_DIAGNOSTICS");
  125. }
  126. }
  127. this->SkipPreprocessedSourceRules =
  128. this->Makefile->IsOn("CMAKE_SKIP_PREPROCESSED_SOURCE_RULES");
  129. this->SkipAssemblySourceRules =
  130. this->Makefile->IsOn("CMAKE_SKIP_ASSEMBLY_SOURCE_RULES");
  131. // Generate the rule files for each target.
  132. cmGlobalUnixMakefileGenerator3* gg =
  133. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  134. for (cmGeneratorTarget* gt :
  135. this->GlobalGenerator->GetLocalGeneratorTargetsInOrder(this)) {
  136. if (!gt->IsInBuildSystem()) {
  137. continue;
  138. }
  139. auto& gtVisited = this->GetCommandsVisited(gt);
  140. const auto& deps = this->GlobalGenerator->GetTargetDirectDepends(gt);
  141. for (const auto& d : deps) {
  142. // Take the union of visited source files of custom commands
  143. auto depVisited = this->GetCommandsVisited(d);
  144. gtVisited.insert(depVisited.begin(), depVisited.end());
  145. }
  146. std::unique_ptr<cmMakefileTargetGenerator> tg(
  147. cmMakefileTargetGenerator::New(gt));
  148. if (tg) {
  149. tg->WriteRuleFiles();
  150. gg->RecordTargetProgress(tg.get());
  151. }
  152. }
  153. // write the local Makefile
  154. this->WriteLocalMakefile();
  155. // Write the cmake file with information for this directory.
  156. this->WriteDirectoryInformationFile();
  157. }
  158. void cmLocalUnixMakefileGenerator3::ComputeHomeRelativeOutputPath()
  159. {
  160. // Compute the path to use when referencing the current output
  161. // directory from the top output directory.
  162. this->HomeRelativeOutputPath =
  163. this->MaybeRelativeToTopBinDir(this->GetCurrentBinaryDirectory());
  164. if (this->HomeRelativeOutputPath == ".") {
  165. this->HomeRelativeOutputPath.clear();
  166. }
  167. if (!this->HomeRelativeOutputPath.empty()) {
  168. this->HomeRelativeOutputPath += "/";
  169. }
  170. }
  171. void cmLocalUnixMakefileGenerator3::GetLocalObjectFiles(
  172. std::map<std::string, LocalObjectInfo>& localObjectFiles)
  173. {
  174. for (const auto& gt : this->GetGeneratorTargets()) {
  175. if (!gt->CanCompileSources()) {
  176. continue;
  177. }
  178. std::vector<cmSourceFile const*> objectSources;
  179. gt->GetObjectSources(objectSources, this->GetConfigName());
  180. // Compute full path to object file directory for this target.
  181. std::string dir = cmStrCat(gt->LocalGenerator->GetCurrentBinaryDirectory(),
  182. '/', this->GetTargetDirectory(gt.get()), '/');
  183. // Compute the name of each object file.
  184. for (cmSourceFile const* sf : objectSources) {
  185. bool hasSourceExtension = true;
  186. std::string objectName =
  187. this->GetObjectFileNameWithoutTarget(*sf, dir, &hasSourceExtension);
  188. if (cmSystemTools::FileIsFullPath(objectName)) {
  189. objectName = cmSystemTools::GetFilenameName(objectName);
  190. }
  191. LocalObjectInfo& info = localObjectFiles[objectName];
  192. info.HasSourceExtension = hasSourceExtension;
  193. info.emplace_back(gt.get(), sf->GetLanguage());
  194. }
  195. }
  196. }
  197. void cmLocalUnixMakefileGenerator3::GetIndividualFileTargets(
  198. std::vector<std::string>& targets)
  199. {
  200. std::map<std::string, LocalObjectInfo> localObjectFiles;
  201. this->GetLocalObjectFiles(localObjectFiles);
  202. for (auto const& localObjectFile : localObjectFiles) {
  203. targets.push_back(localObjectFile.first);
  204. std::string::size_type dot_pos = localObjectFile.first.rfind(".");
  205. std::string base = localObjectFile.first.substr(0, dot_pos);
  206. if (localObjectFile.second.HasPreprocessRule) {
  207. targets.push_back(base + ".i");
  208. }
  209. if (localObjectFile.second.HasAssembleRule) {
  210. targets.push_back(base + ".s");
  211. }
  212. }
  213. }
  214. void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
  215. {
  216. // generate the includes
  217. std::string ruleFileName = "Makefile";
  218. // Open the rule file. This should be copy-if-different because the
  219. // rules may depend on this file itself.
  220. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  221. cmGeneratedFileStream ruleFileStream(
  222. ruleFileNameFull, false, this->GlobalGenerator->GetMakefileEncoding());
  223. if (!ruleFileStream) {
  224. return;
  225. }
  226. // always write the top makefile
  227. if (!this->IsRootMakefile()) {
  228. ruleFileStream.SetCopyIfDifferent(true);
  229. }
  230. // write the all rules
  231. this->WriteLocalAllRules(ruleFileStream);
  232. // only write local targets unless at the top Keep track of targets already
  233. // listed.
  234. std::set<std::string> emittedTargets;
  235. if (!this->IsRootMakefile()) {
  236. // write our targets, and while doing it collect up the object
  237. // file rules
  238. this->WriteLocalMakefileTargets(ruleFileStream, emittedTargets);
  239. } else {
  240. cmGlobalUnixMakefileGenerator3* gg =
  241. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  242. gg->WriteConvenienceRules(ruleFileStream, emittedTargets);
  243. }
  244. bool do_preprocess_rules = this->GetCreatePreprocessedSourceRules();
  245. bool do_assembly_rules = this->GetCreateAssemblySourceRules();
  246. std::map<std::string, LocalObjectInfo> localObjectFiles;
  247. this->GetLocalObjectFiles(localObjectFiles);
  248. // now write out the object rules
  249. // for each object file name
  250. for (auto& localObjectFile : localObjectFiles) {
  251. // Add a convenience rule for building the object file.
  252. this->WriteObjectConvenienceRule(
  253. ruleFileStream, "target to build an object file", localObjectFile.first,
  254. localObjectFile.second);
  255. // Check whether preprocessing and assembly rules make sense.
  256. // They make sense only for C and C++ sources.
  257. bool lang_has_preprocessor = false;
  258. bool lang_has_assembly = false;
  259. for (LocalObjectEntry const& entry : localObjectFile.second) {
  260. if (entry.Language == "C" || entry.Language == "CXX" ||
  261. entry.Language == "CUDA" || entry.Language == "Fortran" ||
  262. entry.Language == "HIP" || entry.Language == "ISPC") {
  263. // Right now, C, C++, CUDA, Fortran, HIP and ISPC have both a
  264. // preprocessor and the ability to generate assembly code
  265. lang_has_preprocessor = true;
  266. lang_has_assembly = true;
  267. break;
  268. }
  269. }
  270. // Add convenience rules for preprocessed and assembly files.
  271. if (lang_has_preprocessor && do_preprocess_rules) {
  272. std::string::size_type dot_pos = localObjectFile.first.rfind(".");
  273. std::string base = localObjectFile.first.substr(0, dot_pos);
  274. this->WriteObjectConvenienceRule(ruleFileStream,
  275. "target to preprocess a source file",
  276. (base + ".i"), localObjectFile.second);
  277. localObjectFile.second.HasPreprocessRule = true;
  278. }
  279. if (lang_has_assembly && do_assembly_rules) {
  280. std::string::size_type dot_pos = localObjectFile.first.rfind(".");
  281. std::string base = localObjectFile.first.substr(0, dot_pos);
  282. this->WriteObjectConvenienceRule(
  283. ruleFileStream, "target to generate assembly for a file",
  284. (base + ".s"), localObjectFile.second);
  285. localObjectFile.second.HasAssembleRule = true;
  286. }
  287. }
  288. // add a help target as long as there isn;t a real target named help
  289. if (emittedTargets.insert("help").second) {
  290. cmGlobalUnixMakefileGenerator3* gg =
  291. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  292. gg->WriteHelpRule(ruleFileStream, this);
  293. }
  294. this->WriteSpecialTargetsBottom(ruleFileStream);
  295. }
  296. void cmLocalUnixMakefileGenerator3::WriteObjectConvenienceRule(
  297. std::ostream& ruleFileStream, const char* comment, const std::string& output,
  298. LocalObjectInfo const& info)
  299. {
  300. // If the rule includes the source file extension then create a
  301. // version that has the extension removed. The help should include
  302. // only the version without source extension.
  303. bool inHelp = true;
  304. if (info.HasSourceExtension) {
  305. // Remove the last extension. This should be kept.
  306. std::string outBase1 = output;
  307. std::string outExt1 = cmSplitExtension(outBase1, outBase1);
  308. // Now remove the source extension and put back the last
  309. // extension.
  310. std::string outNoExt;
  311. cmSplitExtension(outBase1, outNoExt);
  312. outNoExt += outExt1;
  313. // Add a rule to drive the rule below.
  314. std::vector<std::string> depends;
  315. depends.emplace_back(output);
  316. std::vector<std::string> no_commands;
  317. this->WriteMakeRule(ruleFileStream, nullptr, outNoExt, depends,
  318. no_commands, true, true);
  319. inHelp = false;
  320. }
  321. // Recursively make the rule for each target using the object file.
  322. std::vector<std::string> commands;
  323. for (LocalObjectEntry const& t : info) {
  324. std::string tgtMakefileName = this->GetRelativeTargetDirectory(t.Target);
  325. std::string targetName = tgtMakefileName;
  326. tgtMakefileName += "/build.make";
  327. targetName += "/";
  328. targetName += output;
  329. commands.push_back(
  330. this->GetRecursiveMakeCall(tgtMakefileName, targetName));
  331. }
  332. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  333. this->GetCurrentBinaryDirectory());
  334. // Write the rule to the makefile.
  335. std::vector<std::string> no_depends;
  336. this->WriteMakeRule(ruleFileStream, comment, output, no_depends, commands,
  337. true, inHelp);
  338. }
  339. void cmLocalUnixMakefileGenerator3::WriteLocalMakefileTargets(
  340. std::ostream& ruleFileStream, std::set<std::string>& emitted)
  341. {
  342. std::vector<std::string> depends;
  343. std::vector<std::string> commands;
  344. // for each target we just provide a rule to cd up to the top and do a make
  345. // on the target
  346. std::string localName;
  347. for (const auto& target : this->GetGeneratorTargets()) {
  348. if ((target->GetType() == cmStateEnums::EXECUTABLE) ||
  349. (target->GetType() == cmStateEnums::STATIC_LIBRARY) ||
  350. (target->GetType() == cmStateEnums::SHARED_LIBRARY) ||
  351. (target->GetType() == cmStateEnums::MODULE_LIBRARY) ||
  352. (target->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
  353. (target->GetType() == cmStateEnums::UTILITY)) {
  354. emitted.insert(target->GetName());
  355. // for subdirs add a rule to build this specific target by name.
  356. localName =
  357. cmStrCat(this->GetRelativeTargetDirectory(target.get()), "/rule");
  358. commands.clear();
  359. depends.clear();
  360. // Build the target for this pass.
  361. std::string makefile2 = "CMakeFiles/Makefile2";
  362. commands.push_back(this->GetRecursiveMakeCall(makefile2, localName));
  363. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  364. this->GetCurrentBinaryDirectory());
  365. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  366. localName, depends, commands, true);
  367. // Add a target with the canonical name (no prefix, suffix or path).
  368. if (localName != target->GetName()) {
  369. commands.clear();
  370. depends.push_back(localName);
  371. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  372. target->GetName(), depends, commands, true);
  373. }
  374. // Add a fast rule to build the target
  375. std::string makefileName = cmStrCat(
  376. this->GetRelativeTargetDirectory(target.get()), "/build.make");
  377. // make sure the makefile name is suitable for a makefile
  378. std::string makeTargetName =
  379. cmStrCat(this->GetRelativeTargetDirectory(target.get()), "/build");
  380. localName = cmStrCat(target->GetName(), "/fast");
  381. depends.clear();
  382. commands.clear();
  383. commands.push_back(
  384. this->GetRecursiveMakeCall(makefileName, makeTargetName));
  385. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  386. this->GetCurrentBinaryDirectory());
  387. this->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  388. localName, depends, commands, true);
  389. // Add a local name for the rule to relink the target before
  390. // installation.
  391. if (target->NeedRelinkBeforeInstall(this->GetConfigName())) {
  392. makeTargetName = cmStrCat(
  393. this->GetRelativeTargetDirectory(target.get()), "/preinstall");
  394. localName = cmStrCat(target->GetName(), "/preinstall");
  395. depends.clear();
  396. commands.clear();
  397. commands.push_back(
  398. this->GetRecursiveMakeCall(makefile2, makeTargetName));
  399. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  400. this->GetCurrentBinaryDirectory());
  401. this->WriteMakeRule(ruleFileStream,
  402. "Manual pre-install relink rule for target.",
  403. localName, depends, commands, true);
  404. }
  405. }
  406. }
  407. }
  408. void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
  409. {
  410. std::string infoFileName =
  411. cmStrCat(this->GetCurrentBinaryDirectory(),
  412. "/CMakeFiles/CMakeDirectoryInformation.cmake");
  413. // Open the output file.
  414. cmGeneratedFileStream infoFileStream(infoFileName);
  415. if (!infoFileStream) {
  416. return;
  417. }
  418. infoFileStream.SetCopyIfDifferent(true);
  419. // Write the do not edit header.
  420. this->WriteDisclaimer(infoFileStream);
  421. // Setup relative path conversion tops.
  422. /* clang-format off */
  423. infoFileStream
  424. << "# Relative path conversion top directories.\n"
  425. << "set(CMAKE_RELATIVE_PATH_TOP_SOURCE \""
  426. << this->GetRelativePathTopSource() << "\")\n"
  427. << "set(CMAKE_RELATIVE_PATH_TOP_BINARY \""
  428. << this->GetRelativePathTopBinary() << "\")\n"
  429. << "\n";
  430. /* clang-format on */
  431. // Tell the dependency scanner to use unix paths if necessary.
  432. if (cmSystemTools::GetForceUnixPaths()) {
  433. /* clang-format off */
  434. infoFileStream
  435. << "# Force unix paths in dependencies.\n"
  436. << "set(CMAKE_FORCE_UNIX_PATHS 1)\n"
  437. << "\n";
  438. /* clang-format on */
  439. }
  440. // Store the include regular expressions for this directory.
  441. infoFileStream << "\n"
  442. << "# The C and CXX include file regular expressions for "
  443. << "this directory.\n";
  444. infoFileStream << "set(CMAKE_C_INCLUDE_REGEX_SCAN ";
  445. cmLocalUnixMakefileGenerator3::WriteCMakeArgument(
  446. infoFileStream, this->Makefile->GetIncludeRegularExpression());
  447. infoFileStream << ")\n";
  448. infoFileStream << "set(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
  449. cmLocalUnixMakefileGenerator3::WriteCMakeArgument(
  450. infoFileStream, this->Makefile->GetComplainRegularExpression());
  451. infoFileStream << ")\n";
  452. infoFileStream
  453. << "set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
  454. infoFileStream << "set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN "
  455. "${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
  456. }
  457. std::string cmLocalUnixMakefileGenerator3::ConvertToFullPath(
  458. const std::string& localPath)
  459. {
  460. std::string dir =
  461. cmStrCat(this->GetCurrentBinaryDirectory(), '/', localPath);
  462. return dir;
  463. }
  464. const std::string& cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
  465. {
  466. return this->HomeRelativeOutputPath;
  467. }
  468. std::string cmLocalUnixMakefileGenerator3::ConvertToMakefilePath(
  469. std::string const& path) const
  470. {
  471. cmGlobalUnixMakefileGenerator3* gg =
  472. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  473. return gg->ConvertToMakefilePath(path);
  474. }
  475. void cmLocalUnixMakefileGenerator3::WriteMakeRule(
  476. std::ostream& os, const char* comment, const std::string& target,
  477. const std::vector<std::string>& depends,
  478. const std::vector<std::string>& commands, bool symbolic, bool in_help)
  479. {
  480. // Make sure there is a target.
  481. if (target.empty()) {
  482. std::string err("No target for WriteMakeRule! called with comment: ");
  483. if (comment) {
  484. err += comment;
  485. }
  486. cmSystemTools::Error(err);
  487. return;
  488. }
  489. std::string replace;
  490. // Write the comment describing the rule in the makefile.
  491. if (comment) {
  492. replace = comment;
  493. std::string::size_type lpos = 0;
  494. std::string::size_type rpos;
  495. while ((rpos = replace.find('\n', lpos)) != std::string::npos) {
  496. os << "# " << replace.substr(lpos, rpos - lpos) << "\n";
  497. lpos = rpos + 1;
  498. }
  499. os << "# " << replace.substr(lpos) << "\n";
  500. }
  501. // Construct the left hand side of the rule.
  502. std::string tgt =
  503. this->ConvertToMakefilePath(this->MaybeRelativeToTopBinDir(target));
  504. const char* space = "";
  505. if (tgt.size() == 1) {
  506. // Add a space before the ":" to avoid drive letter confusion on
  507. // Windows.
  508. space = " ";
  509. }
  510. // Mark the rule as symbolic if requested.
  511. if (symbolic) {
  512. if (cmValue sym =
  513. this->Makefile->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE")) {
  514. os << tgt << space << ": " << *sym << "\n";
  515. }
  516. }
  517. // Write the rule.
  518. if (depends.empty()) {
  519. // No dependencies. The commands will always run.
  520. os << tgt << space << ":\n";
  521. } else {
  522. // Split dependencies into multiple rule lines. This allows for
  523. // very long dependency lists even on older make implementations.
  524. for (std::string const& depend : depends) {
  525. os << tgt << space << ": "
  526. << this->ConvertToMakefilePath(this->MaybeRelativeToTopBinDir(depend))
  527. << '\n';
  528. }
  529. }
  530. if (!commands.empty()) {
  531. // Write the list of commands.
  532. os << cmWrap("\t", commands, "", "\n") << "\n";
  533. }
  534. if (symbolic && !this->IsWatcomWMake()) {
  535. os << ".PHONY : " << tgt << "\n";
  536. }
  537. os << "\n";
  538. // Add the output to the local help if requested.
  539. if (in_help) {
  540. this->LocalHelp.push_back(target);
  541. }
  542. }
  543. std::string cmLocalUnixMakefileGenerator3::MaybeConvertWatcomShellCommand(
  544. std::string const& cmd)
  545. {
  546. if (this->IsWatcomWMake() && cmSystemTools::FileIsFullPath(cmd) &&
  547. cmd.find_first_of("( )") != std::string::npos) {
  548. // On Watcom WMake use the windows short path for the command
  549. // name. This is needed to avoid funny quoting problems on
  550. // lines with shell redirection operators.
  551. std::string scmd;
  552. if (cmSystemTools::GetShortPath(cmd, scmd)) {
  553. return this->ConvertToOutputFormat(scmd, cmOutputConverter::SHELL);
  554. }
  555. }
  556. return std::string();
  557. }
  558. void cmLocalUnixMakefileGenerator3::WriteMakeVariables(
  559. std::ostream& makefileStream)
  560. {
  561. this->WriteDivider(makefileStream);
  562. makefileStream << "# Set environment variables for the build.\n"
  563. << "\n";
  564. cmGlobalUnixMakefileGenerator3* gg =
  565. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  566. if (gg->DefineWindowsNULL) {
  567. makefileStream << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  568. << "NULL=\n"
  569. << "!ELSE\n"
  570. << "NULL=nul\n"
  571. << "!ENDIF\n";
  572. }
  573. if (this->IsWindowsShell()) {
  574. makefileStream << "SHELL = cmd.exe\n"
  575. << "\n";
  576. } else {
  577. #if !defined(__VMS)
  578. /* clang-format off */
  579. makefileStream
  580. << "# The shell in which to execute make rules.\n"
  581. << "SHELL = /bin/sh\n"
  582. << "\n";
  583. /* clang-format on */
  584. #endif
  585. }
  586. std::string cmakeShellCommand =
  587. this->MaybeConvertWatcomShellCommand(cmSystemTools::GetCMakeCommand());
  588. if (cmakeShellCommand.empty()) {
  589. cmakeShellCommand = this->ConvertToOutputFormat(
  590. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  591. }
  592. /* clang-format off */
  593. makefileStream
  594. << "# The CMake executable.\n"
  595. << "CMAKE_COMMAND = "
  596. << cmakeShellCommand
  597. << "\n"
  598. << "\n";
  599. makefileStream
  600. << "# The command to remove a file.\n"
  601. << "RM = "
  602. << cmakeShellCommand
  603. << " -E rm -f\n"
  604. << "\n";
  605. makefileStream
  606. << "# Escaping for special characters.\n"
  607. << "EQUALS = =\n"
  608. << "\n";
  609. makefileStream
  610. << "# The top-level source directory on which CMake was run.\n"
  611. << "CMAKE_SOURCE_DIR = "
  612. << this->ConvertToOutputFormat(
  613. this->GetSourceDirectory(), cmOutputConverter::SHELL)
  614. << "\n"
  615. << "\n";
  616. makefileStream
  617. << "# The top-level build directory on which CMake was run.\n"
  618. << "CMAKE_BINARY_DIR = "
  619. << this->ConvertToOutputFormat(
  620. this->GetBinaryDirectory(), cmOutputConverter::SHELL)
  621. << "\n"
  622. << "\n";
  623. /* clang-format on */
  624. }
  625. void cmLocalUnixMakefileGenerator3::WriteSpecialTargetsTop(
  626. std::ostream& makefileStream)
  627. {
  628. this->WriteDivider(makefileStream);
  629. makefileStream << "# Special targets provided by cmake.\n"
  630. << "\n";
  631. std::vector<std::string> no_commands;
  632. std::vector<std::string> no_depends;
  633. // Special target to cleanup operation of make tool.
  634. // This should be the first target except for the default_target in
  635. // the interface Makefile.
  636. this->WriteMakeRule(makefileStream,
  637. "Disable implicit rules so canonical targets will work.",
  638. ".SUFFIXES", no_depends, no_commands, false);
  639. if (!this->IsNMake() && !this->IsWatcomWMake() &&
  640. !this->BorlandMakeCurlyHack) {
  641. // turn off RCS and SCCS automatic stuff from gmake
  642. constexpr const char* vcs_rules[] = {
  643. "%,v", "RCS/%", "RCS/%,v", "SCCS/s.%", "s.%",
  644. };
  645. for (const auto* vcs_rule : vcs_rules) {
  646. std::vector<std::string> vcs_depend;
  647. vcs_depend.emplace_back(vcs_rule);
  648. this->WriteMakeRule(makefileStream, "Disable VCS-based implicit rules.",
  649. "%", vcs_depend, no_commands, false);
  650. }
  651. }
  652. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  653. std::vector<std::string> depends;
  654. depends.emplace_back(".hpux_make_needs_suffix_list");
  655. this->WriteMakeRule(makefileStream, nullptr, ".SUFFIXES", depends,
  656. no_commands, false);
  657. if (this->IsWatcomWMake()) {
  658. // Switch on WMake feature, if an error or interrupt occurs during
  659. // makefile processing, the current target being made may be deleted
  660. // without prompting (the same as command line -e option).
  661. /* clang-format off */
  662. makefileStream <<
  663. "\n"
  664. ".ERASE\n"
  665. "\n"
  666. ;
  667. /* clang-format on */
  668. }
  669. if (this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE")) {
  670. /* clang-format off */
  671. makefileStream
  672. << "# Produce verbose output by default.\n"
  673. << "VERBOSE = 1\n"
  674. << "\n";
  675. /* clang-format on */
  676. }
  677. if (this->IsWatcomWMake()) {
  678. /* clang-format off */
  679. makefileStream <<
  680. "!ifndef VERBOSE\n"
  681. ".SILENT\n"
  682. "!endif\n"
  683. "\n"
  684. ;
  685. /* clang-format on */
  686. } else {
  687. makefileStream << "# Command-line flag to silence nested $(MAKE).\n"
  688. "$(VERBOSE)MAKESILENT = -s\n"
  689. "\n";
  690. // Write special target to silence make output. This must be after
  691. // the default target in case VERBOSE is set (which changes the
  692. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  693. // "VERBOSE=1" to be added as a make variable which will change the
  694. // name of this special target. This gives a make-time choice to
  695. // the user.
  696. // Write directly to the stream since WriteMakeRule escapes '$'.
  697. makefileStream << "#Suppress display of executed commands.\n"
  698. "$(VERBOSE).SILENT:\n"
  699. "\n";
  700. }
  701. // Work-around for makes that drop rules that have no dependencies
  702. // or commands.
  703. cmGlobalUnixMakefileGenerator3* gg =
  704. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  705. std::string hack = gg->GetEmptyRuleHackDepends();
  706. if (!hack.empty()) {
  707. no_depends.push_back(std::move(hack));
  708. }
  709. std::string hack_cmd = gg->GetEmptyRuleHackCommand();
  710. if (!hack_cmd.empty()) {
  711. no_commands.push_back(std::move(hack_cmd));
  712. }
  713. // Special symbolic target that never exists to force dependers to
  714. // run their rules.
  715. this->WriteMakeRule(makefileStream, "A target that is always out of date.",
  716. "cmake_force", no_depends, no_commands, true);
  717. // Variables for reference by other rules.
  718. this->WriteMakeVariables(makefileStream);
  719. }
  720. void cmLocalUnixMakefileGenerator3::WriteSpecialTargetsBottom(
  721. std::ostream& makefileStream)
  722. {
  723. this->WriteDivider(makefileStream);
  724. makefileStream << "# Special targets to cleanup operation of make.\n"
  725. << "\n";
  726. // Write special "cmake_check_build_system" target to run cmake with
  727. // the --check-build-system flag.
  728. if (!this->GlobalGenerator->GlobalSettingIsOn(
  729. "CMAKE_SUPPRESS_REGENERATION")) {
  730. // Build command to run CMake to check if anything needs regenerating.
  731. std::vector<std::string> commands;
  732. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  733. if (cm->DoWriteGlobVerifyTarget()) {
  734. std::string rescanRule =
  735. cmStrCat("$(CMAKE_COMMAND) -P ",
  736. this->ConvertToOutputFormat(cm->GetGlobVerifyScript(),
  737. cmOutputConverter::SHELL));
  738. commands.push_back(rescanRule);
  739. }
  740. std::string cmakefileName = "CMakeFiles/Makefile.cmake";
  741. std::string runRule = cmStrCat(
  742. "$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) "
  743. "--check-build-system ",
  744. this->ConvertToOutputFormat(cmakefileName, cmOutputConverter::SHELL),
  745. " 0");
  746. std::vector<std::string> no_depends;
  747. commands.push_back(std::move(runRule));
  748. if (!this->IsRootMakefile()) {
  749. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  750. this->GetCurrentBinaryDirectory());
  751. }
  752. this->WriteMakeRule(makefileStream,
  753. "Special rule to run CMake to check the build system "
  754. "integrity.\n"
  755. "No rule that depends on this can have "
  756. "commands that come from listfiles\n"
  757. "because they might be regenerated.",
  758. "cmake_check_build_system", no_depends, commands,
  759. true);
  760. }
  761. }
  762. void cmLocalUnixMakefileGenerator3::WriteConvenienceRule(
  763. std::ostream& ruleFileStream, const std::string& realTarget,
  764. const std::string& helpTarget)
  765. {
  766. // A rule is only needed if the names are different.
  767. if (realTarget != helpTarget) {
  768. // The helper target depends on the real target.
  769. std::vector<std::string> depends;
  770. depends.push_back(realTarget);
  771. // There are no commands.
  772. std::vector<std::string> no_commands;
  773. // Write the rule.
  774. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  775. helpTarget, depends, no_commands, true);
  776. }
  777. }
  778. std::string cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(
  779. cmGeneratorTarget const* target) const
  780. {
  781. std::string dir =
  782. cmStrCat(this->HomeRelativeOutputPath, this->GetTargetDirectory(target));
  783. return dir;
  784. }
  785. void cmLocalUnixMakefileGenerator3::AppendFlags(
  786. std::string& flags, const std::string& newFlags) const
  787. {
  788. if (this->IsWatcomWMake() && !newFlags.empty()) {
  789. std::string newf = newFlags;
  790. if (newf.find("\\\"") != std::string::npos) {
  791. cmSystemTools::ReplaceString(newf, "\\\"", "\"");
  792. this->cmLocalGenerator::AppendFlags(flags, newf);
  793. return;
  794. }
  795. }
  796. this->cmLocalGenerator::AppendFlags(flags, newFlags);
  797. }
  798. void cmLocalUnixMakefileGenerator3::AppendRuleDepend(
  799. std::vector<std::string>& depends, const char* ruleFileName)
  800. {
  801. // Add a dependency on the rule file itself unless an option to skip
  802. // it is specifically enabled by the user or project.
  803. cmValue nodep = this->Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  804. if (cmIsOff(nodep)) {
  805. depends.emplace_back(ruleFileName);
  806. }
  807. }
  808. void cmLocalUnixMakefileGenerator3::AppendRuleDepends(
  809. std::vector<std::string>& depends, std::vector<std::string> const& ruleFiles)
  810. {
  811. // Add a dependency on the rule file itself unless an option to skip
  812. // it is specifically enabled by the user or project.
  813. if (!this->Makefile->IsOn("CMAKE_SKIP_RULE_DEPENDENCY")) {
  814. cm::append(depends, ruleFiles);
  815. }
  816. }
  817. void cmLocalUnixMakefileGenerator3::AppendCustomDepends(
  818. std::vector<std::string>& depends, const std::vector<cmCustomCommand>& ccs)
  819. {
  820. for (cmCustomCommand const& cc : ccs) {
  821. cmCustomCommandGenerator ccg(cc, this->GetConfigName(), this);
  822. this->AppendCustomDepend(depends, ccg);
  823. }
  824. }
  825. void cmLocalUnixMakefileGenerator3::AppendCustomDepend(
  826. std::vector<std::string>& depends, cmCustomCommandGenerator const& ccg)
  827. {
  828. for (std::string const& d : ccg.GetDepends()) {
  829. // Lookup the real name of the dependency in case it is a CMake target.
  830. std::string dep;
  831. if (this->GetRealDependency(d, this->GetConfigName(), dep)) {
  832. depends.push_back(std::move(dep));
  833. }
  834. }
  835. }
  836. void cmLocalUnixMakefileGenerator3::AppendCustomCommands(
  837. std::vector<std::string>& commands, const std::vector<cmCustomCommand>& ccs,
  838. cmGeneratorTarget* target, std::string const& relative)
  839. {
  840. for (cmCustomCommand const& cc : ccs) {
  841. cmCustomCommandGenerator ccg(cc, this->GetConfigName(), this);
  842. this->AppendCustomCommand(commands, ccg, target, relative, true);
  843. }
  844. }
  845. void cmLocalUnixMakefileGenerator3::AppendCustomCommand(
  846. std::vector<std::string>& commands, cmCustomCommandGenerator const& ccg,
  847. cmGeneratorTarget* target, std::string const& relative, bool echo_comment,
  848. std::ostream* content)
  849. {
  850. // Optionally create a command to display the custom command's
  851. // comment text. This is used for pre-build, pre-link, and
  852. // post-build command comments. Custom build step commands have
  853. // their comments generated elsewhere.
  854. if (echo_comment) {
  855. const char* comment = ccg.GetComment();
  856. if (comment && *comment) {
  857. this->AppendEcho(commands, comment,
  858. cmLocalUnixMakefileGenerator3::EchoGenerate);
  859. }
  860. }
  861. // if the command specified a working directory use it.
  862. std::string dir = this->GetCurrentBinaryDirectory();
  863. std::string workingDir = ccg.GetWorkingDirectory();
  864. if (!workingDir.empty()) {
  865. dir = workingDir;
  866. }
  867. if (content) {
  868. *content << dir;
  869. }
  870. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  871. this->CreateRulePlaceholderExpander());
  872. // Add each command line to the set of commands.
  873. std::vector<std::string> commands1;
  874. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  875. // Build the command line in a single string.
  876. std::string cmd = ccg.GetCommand(c);
  877. if (!cmd.empty()) {
  878. // Use "call " before any invocations of .bat or .cmd files
  879. // invoked as custom commands in the WindowsShell.
  880. //
  881. bool useCall = false;
  882. if (this->IsWindowsShell()) {
  883. std::string suffix;
  884. if (cmd.size() > 4) {
  885. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
  886. if (suffix == ".bat" || suffix == ".cmd") {
  887. useCall = true;
  888. }
  889. }
  890. }
  891. cmSystemTools::ReplaceString(cmd, "/./", "/");
  892. // Convert the command to a relative path only if the current
  893. // working directory will be the start-output directory.
  894. bool had_slash = cmd.find('/') != std::string::npos;
  895. if (workingDir.empty()) {
  896. cmd = this->MaybeRelativeToCurBinDir(cmd);
  897. }
  898. bool has_slash = cmd.find('/') != std::string::npos;
  899. if (had_slash && !has_slash) {
  900. // This command was specified as a path to a file in the
  901. // current directory. Add a leading "./" so it can run
  902. // without the current directory being in the search path.
  903. cmd = cmStrCat("./", cmd);
  904. }
  905. std::string launcher;
  906. // Short-circuit if there is no launcher.
  907. cmValue val = this->GetRuleLauncher(target, "RULE_LAUNCH_CUSTOM");
  908. if (cmNonempty(val)) {
  909. // Expand rule variables referenced in the given launcher command.
  910. cmRulePlaceholderExpander::RuleVariables vars;
  911. vars.CMTargetName = target->GetName().c_str();
  912. vars.CMTargetType =
  913. cmState::GetTargetTypeName(target->GetType()).c_str();
  914. std::string output;
  915. const std::vector<std::string>& outputs = ccg.GetOutputs();
  916. if (!outputs.empty()) {
  917. output = outputs[0];
  918. if (workingDir.empty()) {
  919. output = this->MaybeRelativeToCurBinDir(output);
  920. }
  921. output =
  922. this->ConvertToOutputFormat(output, cmOutputConverter::SHELL);
  923. }
  924. vars.Output = output.c_str();
  925. launcher = *val;
  926. rulePlaceholderExpander->ExpandRuleVariables(this, launcher, vars);
  927. if (!launcher.empty()) {
  928. launcher += " ";
  929. }
  930. }
  931. std::string shellCommand = this->MaybeConvertWatcomShellCommand(cmd);
  932. if (shellCommand.empty()) {
  933. shellCommand =
  934. this->ConvertToOutputFormat(cmd, cmOutputConverter::SHELL);
  935. }
  936. cmd = launcher + shellCommand;
  937. ccg.AppendArguments(c, cmd);
  938. if (content) {
  939. // Rule content does not include the launcher.
  940. *content << (cmd.c_str() + launcher.size());
  941. }
  942. if (this->BorlandMakeCurlyHack) {
  943. // Borland Make has a very strange bug. If the first curly
  944. // brace anywhere in the command string is a left curly, it
  945. // must be written {{} instead of just {. Otherwise some
  946. // curly braces are removed. The hack can be skipped if the
  947. // first curly brace is the last character.
  948. std::string::size_type lcurly = cmd.find('{');
  949. if (lcurly != std::string::npos && lcurly < (cmd.size() - 1)) {
  950. std::string::size_type rcurly = cmd.find('}');
  951. if (rcurly == std::string::npos || rcurly > lcurly) {
  952. // The first curly is a left curly. Use the hack.
  953. cmd =
  954. cmStrCat(cmd.substr(0, lcurly), "{{}", cmd.substr(lcurly + 1));
  955. }
  956. }
  957. }
  958. if (launcher.empty()) {
  959. if (useCall) {
  960. cmd = cmStrCat("call ", cmd);
  961. } else if (this->IsNMake() && cmd[0] == '"') {
  962. cmd = cmStrCat("echo >nul && ", cmd);
  963. }
  964. }
  965. commands1.push_back(std::move(cmd));
  966. }
  967. }
  968. // Setup the proper working directory for the commands.
  969. this->CreateCDCommand(commands1, dir, relative);
  970. // push back the custom commands
  971. cm::append(commands, commands1);
  972. }
  973. void cmLocalUnixMakefileGenerator3::AppendCleanCommand(
  974. std::vector<std::string>& commands, const std::set<std::string>& files,
  975. cmGeneratorTarget* target, const char* filename)
  976. {
  977. std::string currentBinDir = this->GetCurrentBinaryDirectory();
  978. std::string cleanfile = cmStrCat(
  979. currentBinDir, '/', this->GetTargetDirectory(target), "/cmake_clean");
  980. if (filename) {
  981. cleanfile += "_";
  982. cleanfile += filename;
  983. }
  984. cleanfile += ".cmake";
  985. cmsys::ofstream fout(cleanfile.c_str());
  986. if (!fout) {
  987. cmSystemTools::Error("Could not create " + cleanfile);
  988. }
  989. if (!files.empty()) {
  990. fout << "file(REMOVE_RECURSE\n";
  991. for (std::string const& file : files) {
  992. std::string fc = this->MaybeRelativeToCurBinDir(file);
  993. fout << " " << cmOutputConverter::EscapeForCMake(fc) << "\n";
  994. }
  995. fout << ")\n";
  996. }
  997. {
  998. std::string remove = cmStrCat(
  999. "$(CMAKE_COMMAND) -P ",
  1000. this->ConvertToOutputFormat(this->MaybeRelativeToCurBinDir(cleanfile),
  1001. cmOutputConverter::SHELL));
  1002. commands.push_back(std::move(remove));
  1003. }
  1004. // For the main clean rule add per-language cleaning.
  1005. if (!filename) {
  1006. // Get the set of source languages in the target.
  1007. std::set<std::string> languages;
  1008. target->GetLanguages(
  1009. languages, this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1010. /* clang-format off */
  1011. fout << "\n"
  1012. << "# Per-language clean rules from dependency scanning.\n"
  1013. << "foreach(lang " << cmJoin(languages, " ") << ")\n"
  1014. << " include(" << this->GetTargetDirectory(target)
  1015. << "/cmake_clean_${lang}.cmake OPTIONAL)\n"
  1016. << "endforeach()\n";
  1017. /* clang-format on */
  1018. }
  1019. }
  1020. void cmLocalUnixMakefileGenerator3::AppendDirectoryCleanCommand(
  1021. std::vector<std::string>& commands)
  1022. {
  1023. std::vector<std::string> cleanFiles;
  1024. // Look for additional files registered for cleaning in this directory.
  1025. if (cmValue prop_value =
  1026. this->Makefile->GetProperty("ADDITIONAL_CLEAN_FILES")) {
  1027. cmExpandList(cmGeneratorExpression::Evaluate(
  1028. *prop_value, this,
  1029. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE")),
  1030. cleanFiles);
  1031. }
  1032. if (cleanFiles.empty()) {
  1033. return;
  1034. }
  1035. const auto& rootLG = this->GetGlobalGenerator()->GetLocalGenerators().at(0);
  1036. std::string const& currentBinaryDir = this->GetCurrentBinaryDirectory();
  1037. std::string cleanfile =
  1038. cmStrCat(currentBinaryDir, "/CMakeFiles/cmake_directory_clean.cmake");
  1039. // Write clean script
  1040. {
  1041. cmsys::ofstream fout(cleanfile.c_str());
  1042. if (!fout) {
  1043. cmSystemTools::Error("Could not create " + cleanfile);
  1044. return;
  1045. }
  1046. fout << "file(REMOVE_RECURSE\n";
  1047. for (std::string const& cfl : cleanFiles) {
  1048. std::string fc = rootLG->MaybeRelativeToCurBinDir(
  1049. cmSystemTools::CollapseFullPath(cfl, currentBinaryDir));
  1050. fout << " " << cmOutputConverter::EscapeForCMake(fc) << "\n";
  1051. }
  1052. fout << ")\n";
  1053. }
  1054. // Create command
  1055. {
  1056. std::string remove = cmStrCat(
  1057. "$(CMAKE_COMMAND) -P ",
  1058. this->ConvertToOutputFormat(rootLG->MaybeRelativeToCurBinDir(cleanfile),
  1059. cmOutputConverter::SHELL));
  1060. commands.push_back(std::move(remove));
  1061. }
  1062. }
  1063. void cmLocalUnixMakefileGenerator3::AppendEcho(
  1064. std::vector<std::string>& commands, std::string const& text, EchoColor color,
  1065. EchoProgress const* progress)
  1066. {
  1067. // Choose the color for the text.
  1068. std::string color_name;
  1069. if (this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile) {
  1070. // See cmake::ExecuteEchoColor in cmake.cxx for these options.
  1071. // This color set is readable on both black and white backgrounds.
  1072. switch (color) {
  1073. case EchoNormal:
  1074. break;
  1075. case EchoDepend:
  1076. color_name = "--magenta --bold ";
  1077. break;
  1078. case EchoBuild:
  1079. color_name = "--green ";
  1080. break;
  1081. case EchoLink:
  1082. color_name = "--green --bold ";
  1083. break;
  1084. case EchoGenerate:
  1085. color_name = "--blue --bold ";
  1086. break;
  1087. case EchoGlobal:
  1088. color_name = "--cyan ";
  1089. break;
  1090. }
  1091. }
  1092. // Echo one line at a time.
  1093. std::string line;
  1094. line.reserve(200);
  1095. for (const char* c = text.c_str();; ++c) {
  1096. if (*c == '\n' || *c == '\0') {
  1097. // Avoid writing a blank last line on end-of-string.
  1098. if (*c != '\0' || !line.empty()) {
  1099. // Add a command to echo this line.
  1100. std::string cmd;
  1101. if (color_name.empty() && !progress) {
  1102. // Use the native echo command.
  1103. cmd = cmStrCat("@echo ", this->EscapeForShell(line, false, true));
  1104. } else {
  1105. // Use cmake to echo the text in color.
  1106. cmd = cmStrCat(
  1107. "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ",
  1108. color_name);
  1109. if (progress) {
  1110. cmd += "--progress-dir=";
  1111. cmd += this->ConvertToOutputFormat(progress->Dir,
  1112. cmOutputConverter::SHELL);
  1113. cmd += " ";
  1114. cmd += "--progress-num=";
  1115. cmd += progress->Arg;
  1116. cmd += " ";
  1117. }
  1118. cmd += this->EscapeForShell(line);
  1119. }
  1120. commands.push_back(std::move(cmd));
  1121. }
  1122. // Reset the line to empty.
  1123. line.clear();
  1124. // Progress appears only on first line.
  1125. progress = nullptr;
  1126. // Terminate on end-of-string.
  1127. if (*c == '\0') {
  1128. return;
  1129. }
  1130. } else if (*c != '\r') {
  1131. // Append this character to the current line.
  1132. line += *c;
  1133. }
  1134. }
  1135. }
  1136. std::string cmLocalUnixMakefileGenerator3::CreateMakeVariable(
  1137. std::string const& s, std::string const& s2)
  1138. {
  1139. std::string unmodified = cmStrCat(s, s2);
  1140. // if there is no restriction on the length of make variables
  1141. // and there are no "." characters in the string, then return the
  1142. // unmodified combination.
  1143. if ((!this->MakefileVariableSize &&
  1144. unmodified.find('.') == std::string::npos) &&
  1145. (!this->MakefileVariableSize &&
  1146. unmodified.find('+') == std::string::npos) &&
  1147. (!this->MakefileVariableSize &&
  1148. unmodified.find('-') == std::string::npos)) {
  1149. return unmodified;
  1150. }
  1151. // see if the variable has been defined before and return
  1152. // the modified version of the variable
  1153. auto i = this->MakeVariableMap.find(unmodified);
  1154. if (i != this->MakeVariableMap.end()) {
  1155. return i->second;
  1156. }
  1157. // start with the unmodified variable
  1158. std::string ret = unmodified;
  1159. // if this there is no value for this->MakefileVariableSize then
  1160. // the string must have bad characters in it
  1161. if (!this->MakefileVariableSize) {
  1162. std::replace(ret.begin(), ret.end(), '.', '_');
  1163. cmSystemTools::ReplaceString(ret, "-", "__");
  1164. cmSystemTools::ReplaceString(ret, "+", "___");
  1165. int ni = 0;
  1166. char buffer[12];
  1167. // make sure the _ version is not already used, if
  1168. // it is used then add number to the end of the variable
  1169. while (this->ShortMakeVariableMap.count(ret) && ni < 1000) {
  1170. ++ni;
  1171. snprintf(buffer, sizeof(buffer), "%04d", ni);
  1172. ret = unmodified + buffer;
  1173. }
  1174. this->ShortMakeVariableMap[ret] = "1";
  1175. this->MakeVariableMap[unmodified] = ret;
  1176. return ret;
  1177. }
  1178. // if the string is greater than 32 chars it is an invalid variable name
  1179. // for borland make
  1180. if (static_cast<int>(ret.size()) > this->MakefileVariableSize) {
  1181. int keep = this->MakefileVariableSize - 8;
  1182. int size = keep + 3;
  1183. std::string str1 = s;
  1184. std::string str2 = s2;
  1185. // we must shorten the combined string by 4 characters
  1186. // keep no more than 24 characters from the second string
  1187. if (static_cast<int>(str2.size()) > keep) {
  1188. str2 = str2.substr(0, keep);
  1189. }
  1190. if (static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size) {
  1191. str1 = str1.substr(0, size - str2.size());
  1192. }
  1193. char buffer[12];
  1194. int ni = 0;
  1195. snprintf(buffer, sizeof(buffer), "%04d", ni);
  1196. ret = str1 + str2 + buffer;
  1197. while (this->ShortMakeVariableMap.count(ret) && ni < 1000) {
  1198. ++ni;
  1199. snprintf(buffer, sizeof(buffer), "%04d", ni);
  1200. ret = str1 + str2 + buffer;
  1201. }
  1202. if (ni == 1000) {
  1203. cmSystemTools::Error("Borland makefile variable length too long");
  1204. return unmodified;
  1205. }
  1206. // once an unused variable is found
  1207. this->ShortMakeVariableMap[ret] = "1";
  1208. }
  1209. // always make an entry into the unmodified to variable map
  1210. this->MakeVariableMap[unmodified] = ret;
  1211. return ret;
  1212. }
  1213. bool cmLocalUnixMakefileGenerator3::UpdateDependencies(
  1214. const std::string& tgtInfo, bool verbose, bool color)
  1215. {
  1216. // read in the target info file
  1217. if (!this->Makefile->ReadListFile(tgtInfo) ||
  1218. cmSystemTools::GetErrorOccurredFlag()) {
  1219. cmSystemTools::Error("Target DependInfo.cmake file not found");
  1220. }
  1221. bool status = true;
  1222. // Check if any multiple output pairs have a missing file.
  1223. this->CheckMultipleOutputs(verbose);
  1224. std::string const targetDir = cmSystemTools::GetFilenamePath(tgtInfo);
  1225. if (!this->Makefile->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES").empty()) {
  1226. // dependencies are managed by CMake itself
  1227. std::string const internalDependFile = targetDir + "/depend.internal";
  1228. std::string const dependFile = targetDir + "/depend.make";
  1229. // If the target DependInfo.cmake file has changed since the last
  1230. // time dependencies were scanned then force rescanning. This may
  1231. // happen when a new source file is added and CMake regenerates the
  1232. // project but no other sources were touched.
  1233. bool needRescanDependInfo = false;
  1234. cmFileTimeCache* ftc =
  1235. this->GlobalGenerator->GetCMakeInstance()->GetFileTimeCache();
  1236. {
  1237. int result;
  1238. if (!ftc->Compare(internalDependFile, tgtInfo, &result) || result < 0) {
  1239. if (verbose) {
  1240. cmSystemTools::Stdout(cmStrCat("Dependee \"", tgtInfo,
  1241. "\" is newer than depender \"",
  1242. internalDependFile, "\".\n"));
  1243. }
  1244. needRescanDependInfo = true;
  1245. }
  1246. }
  1247. // If the directory information is newer than depend.internal, include
  1248. // dirs may have changed. In this case discard all old dependencies.
  1249. bool needRescanDirInfo = false;
  1250. {
  1251. std::string dirInfoFile =
  1252. cmStrCat(this->GetCurrentBinaryDirectory(),
  1253. "/CMakeFiles/CMakeDirectoryInformation.cmake");
  1254. int result;
  1255. if (!ftc->Compare(internalDependFile, dirInfoFile, &result) ||
  1256. result < 0) {
  1257. if (verbose) {
  1258. cmSystemTools::Stdout(cmStrCat("Dependee \"", dirInfoFile,
  1259. "\" is newer than depender \"",
  1260. internalDependFile, "\".\n"));
  1261. }
  1262. needRescanDirInfo = true;
  1263. }
  1264. }
  1265. // Check the implicit dependencies to see if they are up to date.
  1266. // The build.make file may have explicit dependencies for the object
  1267. // files but these will not affect the scanning process so they need
  1268. // not be considered.
  1269. cmDepends::DependencyMap validDependencies;
  1270. bool needRescanDependencies = false;
  1271. if (!needRescanDirInfo) {
  1272. cmDependsC checker;
  1273. checker.SetVerbose(verbose);
  1274. checker.SetFileTimeCache(ftc);
  1275. // cmDependsC::Check() fills the vector validDependencies() with the
  1276. // dependencies for those files where they are still valid, i.e.
  1277. // neither the files themselves nor any files they depend on have
  1278. // changed. We don't do that if the CMakeDirectoryInformation.cmake
  1279. // file has changed, because then potentially all dependencies have
  1280. // changed. This information is given later on to cmDependsC, which
  1281. // then only rescans the files where it did not get valid dependencies
  1282. // via this dependency vector. This means that in the normal case, when
  1283. // only few or one file have been edited, then also only this one file
  1284. // is actually scanned again, instead of all files for this target.
  1285. needRescanDependencies =
  1286. !checker.Check(dependFile, internalDependFile, validDependencies);
  1287. }
  1288. if (needRescanDependInfo || needRescanDirInfo || needRescanDependencies) {
  1289. // The dependencies must be regenerated.
  1290. if (verbose) {
  1291. std::string targetName = cmSystemTools::GetFilenameName(targetDir);
  1292. targetName = targetName.substr(0, targetName.length() - 4);
  1293. std::string message =
  1294. cmStrCat("Scanning dependencies of target ", targetName);
  1295. cmSystemTools::MakefileColorEcho(
  1296. cmsysTerminal_Color_ForegroundMagenta |
  1297. cmsysTerminal_Color_ForegroundBold,
  1298. message.c_str(), true, color);
  1299. }
  1300. status = this->ScanDependencies(targetDir, dependFile,
  1301. internalDependFile, validDependencies);
  1302. }
  1303. }
  1304. auto depends =
  1305. this->Makefile->GetSafeDefinition("CMAKE_DEPENDS_DEPENDENCY_FILES");
  1306. if (!depends.empty()) {
  1307. // dependencies are managed by compiler
  1308. auto depFiles = cmExpandedList(depends, true);
  1309. std::string const internalDepFile =
  1310. targetDir + "/compiler_depend.internal";
  1311. std::string const depFile = targetDir + "/compiler_depend.make";
  1312. cmDepends::DependencyMap dependencies;
  1313. cmDependsCompiler depsManager;
  1314. bool projectOnly = cmIsOn(
  1315. this->Makefile->GetSafeDefinition("CMAKE_DEPENDS_IN_PROJECT_ONLY"));
  1316. depsManager.SetVerbose(verbose);
  1317. depsManager.SetLocalGenerator(this);
  1318. if (!depsManager.CheckDependencies(
  1319. internalDepFile, depFiles, dependencies,
  1320. projectOnly ? NotInProjectDir(this->GetSourceDirectory(),
  1321. this->GetBinaryDirectory())
  1322. : std::function<bool(const std::string&)>())) {
  1323. // regenerate dependencies files
  1324. if (verbose) {
  1325. std::string targetName = cmCMakePath(targetDir)
  1326. .GetFileName()
  1327. .RemoveExtension()
  1328. .GenericString();
  1329. auto message =
  1330. cmStrCat("Consolidate compiler generated dependencies of target ",
  1331. targetName);
  1332. cmSystemTools::MakefileColorEcho(
  1333. cmsysTerminal_Color_ForegroundMagenta |
  1334. cmsysTerminal_Color_ForegroundBold,
  1335. message.c_str(), true, color);
  1336. }
  1337. // Open the make depends file. This should be copy-if-different
  1338. // because the make tool may try to reload it needlessly otherwise.
  1339. cmGeneratedFileStream ruleFileStream(
  1340. depFile, false, this->GlobalGenerator->GetMakefileEncoding());
  1341. ruleFileStream.SetCopyIfDifferent(true);
  1342. if (!ruleFileStream) {
  1343. return false;
  1344. }
  1345. // Open the cmake dependency tracking file. This should not be
  1346. // copy-if-different because dependencies are re-scanned when it is
  1347. // older than the DependInfo.cmake.
  1348. cmGeneratedFileStream internalRuleFileStream(
  1349. internalDepFile, false, this->GlobalGenerator->GetMakefileEncoding());
  1350. if (!internalRuleFileStream) {
  1351. return false;
  1352. }
  1353. this->WriteDisclaimer(ruleFileStream);
  1354. this->WriteDisclaimer(internalRuleFileStream);
  1355. depsManager.WriteDependencies(dependencies, ruleFileStream,
  1356. internalRuleFileStream);
  1357. }
  1358. }
  1359. // The dependencies are already up-to-date.
  1360. return status;
  1361. }
  1362. bool cmLocalUnixMakefileGenerator3::ScanDependencies(
  1363. std::string const& targetDir, std::string const& dependFile,
  1364. std::string const& internalDependFile, cmDepends::DependencyMap& validDeps)
  1365. {
  1366. // Read the directory information file.
  1367. cmMakefile* mf = this->Makefile;
  1368. bool haveDirectoryInfo = false;
  1369. {
  1370. std::string dirInfoFile =
  1371. cmStrCat(this->GetCurrentBinaryDirectory(),
  1372. "/CMakeFiles/CMakeDirectoryInformation.cmake");
  1373. if (mf->ReadListFile(dirInfoFile) &&
  1374. !cmSystemTools::GetErrorOccurredFlag()) {
  1375. haveDirectoryInfo = true;
  1376. }
  1377. }
  1378. // Lookup useful directory information.
  1379. if (haveDirectoryInfo) {
  1380. // Test whether we need to force Unix paths.
  1381. if (cmValue force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS")) {
  1382. if (!cmIsOff(force)) {
  1383. cmSystemTools::SetForceUnixPaths(true);
  1384. }
  1385. }
  1386. // Setup relative path top directories.
  1387. cmValue relativePathTopSource =
  1388. mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_SOURCE");
  1389. cmValue relativePathTopBinary =
  1390. mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_BINARY");
  1391. if (relativePathTopSource && relativePathTopBinary) {
  1392. this->SetRelativePathTop(*relativePathTopSource, *relativePathTopBinary);
  1393. }
  1394. } else {
  1395. cmSystemTools::Error("Directory Information file not found");
  1396. }
  1397. // Open the make depends file. This should be copy-if-different
  1398. // because the make tool may try to reload it needlessly otherwise.
  1399. cmGeneratedFileStream ruleFileStream(
  1400. dependFile, false, this->GlobalGenerator->GetMakefileEncoding());
  1401. ruleFileStream.SetCopyIfDifferent(true);
  1402. if (!ruleFileStream) {
  1403. return false;
  1404. }
  1405. // Open the cmake dependency tracking file. This should not be
  1406. // copy-if-different because dependencies are re-scanned when it is
  1407. // older than the DependInfo.cmake.
  1408. cmGeneratedFileStream internalRuleFileStream(
  1409. internalDependFile, false, this->GlobalGenerator->GetMakefileEncoding());
  1410. if (!internalRuleFileStream) {
  1411. return false;
  1412. }
  1413. this->WriteDisclaimer(ruleFileStream);
  1414. this->WriteDisclaimer(internalRuleFileStream);
  1415. // for each language we need to scan, scan it
  1416. std::vector<std::string> langs =
  1417. cmExpandedList(mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES"));
  1418. for (std::string const& lang : langs) {
  1419. // construct the checker
  1420. // Create the scanner for this language
  1421. std::unique_ptr<cmDepends> scanner;
  1422. if (lang == "C" || lang == "CXX" || lang == "RC" || lang == "ASM" ||
  1423. lang == "OBJC" || lang == "OBJCXX" || lang == "CUDA" ||
  1424. lang == "HIP" || lang == "ISPC") {
  1425. // TODO: Handle RC (resource files) dependencies correctly.
  1426. scanner = cm::make_unique<cmDependsC>(this, targetDir, lang, &validDeps);
  1427. }
  1428. #ifndef CMAKE_BOOTSTRAP
  1429. else if (lang == "Fortran") {
  1430. ruleFileStream << "# Note that incremental build could trigger "
  1431. << "a call to cmake_copy_f90_mod on each re-build\n";
  1432. scanner = cm::make_unique<cmDependsFortran>(this);
  1433. } else if (lang == "Java") {
  1434. scanner = cm::make_unique<cmDependsJava>();
  1435. }
  1436. #endif
  1437. if (scanner) {
  1438. scanner->SetLocalGenerator(this);
  1439. scanner->SetFileTimeCache(
  1440. this->GlobalGenerator->GetCMakeInstance()->GetFileTimeCache());
  1441. scanner->SetLanguage(lang);
  1442. scanner->SetTargetDirectory(targetDir);
  1443. scanner->Write(ruleFileStream, internalRuleFileStream);
  1444. }
  1445. }
  1446. return true;
  1447. }
  1448. void cmLocalUnixMakefileGenerator3::CheckMultipleOutputs(bool verbose)
  1449. {
  1450. cmMakefile* mf = this->Makefile;
  1451. // Get the string listing the multiple output pairs.
  1452. cmValue pairs_string = mf->GetDefinition("CMAKE_MULTIPLE_OUTPUT_PAIRS");
  1453. if (!pairs_string) {
  1454. return;
  1455. }
  1456. // Convert the string to a list and preserve empty entries.
  1457. std::vector<std::string> pairs = cmExpandedList(*pairs_string, true);
  1458. for (auto i = pairs.begin(); i != pairs.end() && (i + 1) != pairs.end();) {
  1459. const std::string& depender = *i++;
  1460. const std::string& dependee = *i++;
  1461. // If the depender is missing then delete the dependee to make
  1462. // sure both will be regenerated.
  1463. if (cmSystemTools::FileExists(dependee) &&
  1464. !cmSystemTools::FileExists(depender)) {
  1465. if (verbose) {
  1466. cmSystemTools::Stdout(cmStrCat(
  1467. "Deleting primary custom command output \"", dependee,
  1468. "\" because another output \"", depender, "\" does not exist.\n"));
  1469. }
  1470. cmSystemTools::RemoveFile(dependee);
  1471. }
  1472. }
  1473. }
  1474. void cmLocalUnixMakefileGenerator3::WriteLocalAllRules(
  1475. std::ostream& ruleFileStream)
  1476. {
  1477. this->WriteDisclaimer(ruleFileStream);
  1478. // Write the main entry point target. This must be the VERY first
  1479. // target so that make with no arguments will run it.
  1480. {
  1481. // Just depend on the all target to drive the build.
  1482. std::vector<std::string> depends;
  1483. std::vector<std::string> no_commands;
  1484. depends.emplace_back("all");
  1485. // Write the rule.
  1486. this->WriteMakeRule(ruleFileStream,
  1487. "Default target executed when no arguments are "
  1488. "given to make.",
  1489. "default_target", depends, no_commands, true);
  1490. // Help out users that try "gmake target1 target2 -j".
  1491. cmGlobalUnixMakefileGenerator3* gg =
  1492. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  1493. if (gg->AllowNotParallel()) {
  1494. std::vector<std::string> no_depends;
  1495. this->WriteMakeRule(ruleFileStream,
  1496. "Allow only one \"make -f "
  1497. "Makefile2\" at a time, but pass "
  1498. "parallelism.",
  1499. ".NOTPARALLEL", no_depends, no_commands, false);
  1500. }
  1501. }
  1502. this->WriteSpecialTargetsTop(ruleFileStream);
  1503. // Include the progress variables for the target.
  1504. // Write all global targets
  1505. this->WriteDivider(ruleFileStream);
  1506. ruleFileStream << "# Targets provided globally by CMake.\n"
  1507. << "\n";
  1508. const auto& targets = this->GetGeneratorTargets();
  1509. for (const auto& gt : targets) {
  1510. if (gt->GetType() == cmStateEnums::GLOBAL_TARGET) {
  1511. std::string targetString =
  1512. "Special rule for the target " + gt->GetName();
  1513. std::vector<std::string> commands;
  1514. std::vector<std::string> depends;
  1515. cmValue p = gt->GetProperty("EchoString");
  1516. const char* text = p ? p->c_str() : "Running external command ...";
  1517. depends.reserve(gt->GetUtilities().size());
  1518. for (BT<std::pair<std::string, bool>> const& u : gt->GetUtilities()) {
  1519. depends.push_back(u.Value.first);
  1520. }
  1521. this->AppendEcho(commands, text,
  1522. cmLocalUnixMakefileGenerator3::EchoGlobal);
  1523. // Global targets store their rules in pre- and post-build commands.
  1524. this->AppendCustomDepends(depends, gt->GetPreBuildCommands());
  1525. this->AppendCustomDepends(depends, gt->GetPostBuildCommands());
  1526. this->AppendCustomCommands(commands, gt->GetPreBuildCommands(), gt.get(),
  1527. this->GetCurrentBinaryDirectory());
  1528. this->AppendCustomCommands(commands, gt->GetPostBuildCommands(),
  1529. gt.get(), this->GetCurrentBinaryDirectory());
  1530. std::string targetName = gt->GetName();
  1531. this->WriteMakeRule(ruleFileStream, targetString.c_str(), targetName,
  1532. depends, commands, true);
  1533. // Provide a "/fast" version of the target.
  1534. depends.clear();
  1535. if ((targetName == "install") || (targetName == "install/local") ||
  1536. (targetName == "install/strip")) {
  1537. // Provide a fast install target that does not depend on all
  1538. // but has the same command.
  1539. depends.emplace_back("preinstall/fast");
  1540. } else {
  1541. // Just forward to the real target so at least it will work.
  1542. depends.push_back(targetName);
  1543. commands.clear();
  1544. }
  1545. targetName += "/fast";
  1546. this->WriteMakeRule(ruleFileStream, targetString.c_str(), targetName,
  1547. depends, commands, true);
  1548. }
  1549. }
  1550. std::vector<std::string> depends;
  1551. std::vector<std::string> commands;
  1552. // Write the all rule.
  1553. std::string recursiveTarget =
  1554. cmStrCat(this->GetCurrentBinaryDirectory(), "/all");
  1555. bool regenerate =
  1556. !this->GlobalGenerator->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
  1557. if (regenerate) {
  1558. depends.emplace_back("cmake_check_build_system");
  1559. }
  1560. std::string progressDir =
  1561. cmStrCat(this->GetBinaryDirectory(), "/CMakeFiles");
  1562. {
  1563. std::ostringstream progCmd;
  1564. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  1565. progCmd << this->ConvertToOutputFormat(progressDir,
  1566. cmOutputConverter::SHELL);
  1567. std::string progressFile = "/CMakeFiles/progress.marks";
  1568. std::string progressFileNameFull = this->ConvertToFullPath(progressFile);
  1569. progCmd << " "
  1570. << this->ConvertToOutputFormat(progressFileNameFull,
  1571. cmOutputConverter::SHELL);
  1572. commands.push_back(progCmd.str());
  1573. }
  1574. std::string mf2Dir = "CMakeFiles/Makefile2";
  1575. commands.push_back(this->GetRecursiveMakeCall(mf2Dir, recursiveTarget));
  1576. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  1577. this->GetCurrentBinaryDirectory());
  1578. {
  1579. std::ostringstream progCmd;
  1580. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  1581. progCmd << this->ConvertToOutputFormat(progressDir,
  1582. cmOutputConverter::SHELL);
  1583. progCmd << " 0";
  1584. commands.push_back(progCmd.str());
  1585. }
  1586. this->WriteMakeRule(ruleFileStream, "The main all target", "all", depends,
  1587. commands, true);
  1588. // Write the clean rule.
  1589. recursiveTarget = cmStrCat(this->GetCurrentBinaryDirectory(), "/clean");
  1590. commands.clear();
  1591. depends.clear();
  1592. commands.push_back(this->GetRecursiveMakeCall(mf2Dir, recursiveTarget));
  1593. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  1594. this->GetCurrentBinaryDirectory());
  1595. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
  1596. depends, commands, true);
  1597. commands.clear();
  1598. depends.clear();
  1599. depends.emplace_back("clean");
  1600. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
  1601. depends, commands, true);
  1602. // Write the preinstall rule.
  1603. recursiveTarget = cmStrCat(this->GetCurrentBinaryDirectory(), "/preinstall");
  1604. commands.clear();
  1605. depends.clear();
  1606. cmValue noall =
  1607. this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1608. if (cmIsOff(noall)) {
  1609. // Drive the build before installing.
  1610. depends.emplace_back("all");
  1611. } else if (regenerate) {
  1612. // At least make sure the build system is up to date.
  1613. depends.emplace_back("cmake_check_build_system");
  1614. }
  1615. commands.push_back(this->GetRecursiveMakeCall(mf2Dir, recursiveTarget));
  1616. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  1617. this->GetCurrentBinaryDirectory());
  1618. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1619. "preinstall", depends, commands, true);
  1620. depends.clear();
  1621. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1622. "preinstall/fast", depends, commands, true);
  1623. if (regenerate) {
  1624. // write the depend rule, really a recompute depends rule
  1625. depends.clear();
  1626. commands.clear();
  1627. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  1628. if (cm->DoWriteGlobVerifyTarget()) {
  1629. std::string rescanRule =
  1630. cmStrCat("$(CMAKE_COMMAND) -P ",
  1631. this->ConvertToOutputFormat(cm->GetGlobVerifyScript(),
  1632. cmOutputConverter::SHELL));
  1633. commands.push_back(rescanRule);
  1634. }
  1635. std::string cmakefileName = "CMakeFiles/Makefile.cmake";
  1636. {
  1637. std::string runRule = cmStrCat(
  1638. "$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) "
  1639. "--check-build-system ",
  1640. this->ConvertToOutputFormat(cmakefileName, cmOutputConverter::SHELL),
  1641. " 1");
  1642. commands.push_back(std::move(runRule));
  1643. }
  1644. this->CreateCDCommand(commands, this->GetBinaryDirectory(),
  1645. this->GetCurrentBinaryDirectory());
  1646. this->WriteMakeRule(ruleFileStream, "clear depends", "depend", depends,
  1647. commands, true);
  1648. }
  1649. }
  1650. void cmLocalUnixMakefileGenerator3::ClearDependencies(cmMakefile* mf,
  1651. bool verbose)
  1652. {
  1653. // Get the list of target files to check
  1654. cmValue infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  1655. if (!infoDef) {
  1656. return;
  1657. }
  1658. std::vector<std::string> files = cmExpandedList(*infoDef);
  1659. // Each depend information file corresponds to a target. Clear the
  1660. // dependencies for that target.
  1661. cmDepends clearer;
  1662. clearer.SetVerbose(verbose);
  1663. for (std::string const& file : files) {
  1664. auto snapshot = mf->GetState()->CreateBaseSnapshot();
  1665. cmMakefile lmf(mf->GetGlobalGenerator(), snapshot);
  1666. lmf.ReadListFile(file);
  1667. if (!lmf.GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES").empty()) {
  1668. std::string dir = cmSystemTools::GetFilenamePath(file);
  1669. // Clear the implicit dependency makefile.
  1670. std::string dependFile = dir + "/depend.make";
  1671. clearer.Clear(dependFile);
  1672. // Remove the internal dependency check file to force
  1673. // regeneration.
  1674. std::string internalDependFile = dir + "/depend.internal";
  1675. cmSystemTools::RemoveFile(internalDependFile);
  1676. }
  1677. auto depsFiles = lmf.GetSafeDefinition("CMAKE_DEPENDS_DEPENDENCY_FILES");
  1678. if (!depsFiles.empty()) {
  1679. auto dir = cmCMakePath(file).GetParentPath();
  1680. // Clear the implicit dependency makefile.
  1681. auto depFile = cmCMakePath(dir).Append("compiler_depend.make");
  1682. clearer.Clear(depFile.GenericString());
  1683. // Remove the internal dependency check file
  1684. auto internalDepFile =
  1685. cmCMakePath(dir).Append("compiler_depend.internal");
  1686. cmSystemTools::RemoveFile(internalDepFile.GenericString());
  1687. // Touch timestamp file to force dependencies regeneration
  1688. auto DepTimestamp = cmCMakePath(dir).Append("compiler_depend.ts");
  1689. cmSystemTools::Touch(DepTimestamp.GenericString(), true);
  1690. // clear the dependencies files generated by the compiler
  1691. std::vector<std::string> dependencies = cmExpandedList(depsFiles, true);
  1692. cmDependsCompiler depsManager;
  1693. depsManager.SetVerbose(verbose);
  1694. depsManager.ClearDependencies(dependencies);
  1695. }
  1696. }
  1697. }
  1698. void cmLocalUnixMakefileGenerator3::WriteDependLanguageInfo(
  1699. std::ostream& cmakefileStream, cmGeneratorTarget* target)
  1700. {
  1701. // To enable dependencies filtering
  1702. cmakefileStream << "\n"
  1703. << "# Consider dependencies only in project.\n"
  1704. << "set(CMAKE_DEPENDS_IN_PROJECT_ONLY "
  1705. << (cmIsOn(this->Makefile->GetSafeDefinition(
  1706. "CMAKE_DEPENDS_IN_PROJECT_ONLY"))
  1707. ? "ON"
  1708. : "OFF")
  1709. << ")\n\n";
  1710. auto const& implicitLangs =
  1711. this->GetImplicitDepends(target, cmDependencyScannerKind::CMake);
  1712. // list the languages
  1713. cmakefileStream << "# The set of languages for which implicit "
  1714. "dependencies are needed:\n";
  1715. cmakefileStream << "set(CMAKE_DEPENDS_LANGUAGES\n";
  1716. for (auto const& implicitLang : implicitLangs) {
  1717. cmakefileStream << " \"" << implicitLang.first << "\"\n";
  1718. }
  1719. cmakefileStream << " )\n";
  1720. if (!implicitLangs.empty()) {
  1721. // now list the files for each language
  1722. cmakefileStream
  1723. << "# The set of files for implicit dependencies of each language:\n";
  1724. for (auto const& implicitLang : implicitLangs) {
  1725. const auto& lang = implicitLang.first;
  1726. cmakefileStream << "set(CMAKE_DEPENDS_CHECK_" << lang << "\n";
  1727. auto const& implicitPairs = implicitLang.second;
  1728. // for each file pair
  1729. for (auto const& implicitPair : implicitPairs) {
  1730. for (auto const& di : implicitPair.second) {
  1731. cmakefileStream << " \"" << di << "\" ";
  1732. cmakefileStream << "\"" << implicitPair.first << "\"\n";
  1733. }
  1734. }
  1735. cmakefileStream << " )\n";
  1736. // Tell the dependency scanner what compiler is used.
  1737. std::string cidVar = cmStrCat("CMAKE_", lang, "_COMPILER_ID");
  1738. cmValue cid = this->Makefile->GetDefinition(cidVar);
  1739. if (cmNonempty(cid)) {
  1740. cmakefileStream << "set(CMAKE_" << lang << "_COMPILER_ID \"" << *cid
  1741. << "\")\n";
  1742. }
  1743. if (lang == "Fortran") {
  1744. std::string smodSep =
  1745. this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_SEP");
  1746. std::string smodExt =
  1747. this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
  1748. cmakefileStream << "set(CMAKE_Fortran_SUBMODULE_SEP \"" << smodSep
  1749. << "\")\n";
  1750. cmakefileStream << "set(CMAKE_Fortran_SUBMODULE_EXT \"" << smodExt
  1751. << "\")\n";
  1752. }
  1753. // Build a list of preprocessor definitions for the target.
  1754. std::set<std::string> defines;
  1755. this->GetTargetDefines(target, this->GetConfigName(), lang, defines);
  1756. if (!defines.empty()) {
  1757. /* clang-format off */
  1758. cmakefileStream
  1759. << "\n"
  1760. << "# Preprocessor definitions for this target.\n"
  1761. << "set(CMAKE_TARGET_DEFINITIONS_" << lang << "\n";
  1762. /* clang-format on */
  1763. for (std::string const& define : defines) {
  1764. cmakefileStream << " " << cmOutputConverter::EscapeForCMake(define)
  1765. << "\n";
  1766. }
  1767. cmakefileStream << " )\n";
  1768. }
  1769. // Target-specific include directories:
  1770. cmakefileStream << "\n"
  1771. << "# The include file search paths:\n";
  1772. cmakefileStream << "set(CMAKE_" << lang << "_TARGET_INCLUDE_PATH\n";
  1773. std::vector<std::string> includes;
  1774. this->GetIncludeDirectories(includes, target, lang,
  1775. this->GetConfigName());
  1776. std::string const& binaryDir = this->GetState()->GetBinaryDirectory();
  1777. if (this->Makefile->IsOn("CMAKE_DEPENDS_IN_PROJECT_ONLY")) {
  1778. std::string const& sourceDir = this->GetState()->GetSourceDirectory();
  1779. cm::erase_if(includes, ::NotInProjectDir(sourceDir, binaryDir));
  1780. }
  1781. for (std::string const& include : includes) {
  1782. cmakefileStream << " \"" << this->MaybeRelativeToTopBinDir(include)
  1783. << "\"\n";
  1784. }
  1785. cmakefileStream << " )\n";
  1786. }
  1787. // Store include transform rule properties. Write the directory
  1788. // rules first because they may be overridden by later target rules.
  1789. std::vector<std::string> transformRules;
  1790. if (cmValue xform =
  1791. this->Makefile->GetProperty("IMPLICIT_DEPENDS_INCLUDE_TRANSFORM")) {
  1792. cmExpandList(*xform, transformRules);
  1793. }
  1794. if (cmValue xform =
  1795. target->GetProperty("IMPLICIT_DEPENDS_INCLUDE_TRANSFORM")) {
  1796. cmExpandList(*xform, transformRules);
  1797. }
  1798. if (!transformRules.empty()) {
  1799. cmakefileStream << "\nset(CMAKE_INCLUDE_TRANSFORMS\n";
  1800. for (std::string const& tr : transformRules) {
  1801. cmakefileStream << " " << cmOutputConverter::EscapeForCMake(tr)
  1802. << "\n";
  1803. }
  1804. cmakefileStream << " )\n";
  1805. }
  1806. }
  1807. auto const& compilerLangs =
  1808. this->GetImplicitDepends(target, cmDependencyScannerKind::Compiler);
  1809. // list the dependency files managed by the compiler
  1810. cmakefileStream << "\n# The set of dependency files which are needed:\n";
  1811. cmakefileStream << "set(CMAKE_DEPENDS_DEPENDENCY_FILES\n";
  1812. for (auto const& compilerLang : compilerLangs) {
  1813. auto const& compilerPairs = compilerLang.second;
  1814. if (compilerLang.first == "CUSTOM"_s) {
  1815. for (auto const& compilerPair : compilerPairs) {
  1816. for (auto const& src : compilerPair.second) {
  1817. cmakefileStream << R"( "" ")"
  1818. << this->MaybeRelativeToTopBinDir(compilerPair.first)
  1819. << R"(" "custom" ")"
  1820. << this->MaybeRelativeToTopBinDir(src) << "\"\n";
  1821. }
  1822. }
  1823. } else {
  1824. auto depFormat = this->Makefile->GetSafeDefinition(
  1825. cmStrCat("CMAKE_", compilerLang.first, "_DEPFILE_FORMAT"));
  1826. for (auto const& compilerPair : compilerPairs) {
  1827. for (auto const& src : compilerPair.second) {
  1828. cmakefileStream << " \"" << src << "\" \""
  1829. << this->MaybeRelativeToTopBinDir(compilerPair.first)
  1830. << "\" \"" << depFormat << "\" \""
  1831. << this->MaybeRelativeToTopBinDir(compilerPair.first)
  1832. << ".d\"\n";
  1833. }
  1834. }
  1835. }
  1836. }
  1837. cmakefileStream << " )\n";
  1838. }
  1839. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  1840. {
  1841. os << "# CMAKE generated file: DO NOT EDIT!\n"
  1842. << "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
  1843. << " Generator, CMake Version " << cmVersion::GetMajorVersion() << "."
  1844. << cmVersion::GetMinorVersion() << "\n\n";
  1845. }
  1846. std::string cmLocalUnixMakefileGenerator3::GetRecursiveMakeCall(
  1847. const std::string& makefile, const std::string& tgt)
  1848. {
  1849. // Call make on the given file.
  1850. std::string cmd = cmStrCat(
  1851. "$(MAKE) $(MAKESILENT) -f ",
  1852. this->ConvertToOutputFormat(makefile, cmOutputConverter::SHELL), ' ');
  1853. cmGlobalUnixMakefileGenerator3* gg =
  1854. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  1855. // Pass down verbosity level.
  1856. if (!gg->MakeSilentFlag.empty()) {
  1857. cmd += gg->MakeSilentFlag;
  1858. cmd += " ";
  1859. }
  1860. // Most unix makes will pass the command line flags to make down to
  1861. // sub-invoked makes via an environment variable. However, some
  1862. // makes do not support that, so you have to pass the flags
  1863. // explicitly.
  1864. if (gg->PassMakeflags) {
  1865. cmd += "-$(MAKEFLAGS) ";
  1866. }
  1867. // Add the target.
  1868. if (!tgt.empty()) {
  1869. // The make target is always relative to the top of the build tree.
  1870. std::string tgt2 = this->MaybeRelativeToTopBinDir(tgt);
  1871. // The target may have been written with windows paths.
  1872. cmSystemTools::ConvertToOutputSlashes(tgt2);
  1873. // Escape one extra time if the make tool requires it.
  1874. if (this->MakeCommandEscapeTargetTwice) {
  1875. tgt2 = this->EscapeForShell(tgt2, true, false);
  1876. }
  1877. // The target name is now a string that should be passed verbatim
  1878. // on the command line.
  1879. cmd += this->EscapeForShell(tgt2, true, false);
  1880. }
  1881. return cmd;
  1882. }
  1883. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  1884. {
  1885. os << "#======================================"
  1886. "=======================================\n";
  1887. }
  1888. void cmLocalUnixMakefileGenerator3::WriteCMakeArgument(std::ostream& os,
  1889. const std::string& s)
  1890. {
  1891. // Write the given string to the stream with escaping to get it back
  1892. // into CMake through the lexical scanner.
  1893. os << '"';
  1894. for (char c : s) {
  1895. if (c == '\\') {
  1896. os << "\\\\";
  1897. } else if (c == '"') {
  1898. os << "\\\"";
  1899. } else {
  1900. os << c;
  1901. }
  1902. }
  1903. os << '"';
  1904. }
  1905. std::string cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(
  1906. const std::string& p, bool useWatcomQuote)
  1907. {
  1908. // Split the path into its components.
  1909. std::vector<std::string> components;
  1910. cmSystemTools::SplitPath(p, components);
  1911. // Open the quoted result.
  1912. std::string result;
  1913. if (useWatcomQuote) {
  1914. #if defined(_WIN32) && !defined(__CYGWIN__)
  1915. result = "'";
  1916. #else
  1917. result = "\"'";
  1918. #endif
  1919. } else {
  1920. result = "\"";
  1921. }
  1922. // Return an empty path if there are no components.
  1923. if (!components.empty()) {
  1924. // Choose a slash direction and fix root component.
  1925. const char* slash = "/";
  1926. #if defined(_WIN32) && !defined(__CYGWIN__)
  1927. if (!cmSystemTools::GetForceUnixPaths()) {
  1928. slash = "\\";
  1929. for (char& i : components[0]) {
  1930. if (i == '/') {
  1931. i = '\\';
  1932. }
  1933. }
  1934. }
  1935. #endif
  1936. // Begin the quoted result with the root component.
  1937. result += components[0];
  1938. if (components.size() > 1) {
  1939. // Now add the rest of the components separated by the proper slash
  1940. // direction for this platform.
  1941. auto compEnd = std::remove(components.begin() + 1, components.end() - 1,
  1942. std::string());
  1943. auto compStart = components.begin() + 1;
  1944. result += cmJoin(cmMakeRange(compStart, compEnd), slash);
  1945. // Only the last component can be empty to avoid double slashes.
  1946. result += slash;
  1947. result += components.back();
  1948. }
  1949. }
  1950. // Close the quoted result.
  1951. if (useWatcomQuote) {
  1952. #if defined(_WIN32) && !defined(__CYGWIN__)
  1953. result += "'";
  1954. #else
  1955. result += "'\"";
  1956. #endif
  1957. } else {
  1958. result += "\"";
  1959. }
  1960. return result;
  1961. }
  1962. std::string cmLocalUnixMakefileGenerator3::GetTargetDirectory(
  1963. cmGeneratorTarget const* target) const
  1964. {
  1965. std::string dir = cmStrCat("CMakeFiles/", target->GetName());
  1966. #if defined(__VMS)
  1967. dir += "_dir";
  1968. #else
  1969. dir += ".dir";
  1970. #endif
  1971. return dir;
  1972. }
  1973. cmLocalUnixMakefileGenerator3::ImplicitDependLanguageMap const&
  1974. cmLocalUnixMakefileGenerator3::GetImplicitDepends(
  1975. const cmGeneratorTarget* tgt, cmDependencyScannerKind scanner)
  1976. {
  1977. return this->ImplicitDepends[tgt->GetName()][scanner];
  1978. }
  1979. void cmLocalUnixMakefileGenerator3::AddImplicitDepends(
  1980. const cmGeneratorTarget* tgt, const std::string& lang,
  1981. const std::string& obj, const std::string& src,
  1982. cmDependencyScannerKind scanner)
  1983. {
  1984. this->ImplicitDepends[tgt->GetName()][scanner][lang][obj].push_back(src);
  1985. }
  1986. void cmLocalUnixMakefileGenerator3::CreateCDCommand(
  1987. std::vector<std::string>& commands, std::string const& tgtDir,
  1988. std::string const& relDir)
  1989. {
  1990. // do we need to cd?
  1991. if (tgtDir == relDir) {
  1992. return;
  1993. }
  1994. // In a Windows shell we must change drive letter too. The shell
  1995. // used by NMake and Borland make does not support "cd /d" so this
  1996. // feature simply cannot work with them (Borland make does not even
  1997. // support changing the drive letter with just "d:").
  1998. const char* cd_cmd = this->IsMinGWMake() ? "cd /d " : "cd ";
  1999. cmGlobalUnixMakefileGenerator3* gg =
  2000. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  2001. if (!gg->UnixCD) {
  2002. // On Windows we must perform each step separately and then change
  2003. // back because the shell keeps the working directory between
  2004. // commands.
  2005. std::string cmd =
  2006. cmStrCat(cd_cmd, this->ConvertToOutputForExisting(tgtDir));
  2007. commands.insert(commands.begin(), cmd);
  2008. // Change back to the starting directory.
  2009. cmd = cmStrCat(cd_cmd, this->ConvertToOutputForExisting(relDir));
  2010. commands.push_back(std::move(cmd));
  2011. } else {
  2012. // On UNIX we must construct a single shell command to change
  2013. // directory and build because make resets the directory between
  2014. // each command.
  2015. std::string outputForExisting = this->ConvertToOutputForExisting(tgtDir);
  2016. std::string prefix = cd_cmd + outputForExisting + " && ";
  2017. std::transform(commands.begin(), commands.end(), commands.begin(),
  2018. [&prefix](std::string const& s) { return prefix + s; });
  2019. }
  2020. }