cmLocalUnixMakefileGenerator3.cxx 61 KB

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