cmUnixMakefileGenerator.cxx 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmUnixMakefileGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmStandardIncludes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. cmUnixMakefileGenerator::cmUnixMakefileGenerator()
  22. :m_SharedLibraryExtension("$(SHLIB_SUFFIX)"),
  23. m_ObjectFileExtension(".o"),
  24. m_ExecutableExtension(""),
  25. m_StaticLibraryExtension(".a"),
  26. m_LibraryPrefix("lib")
  27. {
  28. m_CacheOnly = false;
  29. m_Recurse = false;
  30. }
  31. cmUnixMakefileGenerator::~cmUnixMakefileGenerator()
  32. {
  33. }
  34. void cmUnixMakefileGenerator::GenerateMakefile()
  35. {
  36. // suppoirt override in output directories
  37. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  38. {
  39. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  40. if(m_LibraryOutputPath.size())
  41. {
  42. if(m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
  43. {
  44. m_LibraryOutputPath += "/";
  45. }
  46. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  47. {
  48. cmSystemTools::Error("Error failed create "
  49. "LIBRARY_OUTPUT_PATH directory:",
  50. m_LibraryOutputPath.c_str());
  51. }
  52. m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  53. }
  54. }
  55. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  56. {
  57. m_ExecutableOutputPath =
  58. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  59. if(m_ExecutableOutputPath.size())
  60. {
  61. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
  62. {
  63. m_ExecutableOutputPath += "/";
  64. }
  65. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  66. {
  67. cmSystemTools::Error("Error failed to create "
  68. "EXECUTABLE_OUTPUT_PATH directory:",
  69. m_ExecutableOutputPath.c_str());
  70. }
  71. m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
  72. }
  73. }
  74. if(m_CacheOnly)
  75. {
  76. // Generate the cache only stuff
  77. this->GenerateCacheOnly();
  78. // if recurse then generate for all sub- makefiles
  79. if(m_Recurse)
  80. {
  81. this->RecursiveGenerateCacheOnly();
  82. }
  83. }
  84. // normal makefile output
  85. else
  86. {
  87. // Generate depends
  88. cmMakeDepend md;
  89. md.SetMakefile(m_Makefile);
  90. md.GenerateMakefileDependencies();
  91. this->ProcessDepends(md);
  92. // output the makefile fragment
  93. this->OutputMakefile("Makefile");
  94. }
  95. }
  96. void cmUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  97. {
  98. // Now create cmDependInformation objects for files in the directory
  99. cmTargets &tgts = m_Makefile->GetTargets();
  100. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  101. {
  102. std::vector<cmSourceFile> &classes = l->second.GetSourceFiles();
  103. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  104. i != classes.end(); ++i)
  105. {
  106. if(!i->GetIsAHeaderFileOnly())
  107. {
  108. // get the depends
  109. const cmDependInformation *info =
  110. md.GetDependInformationForSourceFile(*i);
  111. // Delete any hints from the source file's dependencies.
  112. i->GetDepends().erase(i->GetDepends().begin(), i->GetDepends().end());
  113. // Now add the real dependencies for the file.
  114. if (info)
  115. {
  116. for(cmDependInformation::DependencySet::const_iterator d =
  117. info->m_DependencySet.begin();
  118. d != info->m_DependencySet.end(); ++d)
  119. {
  120. // Make sure the full path is given. If not, the dependency was
  121. // not found.
  122. if((*d)->m_FullPath != "")
  123. {
  124. i->GetDepends().push_back((*d)->m_FullPath);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. // This is where CMakeTargets.make is generated
  133. void cmUnixMakefileGenerator::OutputMakefile(const char* file)
  134. {
  135. // Create sub directories fro aux source directories
  136. std::vector<std::string>& auxSourceDirs =
  137. m_Makefile->GetAuxSourceDirectories();
  138. if( auxSourceDirs.size() )
  139. {
  140. // For the case when this is running as a remote build
  141. // on unix, make the directory
  142. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  143. i != auxSourceDirs.end(); ++i)
  144. {
  145. if(i->c_str()[0] != '/')
  146. {
  147. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  148. if(dir.size() && dir[dir.size()-1] != '/')
  149. {
  150. dir += "/";
  151. }
  152. dir += *i;
  153. cmSystemTools::MakeDirectory(dir.c_str());
  154. }
  155. else
  156. {
  157. cmSystemTools::MakeDirectory(i->c_str());
  158. }
  159. }
  160. }
  161. // Create a stream that writes to a temporary file
  162. // then does a copy at the end. This is to allow users
  163. // to hit control-c during the make of the makefile
  164. cmGeneratedFileStream tempFile(file);
  165. tempFile.SetAlwaysCopy(true);
  166. std::ostream& fout = tempFile.GetStream();
  167. if(!fout)
  168. {
  169. cmSystemTools::Error("Error can not open for write: ", file);
  170. return;
  171. }
  172. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  173. << "# Generated by \"" << this->GetName() << "\""
  174. << " Generator, CMake Version "
  175. << cmMakefile::GetMajorVersion() << "."
  176. << cmMakefile::GetMinorVersion() << "\n"
  177. << "# Generated from the following files:\n# "
  178. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  179. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  180. // sort the array
  181. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  182. // remove duplicates
  183. std::vector<std::string>::iterator new_end =
  184. std::unique(lfiles.begin(), lfiles.end());
  185. lfiles.erase(new_end, lfiles.end());
  186. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  187. i != lfiles.end(); ++i)
  188. {
  189. fout << "# " << i->c_str() << "\n";
  190. }
  191. fout << "\n\n";
  192. fout << "# Suppresses display of executed commands\n";
  193. fout << ".SILENT:\n";
  194. fout << "# disable some common implicit rules to speed things up\n";
  195. fout << ".SUFFIXES:\n";
  196. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  197. // create a make variable with all of the sources for this Makefile
  198. // for depend purposes.
  199. fout << "CMAKE_MAKEFILE_SOURCES = ";
  200. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  201. i != lfiles.end(); ++i)
  202. {
  203. fout << " " << this->ConvertToOutputPath(i->c_str());
  204. }
  205. // Add the cache to the list
  206. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  207. cacheFile += "/CMakeCache.txt";
  208. fout << " " << this->ConvertToOutputPath(cacheFile.c_str());
  209. fout << "\n\n\n";
  210. this->OutputMakeVariables(fout);
  211. // Set up the default target as the VERY first target, so that make with no arguments will run it
  212. this->
  213. OutputMakeRule(fout,
  214. "Default target executed when no arguments are given to make, first make sure cmake.depends exists, cmake.check_depends is up-to-date, check the sources, then build the all target",
  215. "default_target",
  216. 0,
  217. "$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends",
  218. "$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends",
  219. "$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends",
  220. "$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) all");
  221. this->OutputTargetRules(fout);
  222. this->OutputDependLibs(fout);
  223. this->OutputTargets(fout);
  224. this->OutputSubDirectoryRules(fout);
  225. std::string dependName = m_Makefile->GetStartOutputDirectory();
  226. dependName += "/cmake.depends";
  227. if(!this->m_CacheOnly)
  228. {
  229. std::ofstream dependout(dependName.c_str());
  230. if(!dependout)
  231. {
  232. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  233. return;
  234. }
  235. dependout << "# .o dependencies in this directory." << std::endl;
  236. std::string checkDepend = m_Makefile->GetStartOutputDirectory();
  237. checkDepend += "/cmake.check_depends";
  238. std::ofstream checkdependout(checkDepend.c_str());
  239. if(!checkdependout)
  240. {
  241. cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
  242. return;
  243. }
  244. checkdependout << "# This file is used as a tag file, that all sources depend on. If a source changes, then the rule to rebuild this file will cause cmake.depends to be rebuilt." << std::endl;
  245. // if there were any depends output, then output the check depends
  246. // information inot checkdependout
  247. if(this->OutputObjectDepends(dependout))
  248. {
  249. this->OutputCheckDepends(checkdependout);
  250. }
  251. else
  252. {
  253. checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
  254. }
  255. }
  256. this->OutputCustomRules(fout);
  257. this->OutputMakeRules(fout);
  258. this->OutputInstallRules(fout);
  259. // only add the depend include if the depend file exists
  260. if(cmSystemTools::FileExists(dependName.c_str()))
  261. {
  262. this->OutputIncludeMakefile(fout, "cmake.depends");
  263. }
  264. }
  265. void cmUnixMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  266. const char* file)
  267. {
  268. fout << "include " << file << "\n";
  269. }
  270. std::string
  271. cmUnixMakefileGenerator::GetOutputExtension(const char*)
  272. {
  273. return m_ObjectFileExtension;
  274. }
  275. // Output the rules for any targets
  276. void cmUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  277. {
  278. // for each target add to the list of targets
  279. fout << "TARGETS = ";
  280. const cmTargets &tgts = m_Makefile->GetTargets();
  281. // list libraries first
  282. for(cmTargets::const_iterator l = tgts.begin();
  283. l != tgts.end(); l++)
  284. {
  285. if (l->second.IsInAll())
  286. {
  287. std::string path = m_LibraryOutputPath + m_LibraryPrefix;
  288. if(l->second.GetType() == cmTarget::STATIC_LIBRARY)
  289. {
  290. path = path + l->first + m_StaticLibraryExtension;
  291. fout << " \\\n"
  292. << this->ConvertToOutputPath(path.c_str());
  293. }
  294. else if(l->second.GetType() == cmTarget::SHARED_LIBRARY)
  295. {
  296. path = path + l->first +
  297. m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  298. fout << " \\\n"
  299. << this->ConvertToOutputPath(path.c_str());
  300. }
  301. else if(l->second.GetType() == cmTarget::MODULE_LIBRARY)
  302. {
  303. path = path + l->first +
  304. m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  305. fout << " \\\n"
  306. << this->ConvertToOutputPath(path.c_str());
  307. }
  308. }
  309. }
  310. // executables
  311. for(cmTargets::const_iterator l = tgts.begin();
  312. l != tgts.end(); l++)
  313. {
  314. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  315. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  316. l->second.IsInAll())
  317. {
  318. std::string path = m_ExecutableOutputPath + l->first +
  319. m_ExecutableExtension;
  320. fout << " \\\n" << this->ConvertToOutputPath(path.c_str());
  321. }
  322. }
  323. // list utilities last
  324. for(cmTargets::const_iterator l = tgts.begin();
  325. l != tgts.end(); l++)
  326. {
  327. if (l->second.GetType() == cmTarget::UTILITY &&
  328. l->second.IsInAll())
  329. {
  330. fout << " \\\n" << l->first.c_str();
  331. }
  332. }
  333. fout << "\n\n";
  334. // get the classes from the source lists then add them to the groups
  335. for(cmTargets::const_iterator l = tgts.begin();
  336. l != tgts.end(); l++)
  337. {
  338. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  339. if (classes.begin() != classes.end())
  340. {
  341. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS") << " = ";
  342. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  343. i != classes.end(); i++)
  344. {
  345. if(!i->IsAHeaderFileOnly())
  346. {
  347. std::string outExt(this->GetOutputExtension(i->GetSourceExtension().c_str()));
  348. if(outExt.size())
  349. {
  350. fout << "\\\n" << this->ConvertToOutputPath(i->GetSourceName().c_str())
  351. << outExt.c_str() << " ";
  352. }
  353. }
  354. }
  355. fout << "\n\n";
  356. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS_QUOTED") << " = ";
  357. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  358. i != classes.end(); i++)
  359. {
  360. if(!i->IsAHeaderFileOnly())
  361. {
  362. std::string outExt(this->GetOutputExtension(i->GetSourceExtension().c_str()));
  363. if(outExt.size())
  364. {
  365. fout << "\\\n\"" << this->ConvertToOutputPath(i->GetSourceName().c_str())
  366. << outExt.c_str() << "\" ";
  367. }
  368. }
  369. }
  370. fout << "\n\n";
  371. }
  372. }
  373. fout << "CLEAN_OBJECT_FILES = ";
  374. for(cmTargets::const_iterator l = tgts.begin();
  375. l != tgts.end(); l++)
  376. {
  377. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  378. if (classes.begin() != classes.end())
  379. {
  380. fout << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
  381. << ") ";
  382. }
  383. }
  384. fout << "\n\n";
  385. }
  386. /**
  387. * Output the linking rules on a command line. For executables,
  388. * targetLibrary should be a NULL pointer. For libraries, it should point
  389. * to the name of the library. This will not link a library against itself.
  390. */
  391. void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  392. const char* targetLibrary,
  393. const cmTarget &tgt)
  394. {
  395. // Try to emit each search path once
  396. std::set<std::string> emitted;
  397. // Embed runtime search paths if possible and if required.
  398. bool outputRuntime = true;
  399. std::string runtimeFlag;
  400. std::string runtimeSep;
  401. std::vector<std::string> runtimeDirs;
  402. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG"))
  403. {
  404. runtimeFlag = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG");
  405. }
  406. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP"))
  407. {
  408. runtimeSep = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP");
  409. }
  410. // concatenate all paths or no?
  411. bool runtimeConcatenate = ( runtimeSep!="" );
  412. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  413. {
  414. outputRuntime = false;
  415. }
  416. // Some search paths should never be emitted
  417. emitted.insert("");
  418. emitted.insert("/usr/lib");
  419. // collect all the flags needed for linking libraries
  420. std::string linkLibs;
  421. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  422. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  423. libDir != libdirs.end(); ++libDir)
  424. {
  425. std::string libpath = this->ConvertToOutputPath(libDir->c_str());
  426. if(emitted.insert(libpath).second)
  427. {
  428. std::string::size_type pos = libDir->find("-L");
  429. if((pos == std::string::npos || pos > 0)
  430. && libDir->find("${") == std::string::npos)
  431. {
  432. linkLibs += "-L";
  433. if(outputRuntime)
  434. {
  435. runtimeDirs.push_back( libpath );
  436. }
  437. }
  438. linkLibs += libpath;
  439. linkLibs += " ";
  440. }
  441. }
  442. std::string librariesLinked;
  443. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  444. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  445. lib != libs.end(); ++lib)
  446. {
  447. // Don't link the library against itself!
  448. if(targetLibrary && (lib->first == targetLibrary)) continue;
  449. // don't look at debug libraries
  450. if (lib->second == cmTarget::DEBUG) continue;
  451. // skip zero size library entries, this may happen
  452. // if a variable expands to nothing.
  453. if (lib->first.size() == 0) continue;
  454. // if it is a full path break it into -L and -l
  455. cmRegularExpression reg("(^[ \t]*\\-l)|(^[ \t]*\\-framework)|(\\${)");
  456. if(lib->first.find('/') != std::string::npos
  457. && !reg.find(lib->first))
  458. {
  459. std::string dir, file;
  460. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  461. dir, file);
  462. std::string libpath = this->ConvertToOutputPath(dir.c_str());
  463. if(emitted.insert(libpath).second)
  464. {
  465. linkLibs += "-L";
  466. linkLibs += libpath;
  467. linkLibs += " ";
  468. if(outputRuntime)
  469. {
  470. runtimeDirs.push_back( libpath );
  471. }
  472. }
  473. cmRegularExpression libname("lib(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
  474. cmRegularExpression libname_noprefix("(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
  475. if(libname.find(file))
  476. {
  477. librariesLinked += "-l";
  478. file = libname.match(1);
  479. librariesLinked += file;
  480. librariesLinked += " ";
  481. }
  482. else if(libname_noprefix.find(file))
  483. {
  484. librariesLinked += "-l";
  485. file = libname_noprefix.match(1);
  486. librariesLinked += file;
  487. librariesLinked += " ";
  488. }
  489. }
  490. // not a full path, so add -l name
  491. else
  492. {
  493. if(!reg.find(lib->first))
  494. {
  495. librariesLinked += "-l";
  496. }
  497. librariesLinked += lib->first;
  498. librariesLinked += " ";
  499. }
  500. }
  501. linkLibs += librariesLinked;
  502. if(!targetLibrary)
  503. {
  504. // For executables, add these a second time so order does not matter
  505. linkLibs += librariesLinked;
  506. }
  507. fout << linkLibs;
  508. if(outputRuntime && runtimeDirs.size()>0)
  509. {
  510. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  511. // a "-R a -R b -R c" type link line
  512. fout << runtimeFlag;
  513. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  514. fout << *itr;
  515. ++itr;
  516. for( ; itr != runtimeDirs.end(); ++itr )
  517. {
  518. if(runtimeConcatenate)
  519. {
  520. fout << runtimeSep << *itr;
  521. }
  522. else
  523. {
  524. fout << " " << runtimeFlag << *itr;
  525. }
  526. }
  527. fout << " ";
  528. }
  529. }
  530. std::string cmUnixMakefileGenerator::CreateTargetRules(const cmTarget &target,
  531. const char* targetName)
  532. {
  533. std::string customRuleCode = "";
  534. bool initNext = false;
  535. for (std::vector<cmCustomCommand>::const_iterator cr =
  536. target.GetCustomCommands().begin();
  537. cr != target.GetCustomCommands().end(); ++cr)
  538. {
  539. cmCustomCommand cc(*cr);
  540. cc.ExpandVariables(*m_Makefile);
  541. if (cc.GetSourceName() == targetName)
  542. {
  543. if(initNext)
  544. {
  545. customRuleCode += "\n\t";
  546. }
  547. else
  548. {
  549. initNext = true;
  550. }
  551. std::string command = this->ConvertToOutputPath(cc.GetCommand().c_str());
  552. customRuleCode += command + " " + cc.GetArguments();
  553. }
  554. }
  555. return customRuleCode;
  556. }
  557. void cmUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  558. const char* name,
  559. const cmTarget &t)
  560. {
  561. std::string target = m_LibraryOutputPath + "lib" + name + "$(SHLIB_SUFFIX)";
  562. std::string depend = "$(";
  563. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  564. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  565. std::string command = "$(RM) lib";
  566. command += name;
  567. command += "$(SHLIB_SUFFIX)";
  568. std::string command2 = "$(CMAKE_CXX_COMPILER) $(CMAKE_SHLIB_LINK_FLAGS) "
  569. "$(CMAKE_SHLIB_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  570. command2 += "\t ";
  571. std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(SHLIB_SUFFIX)";
  572. libName = this->ConvertToOutputPath(libName.c_str());
  573. command2 += libName + " \\\n";
  574. command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  575. std::strstream linklibs;
  576. this->OutputLinkLibraries(linklibs, name, t);
  577. linklibs << std::ends;
  578. command2 += linklibs.str();
  579. delete [] linklibs.str();
  580. std::string customCommands = this->CreateTargetRules(t, name);
  581. const char* cc = 0;
  582. if(customCommands.size() > 0)
  583. {
  584. cc = customCommands.c_str();
  585. }
  586. this->OutputMakeRule(fout, "rules for a shared library",
  587. target.c_str(),
  588. depend.c_str(),
  589. command.c_str(),
  590. command2.c_str(),
  591. cc);
  592. }
  593. void cmUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  594. const char* name,
  595. const cmTarget &t)
  596. {
  597. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  598. std::string depend = "$(";
  599. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  600. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  601. std::string command = "$(RM) lib" + std::string(name) + "$(MODULE_SUFFIX)";
  602. std::string command2 = "$(CMAKE_CXX_COMPILER) $(CMAKE_MODULE_LINK_FLAGS) "
  603. "$(CMAKE_MODULE_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  604. command2 += "\t ";
  605. std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  606. libName = this->ConvertToOutputPath(libName.c_str());
  607. command2 += libName + " \\\n";
  608. command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  609. std::strstream linklibs;
  610. this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
  611. linklibs << std::ends;
  612. command2 += linklibs.str();
  613. delete [] linklibs.str();
  614. std::string customCommands = this->CreateTargetRules(t, name);
  615. const char* cc = 0;
  616. if(customCommands.size() > 0)
  617. {
  618. cc = customCommands.c_str();
  619. }
  620. this->OutputMakeRule(fout, "rules for a shared module library",
  621. target.c_str(),
  622. depend.c_str(),
  623. command.c_str(),
  624. command2.c_str(),
  625. cc);
  626. }
  627. void cmUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  628. const char* name,
  629. const cmTarget &t)
  630. {
  631. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + ".a";
  632. target = this->ConvertToOutputPath(target.c_str());
  633. std::string depend = "$(";
  634. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  635. std::string command = "$(CMAKE_AR) $(CMAKE_AR_ARGS) ";
  636. command += target;
  637. command += " $(";
  638. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  639. std::string command2 = "$(CMAKE_RANLIB) ";
  640. command2 += target;
  641. std::string comment = "rule to build static library: ";
  642. comment += name;
  643. std::string customCommands = this->CreateTargetRules(t, name);
  644. const char* cc = 0;
  645. if(customCommands.size() > 0)
  646. {
  647. cc = customCommands.c_str();
  648. }
  649. this->OutputMakeRule(fout,
  650. comment.c_str(),
  651. target.c_str(),
  652. depend.c_str(),
  653. command.c_str(),
  654. command2.c_str(),
  655. cc);
  656. }
  657. void cmUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  658. const char* name,
  659. const cmTarget &t)
  660. {
  661. std::string target = m_ExecutableOutputPath + name;
  662. std::string depend = "$(";
  663. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  664. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  665. std::string command =
  666. "$(CMAKE_CXX_COMPILER) $(CMAKE_SHLIB_LINK_FLAGS) $(CMAKE_CXX_FLAGS) ";
  667. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  668. std::strstream linklibs;
  669. this->OutputLinkLibraries(linklibs, 0, t);
  670. linklibs << std::ends;
  671. command += linklibs.str();
  672. std::string outputFile = m_ExecutableOutputPath + name;
  673. command += " -o " + this->ConvertToOutputPath(outputFile.c_str());
  674. std::string comment = "rule to build executable: ";
  675. comment += name;
  676. std::string customCommands = this->CreateTargetRules(t, name);
  677. const char* cc = 0;
  678. if(customCommands.size() > 0)
  679. {
  680. cc = customCommands.c_str();
  681. }
  682. this->OutputMakeRule(fout,
  683. comment.c_str(),
  684. target.c_str(),
  685. depend.c_str(),
  686. command.c_str(),
  687. cc);
  688. }
  689. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  690. {
  691. // for each target
  692. const cmTargets &tgts = m_Makefile->GetTargets();
  693. for(cmTargets::const_iterator l = tgts.begin();
  694. l != tgts.end(); l++)
  695. {
  696. switch(l->second.GetType())
  697. {
  698. case cmTarget::STATIC_LIBRARY:
  699. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  700. break;
  701. case cmTarget::SHARED_LIBRARY:
  702. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  703. break;
  704. case cmTarget::MODULE_LIBRARY:
  705. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  706. break;
  707. case cmTarget::EXECUTABLE:
  708. case cmTarget::WIN32_EXECUTABLE:
  709. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  710. break;
  711. case cmTarget::UTILITY:
  712. // This is handled by the OutputCustomRules method
  713. case cmTarget::INSTALL_FILES:
  714. // This is handled by the OutputInstallRules method
  715. case cmTarget::INSTALL_PROGRAMS:
  716. // This is handled by the OutputInstallRules method
  717. break;
  718. }
  719. }
  720. }
  721. // For each target that is an executable or shared library, generate
  722. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  723. void cmUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  724. {
  725. // Build a set of libraries that will be linked into any target in
  726. // this directory.
  727. std::set<std::string> used;
  728. // for each target
  729. const cmTargets &tgts = m_Makefile->GetTargets();
  730. for(cmTargets::const_iterator l = tgts.begin();
  731. l != tgts.end(); l++)
  732. {
  733. // Each dependency should only be emitted once per target.
  734. std::set<std::string> emitted;
  735. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  736. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  737. || (l->second.GetType() == cmTarget::EXECUTABLE)
  738. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  739. {
  740. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  741. // A library should not depend on itself!
  742. emitted.insert(l->first);
  743. // First look at all makefile level link libraries.
  744. const cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  745. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  746. lib != libs.end(); ++lib)
  747. {
  748. // Record that this library was used.
  749. used.insert(lib->first);
  750. // Don't emit the same library twice for this target.
  751. if(emitted.insert(lib->first).second)
  752. {
  753. // Output this dependency.
  754. this->OutputLibDepend(fout, lib->first.c_str());
  755. }
  756. }
  757. // Now, look at all link libraries specific to this target.
  758. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  759. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  760. lib != tlibs.end(); ++lib)
  761. {
  762. // Record that this library was used.
  763. used.insert(lib->first);
  764. // Don't emit the same library twice for this target.
  765. if(emitted.insert(lib->first).second)
  766. {
  767. // Output this dependency.
  768. this->OutputLibDepend(fout, lib->first.c_str());
  769. }
  770. }
  771. // Now, look at all utilities specific to this target.
  772. const std::set<std::string>& tutils = l->second.GetUtilities();
  773. for(std::set<std::string>::const_iterator util = tutils.begin();
  774. util != tutils.end(); ++util)
  775. {
  776. // Record that this utility was used.
  777. used.insert(*util);
  778. // Don't emit the same utility twice for this target.
  779. if(emitted.insert(*util).second)
  780. {
  781. // Output this dependency.
  782. this->OutputExeDepend(fout, util->c_str());
  783. }
  784. }
  785. fout << "\n";
  786. }
  787. }
  788. fout << "\n";
  789. // Loop over the libraries used and make sure there is a rule to
  790. // build them in this makefile. If the library is in another
  791. // directory, add a rule to jump to that directory and make sure it
  792. // exists.
  793. for(std::set<std::string>::const_iterator lib = used.begin();
  794. lib != used.end(); ++lib)
  795. {
  796. // loop over the list of directories that the libraries might
  797. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  798. // be stored in the cache
  799. const char* cacheValue = m_Makefile->GetDefinition(lib->c_str());
  800. // if cache and not the current directory add a rule, to
  801. // jump into the directory and build for the first time
  802. if(cacheValue &&
  803. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  804. {
  805. // add the correct extension
  806. std::string ltname = *lib+"_LIBRARY_TYPE";
  807. const char* libType
  808. = m_Makefile->GetDefinition(ltname.c_str());
  809. // if it was a library..
  810. if (libType)
  811. {
  812. std::string library = m_LibraryPrefix;
  813. library += *lib;
  814. std::string libpath = cacheValue;
  815. if(libType && std::string(libType) == "SHARED")
  816. {
  817. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  818. }
  819. else if(libType && std::string(libType) == "MODULE")
  820. {
  821. library += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  822. }
  823. else if(libType && std::string(libType) == "STATIC")
  824. {
  825. library += m_StaticLibraryExtension;
  826. }
  827. else
  828. {
  829. cmSystemTools::Error("Unknown library type!");
  830. return;
  831. }
  832. if(m_LibraryOutputPath.size())
  833. {
  834. libpath = m_LibraryOutputPath;
  835. }
  836. else
  837. {
  838. libpath += "/";
  839. }
  840. libpath += library;
  841. // put out a rule to build the library if it does not exist
  842. this->OutputBuildLibraryInDir(fout,
  843. cacheValue,
  844. library.c_str(),
  845. libpath.c_str());
  846. }
  847. // something other than a library...
  848. else
  849. {
  850. std::string exepath = cacheValue;
  851. if(m_ExecutableOutputPath.size())
  852. {
  853. exepath = m_ExecutableOutputPath;
  854. }
  855. else
  856. {
  857. exepath += "/";
  858. }
  859. exepath += *lib;
  860. this->OutputBuildExecutableInDir(fout,
  861. cacheValue,
  862. lib->c_str(),
  863. exepath.c_str());
  864. }
  865. }
  866. }
  867. }
  868. void cmUnixMakefileGenerator::OutputBuildLibraryInDir(std::ostream& fout,
  869. const char* path,
  870. const char* library,
  871. const char* fullpath)
  872. {
  873. const char* makeTarget = library;
  874. const char* libOutPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  875. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  876. {
  877. makeTarget = fullpath;
  878. }
  879. fout << this->ConvertToOutputPath(fullpath)
  880. << ":\n\tcd " << this->ConvertToOutputPath(path)
  881. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends"
  882. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends"
  883. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends"
  884. << "; $(MAKE) $(MAKESILENT) " << makeTarget << "\n\n";
  885. }
  886. void cmUnixMakefileGenerator::OutputBuildExecutableInDir(std::ostream& fout,
  887. const char* path,
  888. const char* library,
  889. const char* fullpath)
  890. {
  891. const char* makeTarget = library;
  892. const char* libOutPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  893. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  894. {
  895. makeTarget = fullpath;
  896. }
  897. fout << this->ConvertToOutputPath(fullpath)
  898. << ":\n\tcd " << this->ConvertToOutputPath(path)
  899. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends"
  900. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends"
  901. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends"
  902. << "; $(MAKE) $(MAKESILENT) " << makeTarget << "\n\n";
  903. }
  904. bool cmUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  905. {
  906. return strcmp(path1, path2) == 0;
  907. }
  908. void cmUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  909. const char* name)
  910. {
  911. const char* cacheValue = m_Makefile->GetDefinition(name);
  912. if(cacheValue )
  913. {
  914. // if there is a cache value, then this is a library that cmake
  915. // knows how to build, so we can depend on it
  916. std::string libpath;
  917. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  918. {
  919. // if the library is not in the current directory, then get the full
  920. // path to it
  921. if(m_LibraryOutputPath.size())
  922. {
  923. libpath = m_LibraryOutputPath;
  924. libpath += m_LibraryPrefix;
  925. }
  926. else
  927. {
  928. libpath = cacheValue;
  929. libpath += "/";
  930. libpath += m_LibraryPrefix;
  931. }
  932. }
  933. else
  934. {
  935. // library is in current Makefile so use lib as a prefix
  936. libpath = m_LibraryOutputPath;
  937. libpath += m_LibraryPrefix;
  938. }
  939. // add the library name
  940. libpath += name;
  941. // add the correct extension
  942. std::string ltname = name;
  943. ltname += "_LIBRARY_TYPE";
  944. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  945. if(libType && std::string(libType) == "SHARED")
  946. {
  947. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  948. }
  949. else if (libType && std::string(libType) == "MODULE")
  950. {
  951. libpath += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  952. }
  953. else if (libType && std::string(libType) == "STATIC")
  954. {
  955. libpath += m_StaticLibraryExtension;
  956. }
  957. fout << this->ConvertToOutputPath(libpath.c_str()) << " ";
  958. }
  959. }
  960. void cmUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  961. const char* name)
  962. {
  963. const char* cacheValue = m_Makefile->GetDefinition(name);
  964. if(cacheValue )
  965. {
  966. // if there is a cache value, then this is a executable/utility that cmake
  967. // knows how to build, so we can depend on it
  968. std::string exepath;
  969. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  970. {
  971. // if the exe/utility is not in the current directory, then get the full
  972. // path to it
  973. if(m_ExecutableOutputPath.size())
  974. {
  975. exepath = m_ExecutableOutputPath;
  976. }
  977. else
  978. {
  979. exepath = cacheValue;
  980. exepath += "/";
  981. }
  982. }
  983. else
  984. {
  985. // library is in current Makefile
  986. exepath = m_ExecutableOutputPath;
  987. }
  988. // add the library name
  989. exepath += name;
  990. // add the correct extension
  991. if (m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX"))
  992. {
  993. std::string replaceVars =
  994. m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
  995. if (!strcmp(replaceVars.c_str(),"@CMAKE_EXECUTABLE_SUFFIX@"))
  996. {
  997. replaceVars = "";
  998. }
  999. exepath += replaceVars;
  1000. }
  1001. fout << this->ConvertToOutputPath(exepath.c_str()) << " ";
  1002. }
  1003. }
  1004. // fix up names of directories so they can be used
  1005. // as targets in makefiles.
  1006. inline std::string FixDirectoryName(const char* dir)
  1007. {
  1008. std::string s = dir;
  1009. // replace ../ with 3 under bars
  1010. size_t pos = s.find("../");
  1011. if(pos != std::string::npos)
  1012. {
  1013. s.replace(pos, 3, "___");
  1014. }
  1015. // replace / directory separators with a single under bar
  1016. pos = s.find("/");
  1017. while(pos != std::string::npos)
  1018. {
  1019. s.replace(pos, 1, "_");
  1020. pos = s.find("/");
  1021. }
  1022. return s;
  1023. }
  1024. void cmUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1025. const char* dir,
  1026. const char* target1,
  1027. const char* target2)
  1028. {
  1029. std::string directory = this->ConvertToOutputPath(dir);
  1030. if(target1)
  1031. {
  1032. fout << "\t@if test ! -d " << directory
  1033. << "; then $(MAKE) rebuild_cache; fi\n"
  1034. << "\techo Building " << target1 << " in directory " << directory << "\n"
  1035. << "\t@cd " << directory
  1036. << "; $(MAKE) -$(MAKEFLAGS) " << target1 << "\n";
  1037. }
  1038. if(target2)
  1039. {
  1040. fout << "\techo Building " << target2 << " in directory " << directory << "\n";
  1041. fout << "\t@cd " << directory
  1042. << "; $(MAKE) -$(MAKEFLAGS) " << target2 << "\n";
  1043. }
  1044. fout << "\n";
  1045. }
  1046. void
  1047. cmUnixMakefileGenerator::
  1048. OutputSubDirectoryVars(std::ostream& fout,
  1049. const char* var,
  1050. const char* target,
  1051. const char* target1,
  1052. const char* target2,
  1053. const char* depend,
  1054. const std::vector<std::string>& SubDirectories)
  1055. {
  1056. if(!depend)
  1057. {
  1058. depend = "";
  1059. }
  1060. if( SubDirectories.size() == 0)
  1061. {
  1062. return;
  1063. }
  1064. fout << "# Variable for making " << target << " in subdirectories.\n";
  1065. fout << var << " = \\\n";
  1066. unsigned int i;
  1067. for(i =0; i < SubDirectories.size(); i++)
  1068. {
  1069. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1070. fout << target << "_" << subdir.c_str();
  1071. if(i == SubDirectories.size()-1)
  1072. {
  1073. fout << " \n\n";
  1074. }
  1075. else
  1076. {
  1077. fout << " \\\n";
  1078. }
  1079. }
  1080. fout << "# Targets for making " << target << " in subdirectories.\n";
  1081. std::string last = "";
  1082. for(unsigned int i =0; i < SubDirectories.size(); i++)
  1083. {
  1084. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1085. fout << target << "_" << subdir.c_str() << ": " << depend;
  1086. // Make each subdirectory depend on previous one. This forces
  1087. // parallel builds (make -j 2) to build in same order as a single
  1088. // threaded build to avoid dependency problems.
  1089. if(i > 0)
  1090. {
  1091. fout << " " << target << "_" << last.c_str();
  1092. }
  1093. fout << "\n";
  1094. last = subdir;
  1095. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1096. dir += "/";
  1097. dir += SubDirectories[i];
  1098. this->BuildInSubDirectory(fout, dir.c_str(),
  1099. target1, target2);
  1100. }
  1101. fout << "\n\n";
  1102. }
  1103. // output rules for decending into sub directories
  1104. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1105. {
  1106. // Output Sub directory build rules
  1107. const std::vector<std::string>& SubDirectories
  1108. = m_Makefile->GetSubDirectories();
  1109. if( SubDirectories.size() == 0)
  1110. {
  1111. return;
  1112. }
  1113. this->OutputSubDirectoryVars(fout,
  1114. "SUBDIR_BUILD",
  1115. "default_target",
  1116. "default_target",
  1117. 0, "$(TARGETS)",
  1118. SubDirectories);
  1119. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1120. "clean",
  1121. 0, 0,
  1122. SubDirectories);
  1123. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1124. "depend",
  1125. 0, 0,
  1126. SubDirectories);
  1127. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1128. "install",
  1129. 0, 0,
  1130. SubDirectories);
  1131. }
  1132. // Output the depend information for all the classes
  1133. // in the makefile. These would have been generated
  1134. // by the class cmMakeDepend GenerateMakefile
  1135. bool cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1136. {
  1137. bool ret = false;
  1138. // Iterate over every target.
  1139. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1140. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1141. target != targets.end(); ++target)
  1142. {
  1143. // Iterate over every source for this target.
  1144. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1145. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1146. source != sources.end(); ++source)
  1147. {
  1148. if(!source->IsAHeaderFileOnly())
  1149. {
  1150. if(!source->GetDepends().empty())
  1151. {
  1152. fout << source->GetSourceName() << m_ObjectFileExtension << " :";
  1153. // Iterate through all the dependencies for this source.
  1154. for(std::vector<std::string>::const_iterator dep =
  1155. source->GetDepends().begin();
  1156. dep != source->GetDepends().end(); ++dep)
  1157. {
  1158. fout << " \\\n"
  1159. << this->ConvertToOutputPath(dep->c_str());
  1160. ret = true;
  1161. }
  1162. fout << "\n\n";
  1163. }
  1164. }
  1165. }
  1166. }
  1167. return ret;
  1168. }
  1169. // Output the depend information for all the classes
  1170. // in the makefile. These would have been generated
  1171. // by the class cmMakeDepend GenerateMakefile
  1172. void cmUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1173. {
  1174. std::set<std::string> emitted;
  1175. // Iterate over every target.
  1176. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1177. fout << "# Suppresses display of executed commands\n";
  1178. fout << ".SILENT:\n";
  1179. fout << "# disable some common implicit rules to speed things up\n";
  1180. fout << ".SUFFIXES:\n";
  1181. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1182. this->OutputMakeVariables(fout);
  1183. fout << "default:\n";
  1184. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends all\n"
  1185. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1186. fout << "all: ";
  1187. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1188. target != targets.end(); ++target)
  1189. {
  1190. // Iterate over every source for this target.
  1191. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1192. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1193. source != sources.end(); ++source)
  1194. {
  1195. if(!source->IsAHeaderFileOnly())
  1196. {
  1197. if(!source->GetDepends().empty())
  1198. {
  1199. for(std::vector<std::string>::const_iterator dep =
  1200. source->GetDepends().begin();
  1201. dep != source->GetDepends().end(); ++dep)
  1202. {
  1203. std::string dependfile =
  1204. this->ConvertToOutputPath(dep->c_str());
  1205. if(emitted.insert(dependfile).second)
  1206. {
  1207. fout << " \\\n" << dependfile ;
  1208. }
  1209. }
  1210. }
  1211. }
  1212. }
  1213. }
  1214. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1215. fout << "cmake.depends: ";
  1216. std::set<std::string>::iterator i;
  1217. for(i = emitted.begin(); i != emitted.end(); ++i)
  1218. {
  1219. fout << " \\\n" << *i;
  1220. }
  1221. fout << "\n\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) dependlocal\n\n";
  1222. fout << "\n\n";
  1223. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1224. for(std::set<std::string>::iterator i = emitted.begin();
  1225. i != emitted.end(); ++i)
  1226. {
  1227. fout << *i << ":\n"
  1228. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) dependlocal\n\n";
  1229. }
  1230. }
  1231. // Output each custom rule in the following format:
  1232. // output: source depends...
  1233. // (tab) command...
  1234. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1235. {
  1236. // We may be modifying the source groups temporarily, so make a copy.
  1237. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1238. const cmTargets &tgts = m_Makefile->GetTargets();
  1239. for(cmTargets::const_iterator tgt = tgts.begin();
  1240. tgt != tgts.end(); ++tgt)
  1241. {
  1242. // add any custom rules to the source groups
  1243. for (std::vector<cmCustomCommand>::const_iterator cr =
  1244. tgt->second.GetCustomCommands().begin();
  1245. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1246. {
  1247. cmSourceGroup& sourceGroup =
  1248. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1249. sourceGroups);
  1250. cmCustomCommand cc(*cr);
  1251. cc.ExpandVariables(*m_Makefile);
  1252. sourceGroup.AddCustomCommand(cc);
  1253. }
  1254. }
  1255. // Loop through every source group.
  1256. for(std::vector<cmSourceGroup>::const_iterator sg =
  1257. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1258. {
  1259. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1260. if(buildRules.empty())
  1261. { continue; }
  1262. std::string name = sg->GetName();
  1263. if(name != "")
  1264. {
  1265. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1266. }
  1267. // Loop through each source in the source group.
  1268. for(cmSourceGroup::BuildRules::const_iterator cc =
  1269. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1270. {
  1271. std::string source = cc->first;
  1272. const cmSourceGroup::Commands& commands = cc->second;
  1273. // Loop through every command generating code from the current source.
  1274. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1275. c != commands.end(); ++c)
  1276. {
  1277. // escape spaces and convert to native slashes path for
  1278. // the command
  1279. std::string command =
  1280. this->ConvertToOutputPath(c->second.m_Command.c_str());
  1281. command += " ";
  1282. // now add the arguments
  1283. command += c->second.m_Arguments;
  1284. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1285. // if the command has no outputs, then it is a utility command
  1286. // with no outputs
  1287. if(commandFiles.m_Outputs.size() == 0)
  1288. {
  1289. std::string depends;
  1290. // collect out all the dependencies for this rule.
  1291. for(std::set<std::string>::const_iterator d =
  1292. commandFiles.m_Depends.begin();
  1293. d != commandFiles.m_Depends.end(); ++d)
  1294. {
  1295. std::string dep = this->ConvertToOutputPath(d->c_str());
  1296. depends += " ";
  1297. depends += dep;
  1298. }
  1299. // output rule
  1300. this->OutputMakeRule(fout,
  1301. "Custom command",
  1302. source.c_str(),
  1303. depends.c_str(),
  1304. command.c_str());
  1305. }
  1306. // Write a rule for every output generated by this command.
  1307. for(std::set<std::string>::const_iterator output =
  1308. commandFiles.m_Outputs.begin();
  1309. output != commandFiles.m_Outputs.end(); ++output)
  1310. {
  1311. std::string src = this->ConvertToOutputPath(source.c_str());
  1312. std::string depends;
  1313. depends += src;
  1314. // Collect out all the dependencies for this rule.
  1315. for(std::set<std::string>::const_iterator d =
  1316. commandFiles.m_Depends.begin();
  1317. d != commandFiles.m_Depends.end(); ++d)
  1318. {
  1319. std::string dep = this->ConvertToOutputPath(d->c_str());
  1320. depends += " ";
  1321. depends += dep;
  1322. }
  1323. // output rule
  1324. this->OutputMakeRule(fout,
  1325. "Custom command",
  1326. output->c_str(),
  1327. depends.c_str(),
  1328. command.c_str());
  1329. }
  1330. }
  1331. }
  1332. if(name != "")
  1333. {
  1334. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1335. }
  1336. }
  1337. }
  1338. void cmUnixMakefileGenerator::GenerateCacheOnly()
  1339. {
  1340. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  1341. std::string dest = m_Makefile->GetStartOutputDirectory();
  1342. dest += "/Makefile";
  1343. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  1344. this->OutputMakefile(dest.c_str());
  1345. return;
  1346. }
  1347. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  1348. {
  1349. std::vector<cmMakefile*> makefiles;
  1350. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  1351. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  1352. i != makefiles.end(); ++i)
  1353. {
  1354. cmMakefile* mf = *i;
  1355. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  1356. {
  1357. mf->AddDefinition("RUN_CONFIGURE", true);
  1358. }
  1359. cmUnixMakefileGenerator* gen =
  1360. static_cast<cmUnixMakefileGenerator*>(this->CreateObject());
  1361. gen->SetCacheOnlyOn();
  1362. gen->SetRecurseOff();
  1363. mf->SetMakefileGenerator(gen);
  1364. mf->GenerateMakefile();
  1365. }
  1366. // CLEAN up the makefiles created
  1367. for(unsigned int i =0; i < makefiles.size(); ++i)
  1368. {
  1369. delete makefiles[i];
  1370. }
  1371. }
  1372. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1373. {
  1374. const char* variables =
  1375. "# the standard shell for make\n"
  1376. "SHELL = /bin/sh\n"
  1377. "\n"
  1378. "CMAKE_LIB_EXT = @CMAKE_LIB_EXT@\n"
  1379. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  1380. "CMAKE_AR = @CMAKE_AR@\n"
  1381. "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
  1382. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  1383. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@\n"
  1384. "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
  1385. "\n"
  1386. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  1387. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@\n"
  1388. "\n"
  1389. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  1390. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  1391. "CMAKE_MODULE_BUILD_FLAGS = @CMAKE_MODULE_BUILD_FLAGS@\n"
  1392. "CMAKE_MODULE_LINK_FLAGS = @CMAKE_MODULE_LINK_FLAGS@\n"
  1393. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  1394. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  1395. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  1396. "MODULE_SUFFIX = @CMAKE_MODULE_SUFFIX@\n"
  1397. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  1398. "RM = rm -f\n"
  1399. "\n"
  1400. "# set up the path to the rulesgen program\n"
  1401. "CMAKE_COMMAND = ${CMAKE_COMMAND}"
  1402. "\n"
  1403. "\n"
  1404. "\n";
  1405. std::string replaceVars = variables;
  1406. m_Makefile->ExpandVariablesInString(replaceVars);
  1407. fout << replaceVars.c_str();
  1408. fout << "CMAKE_CURRENT_SOURCE = " <<
  1409. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << "\n";
  1410. fout << "CMAKE_CURRENT_BINARY = " <<
  1411. this->ConvertToOutputPath(m_Makefile->GetStartOutputDirectory()) << "\n";
  1412. fout << "CMAKE_SOURCE_DIR = " <<
  1413. this->ConvertToOutputPath(m_Makefile->GetHomeDirectory()) << "\n";
  1414. fout << "CMAKE_BINARY_DIR = " <<
  1415. this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()) << "\n";
  1416. // Output Include paths
  1417. fout << "INCLUDE_FLAGS = ";
  1418. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1419. std::vector<std::string>::iterator i;
  1420. fout << "-I" <<
  1421. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  1422. for(i = includes.begin(); i != includes.end(); ++i)
  1423. {
  1424. std::string include = *i;
  1425. // Don't output a -I for the standard include path "/usr/include".
  1426. // This can cause problems with certain standard library
  1427. // implementations because the wrong headers may be found first.
  1428. if(include != "/usr/include")
  1429. {
  1430. fout << "-I" << this->ConvertToOutputPath(i->c_str()) << " ";
  1431. }
  1432. }
  1433. fout << m_Makefile->GetDefineFlags();
  1434. fout << "\n\n";
  1435. }
  1436. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1437. {
  1438. const char* root
  1439. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1440. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1441. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1442. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1443. const cmTargets &tgts = m_Makefile->GetTargets();
  1444. fout << "install: $(SUBDIR_INSTALL)\n";
  1445. fout << "\t@echo \"Installing ...\"\n";
  1446. const char* prefix
  1447. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1448. if (!prefix)
  1449. {
  1450. prefix = "/usr/local";
  1451. }
  1452. for(cmTargets::const_iterator l = tgts.begin();
  1453. l != tgts.end(); l++)
  1454. {
  1455. if (l->second.GetInstallPath() != "")
  1456. {
  1457. // first make the directories for each target
  1458. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1459. " ] ; then \\\n";
  1460. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1461. << l->second.GetInstallPath() << " \"; \\\n";
  1462. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1463. << "; \\\n";
  1464. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1465. << "; \\\n";
  1466. fout << "\t else true; \\\n";
  1467. fout << "\t fi\n";
  1468. // now install the target
  1469. switch (l->second.GetType())
  1470. {
  1471. case cmTarget::STATIC_LIBRARY:
  1472. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1473. << l->first;
  1474. fout << ".a";
  1475. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1476. break;
  1477. case cmTarget::SHARED_LIBRARY:
  1478. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1479. << l->first;
  1480. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1481. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1482. break;
  1483. case cmTarget::MODULE_LIBRARY:
  1484. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1485. << l->first;
  1486. fout << m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1487. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1488. break;
  1489. case cmTarget::WIN32_EXECUTABLE:
  1490. case cmTarget::EXECUTABLE:
  1491. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1492. << l->first
  1493. << cmSystemTools::GetExecutableExtension()
  1494. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1495. break;
  1496. case cmTarget::INSTALL_FILES:
  1497. {
  1498. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1499. std::vector<std::string>::const_iterator i;
  1500. for (i = sf.begin(); i != sf.end(); ++i)
  1501. {
  1502. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1503. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1504. // avoid using install-sh to install install-sh
  1505. // does not work on windows....
  1506. if(*i == "install-sh")
  1507. {
  1508. fout << "\t cp ";
  1509. }
  1510. else
  1511. {
  1512. fout << "\t $(INSTALL_DATA) ";
  1513. }
  1514. fout << *i
  1515. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1516. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1517. // avoid using install-sh to install install-sh
  1518. // does not work on windows....
  1519. if(*i == "install-sh")
  1520. {
  1521. fout << "\t cp ";
  1522. }
  1523. else
  1524. {
  1525. fout << "\t $(INSTALL_DATA) ";
  1526. }
  1527. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1528. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1529. fout << "\telse \\\n";
  1530. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1531. << " \"; \\\n";
  1532. fout << "\t fi\n";
  1533. }
  1534. }
  1535. break;
  1536. case cmTarget::INSTALL_PROGRAMS:
  1537. {
  1538. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1539. std::vector<std::string>::const_iterator i;
  1540. for (i = sf.begin(); i != sf.end(); ++i)
  1541. {
  1542. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1543. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1544. // avoid using install-sh to install install-sh
  1545. // does not work on windows....
  1546. if(*i == "install-sh")
  1547. {
  1548. fout << "\t cp ";
  1549. }
  1550. else
  1551. {
  1552. fout << "\t $(INSTALL_PROGRAM) ";
  1553. }
  1554. fout << *i
  1555. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1556. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1557. // avoid using install-sh to install install-sh
  1558. // does not work on windows....
  1559. if(*i == "install-sh")
  1560. {
  1561. fout << "\t cp ";
  1562. }
  1563. else
  1564. {
  1565. fout << "\t $(INSTALL_PROGRAM) ";
  1566. }
  1567. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1568. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1569. fout << "\telse \\\n";
  1570. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1571. << " \"; \\\n";
  1572. fout << "\t fi\n";
  1573. }
  1574. }
  1575. break;
  1576. case cmTarget::UTILITY:
  1577. default:
  1578. break;
  1579. }
  1580. }
  1581. }
  1582. }
  1583. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1584. {
  1585. this->OutputMakeRule(fout,
  1586. "Default build rule",
  1587. "all",
  1588. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1589. 0);
  1590. this->OutputMakeRule(fout,
  1591. "remove generated files",
  1592. "clean",
  1593. "$(SUBDIR_CLEAN)",
  1594. "-@ $(RM) $(CLEAN_OBJECT_FILES) $(EXECUTABLES)"
  1595. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  1596. // collect up all the sources
  1597. std::string allsources;
  1598. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1599. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1600. target != targets.end(); ++target)
  1601. {
  1602. // Iterate over every source for this target.
  1603. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1604. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1605. source != sources.end(); ++source)
  1606. {
  1607. if(!source->IsAHeaderFileOnly())
  1608. {
  1609. allsources += " \\\n";
  1610. allsources += source->GetFullPath();
  1611. }
  1612. }
  1613. }
  1614. this->OutputMakeRule(fout,
  1615. "Rule to build the cmake.depends and Makefile as side effect, if a source cmakelist file is out of date.",
  1616. "cmake.depends",
  1617. "$(CMAKE_MAKEFILE_SOURCES)",
  1618. "$(CMAKE_COMMAND) "
  1619. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1620. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1621. );
  1622. this->OutputMakeRule(fout,
  1623. "Rule to build the cmake.check_depends and Makefile as side effect, if any source file has changed.",
  1624. "cmake.check_depends",
  1625. allsources.c_str(),
  1626. "$(CMAKE_COMMAND) "
  1627. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1628. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1629. );
  1630. this->OutputMakeRule(fout,
  1631. "Rule to force the build of cmake.depends",
  1632. "depend",
  1633. "$(SUBDIR_DEPEND)",
  1634. "$(CMAKE_COMMAND) "
  1635. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1636. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1637. this->OutputMakeRule(fout,
  1638. "Rule to force the build of cmake.depends "
  1639. "in the current directory only.",
  1640. "dependlocal",
  1641. 0,
  1642. "$(CMAKE_COMMAND) "
  1643. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1644. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1645. this->OutputMakeRule(fout,
  1646. "Rebuild CMakeCache.txt file",
  1647. "rebuild_cache",
  1648. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1649. "$(CMAKE_COMMAND) "
  1650. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1651. this->OutputMakeRule(fout,
  1652. "Create CMakeCache.txt file",
  1653. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1654. 0,
  1655. "$(CMAKE_COMMAND) "
  1656. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1657. this->OutputMakeRule(fout,
  1658. "Rule to keep make from removing Makefiles "
  1659. "if control-C is hit during a run of cmake.",
  1660. ".PRECIOUS",
  1661. "Makefile cmake.depends",
  1662. 0);
  1663. this->OutputSourceObjectBuildRules(fout);
  1664. // find ctest
  1665. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1666. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1667. ctest += "/";
  1668. ctest += "ctest";
  1669. ctest += cmSystemTools::GetExecutableExtension();
  1670. if(!cmSystemTools::FileExists(ctest.c_str()))
  1671. {
  1672. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1673. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1674. ctest += "/Debug/";
  1675. ctest += "ctest";
  1676. ctest += cmSystemTools::GetExecutableExtension();
  1677. }
  1678. if(!cmSystemTools::FileExists(ctest.c_str()))
  1679. {
  1680. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1681. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1682. ctest += "/Release/";
  1683. ctest += "ctest";
  1684. ctest += cmSystemTools::GetExecutableExtension();
  1685. }
  1686. if (cmSystemTools::FileExists(ctest.c_str()))
  1687. {
  1688. this->OutputMakeRule(fout,
  1689. "run any tests",
  1690. "test",
  1691. "",
  1692. ctest.c_str());
  1693. }
  1694. }
  1695. void
  1696. cmUnixMakefileGenerator::
  1697. OutputBuildObjectFromSource(std::ostream& fout,
  1698. const char* shortName,
  1699. const cmSourceFile& source,
  1700. const char* extraCompileFlags,
  1701. bool shared)
  1702. {
  1703. // Header files shouldn't have build rules.
  1704. if(source.IsAHeaderFileOnly())
  1705. return;
  1706. std::string comment = "Build ";
  1707. std::string objectFile = std::string(shortName) + m_ObjectFileExtension;
  1708. objectFile = this->ConvertToOutputPath(objectFile.c_str());
  1709. comment += objectFile + " From ";
  1710. comment += source.GetFullPath();
  1711. std::string compileCommand;
  1712. std::string ext = source.GetSourceExtension();
  1713. if(ext == "c" )
  1714. {
  1715. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  1716. compileCommand += extraCompileFlags;
  1717. if(shared)
  1718. {
  1719. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1720. }
  1721. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1722. compileCommand +=
  1723. this->ConvertToOutputPath(source.GetFullPath().c_str());
  1724. compileCommand += " -o ";
  1725. compileCommand += objectFile;
  1726. }
  1727. else
  1728. {
  1729. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  1730. compileCommand += extraCompileFlags;
  1731. if(shared)
  1732. {
  1733. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1734. }
  1735. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1736. compileCommand +=
  1737. this->ConvertToOutputPath(source.GetFullPath().c_str());
  1738. compileCommand += " -o ";
  1739. compileCommand += objectFile;
  1740. }
  1741. this->OutputMakeRule(fout,
  1742. comment.c_str(),
  1743. objectFile.c_str(),
  1744. this->ConvertToOutputPath(source.GetFullPath().
  1745. c_str()).c_str(),
  1746. compileCommand.c_str());
  1747. }
  1748. void cmUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  1749. {
  1750. fout << "# Rules to build " << m_ObjectFileExtension
  1751. << " files from their sources:\n";
  1752. std::set<std::string> rules;
  1753. // Iterate over every target.
  1754. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1755. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1756. target != targets.end(); ++target)
  1757. {
  1758. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  1759. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  1760. std::string exportsDef = "";
  1761. if(shared)
  1762. {
  1763. exportsDef = "-D"+target->first+"_EXPORTS ";
  1764. }
  1765. // Iterate over every source for this target.
  1766. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1767. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1768. source != sources.end(); ++source)
  1769. {
  1770. if(!source->IsAHeaderFileOnly())
  1771. {
  1772. std::string shortName;
  1773. std::string sourceName;
  1774. // If the full path to the source file includes this
  1775. // directory, we want to use the relative path for the
  1776. // filename of the object file. Otherwise, we will use just
  1777. // the filename portion.
  1778. if((cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  1779. || (cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->
  1780. GetCurrentOutputDirectory()) == 0))
  1781. {
  1782. sourceName = source->GetSourceName()+"."+source->GetSourceExtension();
  1783. shortName = source->GetSourceName();
  1784. // The path may be relative. See if a directory needs to be
  1785. // created for the output file. This is a ugly, and perhaps
  1786. // should be moved elsewhere.
  1787. std::string relPath =
  1788. cmSystemTools::GetFilenamePath(source->GetSourceName());
  1789. if(relPath != "")
  1790. {
  1791. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  1792. outPath += "/"+relPath;
  1793. cmSystemTools::MakeDirectory(outPath.c_str());
  1794. }
  1795. }
  1796. else
  1797. {
  1798. sourceName = source->GetFullPath();
  1799. shortName = cmSystemTools::GetFilenameName(source->GetSourceName());
  1800. }
  1801. std::string shortNameWithExt = shortName +
  1802. source->GetSourceExtension();
  1803. // Only output a rule for each .o once.
  1804. if(rules.find(shortNameWithExt) == rules.end())
  1805. {
  1806. this->OutputBuildObjectFromSource(fout,
  1807. shortName.c_str(),
  1808. *source,
  1809. exportsDef.c_str(),
  1810. shared);
  1811. rules.insert(shortNameWithExt);
  1812. }
  1813. }
  1814. }
  1815. }
  1816. }
  1817. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  1818. const char* comment,
  1819. const char* target,
  1820. const char* depends,
  1821. const char* command,
  1822. const char* command2,
  1823. const char* command3,
  1824. const char* command4)
  1825. {
  1826. if(!target)
  1827. {
  1828. cmSystemTools::Error("no target for OutputMakeRule");
  1829. return;
  1830. }
  1831. std::string replace;
  1832. if(comment)
  1833. {
  1834. replace = comment;
  1835. m_Makefile->ExpandVariablesInString(replace);
  1836. fout << "#---------------------------------------------------------\n";
  1837. fout << "# " << comment;
  1838. fout << "\n#\n";
  1839. }
  1840. fout << "\n";
  1841. replace = target;
  1842. m_Makefile->ExpandVariablesInString(replace);
  1843. fout << this->ConvertToOutputPath(replace.c_str()) << ": ";
  1844. if(depends)
  1845. {
  1846. replace = depends;
  1847. m_Makefile->ExpandVariablesInString(replace);
  1848. fout << replace.c_str();
  1849. }
  1850. fout << "\n";
  1851. if(command)
  1852. {
  1853. replace = command;
  1854. m_Makefile->ExpandVariablesInString(replace);
  1855. if(replace[0] != '-' && replace.find("echo") != 0
  1856. && replace.find("$(MAKE)") != 0)
  1857. {
  1858. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1859. }
  1860. fout << "\t" << replace.c_str() << "\n";
  1861. }
  1862. if(command2)
  1863. {
  1864. replace = command2;
  1865. m_Makefile->ExpandVariablesInString(replace);
  1866. if(replace[0] != '-' && replace.find("echo") != 0
  1867. && replace.find("$(MAKE)") != 0)
  1868. {
  1869. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1870. }
  1871. fout << "\t" << replace.c_str() << "\n";
  1872. }
  1873. if(command3)
  1874. {
  1875. replace = command3;
  1876. m_Makefile->ExpandVariablesInString(replace);
  1877. if(replace[0] != '-' && replace.find("echo") != 0
  1878. && replace.find("$(MAKE)") != 0)
  1879. {
  1880. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1881. }
  1882. fout << "\t" << replace.c_str() << "\n";
  1883. }
  1884. if(command4)
  1885. {
  1886. replace = command4;
  1887. m_Makefile->ExpandVariablesInString(replace);
  1888. if(replace[0] != '-' && replace.find("echo") != 0
  1889. && replace.find("$(MAKE)") != 0)
  1890. {
  1891. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1892. }
  1893. fout << "\t" << replace.c_str() << "\n";
  1894. }
  1895. fout << "\n";
  1896. }
  1897. void cmUnixMakefileGenerator::SetLocal (bool local)
  1898. {
  1899. if (local)
  1900. {
  1901. m_CacheOnly = false;
  1902. m_Recurse = false;
  1903. }
  1904. else
  1905. {
  1906. m_CacheOnly = true;
  1907. m_Recurse = true;
  1908. }
  1909. }
  1910. void cmUnixMakefileGenerator::ComputeSystemInfo()
  1911. {
  1912. if (m_CacheOnly)
  1913. {
  1914. // see man putenv for explaination of this stupid code....
  1915. static char envCXX[5000];
  1916. static char envCC[5000];
  1917. if(m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"))
  1918. {
  1919. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  1920. m_Makefile->ExpandVariablesInString(env);
  1921. strncpy(envCXX, env.c_str(), 4999);
  1922. envCXX[4999] = 0;
  1923. putenv(envCXX);
  1924. }
  1925. if(m_Makefile->GetDefinition("CMAKE_C_COMPILER"))
  1926. {
  1927. std::string env = "CC=${CMAKE_C_COMPILER}";
  1928. m_Makefile->ExpandVariablesInString(env);
  1929. strncpy(envCC, env.c_str(), 4999);
  1930. envCC[4999] = 0;
  1931. putenv(envCC);
  1932. }
  1933. // currently we run configure shell script here to determine the info
  1934. std::string output;
  1935. std::string cmd = "cd ";
  1936. cmd += this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory());
  1937. cmd += "; ";
  1938. const char* root
  1939. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1940. cmd += root;
  1941. cmd += "/Templates/configure";
  1942. cmSystemTools::RunCommand(cmd.c_str(), output);
  1943. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  1944. }
  1945. // now load the settings
  1946. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  1947. fpath += "/CMakeSystemConfig.cmake";
  1948. m_Makefile->ReadListFile(NULL,fpath.c_str());
  1949. }