cmLocalUnixMakefileGenerator.cxx 71 KB

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