cmLocalUnixMakefileGenerator3.cxx 60 KB

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