cmLocalUnixMakefileGenerator3.cxx 62 KB

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