cmLocalUnixMakefileGenerator3.cxx 72 KB

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