cmLocalUnixMakefileGenerator.cxx 94 KB

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