cmLocalUnixMakefileGenerator3.cxx 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  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->DefineWindowsNULL = false;
  40. this->UnixCD = true;
  41. this->ForceVerboseMakefiles=false;
  42. this->ColorMakefile = false;
  43. this->SkipPreprocessedSourceRules = false;
  44. this->SkipAssemblySourceRules = false;
  45. this->NativeEchoCommand = "@echo ";
  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 = this->NativeEchoCommand;
  950. cmd += this->EscapeForShell(line.c_str(), false, true);
  951. }
  952. else
  953. {
  954. // Use cmake to echo the text in color.
  955. cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
  956. cmd += color_name;
  957. cmd += this->EscapeForShell(line.c_str());
  958. }
  959. commands.push_back(cmd);
  960. }
  961. // Reset the line to emtpy.
  962. line = "";
  963. // Terminate on end-of-string.
  964. if(*c == '\0')
  965. {
  966. return;
  967. }
  968. }
  969. else if(*c != '\r')
  970. {
  971. // Append this character to the current line.
  972. line += *c;
  973. }
  974. }
  975. }
  976. //----------------------------------------------------------------------------
  977. //take a tgt path and convert it into a make target, it could be full, or
  978. //relative
  979. std::string cmLocalUnixMakefileGenerator3
  980. ::ConvertToMakeTarget(const char* tgt)
  981. {
  982. // Make targets should not have a leading './' for a file in the
  983. // directory containing the makefile.
  984. std::string ret = tgt;
  985. if(ret.size() > 2 && (ret[0] == '.') &&
  986. ( (ret[1] == '/') || ret[1] == '\\'))
  987. {
  988. std::string upath = ret;
  989. cmSystemTools::ConvertToUnixSlashes(upath);
  990. if(upath.find(2, '/') == upath.npos)
  991. {
  992. ret = ret.substr(2, ret.size()-2);
  993. }
  994. }
  995. return ret;
  996. }
  997. //----------------------------------------------------------------------------
  998. std::string
  999. cmLocalUnixMakefileGenerator3
  1000. ::CreateMakeVariable(const char* sin, const char* s2in)
  1001. {
  1002. std::string s = sin;
  1003. std::string s2 = s2in;
  1004. std::string unmodified = s;
  1005. unmodified += s2;
  1006. // if there is no restriction on the length of make variables
  1007. // and there are no "." charactors in the string, then return the
  1008. // unmodified combination.
  1009. if((!this->MakefileVariableSize && unmodified.find('.') == s.npos)
  1010. && (!this->MakefileVariableSize && unmodified.find('-') == s.npos))
  1011. {
  1012. return unmodified;
  1013. }
  1014. // see if the variable has been defined before and return
  1015. // the modified version of the variable
  1016. std::map<cmStdString, cmStdString>::iterator i =
  1017. this->MakeVariableMap.find(unmodified);
  1018. if(i != this->MakeVariableMap.end())
  1019. {
  1020. return i->second;
  1021. }
  1022. // start with the unmodified variable
  1023. std::string ret = unmodified;
  1024. // if this there is no value for this->MakefileVariableSize then
  1025. // the string must have bad characters in it
  1026. if(!this->MakefileVariableSize)
  1027. {
  1028. cmSystemTools::ReplaceString(ret, ".", "_");
  1029. cmSystemTools::ReplaceString(ret, "-", "__");
  1030. int ni = 0;
  1031. char buffer[5];
  1032. // make sure the _ version is not already used, if
  1033. // it is used then add number to the end of the variable
  1034. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1035. {
  1036. ++ni;
  1037. sprintf(buffer, "%04d", ni);
  1038. ret = unmodified + buffer;
  1039. }
  1040. this->ShortMakeVariableMap[ret] = "1";
  1041. this->MakeVariableMap[unmodified] = ret;
  1042. return ret;
  1043. }
  1044. // if the string is greater the 32 chars it is an invalid vairable name
  1045. // for borland make
  1046. if(static_cast<int>(ret.size()) > this->MakefileVariableSize)
  1047. {
  1048. int keep = this->MakefileVariableSize - 8;
  1049. int size = keep + 3;
  1050. std::string str1 = s;
  1051. std::string str2 = s2;
  1052. // we must shorten the combined string by 4 charactors
  1053. // keep no more than 24 charactors from the second string
  1054. if(static_cast<int>(str2.size()) > keep)
  1055. {
  1056. str2 = str2.substr(0, keep);
  1057. }
  1058. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  1059. {
  1060. str1 = str1.substr(0, size - str2.size());
  1061. }
  1062. char buffer[5];
  1063. int ni = 0;
  1064. sprintf(buffer, "%04d", ni);
  1065. ret = str1 + str2 + buffer;
  1066. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1067. {
  1068. ++ni;
  1069. sprintf(buffer, "%04d", ni);
  1070. ret = str1 + str2 + buffer;
  1071. }
  1072. if(ni == 1000)
  1073. {
  1074. cmSystemTools::Error("Borland makefile variable length too long");
  1075. return unmodified;
  1076. }
  1077. // once an unused variable is found
  1078. this->ShortMakeVariableMap[ret] = "1";
  1079. }
  1080. // always make an entry into the unmodified to variable map
  1081. this->MakeVariableMap[unmodified] = ret;
  1082. return ret;
  1083. }
  1084. //----------------------------------------------------------------------------
  1085. bool cmLocalUnixMakefileGenerator3::ScanDependencies(const char* tgtInfo)
  1086. {
  1087. // The info file for this target
  1088. std::string infoFile = tgtInfo;
  1089. // Read the directory information file.
  1090. cmMakefile* mf = this->Makefile;
  1091. bool haveDirectoryInfo = false;
  1092. std::string dirInfoFile = this->Makefile->GetStartOutputDirectory();
  1093. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1094. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1095. if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
  1096. !cmSystemTools::GetErrorOccuredFlag())
  1097. {
  1098. haveDirectoryInfo = true;
  1099. }
  1100. // read in the target info file
  1101. if(!mf->ReadListFile(0, infoFile.c_str()) ||
  1102. cmSystemTools::GetErrorOccuredFlag())
  1103. {
  1104. cmSystemTools::Error("Target DependInfo.cmake file not found");
  1105. }
  1106. // Test whether we need to force Unix paths.
  1107. if(haveDirectoryInfo)
  1108. {
  1109. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  1110. {
  1111. if(!cmSystemTools::IsOff(force))
  1112. {
  1113. cmSystemTools::SetForceUnixPaths(true);
  1114. }
  1115. }
  1116. }
  1117. else
  1118. {
  1119. cmSystemTools::Error("Directory Information file not found");
  1120. }
  1121. // create the file stream for the depends file
  1122. std::string dir = cmSystemTools::GetFilenamePath(infoFile);
  1123. // Open the rule file. This should be copy-if-different because the
  1124. // rules may depend on this file itself.
  1125. std::string ruleFileNameFull = dir;
  1126. ruleFileNameFull += "/depend.make";
  1127. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1128. ruleFileStream.SetCopyIfDifferent(true);
  1129. if(!ruleFileStream)
  1130. {
  1131. return false;
  1132. }
  1133. std::string internalRuleFileNameFull = dir;
  1134. internalRuleFileNameFull += "/depend.internal";
  1135. cmGeneratedFileStream
  1136. internalRuleFileStream(internalRuleFileNameFull.c_str());
  1137. internalRuleFileStream.SetCopyIfDifferent(true);
  1138. if(!internalRuleFileStream)
  1139. {
  1140. return false;
  1141. }
  1142. this->WriteDisclaimer(ruleFileStream);
  1143. this->WriteDisclaimer(internalRuleFileStream);
  1144. // for each language we need to scan, scan it
  1145. const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
  1146. std::vector<std::string> langs;
  1147. cmSystemTools::ExpandListArgument(langStr, langs);
  1148. for (std::vector<std::string>::iterator li =
  1149. langs.begin(); li != langs.end(); ++li)
  1150. {
  1151. // construct the checker
  1152. std::string lang = li->c_str();
  1153. // Get the set of include directories.
  1154. std::vector<std::string> includes;
  1155. if(haveDirectoryInfo)
  1156. {
  1157. std::string includePathVar = "CMAKE_";
  1158. includePathVar += lang;
  1159. includePathVar += "_INCLUDE_PATH";
  1160. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  1161. {
  1162. cmSystemTools::ExpandListArgument(includePath, includes);
  1163. }
  1164. }
  1165. // Get the include file regular expression.
  1166. std::string includeRegexScan = "^.*$";
  1167. std::string includeRegexComplain = "^$";
  1168. if(haveDirectoryInfo)
  1169. {
  1170. std::string scanRegexVar = "CMAKE_";
  1171. scanRegexVar += lang;
  1172. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  1173. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  1174. {
  1175. includeRegexScan = scanRegex;
  1176. }
  1177. std::string complainRegexVar = "CMAKE_";
  1178. complainRegexVar += lang;
  1179. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  1180. if(const char* complainRegex =
  1181. mf->GetDefinition(complainRegexVar.c_str()))
  1182. {
  1183. includeRegexComplain = complainRegex;
  1184. }
  1185. }
  1186. // Create the scanner for this language
  1187. cmDepends *scanner = 0;
  1188. if(lang == "C" || lang == "CXX" || lang == "RC")
  1189. {
  1190. std::string includeCacheFileName = dir;
  1191. includeCacheFileName += "/";
  1192. includeCacheFileName += lang;
  1193. includeCacheFileName += ".includecache";
  1194. // TODO: Handle RC (resource files) dependencies correctly.
  1195. scanner = new cmDependsC(includes,
  1196. includeRegexScan.c_str(),
  1197. includeRegexComplain.c_str(),
  1198. includeCacheFileName);
  1199. scanner->SetLocalGenerator(this);
  1200. }
  1201. #ifdef CMAKE_BUILD_WITH_CMAKE
  1202. else if(lang == "Fortran")
  1203. {
  1204. scanner = new cmDependsFortran(includes);
  1205. }
  1206. else if(lang == "Java")
  1207. {
  1208. scanner = new cmDependsJava();
  1209. }
  1210. #endif
  1211. if (scanner)
  1212. {
  1213. scanner->SetFileComparison
  1214. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1215. // for each file we need to scan
  1216. std::string srcLang = "CMAKE_DEPENDS_CHECK_";
  1217. srcLang += lang;
  1218. const char *srcStr = mf->GetSafeDefinition(srcLang.c_str());
  1219. std::vector<std::string> srcs;
  1220. cmSystemTools::ExpandListArgument(srcStr, srcs);
  1221. for (std::vector<std::string>::iterator si =
  1222. srcs.begin(); si != srcs.end(); ++si)
  1223. {
  1224. std::string &src = *si;
  1225. ++si;
  1226. // make sure the object file is relative to home output
  1227. std::string obj = *si;
  1228. obj = this->Convert(obj.c_str(),HOME_OUTPUT,MAKEFILE);
  1229. scanner->Write(src.c_str(),obj.c_str(),
  1230. ruleFileStream, internalRuleFileStream);
  1231. }
  1232. // free the scanner for this language
  1233. delete scanner;
  1234. }
  1235. }
  1236. // dependencies were generated, so touch the mark file
  1237. ruleFileNameFull += ".mark";
  1238. std::ofstream fmark(ruleFileNameFull.c_str());
  1239. fmark << "Dependencies updated>" << std::endl;
  1240. return true;
  1241. }
  1242. //----------------------------------------------------------------------------
  1243. void cmLocalUnixMakefileGenerator3
  1244. ::WriteLocalAllRules(std::ostream& ruleFileStream)
  1245. {
  1246. this->WriteDisclaimer(ruleFileStream);
  1247. // Write the main entry point target. This must be the VERY first
  1248. // target so that make with no arguments will run it.
  1249. {
  1250. // Just depend on the all target to drive the build.
  1251. std::vector<std::string> depends;
  1252. std::vector<std::string> no_commands;
  1253. depends.push_back("all");
  1254. // Write the rule.
  1255. this->WriteMakeRule(ruleFileStream,
  1256. "Default target executed when no arguments are "
  1257. "given to make.",
  1258. "default_target",
  1259. depends,
  1260. no_commands, true);
  1261. }
  1262. this->WriteSpecialTargetsTop(ruleFileStream);
  1263. // Include the progress variables for the target.
  1264. std::string progressFile = cmake::GetCMakeFilesDirectory();
  1265. progressFile += "/progress.make";
  1266. std::string progressFileNameFull =
  1267. this->ConvertToFullPath(progressFile.c_str());
  1268. ruleFileStream
  1269. << "# Include the progress variables for this target.\n"
  1270. << this->IncludeDirective << " "
  1271. << this->Convert(progressFileNameFull.c_str(),
  1272. cmLocalGenerator::START_OUTPUT,
  1273. cmLocalGenerator::MAKEFILE) << "\n\n";
  1274. // Write all global targets
  1275. this->WriteDivider(ruleFileStream);
  1276. ruleFileStream
  1277. << "# Targets provided globally by CMake.\n"
  1278. << "\n";
  1279. cmTargets* targets = &(this->Makefile->GetTargets());
  1280. cmTargets::iterator glIt;
  1281. for ( glIt = targets->begin(); glIt != targets->end(); ++ glIt )
  1282. {
  1283. if ( glIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  1284. {
  1285. std::string targetString = "Special rule for the target " + glIt->first;
  1286. std::vector<std::string> commands;
  1287. std::vector<std::string> depends;
  1288. const char* text = glIt->second.GetProperty("EchoString");
  1289. if ( !text )
  1290. {
  1291. text = "Running external command ...";
  1292. }
  1293. std::set<cmStdString>::const_iterator dit;
  1294. for ( dit = glIt->second.GetUtilities().begin();
  1295. dit != glIt->second.GetUtilities().end();
  1296. ++ dit )
  1297. {
  1298. depends.push_back(dit->c_str());
  1299. }
  1300. this->AppendEcho(commands, text,
  1301. cmLocalUnixMakefileGenerator3::EchoGlobal);
  1302. // Global targets store their rules in pre- and post-build commands.
  1303. this->AppendCustomDepends(depends,
  1304. glIt->second.GetPreBuildCommands());
  1305. this->AppendCustomDepends(depends,
  1306. glIt->second.GetPostBuildCommands());
  1307. this->AppendCustomCommands(commands,
  1308. glIt->second.GetPreBuildCommands());
  1309. this->AppendCustomCommands(commands,
  1310. glIt->second.GetPostBuildCommands());
  1311. std::string targetName = glIt->second.GetName();
  1312. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1313. targetName.c_str(), depends, commands, true);
  1314. // Provide a "/fast" version of the target.
  1315. depends.clear();
  1316. if((targetName == "install") || (targetName == "install_local"))
  1317. {
  1318. // Provide a fast install target that does not depend on all
  1319. // but has the same command.
  1320. depends.push_back("preinstall/fast");
  1321. }
  1322. else
  1323. {
  1324. // Just forward to the real target so at least it will work.
  1325. depends.push_back(targetName);
  1326. commands.clear();
  1327. }
  1328. targetName += "/fast";
  1329. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1330. targetName.c_str(), depends, commands, true);
  1331. }
  1332. }
  1333. std::vector<std::string> depends;
  1334. std::vector<std::string> commands;
  1335. // Write the all rule.
  1336. std::string dir = this->Makefile->GetStartOutputDirectory();
  1337. dir += "/all";
  1338. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1339. depends.push_back("cmake_check_build_system");
  1340. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1341. progressDir += cmake::GetCMakeFilesDirectory();
  1342. {
  1343. cmOStringStream progCmd;
  1344. progCmd <<
  1345. "$(CMAKE_COMMAND) -E cmake_progress_start ";
  1346. progCmd << this->Convert(progressDir.c_str(),
  1347. cmLocalGenerator::FULL,
  1348. cmLocalGenerator::SHELL);
  1349. progCmd << " $(CMAKE_ALL_PROGRESS)";
  1350. commands.push_back(progCmd.str());
  1351. }
  1352. std::string mf2Dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1353. mf2Dir += "Makefile2";
  1354. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1355. dir.c_str()));
  1356. this->CreateCDCommand(commands,
  1357. this->Makefile->GetHomeOutputDirectory(),
  1358. this->Makefile->GetStartOutputDirectory());
  1359. {
  1360. cmOStringStream progCmd;
  1361. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  1362. progCmd << this->Convert(progressDir.c_str(),
  1363. cmLocalGenerator::FULL,
  1364. cmLocalGenerator::SHELL);
  1365. progCmd << " 0";
  1366. commands.push_back(progCmd.str());
  1367. }
  1368. this->WriteMakeRule(ruleFileStream, "The main all target", "all",
  1369. depends, commands, true);
  1370. // Write the clean rule.
  1371. dir = this->Makefile->GetStartOutputDirectory();
  1372. dir += "/clean";
  1373. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1374. commands.clear();
  1375. depends.clear();
  1376. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1377. dir.c_str()));
  1378. this->CreateCDCommand(commands,
  1379. this->Makefile->GetHomeOutputDirectory(),
  1380. this->Makefile->GetStartOutputDirectory());
  1381. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
  1382. depends, commands, true);
  1383. commands.clear();
  1384. depends.clear();
  1385. depends.push_back("clean");
  1386. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
  1387. depends, commands, true);
  1388. // Write the preinstall rule.
  1389. dir = this->Makefile->GetStartOutputDirectory();
  1390. dir += "/preinstall";
  1391. dir = this->Convert(dir.c_str(), HOME_OUTPUT,MAKEFILE);
  1392. commands.clear();
  1393. depends.clear();
  1394. const char* noall =
  1395. this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1396. if(!noall || cmSystemTools::IsOff(noall))
  1397. {
  1398. // Drive the build before installing.
  1399. depends.push_back("all");
  1400. }
  1401. else
  1402. {
  1403. // At least make sure the build system is up to date.
  1404. depends.push_back("cmake_check_build_system");
  1405. }
  1406. commands.push_back
  1407. (this->GetRecursiveMakeCall(mf2Dir.c_str(), dir.c_str()));
  1408. this->CreateCDCommand(commands,
  1409. this->Makefile->GetHomeOutputDirectory(),
  1410. this->Makefile->GetStartOutputDirectory());
  1411. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1412. "preinstall", depends, commands, true);
  1413. depends.clear();
  1414. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1415. "preinstall/fast", depends, commands, true);
  1416. // write the depend rule, really a recompute depends rule
  1417. depends.clear();
  1418. commands.clear();
  1419. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  1420. cmakefileName += "Makefile.cmake";
  1421. this->Convert(cmakefileName.c_str(),HOME_OUTPUT,
  1422. cmLocalGenerator::MAKEFILE);
  1423. std::string runRule =
  1424. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1425. runRule += " --check-build-system ";
  1426. runRule += this->Convert(cmakefileName.c_str(),cmLocalGenerator::NONE,
  1427. cmLocalGenerator::SHELL);
  1428. runRule += " 1";
  1429. commands.push_back(runRule);
  1430. this->WriteMakeRule(ruleFileStream, "clear depends",
  1431. "depend",
  1432. depends, commands, true);
  1433. }
  1434. //----------------------------------------------------------------------------
  1435. void cmLocalUnixMakefileGenerator3::CheckDependencies(cmMakefile* mf,
  1436. bool verbose,
  1437. bool clear)
  1438. {
  1439. // Get the list of target files to check
  1440. const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  1441. if(!infoDef)
  1442. {
  1443. return;
  1444. }
  1445. std::vector<std::string> files;
  1446. cmSystemTools::ExpandListArgument(infoDef, files);
  1447. // For each info file run the check
  1448. cmDependsC checker;
  1449. checker.SetVerbose(verbose);
  1450. checker.SetFileComparison
  1451. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1452. for(std::vector<std::string>::iterator l = files.begin();
  1453. l != files.end(); ++l)
  1454. {
  1455. // either clear or check the files
  1456. std::string dir = cmSystemTools::GetFilenamePath(l->c_str());
  1457. std::string internalDependFile = dir + "/depend.internal";
  1458. std::string dependFile = dir + "/depend.make";
  1459. if (clear)
  1460. {
  1461. checker.Clear(internalDependFile.c_str());
  1462. checker.Clear(dependFile.c_str());
  1463. }
  1464. else
  1465. {
  1466. checker.Check(dependFile.c_str(), internalDependFile.c_str());
  1467. }
  1468. }
  1469. }
  1470. void cmLocalUnixMakefileGenerator3
  1471. ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
  1472. {
  1473. // now write all the language stuff
  1474. // Set the set of files to check for dependency integrity.
  1475. std::set<cmStdString> checkSetLangs;
  1476. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  1477. checkSet = this->GetIntegrityCheckSet()[target.GetName()];
  1478. for(std::map<cmStdString,
  1479. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  1480. l = checkSet.begin(); l != checkSet.end(); ++l)
  1481. {
  1482. checkSetLangs.insert(l->first);
  1483. }
  1484. // list the languages
  1485. cmakefileStream
  1486. << "# The set of files whose dependency integrity should be checked:\n";
  1487. cmakefileStream
  1488. << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  1489. for(std::set<cmStdString>::iterator
  1490. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1491. {
  1492. cmakefileStream << " \"" << l->c_str() << "\"\n";
  1493. }
  1494. cmakefileStream << " )\n";
  1495. // now list the files for each language
  1496. for(std::set<cmStdString>::iterator
  1497. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1498. {
  1499. cmakefileStream
  1500. << "SET(CMAKE_DEPENDS_CHECK_" << l->c_str() << "\n";
  1501. // get the check set for this local gen and language
  1502. cmLocalUnixMakefileGenerator3::IntegrityCheckSet iCheckSet =
  1503. checkSet[*l];
  1504. // for each file
  1505. for(cmLocalUnixMakefileGenerator3::IntegrityCheckSet::const_iterator
  1506. csIter = iCheckSet.begin();
  1507. csIter != iCheckSet.end(); ++csIter)
  1508. {
  1509. cmakefileStream << " \"" << (*csIter)->GetFullPath() << "\"\n";
  1510. // Get the full path name of the object file.
  1511. std::string obj = this->Makefile->GetStartOutputDirectory();
  1512. obj += "/";
  1513. obj += this->GetObjectFileName(target, **csIter);
  1514. cmakefileStream << " \"" <<
  1515. this->Convert(obj.c_str(),
  1516. cmLocalGenerator::FULL).c_str() << "\"\n";
  1517. }
  1518. cmakefileStream << " )\n";
  1519. }
  1520. }
  1521. //----------------------------------------------------------------------------
  1522. std::string
  1523. cmLocalUnixMakefileGenerator3
  1524. ::GetObjectFileName(cmTarget& target,
  1525. const cmSourceFile& source,
  1526. std::string* nameWithoutTargetDir)
  1527. {
  1528. // Get the object file name independent of target.
  1529. std::string objectName = this->GetObjectFileNameWithoutTarget(source);
  1530. if(nameWithoutTargetDir)
  1531. {
  1532. *nameWithoutTargetDir = objectName;
  1533. }
  1534. // Prepend the target directory.
  1535. std::string obj;
  1536. const char* fileTargetDirectory =
  1537. source.GetProperty("MACOSX_PACKAGE_LOCATION");
  1538. if ( fileTargetDirectory )
  1539. {
  1540. objectName = cmSystemTools::GetFilenameName(objectName.c_str());
  1541. std::string targetName;
  1542. std::string targetNameReal;
  1543. target.GetExecutableNames(targetName, targetNameReal,
  1544. this->ConfigurationName.c_str());
  1545. if ( target.GetPropertyAsBool("MACOSX_BUNDLE") )
  1546. {
  1547. // Construct the full path version of the names.
  1548. obj = this->ExecutableOutputPath;
  1549. if(obj.empty())
  1550. {
  1551. obj = this->Makefile->GetStartOutputDirectory();
  1552. obj += "/";
  1553. }
  1554. obj += targetName + ".app/Contents/";
  1555. obj += fileTargetDirectory;
  1556. }
  1557. else
  1558. {
  1559. // Framework not handled yet
  1560. abort();
  1561. }
  1562. obj = cmSystemTools::RelativePath
  1563. (this->Makefile->GetHomeOutputDirectory(), obj.c_str());
  1564. }
  1565. else
  1566. {
  1567. obj = this->GetTargetDirectory(target);
  1568. }
  1569. obj += "/";
  1570. obj += objectName;
  1571. return obj;
  1572. }
  1573. //----------------------------------------------------------------------------
  1574. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  1575. {
  1576. os
  1577. << "# CMAKE generated file: DO NOT EDIT!\n"
  1578. << "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
  1579. << " Generator, CMake Version "
  1580. << cmMakefile::GetMajorVersion() << "."
  1581. << cmMakefile::GetMinorVersion() << "\n\n";
  1582. }
  1583. //----------------------------------------------------------------------------
  1584. std::string
  1585. cmLocalUnixMakefileGenerator3
  1586. ::GetRecursiveMakeCall(const char *makefile, const char* tgt)
  1587. {
  1588. // Call make on the given file.
  1589. std::string cmd;
  1590. cmd += "$(MAKE) -f ";
  1591. cmd += makefile;
  1592. cmd += " ";
  1593. // Passg down verbosity level.
  1594. if(this->GetMakeSilentFlag().size())
  1595. {
  1596. cmd += this->GetMakeSilentFlag();
  1597. cmd += " ";
  1598. }
  1599. // Most unix makes will pass the command line flags to make down to
  1600. // sub-invoked makes via an environment variable. However, some
  1601. // makes do not support that, so you have to pass the flags
  1602. // explicitly.
  1603. if(this->GetPassMakeflags())
  1604. {
  1605. cmd += "-$(MAKEFLAGS) ";
  1606. }
  1607. // Add the target.
  1608. if (tgt && tgt[0] != '\0')
  1609. {
  1610. std::string tgt2 = this->Convert(tgt,HOME_OUTPUT,MAKEFILE);
  1611. tgt2 = this->ConvertToMakeTarget(tgt2.c_str());
  1612. cmd += tgt2;
  1613. }
  1614. return cmd;
  1615. }
  1616. //----------------------------------------------------------------------------
  1617. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  1618. {
  1619. os
  1620. << "#======================================"
  1621. << "=======================================\n";
  1622. }
  1623. //----------------------------------------------------------------------------
  1624. void
  1625. cmLocalUnixMakefileGenerator3
  1626. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1627. {
  1628. // Write the given string to the stream with escaping to get it back
  1629. // into CMake through the lexical scanner.
  1630. os << "\"";
  1631. for(const char* c = s; *c; ++c)
  1632. {
  1633. if(*c == '\\')
  1634. {
  1635. os << "\\\\";
  1636. }
  1637. else if(*c == '"')
  1638. {
  1639. os << "\\\"";
  1640. }
  1641. else
  1642. {
  1643. os << *c;
  1644. }
  1645. }
  1646. os << "\"";
  1647. }
  1648. //----------------------------------------------------------------------------
  1649. std::string
  1650. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1651. {
  1652. // Split the path into its components.
  1653. std::vector<std::string> components;
  1654. cmSystemTools::SplitPath(p, components);
  1655. // Return an empty path if there are no components.
  1656. if(components.empty())
  1657. {
  1658. return "\"\"";
  1659. }
  1660. // Choose a slash direction and fix root component.
  1661. const char* slash = "/";
  1662. #if defined(_WIN32) && !defined(__CYGWIN__)
  1663. if(!cmSystemTools::GetForceUnixPaths())
  1664. {
  1665. slash = "\\";
  1666. for(std::string::iterator i = components[0].begin();
  1667. i != components[0].end(); ++i)
  1668. {
  1669. if(*i == '/')
  1670. {
  1671. *i = '\\';
  1672. }
  1673. }
  1674. }
  1675. #endif
  1676. // Begin the quoted result with the root component.
  1677. std::string result = "\"";
  1678. result += components[0];
  1679. // Now add the rest of the components separated by the proper slash
  1680. // direction for this platform.
  1681. bool first = true;
  1682. for(unsigned int i=1; i < components.size(); ++i)
  1683. {
  1684. // Only the last component can be empty to avoid double slashes.
  1685. if(components[i].length() > 0 || (i == (components.size()-1)))
  1686. {
  1687. if(!first)
  1688. {
  1689. result += slash;
  1690. }
  1691. result += components[i];
  1692. first = false;
  1693. }
  1694. }
  1695. // Close the quoted result.
  1696. result += "\"";
  1697. return result;
  1698. }
  1699. //----------------------------------------------------------------------------
  1700. std::string
  1701. cmLocalUnixMakefileGenerator3::GetTargetDirectory(cmTarget& target)
  1702. {
  1703. std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1704. dir += target.GetName();
  1705. dir += ".dir";
  1706. return dir;
  1707. }
  1708. void cmLocalUnixMakefileGenerator3
  1709. ::CreateCDCommand(std::vector<std::string>& commands, const char *tgtDir,
  1710. const char *retDir)
  1711. {
  1712. // do we need to cd?
  1713. if (!strcmp(tgtDir,retDir))
  1714. {
  1715. return;
  1716. }
  1717. if(!this->UnixCD)
  1718. {
  1719. // On Windows we must perform each step separately and then change
  1720. // back because the shell keeps the working directory between
  1721. // commands.
  1722. std::string cmd = "cd ";
  1723. cmd += this->ConvertToOutputForExisting(tgtDir);
  1724. commands.insert(commands.begin(),cmd);
  1725. // Change back to the starting directory. Any trailing slash must be
  1726. // removed to avoid problems with Borland Make.
  1727. std::string back = retDir;
  1728. if(back.size() && back[back.size()-1] == '/')
  1729. {
  1730. back = back.substr(0, back.size()-1);
  1731. }
  1732. cmd = "cd ";
  1733. cmd += this->ConvertToOutputForExisting(back.c_str());
  1734. commands.push_back(cmd);
  1735. }
  1736. else
  1737. {
  1738. // On UNIX we must construct a single shell command to change
  1739. // directory and build because make resets the directory between
  1740. // each command.
  1741. std::vector<std::string>::iterator i = commands.begin();
  1742. for (; i != commands.end(); ++i)
  1743. {
  1744. std::string cmd = "cd ";
  1745. cmd += this->ConvertToOutputForExisting(tgtDir);
  1746. cmd += " && ";
  1747. cmd += *i;
  1748. *i = cmd;
  1749. }
  1750. }
  1751. }