cmLocalUnixMakefileGenerator.cxx 86 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. #if defined(_WIN32) && ! defined(__CYGWIN__)
  268. std::string sourceExtension = s;
  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 libname("lib([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  528. cmRegularExpression libname_noprefix("([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  529. if(libname.find(file))
  530. {
  531. librariesLinked += libLinkFlag;
  532. file = libname.match(1);
  533. librariesLinked += file;
  534. if(linkSuffix.size() && !hasSuffix.find(file))
  535. {
  536. librariesLinked += linkSuffix;
  537. }
  538. librariesLinked += " ";
  539. }
  540. else if(libname_noprefix.find(file))
  541. {
  542. librariesLinked += libLinkFlag;
  543. file = libname_noprefix.match(1);
  544. librariesLinked += file;
  545. if(linkSuffix.size() && !hasSuffix.find(file))
  546. {
  547. librariesLinked += linkSuffix;
  548. }
  549. librariesLinked += " ";
  550. }
  551. }
  552. // not a full path, so add -l name
  553. else
  554. {
  555. if(!reg.find(lib->first))
  556. {
  557. librariesLinked += libLinkFlag;
  558. }
  559. librariesLinked += lib->first;
  560. if(linkSuffix.size() && !hasSuffix.find(lib->first))
  561. {
  562. librariesLinked += linkSuffix;
  563. }
  564. librariesLinked += " ";
  565. }
  566. }
  567. linkLibs += librariesLinked;
  568. fout << linkLibs;
  569. if(outputRuntime && runtimeDirs.size()>0)
  570. {
  571. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  572. // a "-R a -R b -R c" type link line
  573. fout << runtimeFlag;
  574. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  575. fout << *itr;
  576. ++itr;
  577. for( ; itr != runtimeDirs.end(); ++itr )
  578. {
  579. if(runtimeConcatenate)
  580. {
  581. fout << runtimeSep << *itr;
  582. }
  583. else
  584. {
  585. fout << " " << runtimeFlag << *itr;
  586. }
  587. }
  588. fout << " ";
  589. }
  590. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  591. {
  592. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  593. }
  594. }
  595. std::string cmLocalUnixMakefileGenerator::CreateTargetRules(const cmTarget &target,
  596. const char* targetName)
  597. {
  598. std::string customRuleCode = "";
  599. bool initNext = false;
  600. for (std::vector<cmCustomCommand>::const_iterator cr =
  601. target.GetCustomCommands().begin();
  602. cr != target.GetCustomCommands().end(); ++cr)
  603. {
  604. cmCustomCommand cc(*cr);
  605. cc.ExpandVariables(*m_Makefile);
  606. if (cc.GetSourceName() == targetName)
  607. {
  608. if(initNext)
  609. {
  610. customRuleCode += "\n\t";
  611. }
  612. else
  613. {
  614. initNext = true;
  615. }
  616. std::string command = cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str());
  617. customRuleCode += command + " " + cc.GetArguments();
  618. }
  619. }
  620. return customRuleCode;
  621. }
  622. struct RuleVariables
  623. {
  624. const char* replace;
  625. const char* lookup;
  626. };
  627. static RuleVariables ruleReplaceVars[] =
  628. {
  629. {"<CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>", "CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS"},
  630. {"<CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS>", "CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS"},
  631. {"<CMAKE_CXX_LINK_FLAGS>", "CMAKE_CXX_LINK_FLAGS"},
  632. {"<CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS>", "CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS"},
  633. {"<CMAKE_SHARED_MODULE_CREATE_C_FLAGS>", "CMAKE_SHARED_MODULE_CREATE_C_FLAGS"},
  634. {"<CMAKE_C_LINK_FLAGS>", "CMAKE_C_LINK_FLAGS"},
  635. {"<CMAKE_AR>", "CMAKE_AR"},
  636. {"<CMAKE_RANLIB>", "CMAKE_RANLIB"},
  637. {0, 0}
  638. };
  639. void
  640. cmLocalUnixMakefileGenerator::ExpandRuleVariables(std::string& s,
  641. const char* objects,
  642. const char* target,
  643. const char* linkLibs,
  644. const char* source,
  645. const char* object,
  646. const char* flags,
  647. const char* objectsquoted,
  648. const char* targetBase,
  649. const char* linkFlags)
  650. {
  651. std::string cxxcompiler = this->ConvertToOutputForExisting(
  652. this->GetSafeDefinition("CMAKE_CXX_COMPILER"));
  653. std::string ccompiler = this->ConvertToOutputForExisting(
  654. this->GetSafeDefinition("CMAKE_C_COMPILER"));
  655. cmSystemTools::ReplaceString(s, "<CMAKE_CXX_COMPILER>", cxxcompiler.c_str());
  656. cmSystemTools::ReplaceString(s, "<CMAKE_C_COMPILER>", ccompiler.c_str());
  657. if(linkFlags)
  658. {
  659. cmSystemTools::ReplaceString(s, "<LINK_FLAGS>", linkFlags);
  660. }
  661. if(flags)
  662. {
  663. cmSystemTools::ReplaceString(s, "<FLAGS>", flags);
  664. }
  665. if(source)
  666. {
  667. cmSystemTools::ReplaceString(s, "<SOURCE>", source);
  668. }
  669. if(object)
  670. {
  671. cmSystemTools::ReplaceString(s, "<OBJECT>", object);
  672. }
  673. if(objects)
  674. {
  675. cmSystemTools::ReplaceString(s, "<OBJECTS>", objects);
  676. }
  677. if(objectsquoted)
  678. {
  679. cmSystemTools::ReplaceString(s, "<OBJECTS_QUOTED>", objectsquoted);
  680. }
  681. if(target)
  682. {
  683. cmSystemTools::ReplaceString(s, "<TARGET>", target);
  684. }
  685. if(targetBase)
  686. {
  687. cmSystemTools::ReplaceString(s, "<TARGET_BASE>", targetBase);
  688. }
  689. if(linkLibs)
  690. {
  691. cmSystemTools::ReplaceString(s, "<LINK_LIBRARIES>", linkLibs);
  692. }
  693. RuleVariables* rv = ruleReplaceVars;
  694. while(rv->replace)
  695. {
  696. cmSystemTools::ReplaceString(s, rv->replace,
  697. this->GetSafeDefinition(rv->lookup));
  698. rv++;
  699. }
  700. }
  701. void cmLocalUnixMakefileGenerator::OutputLibraryRule(std::ostream& fout,
  702. const char* name,
  703. const cmTarget &t,
  704. const char* prefix,
  705. const char* suffix,
  706. const char* createVariable,
  707. const char* comment,
  708. const char* linkFlags
  709. )
  710. {
  711. // create the library name
  712. std::string targetNameBase = prefix;
  713. targetNameBase += name;
  714. std::string targetName = prefix;
  715. targetName += name;
  716. targetName += suffix;
  717. // create the target full path name
  718. std::string targetFullPath = m_LibraryOutputPath + targetName;
  719. std::string targetBaseFullPath = m_LibraryOutputPath + targetNameBase;
  720. targetBaseFullPath =
  721. cmSystemTools::ConvertToOutputPath(targetBaseFullPath.c_str());
  722. targetFullPath = cmSystemTools::ConvertToOutputPath(targetFullPath.c_str());
  723. // get the objects that are used to link this library
  724. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  725. std::string objsQuoted = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ") ";
  726. // create a variable with the objects that this library depends on
  727. std::string depend = objs + " $("
  728. + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  729. // collect up the build rules
  730. std::vector<std::string> rules;
  731. std::string command = "$(RM) " + targetFullPath;
  732. rules.push_back(command);
  733. rules.push_back(m_Makefile->GetDefinition(createVariable));
  734. // expand multi-command semi-colon separated lists
  735. // of commands into separate commands
  736. std::vector<std::string> commands;
  737. cmSystemTools::ExpandListArguments(rules, commands);
  738. // collect custom commands for this target and add them to the list
  739. std::string customCommands = this->CreateTargetRules(t, name);
  740. if(customCommands.size() > 0)
  741. {
  742. commands.push_back(customCommands);
  743. }
  744. // collect up the link libraries
  745. cmOStringStream linklibs;
  746. this->OutputLinkLibraries(linklibs, name, t);
  747. for(std::vector<std::string>::iterator i = commands.begin();
  748. i != commands.end(); ++i)
  749. {
  750. this->ExpandRuleVariables(*i,
  751. objs.c_str(),
  752. targetFullPath.c_str(),
  753. linklibs.str().c_str(),
  754. 0, 0, 0, objsQuoted.c_str(),
  755. targetBaseFullPath.c_str(),
  756. linkFlags);
  757. }
  758. this->OutputMakeRule(fout, comment,
  759. targetFullPath.c_str(),
  760. depend.c_str(),
  761. commands);
  762. }
  763. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  764. const char* name,
  765. const cmTarget &t)
  766. {
  767. const char* createRule;
  768. if(t.HasCxx())
  769. {
  770. createRule = "CMAKE_CXX_CREATE_SHARED_LIBRARY";
  771. }
  772. else
  773. {
  774. createRule = "CMAKE_C_CREATE_SHARED_LIBRARY";
  775. }
  776. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  777. buildType = cmSystemTools::UpperCase(buildType);
  778. std::string linkFlags = this->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  779. linkFlags += " ";
  780. if(buildType.size())
  781. {
  782. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  783. build += buildType;
  784. linkFlags += this->GetSafeDefinition(build.c_str());
  785. linkFlags += " ";
  786. }
  787. #ifdef _WIN32
  788. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  789. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  790. i != sources.end(); ++i)
  791. {
  792. if((*i)->GetSourceExtension() == "def")
  793. {
  794. linkFlags += this->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  795. linkFlags += (*i)->GetFullPath();
  796. linkFlags += " ";
  797. }
  798. }
  799. #endif
  800. this->OutputLibraryRule(fout, name, t,
  801. this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX"),
  802. this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
  803. createRule,
  804. "shared library",
  805. linkFlags.c_str());
  806. }
  807. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  808. const char* name,
  809. const cmTarget &t)
  810. {
  811. const char* createRule;
  812. if(t.HasCxx())
  813. {
  814. createRule = "CMAKE_CXX_CREATE_SHARED_MODULE";
  815. }
  816. else
  817. {
  818. createRule = "CMAKE_C_CREATE_SHARED_MODULE";
  819. }
  820. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  821. buildType = cmSystemTools::UpperCase(buildType);
  822. std::string linkFlags = this->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  823. linkFlags += " ";
  824. if(buildType.size())
  825. {
  826. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  827. build += buildType;
  828. linkFlags += this->GetSafeDefinition(build.c_str());
  829. linkFlags += " ";
  830. }
  831. this->OutputLibraryRule(fout, name, t,
  832. this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX"),
  833. this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX"),
  834. createRule,
  835. "shared module",
  836. linkFlags.c_str());
  837. }
  838. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  839. const char* name,
  840. const cmTarget &t)
  841. {
  842. const char* createRule;
  843. if(t.HasCxx())
  844. {
  845. createRule = "CMAKE_CXX_CREATE_STATIC_LIBRARY";
  846. }
  847. else
  848. {
  849. createRule = "CMAKE_C_CREATE_STATIC_LIBRARY";
  850. }
  851. this->OutputLibraryRule(fout, name, t,
  852. this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX"),
  853. this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
  854. createRule,
  855. "static library", 0);
  856. }
  857. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  858. const char* name,
  859. const cmTarget &t)
  860. {
  861. std::string linkFlags;
  862. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  863. buildType = cmSystemTools::UpperCase(buildType);
  864. std::string flags;
  865. std::string target = m_ExecutableOutputPath + name
  866. + cmSystemTools::GetExecutableExtension();
  867. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  868. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  869. std::string depend = "$(";
  870. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  871. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  872. std::vector<std::string> rules;
  873. linkFlags += this->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  874. linkFlags += " ";
  875. if(buildType.size())
  876. {
  877. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  878. build += buildType;
  879. linkFlags += this->GetSafeDefinition(build.c_str());
  880. linkFlags += " ";
  881. }
  882. if(t.HasCxx())
  883. {
  884. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_LINK_EXECUTABLE"));
  885. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  886. flags += " ";
  887. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  888. flags += " ";
  889. }
  890. else
  891. {
  892. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_FLAGS");
  893. flags += " ";
  894. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_LINK_EXECUTABLE"));
  895. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  896. flags += " ";
  897. }
  898. cmOStringStream linklibs;
  899. this->OutputLinkLibraries(linklibs, 0, t);
  900. std::string comment = "executable";
  901. std::vector<std::string> commands;
  902. cmSystemTools::ExpandListArguments(rules, commands);
  903. std::string customCommands = this->CreateTargetRules(t, name);
  904. if(customCommands.size() > 0)
  905. {
  906. commands.push_back(customCommands.c_str());
  907. }
  908. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  909. {
  910. linkFlags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  911. linkFlags += " ";
  912. }
  913. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  914. {
  915. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  916. linkFlags += " ";
  917. }
  918. else
  919. {
  920. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  921. linkFlags += " ";
  922. }
  923. for(std::vector<std::string>::iterator i = commands.begin();
  924. i != commands.end(); ++i)
  925. {
  926. this->ExpandRuleVariables(*i,
  927. objs.c_str(),
  928. target.c_str(),
  929. linklibs.str().c_str(),
  930. 0,
  931. 0,
  932. flags.c_str(),
  933. 0,
  934. 0,
  935. linkFlags.c_str());
  936. }
  937. this->OutputMakeRule(fout,
  938. comment.c_str(),
  939. target.c_str(),
  940. depend.c_str(),
  941. commands);
  942. }
  943. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  944. const char* name,
  945. const cmTarget &t)
  946. {
  947. std::string customCommands = this->CreateTargetRules(t, name);
  948. const char* cc = 0;
  949. if(customCommands.size() > 0)
  950. {
  951. cc = customCommands.c_str();
  952. }
  953. std::string comment = "Utility";
  954. std::string depends;
  955. std::string replaceVars;
  956. const std::vector<cmCustomCommand> &ccs = t.GetCustomCommands();
  957. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  958. i != ccs.end(); ++i)
  959. {
  960. const std::vector<std::string> & dep = i->GetDepends();
  961. for(std::vector<std::string>::const_iterator d = dep.begin();
  962. d != dep.end(); ++d)
  963. {
  964. depends += " \\\n";
  965. replaceVars = *d;
  966. m_Makefile->ExpandVariablesInString(replaceVars);
  967. depends += cmSystemTools::ConvertToOutputPath(replaceVars.c_str());
  968. }
  969. }
  970. this->OutputMakeRule(fout, comment.c_str(), name,
  971. depends.c_str(), cc);
  972. }
  973. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  974. {
  975. // for each target
  976. const cmTargets &tgts = m_Makefile->GetTargets();
  977. for(cmTargets::const_iterator l = tgts.begin();
  978. l != tgts.end(); l++)
  979. {
  980. switch(l->second.GetType())
  981. {
  982. case cmTarget::STATIC_LIBRARY:
  983. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  984. break;
  985. case cmTarget::SHARED_LIBRARY:
  986. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  987. break;
  988. case cmTarget::MODULE_LIBRARY:
  989. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  990. break;
  991. case cmTarget::EXECUTABLE:
  992. case cmTarget::WIN32_EXECUTABLE:
  993. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  994. break;
  995. case cmTarget::UTILITY:
  996. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  997. break;
  998. // This is handled by the OutputCustomRules method
  999. case cmTarget::INSTALL_FILES:
  1000. // This is handled by the OutputInstallRules method
  1001. case cmTarget::INSTALL_PROGRAMS:
  1002. // This is handled by the OutputInstallRules method
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. // For each target that is an executable or shared library, generate
  1008. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1009. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1010. {
  1011. // Build a set of libraries that will be linked into any target in
  1012. // this directory.
  1013. std::set<std::string> used;
  1014. // for each target
  1015. const cmTargets &tgts = m_Makefile->GetTargets();
  1016. for(cmTargets::const_iterator l = tgts.begin();
  1017. l != tgts.end(); l++)
  1018. {
  1019. // Each dependency should only be emitted once per target.
  1020. std::set<std::string> emitted;
  1021. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1022. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1023. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1024. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  1025. {
  1026. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1027. // A library should not depend on itself!
  1028. emitted.insert(l->first);
  1029. // Now, look at all link libraries specific to this target.
  1030. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1031. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1032. lib != tlibs.end(); ++lib)
  1033. {
  1034. // Record that this library was used.
  1035. used.insert(lib->first);
  1036. // Don't emit the same library twice for this target.
  1037. if(emitted.insert(lib->first).second)
  1038. {
  1039. // Output this dependency.
  1040. this->OutputLibDepend(fout, lib->first.c_str());
  1041. }
  1042. }
  1043. // Now, look at all utilities specific to this target.
  1044. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1045. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1046. util != tutils.end(); ++util)
  1047. {
  1048. // Record that this utility was used.
  1049. used.insert(*util);
  1050. // Don't emit the same utility twice for this target.
  1051. if(emitted.insert(*util).second)
  1052. {
  1053. // Output this dependency.
  1054. this->OutputExeDepend(fout, util->c_str());
  1055. }
  1056. }
  1057. fout << "\n";
  1058. }
  1059. }
  1060. fout << "\n";
  1061. // Loop over the libraries used and make sure there is a rule to
  1062. // build them in this makefile. If the library is in another
  1063. // directory, add a rule to jump to that directory and make sure it
  1064. // exists.
  1065. for(std::set<std::string>::const_iterator lib = used.begin();
  1066. lib != used.end(); ++lib)
  1067. {
  1068. // loop over the list of directories that the libraries might
  1069. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1070. // be stored in the cache
  1071. std::string libPath = *lib + "_CMAKE_PATH";
  1072. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1073. // if cache and not the current directory add a rule, to
  1074. // jump into the directory and build for the first time
  1075. if(cacheValue &&
  1076. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1077. {
  1078. // add the correct extension
  1079. std::string ltname = *lib+"_LIBRARY_TYPE";
  1080. const char* libType
  1081. = m_Makefile->GetDefinition(ltname.c_str());
  1082. // if it was a library..
  1083. if (libType)
  1084. {
  1085. std::string library;
  1086. std::string libpath = cacheValue;
  1087. if(libType && std::string(libType) == "SHARED")
  1088. {
  1089. library = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1090. library += *lib;
  1091. library += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1092. }
  1093. else if(libType && std::string(libType) == "MODULE")
  1094. {
  1095. library = this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1096. library += *lib;
  1097. library += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1098. }
  1099. else if(libType && std::string(libType) == "STATIC")
  1100. {
  1101. library = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1102. library += *lib;
  1103. library += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1104. }
  1105. else
  1106. {
  1107. cmSystemTools::Error("Unknown library type!");
  1108. return;
  1109. }
  1110. if(m_LibraryOutputPath.size())
  1111. {
  1112. libpath = m_LibraryOutputPath;
  1113. }
  1114. else
  1115. {
  1116. libpath += "/";
  1117. }
  1118. libpath += library;
  1119. // put out a rule to build the library if it does not exist
  1120. this->OutputBuildTargetInDir(fout,
  1121. cacheValue,
  1122. library.c_str(),
  1123. libpath.c_str(),
  1124. m_Makefile->
  1125. GetDefinition("LIBRARY_OUTPUT_PATH")
  1126. );
  1127. }
  1128. // something other than a library...
  1129. else
  1130. {
  1131. std::string exepath = cacheValue;
  1132. if(m_ExecutableOutputPath.size())
  1133. {
  1134. exepath = m_ExecutableOutputPath;
  1135. }
  1136. else
  1137. {
  1138. exepath += "/";
  1139. }
  1140. exepath += *lib;
  1141. this->OutputBuildTargetInDir(fout,
  1142. cacheValue,
  1143. lib->c_str(),
  1144. exepath.c_str(),
  1145. m_Makefile->
  1146. GetDefinition("EXECUTABLE_OUTPUT_PATH")
  1147. );
  1148. }
  1149. }
  1150. }
  1151. }
  1152. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1153. const char* path,
  1154. const char* library,
  1155. const char* fullpath,
  1156. const char* libOutPath)
  1157. {
  1158. const char* makeTarget = library;
  1159. std::string currentDir =
  1160. cmSystemTools::ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  1161. std::string wpath = cmSystemTools::ConvertToOutputPath(path);
  1162. std::string wfullpath = cmSystemTools::ConvertToOutputPath(fullpath);
  1163. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  1164. {
  1165. makeTarget = wfullpath.c_str();
  1166. }
  1167. fout << wfullpath
  1168. << ":\n\tcd " << wpath << "\n"
  1169. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1170. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1171. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1172. << "\t$(MAKE) $(MAKESILENT) " << makeTarget
  1173. << "\n\tcd " << currentDir << "\n";
  1174. }
  1175. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1176. const char* path,
  1177. const char* library,
  1178. const char* fullpath,
  1179. const char* outputPath)
  1180. {
  1181. if(m_WindowsShell)
  1182. {
  1183. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath, outputPath);
  1184. return;
  1185. }
  1186. const char* makeTarget = library;
  1187. if(outputPath && strcmp( outputPath, "" ) != 0)
  1188. {
  1189. makeTarget = fullpath;
  1190. }
  1191. fout << cmSystemTools::ConvertToOutputPath(fullpath)
  1192. << ":\n\tcd " << cmSystemTools::ConvertToOutputPath(path)
  1193. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1194. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1195. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1196. << "; $(MAKE) $(MAKESILENT) "
  1197. << cmSystemTools::ConvertToOutputPath(makeTarget) << "\n\n";
  1198. }
  1199. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1200. {
  1201. if (strcmp(path1, path2) == 0)
  1202. {
  1203. return true;
  1204. }
  1205. #if defined(_WIN32) || defined(__APPLE__)
  1206. return
  1207. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1208. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1209. #else
  1210. return false;
  1211. #endif
  1212. }
  1213. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1214. const char* name)
  1215. {
  1216. std::string libPath = name;
  1217. libPath += "_CMAKE_PATH";
  1218. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1219. if(cacheValue )
  1220. {
  1221. // if there is a cache value, then this is a library that cmake
  1222. // knows how to build, so we can depend on it
  1223. std::string libpath;
  1224. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1225. {
  1226. // if the library is not in the current directory, then get the full
  1227. // path to it
  1228. if(m_LibraryOutputPath.size())
  1229. {
  1230. libpath = m_LibraryOutputPath;
  1231. }
  1232. else
  1233. {
  1234. libpath = cacheValue;
  1235. libpath += "/";
  1236. }
  1237. }
  1238. else
  1239. {
  1240. // library is in current Makefile so use lib as a prefix
  1241. libpath = m_LibraryOutputPath;
  1242. }
  1243. // add the correct extension
  1244. std::string ltname = name;
  1245. ltname += "_LIBRARY_TYPE";
  1246. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1247. if(libType && std::string(libType) == "SHARED")
  1248. {
  1249. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1250. libpath += name;
  1251. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1252. }
  1253. else if (libType && std::string(libType) == "MODULE")
  1254. {
  1255. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1256. libpath += name;
  1257. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1258. }
  1259. else if (libType && std::string(libType) == "STATIC")
  1260. {
  1261. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1262. libpath += name;
  1263. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1264. }
  1265. fout << cmSystemTools::ConvertToOutputPath(libpath.c_str()) << " ";
  1266. }
  1267. }
  1268. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1269. const char* name)
  1270. {
  1271. std::string exePath = name;
  1272. exePath += "_CMAKE_PATH";
  1273. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1274. if(cacheValue )
  1275. {
  1276. // if there is a cache value, then this is a executable/utility that cmake
  1277. // knows how to build, so we can depend on it
  1278. std::string exepath;
  1279. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1280. {
  1281. // if the exe/utility is not in the current directory, then get the full
  1282. // path to it
  1283. if(m_ExecutableOutputPath.size())
  1284. {
  1285. exepath = m_ExecutableOutputPath;
  1286. }
  1287. else
  1288. {
  1289. exepath = cacheValue;
  1290. exepath += "/";
  1291. }
  1292. }
  1293. else
  1294. {
  1295. // library is in current Makefile
  1296. exepath = m_ExecutableOutputPath;
  1297. }
  1298. // add the library name
  1299. exepath += name;
  1300. // add the correct extension
  1301. exepath += cmSystemTools::GetExecutableExtension();
  1302. fout << cmSystemTools::ConvertToOutputPath(exepath.c_str()) << " ";
  1303. }
  1304. }
  1305. // fix up names of directories so they can be used
  1306. // as targets in makefiles.
  1307. inline std::string FixDirectoryName(const char* dir)
  1308. {
  1309. std::string s = dir;
  1310. // replace ../ with 3 under bars
  1311. size_t pos = s.find("../");
  1312. if(pos != std::string::npos)
  1313. {
  1314. s.replace(pos, 3, "___");
  1315. }
  1316. // replace / directory separators with a single under bar
  1317. pos = s.find("/");
  1318. while(pos != std::string::npos)
  1319. {
  1320. s.replace(pos, 1, "_");
  1321. pos = s.find("/");
  1322. }
  1323. return s;
  1324. }
  1325. void cmLocalUnixMakefileGenerator::BuildInSubDirectoryWindows(std::ostream& fout,
  1326. const char* directory,
  1327. const char* target1,
  1328. const char* target2,
  1329. bool silent)
  1330. {
  1331. if(target1)
  1332. {
  1333. std::string dir = cmSystemTools::ConvertToOutputPath(directory);
  1334. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1335. << " "
  1336. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1337. if (!silent)
  1338. {
  1339. fout << "\techo " << directory << ": building " << target1 << "\n";
  1340. }
  1341. fout << "\tcd " << dir << "\n"
  1342. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1343. }
  1344. if(target2)
  1345. {
  1346. if (!silent)
  1347. {
  1348. fout << "\techo " << directory << ": building " << target2 << "\n";
  1349. }
  1350. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1351. }
  1352. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  1353. fout << "\tcd " << cmSystemTools::ConvertToOutputPath(currentDir.c_str()) << "\n\n";
  1354. }
  1355. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1356. const char* dir,
  1357. const char* target1,
  1358. const char* target2,
  1359. bool silent)
  1360. {
  1361. if(m_WindowsShell)
  1362. {
  1363. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1364. return;
  1365. }
  1366. std::string directory = cmSystemTools::ConvertToOutputPath(dir);
  1367. if(target1)
  1368. {
  1369. fout << "\t@if test ! -d " << directory
  1370. << "; then $(MAKE) rebuild_cache; fi\n";
  1371. if (!silent)
  1372. {
  1373. fout << "\techo " << directory << ": building " << target1 << "\n";
  1374. }
  1375. fout << "\t@cd " << directory
  1376. << "; $(MAKE) " << target1 << "\n";
  1377. }
  1378. if(target2)
  1379. {
  1380. if (!silent)
  1381. {
  1382. fout << "\techo " << directory << ": building " << target2 << "\n";
  1383. }
  1384. fout << "\t@cd " << directory
  1385. << "; $(MAKE) " << target2 << "\n";
  1386. }
  1387. fout << "\n";
  1388. }
  1389. void
  1390. cmLocalUnixMakefileGenerator::
  1391. OutputSubDirectoryVars(std::ostream& fout,
  1392. const char* var,
  1393. const char* target,
  1394. const char* target1,
  1395. const char* target2,
  1396. const char* depend,
  1397. const std::vector<std::string>& SubDirectories,
  1398. bool silent)
  1399. {
  1400. if(!depend)
  1401. {
  1402. depend = "";
  1403. }
  1404. if( SubDirectories.size() == 0)
  1405. {
  1406. return;
  1407. }
  1408. fout << "# Variable for making " << target << " in subdirectories.\n";
  1409. fout << var << " = \\\n";
  1410. unsigned int ii;
  1411. for(ii =0; ii < SubDirectories.size(); ii++)
  1412. {
  1413. std::string subdir = FixDirectoryName(SubDirectories[ii].c_str());
  1414. fout << target << "_" << subdir.c_str();
  1415. if(ii == SubDirectories.size()-1)
  1416. {
  1417. fout << " \n\n";
  1418. }
  1419. else
  1420. {
  1421. fout << " \\\n";
  1422. }
  1423. }
  1424. fout << "# Targets for making " << target << " in subdirectories.\n";
  1425. std::string last = "";
  1426. for(unsigned int cc =0; cc < SubDirectories.size(); cc++)
  1427. {
  1428. std::string subdir = FixDirectoryName(SubDirectories[cc].c_str());
  1429. fout << target << "_" << subdir.c_str() << ": " << depend;
  1430. // Make each subdirectory depend on previous one. This forces
  1431. // parallel builds (make -j 2) to build in same order as a single
  1432. // threaded build to avoid dependency problems.
  1433. if(cc > 0)
  1434. {
  1435. fout << " " << target << "_" << last.c_str();
  1436. }
  1437. fout << "\n";
  1438. last = subdir;
  1439. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1440. dir += "/";
  1441. dir += SubDirectories[cc];
  1442. this->BuildInSubDirectory(fout, dir.c_str(),
  1443. target1, target2, silent);
  1444. }
  1445. fout << "\n\n";
  1446. }
  1447. // output rules for decending into sub directories
  1448. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1449. {
  1450. // Output Sub directory build rules
  1451. const std::vector<std::string>& SubDirectories
  1452. = m_Makefile->GetSubDirectories();
  1453. if( SubDirectories.size() == 0)
  1454. {
  1455. return;
  1456. }
  1457. this->OutputSubDirectoryVars(fout,
  1458. "SUBDIR_BUILD",
  1459. "default_target",
  1460. "default_target",
  1461. 0, "$(TARGETS)",
  1462. SubDirectories,
  1463. false);
  1464. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1465. "clean",
  1466. 0, 0,
  1467. SubDirectories);
  1468. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1469. "depend",
  1470. 0, 0,
  1471. SubDirectories);
  1472. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1473. "install",
  1474. 0, 0,
  1475. SubDirectories);
  1476. }
  1477. // Output the depend information for all the classes
  1478. // in the makefile. These would have been generated
  1479. // by the class cmMakeDepend GenerateMakefile
  1480. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1481. {
  1482. bool ret = false;
  1483. // Iterate over every target.
  1484. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1485. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1486. target != targets.end(); ++target)
  1487. {
  1488. // Iterate over every source for this target.
  1489. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1490. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1491. source != sources.end(); ++source)
  1492. {
  1493. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1494. {
  1495. if(!(*source)->GetDepends().empty())
  1496. {
  1497. // Iterate through all the dependencies for this source.
  1498. for(std::vector<std::string>::const_iterator dep =
  1499. (*source)->GetDepends().begin();
  1500. dep != (*source)->GetDepends().end(); ++dep)
  1501. {
  1502. fout << (*source)->GetSourceName()
  1503. << this->GetOutputExtension(
  1504. (*source)->GetSourceExtension().c_str()) << " : "
  1505. << cmSystemTools::ConvertToOutputPath(dep->c_str()) << "\n";
  1506. ret = true;
  1507. }
  1508. fout << "\n\n";
  1509. }
  1510. }
  1511. }
  1512. }
  1513. return ret;
  1514. }
  1515. // Output the depend information for all the classes
  1516. // in the makefile. These would have been generated
  1517. // by the class cmMakeDepend GenerateMakefile
  1518. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1519. {
  1520. std::set<std::string> emittedLowerPath;
  1521. std::set<std::string> emitted;
  1522. // Iterate over every target.
  1523. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1524. fout << "# Suppresses display of executed commands\n";
  1525. fout << ".SILENT:\n";
  1526. fout << "# disable some common implicit rules to speed things up\n";
  1527. fout << ".SUFFIXES:\n";
  1528. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1529. this->OutputMakeVariables(fout);
  1530. fout << "default:\n";
  1531. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1532. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1533. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1534. target != targets.end(); ++target)
  1535. {
  1536. // Iterate over every source for this target.
  1537. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1538. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1539. source != sources.end(); ++source)
  1540. {
  1541. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1542. {
  1543. if(!(*source)->GetDepends().empty())
  1544. {
  1545. for(std::vector<std::string>::const_iterator dep =
  1546. (*source)->GetDepends().begin();
  1547. dep != (*source)->GetDepends().end(); ++dep)
  1548. {
  1549. std::string dependfile =
  1550. cmSystemTools::ConvertToOutputPath(cmSystemTools::CollapseFullPath(dep->c_str()).c_str());
  1551. // use the lower path function to create uniqe names
  1552. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1553. if(emittedLowerPath.insert(lowerpath).second)
  1554. {
  1555. emitted.insert(dependfile);
  1556. fout << "all:: " << dependfile << "\n";
  1557. }
  1558. }
  1559. }
  1560. }
  1561. }
  1562. }
  1563. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1564. std::set<std::string>::iterator i;
  1565. for(i = emitted.begin(); i != emitted.end(); ++i)
  1566. {
  1567. fout << "cmake.depends:: " << *i <<
  1568. "\n\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1569. }
  1570. fout << "\n\n";
  1571. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1572. for(std::set<std::string>::iterator it = emitted.begin();
  1573. it != emitted.end(); ++it)
  1574. {
  1575. fout << *it << ":\n"
  1576. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1577. }
  1578. }
  1579. // Output each custom rule in the following format:
  1580. // output: source depends...
  1581. // (tab) command...
  1582. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1583. {
  1584. // We may be modifying the source groups temporarily, so make a copy.
  1585. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1586. const cmTargets &tgts = m_Makefile->GetTargets();
  1587. for(cmTargets::const_iterator tgt = tgts.begin();
  1588. tgt != tgts.end(); ++tgt)
  1589. {
  1590. // add any custom rules to the source groups
  1591. for (std::vector<cmCustomCommand>::const_iterator cr =
  1592. tgt->second.GetCustomCommands().begin();
  1593. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1594. {
  1595. // if the source for the custom command is the same name
  1596. // as the target, then to not create a rule in the makefile for
  1597. // the custom command, as the command will be fired when the other target
  1598. // is built.
  1599. if ( cr->GetSourceName().compare(tgt->first) !=0)
  1600. {
  1601. cmSourceGroup& sourceGroup =
  1602. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1603. sourceGroups);
  1604. cmCustomCommand cc(*cr);
  1605. cc.ExpandVariables(*m_Makefile);
  1606. sourceGroup.AddCustomCommand(cc);
  1607. }
  1608. }
  1609. }
  1610. // Loop through every source group.
  1611. for(std::vector<cmSourceGroup>::const_iterator sg =
  1612. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1613. {
  1614. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1615. if(buildRules.empty())
  1616. { continue; }
  1617. std::string name = sg->GetName();
  1618. if(name != "")
  1619. {
  1620. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1621. }
  1622. // Loop through each source in the source group.
  1623. for(cmSourceGroup::BuildRules::const_iterator cc =
  1624. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1625. {
  1626. std::string source = cc->first;
  1627. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  1628. // Loop through every command generating code from the current source.
  1629. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1630. c != commands.end(); ++c)
  1631. {
  1632. // escape spaces and convert to native slashes path for
  1633. // the command
  1634. std::string command =
  1635. cmSystemTools::ConvertToOutputPath(c->second.m_Command.c_str());
  1636. command += " ";
  1637. // now add the arguments
  1638. command += c->second.m_Arguments;
  1639. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1640. // if the command has no outputs, then it is a utility command
  1641. // with no outputs
  1642. if(commandFiles.m_Outputs.size() == 0)
  1643. {
  1644. std::string depends;
  1645. // collect out all the dependencies for this rule.
  1646. for(std::set<std::string>::const_iterator d =
  1647. commandFiles.m_Depends.begin();
  1648. d != commandFiles.m_Depends.end(); ++d)
  1649. {
  1650. std::string dep = cmSystemTools::ConvertToOutputPath(d->c_str());
  1651. depends += " ";
  1652. depends += dep;
  1653. }
  1654. // output rule
  1655. this->OutputMakeRule(fout,
  1656. "Custom command",
  1657. source.c_str(),
  1658. depends.c_str(),
  1659. command.c_str());
  1660. }
  1661. // Write a rule for every output generated by this command.
  1662. for(std::set<std::string>::const_iterator output =
  1663. commandFiles.m_Outputs.begin();
  1664. output != commandFiles.m_Outputs.end(); ++output)
  1665. {
  1666. std::string src = cmSystemTools::ConvertToOutputPath(source.c_str());
  1667. std::string depends;
  1668. depends += src;
  1669. // Collect out all the dependencies for this rule.
  1670. for(std::set<std::string>::const_iterator d =
  1671. commandFiles.m_Depends.begin();
  1672. d != commandFiles.m_Depends.end(); ++d)
  1673. {
  1674. std::string dep = cmSystemTools::ConvertToOutputPath(d->c_str());
  1675. depends += " ";
  1676. depends += dep;
  1677. }
  1678. // output rule
  1679. this->OutputMakeRule(fout,
  1680. "Custom command",
  1681. output->c_str(),
  1682. depends.c_str(),
  1683. command.c_str());
  1684. }
  1685. }
  1686. }
  1687. if(name != "")
  1688. {
  1689. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1690. }
  1691. }
  1692. }
  1693. std::string
  1694. cmLocalUnixMakefileGenerator::ConvertToOutputForExisting(const char* p)
  1695. {
  1696. std::string ret = cmSystemTools::ConvertToOutputPath(p);
  1697. // if there are spaces in the path, then get the short path version
  1698. // if there is one
  1699. if(ret.find(' ') != std::string::npos)
  1700. {
  1701. cmSystemTools::GetShortPath(ret.c_str(), ret);
  1702. }
  1703. return ret;
  1704. }
  1705. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1706. {
  1707. const char* variables =
  1708. "# the standard shell for make\n"
  1709. "SHELL = /bin/sh\n"
  1710. "\n";
  1711. if(!m_WindowsShell)
  1712. {
  1713. fout << variables;
  1714. }
  1715. else
  1716. {
  1717. fout <<
  1718. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  1719. "NULL=\n"
  1720. "!ELSE \n"
  1721. "NULL=nul\n"
  1722. "!ENDIF \n";
  1723. }
  1724. if(m_MakeSilentFlag.size())
  1725. {
  1726. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  1727. }
  1728. std::string cmakecommand = this->ConvertToOutputForExisting(
  1729. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  1730. fout << "CMAKE_COMMAND = "
  1731. << cmakecommand
  1732. << "\n";
  1733. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  1734. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1735. {
  1736. fout << "CMAKE_EDIT_COMMAND = "
  1737. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1738. << "\n";
  1739. }
  1740. fout << "CMAKE_CURRENT_SOURCE = " <<
  1741. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory())
  1742. << "\n";
  1743. fout << "CMAKE_CURRENT_BINARY = " <<
  1744. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory())
  1745. << "\n";
  1746. fout << "CMAKE_SOURCE_DIR = " <<
  1747. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory())
  1748. << "\n";
  1749. fout << "CMAKE_BINARY_DIR = " <<
  1750. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory())
  1751. << "\n";
  1752. // Output Include paths
  1753. fout << "INCLUDE_FLAGS = ";
  1754. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1755. std::vector<std::string>::iterator i;
  1756. fout << "-I" <<
  1757. this->ConvertToOutputForExisting(m_Makefile->GetStartDirectory()) << " ";
  1758. for(i = includes.begin(); i != includes.end(); ++i)
  1759. {
  1760. std::string include = *i;
  1761. // Don't output a -I for the standard include path "/usr/include".
  1762. // This can cause problems with certain standard library
  1763. // implementations because the wrong headers may be found first.
  1764. if(include != "/usr/include")
  1765. {
  1766. fout << "-I" << this->ConvertToOutputForExisting(i->c_str()) << " ";
  1767. }
  1768. }
  1769. fout << m_Makefile->GetDefineFlags();
  1770. fout << "\n\n";
  1771. }
  1772. void cmLocalUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1773. {
  1774. const char* root
  1775. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1776. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1777. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1778. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1779. const cmTargets &tgts = m_Makefile->GetTargets();
  1780. fout << "install: $(SUBDIR_INSTALL)\n";
  1781. fout << "\t@echo \"Installing ...\"\n";
  1782. const char* prefix
  1783. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1784. if (!prefix)
  1785. {
  1786. prefix = "/usr/local";
  1787. }
  1788. for(cmTargets::const_iterator l = tgts.begin();
  1789. l != tgts.end(); l++)
  1790. {
  1791. if (l->second.GetInstallPath() != "")
  1792. {
  1793. // first make the directories for each target
  1794. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1795. " ] ; then \\\n";
  1796. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1797. << l->second.GetInstallPath() << " \"; \\\n";
  1798. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1799. << "; \\\n";
  1800. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1801. << "; \\\n";
  1802. fout << "\t else true; \\\n";
  1803. fout << "\t fi\n";
  1804. // now install the target
  1805. switch (l->second.GetType())
  1806. {
  1807. case cmTarget::STATIC_LIBRARY:
  1808. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1809. << l->first;
  1810. fout << ".a";
  1811. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1812. break;
  1813. case cmTarget::SHARED_LIBRARY:
  1814. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath
  1815. << this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX")
  1816. << l->first;
  1817. fout << this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1818. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1819. break;
  1820. case cmTarget::MODULE_LIBRARY:
  1821. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath
  1822. << this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX")
  1823. << l->first;
  1824. fout << this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1825. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1826. break;
  1827. case cmTarget::WIN32_EXECUTABLE:
  1828. case cmTarget::EXECUTABLE:
  1829. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1830. << l->first
  1831. << cmSystemTools::GetExecutableExtension()
  1832. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1833. break;
  1834. case cmTarget::INSTALL_FILES:
  1835. {
  1836. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  1837. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  1838. sourcePath += "/";
  1839. binaryPath += "/";
  1840. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1841. std::vector<std::string>::const_iterator i;
  1842. for (i = sf.begin(); i != sf.end(); ++i)
  1843. {
  1844. std::string f = *i;
  1845. if(f.substr(0, sourcePath.length()) == sourcePath)
  1846. {
  1847. f = f.substr(sourcePath.length());
  1848. }
  1849. else if(f.substr(0, binaryPath.length()) == binaryPath)
  1850. {
  1851. f = f.substr(binaryPath.length());
  1852. }
  1853. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  1854. // avoid using install-sh to install install-sh
  1855. // does not work on windows....
  1856. if(*i == "install-sh")
  1857. {
  1858. fout << "\t @cp ";
  1859. }
  1860. else
  1861. {
  1862. fout << "\t @$(INSTALL_DATA) ";
  1863. }
  1864. fout << *i
  1865. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1866. }
  1867. }
  1868. break;
  1869. case cmTarget::INSTALL_PROGRAMS:
  1870. {
  1871. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  1872. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  1873. sourcePath += "/";
  1874. binaryPath += "/";
  1875. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1876. std::vector<std::string>::const_iterator i;
  1877. for (i = sf.begin(); i != sf.end(); ++i)
  1878. {
  1879. std::string f = *i;
  1880. if(f.substr(0, sourcePath.length()) == sourcePath)
  1881. {
  1882. f = f.substr(sourcePath.length());
  1883. }
  1884. else if(f.substr(0, binaryPath.length()) == binaryPath)
  1885. {
  1886. f = f.substr(binaryPath.length());
  1887. }
  1888. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  1889. // avoid using install-sh to install install-sh
  1890. // does not work on windows....
  1891. if(*i == "install-sh")
  1892. {
  1893. fout << "\t @cp ";
  1894. }
  1895. else
  1896. {
  1897. fout << "\t @$(INSTALL_PROGRAM) ";
  1898. }
  1899. fout << *i
  1900. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1901. }
  1902. }
  1903. break;
  1904. case cmTarget::UTILITY:
  1905. default:
  1906. break;
  1907. }
  1908. }
  1909. }
  1910. }
  1911. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1912. {
  1913. this->OutputMakeRule(fout,
  1914. "default build rule",
  1915. "all",
  1916. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1917. 0);
  1918. this->OutputMakeRule(fout,
  1919. "clean generated files",
  1920. "clean",
  1921. "$(SUBDIR_CLEAN)",
  1922. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  1923. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  1924. // collect up all the sources
  1925. std::string allsources;
  1926. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1927. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1928. target != targets.end(); ++target)
  1929. {
  1930. // Iterate over every source for this target.
  1931. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1932. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1933. source != sources.end(); ++source)
  1934. {
  1935. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1936. {
  1937. allsources += " \\\n";
  1938. allsources += cmSystemTools::ConvertToOutputPath((*source)->GetFullPath().c_str());
  1939. }
  1940. }
  1941. }
  1942. this->OutputMakeRule(fout,
  1943. "dependencies.",
  1944. "cmake.depends",
  1945. "$(CMAKE_MAKEFILE_SOURCES)",
  1946. "$(CMAKE_COMMAND) "
  1947. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1948. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1949. );
  1950. this->OutputMakeRule(fout,
  1951. "dependencies",
  1952. "cmake.check_depends",
  1953. allsources.c_str(),
  1954. "$(CMAKE_COMMAND) "
  1955. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1956. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1957. );
  1958. this->OutputMakeRule(fout,
  1959. "dependencies",
  1960. "depend",
  1961. "$(SUBDIR_DEPEND)",
  1962. "$(CMAKE_COMMAND) "
  1963. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1964. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1965. this->OutputMakeRule(fout,
  1966. "dependencies",
  1967. "dependlocal",
  1968. 0,
  1969. "$(CMAKE_COMMAND) "
  1970. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1971. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1972. this->OutputMakeRule(fout,
  1973. "CMakeCache.txt",
  1974. "rebuild_cache",
  1975. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1976. "$(CMAKE_COMMAND) "
  1977. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1978. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  1979. // called edit_cache
  1980. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1981. {
  1982. this->OutputMakeRule(fout,
  1983. "edit CMakeCache.txt",
  1984. "edit_cache",
  1985. 0,
  1986. "$(CMAKE_EDIT_COMMAND) "
  1987. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1988. }
  1989. this->OutputMakeRule(fout,
  1990. "CMakeCache.txt",
  1991. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1992. 0,
  1993. "$(CMAKE_COMMAND) "
  1994. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1995. this->OutputMakeRule(fout,
  1996. "Rule to keep make from removing Makefiles "
  1997. "if control-C is hit during a run of cmake.",
  1998. ".PRECIOUS",
  1999. "Makefile cmake.depends",
  2000. 0);
  2001. this->OutputSourceObjectBuildRules(fout);
  2002. // find ctest
  2003. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2004. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2005. ctest += "/";
  2006. ctest += "ctest";
  2007. ctest += cmSystemTools::GetExecutableExtension();
  2008. if(!cmSystemTools::FileExists(ctest.c_str()))
  2009. {
  2010. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2011. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2012. ctest += "/Debug/";
  2013. ctest += "ctest";
  2014. ctest += cmSystemTools::GetExecutableExtension();
  2015. }
  2016. if(!cmSystemTools::FileExists(ctest.c_str()))
  2017. {
  2018. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2019. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2020. ctest += "/Release/";
  2021. ctest += "ctest";
  2022. ctest += cmSystemTools::GetExecutableExtension();
  2023. }
  2024. if (cmSystemTools::FileExists(ctest.c_str()))
  2025. {
  2026. this->OutputMakeRule(fout,
  2027. "tests",
  2028. "test",
  2029. "",
  2030. this->ConvertToOutputForExisting(ctest.c_str()).c_str());
  2031. }
  2032. }
  2033. void
  2034. cmLocalUnixMakefileGenerator::
  2035. OutputBuildObjectFromSource(std::ostream& fout,
  2036. const char* shortName,
  2037. const cmSourceFile& source,
  2038. const char* extraCompileFlags,
  2039. bool shared)
  2040. {
  2041. // Header files shouldn't have build rules.
  2042. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  2043. {
  2044. return;
  2045. }
  2046. std::string comment = "object file";
  2047. std::string objectFile = std::string(shortName) +
  2048. this->GetOutputExtension(source.GetSourceExtension().c_str());
  2049. objectFile = cmSystemTools::ConvertToOutputPath(objectFile.c_str());
  2050. cmSystemTools::FileFormat format =
  2051. cmSystemTools::GetFileFormat(source.GetSourceExtension().c_str());
  2052. std::vector<std::string> rules;
  2053. std::string flags;
  2054. if(extraCompileFlags)
  2055. {
  2056. flags += extraCompileFlags;
  2057. }
  2058. std::string sourceFile =
  2059. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  2060. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2061. buildType = cmSystemTools::UpperCase(buildType);
  2062. switch(format)
  2063. {
  2064. case cmSystemTools::C_FILE_FORMAT:
  2065. {
  2066. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_COMPILE_OBJECT"));
  2067. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  2068. flags += " ";
  2069. if(buildType.size())
  2070. {
  2071. std::string build = "CMAKE_C_FLAGS_";
  2072. build += buildType;
  2073. flags += this->GetSafeDefinition(build.c_str());
  2074. flags += " ";
  2075. }
  2076. if(shared)
  2077. {
  2078. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  2079. flags += " ";
  2080. }
  2081. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2082. {
  2083. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_C_FLAGS");
  2084. flags += " ";
  2085. }
  2086. break;
  2087. }
  2088. case cmSystemTools::CXX_FILE_FORMAT:
  2089. {
  2090. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_COMPILE_OBJECT"));
  2091. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  2092. flags += " ";
  2093. if(buildType.size())
  2094. {
  2095. std::string build = "CMAKE_CXX_FLAGS_";
  2096. build += buildType;
  2097. flags += this->GetSafeDefinition(build.c_str());
  2098. flags += " ";
  2099. }
  2100. if(shared)
  2101. {
  2102. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  2103. flags += " ";
  2104. }
  2105. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2106. {
  2107. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  2108. flags += " ";
  2109. }
  2110. break;
  2111. }
  2112. case cmSystemTools::HEADER_FILE_FORMAT:
  2113. return;
  2114. case cmSystemTools::DEFINITION_FILE_FORMAT:
  2115. return;
  2116. case cmSystemTools::RESOURCE_FILE_FORMAT:
  2117. {
  2118. flags = " $(INCLUDE_FLAGS) ";
  2119. // use rc rule here if it is defined
  2120. const char* rule = m_Makefile->GetDefinition("CMAKE_COMPILE_RESOURCE");
  2121. if(rule)
  2122. {
  2123. rules.push_back(rule);
  2124. }
  2125. }
  2126. break;
  2127. case cmSystemTools::NO_FILE_FORMAT:
  2128. case cmSystemTools::JAVA_FILE_FORMAT:
  2129. case cmSystemTools::STATIC_LIBRARY_FILE_FORMAT:
  2130. case cmSystemTools::SHARED_LIBRARY_FILE_FORMAT:
  2131. case cmSystemTools::MODULE_FILE_FORMAT:
  2132. case cmSystemTools::OBJECT_FILE_FORMAT:
  2133. case cmSystemTools::UNKNOWN_FILE_FORMAT:
  2134. cmSystemTools::Error("Unexpected file type ",
  2135. sourceFile.c_str());
  2136. break;
  2137. }
  2138. flags += "$(INCLUDE_FLAGS) ";
  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(static_cast<int>(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(static_cast<int>(str2.size()) > keep)
  2371. {
  2372. str2 = str2.substr(0, keep);
  2373. }
  2374. if(static_cast<int>(str1.size()) + static_cast<int>(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. }