cmLocalUnixMakefileGenerator.cxx 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. cmLocalUnixMakefileGenerator::cmLocalUnixMakefileGenerator()
  22. :m_SharedLibraryExtension("$(SHLIB_SUFFIX)"),
  23. m_ObjectFileExtension(".o"),
  24. m_ExecutableExtension(cmSystemTools::GetExecutableExtension()),
  25. m_StaticLibraryExtension(".a"),
  26. m_LibraryPrefix("lib")
  27. {
  28. }
  29. cmLocalUnixMakefileGenerator::~cmLocalUnixMakefileGenerator()
  30. {
  31. }
  32. void cmLocalUnixMakefileGenerator::Generate(bool fromTheTop)
  33. {
  34. // for backwards compatibility if niether c or cxx is
  35. // enabled, the enable cxx
  36. if(! (m_GlobalGenerator->GetLanguageEnabled("C") ||
  37. m_GlobalGenerator->GetLanguageEnabled("CXX")))
  38. {
  39. m_GlobalGenerator->EnableLanguage("CXX",m_Makefile);
  40. }
  41. // suppoirt override in output directories
  42. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  43. {
  44. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  45. if(m_LibraryOutputPath.size())
  46. {
  47. if(m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
  48. {
  49. m_LibraryOutputPath += "/";
  50. }
  51. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  52. {
  53. cmSystemTools::Error("Error failed create "
  54. "LIBRARY_OUTPUT_PATH directory:",
  55. m_LibraryOutputPath.c_str());
  56. }
  57. m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  58. }
  59. }
  60. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  61. {
  62. m_ExecutableOutputPath =
  63. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  64. if(m_ExecutableOutputPath.size())
  65. {
  66. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
  67. {
  68. m_ExecutableOutputPath += "/";
  69. }
  70. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  71. {
  72. cmSystemTools::Error("Error failed to create "
  73. "EXECUTABLE_OUTPUT_PATH directory:",
  74. m_ExecutableOutputPath.c_str());
  75. }
  76. m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
  77. }
  78. }
  79. this->m_Makefile->GenerateMakefile();
  80. if (!fromTheTop)
  81. {
  82. // Generate depends
  83. cmMakeDepend md;
  84. md.SetMakefile(m_Makefile);
  85. md.GenerateMakefileDependencies();
  86. this->ProcessDepends(md);
  87. }
  88. // output the makefile fragment
  89. std::string dest = m_Makefile->GetStartOutputDirectory();
  90. dest += "/Makefile";
  91. this->OutputMakefile(dest.c_str(), !fromTheTop);
  92. }
  93. void cmLocalUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  94. {
  95. // Now create cmDependInformation objects for files in the directory
  96. cmTargets &tgts = m_Makefile->GetTargets();
  97. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  98. {
  99. std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  100. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  101. i != classes.end(); ++i)
  102. {
  103. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  104. {
  105. // get the depends
  106. const cmDependInformation *info =
  107. md.GetDependInformationForSourceFile(*(*i));
  108. // Delete any hints from the source file's dependencies.
  109. (*i)->GetDepends().erase((*i)->GetDepends().begin(), (*i)->GetDepends().end());
  110. // Now add the real dependencies for the file.
  111. if (info)
  112. {
  113. for(cmDependInformation::DependencySet::const_iterator d =
  114. info->m_DependencySet.begin();
  115. d != info->m_DependencySet.end(); ++d)
  116. {
  117. // Make sure the full path is given. If not, the dependency was
  118. // not found.
  119. if((*d)->m_FullPath != "")
  120. {
  121. (*i)->GetDepends().push_back((*d)->m_FullPath);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. // This is where CMakeTargets.make is generated
  130. void cmLocalUnixMakefileGenerator::OutputMakefile(const char* file,
  131. bool withDepends)
  132. {
  133. // Create sub directories fro aux source directories
  134. std::vector<std::string>& auxSourceDirs =
  135. m_Makefile->GetAuxSourceDirectories();
  136. if( auxSourceDirs.size() )
  137. {
  138. // For the case when this is running as a remote build
  139. // on unix, make the directory
  140. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  141. i != auxSourceDirs.end(); ++i)
  142. {
  143. if(i->c_str()[0] != '/')
  144. {
  145. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  146. if(dir.size() && dir[dir.size()-1] != '/')
  147. {
  148. dir += "/";
  149. }
  150. dir += *i;
  151. cmSystemTools::MakeDirectory(dir.c_str());
  152. }
  153. else
  154. {
  155. cmSystemTools::MakeDirectory(i->c_str());
  156. }
  157. }
  158. }
  159. // Create a stream that writes to a temporary file
  160. // then does a copy at the end. This is to allow users
  161. // to hit control-c during the make of the makefile
  162. cmGeneratedFileStream tempFile(file);
  163. tempFile.SetAlwaysCopy(true);
  164. std::ostream& fout = tempFile.GetStream();
  165. if(!fout)
  166. {
  167. cmSystemTools::Error("Error can not open for write: ", file);
  168. return;
  169. }
  170. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  171. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  172. << " Generator, CMake Version "
  173. << cmMakefile::GetMajorVersion() << "."
  174. << cmMakefile::GetMinorVersion() << "\n"
  175. << "# Generated from the following files:\n# "
  176. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  177. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  178. // sort the array
  179. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  180. // remove duplicates
  181. std::vector<std::string>::iterator new_end =
  182. std::unique(lfiles.begin(), lfiles.end());
  183. lfiles.erase(new_end, lfiles.end());
  184. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  185. i != lfiles.end(); ++i)
  186. {
  187. fout << "# " << i->c_str() << "\n";
  188. }
  189. fout << "\n\n";
  190. fout << "# Suppresses display of executed commands\n";
  191. fout << ".SILENT:\n";
  192. fout << "# disable some common implicit rules to speed things up\n";
  193. fout << ".SUFFIXES:\n";
  194. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  195. // create a make variable with all of the sources for this Makefile
  196. // for depend purposes.
  197. fout << "CMAKE_MAKEFILE_SOURCES = ";
  198. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  199. i != lfiles.end(); ++i)
  200. {
  201. fout << " " << cmSystemTools::ConvertToOutputPath(i->c_str());
  202. }
  203. // Add the cache to the list
  204. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  205. cacheFile += "/CMakeCache.txt";
  206. fout << " " << cmSystemTools::ConvertToOutputPath(cacheFile.c_str());
  207. fout << "\n\n\n";
  208. this->OutputMakeVariables(fout);
  209. // Set up the default target as the VERY first target, so that make with no arguments will run it
  210. this->
  211. OutputMakeRule(fout,
  212. "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",
  213. "default_target",
  214. 0,
  215. "$(MAKE) $(MAKESILENT) cmake.depends",
  216. "$(MAKE) $(MAKESILENT) cmake.check_depends",
  217. "$(MAKE) $(MAKESILENT) -f cmake.check_depends",
  218. "$(MAKE) $(MAKESILENT) all");
  219. this->OutputTargetRules(fout);
  220. this->OutputDependLibs(fout);
  221. this->OutputTargets(fout);
  222. this->OutputSubDirectoryRules(fout);
  223. std::string dependName = m_Makefile->GetStartOutputDirectory();
  224. dependName += "/cmake.depends";
  225. if(withDepends)
  226. {
  227. std::ofstream dependout(dependName.c_str());
  228. if(!dependout)
  229. {
  230. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  231. return;
  232. }
  233. dependout << "# .o dependencies in this directory." << std::endl;
  234. std::string checkDepend = m_Makefile->GetStartOutputDirectory();
  235. checkDepend += "/cmake.check_depends";
  236. std::ofstream checkdependout(checkDepend.c_str());
  237. if(!checkdependout)
  238. {
  239. cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
  240. return;
  241. }
  242. 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;
  243. // if there were any depends output, then output the check depends
  244. // information inot checkdependout
  245. if(this->OutputObjectDepends(dependout))
  246. {
  247. this->OutputCheckDepends(checkdependout);
  248. }
  249. else
  250. {
  251. checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
  252. }
  253. }
  254. this->OutputCustomRules(fout);
  255. this->OutputMakeRules(fout);
  256. this->OutputInstallRules(fout);
  257. // only add the depend include if the depend file exists
  258. if(cmSystemTools::FileExists(dependName.c_str()))
  259. {
  260. this->OutputIncludeMakefile(fout, "cmake.depends");
  261. }
  262. }
  263. void cmLocalUnixMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  264. const char* file)
  265. {
  266. fout << "include " << file << "\n";
  267. }
  268. std::string
  269. cmLocalUnixMakefileGenerator::GetOutputExtension(const char*)
  270. {
  271. return m_ObjectFileExtension;
  272. }
  273. // Output the rules for any targets
  274. void cmLocalUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  275. {
  276. // for each target add to the list of targets
  277. fout << "TARGETS = ";
  278. const cmTargets &tgts = m_Makefile->GetTargets();
  279. // list libraries first
  280. for(cmTargets::const_iterator l = tgts.begin();
  281. l != tgts.end(); l++)
  282. {
  283. if (l->second.IsInAll())
  284. {
  285. std::string path = m_LibraryOutputPath + m_LibraryPrefix;
  286. if(l->second.GetType() == cmTarget::STATIC_LIBRARY)
  287. {
  288. path = path + l->first + m_StaticLibraryExtension;
  289. fout << " \\\n"
  290. << cmSystemTools::ConvertToOutputPath(path.c_str());
  291. }
  292. else if(l->second.GetType() == cmTarget::SHARED_LIBRARY)
  293. {
  294. path = path + l->first +
  295. m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  296. fout << " \\\n"
  297. << cmSystemTools::ConvertToOutputPath(path.c_str());
  298. }
  299. else if(l->second.GetType() == cmTarget::MODULE_LIBRARY)
  300. {
  301. path = path + l->first +
  302. m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  303. fout << " \\\n"
  304. << cmSystemTools::ConvertToOutputPath(path.c_str());
  305. }
  306. }
  307. }
  308. // executables
  309. for(cmTargets::const_iterator l = tgts.begin();
  310. l != tgts.end(); l++)
  311. {
  312. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  313. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  314. l->second.IsInAll())
  315. {
  316. std::string path = m_ExecutableOutputPath + l->first +
  317. m_ExecutableExtension;
  318. fout << " \\\n" << cmSystemTools::ConvertToOutputPath(path.c_str());
  319. }
  320. }
  321. // list utilities last
  322. for(cmTargets::const_iterator l = tgts.begin();
  323. l != tgts.end(); l++)
  324. {
  325. if (l->second.GetType() == cmTarget::UTILITY &&
  326. l->second.IsInAll())
  327. {
  328. fout << " \\\n" << l->first.c_str();
  329. }
  330. }
  331. fout << "\n\n";
  332. // get the classes from the source lists then add them to the groups
  333. for(cmTargets::const_iterator l = tgts.begin();
  334. l != tgts.end(); l++)
  335. {
  336. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  337. if (classes.begin() != classes.end())
  338. {
  339. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS") << " = ";
  340. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  341. i != classes.end(); i++)
  342. {
  343. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  344. {
  345. std::string outExt(this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
  346. if(outExt.size())
  347. {
  348. fout << "\\\n" << cmSystemTools::ConvertToOutputPath((*i)->GetSourceName().c_str())
  349. << outExt.c_str() << " ";
  350. }
  351. }
  352. }
  353. fout << "\n\n";
  354. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS_QUOTED") << " = ";
  355. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  356. i != classes.end(); i++)
  357. {
  358. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  359. {
  360. std::string outExt(this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
  361. if(outExt.size())
  362. {
  363. fout << "\\\n\"" << cmSystemTools::ConvertToOutputPath((*i)->GetSourceName().c_str())
  364. << outExt.c_str() << "\" ";
  365. }
  366. }
  367. }
  368. fout << "\n\n";
  369. }
  370. }
  371. fout << "CLEAN_OBJECT_FILES = ";
  372. for(cmTargets::const_iterator l = tgts.begin();
  373. l != tgts.end(); l++)
  374. {
  375. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  376. if (classes.begin() != classes.end())
  377. {
  378. fout << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
  379. << ") ";
  380. }
  381. }
  382. fout << "\n\n";
  383. const char * qt_files = m_Makefile->GetDefinition("GENERATED_QT_FILES");
  384. if (m_Makefile->GetDefinition("GENERATED_QT_FILES")!=NULL &&
  385. strlen(m_Makefile->GetDefinition("GENERATED_QT_FILES"))>0)
  386. {
  387. fout << "GENERATED_QT_FILES = ";
  388. fout << m_Makefile->GetDefinition("GENERATED_QT_FILES");
  389. fout << "\n\n";
  390. }
  391. }
  392. /**
  393. * Output the linking rules on a command line. For executables,
  394. * targetLibrary should be a NULL pointer. For libraries, it should point
  395. * to the name of the library. This will not link a library against itself.
  396. */
  397. void cmLocalUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  398. const char* targetLibrary,
  399. const cmTarget &tgt)
  400. {
  401. // Try to emit each search path once
  402. std::set<std::string> emitted;
  403. // Embed runtime search paths if possible and if required.
  404. bool outputRuntime = true;
  405. std::string runtimeFlag;
  406. std::string runtimeSep;
  407. std::vector<std::string> runtimeDirs;
  408. bool cxx = tgt.HasCxx();
  409. if(!cxx )
  410. {
  411. // if linking a c executable use the C runtime flag as cc
  412. // may not be the same program that creates shared libaries
  413. // and may have different flags
  414. if( tgt.GetType() == cmTarget::EXECUTABLE)
  415. {
  416. if(m_Makefile->GetDefinition("CMAKE_C_SHLIB_RUNTIME_FLAG"))
  417. {
  418. runtimeFlag = m_Makefile->GetDefinition("CMAKE_C_SHLIB_RUNTIME_FLAG");
  419. }
  420. }
  421. else
  422. {
  423. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG"))
  424. {
  425. runtimeFlag = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG");
  426. }
  427. }
  428. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP"))
  429. {
  430. runtimeSep = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP");
  431. }
  432. }
  433. else
  434. {
  435. if(m_Makefile->GetDefinition("CMAKE_CXX_SHLIB_RUNTIME_FLAG"))
  436. {
  437. runtimeFlag = m_Makefile->GetDefinition("CMAKE_CXX_SHLIB_RUNTIME_FLAG");
  438. }
  439. if(m_Makefile->GetDefinition("CMAKE_CXX_SHLIB_RUNTIME_SEP"))
  440. {
  441. runtimeSep = m_Makefile->GetDefinition("CMAKE_CXX_SHLIB_RUNTIME_SEP");
  442. }
  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. // collect all the flags needed for linking libraries
  454. std::string linkLibs;
  455. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  456. for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
  457. libDir != libdirs.end(); ++libDir)
  458. {
  459. std::string libpath = cmSystemTools::ConvertToOutputPath(libDir->c_str());
  460. if(emitted.insert(libpath).second)
  461. {
  462. std::string::size_type pos = libDir->find("-L");
  463. if((pos == std::string::npos || pos > 0)
  464. && libDir->find("${") == std::string::npos)
  465. {
  466. linkLibs += "-L";
  467. if(outputRuntime)
  468. {
  469. runtimeDirs.push_back( libpath );
  470. }
  471. }
  472. linkLibs += libpath;
  473. linkLibs += " ";
  474. }
  475. }
  476. std::string librariesLinked;
  477. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  478. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  479. lib != libs.end(); ++lib)
  480. {
  481. // Don't link the library against itself!
  482. if(targetLibrary && (lib->first == targetLibrary)) continue;
  483. // don't look at debug libraries
  484. if (lib->second == cmTarget::DEBUG) continue;
  485. // skip zero size library entries, this may happen
  486. // if a variable expands to nothing.
  487. if (lib->first.size() == 0) continue;
  488. // if it is a full path break it into -L and -l
  489. cmRegularExpression reg("([ \t]*\\-l)|([ \t]*\\-framework)|(\\${)");
  490. if(lib->first.find('/') != std::string::npos
  491. && !reg.find(lib->first))
  492. {
  493. std::string dir, file;
  494. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  495. dir, file);
  496. std::string libpath = cmSystemTools::ConvertToOutputPath(dir.c_str());
  497. if(emitted.insert(libpath).second)
  498. {
  499. linkLibs += "-L";
  500. linkLibs += libpath;
  501. linkLibs += " ";
  502. if(outputRuntime)
  503. {
  504. runtimeDirs.push_back( libpath );
  505. }
  506. }
  507. cmRegularExpression libname("lib(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
  508. cmRegularExpression libname_noprefix("(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
  509. if(libname.find(file))
  510. {
  511. librariesLinked += "-l";
  512. file = libname.match(1);
  513. librariesLinked += file;
  514. librariesLinked += " ";
  515. }
  516. else if(libname_noprefix.find(file))
  517. {
  518. librariesLinked += "-l";
  519. file = libname_noprefix.match(1);
  520. librariesLinked += file;
  521. librariesLinked += " ";
  522. }
  523. }
  524. // not a full path, so add -l name
  525. else
  526. {
  527. if(!reg.find(lib->first))
  528. {
  529. librariesLinked += "-l";
  530. }
  531. librariesLinked += lib->first;
  532. librariesLinked += " ";
  533. }
  534. }
  535. linkLibs += librariesLinked;
  536. fout << linkLibs;
  537. if(outputRuntime && runtimeDirs.size()>0)
  538. {
  539. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  540. // a "-R a -R b -R c" type link line
  541. fout << runtimeFlag;
  542. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  543. fout << *itr;
  544. ++itr;
  545. for( ; itr != runtimeDirs.end(); ++itr )
  546. {
  547. if(runtimeConcatenate)
  548. {
  549. fout << runtimeSep << *itr;
  550. }
  551. else
  552. {
  553. fout << " " << runtimeFlag << *itr;
  554. }
  555. }
  556. fout << " ";
  557. }
  558. }
  559. std::string cmLocalUnixMakefileGenerator::CreateTargetRules(const cmTarget &target,
  560. const char* targetName)
  561. {
  562. std::string customRuleCode = "";
  563. bool initNext = false;
  564. for (std::vector<cmCustomCommand>::const_iterator cr =
  565. target.GetCustomCommands().begin();
  566. cr != target.GetCustomCommands().end(); ++cr)
  567. {
  568. cmCustomCommand cc(*cr);
  569. cc.ExpandVariables(*m_Makefile);
  570. if (cc.GetSourceName() == targetName)
  571. {
  572. if(initNext)
  573. {
  574. customRuleCode += "\n\t";
  575. }
  576. else
  577. {
  578. initNext = true;
  579. }
  580. std::string command = cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str());
  581. customRuleCode += command + " " + cc.GetArguments();
  582. }
  583. }
  584. return customRuleCode;
  585. }
  586. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  587. const char* name,
  588. const cmTarget &t)
  589. {
  590. std::string target = m_LibraryOutputPath + "lib" + name + "$(SHLIB_SUFFIX)";
  591. std::string depend = "$(";
  592. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  593. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  594. std::string command = "$(RM) lib";
  595. command += name;
  596. command += "$(SHLIB_SUFFIX)";
  597. std::string command2;
  598. if(t.HasCxx())
  599. {
  600. command2 = "$(CMAKE_CXX_LINK_SHARED) $(CMAKE_CXX_SHLIB_LINK_FLAGS) "
  601. "$(CMAKE_CXX_SHLIB_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  602. }
  603. else
  604. {
  605. command2 = "$(CMAKE_C_LINK_SHARED) $(CMAKE_SHLIB_LINK_FLAGS) "
  606. "$(CMAKE_SHLIB_BUILD_FLAGS) -o \\\n";
  607. }
  608. command2 += "\t ";
  609. std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(SHLIB_SUFFIX)";
  610. libName = cmSystemTools::ConvertToOutputPath(libName.c_str());
  611. command2 += libName + " \\\n";
  612. command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  613. cmStringStream linklibs;
  614. this->OutputLinkLibraries(linklibs, name, t);
  615. command2 += linklibs.str();
  616. std::string customCommands = this->CreateTargetRules(t, name);
  617. const char* cc = 0;
  618. if(customCommands.size() > 0)
  619. {
  620. cc = customCommands.c_str();
  621. }
  622. this->OutputMakeRule(fout, "rules for a shared library",
  623. target.c_str(),
  624. depend.c_str(),
  625. command.c_str(),
  626. command2.c_str(),
  627. cc);
  628. }
  629. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  630. const char* name,
  631. const cmTarget &t)
  632. {
  633. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  634. std::string depend = "$(";
  635. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  636. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  637. std::string command = "$(RM) lib" + std::string(name) + "$(MODULE_SUFFIX)";
  638. std::string command2;
  639. if(t.HasCxx())
  640. {
  641. command2 = "$(CMAKE_CXX_LINK_SHARED) $(CMAKE_CXX_MODULE_LINK_FLAGS) "
  642. "$(CMAKE_CXX_MODULE_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  643. }
  644. else
  645. {
  646. command2 = "$(CMAKE_C_LINK_SHARED) $(CMAKE_SHLIB_LINK_FLAGS) "
  647. "$(CMAKE_SHLIB_BUILD_FLAGS) -o \\\n";
  648. }
  649. command2 += "\t ";
  650. std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  651. libName = cmSystemTools::ConvertToOutputPath(libName.c_str());
  652. command2 += libName + " \\\n";
  653. command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  654. cmStringStream linklibs;
  655. this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
  656. command2 += linklibs.str();
  657. std::string customCommands = this->CreateTargetRules(t, name);
  658. const char* cc = 0;
  659. if(customCommands.size() > 0)
  660. {
  661. cc = customCommands.c_str();
  662. }
  663. this->OutputMakeRule(fout, "rules for a shared module library",
  664. target.c_str(),
  665. depend.c_str(),
  666. command.c_str(),
  667. command2.c_str(),
  668. cc);
  669. }
  670. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  671. const char* name,
  672. const cmTarget &t)
  673. {
  674. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + ".a";
  675. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  676. std::string depend = "$(";
  677. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  678. std::string command;
  679. if(t.HasCxx())
  680. {
  681. command = "$(CMAKE_CXX_AR) $(CMAKE_CXX_AR_ARGS) ";
  682. }
  683. else
  684. {
  685. command = "$(CMAKE_AR) $(CMAKE_AR_ARGS) ";
  686. }
  687. command += target;
  688. command += " $(";
  689. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  690. std::string command2 = "$(CMAKE_RANLIB) ";
  691. command2 += target;
  692. std::string comment = "rule to build static library: ";
  693. comment += name;
  694. std::string customCommands = this->CreateTargetRules(t, name);
  695. const char* cc = 0;
  696. if(customCommands.size() > 0)
  697. {
  698. cc = customCommands.c_str();
  699. }
  700. this->OutputMakeRule(fout,
  701. comment.c_str(),
  702. target.c_str(),
  703. depend.c_str(),
  704. command.c_str(),
  705. command2.c_str(),
  706. cc);
  707. }
  708. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  709. const char* name,
  710. const cmTarget &t)
  711. {
  712. std::string target = m_ExecutableOutputPath + name + m_ExecutableExtension;
  713. std::string depend = "$(";
  714. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  715. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  716. std::string command;
  717. if(t.HasCxx())
  718. {
  719. command =
  720. "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_SHLIB_LINK_FLAGS) $(CMAKE_CXX_FLAGS) ";
  721. }
  722. else
  723. {
  724. command =
  725. "$(CMAKE_C_COMPILER) $(CMAKE_C_SHLIB_LINK_FLAGS) $(CMAKE_C_FLAGS) ";
  726. }
  727. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  728. cmStringStream linklibs;
  729. this->OutputLinkLibraries(linklibs, 0, t);
  730. command += linklibs.str();
  731. std::string outputFile = m_ExecutableOutputPath + name;
  732. command += " -o " + cmSystemTools::ConvertToOutputPath(outputFile.c_str());
  733. std::string comment = "rule to build executable: ";
  734. comment += name;
  735. std::string customCommands = this->CreateTargetRules(t, name);
  736. const char* cc = 0;
  737. if(customCommands.size() > 0)
  738. {
  739. cc = customCommands.c_str();
  740. }
  741. this->OutputMakeRule(fout,
  742. comment.c_str(),
  743. target.c_str(),
  744. depend.c_str(),
  745. command.c_str(),
  746. cc);
  747. }
  748. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  749. const char* name,
  750. const cmTarget &t)
  751. {
  752. std::string customCommands = this->CreateTargetRules(t, name);
  753. const char* cc = 0;
  754. if(customCommands.size() > 0)
  755. {
  756. cc = customCommands.c_str();
  757. }
  758. std::string comment = "Rule to build Utility ";
  759. comment += name;
  760. std::string depends;
  761. std::string replaceVars;
  762. const std::vector<cmCustomCommand> &ccs = t.GetCustomCommands();
  763. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  764. i != ccs.end(); ++i)
  765. {
  766. const std::vector<std::string> & dep = i->GetDepends();
  767. for(std::vector<std::string>::const_iterator d = dep.begin();
  768. d != dep.end(); ++d)
  769. {
  770. depends += " \\\n";
  771. replaceVars = *d;
  772. m_Makefile->ExpandVariablesInString(replaceVars);
  773. depends += cmSystemTools::ConvertToOutputPath(replaceVars.c_str());
  774. }
  775. }
  776. this->OutputMakeRule(fout, comment.c_str(), name,
  777. depends.c_str(), cc);
  778. }
  779. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  780. {
  781. // for each target
  782. const cmTargets &tgts = m_Makefile->GetTargets();
  783. for(cmTargets::const_iterator l = tgts.begin();
  784. l != tgts.end(); l++)
  785. {
  786. switch(l->second.GetType())
  787. {
  788. case cmTarget::STATIC_LIBRARY:
  789. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  790. break;
  791. case cmTarget::SHARED_LIBRARY:
  792. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  793. break;
  794. case cmTarget::MODULE_LIBRARY:
  795. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  796. break;
  797. case cmTarget::EXECUTABLE:
  798. case cmTarget::WIN32_EXECUTABLE:
  799. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  800. break;
  801. case cmTarget::UTILITY:
  802. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  803. break;
  804. // This is handled by the OutputCustomRules method
  805. case cmTarget::INSTALL_FILES:
  806. // This is handled by the OutputInstallRules method
  807. case cmTarget::INSTALL_PROGRAMS:
  808. // This is handled by the OutputInstallRules method
  809. break;
  810. }
  811. }
  812. }
  813. // For each target that is an executable or shared library, generate
  814. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  815. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  816. {
  817. // Build a set of libraries that will be linked into any target in
  818. // this directory.
  819. std::set<std::string> used;
  820. // for each target
  821. const cmTargets &tgts = m_Makefile->GetTargets();
  822. for(cmTargets::const_iterator l = tgts.begin();
  823. l != tgts.end(); l++)
  824. {
  825. // Each dependency should only be emitted once per target.
  826. std::set<std::string> emitted;
  827. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  828. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  829. || (l->second.GetType() == cmTarget::EXECUTABLE)
  830. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  831. {
  832. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  833. // A library should not depend on itself!
  834. emitted.insert(l->first);
  835. // Now, look at all link libraries specific to this target.
  836. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  837. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  838. lib != tlibs.end(); ++lib)
  839. {
  840. // Record that this library was used.
  841. used.insert(lib->first);
  842. // Don't emit the same library twice for this target.
  843. if(emitted.insert(lib->first).second)
  844. {
  845. // Output this dependency.
  846. this->OutputLibDepend(fout, lib->first.c_str());
  847. }
  848. }
  849. // Now, look at all utilities specific to this target.
  850. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  851. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  852. util != tutils.end(); ++util)
  853. {
  854. // Record that this utility was used.
  855. used.insert(*util);
  856. // Don't emit the same utility twice for this target.
  857. if(emitted.insert(*util).second)
  858. {
  859. // Output this dependency.
  860. this->OutputExeDepend(fout, util->c_str());
  861. }
  862. }
  863. fout << "\n";
  864. }
  865. }
  866. fout << "\n";
  867. // Loop over the libraries used and make sure there is a rule to
  868. // build them in this makefile. If the library is in another
  869. // directory, add a rule to jump to that directory and make sure it
  870. // exists.
  871. for(std::set<std::string>::const_iterator lib = used.begin();
  872. lib != used.end(); ++lib)
  873. {
  874. // loop over the list of directories that the libraries might
  875. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  876. // be stored in the cache
  877. std::string libPath = *lib + "_CMAKE_PATH";
  878. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  879. // if cache and not the current directory add a rule, to
  880. // jump into the directory and build for the first time
  881. if(cacheValue &&
  882. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  883. {
  884. // add the correct extension
  885. std::string ltname = *lib+"_LIBRARY_TYPE";
  886. const char* libType
  887. = m_Makefile->GetDefinition(ltname.c_str());
  888. // if it was a library..
  889. if (libType)
  890. {
  891. std::string library = m_LibraryPrefix;
  892. library += *lib;
  893. std::string libpath = cacheValue;
  894. if(libType && std::string(libType) == "SHARED")
  895. {
  896. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  897. }
  898. else if(libType && std::string(libType) == "MODULE")
  899. {
  900. library += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  901. }
  902. else if(libType && std::string(libType) == "STATIC")
  903. {
  904. library += m_StaticLibraryExtension;
  905. }
  906. else
  907. {
  908. cmSystemTools::Error("Unknown library type!");
  909. return;
  910. }
  911. if(m_LibraryOutputPath.size())
  912. {
  913. libpath = m_LibraryOutputPath;
  914. }
  915. else
  916. {
  917. libpath += "/";
  918. }
  919. libpath += library;
  920. // put out a rule to build the library if it does not exist
  921. this->OutputBuildTargetInDir(fout,
  922. cacheValue,
  923. library.c_str(),
  924. libpath.c_str(),
  925. m_Makefile->
  926. GetDefinition("LIBRARY_OUTPUT_PATH")
  927. );
  928. }
  929. // something other than a library...
  930. else
  931. {
  932. std::string exepath = cacheValue;
  933. if(m_ExecutableOutputPath.size())
  934. {
  935. exepath = m_ExecutableOutputPath;
  936. }
  937. else
  938. {
  939. exepath += "/";
  940. }
  941. exepath += *lib;
  942. this->OutputBuildTargetInDir(fout,
  943. cacheValue,
  944. lib->c_str(),
  945. exepath.c_str(),
  946. m_Makefile->
  947. GetDefinition("EXECUTABLE_OUTPUT_PATH")
  948. );
  949. }
  950. }
  951. }
  952. }
  953. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  954. const char* path,
  955. const char* library,
  956. const char* fullpath,
  957. const char* outputPath)
  958. {
  959. const char* makeTarget = library;
  960. if(outputPath && strcmp( outputPath, "" ) != 0)
  961. {
  962. makeTarget = fullpath;
  963. }
  964. fout << cmSystemTools::ConvertToOutputPath(fullpath)
  965. << ":\n\tcd " << cmSystemTools::ConvertToOutputPath(path)
  966. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  967. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  968. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  969. << "; $(MAKE) $(MAKESILENT) "
  970. << cmSystemTools::ConvertToOutputPath(makeTarget) << "\n\n";
  971. }
  972. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  973. {
  974. return strcmp(path1, path2) == 0;
  975. }
  976. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  977. const char* name)
  978. {
  979. std::string libPath = name;
  980. libPath += "_CMAKE_PATH";
  981. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  982. if(cacheValue )
  983. {
  984. // if there is a cache value, then this is a library that cmake
  985. // knows how to build, so we can depend on it
  986. std::string libpath;
  987. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  988. {
  989. // if the library is not in the current directory, then get the full
  990. // path to it
  991. if(m_LibraryOutputPath.size())
  992. {
  993. libpath = m_LibraryOutputPath;
  994. libpath += m_LibraryPrefix;
  995. }
  996. else
  997. {
  998. libpath = cacheValue;
  999. libpath += "/";
  1000. libpath += m_LibraryPrefix;
  1001. }
  1002. }
  1003. else
  1004. {
  1005. // library is in current Makefile so use lib as a prefix
  1006. libpath = m_LibraryOutputPath;
  1007. libpath += m_LibraryPrefix;
  1008. }
  1009. // add the library name
  1010. libpath += name;
  1011. // add the correct extension
  1012. std::string ltname = name;
  1013. ltname += "_LIBRARY_TYPE";
  1014. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1015. if(libType && std::string(libType) == "SHARED")
  1016. {
  1017. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1018. }
  1019. else if (libType && std::string(libType) == "MODULE")
  1020. {
  1021. libpath += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1022. }
  1023. else if (libType && std::string(libType) == "STATIC")
  1024. {
  1025. libpath += m_StaticLibraryExtension;
  1026. }
  1027. fout << cmSystemTools::ConvertToOutputPath(libpath.c_str()) << " ";
  1028. }
  1029. }
  1030. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1031. const char* name)
  1032. {
  1033. std::string exePath = name;
  1034. exePath += "_CMAKE_PATH";
  1035. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1036. if(cacheValue )
  1037. {
  1038. // if there is a cache value, then this is a executable/utility that cmake
  1039. // knows how to build, so we can depend on it
  1040. std::string exepath;
  1041. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1042. {
  1043. // if the exe/utility is not in the current directory, then get the full
  1044. // path to it
  1045. if(m_ExecutableOutputPath.size())
  1046. {
  1047. exepath = m_ExecutableOutputPath;
  1048. }
  1049. else
  1050. {
  1051. exepath = cacheValue;
  1052. exepath += "/";
  1053. }
  1054. }
  1055. else
  1056. {
  1057. // library is in current Makefile
  1058. exepath = m_ExecutableOutputPath;
  1059. }
  1060. // add the library name
  1061. exepath += name;
  1062. // add the correct extension
  1063. exepath += m_ExecutableExtension;
  1064. fout << cmSystemTools::ConvertToOutputPath(exepath.c_str()) << " ";
  1065. }
  1066. }
  1067. // fix up names of directories so they can be used
  1068. // as targets in makefiles.
  1069. inline std::string FixDirectoryName(const char* dir)
  1070. {
  1071. std::string s = dir;
  1072. // replace ../ with 3 under bars
  1073. size_t pos = s.find("../");
  1074. if(pos != std::string::npos)
  1075. {
  1076. s.replace(pos, 3, "___");
  1077. }
  1078. // replace / directory separators with a single under bar
  1079. pos = s.find("/");
  1080. while(pos != std::string::npos)
  1081. {
  1082. s.replace(pos, 1, "_");
  1083. pos = s.find("/");
  1084. }
  1085. return s;
  1086. }
  1087. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1088. const char* dir,
  1089. const char* target1,
  1090. const char* target2,
  1091. bool silent)
  1092. {
  1093. std::string directory = cmSystemTools::ConvertToOutputPath(dir);
  1094. if(target1)
  1095. {
  1096. fout << "\t@if test ! -d " << directory
  1097. << "; then $(MAKE) rebuild_cache; fi\n";
  1098. if (!silent)
  1099. {
  1100. fout << "\techo " << directory << ": building " << target1 << "\n";
  1101. }
  1102. fout << "\t@cd " << directory
  1103. << "; $(MAKE) " << target1 << "\n";
  1104. }
  1105. if(target2)
  1106. {
  1107. if (!silent)
  1108. {
  1109. fout << "\techo " << directory << ": building " << target2 << "\n";
  1110. }
  1111. fout << "\t@cd " << directory
  1112. << "; $(MAKE) " << target2 << "\n";
  1113. }
  1114. fout << "\n";
  1115. }
  1116. void
  1117. cmLocalUnixMakefileGenerator::
  1118. OutputSubDirectoryVars(std::ostream& fout,
  1119. const char* var,
  1120. const char* target,
  1121. const char* target1,
  1122. const char* target2,
  1123. const char* depend,
  1124. const std::vector<std::string>& SubDirectories,
  1125. bool silent)
  1126. {
  1127. if(!depend)
  1128. {
  1129. depend = "";
  1130. }
  1131. if( SubDirectories.size() == 0)
  1132. {
  1133. return;
  1134. }
  1135. fout << "# Variable for making " << target << " in subdirectories.\n";
  1136. fout << var << " = \\\n";
  1137. unsigned int i;
  1138. for(i =0; i < SubDirectories.size(); i++)
  1139. {
  1140. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1141. fout << target << "_" << subdir.c_str();
  1142. if(i == SubDirectories.size()-1)
  1143. {
  1144. fout << " \n\n";
  1145. }
  1146. else
  1147. {
  1148. fout << " \\\n";
  1149. }
  1150. }
  1151. fout << "# Targets for making " << target << " in subdirectories.\n";
  1152. std::string last = "";
  1153. for(unsigned int i =0; i < SubDirectories.size(); i++)
  1154. {
  1155. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1156. fout << target << "_" << subdir.c_str() << ": " << depend;
  1157. // Make each subdirectory depend on previous one. This forces
  1158. // parallel builds (make -j 2) to build in same order as a single
  1159. // threaded build to avoid dependency problems.
  1160. if(i > 0)
  1161. {
  1162. fout << " " << target << "_" << last.c_str();
  1163. }
  1164. fout << "\n";
  1165. last = subdir;
  1166. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1167. dir += "/";
  1168. dir += SubDirectories[i];
  1169. this->BuildInSubDirectory(fout, dir.c_str(),
  1170. target1, target2, silent);
  1171. }
  1172. fout << "\n\n";
  1173. }
  1174. // output rules for decending into sub directories
  1175. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1176. {
  1177. // Output Sub directory build rules
  1178. const std::vector<std::string>& SubDirectories
  1179. = m_Makefile->GetSubDirectories();
  1180. if( SubDirectories.size() == 0)
  1181. {
  1182. return;
  1183. }
  1184. this->OutputSubDirectoryVars(fout,
  1185. "SUBDIR_BUILD",
  1186. "default_target",
  1187. "default_target",
  1188. 0, "$(TARGETS)",
  1189. SubDirectories,
  1190. false);
  1191. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1192. "clean",
  1193. 0, 0,
  1194. SubDirectories);
  1195. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1196. "depend",
  1197. 0, 0,
  1198. SubDirectories);
  1199. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1200. "install",
  1201. 0, 0,
  1202. SubDirectories);
  1203. }
  1204. // Output the depend information for all the classes
  1205. // in the makefile. These would have been generated
  1206. // by the class cmMakeDepend GenerateMakefile
  1207. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1208. {
  1209. bool ret = false;
  1210. // Iterate over every target.
  1211. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1212. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1213. target != targets.end(); ++target)
  1214. {
  1215. // Iterate over every source for this target.
  1216. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1217. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1218. source != sources.end(); ++source)
  1219. {
  1220. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1221. {
  1222. if(!(*source)->GetDepends().empty())
  1223. {
  1224. fout << (*source)->GetSourceName() << m_ObjectFileExtension << " :";
  1225. // Iterate through all the dependencies for this source.
  1226. for(std::vector<std::string>::const_iterator dep =
  1227. (*source)->GetDepends().begin();
  1228. dep != (*source)->GetDepends().end(); ++dep)
  1229. {
  1230. fout << " \\\n"
  1231. << cmSystemTools::ConvertToOutputPath(dep->c_str());
  1232. ret = true;
  1233. }
  1234. fout << "\n\n";
  1235. }
  1236. }
  1237. }
  1238. }
  1239. return ret;
  1240. }
  1241. // Output the depend information for all the classes
  1242. // in the makefile. These would have been generated
  1243. // by the class cmMakeDepend GenerateMakefile
  1244. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1245. {
  1246. std::set<std::string> emittedLowerPath;
  1247. std::set<std::string> emitted;
  1248. // Iterate over every target.
  1249. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1250. fout << "# Suppresses display of executed commands\n";
  1251. fout << ".SILENT:\n";
  1252. fout << "# disable some common implicit rules to speed things up\n";
  1253. fout << ".SUFFIXES:\n";
  1254. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1255. this->OutputMakeVariables(fout);
  1256. fout << "default:\n";
  1257. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1258. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1259. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1260. target != targets.end(); ++target)
  1261. {
  1262. // Iterate over every source for this target.
  1263. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1264. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1265. source != sources.end(); ++source)
  1266. {
  1267. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1268. {
  1269. if(!(*source)->GetDepends().empty())
  1270. {
  1271. for(std::vector<std::string>::const_iterator dep =
  1272. (*source)->GetDepends().begin();
  1273. dep != (*source)->GetDepends().end(); ++dep)
  1274. {
  1275. std::string dependfile =
  1276. cmSystemTools::ConvertToOutputPath(cmSystemTools::CollapseFullPath(dep->c_str()).c_str());
  1277. // use the lower path function to create uniqe names
  1278. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1279. if(emittedLowerPath.insert(lowerpath).second)
  1280. {
  1281. emitted.insert(dependfile);
  1282. fout << "all:: " << dependfile << "\n";
  1283. }
  1284. }
  1285. }
  1286. }
  1287. }
  1288. }
  1289. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1290. std::set<std::string>::iterator i;
  1291. for(i = emitted.begin(); i != emitted.end(); ++i)
  1292. {
  1293. fout << "cmake.depends:: " << *i <<
  1294. "\n\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1295. }
  1296. fout << "\n\n";
  1297. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1298. for(std::set<std::string>::iterator i = emitted.begin();
  1299. i != emitted.end(); ++i)
  1300. {
  1301. fout << *i << ":\n"
  1302. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1303. }
  1304. }
  1305. // Output each custom rule in the following format:
  1306. // output: source depends...
  1307. // (tab) command...
  1308. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1309. {
  1310. // We may be modifying the source groups temporarily, so make a copy.
  1311. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1312. const cmTargets &tgts = m_Makefile->GetTargets();
  1313. for(cmTargets::const_iterator tgt = tgts.begin();
  1314. tgt != tgts.end(); ++tgt)
  1315. {
  1316. // add any custom rules to the source groups
  1317. for (std::vector<cmCustomCommand>::const_iterator cr =
  1318. tgt->second.GetCustomCommands().begin();
  1319. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1320. {
  1321. // if the source for the custom command is the same name
  1322. // as the target, then to not create a rule in the makefile for
  1323. // the custom command, as the command will be fired when the other target
  1324. // is built.
  1325. if ( cr->GetSourceName().compare(tgt->first) !=0)
  1326. {
  1327. cmSourceGroup& sourceGroup =
  1328. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1329. sourceGroups);
  1330. cmCustomCommand cc(*cr);
  1331. cc.ExpandVariables(*m_Makefile);
  1332. sourceGroup.AddCustomCommand(cc);
  1333. }
  1334. }
  1335. }
  1336. // Loop through every source group.
  1337. for(std::vector<cmSourceGroup>::const_iterator sg =
  1338. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1339. {
  1340. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1341. if(buildRules.empty())
  1342. { continue; }
  1343. std::string name = sg->GetName();
  1344. if(name != "")
  1345. {
  1346. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1347. }
  1348. // Loop through each source in the source group.
  1349. for(cmSourceGroup::BuildRules::const_iterator cc =
  1350. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1351. {
  1352. std::string source = cc->first;
  1353. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  1354. // Loop through every command generating code from the current source.
  1355. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1356. c != commands.end(); ++c)
  1357. {
  1358. // escape spaces and convert to native slashes path for
  1359. // the command
  1360. std::string command =
  1361. cmSystemTools::ConvertToOutputPath(c->second.m_Command.c_str());
  1362. command += " ";
  1363. // now add the arguments
  1364. command += c->second.m_Arguments;
  1365. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1366. // if the command has no outputs, then it is a utility command
  1367. // with no outputs
  1368. if(commandFiles.m_Outputs.size() == 0)
  1369. {
  1370. std::string depends;
  1371. // collect out all the dependencies for this rule.
  1372. for(std::set<std::string>::const_iterator d =
  1373. commandFiles.m_Depends.begin();
  1374. d != commandFiles.m_Depends.end(); ++d)
  1375. {
  1376. std::string dep = cmSystemTools::ConvertToOutputPath(d->c_str());
  1377. depends += " ";
  1378. depends += dep;
  1379. }
  1380. // output rule
  1381. this->OutputMakeRule(fout,
  1382. "Custom command",
  1383. source.c_str(),
  1384. depends.c_str(),
  1385. command.c_str());
  1386. }
  1387. // Write a rule for every output generated by this command.
  1388. for(std::set<std::string>::const_iterator output =
  1389. commandFiles.m_Outputs.begin();
  1390. output != commandFiles.m_Outputs.end(); ++output)
  1391. {
  1392. std::string src = cmSystemTools::ConvertToOutputPath(source.c_str());
  1393. std::string depends;
  1394. depends += src;
  1395. // Collect out all the dependencies for this rule.
  1396. for(std::set<std::string>::const_iterator d =
  1397. commandFiles.m_Depends.begin();
  1398. d != commandFiles.m_Depends.end(); ++d)
  1399. {
  1400. std::string dep = cmSystemTools::ConvertToOutputPath(d->c_str());
  1401. depends += " ";
  1402. depends += dep;
  1403. }
  1404. // output rule
  1405. this->OutputMakeRule(fout,
  1406. "Custom command",
  1407. output->c_str(),
  1408. depends.c_str(),
  1409. command.c_str());
  1410. }
  1411. }
  1412. }
  1413. if(name != "")
  1414. {
  1415. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1416. }
  1417. }
  1418. }
  1419. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1420. {
  1421. const char* variables =
  1422. "# the standard shell for make\n"
  1423. "SHELL = /bin/sh\n"
  1424. "\n"
  1425. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  1426. "CMAKE_AR = @CMAKE_AR@\n"
  1427. "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
  1428. "CMAKE_CXX_AR = @CMAKE_CXX_AR@\n"
  1429. "CMAKE_CXX_AR_ARGS = @CMAKE_CXX_AR_ARGS@\n"
  1430. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@\n"
  1431. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  1432. "CMAKE_C_LINK_SHARED = @CMAKE_C_LINK_SHARED@\n"
  1433. "CMAKE_CXX_LINK_SHARED = @CMAKE_CXX_LINK_SHARED@\n"
  1434. "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
  1435. "CMAKE_CXX_SHLIB_CFLAGS = @CMAKE_CXX_SHLIB_CFLAGS@\n"
  1436. "CMAKE_CXX_SHLIB_BUILD_FLAGS = @CMAKE_CXX_SHLIB_BUILD_FLAGS@\n"
  1437. "CMAKE_CXX_SHLIB_LINK_FLAGS = @CMAKE_CXX_SHLIB_LINK_FLAGS@\n"
  1438. "CMAKE_CXX_MODULE_BUILD_FLAGS = @CMAKE_CXX_MODULE_BUILD_FLAGS@\n"
  1439. "CMAKE_CXX_MODULE_LINK_FLAGS = @CMAKE_CXX_MODULE_LINK_FLAGS@\n"
  1440. "CMAKE_CXX_SHLIB_RUNTIME_FLAG = @CMAKE_CXX_SHLIB_RUNTIME_FLAG@\n"
  1441. "CMAKE_CXX_SHLIB_RUNTIME_SEP = @CMAKE_CXX_SHLIB_RUNTIME_SEP@\n"
  1442. "\n"
  1443. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  1444. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@\n"
  1445. "\n"
  1446. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  1447. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  1448. "CMAKE_C_SHLIB_LINK_FLAGS = @CMAKE_C_SHLIB_LINK_FLAGS@\n"
  1449. "CMAKE_MODULE_BUILD_FLAGS = @CMAKE_MODULE_BUILD_FLAGS@\n"
  1450. "CMAKE_MODULE_LINK_FLAGS = @CMAKE_MODULE_LINK_FLAGS@\n"
  1451. "CMAKE_C_SHLIB_RUNTIME_FLAG = @CMAKE_C_SHLIB_RUNTIME_FLAG@\n"
  1452. "CMAKE_SHLIB_RUNTIME_FLAG = @CMAKE_SHLIB_RUNTIME_FLAG@\n"
  1453. "CMAKE_SHLIB_RUNTIME_SEP = @CMAKE_SHLIB_RUNTIME_SEP@\n"
  1454. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  1455. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  1456. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  1457. "MODULE_SUFFIX = @CMAKE_MODULE_SUFFIX@\n"
  1458. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  1459. "RM = rm -f\n"
  1460. "\n";
  1461. std::string replaceVars = variables;
  1462. m_Makefile->ExpandVariablesInString(replaceVars);
  1463. fout << replaceVars.c_str();
  1464. fout << "CMAKE_COMMAND = "
  1465. << cmSystemTools::ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_COMMAND"))
  1466. << "\n";
  1467. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1468. {
  1469. fout << "CMAKE_EDIT_COMMAND = "
  1470. << cmSystemTools::ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1471. << "\n";
  1472. }
  1473. fout << "CMAKE_CURRENT_SOURCE = " <<
  1474. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory()) << "\n";
  1475. fout << "CMAKE_CURRENT_BINARY = " <<
  1476. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory()) << "\n";
  1477. fout << "CMAKE_SOURCE_DIR = " <<
  1478. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory()) << "\n";
  1479. fout << "CMAKE_BINARY_DIR = " <<
  1480. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()) << "\n";
  1481. // Output Include paths
  1482. fout << "INCLUDE_FLAGS = ";
  1483. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1484. std::vector<std::string>::iterator i;
  1485. fout << "-I" <<
  1486. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  1487. for(i = includes.begin(); i != includes.end(); ++i)
  1488. {
  1489. std::string include = *i;
  1490. // Don't output a -I for the standard include path "/usr/include".
  1491. // This can cause problems with certain standard library
  1492. // implementations because the wrong headers may be found first.
  1493. if(include != "/usr/include")
  1494. {
  1495. fout << "-I" << cmSystemTools::ConvertToOutputPath(i->c_str()) << " ";
  1496. }
  1497. }
  1498. fout << m_Makefile->GetDefineFlags();
  1499. fout << "\n\n";
  1500. }
  1501. void cmLocalUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1502. {
  1503. const char* root
  1504. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1505. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1506. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1507. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1508. const cmTargets &tgts = m_Makefile->GetTargets();
  1509. fout << "install: $(SUBDIR_INSTALL)\n";
  1510. fout << "\t@echo \"Installing ...\"\n";
  1511. const char* prefix
  1512. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1513. if (!prefix)
  1514. {
  1515. prefix = "/usr/local";
  1516. }
  1517. for(cmTargets::const_iterator l = tgts.begin();
  1518. l != tgts.end(); l++)
  1519. {
  1520. if (l->second.GetInstallPath() != "")
  1521. {
  1522. // first make the directories for each target
  1523. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1524. " ] ; then \\\n";
  1525. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1526. << l->second.GetInstallPath() << " \"; \\\n";
  1527. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1528. << "; \\\n";
  1529. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1530. << "; \\\n";
  1531. fout << "\t else true; \\\n";
  1532. fout << "\t fi\n";
  1533. // now install the target
  1534. switch (l->second.GetType())
  1535. {
  1536. case cmTarget::STATIC_LIBRARY:
  1537. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1538. << l->first;
  1539. fout << ".a";
  1540. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1541. break;
  1542. case cmTarget::SHARED_LIBRARY:
  1543. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1544. << l->first;
  1545. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1546. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1547. break;
  1548. case cmTarget::MODULE_LIBRARY:
  1549. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1550. << l->first;
  1551. fout << m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1552. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1553. break;
  1554. case cmTarget::WIN32_EXECUTABLE:
  1555. case cmTarget::EXECUTABLE:
  1556. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1557. << l->first
  1558. << cmSystemTools::GetExecutableExtension()
  1559. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1560. break;
  1561. case cmTarget::INSTALL_FILES:
  1562. {
  1563. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1564. std::vector<std::string>::const_iterator i;
  1565. for (i = sf.begin(); i != sf.end(); ++i)
  1566. {
  1567. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1568. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1569. // avoid using install-sh to install install-sh
  1570. // does not work on windows....
  1571. if(*i == "install-sh")
  1572. {
  1573. fout << "\t cp ";
  1574. }
  1575. else
  1576. {
  1577. fout << "\t $(INSTALL_DATA) ";
  1578. }
  1579. fout << *i
  1580. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1581. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1582. // avoid using install-sh to install install-sh
  1583. // does not work on windows....
  1584. if(*i == "install-sh")
  1585. {
  1586. fout << "\t cp ";
  1587. }
  1588. else
  1589. {
  1590. fout << "\t $(INSTALL_DATA) ";
  1591. }
  1592. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1593. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1594. fout << "\telse \\\n";
  1595. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1596. << " \"; \\\n";
  1597. fout << "\t fi\n";
  1598. }
  1599. }
  1600. break;
  1601. case cmTarget::INSTALL_PROGRAMS:
  1602. {
  1603. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1604. std::vector<std::string>::const_iterator i;
  1605. for (i = sf.begin(); i != sf.end(); ++i)
  1606. {
  1607. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1608. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1609. // avoid using install-sh to install install-sh
  1610. // does not work on windows....
  1611. if(*i == "install-sh")
  1612. {
  1613. fout << "\t cp ";
  1614. }
  1615. else
  1616. {
  1617. fout << "\t $(INSTALL_PROGRAM) ";
  1618. }
  1619. fout << *i
  1620. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1621. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1622. // avoid using install-sh to install install-sh
  1623. // does not work on windows....
  1624. if(*i == "install-sh")
  1625. {
  1626. fout << "\t cp ";
  1627. }
  1628. else
  1629. {
  1630. fout << "\t $(INSTALL_PROGRAM) ";
  1631. }
  1632. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1633. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1634. fout << "\telse \\\n";
  1635. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1636. << " \"; \\\n";
  1637. fout << "\t fi\n";
  1638. }
  1639. }
  1640. break;
  1641. case cmTarget::UTILITY:
  1642. default:
  1643. break;
  1644. }
  1645. }
  1646. }
  1647. }
  1648. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1649. {
  1650. this->OutputMakeRule(fout,
  1651. "Default build rule",
  1652. "all",
  1653. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1654. 0);
  1655. this->OutputMakeRule(fout,
  1656. "remove generated files",
  1657. "clean",
  1658. "$(SUBDIR_CLEAN)",
  1659. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  1660. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  1661. // collect up all the sources
  1662. std::string allsources;
  1663. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1664. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1665. target != targets.end(); ++target)
  1666. {
  1667. // Iterate over every source for this target.
  1668. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1669. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1670. source != sources.end(); ++source)
  1671. {
  1672. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1673. {
  1674. allsources += " \\\n";
  1675. allsources += cmSystemTools::ConvertToOutputPath((*source)->GetFullPath().c_str());
  1676. }
  1677. }
  1678. }
  1679. this->OutputMakeRule(fout,
  1680. "Rule to build the cmake.depends and Makefile as side effect, if a source cmakelist file is out of date.",
  1681. "cmake.depends",
  1682. "$(CMAKE_MAKEFILE_SOURCES)",
  1683. "$(CMAKE_COMMAND) "
  1684. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1685. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1686. );
  1687. this->OutputMakeRule(fout,
  1688. "Rule to build the cmake.check_depends and Makefile as side effect, if any source file has changed.",
  1689. "cmake.check_depends",
  1690. allsources.c_str(),
  1691. "$(CMAKE_COMMAND) "
  1692. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1693. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1694. );
  1695. this->OutputMakeRule(fout,
  1696. "Rule to force the build of cmake.depends",
  1697. "depend",
  1698. "$(SUBDIR_DEPEND)",
  1699. "$(CMAKE_COMMAND) "
  1700. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1701. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1702. this->OutputMakeRule(fout,
  1703. "Rule to force the build of cmake.depends "
  1704. "in the current directory only.",
  1705. "dependlocal",
  1706. 0,
  1707. "$(CMAKE_COMMAND) "
  1708. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1709. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1710. this->OutputMakeRule(fout,
  1711. "Rebuild CMakeCache.txt file",
  1712. "rebuild_cache",
  1713. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1714. "$(CMAKE_COMMAND) "
  1715. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1716. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  1717. // called edit_cache
  1718. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1719. {
  1720. this->OutputMakeRule(fout,
  1721. "Edit the CMakeCache.txt file with ccmake or CMakeSetup",
  1722. "edit_cache",
  1723. 0,
  1724. "$(CMAKE_EDIT_COMMAND) "
  1725. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1726. }
  1727. this->OutputMakeRule(fout,
  1728. "Create CMakeCache.txt file",
  1729. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1730. 0,
  1731. "$(CMAKE_COMMAND) "
  1732. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1733. this->OutputMakeRule(fout,
  1734. "Rule to keep make from removing Makefiles "
  1735. "if control-C is hit during a run of cmake.",
  1736. ".PRECIOUS",
  1737. "Makefile cmake.depends",
  1738. 0);
  1739. this->OutputSourceObjectBuildRules(fout);
  1740. // find ctest
  1741. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1742. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1743. ctest += "/";
  1744. ctest += "ctest";
  1745. ctest += cmSystemTools::GetExecutableExtension();
  1746. if(!cmSystemTools::FileExists(ctest.c_str()))
  1747. {
  1748. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1749. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1750. ctest += "/Debug/";
  1751. ctest += "ctest";
  1752. ctest += cmSystemTools::GetExecutableExtension();
  1753. }
  1754. if(!cmSystemTools::FileExists(ctest.c_str()))
  1755. {
  1756. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1757. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1758. ctest += "/Release/";
  1759. ctest += "ctest";
  1760. ctest += cmSystemTools::GetExecutableExtension();
  1761. }
  1762. if (cmSystemTools::FileExists(ctest.c_str()))
  1763. {
  1764. this->OutputMakeRule(fout,
  1765. "run any tests",
  1766. "test",
  1767. "",
  1768. cmSystemTools::ConvertToOutputPath(ctest.c_str()).c_str());
  1769. }
  1770. }
  1771. void
  1772. cmLocalUnixMakefileGenerator::
  1773. OutputBuildObjectFromSource(std::ostream& fout,
  1774. const char* shortName,
  1775. const cmSourceFile& source,
  1776. const char* extraCompileFlags,
  1777. bool shared)
  1778. {
  1779. // Header files shouldn't have build rules.
  1780. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  1781. {
  1782. return;
  1783. }
  1784. std::string comment = "Build ";
  1785. std::string objectFile = std::string(shortName) + m_ObjectFileExtension;
  1786. objectFile = cmSystemTools::ConvertToOutputPath(objectFile.c_str());
  1787. comment += objectFile + " From ";
  1788. comment += source.GetFullPath();
  1789. std::string compileCommand;
  1790. std::string ext = source.GetSourceExtension();
  1791. if(ext == "c" )
  1792. {
  1793. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  1794. compileCommand += extraCompileFlags;
  1795. if(shared)
  1796. {
  1797. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1798. }
  1799. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1800. compileCommand +=
  1801. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  1802. compileCommand += " -o ";
  1803. compileCommand += objectFile;
  1804. }
  1805. else
  1806. {
  1807. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  1808. compileCommand += extraCompileFlags;
  1809. if(shared)
  1810. {
  1811. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1812. }
  1813. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1814. compileCommand +=
  1815. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  1816. compileCommand += " -o ";
  1817. compileCommand += objectFile;
  1818. }
  1819. this->OutputMakeRule(fout,
  1820. comment.c_str(),
  1821. objectFile.c_str(),
  1822. cmSystemTools::ConvertToOutputPath(source.GetFullPath().
  1823. c_str()).c_str(),
  1824. compileCommand.c_str());
  1825. }
  1826. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  1827. {
  1828. fout << "# Rules to build " << m_ObjectFileExtension
  1829. << " files from their sources:\n";
  1830. std::set<std::string> rules;
  1831. // Iterate over every target.
  1832. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1833. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1834. target != targets.end(); ++target)
  1835. {
  1836. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  1837. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  1838. std::string exportsDef = "";
  1839. if(shared)
  1840. {
  1841. exportsDef = "-D"+target->first+"_EXPORTS ";
  1842. }
  1843. // Iterate over every source for this target.
  1844. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1845. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1846. source != sources.end(); ++source)
  1847. {
  1848. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1849. {
  1850. std::string shortName;
  1851. std::string sourceName;
  1852. // If the full path to the source file includes this
  1853. // directory, we want to use the relative path for the
  1854. // filename of the object file. Otherwise, we will use just
  1855. // the filename portion.
  1856. if((cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  1857. || (cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->
  1858. GetCurrentOutputDirectory()) == 0))
  1859. {
  1860. sourceName = (*source)->GetSourceName()+"."+(*source)->GetSourceExtension();
  1861. shortName = (*source)->GetSourceName();
  1862. // The path may be relative. See if a directory needs to be
  1863. // created for the output file. This is a ugly, and perhaps
  1864. // should be moved elsewhere.
  1865. std::string relPath =
  1866. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  1867. if(relPath != "")
  1868. {
  1869. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  1870. outPath += "/"+relPath;
  1871. cmSystemTools::MakeDirectory(outPath.c_str());
  1872. }
  1873. }
  1874. else
  1875. {
  1876. sourceName = (*source)->GetFullPath();
  1877. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  1878. }
  1879. std::string shortNameWithExt = shortName +
  1880. (*source)->GetSourceExtension();
  1881. // Only output a rule for each .o once.
  1882. if(rules.find(shortNameWithExt) == rules.end())
  1883. {
  1884. if((*source)->GetProperty("COMPILE_FLAGS"))
  1885. {
  1886. exportsDef += (*source)->GetProperty("COMPILE_FLAGS");
  1887. exportsDef += " ";
  1888. }
  1889. this->OutputBuildObjectFromSource(fout,
  1890. shortName.c_str(),
  1891. *(*source),
  1892. exportsDef.c_str(),
  1893. shared);
  1894. rules.insert(shortNameWithExt);
  1895. }
  1896. }
  1897. }
  1898. }
  1899. }
  1900. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  1901. const char* comment,
  1902. const char* target,
  1903. const char* depends,
  1904. const char* command,
  1905. const char* command2,
  1906. const char* command3,
  1907. const char* command4)
  1908. {
  1909. if(!target)
  1910. {
  1911. cmSystemTools::Error("no target for OutputMakeRule");
  1912. return;
  1913. }
  1914. std::string replace;
  1915. if(comment)
  1916. {
  1917. replace = comment;
  1918. m_Makefile->ExpandVariablesInString(replace);
  1919. fout << "#---------------------------------------------------------\n";
  1920. fout << "# " << replace;
  1921. fout << "\n#\n";
  1922. }
  1923. fout << "\n";
  1924. replace = target;
  1925. m_Makefile->ExpandVariablesInString(replace);
  1926. fout << cmSystemTools::ConvertToOutputPath(replace.c_str()) << ": ";
  1927. if(depends)
  1928. {
  1929. replace = depends;
  1930. m_Makefile->ExpandVariablesInString(replace);
  1931. fout << replace.c_str();
  1932. }
  1933. fout << "\n";
  1934. const char* commands[] = { command, command2, command3, command4 };
  1935. for (unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i)
  1936. {
  1937. if(commands[i])
  1938. {
  1939. replace = commands[i];
  1940. m_Makefile->ExpandVariablesInString(replace);
  1941. if(replace[0] != '-' && replace.find("echo") != 0
  1942. && replace.find("$(MAKE)") != 0)
  1943. {
  1944. std::string echostring = replace;
  1945. // for unix we want to quote the output of echo
  1946. // for nmake and borland, the echo should not be quoted
  1947. if(strcmp(m_GlobalGenerator->GetName(), "Unix Makefiles") == 0)
  1948. {
  1949. cmSystemTools::ReplaceString(echostring, "\\\n", " ");
  1950. cmSystemTools::ReplaceString(echostring, " \t", " ");
  1951. cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\techo \"");
  1952. fout << "\techo \"" << echostring.c_str() << "\"\n";
  1953. }
  1954. else
  1955. {
  1956. cmSystemTools::ReplaceString(echostring, "\n\t", "\n\techo ");
  1957. fout << "\techo " << echostring.c_str() << "\n";
  1958. }
  1959. }
  1960. fout << "\t" << replace.c_str() << "\n";
  1961. }
  1962. }
  1963. fout << "\n";
  1964. }