cmLocalUnixMakefileGenerator3.cxx 72 KB

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