cmLocalUnixMakefileGenerator3.cxx 61 KB

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