cmLocalUnixMakefileGenerator.cxx 98 KB

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