cmLocalUnixMakefileGenerator.cxx 85 KB

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