cmUnixMakefileGenerator.cxx 74 KB

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