cmUnixMakefileGenerator.cxx 74 KB

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