cmUnixMakefileGenerator.cxx 69 KB

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