cmUnixMakefileGenerator.cxx 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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->ConvertToNativePath(cmSystemTools::EscapeSpaces(i->c_str()).c_str());
  204. }
  205. // Add the cache to the list
  206. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  207. cacheFile += "/CMakeCache.txt";
  208. fout << " " <<
  209. this->ConvertToNativePath(cmSystemTools::EscapeSpaces(cacheFile.c_str()).c_str());
  210. fout << "\n\n\n";
  211. this->OutputMakeVariables(fout);
  212. // Set up the default target as the VERY first target, so that make with no arguments will run it
  213. this->
  214. OutputMakeRule(fout,
  215. "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",
  216. "default_target",
  217. 0,
  218. "$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends",
  219. "$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends",
  220. "$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends",
  221. "$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) all");
  222. this->OutputTargetRules(fout);
  223. this->OutputDependLibs(fout);
  224. this->OutputTargets(fout);
  225. this->OutputSubDirectoryRules(fout);
  226. std::string dependName = m_Makefile->GetStartOutputDirectory();
  227. dependName += "/cmake.depends";
  228. if(!this->m_CacheOnly)
  229. {
  230. std::ofstream dependout(dependName.c_str());
  231. if(!dependout)
  232. {
  233. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  234. return;
  235. }
  236. dependout << "# .o dependencies in this directory." << std::endl;
  237. std::string checkDepend = m_Makefile->GetStartOutputDirectory();
  238. checkDepend += "/cmake.check_depends";
  239. std::ofstream checkdependout(checkDepend.c_str());
  240. if(!checkdependout)
  241. {
  242. cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
  243. return;
  244. }
  245. 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;
  246. // if there were any depends output, then output the check depends
  247. // information inot checkdependout
  248. if(this->OutputObjectDepends(dependout))
  249. {
  250. this->OutputCheckDepends(checkdependout);
  251. }
  252. else
  253. {
  254. checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
  255. }
  256. }
  257. this->OutputCustomRules(fout);
  258. this->OutputMakeRules(fout);
  259. this->OutputInstallRules(fout);
  260. // only add the depend include if the depend file exists
  261. if(cmSystemTools::FileExists(dependName.c_str()))
  262. {
  263. this->OutputIncludeMakefile(fout, "cmake.depends");
  264. }
  265. }
  266. void cmUnixMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  267. const char* file)
  268. {
  269. fout << "include " << file << "\n";
  270. }
  271. std::string
  272. cmUnixMakefileGenerator::GetOutputExtension(const char*)
  273. {
  274. return m_ObjectFileExtension;
  275. }
  276. // Output the rules for any targets
  277. void cmUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  278. {
  279. // for each target add to the list of targets
  280. fout << "TARGETS = ";
  281. const cmTargets &tgts = m_Makefile->GetTargets();
  282. // list libraries first
  283. for(cmTargets::const_iterator l = tgts.begin();
  284. l != tgts.end(); l++)
  285. {
  286. if (l->second.IsInAll())
  287. {
  288. std::string path = m_LibraryOutputPath + m_LibraryPrefix;
  289. if(l->second.GetType() == cmTarget::STATIC_LIBRARY)
  290. {
  291. path = path + l->first + m_StaticLibraryExtension;
  292. fout << " \\\n"
  293. << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(path.c_str()).c_str());
  294. }
  295. else if(l->second.GetType() == cmTarget::SHARED_LIBRARY)
  296. {
  297. path = path + l->first +
  298. m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  299. fout << " \\\n"
  300. << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(path.c_str()).c_str());
  301. }
  302. else if(l->second.GetType() == cmTarget::MODULE_LIBRARY)
  303. {
  304. path = path + l->first +
  305. m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  306. fout << " \\\n"
  307. << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(path.c_str()).c_str());
  308. }
  309. }
  310. }
  311. // executables
  312. for(cmTargets::const_iterator l = tgts.begin();
  313. l != tgts.end(); l++)
  314. {
  315. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  316. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  317. l->second.IsInAll())
  318. {
  319. std::string path = m_ExecutableOutputPath + l->first +
  320. m_ExecutableExtension;
  321. fout << " \\\n" << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(path.c_str()).c_str());
  322. }
  323. }
  324. // list utilities last
  325. for(cmTargets::const_iterator l = tgts.begin();
  326. l != tgts.end(); l++)
  327. {
  328. if (l->second.GetType() == cmTarget::UTILITY &&
  329. l->second.IsInAll())
  330. {
  331. fout << " \\\n" << l->first.c_str();
  332. }
  333. }
  334. fout << "\n\n";
  335. // get the classes from the source lists then add them to the groups
  336. for(cmTargets::const_iterator l = tgts.begin();
  337. l != tgts.end(); l++)
  338. {
  339. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  340. if (classes.begin() != classes.end())
  341. {
  342. fout << l->first << "_SRC_OBJS = ";
  343. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  344. i != classes.end(); i++)
  345. {
  346. if(!i->IsAHeaderFileOnly())
  347. {
  348. std::string outExt(this->GetOutputExtension(i->GetSourceExtension().c_str()));
  349. if(outExt.size())
  350. {
  351. fout << "\\\n" << this->ConvertToNativePath(i->GetSourceName().c_str())
  352. << outExt.c_str() << " ";
  353. }
  354. }
  355. }
  356. fout << "\n\n";
  357. fout << l->first << "_SRC_OBJS_QUOTED = ";
  358. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  359. i != classes.end(); i++)
  360. {
  361. if(!i->IsAHeaderFileOnly())
  362. {
  363. std::string outExt(this->GetOutputExtension(i->GetSourceExtension().c_str()));
  364. if(outExt.size())
  365. {
  366. fout << "\\\n\"" << this->ConvertToNativePath(i->GetSourceName().c_str())
  367. << outExt.c_str() << "\" ";
  368. }
  369. }
  370. }
  371. fout << "\n\n";
  372. }
  373. }
  374. fout << "CLEAN_OBJECT_FILES = ";
  375. for(cmTargets::const_iterator l = tgts.begin();
  376. l != tgts.end(); l++)
  377. {
  378. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  379. if (classes.begin() != classes.end())
  380. {
  381. fout << "$(" << l->first << "_SRC_OBJS) ";
  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 = cmSystemTools::EscapeSpaces(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 = cmSystemTools::EscapeSpaces(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 = cmSystemTools::EscapeSpaces(cc.GetCommand().c_str());
  552. command = this->ConvertToNativePath(command.c_str());
  553. customRuleCode += command + " " + cc.GetArguments();
  554. }
  555. }
  556. return customRuleCode;
  557. }
  558. void cmUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  559. const char* name,
  560. const cmTarget &t)
  561. {
  562. std::string target = m_LibraryOutputPath + "lib" + name + "$(SHLIB_SUFFIX)";
  563. std::string depend = "$(";
  564. depend += name;
  565. depend += "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  566. std::string command = "$(RM) lib";
  567. command += name;
  568. command += "$(SHLIB_SUFFIX)";
  569. std::string command2 = "$(CMAKE_CXX_COMPILER) $(CMAKE_SHLIB_LINK_FLAGS) "
  570. "$(CMAKE_SHLIB_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  571. command2 += "\t ";
  572. command2 += m_LibraryOutputPath + "lib" + std::string(name) + "$(SHLIB_SUFFIX) \\\n";
  573. command2 += "\t $(" + std::string(name) + "_SRC_OBJS) ";
  574. std::strstream linklibs;
  575. this->OutputLinkLibraries(linklibs, name, t);
  576. linklibs << std::ends;
  577. command2 += linklibs.str();
  578. delete [] linklibs.str();
  579. std::string customCommands = this->CreateTargetRules(t, name);
  580. const char* cc = 0;
  581. if(customCommands.size() > 0)
  582. {
  583. cc = customCommands.c_str();
  584. }
  585. this->OutputMakeRule(fout, "rules for a shared library",
  586. target.c_str(),
  587. depend.c_str(),
  588. command.c_str(),
  589. command2.c_str(),
  590. cc);
  591. }
  592. void cmUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  593. const char* name,
  594. const cmTarget &t)
  595. {
  596. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  597. std::string depend = "$(";
  598. depend += std::string(name) + "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  599. std::string command = "$(RM) lib" + std::string(name) + "$(MODULE_SUFFIX)";
  600. std::string command2 = "$(CMAKE_CXX_COMPILER) $(CMAKE_MODULE_LINK_FLAGS) "
  601. "$(CMAKE_MODULE_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  602. command2 += "\t ";
  603. command2 += m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX) \\\n";
  604. command2 += "\t $(" + std::string(name) + "_SRC_OBJS) ";
  605. std::strstream linklibs;
  606. this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
  607. linklibs << std::ends;
  608. command2 += linklibs.str();
  609. delete [] linklibs.str();
  610. std::string customCommands = this->CreateTargetRules(t, name);
  611. const char* cc = 0;
  612. if(customCommands.size() > 0)
  613. {
  614. cc = customCommands.c_str();
  615. }
  616. this->OutputMakeRule(fout, "rules for a shared module library",
  617. target.c_str(),
  618. depend.c_str(),
  619. command.c_str(),
  620. command2.c_str(),
  621. cc);
  622. }
  623. void cmUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  624. const char* name,
  625. const cmTarget &t)
  626. {
  627. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + ".a";
  628. std::string depend = "$(";
  629. depend += std::string(name) + "_SRC_OBJS)";
  630. std::string command = "$(CMAKE_AR) $(CMAKE_AR_ARGS) ";
  631. command += m_LibraryOutputPath;
  632. command += "lib";
  633. command += name;
  634. command += ".a $(";
  635. command += std::string(name) + "_SRC_OBJS)";
  636. std::string command2 = "$(CMAKE_RANLIB) ";
  637. command2 += m_LibraryOutputPath;
  638. command2 += "lib";
  639. command2 += std::string(name) + ".a";
  640. std::string comment = "rule to build static library: ";
  641. comment += name;
  642. std::string customCommands = this->CreateTargetRules(t, name);
  643. const char* cc = 0;
  644. if(customCommands.size() > 0)
  645. {
  646. cc = customCommands.c_str();
  647. }
  648. this->OutputMakeRule(fout,
  649. comment.c_str(),
  650. target.c_str(),
  651. depend.c_str(),
  652. command.c_str(),
  653. command2.c_str(),
  654. cc);
  655. }
  656. void cmUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  657. const char* name,
  658. const cmTarget &t)
  659. {
  660. std::string target = m_ExecutableOutputPath + name;
  661. std::string depend = "$(";
  662. depend += std::string(name) + "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  663. std::string command =
  664. "$(CMAKE_CXX_COMPILER) $(CMAKE_SHLIB_LINK_FLAGS) $(CMAKE_CXX_FLAGS) ";
  665. command += "$(" + std::string(name) + "_SRC_OBJS) ";
  666. std::strstream linklibs;
  667. this->OutputLinkLibraries(linklibs, 0, t);
  668. linklibs << std::ends;
  669. command += linklibs.str();
  670. command += " -o " + m_ExecutableOutputPath + name;
  671. std::string comment = "rule to build executable: ";
  672. comment += name;
  673. std::string customCommands = this->CreateTargetRules(t, name);
  674. const char* cc = 0;
  675. if(customCommands.size() > 0)
  676. {
  677. cc = customCommands.c_str();
  678. }
  679. this->OutputMakeRule(fout,
  680. comment.c_str(),
  681. target.c_str(),
  682. depend.c_str(),
  683. command.c_str(),
  684. cc);
  685. }
  686. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  687. {
  688. // for each target
  689. const cmTargets &tgts = m_Makefile->GetTargets();
  690. for(cmTargets::const_iterator l = tgts.begin();
  691. l != tgts.end(); l++)
  692. {
  693. switch(l->second.GetType())
  694. {
  695. case cmTarget::STATIC_LIBRARY:
  696. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  697. break;
  698. case cmTarget::SHARED_LIBRARY:
  699. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  700. break;
  701. case cmTarget::MODULE_LIBRARY:
  702. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  703. break;
  704. case cmTarget::EXECUTABLE:
  705. case cmTarget::WIN32_EXECUTABLE:
  706. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  707. break;
  708. case cmTarget::UTILITY:
  709. // This is handled by the OutputCustomRules method
  710. case cmTarget::INSTALL_FILES:
  711. // This is handled by the OutputInstallRules method
  712. case cmTarget::INSTALL_PROGRAMS:
  713. // This is handled by the OutputInstallRules method
  714. break;
  715. }
  716. }
  717. }
  718. // For each target that is an executable or shared library, generate
  719. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  720. void cmUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  721. {
  722. // Build a set of libraries that will be linked into any target in
  723. // this directory.
  724. std::set<std::string> used;
  725. // for each target
  726. const cmTargets &tgts = m_Makefile->GetTargets();
  727. for(cmTargets::const_iterator l = tgts.begin();
  728. l != tgts.end(); l++)
  729. {
  730. // Each dependency should only be emitted once per target.
  731. std::set<std::string> emitted;
  732. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  733. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  734. || (l->second.GetType() == cmTarget::EXECUTABLE)
  735. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  736. {
  737. fout << l->first << "_DEPEND_LIBS = ";
  738. // A library should not depend on itself!
  739. emitted.insert(l->first);
  740. // First look at all makefile level link libraries.
  741. const cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  742. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  743. lib != libs.end(); ++lib)
  744. {
  745. // Record that this library was used.
  746. used.insert(lib->first);
  747. // Don't emit the same library twice for this target.
  748. if(emitted.insert(lib->first).second)
  749. {
  750. // Output this dependency.
  751. this->OutputLibDepend(fout, lib->first.c_str());
  752. }
  753. }
  754. // Now, look at all link libraries specific to this target.
  755. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  756. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  757. lib != tlibs.end(); ++lib)
  758. {
  759. // Record that this library was used.
  760. used.insert(lib->first);
  761. // Don't emit the same library twice for this target.
  762. if(emitted.insert(lib->first).second)
  763. {
  764. // Output this dependency.
  765. this->OutputLibDepend(fout, lib->first.c_str());
  766. }
  767. }
  768. // Now, look at all utilities specific to this target.
  769. const std::set<std::string>& tutils = l->second.GetUtilities();
  770. for(std::set<std::string>::const_iterator util = tutils.begin();
  771. util != tutils.end(); ++util)
  772. {
  773. // Record that this utility was used.
  774. used.insert(*util);
  775. // Don't emit the same utility twice for this target.
  776. if(emitted.insert(*util).second)
  777. {
  778. // Output this dependency.
  779. this->OutputExeDepend(fout, util->c_str());
  780. }
  781. }
  782. fout << "\n";
  783. }
  784. }
  785. fout << "\n";
  786. // Loop over the libraries used and make sure there is a rule to
  787. // build them in this makefile. If the library is in another
  788. // directory, add a rule to jump to that directory and make sure it
  789. // exists.
  790. for(std::set<std::string>::const_iterator lib = used.begin();
  791. lib != used.end(); ++lib)
  792. {
  793. // loop over the list of directories that the libraries might
  794. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  795. // be stored in the cache
  796. const char* cacheValue = m_Makefile->GetDefinition(lib->c_str());
  797. // if cache and not the current directory add a rule, to
  798. // jump into the directory and build for the first time
  799. if(cacheValue &&
  800. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  801. {
  802. // add the correct extension
  803. std::string ltname = *lib+"_LIBRARY_TYPE";
  804. const char* libType
  805. = m_Makefile->GetDefinition(ltname.c_str());
  806. // if it was a library..
  807. if (libType)
  808. {
  809. std::string library = m_LibraryPrefix;
  810. library += *lib;
  811. std::string libpath = cacheValue;
  812. if(libType && std::string(libType) == "SHARED")
  813. {
  814. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  815. }
  816. else if(libType && std::string(libType) == "MODULE")
  817. {
  818. library += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  819. }
  820. else if(libType && std::string(libType) == "STATIC")
  821. {
  822. library += m_StaticLibraryExtension;
  823. }
  824. else
  825. {
  826. cmSystemTools::Error("Unknown library type!");
  827. return;
  828. }
  829. if(m_LibraryOutputPath.size())
  830. {
  831. libpath = m_LibraryOutputPath;
  832. }
  833. else
  834. {
  835. libpath += "/";
  836. }
  837. libpath += library;
  838. // put out a rule to build the library if it does not exist
  839. this->OutputBuildLibraryInDir(fout,
  840. cacheValue,
  841. library.c_str(),
  842. libpath.c_str());
  843. }
  844. // something other than a library...
  845. else
  846. {
  847. std::string exepath = cacheValue;
  848. if(m_ExecutableOutputPath.size())
  849. {
  850. exepath = m_ExecutableOutputPath;
  851. }
  852. else
  853. {
  854. exepath += "/";
  855. }
  856. exepath += *lib;
  857. this->OutputBuildExecutableInDir(fout,
  858. cacheValue,
  859. lib->c_str(),
  860. exepath.c_str());
  861. }
  862. }
  863. }
  864. }
  865. void cmUnixMakefileGenerator::OutputBuildLibraryInDir(std::ostream& fout,
  866. const char* path,
  867. const char* library,
  868. const char* fullpath)
  869. {
  870. const char* makeTarget = library;
  871. const char* libOutPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  872. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  873. {
  874. makeTarget = fullpath;
  875. }
  876. fout << cmSystemTools::EscapeSpaces(fullpath)
  877. << ":\n\tcd " << cmSystemTools::EscapeSpaces(path)
  878. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends"
  879. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends"
  880. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends"
  881. << "; $(MAKE) $(MAKESILENT) " << makeTarget << "\n\n";
  882. }
  883. void cmUnixMakefileGenerator::OutputBuildExecutableInDir(std::ostream& fout,
  884. const char* path,
  885. const char* library,
  886. const char* fullpath)
  887. {
  888. const char* makeTarget = library;
  889. const char* libOutPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  890. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  891. {
  892. makeTarget = fullpath;
  893. }
  894. fout << cmSystemTools::EscapeSpaces(fullpath)
  895. << ":\n\tcd " << cmSystemTools::EscapeSpaces(path)
  896. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends"
  897. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends"
  898. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends"
  899. << "; $(MAKE) $(MAKESILENT) " << makeTarget << "\n\n";
  900. }
  901. bool cmUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  902. {
  903. return strcmp(path1, path2) == 0;
  904. }
  905. void cmUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  906. const char* name)
  907. {
  908. const char* cacheValue = m_Makefile->GetDefinition(name);
  909. if(cacheValue )
  910. {
  911. // if there is a cache value, then this is a library that cmake
  912. // knows how to build, so we can depend on it
  913. std::string libpath;
  914. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  915. {
  916. // if the library is not in the current directory, then get the full
  917. // path to it
  918. if(m_LibraryOutputPath.size())
  919. {
  920. libpath = m_LibraryOutputPath;
  921. libpath += m_LibraryPrefix;
  922. }
  923. else
  924. {
  925. libpath = cacheValue;
  926. libpath += "/";
  927. libpath += m_LibraryPrefix;
  928. }
  929. }
  930. else
  931. {
  932. // library is in current Makefile so use lib as a prefix
  933. libpath = m_LibraryOutputPath;
  934. libpath += m_LibraryPrefix;
  935. }
  936. // add the library name
  937. libpath += name;
  938. // add the correct extension
  939. std::string ltname = name;
  940. ltname += "_LIBRARY_TYPE";
  941. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  942. if(libType && std::string(libType) == "SHARED")
  943. {
  944. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  945. }
  946. else if (libType && std::string(libType) == "MODULE")
  947. {
  948. libpath += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  949. }
  950. else if (libType && std::string(libType) == "STATIC")
  951. {
  952. libpath += m_StaticLibraryExtension;
  953. }
  954. fout << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(libpath.c_str()).c_str())
  955. << " ";
  956. }
  957. }
  958. void cmUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  959. const char* name)
  960. {
  961. const char* cacheValue = m_Makefile->GetDefinition(name);
  962. if(cacheValue )
  963. {
  964. // if there is a cache value, then this is a executable/utility that cmake
  965. // knows how to build, so we can depend on it
  966. std::string exepath;
  967. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  968. {
  969. // if the exe/utility is not in the current directory, then get the full
  970. // path to it
  971. if(m_ExecutableOutputPath.size())
  972. {
  973. exepath = m_ExecutableOutputPath;
  974. }
  975. else
  976. {
  977. exepath = cacheValue;
  978. exepath += "/";
  979. }
  980. }
  981. else
  982. {
  983. // library is in current Makefile
  984. exepath = m_ExecutableOutputPath;
  985. }
  986. // add the library name
  987. exepath += name;
  988. // add the correct extension
  989. if (m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX"))
  990. {
  991. std::string replaceVars =
  992. m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
  993. if (!strcmp(replaceVars.c_str(),"@CMAKE_EXECUTABLE_SUFFIX@"))
  994. {
  995. replaceVars = "";
  996. }
  997. exepath += replaceVars;
  998. }
  999. fout << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(exepath.c_str()).c_str())
  1000. << " ";
  1001. }
  1002. }
  1003. // fix up names of directories so they can be used
  1004. // as targets in makefiles.
  1005. inline std::string FixDirectoryName(const char* dir)
  1006. {
  1007. std::string s = dir;
  1008. // replace ../ with 3 under bars
  1009. size_t pos = s.find("../");
  1010. if(pos != std::string::npos)
  1011. {
  1012. s.replace(pos, 3, "___");
  1013. }
  1014. // replace / directory separators with a single under bar
  1015. pos = s.find("/");
  1016. while(pos != std::string::npos)
  1017. {
  1018. s.replace(pos, 1, "_");
  1019. pos = s.find("/");
  1020. }
  1021. return s;
  1022. }
  1023. void cmUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1024. const char* dir,
  1025. const char* target1,
  1026. const char* target2)
  1027. {
  1028. std::string directory = cmSystemTools::EscapeSpaces(dir);
  1029. if(target1)
  1030. {
  1031. fout << "\t@if test ! -d " << directory
  1032. << "; then $(MAKE) rebuild_cache; fi\n"
  1033. << "\techo Building " << target1 << " in directory " << directory << "\n"
  1034. << "\t@cd " << directory
  1035. << "; $(MAKE) -$(MAKEFLAGS) " << target1 << "\n";
  1036. }
  1037. if(target2)
  1038. {
  1039. fout << "\techo Building " << target2 << " in directory " << directory << "\n";
  1040. fout << "\t@cd " << directory
  1041. << "; $(MAKE) -$(MAKEFLAGS) " << target2 << "\n";
  1042. }
  1043. fout << "\n";
  1044. }
  1045. void
  1046. cmUnixMakefileGenerator::
  1047. OutputSubDirectoryVars(std::ostream& fout,
  1048. const char* var,
  1049. const char* target,
  1050. const char* target1,
  1051. const char* target2,
  1052. const char* depend,
  1053. const std::vector<std::string>& SubDirectories)
  1054. {
  1055. if(!depend)
  1056. {
  1057. depend = "";
  1058. }
  1059. if( SubDirectories.size() == 0)
  1060. {
  1061. return;
  1062. }
  1063. fout << "# Variable for making " << target << " in subdirectories.\n";
  1064. fout << var << " = \\\n";
  1065. unsigned int i;
  1066. for(i =0; i < SubDirectories.size(); i++)
  1067. {
  1068. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1069. fout << target << "_" << subdir.c_str();
  1070. if(i == SubDirectories.size()-1)
  1071. {
  1072. fout << " \n\n";
  1073. }
  1074. else
  1075. {
  1076. fout << " \\\n";
  1077. }
  1078. }
  1079. fout << "# Targets for making " << target << " in subdirectories.\n";
  1080. std::string last = "";
  1081. for(unsigned int i =0; i < SubDirectories.size(); i++)
  1082. {
  1083. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1084. fout << target << "_" << subdir.c_str() << ": " << depend;
  1085. // Make each subdirectory depend on previous one. This forces
  1086. // parallel builds (make -j 2) to build in same order as a single
  1087. // threaded build to avoid dependency problems.
  1088. if(i > 0)
  1089. {
  1090. fout << " " << target << "_" << last.c_str();
  1091. }
  1092. fout << "\n";
  1093. last = subdir;
  1094. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1095. dir += "/";
  1096. dir += SubDirectories[i];
  1097. this->BuildInSubDirectory(fout, dir.c_str(),
  1098. target1, target2);
  1099. }
  1100. fout << "\n\n";
  1101. }
  1102. // output rules for decending into sub directories
  1103. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1104. {
  1105. // Output Sub directory build rules
  1106. const std::vector<std::string>& SubDirectories
  1107. = m_Makefile->GetSubDirectories();
  1108. if( SubDirectories.size() == 0)
  1109. {
  1110. return;
  1111. }
  1112. this->OutputSubDirectoryVars(fout,
  1113. "SUBDIR_BUILD",
  1114. "default_target",
  1115. "default_target",
  1116. 0, "$(TARGETS)",
  1117. SubDirectories);
  1118. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1119. "clean",
  1120. 0, 0,
  1121. SubDirectories);
  1122. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1123. "depend",
  1124. 0, 0,
  1125. SubDirectories);
  1126. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1127. "install",
  1128. 0, 0,
  1129. SubDirectories);
  1130. }
  1131. // Output the depend information for all the classes
  1132. // in the makefile. These would have been generated
  1133. // by the class cmMakeDepend GenerateMakefile
  1134. bool cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1135. {
  1136. bool ret = false;
  1137. // Iterate over every target.
  1138. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1139. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1140. target != targets.end(); ++target)
  1141. {
  1142. // Iterate over every source for this target.
  1143. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1144. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1145. source != sources.end(); ++source)
  1146. {
  1147. if(!source->IsAHeaderFileOnly())
  1148. {
  1149. if(!source->GetDepends().empty())
  1150. {
  1151. fout << source->GetSourceName() << m_ObjectFileExtension << " :";
  1152. // Iterate through all the dependencies for this source.
  1153. for(std::vector<std::string>::const_iterator dep =
  1154. source->GetDepends().begin();
  1155. dep != source->GetDepends().end(); ++dep)
  1156. {
  1157. fout << " \\\n"
  1158. << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(
  1159. dep->c_str()).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->ConvertToNativePath(cmSystemTools::EscapeSpaces(
  1205. dep->c_str()).c_str());
  1206. if(emitted.insert(dependfile).second)
  1207. {
  1208. fout << " \\\n" << dependfile ;
  1209. }
  1210. }
  1211. }
  1212. }
  1213. }
  1214. }
  1215. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1216. fout << "cmake.depends: ";
  1217. std::set<std::string>::iterator i;
  1218. for(i = emitted.begin(); i != emitted.end(); ++i)
  1219. {
  1220. fout << " \\\n" << *i;
  1221. }
  1222. fout << "\n\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) dependlocal\n\n";
  1223. fout << "\n\n";
  1224. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1225. for(std::set<std::string>::iterator i = emitted.begin();
  1226. i != emitted.end(); ++i)
  1227. {
  1228. fout << *i << ":\n"
  1229. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) dependlocal\n\n";
  1230. }
  1231. }
  1232. // Output each custom rule in the following format:
  1233. // output: source depends...
  1234. // (tab) command...
  1235. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1236. {
  1237. // We may be modifying the source groups temporarily, so make a copy.
  1238. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1239. const cmTargets &tgts = m_Makefile->GetTargets();
  1240. for(cmTargets::const_iterator tgt = tgts.begin();
  1241. tgt != tgts.end(); ++tgt)
  1242. {
  1243. // add any custom rules to the source groups
  1244. for (std::vector<cmCustomCommand>::const_iterator cr =
  1245. tgt->second.GetCustomCommands().begin();
  1246. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1247. {
  1248. cmSourceGroup& sourceGroup =
  1249. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1250. sourceGroups);
  1251. cmCustomCommand cc(*cr);
  1252. cc.ExpandVariables(*m_Makefile);
  1253. sourceGroup.AddCustomCommand(cc);
  1254. }
  1255. }
  1256. // Loop through every source group.
  1257. for(std::vector<cmSourceGroup>::const_iterator sg =
  1258. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1259. {
  1260. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1261. if(buildRules.empty())
  1262. { continue; }
  1263. std::string name = sg->GetName();
  1264. if(name != "")
  1265. {
  1266. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1267. }
  1268. // Loop through each source in the source group.
  1269. for(cmSourceGroup::BuildRules::const_iterator cc =
  1270. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1271. {
  1272. std::string source = cc->first;
  1273. const cmSourceGroup::Commands& commands = cc->second;
  1274. // Loop through every command generating code from the current source.
  1275. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1276. c != commands.end(); ++c)
  1277. {
  1278. // escape spaces and convert to native slashes path for
  1279. // the command
  1280. std::string command =
  1281. cmSystemTools::EscapeSpaces(c->second.m_Command.c_str());
  1282. command = this->ConvertToNativePath(command.c_str());
  1283. command += " ";
  1284. // now add the arguments
  1285. command += c->second.m_Arguments;
  1286. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1287. // if the command has no outputs, then it is a utility command
  1288. // with no outputs
  1289. if(commandFiles.m_Outputs.size() == 0)
  1290. {
  1291. std::string depends;
  1292. // collect out all the dependencies for this rule.
  1293. for(std::set<std::string>::const_iterator d =
  1294. commandFiles.m_Depends.begin();
  1295. d != commandFiles.m_Depends.end(); ++d)
  1296. {
  1297. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  1298. depends += " ";
  1299. depends += dep;
  1300. }
  1301. // output rule
  1302. this->OutputMakeRule(fout,
  1303. "Custom command",
  1304. source.c_str(),
  1305. depends.c_str(),
  1306. command.c_str());
  1307. }
  1308. // Write a rule for every output generated by this command.
  1309. for(std::set<std::string>::const_iterator output =
  1310. commandFiles.m_Outputs.begin();
  1311. output != commandFiles.m_Outputs.end(); ++output)
  1312. {
  1313. std::string src = cmSystemTools::EscapeSpaces(source.c_str());
  1314. std::string depends;
  1315. depends += src;
  1316. // Collect out all the dependencies for this rule.
  1317. for(std::set<std::string>::const_iterator d =
  1318. commandFiles.m_Depends.begin();
  1319. d != commandFiles.m_Depends.end(); ++d)
  1320. {
  1321. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  1322. depends += " ";
  1323. depends += dep;
  1324. }
  1325. // output rule
  1326. this->OutputMakeRule(fout,
  1327. "Custom command",
  1328. output->c_str(),
  1329. depends.c_str(),
  1330. command.c_str());
  1331. }
  1332. }
  1333. }
  1334. if(name != "")
  1335. {
  1336. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1337. }
  1338. }
  1339. }
  1340. void cmUnixMakefileGenerator::GenerateCacheOnly()
  1341. {
  1342. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  1343. std::string dest = m_Makefile->GetStartOutputDirectory();
  1344. dest += "/Makefile";
  1345. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  1346. this->OutputMakefile(dest.c_str());
  1347. return;
  1348. }
  1349. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  1350. {
  1351. std::vector<cmMakefile*> makefiles;
  1352. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  1353. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  1354. i != makefiles.end(); ++i)
  1355. {
  1356. cmMakefile* mf = *i;
  1357. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  1358. {
  1359. mf->AddDefinition("RUN_CONFIGURE", true);
  1360. }
  1361. cmUnixMakefileGenerator* gen =
  1362. static_cast<cmUnixMakefileGenerator*>(this->CreateObject());
  1363. gen->SetCacheOnlyOn();
  1364. gen->SetRecurseOff();
  1365. mf->SetMakefileGenerator(gen);
  1366. mf->GenerateMakefile();
  1367. }
  1368. // CLEAN up the makefiles created
  1369. for(unsigned int i =0; i < makefiles.size(); ++i)
  1370. {
  1371. delete makefiles[i];
  1372. }
  1373. }
  1374. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1375. {
  1376. const char* variables =
  1377. "# the standard shell for make\n"
  1378. "SHELL = /bin/sh\n"
  1379. "\n"
  1380. "CMAKE_LIB_EXT = @CMAKE_LIB_EXT@\n"
  1381. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  1382. "CMAKE_AR = @CMAKE_AR@\n"
  1383. "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
  1384. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  1385. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@\n"
  1386. "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
  1387. "\n"
  1388. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  1389. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@\n"
  1390. "\n"
  1391. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  1392. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  1393. "CMAKE_MODULE_BUILD_FLAGS = @CMAKE_MODULE_BUILD_FLAGS@\n"
  1394. "CMAKE_MODULE_LINK_FLAGS = @CMAKE_MODULE_LINK_FLAGS@\n"
  1395. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  1396. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  1397. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  1398. "MODULE_SUFFIX = @CMAKE_MODULE_SUFFIX@\n"
  1399. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  1400. "RM = rm -f\n"
  1401. "\n"
  1402. "# set up the path to the rulesgen program\n"
  1403. "CMAKE_COMMAND = ${CMAKE_COMMAND}"
  1404. "\n"
  1405. "\n"
  1406. "\n";
  1407. std::string replaceVars = variables;
  1408. m_Makefile->ExpandVariablesInString(replaceVars);
  1409. fout << replaceVars.c_str();
  1410. fout << "CMAKE_CURRENT_SOURCE = " <<
  1411. cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << "\n";
  1412. fout << "CMAKE_CURRENT_BINARY = " <<
  1413. cmSystemTools::EscapeSpaces(m_Makefile->GetStartOutputDirectory()) << "\n";
  1414. fout << "CMAKE_SOURCE_DIR = " <<
  1415. cmSystemTools::EscapeSpaces(m_Makefile->GetHomeDirectory()) << "\n";
  1416. fout << "CMAKE_BINARY_DIR = " <<
  1417. cmSystemTools::EscapeSpaces(m_Makefile->GetHomeOutputDirectory()) << "\n";
  1418. // Output Include paths
  1419. fout << "INCLUDE_FLAGS = ";
  1420. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1421. std::vector<std::string>::iterator i;
  1422. fout << "-I" <<
  1423. cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << " ";
  1424. for(i = includes.begin(); i != includes.end(); ++i)
  1425. {
  1426. std::string include = *i;
  1427. // Don't output a -I for the standard include path "/usr/include".
  1428. // This can cause problems with certain standard library
  1429. // implementations because the wrong headers may be found first.
  1430. if(include != "/usr/include")
  1431. {
  1432. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  1433. }
  1434. }
  1435. fout << m_Makefile->GetDefineFlags();
  1436. fout << "\n\n";
  1437. }
  1438. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1439. {
  1440. const char* root
  1441. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1442. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1443. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1444. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1445. const cmTargets &tgts = m_Makefile->GetTargets();
  1446. fout << "install: $(SUBDIR_INSTALL)\n";
  1447. fout << "\t@echo \"Installing ...\"\n";
  1448. const char* prefix
  1449. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1450. if (!prefix)
  1451. {
  1452. prefix = "/usr/local";
  1453. }
  1454. for(cmTargets::const_iterator l = tgts.begin();
  1455. l != tgts.end(); l++)
  1456. {
  1457. if (l->second.GetInstallPath() != "")
  1458. {
  1459. // first make the directories for each target
  1460. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1461. " ] ; then \\\n";
  1462. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1463. << l->second.GetInstallPath() << " \"; \\\n";
  1464. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1465. << "; \\\n";
  1466. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1467. << "; \\\n";
  1468. fout << "\t else true; \\\n";
  1469. fout << "\t fi\n";
  1470. // now install the target
  1471. switch (l->second.GetType())
  1472. {
  1473. case cmTarget::STATIC_LIBRARY:
  1474. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1475. << l->first;
  1476. fout << ".a";
  1477. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1478. break;
  1479. case cmTarget::SHARED_LIBRARY:
  1480. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1481. << l->first;
  1482. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1483. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1484. break;
  1485. case cmTarget::MODULE_LIBRARY:
  1486. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1487. << l->first;
  1488. fout << m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1489. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1490. break;
  1491. case cmTarget::WIN32_EXECUTABLE:
  1492. case cmTarget::EXECUTABLE:
  1493. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1494. << l->first
  1495. << cmSystemTools::GetExecutableExtension()
  1496. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1497. break;
  1498. case cmTarget::INSTALL_FILES:
  1499. {
  1500. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1501. std::vector<std::string>::const_iterator i;
  1502. for (i = sf.begin(); i != sf.end(); ++i)
  1503. {
  1504. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1505. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1506. // avoid using install-sh to install install-sh
  1507. // does not work on windows....
  1508. if(*i == "install-sh")
  1509. {
  1510. fout << "\t cp ";
  1511. }
  1512. else
  1513. {
  1514. fout << "\t $(INSTALL_DATA) ";
  1515. }
  1516. fout << *i
  1517. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1518. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1519. // avoid using install-sh to install install-sh
  1520. // does not work on windows....
  1521. if(*i == "install-sh")
  1522. {
  1523. fout << "\t cp ";
  1524. }
  1525. else
  1526. {
  1527. fout << "\t $(INSTALL_DATA) ";
  1528. }
  1529. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1530. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1531. fout << "\telse \\\n";
  1532. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1533. << " \"; \\\n";
  1534. fout << "\t fi\n";
  1535. }
  1536. }
  1537. break;
  1538. case cmTarget::INSTALL_PROGRAMS:
  1539. {
  1540. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1541. std::vector<std::string>::const_iterator i;
  1542. for (i = sf.begin(); i != sf.end(); ++i)
  1543. {
  1544. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1545. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1546. // avoid using install-sh to install install-sh
  1547. // does not work on windows....
  1548. if(*i == "install-sh")
  1549. {
  1550. fout << "\t cp ";
  1551. }
  1552. else
  1553. {
  1554. fout << "\t $(INSTALL_PROGRAM) ";
  1555. }
  1556. fout << *i
  1557. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1558. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1559. // avoid using install-sh to install install-sh
  1560. // does not work on windows....
  1561. if(*i == "install-sh")
  1562. {
  1563. fout << "\t cp ";
  1564. }
  1565. else
  1566. {
  1567. fout << "\t $(INSTALL_PROGRAM) ";
  1568. }
  1569. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1570. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1571. fout << "\telse \\\n";
  1572. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1573. << " \"; \\\n";
  1574. fout << "\t fi\n";
  1575. }
  1576. }
  1577. break;
  1578. case cmTarget::UTILITY:
  1579. default:
  1580. break;
  1581. }
  1582. }
  1583. }
  1584. }
  1585. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1586. {
  1587. this->OutputMakeRule(fout,
  1588. "Default build rule",
  1589. "all",
  1590. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1591. 0);
  1592. this->OutputMakeRule(fout,
  1593. "remove generated files",
  1594. "clean",
  1595. "$(SUBDIR_CLEAN)",
  1596. "-@ $(RM) $(CLEAN_OBJECT_FILES) $(EXECUTABLES)"
  1597. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  1598. // collect up all the sources
  1599. std::string allsources;
  1600. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1601. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1602. target != targets.end(); ++target)
  1603. {
  1604. // Iterate over every source for this target.
  1605. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1606. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1607. source != sources.end(); ++source)
  1608. {
  1609. if(!source->IsAHeaderFileOnly())
  1610. {
  1611. allsources += " \\\n";
  1612. allsources += source->GetFullPath();
  1613. }
  1614. }
  1615. }
  1616. this->OutputMakeRule(fout,
  1617. "Rule to build the cmake.depends and Makefile as side effect, if a source cmakelist file is out of date.",
  1618. "cmake.depends",
  1619. "$(CMAKE_MAKEFILE_SOURCES)",
  1620. "$(CMAKE_COMMAND) "
  1621. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1622. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1623. );
  1624. this->OutputMakeRule(fout,
  1625. "Rule to build the cmake.check_depends and Makefile as side effect, if any source file has changed.",
  1626. "cmake.check_depends",
  1627. allsources.c_str(),
  1628. "$(CMAKE_COMMAND) "
  1629. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1630. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1631. );
  1632. this->OutputMakeRule(fout,
  1633. "Rule to force the build of cmake.depends",
  1634. "depend",
  1635. "$(SUBDIR_DEPEND)",
  1636. "$(CMAKE_COMMAND) "
  1637. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1638. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1639. this->OutputMakeRule(fout,
  1640. "Rule to force the build of cmake.depends "
  1641. "in the current directory only.",
  1642. "dependlocal",
  1643. 0,
  1644. "$(CMAKE_COMMAND) "
  1645. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1646. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1647. this->OutputMakeRule(fout,
  1648. "Rebuild CMakeCache.txt file",
  1649. "rebuild_cache",
  1650. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1651. "$(CMAKE_COMMAND) "
  1652. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1653. this->OutputMakeRule(fout,
  1654. "Create CMakeCache.txt file",
  1655. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1656. 0,
  1657. "$(CMAKE_COMMAND) "
  1658. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1659. this->OutputMakeRule(fout,
  1660. "Rule to keep make from removing Makefiles "
  1661. "if control-C is hit during a run of cmake.",
  1662. ".PRECIOUS",
  1663. "Makefile cmake.depends",
  1664. 0);
  1665. this->OutputSourceObjectBuildRules(fout);
  1666. // find ctest
  1667. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1668. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1669. ctest += "/";
  1670. ctest += "ctest";
  1671. ctest += cmSystemTools::GetExecutableExtension();
  1672. if(!cmSystemTools::FileExists(ctest.c_str()))
  1673. {
  1674. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1675. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1676. ctest += "/Debug/";
  1677. ctest += "ctest";
  1678. ctest += cmSystemTools::GetExecutableExtension();
  1679. }
  1680. if(!cmSystemTools::FileExists(ctest.c_str()))
  1681. {
  1682. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1683. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1684. ctest += "/Release/";
  1685. ctest += "ctest";
  1686. ctest += cmSystemTools::GetExecutableExtension();
  1687. }
  1688. if (cmSystemTools::FileExists(ctest.c_str()))
  1689. {
  1690. this->OutputMakeRule(fout,
  1691. "run any tests",
  1692. "test",
  1693. "",
  1694. ctest.c_str());
  1695. }
  1696. }
  1697. void
  1698. cmUnixMakefileGenerator::
  1699. OutputBuildObjectFromSource(std::ostream& fout,
  1700. const char* shortName,
  1701. const cmSourceFile& source,
  1702. const char* extraCompileFlags,
  1703. bool shared)
  1704. {
  1705. // Header files shouldn't have build rules.
  1706. if(source.IsAHeaderFileOnly())
  1707. return;
  1708. std::string comment = "Build ";
  1709. std::string objectFile = std::string(shortName) + m_ObjectFileExtension;
  1710. comment += objectFile + " From ";
  1711. comment += source.GetFullPath();
  1712. std::string compileCommand;
  1713. std::string ext = source.GetSourceExtension();
  1714. if(ext == "c" )
  1715. {
  1716. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  1717. compileCommand += extraCompileFlags;
  1718. if(shared)
  1719. {
  1720. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1721. }
  1722. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1723. compileCommand +=
  1724. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  1725. compileCommand += " -o ";
  1726. compileCommand += objectFile;
  1727. }
  1728. else
  1729. {
  1730. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  1731. compileCommand += extraCompileFlags;
  1732. if(shared)
  1733. {
  1734. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1735. }
  1736. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1737. compileCommand +=
  1738. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  1739. compileCommand += " -o ";
  1740. compileCommand += objectFile;
  1741. }
  1742. this->OutputMakeRule(fout,
  1743. comment.c_str(),
  1744. objectFile.c_str(),
  1745. cmSystemTools::EscapeSpaces(source.GetFullPath().
  1746. c_str()).c_str(),
  1747. compileCommand.c_str());
  1748. }
  1749. void cmUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  1750. {
  1751. fout << "# Rules to build " << m_ObjectFileExtension
  1752. << " files from their sources:\n";
  1753. std::set<std::string> rules;
  1754. // Iterate over every target.
  1755. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1756. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1757. target != targets.end(); ++target)
  1758. {
  1759. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  1760. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  1761. std::string exportsDef = "";
  1762. if(shared)
  1763. {
  1764. exportsDef = "-D"+target->first+"_EXPORTS ";
  1765. }
  1766. // Iterate over every source for this target.
  1767. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1768. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1769. source != sources.end(); ++source)
  1770. {
  1771. if(!source->IsAHeaderFileOnly())
  1772. {
  1773. std::string shortName;
  1774. std::string sourceName;
  1775. // If the full path to the source file includes this
  1776. // directory, we want to use the relative path for the
  1777. // filename of the object file. Otherwise, we will use just
  1778. // the filename portion.
  1779. if((cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  1780. || (cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->
  1781. GetCurrentOutputDirectory()) == 0))
  1782. {
  1783. sourceName = source->GetSourceName()+"."+source->GetSourceExtension();
  1784. shortName = source->GetSourceName();
  1785. // The path may be relative. See if a directory needs to be
  1786. // created for the output file. This is a ugly, and perhaps
  1787. // should be moved elsewhere.
  1788. std::string relPath =
  1789. cmSystemTools::GetFilenamePath(source->GetSourceName());
  1790. if(relPath != "")
  1791. {
  1792. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  1793. outPath += "/"+relPath;
  1794. cmSystemTools::MakeDirectory(outPath.c_str());
  1795. }
  1796. }
  1797. else
  1798. {
  1799. sourceName = source->GetFullPath();
  1800. shortName = cmSystemTools::GetFilenameName(source->GetSourceName());
  1801. }
  1802. std::string shortNameWithExt = shortName +
  1803. source->GetSourceExtension();
  1804. // Only output a rule for each .o once.
  1805. if(rules.find(shortNameWithExt) == rules.end())
  1806. {
  1807. this->OutputBuildObjectFromSource(fout,
  1808. shortName.c_str(),
  1809. *source,
  1810. exportsDef.c_str(),
  1811. shared);
  1812. rules.insert(shortNameWithExt);
  1813. }
  1814. }
  1815. }
  1816. }
  1817. }
  1818. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  1819. const char* comment,
  1820. const char* target,
  1821. const char* depends,
  1822. const char* command,
  1823. const char* command2,
  1824. const char* command3,
  1825. const char* command4)
  1826. {
  1827. if(!target)
  1828. {
  1829. cmSystemTools::Error("no target for OutputMakeRule");
  1830. return;
  1831. }
  1832. std::string replace;
  1833. if(comment)
  1834. {
  1835. replace = comment;
  1836. m_Makefile->ExpandVariablesInString(replace);
  1837. fout << "#---------------------------------------------------------\n";
  1838. fout << "# " << comment;
  1839. fout << "\n#\n";
  1840. }
  1841. fout << "\n";
  1842. replace = target;
  1843. m_Makefile->ExpandVariablesInString(replace);
  1844. fout << replace.c_str() << ": ";
  1845. if(depends)
  1846. {
  1847. replace = depends;
  1848. m_Makefile->ExpandVariablesInString(replace);
  1849. fout << replace.c_str();
  1850. }
  1851. fout << "\n";
  1852. if(command)
  1853. {
  1854. replace = command;
  1855. m_Makefile->ExpandVariablesInString(replace);
  1856. if(replace[0] != '-' && replace.find("echo") != 0
  1857. && replace.find("$(MAKE)") != 0)
  1858. {
  1859. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1860. }
  1861. fout << "\t" << replace.c_str() << "\n";
  1862. }
  1863. if(command2)
  1864. {
  1865. replace = command2;
  1866. m_Makefile->ExpandVariablesInString(replace);
  1867. if(replace[0] != '-' && replace.find("echo") != 0
  1868. && replace.find("$(MAKE)") != 0)
  1869. {
  1870. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1871. }
  1872. fout << "\t" << replace.c_str() << "\n";
  1873. }
  1874. if(command3)
  1875. {
  1876. replace = command3;
  1877. m_Makefile->ExpandVariablesInString(replace);
  1878. if(replace[0] != '-' && replace.find("echo") != 0
  1879. && replace.find("$(MAKE)") != 0)
  1880. {
  1881. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1882. }
  1883. fout << "\t" << replace.c_str() << "\n";
  1884. }
  1885. if(command4)
  1886. {
  1887. replace = command4;
  1888. m_Makefile->ExpandVariablesInString(replace);
  1889. if(replace[0] != '-' && replace.find("echo") != 0
  1890. && replace.find("$(MAKE)") != 0)
  1891. {
  1892. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1893. }
  1894. fout << "\t" << replace.c_str() << "\n";
  1895. }
  1896. fout << "\n";
  1897. }
  1898. void cmUnixMakefileGenerator::SetLocal (bool local)
  1899. {
  1900. if (local)
  1901. {
  1902. m_CacheOnly = false;
  1903. m_Recurse = false;
  1904. }
  1905. else
  1906. {
  1907. m_CacheOnly = true;
  1908. m_Recurse = true;
  1909. }
  1910. }
  1911. void cmUnixMakefileGenerator::ComputeSystemInfo()
  1912. {
  1913. if (m_CacheOnly)
  1914. {
  1915. // see man putenv for explaination of this stupid code....
  1916. static char envCXX[5000];
  1917. static char envCC[5000];
  1918. if(m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"))
  1919. {
  1920. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  1921. m_Makefile->ExpandVariablesInString(env);
  1922. strncpy(envCXX, env.c_str(), 4999);
  1923. envCXX[4999] = 0;
  1924. putenv(envCXX);
  1925. }
  1926. if(m_Makefile->GetDefinition("CMAKE_C_COMPILER"))
  1927. {
  1928. std::string env = "CC=${CMAKE_C_COMPILER}";
  1929. m_Makefile->ExpandVariablesInString(env);
  1930. strncpy(envCC, env.c_str(), 4999);
  1931. envCC[4999] = 0;
  1932. putenv(envCC);
  1933. }
  1934. // currently we run configure shell script here to determine the info
  1935. std::string output;
  1936. std::string cmd = "cd ";
  1937. cmd += cmSystemTools::EscapeSpaces(m_Makefile->GetHomeOutputDirectory());
  1938. cmd += "; ";
  1939. const char* root
  1940. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1941. cmd += root;
  1942. cmd += "/Templates/configure";
  1943. cmSystemTools::RunCommand(cmd.c_str(), output);
  1944. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  1945. }
  1946. // now load the settings
  1947. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  1948. fpath += "/CMakeSystemConfig.cmake";
  1949. m_Makefile->ReadListFile(NULL,fpath.c_str());
  1950. }