cmLocalUnixMakefileGenerator3.cxx 61 KB

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