cmUnixMakefileGenerator.cxx 72 KB

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