cmLocalUnixMakefileGenerator3.cxx 79 KB

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