cmLocalUnixMakefileGenerator.cxx 99 KB

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