cmLocalUnixMakefileGenerator3.cxx 72 KB

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