cmLocalUnixMakefileGenerator3.cxx 79 KB

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