cmLocalUnixMakefileGenerator3.cxx 73 KB

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