cmLocalUnixMakefileGenerator3.cxx 61 KB

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