cmLocalUnixMakefileGenerator3.cxx 79 KB

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