cmLocalUnixMakefileGenerator.cxx 86 KB

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