cmLocalUnixMakefileGenerator.cxx 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalUnixMakefileGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include <stdio.h>
  22. cmLocalUnixMakefileGenerator::cmLocalUnixMakefileGenerator()
  23. {
  24. m_WindowsShell = false;
  25. m_IncludeDirective = "include";
  26. m_MakefileVariableSize = 0;
  27. }
  28. cmLocalUnixMakefileGenerator::~cmLocalUnixMakefileGenerator()
  29. {
  30. }
  31. void cmLocalUnixMakefileGenerator::Generate(bool fromTheTop)
  32. {
  33. // suppoirt override in output directories
  34. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  35. {
  36. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  37. if(m_LibraryOutputPath.size())
  38. {
  39. if(m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
  40. {
  41. m_LibraryOutputPath += "/";
  42. }
  43. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  44. {
  45. cmSystemTools::Error("Error failed create "
  46. "LIBRARY_OUTPUT_PATH directory:",
  47. m_LibraryOutputPath.c_str());
  48. }
  49. m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  50. }
  51. }
  52. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  53. {
  54. m_ExecutableOutputPath =
  55. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  56. if(m_ExecutableOutputPath.size())
  57. {
  58. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
  59. {
  60. m_ExecutableOutputPath += "/";
  61. }
  62. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  63. {
  64. cmSystemTools::Error("Error failed to create "
  65. "EXECUTABLE_OUTPUT_PATH directory:",
  66. m_ExecutableOutputPath.c_str());
  67. }
  68. m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
  69. }
  70. }
  71. if (!fromTheTop)
  72. {
  73. // Generate depends
  74. cmMakeDepend md;
  75. md.SetMakefile(m_Makefile);
  76. md.GenerateMakefileDependencies();
  77. this->ProcessDepends(md);
  78. }
  79. // output the makefile fragment
  80. std::string dest = m_Makefile->GetStartOutputDirectory();
  81. dest += "/Makefile";
  82. this->OutputMakefile(dest.c_str(), !fromTheTop);
  83. }
  84. void
  85. cmLocalUnixMakefileGenerator::AddDependenciesToSourceFile(cmDependInformation const *info,
  86. cmSourceFile *i,
  87. std::set<cmDependInformation const*> *visited)
  88. {
  89. // add info to the visited set
  90. visited->insert(info);
  91. // add this dependency and the recurse
  92. // now recurse with info's dependencies
  93. for(cmDependInformation::DependencySet::const_iterator d =
  94. info->m_DependencySet.begin();
  95. d != info->m_DependencySet.end(); ++d)
  96. {
  97. if (visited->find(*d) == visited->end())
  98. {
  99. if((*d)->m_FullPath != "")
  100. {
  101. i->GetDepends().push_back((*d)->m_FullPath);
  102. }
  103. this->AddDependenciesToSourceFile(*d,i,visited);
  104. }
  105. }
  106. }
  107. void cmLocalUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  108. {
  109. // Now create cmDependInformation objects for files in the directory
  110. cmTargets &tgts = m_Makefile->GetTargets();
  111. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  112. {
  113. std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  114. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  115. i != classes.end(); ++i)
  116. {
  117. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  118. {
  119. // get the depends
  120. const cmDependInformation *info =
  121. md.GetDependInformationForSourceFile(*(*i));
  122. // Delete any hints from the source file's dependencies.
  123. (*i)->GetDepends().erase((*i)->GetDepends().begin(), (*i)->GetDepends().end());
  124. // Now add the real dependencies for the file.
  125. if (info)
  126. {
  127. // create visited set
  128. std::set<cmDependInformation const*> visited;
  129. this->AddDependenciesToSourceFile(info,*i, &visited);
  130. }
  131. }
  132. }
  133. }
  134. }
  135. // This is where CMakeTargets.make is generated
  136. void cmLocalUnixMakefileGenerator::OutputMakefile(const char* file,
  137. bool withDepends)
  138. {
  139. // Create sub directories fro aux source directories
  140. std::vector<std::string>& auxSourceDirs =
  141. m_Makefile->GetAuxSourceDirectories();
  142. if( auxSourceDirs.size() )
  143. {
  144. // For the case when this is running as a remote build
  145. // on unix, make the directory
  146. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  147. i != auxSourceDirs.end(); ++i)
  148. {
  149. if(i->c_str()[0] != '/')
  150. {
  151. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  152. if(dir.size() && dir[dir.size()-1] != '/')
  153. {
  154. dir += "/";
  155. }
  156. dir += *i;
  157. cmSystemTools::MakeDirectory(dir.c_str());
  158. }
  159. else
  160. {
  161. cmSystemTools::MakeDirectory(i->c_str());
  162. }
  163. }
  164. }
  165. // Create a stream that writes to a temporary file
  166. // then does a copy at the end. This is to allow users
  167. // to hit control-c during the make of the makefile
  168. cmGeneratedFileStream tempFile(file);
  169. tempFile.SetAlwaysCopy(true);
  170. std::ostream& fout = tempFile.GetStream();
  171. if(!fout)
  172. {
  173. cmSystemTools::Error("Error can not open for write: ", file);
  174. return;
  175. }
  176. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  177. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  178. << " Generator, CMake Version "
  179. << cmMakefile::GetMajorVersion() << "."
  180. << cmMakefile::GetMinorVersion() << "\n"
  181. << "# Generated from the following files:\n# "
  182. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  183. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  184. // sort the array
  185. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  186. // remove duplicates
  187. std::vector<std::string>::iterator new_end =
  188. std::unique(lfiles.begin(), lfiles.end());
  189. lfiles.erase(new_end, lfiles.end());
  190. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  191. i != lfiles.end(); ++i)
  192. {
  193. fout << "# " << i->c_str() << "\n";
  194. }
  195. fout << "\n\n";
  196. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  197. {
  198. fout << "# Suppresses display of executed commands\n";
  199. fout << ".SILENT:\n";
  200. }
  201. fout << "# disable some common implicit rules to speed things up\n";
  202. fout << ".SUFFIXES:\n";
  203. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  204. // create a make variable with all of the sources for this Makefile
  205. // for depend purposes.
  206. fout << "CMAKE_MAKEFILE_SOURCES = ";
  207. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  208. i != lfiles.end(); ++i)
  209. {
  210. fout << " " << cmSystemTools::ConvertToOutputPath(i->c_str());
  211. }
  212. // Add the cache to the list
  213. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  214. cacheFile += "/CMakeCache.txt";
  215. fout << " " << cmSystemTools::ConvertToOutputPath(cacheFile.c_str());
  216. fout << "\n\n\n";
  217. this->OutputMakeVariables(fout);
  218. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  219. checkCache += "/cmake.check_cache";
  220. checkCache = cmSystemTools::ConvertToOutputPath(checkCache.c_str());
  221. // Set up the default target as the VERY first target, so that make with no arguments will run it
  222. this->
  223. OutputMakeRule(fout,
  224. "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",
  225. "default_target",
  226. checkCache.c_str(),
  227. "$(MAKE) $(MAKESILENT) cmake.depends",
  228. "$(MAKE) $(MAKESILENT) cmake.check_depends",
  229. "$(MAKE) $(MAKESILENT) -f cmake.check_depends",
  230. "$(MAKE) $(MAKESILENT) all");
  231. this->OutputTargetRules(fout);
  232. this->OutputDependLibs(fout);
  233. this->OutputTargets(fout);
  234. this->OutputSubDirectoryRules(fout);
  235. std::string dependName = m_Makefile->GetStartOutputDirectory();
  236. dependName += "/cmake.depends";
  237. if(withDepends)
  238. {
  239. std::ofstream dependout(dependName.c_str());
  240. if(!dependout)
  241. {
  242. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  243. return;
  244. }
  245. dependout << "# .o dependencies in this directory." << std::endl;
  246. std::string checkDepend = m_Makefile->GetStartOutputDirectory();
  247. checkDepend += "/cmake.check_depends";
  248. std::ofstream checkdependout(checkDepend.c_str());
  249. if(!checkdependout)
  250. {
  251. cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
  252. return;
  253. }
  254. 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;
  255. // if there were any depends output, then output the check depends
  256. // information inot checkdependout
  257. if(this->OutputObjectDepends(dependout))
  258. {
  259. this->OutputCheckDepends(checkdependout);
  260. }
  261. else
  262. {
  263. checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
  264. }
  265. }
  266. this->OutputCustomRules(fout);
  267. this->OutputMakeRules(fout);
  268. this->OutputInstallRules(fout);
  269. // only add the depend include if the depend file exists
  270. if(cmSystemTools::FileExists(dependName.c_str()))
  271. {
  272. fout << m_IncludeDirective << " cmake.depends\n";
  273. }
  274. }
  275. #if defined(_WIN32) && ! defined(__CYGWIN__)
  276. std::string cmLocalUnixMakefileGenerator::GetOutputExtension(const char* s)
  277. {
  278. std::string sourceExtension = s;
  279. if(sourceExtension == "def")
  280. {
  281. return "";
  282. }
  283. if(sourceExtension == "ico" || sourceExtension == "rc2")
  284. {
  285. return "";
  286. }
  287. if(sourceExtension == "rc")
  288. {
  289. return ".res";
  290. }
  291. return ".obj";
  292. }
  293. #else
  294. std::string cmLocalUnixMakefileGenerator::GetOutputExtension(const char*)
  295. {
  296. return ".o";
  297. }
  298. #endif
  299. // Output the rules for any targets
  300. void cmLocalUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  301. {
  302. // for each target add to the list of targets
  303. fout << "TARGETS = ";
  304. const cmTargets &tgts = m_Makefile->GetTargets();
  305. // list libraries first
  306. for(cmTargets::const_iterator l = tgts.begin();
  307. l != tgts.end(); l++)
  308. {
  309. if (l->second.IsInAll())
  310. {
  311. const char* targetPrefix = l->second.GetProperty("PREFIX");
  312. const char* targetSuffix = l->second.GetProperty("SUFFIX");
  313. std::string path = m_LibraryOutputPath;
  314. const char* prefixVar = 0;
  315. const char* suffixVar = 0;
  316. switch(l->second.GetType())
  317. {
  318. case cmTarget::STATIC_LIBRARY:
  319. prefixVar = "CMAKE_STATIC_LIBRARY_PREFIX";
  320. suffixVar = "CMAKE_STATIC_LIBRARY_SUFFIX";
  321. break;
  322. case cmTarget::SHARED_LIBRARY:
  323. prefixVar = "CMAKE_SHARED_LIBRARY_PREFIX";
  324. suffixVar = "CMAKE_SHARED_LIBRARY_SUFFIX";
  325. break;
  326. case cmTarget::MODULE_LIBRARY:
  327. prefixVar = "CMAKE_SHARED_MODULE_PREFIX";
  328. suffixVar = "CMAKE_SHARED_MODULE_SUFFIX";
  329. break;
  330. case cmTarget::EXECUTABLE:
  331. case cmTarget::WIN32_EXECUTABLE:
  332. case cmTarget::UTILITY:
  333. case cmTarget::INSTALL_FILES:
  334. case cmTarget::INSTALL_PROGRAMS:
  335. break;
  336. }
  337. // if it is a library this will be set
  338. if(prefixVar)
  339. {
  340. // if there is no prefix on the target use the cmake definition
  341. if(!targetPrefix)
  342. {
  343. targetPrefix = this->GetSafeDefinition(prefixVar);
  344. }
  345. // if there is no suffix on the target use the cmake definition
  346. if(!targetSuffix)
  347. {
  348. targetSuffix = this->GetSafeDefinition(suffixVar);
  349. }
  350. path +=
  351. targetPrefix + l->first + targetSuffix;
  352. fout << " \\\n"
  353. << cmSystemTools::ConvertToOutputPath(path.c_str());
  354. }
  355. }
  356. }
  357. // executables
  358. for(cmTargets::const_iterator l = tgts.begin();
  359. l != tgts.end(); l++)
  360. {
  361. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  362. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  363. l->second.IsInAll())
  364. {
  365. std::string path = m_ExecutableOutputPath + l->first +
  366. cmSystemTools::GetExecutableExtension();
  367. fout << " \\\n" << cmSystemTools::ConvertToOutputPath(path.c_str());
  368. }
  369. }
  370. // list utilities last
  371. for(cmTargets::const_iterator l = tgts.begin();
  372. l != tgts.end(); l++)
  373. {
  374. if (l->second.GetType() == cmTarget::UTILITY &&
  375. l->second.IsInAll())
  376. {
  377. fout << " \\\n" << l->first.c_str();
  378. }
  379. }
  380. fout << "\n\n";
  381. // get the classes from the source lists then add them to the groups
  382. for(cmTargets::const_iterator l = tgts.begin();
  383. l != tgts.end(); l++)
  384. {
  385. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  386. if (classes.begin() != classes.end())
  387. {
  388. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS") << " = ";
  389. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  390. i != classes.end(); i++)
  391. {
  392. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  393. {
  394. std::string outExt(
  395. this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
  396. if(outExt.size())
  397. {
  398. fout << "\\\n"
  399. << cmSystemTools::ConvertToOutputPath((*i)->GetSourceName().c_str())
  400. << outExt.c_str() << " ";
  401. }
  402. }
  403. }
  404. fout << "\n\n";
  405. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS_QUOTED") << " = ";
  406. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  407. i != classes.end(); i++)
  408. {
  409. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  410. {
  411. std::string outExt(this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
  412. if(outExt.size())
  413. {
  414. fout << "\\\n\"" << cmSystemTools::ConvertToOutputPath((*i)->GetSourceName().c_str())
  415. << outExt.c_str() << "\" ";
  416. }
  417. }
  418. }
  419. fout << "\n\n";
  420. }
  421. }
  422. fout << "CLEAN_OBJECT_FILES = ";
  423. for(cmTargets::const_iterator l = tgts.begin();
  424. l != tgts.end(); l++)
  425. {
  426. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  427. if (classes.begin() != classes.end())
  428. {
  429. fout << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
  430. << ") ";
  431. }
  432. }
  433. fout << "\n\n";
  434. const char * qt_files = m_Makefile->GetDefinition("GENERATED_QT_FILES");
  435. if (qt_files != NULL &&
  436. strlen(m_Makefile->GetDefinition("GENERATED_QT_FILES"))>0)
  437. {
  438. fout << "GENERATED_QT_FILES = ";
  439. fout << qt_files;
  440. fout << "\n\n";
  441. }
  442. }
  443. /**
  444. * Output the linking rules on a command line. For executables,
  445. * targetLibrary should be a NULL pointer. For libraries, it should point
  446. * to the name of the library. This will not link a library against itself.
  447. */
  448. void cmLocalUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  449. const char* targetLibrary,
  450. const cmTarget &tgt)
  451. {
  452. // Try to emit each search path once
  453. std::set<std::string> emitted;
  454. // Embed runtime search paths if possible and if required.
  455. bool outputRuntime = true;
  456. std::string runtimeFlag;
  457. std::string runtimeSep;
  458. std::vector<std::string> runtimeDirs;
  459. bool cxx = tgt.HasCxx();
  460. if(!cxx )
  461. {
  462. // if linking a c executable use the C runtime flag as cc
  463. // may not be the same program that creates shared libaries
  464. // and may have different flags
  465. runtimeFlag = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_FLAG");
  466. runtimeSep = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_FLAG_SEP");
  467. }
  468. else
  469. {
  470. runtimeFlag = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG");
  471. runtimeSep = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP");
  472. }
  473. // concatenate all paths or no?
  474. bool runtimeConcatenate = ( runtimeSep!="" );
  475. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  476. {
  477. outputRuntime = false;
  478. }
  479. // Some search paths should never be emitted
  480. emitted.insert("");
  481. emitted.insert("/usr/lib");
  482. std::string libPathFlag = m_Makefile->GetDefinition("CMAKE_LIBRARY_PATH_FLAG");
  483. std::string libLinkFlag = this->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  484. // collect all the flags needed for linking libraries
  485. std::string linkLibs;
  486. // Flags to link an executable to shared libraries.
  487. if( tgt.GetType() == cmTarget::EXECUTABLE)
  488. {
  489. if(cxx)
  490. {
  491. linkLibs = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS");
  492. }
  493. else
  494. {
  495. linkLibs = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_FLAGS");
  496. }
  497. linkLibs += " ";
  498. }
  499. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  500. for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
  501. libDir != libdirs.end(); ++libDir)
  502. {
  503. std::string libpath = cmSystemTools::ConvertToOutputPath(libDir->c_str());
  504. if(emitted.insert(libpath).second)
  505. {
  506. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  507. if((pos == std::string::npos || pos > 0)
  508. && libDir->find("${") == std::string::npos)
  509. {
  510. linkLibs += libPathFlag;
  511. if(outputRuntime)
  512. {
  513. runtimeDirs.push_back( libpath );
  514. }
  515. }
  516. linkLibs += libpath;
  517. linkLibs += " ";
  518. }
  519. }
  520. std::string linkSuffix = this->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  521. std::string regexp = ".*\\";
  522. regexp += linkSuffix;
  523. regexp += "$";
  524. cmRegularExpression hasSuffix(regexp.c_str());
  525. std::string librariesLinked;
  526. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  527. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  528. lib != libs.end(); ++lib)
  529. {
  530. // Don't link the library against itself!
  531. if(targetLibrary && (lib->first == targetLibrary)) continue;
  532. // don't look at debug libraries
  533. if (lib->second == cmTarget::DEBUG) continue;
  534. // skip zero size library entries, this may happen
  535. // if a variable expands to nothing.
  536. if (lib->first.size() == 0) continue;
  537. // if it is a full path break it into -L and -l
  538. cmRegularExpression reg("([ \t]*\\-l)|([ \t]*\\-framework)|(\\${)");
  539. if(lib->first.find('/') != std::string::npos
  540. && !reg.find(lib->first))
  541. {
  542. std::string dir, file;
  543. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  544. dir, file);
  545. std::string libpath = this->ConvertToOutputForExisting(dir.c_str());
  546. if(emitted.insert(libpath).second)
  547. {
  548. linkLibs += libPathFlag;
  549. linkLibs += libpath;
  550. linkLibs += " ";
  551. if(outputRuntime)
  552. {
  553. runtimeDirs.push_back( libpath );
  554. }
  555. }
  556. cmRegularExpression libname("lib([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  557. cmRegularExpression libname_noprefix("([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  558. if(libname.find(file))
  559. {
  560. // Library had "lib" prefix.
  561. librariesLinked += libLinkFlag;
  562. file = libname.match(1);
  563. librariesLinked += file;
  564. if(linkSuffix.size() && !hasSuffix.find(file))
  565. {
  566. librariesLinked += linkSuffix;
  567. }
  568. librariesLinked += " ";
  569. }
  570. else if(libname_noprefix.find(file))
  571. {
  572. // Library had no "lib" prefix.
  573. librariesLinked += libLinkFlag;
  574. file = libname_noprefix.match(1);
  575. librariesLinked += file;
  576. if(linkSuffix.size() && !hasSuffix.find(file))
  577. {
  578. librariesLinked += linkSuffix;
  579. }
  580. librariesLinked += " ";
  581. }
  582. else
  583. {
  584. // Error parsing the library name. Just use the full path.
  585. // The linker will give an error if it is invalid.
  586. librariesLinked += lib->first;
  587. librariesLinked += " ";
  588. }
  589. }
  590. // not a full path, so add -l name
  591. else
  592. {
  593. if(!reg.find(lib->first))
  594. {
  595. librariesLinked += libLinkFlag;
  596. }
  597. librariesLinked += lib->first;
  598. if(linkSuffix.size() && !hasSuffix.find(lib->first))
  599. {
  600. librariesLinked += linkSuffix;
  601. }
  602. librariesLinked += " ";
  603. }
  604. }
  605. linkLibs += librariesLinked;
  606. fout << linkLibs;
  607. if(outputRuntime && runtimeDirs.size()>0)
  608. {
  609. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  610. // a "-R a -R b -R c" type link line
  611. fout << runtimeFlag;
  612. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  613. fout << *itr;
  614. ++itr;
  615. for( ; itr != runtimeDirs.end(); ++itr )
  616. {
  617. if(runtimeConcatenate)
  618. {
  619. fout << runtimeSep << *itr;
  620. }
  621. else
  622. {
  623. fout << " " << runtimeFlag << *itr;
  624. }
  625. }
  626. fout << " ";
  627. }
  628. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  629. {
  630. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  631. }
  632. }
  633. std::string cmLocalUnixMakefileGenerator::CreateTargetRules(const cmTarget &target,
  634. const char* targetName)
  635. {
  636. std::string customRuleCode = "";
  637. bool initNext = false;
  638. for (std::vector<cmCustomCommand>::const_iterator cr =
  639. target.GetCustomCommands().begin();
  640. cr != target.GetCustomCommands().end(); ++cr)
  641. {
  642. cmCustomCommand cc(*cr);
  643. cc.ExpandVariables(*m_Makefile);
  644. if (cc.GetSourceName() == targetName)
  645. {
  646. if(initNext)
  647. {
  648. customRuleCode += "\n\t";
  649. }
  650. else
  651. {
  652. initNext = true;
  653. }
  654. std::string command = cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str());
  655. customRuleCode += command + " " + cc.GetArguments();
  656. }
  657. }
  658. return customRuleCode;
  659. }
  660. struct RuleVariables
  661. {
  662. const char* replace;
  663. const char* lookup;
  664. };
  665. static RuleVariables ruleReplaceVars[] =
  666. {
  667. {"<CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>", "CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS"},
  668. {"<CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS>", "CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS"},
  669. {"<CMAKE_SHARED_MODULE_C_FLAGS>", "CMAKE_SHARED_MODULE_C_FLAGS"},
  670. {"<CMAKE_SHARED_MODULE_CXX_FLAGS>", "CMAKE_SHARED_MODULE_CXX_FLAGS"},
  671. {"<CMAKE_SHARED_LIBRARY_C_FLAGS>", "CMAKE_SHARED_LIBRARY_C_FLAGS"},
  672. {"<CMAKE_SHARED_LIBRARY_CXX_FLAGS>", "CMAKE_SHARED_LIBRARY_CXX_FLAGS"},
  673. {"<CMAKE_CXX_LINK_FLAGS>", "CMAKE_CXX_LINK_FLAGS"},
  674. {"<CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS>", "CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS"},
  675. {"<CMAKE_SHARED_MODULE_CREATE_C_FLAGS>", "CMAKE_SHARED_MODULE_CREATE_C_FLAGS"},
  676. {"<CMAKE_C_LINK_FLAGS>", "CMAKE_C_LINK_FLAGS"},
  677. {"<CMAKE_AR>", "CMAKE_AR"},
  678. {"<CMAKE_RANLIB>", "CMAKE_RANLIB"},
  679. {0, 0}
  680. };
  681. void
  682. cmLocalUnixMakefileGenerator::ExpandRuleVariables(std::string& s,
  683. const char* objects,
  684. const char* target,
  685. const char* linkLibs,
  686. const char* source,
  687. const char* object,
  688. const char* flags,
  689. const char* objectsquoted,
  690. const char* targetBase,
  691. const char* linkFlags)
  692. {
  693. std::string cxxcompiler = this->ConvertToOutputForExisting(
  694. this->GetSafeDefinition("CMAKE_CXX_COMPILER"));
  695. std::string ccompiler = this->ConvertToOutputForExisting(
  696. this->GetSafeDefinition("CMAKE_C_COMPILER"));
  697. cmSystemTools::ReplaceString(s, "<CMAKE_CXX_COMPILER>", cxxcompiler.c_str());
  698. cmSystemTools::ReplaceString(s, "<CMAKE_C_COMPILER>", ccompiler.c_str());
  699. if(linkFlags)
  700. {
  701. cmSystemTools::ReplaceString(s, "<LINK_FLAGS>", linkFlags);
  702. }
  703. if(flags)
  704. {
  705. cmSystemTools::ReplaceString(s, "<FLAGS>", flags);
  706. }
  707. if(source)
  708. {
  709. cmSystemTools::ReplaceString(s, "<SOURCE>", source);
  710. }
  711. if(object)
  712. {
  713. cmSystemTools::ReplaceString(s, "<OBJECT>", object);
  714. }
  715. if(objects)
  716. {
  717. cmSystemTools::ReplaceString(s, "<OBJECTS>", objects);
  718. }
  719. if(objectsquoted)
  720. {
  721. cmSystemTools::ReplaceString(s, "<OBJECTS_QUOTED>", objectsquoted);
  722. }
  723. if(target)
  724. {
  725. cmSystemTools::ReplaceString(s, "<TARGET>", target);
  726. }
  727. if(targetBase)
  728. {
  729. // special case for quoted paths with spaces
  730. // if you see <TARGET_BASE>.lib then put the .lib inside
  731. // the quotes, same for .dll
  732. if((strlen(targetBase) > 1) && targetBase[0] == '\"')
  733. {
  734. std::string base = targetBase;
  735. base[base.size()-1] = '.';
  736. std::string baseLib = base + "lib\"";
  737. std::string baseDll = base + "dll\"";
  738. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.lib", baseLib.c_str());
  739. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.dll", baseDll.c_str());
  740. }
  741. cmSystemTools::ReplaceString(s, "<TARGET_BASE>", targetBase);
  742. }
  743. if(linkLibs)
  744. {
  745. cmSystemTools::ReplaceString(s, "<LINK_LIBRARIES>", linkLibs);
  746. }
  747. RuleVariables* rv = ruleReplaceVars;
  748. while(rv->replace)
  749. {
  750. cmSystemTools::ReplaceString(s, rv->replace,
  751. this->GetSafeDefinition(rv->lookup));
  752. rv++;
  753. }
  754. }
  755. void cmLocalUnixMakefileGenerator::OutputLibraryRule(std::ostream& fout,
  756. const char* name,
  757. const cmTarget &t,
  758. const char* prefix,
  759. const char* suffix,
  760. const char* createVariable,
  761. const char* comment,
  762. const char* linkFlags
  763. )
  764. {
  765. // create the library name
  766. std::string targetNameBase = prefix;
  767. targetNameBase += name;
  768. std::string targetName = prefix;
  769. targetName += name;
  770. targetName += suffix;
  771. // create the target full path name
  772. std::string targetFullPath = m_LibraryOutputPath + targetName;
  773. std::string targetBaseFullPath = m_LibraryOutputPath + targetNameBase;
  774. targetBaseFullPath =
  775. cmSystemTools::ConvertToOutputPath(targetBaseFullPath.c_str());
  776. targetFullPath = cmSystemTools::ConvertToOutputPath(targetFullPath.c_str());
  777. // get the objects that are used to link this library
  778. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  779. std::string objsQuoted = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ") ";
  780. // create a variable with the objects that this library depends on
  781. std::string depend = objs + " $("
  782. + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  783. // collect up the build rules
  784. std::vector<std::string> rules;
  785. std::string command = "$(RM) " + targetFullPath;
  786. rules.push_back(command);
  787. rules.push_back(m_Makefile->GetDefinition(createVariable));
  788. // expand multi-command semi-colon separated lists
  789. // of commands into separate commands
  790. std::vector<std::string> commands;
  791. cmSystemTools::ExpandList(rules, commands);
  792. // collect custom commands for this target and add them to the list
  793. std::string customCommands = this->CreateTargetRules(t, name);
  794. if(customCommands.size() > 0)
  795. {
  796. commands.push_back(customCommands);
  797. }
  798. // collect up the link libraries
  799. cmOStringStream linklibs;
  800. this->OutputLinkLibraries(linklibs, name, t);
  801. for(std::vector<std::string>::iterator i = commands.begin();
  802. i != commands.end(); ++i)
  803. {
  804. this->ExpandRuleVariables(*i,
  805. objs.c_str(),
  806. targetFullPath.c_str(),
  807. linklibs.str().c_str(),
  808. 0, 0, 0, objsQuoted.c_str(),
  809. targetBaseFullPath.c_str(),
  810. linkFlags);
  811. }
  812. this->OutputMakeRule(fout, comment,
  813. targetFullPath.c_str(),
  814. depend.c_str(),
  815. commands);
  816. }
  817. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  818. const char* name,
  819. const cmTarget &t)
  820. {
  821. const char* createRule;
  822. if(t.HasCxx())
  823. {
  824. createRule = "CMAKE_CXX_CREATE_SHARED_LIBRARY";
  825. }
  826. else
  827. {
  828. createRule = "CMAKE_C_CREATE_SHARED_LIBRARY";
  829. }
  830. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  831. buildType = cmSystemTools::UpperCase(buildType);
  832. std::string linkFlags = this->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  833. linkFlags += " ";
  834. if(buildType.size())
  835. {
  836. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  837. build += buildType;
  838. linkFlags += this->GetSafeDefinition(build.c_str());
  839. linkFlags += " ";
  840. }
  841. #ifdef _WIN32
  842. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  843. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  844. i != sources.end(); ++i)
  845. {
  846. if((*i)->GetSourceExtension() == "def")
  847. {
  848. linkFlags += this->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  849. linkFlags += (*i)->GetFullPath();
  850. linkFlags += " ";
  851. }
  852. }
  853. #endif
  854. const char* targetPrefix = t.GetProperty("PREFIX");
  855. if(!targetPrefix)
  856. {
  857. targetPrefix = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  858. }
  859. const char* targetSuffix = t.GetProperty("SUFFIX");
  860. if(!targetSuffix)
  861. {
  862. targetSuffix = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  863. }
  864. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  865. if(targetLinkFlags)
  866. {
  867. linkFlags += targetLinkFlags;
  868. linkFlags += " ";
  869. }
  870. this->OutputLibraryRule(fout, name, t,
  871. targetPrefix,
  872. targetSuffix,
  873. createRule,
  874. "shared library",
  875. linkFlags.c_str());
  876. }
  877. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  878. const char* name,
  879. const cmTarget &t)
  880. {
  881. const char* createRule;
  882. if(t.HasCxx())
  883. {
  884. createRule = "CMAKE_CXX_CREATE_SHARED_MODULE";
  885. }
  886. else
  887. {
  888. createRule = "CMAKE_C_CREATE_SHARED_MODULE";
  889. }
  890. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  891. buildType = cmSystemTools::UpperCase(buildType);
  892. std::string linkFlags = this->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  893. linkFlags += " ";
  894. if(buildType.size())
  895. {
  896. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  897. build += buildType;
  898. linkFlags += this->GetSafeDefinition(build.c_str());
  899. linkFlags += " ";
  900. }
  901. const char* targetPrefix = t.GetProperty("PREFIX");
  902. if(!targetPrefix)
  903. {
  904. targetPrefix = this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  905. }
  906. const char* targetSuffix = t.GetProperty("SUFFIX");
  907. if(!targetSuffix)
  908. {
  909. targetSuffix = this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  910. }
  911. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  912. if(targetLinkFlags)
  913. {
  914. linkFlags += targetLinkFlags;
  915. linkFlags += " ";
  916. }
  917. this->OutputLibraryRule(fout, name, t,
  918. targetPrefix,
  919. targetSuffix,
  920. createRule,
  921. "shared module",
  922. linkFlags.c_str());
  923. }
  924. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  925. const char* name,
  926. const cmTarget &t)
  927. {
  928. const char* createRule;
  929. if(t.HasCxx())
  930. {
  931. createRule = "CMAKE_CXX_CREATE_STATIC_LIBRARY";
  932. }
  933. else
  934. {
  935. createRule = "CMAKE_C_CREATE_STATIC_LIBRARY";
  936. }
  937. const char* targetPrefix = t.GetProperty("PREFIX");
  938. if(!targetPrefix)
  939. {
  940. targetPrefix = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  941. }
  942. const char* targetSuffix = t.GetProperty("SUFFIX");
  943. if(!targetSuffix)
  944. {
  945. targetSuffix = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  946. }
  947. this->OutputLibraryRule(fout, name, t,
  948. targetPrefix,
  949. targetSuffix,
  950. createRule,
  951. "static library",
  952. t.GetProperty("LINK_FLAGS"));
  953. }
  954. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  955. const char* name,
  956. const cmTarget &t)
  957. {
  958. std::string linkFlags;
  959. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  960. buildType = cmSystemTools::UpperCase(buildType);
  961. std::string flags;
  962. std::string target = m_ExecutableOutputPath + name
  963. + cmSystemTools::GetExecutableExtension();
  964. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  965. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  966. std::string depend = "$(";
  967. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  968. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  969. std::vector<std::string> rules;
  970. linkFlags += this->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  971. linkFlags += " ";
  972. if(buildType.size())
  973. {
  974. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  975. build += buildType;
  976. linkFlags += this->GetSafeDefinition(build.c_str());
  977. linkFlags += " ";
  978. }
  979. if(t.HasCxx())
  980. {
  981. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_LINK_EXECUTABLE"));
  982. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  983. flags += " ";
  984. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  985. flags += " ";
  986. }
  987. else
  988. {
  989. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_FLAGS");
  990. flags += " ";
  991. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_LINK_EXECUTABLE"));
  992. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  993. flags += " ";
  994. }
  995. cmOStringStream linklibs;
  996. this->OutputLinkLibraries(linklibs, 0, t);
  997. std::string comment = "executable";
  998. std::vector<std::string> commands;
  999. cmSystemTools::ExpandList(rules, commands);
  1000. std::string customCommands = this->CreateTargetRules(t, name);
  1001. if(customCommands.size() > 0)
  1002. {
  1003. commands.push_back(customCommands.c_str());
  1004. }
  1005. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1006. {
  1007. linkFlags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  1008. linkFlags += " ";
  1009. }
  1010. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  1011. {
  1012. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1013. linkFlags += " ";
  1014. }
  1015. else
  1016. {
  1017. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1018. linkFlags += " ";
  1019. }
  1020. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1021. if(targetLinkFlags)
  1022. {
  1023. linkFlags += targetLinkFlags;
  1024. linkFlags += " ";
  1025. }
  1026. for(std::vector<std::string>::iterator i = commands.begin();
  1027. i != commands.end(); ++i)
  1028. {
  1029. this->ExpandRuleVariables(*i,
  1030. objs.c_str(),
  1031. target.c_str(),
  1032. linklibs.str().c_str(),
  1033. 0,
  1034. 0,
  1035. flags.c_str(),
  1036. 0,
  1037. 0,
  1038. linkFlags.c_str());
  1039. }
  1040. this->OutputMakeRule(fout,
  1041. comment.c_str(),
  1042. target.c_str(),
  1043. depend.c_str(),
  1044. commands);
  1045. }
  1046. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  1047. const char* name,
  1048. const cmTarget &t)
  1049. {
  1050. std::string customCommands = this->CreateTargetRules(t, name);
  1051. const char* cc = 0;
  1052. if(customCommands.size() > 0)
  1053. {
  1054. cc = customCommands.c_str();
  1055. }
  1056. std::string comment = "Utility";
  1057. std::string depends;
  1058. std::string replaceVars;
  1059. const std::vector<cmCustomCommand> &ccs = t.GetCustomCommands();
  1060. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1061. i != ccs.end(); ++i)
  1062. {
  1063. const std::vector<std::string> & dep = i->GetDepends();
  1064. for(std::vector<std::string>::const_iterator d = dep.begin();
  1065. d != dep.end(); ++d)
  1066. {
  1067. depends += " \\\n";
  1068. replaceVars = *d;
  1069. m_Makefile->ExpandVariablesInString(replaceVars);
  1070. depends += cmSystemTools::ConvertToOutputPath(replaceVars.c_str());
  1071. }
  1072. }
  1073. this->OutputMakeRule(fout, comment.c_str(), name,
  1074. depends.c_str(), cc);
  1075. }
  1076. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  1077. {
  1078. // for each target
  1079. const cmTargets &tgts = m_Makefile->GetTargets();
  1080. for(cmTargets::const_iterator l = tgts.begin();
  1081. l != tgts.end(); l++)
  1082. {
  1083. switch(l->second.GetType())
  1084. {
  1085. case cmTarget::STATIC_LIBRARY:
  1086. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  1087. break;
  1088. case cmTarget::SHARED_LIBRARY:
  1089. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  1090. break;
  1091. case cmTarget::MODULE_LIBRARY:
  1092. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  1093. break;
  1094. case cmTarget::EXECUTABLE:
  1095. case cmTarget::WIN32_EXECUTABLE:
  1096. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  1097. break;
  1098. case cmTarget::UTILITY:
  1099. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  1100. break;
  1101. // This is handled by the OutputCustomRules method
  1102. case cmTarget::INSTALL_FILES:
  1103. // This is handled by the OutputInstallRules method
  1104. case cmTarget::INSTALL_PROGRAMS:
  1105. // This is handled by the OutputInstallRules method
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. // For each target that is an executable or shared library, generate
  1111. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1112. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1113. {
  1114. // Build a set of libraries that will be linked into any target in
  1115. // this directory.
  1116. std::set<std::string> used;
  1117. // for each target
  1118. const cmTargets &tgts = m_Makefile->GetTargets();
  1119. for(cmTargets::const_iterator l = tgts.begin();
  1120. l != tgts.end(); l++)
  1121. {
  1122. // Each dependency should only be emitted once per target.
  1123. std::set<std::string> emitted;
  1124. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1125. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1126. || (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  1127. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1128. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  1129. {
  1130. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1131. // A library should not depend on itself!
  1132. emitted.insert(l->first);
  1133. // for static libraries do not depend on other libraries
  1134. if(l->second.GetType() != cmTarget::STATIC_LIBRARY)
  1135. {
  1136. // Now, look at all link libraries specific to this target.
  1137. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1138. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1139. lib != tlibs.end(); ++lib)
  1140. {
  1141. // Record that this library was used.
  1142. used.insert(lib->first);
  1143. // Don't emit the same library twice for this target.
  1144. if(emitted.insert(lib->first).second)
  1145. {
  1146. // Output this dependency.
  1147. this->OutputLibDepend(fout, lib->first.c_str());
  1148. }
  1149. }
  1150. }
  1151. // for all targets depend on utilities
  1152. // Now, look at all utilities specific to this target.
  1153. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1154. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1155. util != tutils.end(); ++util)
  1156. {
  1157. // Record that this utility was used.
  1158. used.insert(*util);
  1159. // Don't emit the same utility twice for this target.
  1160. if(emitted.insert(*util).second)
  1161. {
  1162. // Output this dependency.
  1163. this->OutputExeDepend(fout, util->c_str());
  1164. }
  1165. }
  1166. fout << "\n";
  1167. }
  1168. }
  1169. fout << "\n";
  1170. // Loop over the libraries used and make sure there is a rule to
  1171. // build them in this makefile. If the library is in another
  1172. // directory, add a rule to jump to that directory and make sure it
  1173. // exists.
  1174. for(std::set<std::string>::const_iterator lib = used.begin();
  1175. lib != used.end(); ++lib)
  1176. {
  1177. // loop over the list of directories that the libraries might
  1178. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1179. // be stored in the cache
  1180. std::string libPath = *lib + "_CMAKE_PATH";
  1181. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1182. // if cache and not the current directory add a rule, to
  1183. // jump into the directory and build for the first time
  1184. if(cacheValue &&
  1185. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1186. {
  1187. // add the correct extension
  1188. std::string ltname = *lib+"_LIBRARY_TYPE";
  1189. const char* libType
  1190. = m_Makefile->GetDefinition(ltname.c_str());
  1191. // if it was a library..
  1192. if (libType)
  1193. {
  1194. std::string library;
  1195. std::string libpath = cacheValue;
  1196. if(libType && std::string(libType) == "SHARED")
  1197. {
  1198. library = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1199. library += *lib;
  1200. library += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1201. }
  1202. else if(libType && std::string(libType) == "MODULE")
  1203. {
  1204. library = this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1205. library += *lib;
  1206. library += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1207. }
  1208. else if(libType && std::string(libType) == "STATIC")
  1209. {
  1210. library = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1211. library += *lib;
  1212. library += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1213. }
  1214. else
  1215. {
  1216. cmSystemTools::Error("Unknown library type!");
  1217. return;
  1218. }
  1219. if(m_LibraryOutputPath.size())
  1220. {
  1221. libpath = m_LibraryOutputPath;
  1222. }
  1223. else
  1224. {
  1225. libpath += "/";
  1226. }
  1227. libpath += library;
  1228. // put out a rule to build the library if it does not exist
  1229. this->OutputBuildTargetInDir(fout,
  1230. cacheValue,
  1231. library.c_str(),
  1232. libpath.c_str(),
  1233. m_Makefile->
  1234. GetDefinition("LIBRARY_OUTPUT_PATH")
  1235. );
  1236. }
  1237. // something other than a library...
  1238. else
  1239. {
  1240. std::string exepath = cacheValue;
  1241. if(m_ExecutableOutputPath.size())
  1242. {
  1243. exepath = m_ExecutableOutputPath;
  1244. }
  1245. else
  1246. {
  1247. exepath += "/";
  1248. }
  1249. std::string fullName = lib->c_str();
  1250. fullName += cmSystemTools::GetExecutableExtension();
  1251. exepath += fullName;
  1252. this->OutputBuildTargetInDir(fout,
  1253. cacheValue,
  1254. fullName.c_str(),
  1255. exepath.c_str(),
  1256. m_Makefile->
  1257. GetDefinition("EXECUTABLE_OUTPUT_PATH")
  1258. );
  1259. }
  1260. }
  1261. }
  1262. }
  1263. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1264. const char* path,
  1265. const char* library,
  1266. const char* fullpath,
  1267. const char* libOutPath)
  1268. {
  1269. const char* makeTarget = library;
  1270. std::string currentDir =
  1271. cmSystemTools::ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  1272. std::string wpath = this->ConvertToOutputForExisting(path);
  1273. std::string wfullpath = this->ConvertToOutputForExisting(fullpath);
  1274. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  1275. {
  1276. makeTarget = wfullpath.c_str();
  1277. }
  1278. fout << wfullpath
  1279. << ":\n\tcd " << wpath << "\n"
  1280. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1281. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1282. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1283. << "\t$(MAKE) $(MAKESILENT) " << makeTarget
  1284. << "\n\tcd " << currentDir << "\n";
  1285. }
  1286. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1287. const char* path,
  1288. const char* library,
  1289. const char* fullpath,
  1290. const char* outputPath)
  1291. {
  1292. if(m_WindowsShell)
  1293. {
  1294. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath, outputPath);
  1295. return;
  1296. }
  1297. const char* makeTarget = library;
  1298. if(outputPath && strcmp( outputPath, "" ) != 0)
  1299. {
  1300. makeTarget = fullpath;
  1301. }
  1302. fout << this->ConvertToOutputForExisting(fullpath)
  1303. << ":\n\tcd " << this->ConvertToOutputForExisting(path)
  1304. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1305. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1306. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1307. << "; $(MAKE) $(MAKESILENT) "
  1308. << cmSystemTools::ConvertToOutputPath(makeTarget) << "\n\n";
  1309. }
  1310. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1311. {
  1312. if (strcmp(path1, path2) == 0)
  1313. {
  1314. return true;
  1315. }
  1316. #if defined(_WIN32) || defined(__APPLE__)
  1317. return
  1318. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1319. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1320. #else
  1321. return false;
  1322. #endif
  1323. }
  1324. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1325. const char* name)
  1326. {
  1327. std::string libPath = name;
  1328. libPath += "_CMAKE_PATH";
  1329. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1330. if(cacheValue )
  1331. {
  1332. // if there is a cache value, then this is a library that cmake
  1333. // knows how to build, so we can depend on it
  1334. std::string libpath;
  1335. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1336. {
  1337. // if the library is not in the current directory, then get the full
  1338. // path to it
  1339. if(m_LibraryOutputPath.size())
  1340. {
  1341. libpath = m_LibraryOutputPath;
  1342. }
  1343. else
  1344. {
  1345. libpath = cacheValue;
  1346. libpath += "/";
  1347. }
  1348. }
  1349. else
  1350. {
  1351. // library is in current Makefile so use lib as a prefix
  1352. libpath = m_LibraryOutputPath;
  1353. }
  1354. // add the correct extension
  1355. std::string ltname = name;
  1356. ltname += "_LIBRARY_TYPE";
  1357. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1358. if(libType && std::string(libType) == "SHARED")
  1359. {
  1360. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1361. libpath += name;
  1362. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1363. }
  1364. else if (libType && std::string(libType) == "MODULE")
  1365. {
  1366. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1367. libpath += name;
  1368. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1369. }
  1370. else if (libType && std::string(libType) == "STATIC")
  1371. {
  1372. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1373. libpath += name;
  1374. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1375. }
  1376. fout << cmSystemTools::ConvertToOutputPath(libpath.c_str()) << " ";
  1377. }
  1378. }
  1379. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1380. const char* name)
  1381. {
  1382. std::string exePath = name;
  1383. exePath += "_CMAKE_PATH";
  1384. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1385. if(cacheValue )
  1386. {
  1387. // if there is a cache value, then this is a executable/utility that cmake
  1388. // knows how to build, so we can depend on it
  1389. std::string exepath;
  1390. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1391. {
  1392. // if the exe/utility is not in the current directory, then get the full
  1393. // path to it
  1394. if(m_ExecutableOutputPath.size())
  1395. {
  1396. exepath = m_ExecutableOutputPath;
  1397. }
  1398. else
  1399. {
  1400. exepath = cacheValue;
  1401. exepath += "/";
  1402. }
  1403. }
  1404. else
  1405. {
  1406. // library is in current Makefile
  1407. exepath = m_ExecutableOutputPath;
  1408. }
  1409. // add the library name
  1410. exepath += name;
  1411. // add the correct extension
  1412. exepath += cmSystemTools::GetExecutableExtension();
  1413. fout << cmSystemTools::ConvertToOutputPath(exepath.c_str()) << " ";
  1414. }
  1415. }
  1416. // fix up names of directories so they can be used
  1417. // as targets in makefiles.
  1418. inline std::string FixDirectoryName(const char* dir)
  1419. {
  1420. std::string s = dir;
  1421. // replace ../ with 3 under bars
  1422. size_t pos = s.find("../");
  1423. if(pos != std::string::npos)
  1424. {
  1425. s.replace(pos, 3, "___");
  1426. }
  1427. // replace / directory separators with a single under bar
  1428. pos = s.find("/");
  1429. while(pos != std::string::npos)
  1430. {
  1431. s.replace(pos, 1, "_");
  1432. pos = s.find("/");
  1433. }
  1434. return s;
  1435. }
  1436. void cmLocalUnixMakefileGenerator::BuildInSubDirectoryWindows(std::ostream& fout,
  1437. const char* directory,
  1438. const char* target1,
  1439. const char* target2,
  1440. bool silent)
  1441. {
  1442. if(target1)
  1443. {
  1444. std::string dir = this->ConvertToOutputForExisting(directory);
  1445. if(dir[0] == '\"')
  1446. {
  1447. dir = dir.substr(1, dir.size()-2);
  1448. }
  1449. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1450. << " "
  1451. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1452. if (!silent)
  1453. {
  1454. fout << "\techo " << directory << ": building " << target1 << "\n";
  1455. }
  1456. fout << "\tcd " << dir << "\n"
  1457. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1458. }
  1459. if(target2)
  1460. {
  1461. if (!silent)
  1462. {
  1463. fout << "\techo " << directory << ": building " << target2 << "\n";
  1464. }
  1465. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1466. }
  1467. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  1468. fout << "\tcd " << this->ConvertToOutputForExisting(currentDir.c_str()) << "\n\n";
  1469. }
  1470. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1471. const char* dir,
  1472. const char* target1,
  1473. const char* target2,
  1474. bool silent)
  1475. {
  1476. if(m_WindowsShell)
  1477. {
  1478. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1479. return;
  1480. }
  1481. std::string directory = cmSystemTools::ConvertToOutputPath(dir);
  1482. if(target1)
  1483. {
  1484. fout << "\t@if test ! -d " << directory
  1485. << "; then $(MAKE) rebuild_cache; fi\n";
  1486. if (!silent)
  1487. {
  1488. fout << "\techo " << directory << ": building " << target1 << "\n";
  1489. }
  1490. fout << "\t@cd " << directory
  1491. << "; $(MAKE) " << target1 << "\n";
  1492. }
  1493. if(target2)
  1494. {
  1495. if (!silent)
  1496. {
  1497. fout << "\techo " << directory << ": building " << target2 << "\n";
  1498. }
  1499. fout << "\t@cd " << directory
  1500. << "; $(MAKE) " << target2 << "\n";
  1501. }
  1502. fout << "\n";
  1503. }
  1504. void
  1505. cmLocalUnixMakefileGenerator::
  1506. OutputSubDirectoryVars(std::ostream& fout,
  1507. const char* var,
  1508. const char* target,
  1509. const char* target1,
  1510. const char* target2,
  1511. const char* depend,
  1512. const std::vector<std::string>& SubDirectories,
  1513. bool silent)
  1514. {
  1515. if(!depend)
  1516. {
  1517. depend = "";
  1518. }
  1519. if( SubDirectories.size() == 0)
  1520. {
  1521. return;
  1522. }
  1523. fout << "# Variable for making " << target << " in subdirectories.\n";
  1524. fout << var << " = \\\n";
  1525. unsigned int ii;
  1526. for(ii =0; ii < SubDirectories.size(); ii++)
  1527. {
  1528. std::string subdir = FixDirectoryName(SubDirectories[ii].c_str());
  1529. fout << target << "_" << subdir.c_str();
  1530. if(ii == SubDirectories.size()-1)
  1531. {
  1532. fout << " \n\n";
  1533. }
  1534. else
  1535. {
  1536. fout << " \\\n";
  1537. }
  1538. }
  1539. fout << "# Targets for making " << target << " in subdirectories.\n";
  1540. std::string last = "";
  1541. for(unsigned int cc =0; cc < SubDirectories.size(); cc++)
  1542. {
  1543. std::string subdir = FixDirectoryName(SubDirectories[cc].c_str());
  1544. fout << target << "_" << subdir.c_str() << ": " << depend;
  1545. // Make each subdirectory depend on previous one. This forces
  1546. // parallel builds (make -j 2) to build in same order as a single
  1547. // threaded build to avoid dependency problems.
  1548. if(cc > 0)
  1549. {
  1550. fout << " " << target << "_" << last.c_str();
  1551. }
  1552. fout << "\n";
  1553. last = subdir;
  1554. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1555. dir += "/";
  1556. dir += SubDirectories[cc];
  1557. this->BuildInSubDirectory(fout, dir.c_str(),
  1558. target1, target2, silent);
  1559. }
  1560. fout << "\n\n";
  1561. }
  1562. // output rules for decending into sub directories
  1563. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1564. {
  1565. // Output Sub directory build rules
  1566. const std::vector<std::string>& SubDirectories
  1567. = m_Makefile->GetSubDirectories();
  1568. if( SubDirectories.size() == 0)
  1569. {
  1570. return;
  1571. }
  1572. this->OutputSubDirectoryVars(fout,
  1573. "SUBDIR_BUILD",
  1574. "default_target",
  1575. "default_target",
  1576. 0, "$(TARGETS)",
  1577. SubDirectories,
  1578. false);
  1579. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1580. "clean",
  1581. 0, 0,
  1582. SubDirectories);
  1583. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1584. "depend",
  1585. 0, 0,
  1586. SubDirectories);
  1587. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1588. "install",
  1589. 0, 0,
  1590. SubDirectories);
  1591. }
  1592. // Output the depend information for all the classes
  1593. // in the makefile. These would have been generated
  1594. // by the class cmMakeDepend GenerateMakefile
  1595. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1596. {
  1597. bool ret = false;
  1598. // Iterate over every target.
  1599. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1600. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1601. target != targets.end(); ++target)
  1602. {
  1603. // Iterate over every source for this target.
  1604. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1605. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1606. source != sources.end(); ++source)
  1607. {
  1608. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1609. {
  1610. if(!(*source)->GetDepends().empty())
  1611. {
  1612. // Iterate through all the dependencies for this source.
  1613. for(std::vector<std::string>::const_iterator dep =
  1614. (*source)->GetDepends().begin();
  1615. dep != (*source)->GetDepends().end(); ++dep)
  1616. {
  1617. fout << (*source)->GetSourceName()
  1618. << this->GetOutputExtension(
  1619. (*source)->GetSourceExtension().c_str()) << " : "
  1620. << cmSystemTools::ConvertToOutputPath(dep->c_str()) << "\n";
  1621. ret = true;
  1622. }
  1623. fout << "\n\n";
  1624. }
  1625. }
  1626. }
  1627. }
  1628. return ret;
  1629. }
  1630. // Output the depend information for all the classes
  1631. // in the makefile. These would have been generated
  1632. // by the class cmMakeDepend GenerateMakefile
  1633. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1634. {
  1635. std::set<std::string> emittedLowerPath;
  1636. std::set<std::string> emitted;
  1637. // Iterate over every target.
  1638. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1639. fout << "# Suppresses display of executed commands\n";
  1640. fout << ".SILENT:\n";
  1641. fout << "# disable some common implicit rules to speed things up\n";
  1642. fout << ".SUFFIXES:\n";
  1643. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1644. this->OutputMakeVariables(fout);
  1645. fout << "default:\n";
  1646. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1647. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1648. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1649. target != targets.end(); ++target)
  1650. {
  1651. // Iterate over every source for this target.
  1652. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1653. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1654. source != sources.end(); ++source)
  1655. {
  1656. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1657. {
  1658. if(!(*source)->GetDepends().empty())
  1659. {
  1660. for(std::vector<std::string>::const_iterator dep =
  1661. (*source)->GetDepends().begin();
  1662. dep != (*source)->GetDepends().end(); ++dep)
  1663. {
  1664. std::string dependfile =
  1665. cmSystemTools::ConvertToOutputPath(cmSystemTools::CollapseFullPath(dep->c_str()).c_str());
  1666. // use the lower path function to create uniqe names
  1667. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1668. if(emittedLowerPath.insert(lowerpath).second)
  1669. {
  1670. emitted.insert(dependfile);
  1671. fout << "all:: " << dependfile << "\n";
  1672. }
  1673. }
  1674. }
  1675. }
  1676. }
  1677. }
  1678. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1679. std::set<std::string>::iterator i;
  1680. for(i = emitted.begin(); i != emitted.end(); ++i)
  1681. {
  1682. fout << "cmake.depends:: " << *i <<
  1683. "\n\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1684. }
  1685. fout << "\n\n";
  1686. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1687. for(std::set<std::string>::iterator it = emitted.begin();
  1688. it != emitted.end(); ++it)
  1689. {
  1690. fout << *it << ":\n"
  1691. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1692. }
  1693. }
  1694. // Output each custom rule in the following format:
  1695. // output: source depends...
  1696. // (tab) command...
  1697. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1698. {
  1699. // We may be modifying the source groups temporarily, so make a copy.
  1700. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1701. const cmTargets &tgts = m_Makefile->GetTargets();
  1702. for(cmTargets::const_iterator tgt = tgts.begin();
  1703. tgt != tgts.end(); ++tgt)
  1704. {
  1705. // add any custom rules to the source groups
  1706. for (std::vector<cmCustomCommand>::const_iterator cr =
  1707. tgt->second.GetCustomCommands().begin();
  1708. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1709. {
  1710. // if the source for the custom command is the same name
  1711. // as the target, then to not create a rule in the makefile for
  1712. // the custom command, as the command will be fired when the other target
  1713. // is built.
  1714. if ( cr->GetSourceName().compare(tgt->first) !=0)
  1715. {
  1716. cmSourceGroup& sourceGroup =
  1717. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1718. sourceGroups);
  1719. cmCustomCommand cc(*cr);
  1720. cc.ExpandVariables(*m_Makefile);
  1721. sourceGroup.AddCustomCommand(cc);
  1722. }
  1723. }
  1724. }
  1725. // Loop through every source group.
  1726. for(std::vector<cmSourceGroup>::const_iterator sg =
  1727. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1728. {
  1729. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1730. if(buildRules.empty())
  1731. { continue; }
  1732. std::string name = sg->GetName();
  1733. if(name != "")
  1734. {
  1735. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1736. }
  1737. // Loop through each source in the source group.
  1738. for(cmSourceGroup::BuildRules::const_iterator cc =
  1739. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1740. {
  1741. std::string source = cc->first;
  1742. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  1743. // Loop through every command generating code from the current source.
  1744. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1745. c != commands.end(); ++c)
  1746. {
  1747. // escape spaces and convert to native slashes path for
  1748. // the command
  1749. const char* comment = c->second.m_Comment.c_str();
  1750. std::string command = c->second.m_Command;
  1751. cmSystemTools::ReplaceString(command, "/./", "/");
  1752. command = cmSystemTools::ConvertToOutputPath(command.c_str());
  1753. command += " ";
  1754. // now add the arguments
  1755. command += c->second.m_Arguments;
  1756. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1757. // if the command has no outputs, then it is a utility command
  1758. // with no outputs
  1759. if(commandFiles.m_Outputs.size() == 0)
  1760. {
  1761. std::string depends;
  1762. // collect out all the dependencies for this rule.
  1763. for(std::set<std::string>::const_iterator d =
  1764. commandFiles.m_Depends.begin();
  1765. d != commandFiles.m_Depends.end(); ++d)
  1766. {
  1767. std::string dep = *d;
  1768. cmSystemTools::ReplaceString(dep, "/./", "/");
  1769. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  1770. dep = cmSystemTools::ConvertToOutputPath(dep.c_str());
  1771. depends += " ";
  1772. depends += dep;
  1773. }
  1774. // output rule
  1775. this->OutputMakeRule(fout,
  1776. (*comment?comment:"Custom command"),
  1777. source.c_str(),
  1778. depends.c_str(),
  1779. command.c_str());
  1780. }
  1781. // Write a rule for every output generated by this command.
  1782. for(std::set<std::string>::const_iterator output =
  1783. commandFiles.m_Outputs.begin();
  1784. output != commandFiles.m_Outputs.end(); ++output)
  1785. {
  1786. std::string src = cmSystemTools::ConvertToOutputPath(source.c_str());
  1787. std::string depends;
  1788. depends += src;
  1789. // Collect out all the dependencies for this rule.
  1790. for(std::set<std::string>::const_iterator d =
  1791. commandFiles.m_Depends.begin();
  1792. d != commandFiles.m_Depends.end(); ++d)
  1793. {
  1794. std::string dep = *d;
  1795. cmSystemTools::ReplaceString(dep, "/./", "/");
  1796. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  1797. dep = cmSystemTools::ConvertToOutputPath(dep.c_str());
  1798. depends += " ";
  1799. depends += dep;
  1800. }
  1801. // output rule
  1802. this->OutputMakeRule(fout,
  1803. (*comment?comment:"Custom command"),
  1804. output->c_str(),
  1805. depends.c_str(),
  1806. command.c_str());
  1807. }
  1808. }
  1809. }
  1810. if(name != "")
  1811. {
  1812. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1813. }
  1814. }
  1815. }
  1816. std::string
  1817. cmLocalUnixMakefileGenerator::ConvertToOutputForExisting(const char* p)
  1818. {
  1819. std::string ret = cmSystemTools::ConvertToOutputPath(p);
  1820. // if there are spaces in the path, then get the short path version
  1821. // if there is one
  1822. if(ret.find(' ') != std::string::npos)
  1823. {
  1824. if(cmSystemTools::FileExists(p))
  1825. {
  1826. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  1827. {
  1828. ret = p;
  1829. }
  1830. }
  1831. }
  1832. return ret;
  1833. }
  1834. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1835. {
  1836. const char* variables =
  1837. "# the standard shell for make\n"
  1838. "SHELL = /bin/sh\n"
  1839. "\n";
  1840. if(!m_WindowsShell)
  1841. {
  1842. fout << variables;
  1843. }
  1844. else
  1845. {
  1846. fout <<
  1847. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  1848. "NULL=\n"
  1849. "!ELSE \n"
  1850. "NULL=nul\n"
  1851. "!ENDIF \n";
  1852. }
  1853. if(m_MakeSilentFlag.size())
  1854. {
  1855. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  1856. }
  1857. std::string cmakecommand = this->ConvertToOutputForExisting(
  1858. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  1859. fout << "CMAKE_COMMAND = "
  1860. << cmakecommand
  1861. << "\n";
  1862. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  1863. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1864. {
  1865. fout << "CMAKE_EDIT_COMMAND = "
  1866. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1867. << "\n";
  1868. }
  1869. fout << "CMAKE_CURRENT_SOURCE = " <<
  1870. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory())
  1871. << "\n";
  1872. fout << "CMAKE_CURRENT_BINARY = " <<
  1873. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory())
  1874. << "\n";
  1875. fout << "CMAKE_SOURCE_DIR = " <<
  1876. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory())
  1877. << "\n";
  1878. fout << "CMAKE_BINARY_DIR = " <<
  1879. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory())
  1880. << "\n";
  1881. // Output Include paths
  1882. fout << "INCLUDE_FLAGS = ";
  1883. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1884. std::vector<std::string>::iterator i;
  1885. fout << "-I" <<
  1886. this->ConvertToOutputForExisting(m_Makefile->GetStartDirectory()) << " ";
  1887. for(i = includes.begin(); i != includes.end(); ++i)
  1888. {
  1889. std::string include = *i;
  1890. // Don't output a -I for the standard include path "/usr/include".
  1891. // This can cause problems with certain standard library
  1892. // implementations because the wrong headers may be found first.
  1893. if(include != "/usr/include")
  1894. {
  1895. fout << "-I" << this->ConvertToOutputForExisting(i->c_str()) << " ";
  1896. }
  1897. }
  1898. fout << m_Makefile->GetDefineFlags();
  1899. fout << "\n\n";
  1900. }
  1901. void cmLocalUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1902. {
  1903. const char* root
  1904. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1905. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1906. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1907. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1908. const cmTargets &tgts = m_Makefile->GetTargets();
  1909. fout << "install: $(SUBDIR_INSTALL)\n";
  1910. fout << "\t@echo \"Installing ...\"\n";
  1911. const char* prefix
  1912. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1913. if (!prefix)
  1914. {
  1915. prefix = "/usr/local";
  1916. }
  1917. for(cmTargets::const_iterator l = tgts.begin();
  1918. l != tgts.end(); l++)
  1919. {
  1920. if (l->second.GetInstallPath() != "")
  1921. {
  1922. // first make the directories for each target
  1923. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1924. " ] ; then \\\n";
  1925. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1926. << l->second.GetInstallPath() << " \"; \\\n";
  1927. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1928. << "; \\\n";
  1929. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1930. << "; \\\n";
  1931. fout << "\t else true; \\\n";
  1932. fout << "\t fi\n";
  1933. // now install the target
  1934. switch (l->second.GetType())
  1935. {
  1936. case cmTarget::STATIC_LIBRARY:
  1937. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1938. << l->first;
  1939. fout << ".a";
  1940. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1941. break;
  1942. case cmTarget::SHARED_LIBRARY:
  1943. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath
  1944. << this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX")
  1945. << l->first;
  1946. fout << this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1947. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1948. break;
  1949. case cmTarget::MODULE_LIBRARY:
  1950. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath
  1951. << this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX")
  1952. << l->first;
  1953. fout << this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1954. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1955. break;
  1956. case cmTarget::WIN32_EXECUTABLE:
  1957. case cmTarget::EXECUTABLE:
  1958. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1959. << l->first
  1960. << cmSystemTools::GetExecutableExtension()
  1961. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1962. break;
  1963. case cmTarget::INSTALL_FILES:
  1964. {
  1965. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  1966. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  1967. sourcePath += "/";
  1968. binaryPath += "/";
  1969. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1970. std::vector<std::string>::const_iterator i;
  1971. for (i = sf.begin(); i != sf.end(); ++i)
  1972. {
  1973. std::string f = *i;
  1974. if(f.substr(0, sourcePath.length()) == sourcePath)
  1975. {
  1976. f = f.substr(sourcePath.length());
  1977. }
  1978. else if(f.substr(0, binaryPath.length()) == binaryPath)
  1979. {
  1980. f = f.substr(binaryPath.length());
  1981. }
  1982. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  1983. // avoid using install-sh to install install-sh
  1984. // does not work on windows....
  1985. if(*i == "install-sh")
  1986. {
  1987. fout << "\t @cp ";
  1988. }
  1989. else
  1990. {
  1991. fout << "\t @$(INSTALL_DATA) ";
  1992. }
  1993. fout << *i
  1994. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1995. }
  1996. }
  1997. break;
  1998. case cmTarget::INSTALL_PROGRAMS:
  1999. {
  2000. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  2001. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  2002. sourcePath += "/";
  2003. binaryPath += "/";
  2004. const std::vector<std::string> &sf = l->second.GetSourceLists();
  2005. std::vector<std::string>::const_iterator i;
  2006. for (i = sf.begin(); i != sf.end(); ++i)
  2007. {
  2008. std::string f = *i;
  2009. if(f.substr(0, sourcePath.length()) == sourcePath)
  2010. {
  2011. f = f.substr(sourcePath.length());
  2012. }
  2013. else if(f.substr(0, binaryPath.length()) == binaryPath)
  2014. {
  2015. f = f.substr(binaryPath.length());
  2016. }
  2017. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  2018. // avoid using install-sh to install install-sh
  2019. // does not work on windows....
  2020. if(*i == "install-sh")
  2021. {
  2022. fout << "\t @cp ";
  2023. }
  2024. else
  2025. {
  2026. fout << "\t @$(INSTALL_PROGRAM) ";
  2027. }
  2028. fout << *i
  2029. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  2030. }
  2031. }
  2032. break;
  2033. case cmTarget::UTILITY:
  2034. default:
  2035. break;
  2036. }
  2037. }
  2038. }
  2039. }
  2040. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  2041. {
  2042. this->OutputMakeRule(fout,
  2043. "default build rule",
  2044. "all",
  2045. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  2046. 0);
  2047. this->OutputMakeRule(fout,
  2048. "clean generated files",
  2049. "clean",
  2050. "$(SUBDIR_CLEAN)",
  2051. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  2052. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  2053. // collect up all the sources
  2054. std::string allsources;
  2055. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2056. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2057. target != targets.end(); ++target)
  2058. {
  2059. // Iterate over every source for this target.
  2060. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2061. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2062. source != sources.end(); ++source)
  2063. {
  2064. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2065. {
  2066. allsources += " \\\n";
  2067. allsources += cmSystemTools::ConvertToOutputPath((*source)->GetFullPath().c_str());
  2068. }
  2069. }
  2070. }
  2071. this->OutputMakeRule(fout,
  2072. "dependencies.",
  2073. "cmake.depends",
  2074. "$(CMAKE_MAKEFILE_SOURCES)",
  2075. "$(CMAKE_COMMAND) "
  2076. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2077. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  2078. );
  2079. this->OutputMakeRule(fout,
  2080. "dependencies",
  2081. "cmake.check_depends",
  2082. allsources.c_str(),
  2083. "$(CMAKE_COMMAND) "
  2084. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2085. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  2086. );
  2087. this->OutputMakeRule(fout,
  2088. "dependencies",
  2089. "depend",
  2090. "$(SUBDIR_DEPEND)",
  2091. "$(CMAKE_COMMAND) "
  2092. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2093. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2094. this->OutputMakeRule(fout,
  2095. "dependencies",
  2096. "dependlocal",
  2097. 0,
  2098. "$(CMAKE_COMMAND) "
  2099. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2100. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2101. this->OutputMakeRule(fout,
  2102. "CMakeCache.txt",
  2103. "rebuild_cache",
  2104. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2105. "$(CMAKE_COMMAND) "
  2106. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2107. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  2108. checkCache += "/cmake.check_cache";
  2109. checkCache = cmSystemTools::ConvertToOutputPath(checkCache.c_str());
  2110. std::string CMakeCache = m_Makefile->GetHomeOutputDirectory();
  2111. CMakeCache += "/CMakeCache.txt";
  2112. CMakeCache = cmSystemTools::ConvertToOutputPath(CMakeCache.c_str());
  2113. this->OutputMakeRule(fout,
  2114. "CMakeCache.txt because out-of-date:",
  2115. checkCache.c_str(),
  2116. CMakeCache.c_str(),
  2117. "$(CMAKE_COMMAND) "
  2118. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2119. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  2120. // called edit_cache
  2121. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2122. {
  2123. this->OutputMakeRule(fout,
  2124. "edit CMakeCache.txt",
  2125. "edit_cache",
  2126. 0,
  2127. "$(CMAKE_EDIT_COMMAND) "
  2128. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2129. }
  2130. this->OutputMakeRule(fout,
  2131. "CMakeCache.txt",
  2132. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2133. 0,
  2134. "$(CMAKE_COMMAND) "
  2135. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2136. this->OutputMakeRule(fout,
  2137. "Rule to keep make from removing Makefiles "
  2138. "if control-C is hit during a run of cmake.",
  2139. ".PRECIOUS",
  2140. "Makefile cmake.depends",
  2141. 0);
  2142. this->OutputSourceObjectBuildRules(fout);
  2143. // find ctest
  2144. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2145. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2146. ctest += "/";
  2147. ctest += "ctest";
  2148. ctest += cmSystemTools::GetExecutableExtension();
  2149. if(!cmSystemTools::FileExists(ctest.c_str()))
  2150. {
  2151. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2152. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2153. ctest += "/Debug/";
  2154. ctest += "ctest";
  2155. ctest += cmSystemTools::GetExecutableExtension();
  2156. }
  2157. if(!cmSystemTools::FileExists(ctest.c_str()))
  2158. {
  2159. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2160. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2161. ctest += "/Release/";
  2162. ctest += "ctest";
  2163. ctest += cmSystemTools::GetExecutableExtension();
  2164. }
  2165. if (cmSystemTools::FileExists(ctest.c_str()))
  2166. {
  2167. this->OutputMakeRule(fout,
  2168. "tests",
  2169. "test",
  2170. "",
  2171. this->ConvertToOutputForExisting(ctest.c_str()).c_str());
  2172. }
  2173. }
  2174. void
  2175. cmLocalUnixMakefileGenerator::
  2176. OutputBuildObjectFromSource(std::ostream& fout,
  2177. const char* shortName,
  2178. const cmSourceFile& source,
  2179. const char* extraCompileFlags,
  2180. bool shared)
  2181. {
  2182. // Header files shouldn't have build rules.
  2183. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  2184. {
  2185. return;
  2186. }
  2187. std::string comment = "object file";
  2188. std::string objectFile = std::string(shortName) +
  2189. this->GetOutputExtension(source.GetSourceExtension().c_str());
  2190. objectFile = cmSystemTools::ConvertToOutputPath(objectFile.c_str());
  2191. cmSystemTools::FileFormat format =
  2192. cmSystemTools::GetFileFormat(source.GetSourceExtension().c_str());
  2193. std::vector<std::string> rules;
  2194. std::string flags;
  2195. if(extraCompileFlags)
  2196. {
  2197. flags += extraCompileFlags;
  2198. }
  2199. std::string sourceFile =
  2200. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  2201. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2202. buildType = cmSystemTools::UpperCase(buildType);
  2203. switch(format)
  2204. {
  2205. case cmSystemTools::C_FILE_FORMAT:
  2206. {
  2207. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_COMPILE_OBJECT"));
  2208. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  2209. flags += " ";
  2210. if(buildType.size())
  2211. {
  2212. std::string build = "CMAKE_C_FLAGS_";
  2213. build += buildType;
  2214. flags += this->GetSafeDefinition(build.c_str());
  2215. flags += " ";
  2216. }
  2217. if(shared)
  2218. {
  2219. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  2220. flags += " ";
  2221. }
  2222. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2223. {
  2224. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_C_FLAGS");
  2225. flags += " ";
  2226. }
  2227. break;
  2228. }
  2229. case cmSystemTools::CXX_FILE_FORMAT:
  2230. {
  2231. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_COMPILE_OBJECT"));
  2232. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  2233. flags += " ";
  2234. if(buildType.size())
  2235. {
  2236. std::string build = "CMAKE_CXX_FLAGS_";
  2237. build += buildType;
  2238. flags += this->GetSafeDefinition(build.c_str());
  2239. flags += " ";
  2240. }
  2241. if(shared)
  2242. {
  2243. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  2244. flags += " ";
  2245. }
  2246. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2247. {
  2248. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  2249. flags += " ";
  2250. }
  2251. break;
  2252. }
  2253. case cmSystemTools::HEADER_FILE_FORMAT:
  2254. return;
  2255. case cmSystemTools::DEFINITION_FILE_FORMAT:
  2256. return;
  2257. case cmSystemTools::RESOURCE_FILE_FORMAT:
  2258. {
  2259. flags = " $(INCLUDE_FLAGS) ";
  2260. // use rc rule here if it is defined
  2261. const char* rule = m_Makefile->GetDefinition("CMAKE_COMPILE_RESOURCE");
  2262. if(rule)
  2263. {
  2264. rules.push_back(rule);
  2265. }
  2266. }
  2267. break;
  2268. case cmSystemTools::NO_FILE_FORMAT:
  2269. case cmSystemTools::JAVA_FILE_FORMAT:
  2270. case cmSystemTools::STATIC_LIBRARY_FILE_FORMAT:
  2271. case cmSystemTools::SHARED_LIBRARY_FILE_FORMAT:
  2272. case cmSystemTools::MODULE_FILE_FORMAT:
  2273. case cmSystemTools::OBJECT_FILE_FORMAT:
  2274. case cmSystemTools::UNKNOWN_FILE_FORMAT:
  2275. cmSystemTools::Error("Unexpected file type ",
  2276. sourceFile.c_str());
  2277. break;
  2278. }
  2279. flags += "$(INCLUDE_FLAGS) ";
  2280. // expand multi-command semi-colon separated lists
  2281. // of commands into separate commands
  2282. std::vector<std::string> commands;
  2283. cmSystemTools::ExpandList(rules, commands);
  2284. for(std::vector<std::string>::iterator i = commands.begin();
  2285. i != commands.end(); ++i)
  2286. {
  2287. this->ExpandRuleVariables(*i,
  2288. 0, // no objects
  2289. 0, // no target
  2290. 0, // no link libs
  2291. sourceFile.c_str(),
  2292. objectFile.c_str(),
  2293. flags.c_str());
  2294. }
  2295. std::vector<std::string> sourceAndDeps;
  2296. sourceAndDeps.push_back(sourceFile);
  2297. // Check for extra object-file dependencies.
  2298. std::vector<std::string> depends;
  2299. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  2300. if(additionalDeps)
  2301. {
  2302. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  2303. for(std::vector<std::string>::iterator i = depends.begin();
  2304. i != depends.end(); ++i)
  2305. {
  2306. sourceAndDeps.push_back(cmSystemTools::ConvertToOutputPath(i->c_str()));
  2307. }
  2308. }
  2309. this->OutputMakeRule(fout,
  2310. comment.c_str(),
  2311. objectFile.c_str(),
  2312. sourceAndDeps,
  2313. commands);
  2314. }
  2315. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  2316. {
  2317. fout << "# Rules to build " << this->GetOutputExtension("")
  2318. << " files from their sources:\n";
  2319. std::set<std::string> rules;
  2320. // Iterate over every target.
  2321. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2322. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2323. target != targets.end(); ++target)
  2324. {
  2325. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2326. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  2327. std::string exportsDef = "";
  2328. if(shared)
  2329. {
  2330. exportsDef = "-D"+target->first+"_EXPORTS ";
  2331. }
  2332. // Iterate over every source for this target.
  2333. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2334. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2335. source != sources.end(); ++source)
  2336. {
  2337. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2338. {
  2339. std::string shortName;
  2340. std::string sourceName;
  2341. // If the full path to the source file includes this
  2342. // directory, we want to use the relative path for the
  2343. // filename of the object file. Otherwise, we will use just
  2344. // the filename portion.
  2345. if((cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  2346. || (cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->
  2347. GetCurrentOutputDirectory()) == 0))
  2348. {
  2349. sourceName = (*source)->GetSourceName()+"."+(*source)->GetSourceExtension();
  2350. shortName = (*source)->GetSourceName();
  2351. // The path may be relative. See if a directory needs to be
  2352. // created for the output file. This is a ugly, and perhaps
  2353. // should be moved elsewhere.
  2354. std::string relPath =
  2355. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  2356. if(relPath != "")
  2357. {
  2358. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  2359. outPath += "/"+relPath;
  2360. cmSystemTools::MakeDirectory(outPath.c_str());
  2361. }
  2362. }
  2363. else
  2364. {
  2365. sourceName = (*source)->GetFullPath();
  2366. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  2367. }
  2368. std::string shortNameWithExt = shortName +
  2369. (*source)->GetSourceExtension();
  2370. // Only output a rule for each .o once.
  2371. std::string compileFlags = exportsDef;
  2372. compileFlags += " ";
  2373. if(rules.find(shortNameWithExt) == rules.end())
  2374. {
  2375. if((*source)->GetProperty("COMPILE_FLAGS"))
  2376. {
  2377. compileFlags += (*source)->GetProperty("COMPILE_FLAGS");
  2378. compileFlags += " ";
  2379. }
  2380. this->OutputBuildObjectFromSource(fout,
  2381. shortName.c_str(),
  2382. *(*source),
  2383. compileFlags.c_str(),
  2384. shared);
  2385. rules.insert(shortNameWithExt);
  2386. }
  2387. }
  2388. }
  2389. }
  2390. }
  2391. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2392. const char* comment,
  2393. const char* target,
  2394. const char* depends,
  2395. const char* command,
  2396. const char* command2,
  2397. const char* command3,
  2398. const char* command4)
  2399. {
  2400. std::vector<std::string> commands;
  2401. if(command)
  2402. {
  2403. commands.push_back(command);
  2404. }
  2405. if(command2)
  2406. {
  2407. commands.push_back(command2);
  2408. }
  2409. if(command3)
  2410. {
  2411. commands.push_back(command3);
  2412. }
  2413. if(command4)
  2414. {
  2415. commands.push_back(command4);
  2416. }
  2417. this->OutputMakeRule(fout, comment, target, depends, commands);
  2418. }
  2419. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2420. const char* comment,
  2421. const char* target,
  2422. const char* depends,
  2423. const std::vector<std::string>& commands)
  2424. {
  2425. std::vector<std::string> depend;
  2426. if(depends)
  2427. {
  2428. depend.push_back(depends);
  2429. }
  2430. this->OutputMakeRule(fout, comment, target, depend, commands);
  2431. }
  2432. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2433. const char* comment,
  2434. const char* target,
  2435. const std::vector<std::string>& depends,
  2436. const std::vector<std::string>& commands)
  2437. {
  2438. if(!target)
  2439. {
  2440. cmSystemTools::Error("no target for OutputMakeRule");
  2441. return;
  2442. }
  2443. std::string replace;
  2444. if(comment)
  2445. {
  2446. replace = comment;
  2447. m_Makefile->ExpandVariablesInString(replace);
  2448. fout << "#---------------------------------------------------------\n";
  2449. fout << "# " << replace;
  2450. fout << "\n#\n";
  2451. }
  2452. fout << "\n";
  2453. replace = target;
  2454. m_Makefile->ExpandVariablesInString(replace);
  2455. fout << cmSystemTools::ConvertToOutputPath(replace.c_str()) << ":";
  2456. for(std::vector<std::string>::const_iterator dep = depends.begin();
  2457. dep != depends.end(); ++dep)
  2458. {
  2459. replace = *dep;
  2460. m_Makefile->ExpandVariablesInString(replace);
  2461. fout << " " << replace.c_str();
  2462. }
  2463. fout << "\n";
  2464. int count = 0;
  2465. for (std::vector<std::string>::const_iterator i = commands.begin();
  2466. i != commands.end(); ++i)
  2467. {
  2468. replace = *i;
  2469. m_Makefile->ExpandVariablesInString(replace);
  2470. if(count == 0 && replace.find_first_not_of(" \t\n\r") != std::string::npos &&
  2471. replace[0] != '-' && replace.find("echo") != 0
  2472. && replace.find("$(MAKE)") != 0)
  2473. {
  2474. std::string echostring = "Building ";
  2475. echostring += comment;
  2476. echostring += " ";
  2477. echostring += target;
  2478. echostring += "...";
  2479. // for unix we want to quote the output of echo
  2480. // for nmake and borland, the echo should not be quoted
  2481. if(strcmp(m_GlobalGenerator->GetName(), "Unix Makefiles") == 0)
  2482. {
  2483. cmSystemTools::ReplaceString(echostring, "\\\n", " ");
  2484. cmSystemTools::ReplaceString(echostring, " \t", " ");
  2485. cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\techo \"");
  2486. fout << "\techo \"" << echostring.c_str() << "\"\n";
  2487. }
  2488. else
  2489. {
  2490. cmSystemTools::ReplaceString(echostring, "\n\t", "\n\techo ");
  2491. fout << "\techo " << echostring.c_str() << "\n";
  2492. }
  2493. }
  2494. fout << "\t" << replace.c_str() << "\n";
  2495. count++;
  2496. }
  2497. fout << "\n";
  2498. }
  2499. const char* cmLocalUnixMakefileGenerator::GetSafeDefinition(const char* def)
  2500. {
  2501. const char* ret = m_Makefile->GetDefinition(def);
  2502. if(!ret)
  2503. {
  2504. return "";
  2505. }
  2506. return ret;
  2507. }
  2508. std::string cmLocalUnixMakefileGenerator::LowerCasePath(const char* path)
  2509. {
  2510. #ifdef _WIN32
  2511. return cmSystemTools::LowerCase(path);
  2512. #else
  2513. return std::string(path);
  2514. #endif
  2515. }
  2516. std::string
  2517. cmLocalUnixMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  2518. {
  2519. if(!m_MakefileVariableSize)
  2520. {
  2521. return std::string(s) + std::string(s2);
  2522. }
  2523. std::string unmodified = s;
  2524. unmodified += s2;
  2525. // see if th
  2526. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2527. if(i != m_MakeVariableMap.end())
  2528. {
  2529. return i->second;
  2530. }
  2531. std::string ret = unmodified;
  2532. // if the string is greater the 32 chars it is an invalid vairable name
  2533. // for borland make
  2534. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2535. {
  2536. int keep = m_MakefileVariableSize - 8;
  2537. int size = keep + 3;
  2538. std::string str1 = s;
  2539. std::string str2 = s2;
  2540. // we must shorten the combined string by 4 charactors
  2541. // keep no more than 24 charactors from the second string
  2542. if(static_cast<int>(str2.size()) > keep)
  2543. {
  2544. str2 = str2.substr(0, keep);
  2545. }
  2546. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2547. {
  2548. str1 = str1.substr(0, size - str2.size());
  2549. }
  2550. char buffer[5];
  2551. int ni = 0;
  2552. sprintf(buffer, "%04d", ni);
  2553. ret = str1 + str2 + buffer;
  2554. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2555. {
  2556. ++ni;
  2557. sprintf(buffer, "%04d", ni);
  2558. ret = str1 + str2 + buffer;
  2559. }
  2560. if(ni == 1000)
  2561. {
  2562. cmSystemTools::Error("Borland makefile varible length too long");
  2563. return unmodified;
  2564. }
  2565. // once an unused variable is found
  2566. m_ShortMakeVariableMap[ret] = "1";
  2567. }
  2568. // always make an entry into the unmodified to varible map
  2569. m_MakeVariableMap[unmodified] = ret;
  2570. return ret;
  2571. }