cmLocalUnixMakefileGenerator3.cxx 73 KB

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