cmUnixMakefileGenerator.cxx 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  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 << this->CreateMakeVariable(l->first.c_str(), "_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 << this->CreateMakeVariable(l->first.c_str(), "_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 << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
  382. << ") ";
  383. }
  384. }
  385. fout << "\n\n";
  386. }
  387. /**
  388. * Output the linking rules on a command line. For executables,
  389. * targetLibrary should be a NULL pointer. For libraries, it should point
  390. * to the name of the library. This will not link a library against itself.
  391. */
  392. void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  393. const char* targetLibrary,
  394. const cmTarget &tgt)
  395. {
  396. // Try to emit each search path once
  397. std::set<std::string> emitted;
  398. // Embed runtime search paths if possible and if required.
  399. bool outputRuntime = true;
  400. std::string runtimeFlag;
  401. std::string runtimeSep;
  402. std::vector<std::string> runtimeDirs;
  403. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG"))
  404. {
  405. runtimeFlag = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG");
  406. }
  407. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP"))
  408. {
  409. runtimeSep = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP");
  410. }
  411. // concatenate all paths or no?
  412. bool runtimeConcatenate = ( runtimeSep!="" );
  413. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  414. {
  415. outputRuntime = false;
  416. }
  417. // Some search paths should never be emitted
  418. emitted.insert("");
  419. emitted.insert("/usr/lib");
  420. // collect all the flags needed for linking libraries
  421. std::string linkLibs;
  422. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  423. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  424. libDir != libdirs.end(); ++libDir)
  425. {
  426. std::string libpath = cmSystemTools::EscapeSpaces(libDir->c_str());
  427. if(emitted.insert(libpath).second)
  428. {
  429. std::string::size_type pos = libDir->find("-L");
  430. if((pos == std::string::npos || pos > 0)
  431. && libDir->find("${") == std::string::npos)
  432. {
  433. linkLibs += "-L";
  434. if(outputRuntime)
  435. {
  436. runtimeDirs.push_back( libpath );
  437. }
  438. }
  439. linkLibs += libpath;
  440. linkLibs += " ";
  441. }
  442. }
  443. std::string librariesLinked;
  444. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  445. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  446. lib != libs.end(); ++lib)
  447. {
  448. // Don't link the library against itself!
  449. if(targetLibrary && (lib->first == targetLibrary)) continue;
  450. // don't look at debug libraries
  451. if (lib->second == cmTarget::DEBUG) continue;
  452. // skip zero size library entries, this may happen
  453. // if a variable expands to nothing.
  454. if (lib->first.size() == 0) continue;
  455. // if it is a full path break it into -L and -l
  456. cmRegularExpression reg("(^[ \t]*\\-l)|(^[ \t]*\\-framework)|(\\${)");
  457. if(lib->first.find('/') != std::string::npos
  458. && !reg.find(lib->first))
  459. {
  460. std::string dir, file;
  461. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  462. dir, file);
  463. std::string libpath = cmSystemTools::EscapeSpaces(dir.c_str());
  464. if(emitted.insert(libpath).second)
  465. {
  466. linkLibs += "-L";
  467. linkLibs += libpath;
  468. linkLibs += " ";
  469. if(outputRuntime)
  470. {
  471. runtimeDirs.push_back( libpath );
  472. }
  473. }
  474. cmRegularExpression libname("lib(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
  475. cmRegularExpression libname_noprefix("(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
  476. if(libname.find(file))
  477. {
  478. librariesLinked += "-l";
  479. file = libname.match(1);
  480. librariesLinked += file;
  481. librariesLinked += " ";
  482. }
  483. else if(libname_noprefix.find(file))
  484. {
  485. librariesLinked += "-l";
  486. file = libname_noprefix.match(1);
  487. librariesLinked += file;
  488. librariesLinked += " ";
  489. }
  490. }
  491. // not a full path, so add -l name
  492. else
  493. {
  494. if(!reg.find(lib->first))
  495. {
  496. librariesLinked += "-l";
  497. }
  498. librariesLinked += lib->first;
  499. librariesLinked += " ";
  500. }
  501. }
  502. linkLibs += librariesLinked;
  503. if(!targetLibrary)
  504. {
  505. // For executables, add these a second time so order does not matter
  506. linkLibs += librariesLinked;
  507. }
  508. fout << linkLibs;
  509. if(outputRuntime && runtimeDirs.size()>0)
  510. {
  511. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  512. // a "-R a -R b -R c" type link line
  513. fout << runtimeFlag;
  514. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  515. fout << *itr;
  516. ++itr;
  517. for( ; itr != runtimeDirs.end(); ++itr )
  518. {
  519. if(runtimeConcatenate)
  520. {
  521. fout << runtimeSep << *itr;
  522. }
  523. else
  524. {
  525. fout << " " << runtimeFlag << *itr;
  526. }
  527. }
  528. fout << " ";
  529. }
  530. }
  531. std::string cmUnixMakefileGenerator::CreateTargetRules(const cmTarget &target,
  532. const char* targetName)
  533. {
  534. std::string customRuleCode = "";
  535. bool initNext = false;
  536. for (std::vector<cmCustomCommand>::const_iterator cr =
  537. target.GetCustomCommands().begin();
  538. cr != target.GetCustomCommands().end(); ++cr)
  539. {
  540. cmCustomCommand cc(*cr);
  541. cc.ExpandVariables(*m_Makefile);
  542. if (cc.GetSourceName() == targetName)
  543. {
  544. if(initNext)
  545. {
  546. customRuleCode += "\n\t";
  547. }
  548. else
  549. {
  550. initNext = true;
  551. }
  552. std::string command = cmSystemTools::EscapeSpaces(cc.GetCommand().c_str());
  553. command = this->ConvertToNativePath(command.c_str());
  554. customRuleCode += command + " " + cc.GetArguments();
  555. }
  556. }
  557. return customRuleCode;
  558. }
  559. void cmUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  560. const char* name,
  561. const cmTarget &t)
  562. {
  563. std::string target = m_LibraryOutputPath + "lib" + name + "$(SHLIB_SUFFIX)";
  564. std::string depend = "$(";
  565. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  566. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  567. std::string command = "$(RM) lib";
  568. command += name;
  569. command += "$(SHLIB_SUFFIX)";
  570. std::string command2 = "$(CMAKE_CXX_COMPILER) $(CMAKE_SHLIB_LINK_FLAGS) "
  571. "$(CMAKE_SHLIB_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  572. command2 += "\t ";
  573. command2 += m_LibraryOutputPath + "lib" + std::string(name) + "$(SHLIB_SUFFIX) \\\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. command2 += m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX) \\\n";
  606. command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  607. std::strstream linklibs;
  608. this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
  609. linklibs << std::ends;
  610. command2 += linklibs.str();
  611. delete [] linklibs.str();
  612. std::string customCommands = this->CreateTargetRules(t, name);
  613. const char* cc = 0;
  614. if(customCommands.size() > 0)
  615. {
  616. cc = customCommands.c_str();
  617. }
  618. this->OutputMakeRule(fout, "rules for a shared module library",
  619. target.c_str(),
  620. depend.c_str(),
  621. command.c_str(),
  622. command2.c_str(),
  623. cc);
  624. }
  625. void cmUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  626. const char* name,
  627. const cmTarget &t)
  628. {
  629. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + ".a";
  630. std::string depend = "$(";
  631. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  632. std::string command = "$(CMAKE_AR) $(CMAKE_AR_ARGS) ";
  633. command += m_LibraryOutputPath;
  634. command += "lib";
  635. command += name;
  636. command += ".a $(";
  637. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  638. std::string command2 = "$(CMAKE_RANLIB) ";
  639. command2 += m_LibraryOutputPath;
  640. command2 += "lib";
  641. command2 += std::string(name) + ".a";
  642. std::string comment = "rule to build static library: ";
  643. comment += name;
  644. std::string customCommands = this->CreateTargetRules(t, name);
  645. const char* cc = 0;
  646. if(customCommands.size() > 0)
  647. {
  648. cc = customCommands.c_str();
  649. }
  650. this->OutputMakeRule(fout,
  651. comment.c_str(),
  652. target.c_str(),
  653. depend.c_str(),
  654. command.c_str(),
  655. command2.c_str(),
  656. cc);
  657. }
  658. void cmUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  659. const char* name,
  660. const cmTarget &t)
  661. {
  662. std::string target = m_ExecutableOutputPath + name;
  663. std::string depend = "$(";
  664. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  665. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  666. std::string command =
  667. "$(CMAKE_CXX_COMPILER) $(CMAKE_SHLIB_LINK_FLAGS) $(CMAKE_CXX_FLAGS) ";
  668. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  669. std::strstream linklibs;
  670. this->OutputLinkLibraries(linklibs, 0, t);
  671. linklibs << std::ends;
  672. command += linklibs.str();
  673. command += " -o " + m_ExecutableOutputPath + name;
  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 << cmSystemTools::EscapeSpaces(fullpath)
  880. << ":\n\tcd " << cmSystemTools::EscapeSpaces(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 << cmSystemTools::EscapeSpaces(fullpath)
  898. << ":\n\tcd " << cmSystemTools::EscapeSpaces(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->ConvertToNativePath(cmSystemTools::EscapeSpaces(libpath.c_str()).c_str())
  958. << " ";
  959. }
  960. }
  961. void cmUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  962. const char* name)
  963. {
  964. const char* cacheValue = m_Makefile->GetDefinition(name);
  965. if(cacheValue )
  966. {
  967. // if there is a cache value, then this is a executable/utility that cmake
  968. // knows how to build, so we can depend on it
  969. std::string exepath;
  970. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  971. {
  972. // if the exe/utility is not in the current directory, then get the full
  973. // path to it
  974. if(m_ExecutableOutputPath.size())
  975. {
  976. exepath = m_ExecutableOutputPath;
  977. }
  978. else
  979. {
  980. exepath = cacheValue;
  981. exepath += "/";
  982. }
  983. }
  984. else
  985. {
  986. // library is in current Makefile
  987. exepath = m_ExecutableOutputPath;
  988. }
  989. // add the library name
  990. exepath += name;
  991. // add the correct extension
  992. if (m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX"))
  993. {
  994. std::string replaceVars =
  995. m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
  996. if (!strcmp(replaceVars.c_str(),"@CMAKE_EXECUTABLE_SUFFIX@"))
  997. {
  998. replaceVars = "";
  999. }
  1000. exepath += replaceVars;
  1001. }
  1002. fout << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(exepath.c_str()).c_str())
  1003. << " ";
  1004. }
  1005. }
  1006. // fix up names of directories so they can be used
  1007. // as targets in makefiles.
  1008. inline std::string FixDirectoryName(const char* dir)
  1009. {
  1010. std::string s = dir;
  1011. // replace ../ with 3 under bars
  1012. size_t pos = s.find("../");
  1013. if(pos != std::string::npos)
  1014. {
  1015. s.replace(pos, 3, "___");
  1016. }
  1017. // replace / directory separators with a single under bar
  1018. pos = s.find("/");
  1019. while(pos != std::string::npos)
  1020. {
  1021. s.replace(pos, 1, "_");
  1022. pos = s.find("/");
  1023. }
  1024. return s;
  1025. }
  1026. void cmUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1027. const char* dir,
  1028. const char* target1,
  1029. const char* target2)
  1030. {
  1031. std::string directory = cmSystemTools::EscapeSpaces(dir);
  1032. if(target1)
  1033. {
  1034. fout << "\t@if test ! -d " << directory
  1035. << "; then $(MAKE) rebuild_cache; fi\n"
  1036. << "\techo Building " << target1 << " in directory " << directory << "\n"
  1037. << "\t@cd " << directory
  1038. << "; $(MAKE) -$(MAKEFLAGS) " << target1 << "\n";
  1039. }
  1040. if(target2)
  1041. {
  1042. fout << "\techo Building " << target2 << " in directory " << directory << "\n";
  1043. fout << "\t@cd " << directory
  1044. << "; $(MAKE) -$(MAKEFLAGS) " << target2 << "\n";
  1045. }
  1046. fout << "\n";
  1047. }
  1048. void
  1049. cmUnixMakefileGenerator::
  1050. OutputSubDirectoryVars(std::ostream& fout,
  1051. const char* var,
  1052. const char* target,
  1053. const char* target1,
  1054. const char* target2,
  1055. const char* depend,
  1056. const std::vector<std::string>& SubDirectories)
  1057. {
  1058. if(!depend)
  1059. {
  1060. depend = "";
  1061. }
  1062. if( SubDirectories.size() == 0)
  1063. {
  1064. return;
  1065. }
  1066. fout << "# Variable for making " << target << " in subdirectories.\n";
  1067. fout << var << " = \\\n";
  1068. unsigned int i;
  1069. for(i =0; i < SubDirectories.size(); i++)
  1070. {
  1071. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1072. fout << target << "_" << subdir.c_str();
  1073. if(i == SubDirectories.size()-1)
  1074. {
  1075. fout << " \n\n";
  1076. }
  1077. else
  1078. {
  1079. fout << " \\\n";
  1080. }
  1081. }
  1082. fout << "# Targets for making " << target << " in subdirectories.\n";
  1083. std::string last = "";
  1084. for(unsigned int i =0; i < SubDirectories.size(); i++)
  1085. {
  1086. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1087. fout << target << "_" << subdir.c_str() << ": " << depend;
  1088. // Make each subdirectory depend on previous one. This forces
  1089. // parallel builds (make -j 2) to build in same order as a single
  1090. // threaded build to avoid dependency problems.
  1091. if(i > 0)
  1092. {
  1093. fout << " " << target << "_" << last.c_str();
  1094. }
  1095. fout << "\n";
  1096. last = subdir;
  1097. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1098. dir += "/";
  1099. dir += SubDirectories[i];
  1100. this->BuildInSubDirectory(fout, dir.c_str(),
  1101. target1, target2);
  1102. }
  1103. fout << "\n\n";
  1104. }
  1105. // output rules for decending into sub directories
  1106. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1107. {
  1108. // Output Sub directory build rules
  1109. const std::vector<std::string>& SubDirectories
  1110. = m_Makefile->GetSubDirectories();
  1111. if( SubDirectories.size() == 0)
  1112. {
  1113. return;
  1114. }
  1115. this->OutputSubDirectoryVars(fout,
  1116. "SUBDIR_BUILD",
  1117. "default_target",
  1118. "default_target",
  1119. 0, "$(TARGETS)",
  1120. SubDirectories);
  1121. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1122. "clean",
  1123. 0, 0,
  1124. SubDirectories);
  1125. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1126. "depend",
  1127. 0, 0,
  1128. SubDirectories);
  1129. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1130. "install",
  1131. 0, 0,
  1132. SubDirectories);
  1133. }
  1134. // Output the depend information for all the classes
  1135. // in the makefile. These would have been generated
  1136. // by the class cmMakeDepend GenerateMakefile
  1137. bool cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1138. {
  1139. bool ret = false;
  1140. // Iterate over every target.
  1141. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1142. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1143. target != targets.end(); ++target)
  1144. {
  1145. // Iterate over every source for this target.
  1146. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1147. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1148. source != sources.end(); ++source)
  1149. {
  1150. if(!source->IsAHeaderFileOnly())
  1151. {
  1152. if(!source->GetDepends().empty())
  1153. {
  1154. fout << source->GetSourceName() << m_ObjectFileExtension << " :";
  1155. // Iterate through all the dependencies for this source.
  1156. for(std::vector<std::string>::const_iterator dep =
  1157. source->GetDepends().begin();
  1158. dep != source->GetDepends().end(); ++dep)
  1159. {
  1160. fout << " \\\n"
  1161. << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(
  1162. dep->c_str()).c_str());
  1163. ret = true;
  1164. }
  1165. fout << "\n\n";
  1166. }
  1167. }
  1168. }
  1169. }
  1170. return ret;
  1171. }
  1172. // Output the depend information for all the classes
  1173. // in the makefile. These would have been generated
  1174. // by the class cmMakeDepend GenerateMakefile
  1175. void cmUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1176. {
  1177. std::set<std::string> emitted;
  1178. // Iterate over every target.
  1179. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1180. fout << "# Suppresses display of executed commands\n";
  1181. fout << ".SILENT:\n";
  1182. fout << "# disable some common implicit rules to speed things up\n";
  1183. fout << ".SUFFIXES:\n";
  1184. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1185. this->OutputMakeVariables(fout);
  1186. fout << "default:\n";
  1187. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends all\n"
  1188. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1189. fout << "all: ";
  1190. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1191. target != targets.end(); ++target)
  1192. {
  1193. // Iterate over every source for this target.
  1194. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1195. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1196. source != sources.end(); ++source)
  1197. {
  1198. if(!source->IsAHeaderFileOnly())
  1199. {
  1200. if(!source->GetDepends().empty())
  1201. {
  1202. for(std::vector<std::string>::const_iterator dep =
  1203. source->GetDepends().begin();
  1204. dep != source->GetDepends().end(); ++dep)
  1205. {
  1206. std::string dependfile =
  1207. this->ConvertToNativePath(cmSystemTools::EscapeSpaces(
  1208. dep->c_str()).c_str());
  1209. if(emitted.insert(dependfile).second)
  1210. {
  1211. fout << " \\\n" << dependfile ;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1219. fout << "cmake.depends: ";
  1220. std::set<std::string>::iterator i;
  1221. for(i = emitted.begin(); i != emitted.end(); ++i)
  1222. {
  1223. fout << " \\\n" << *i;
  1224. }
  1225. fout << "\n\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) dependlocal\n\n";
  1226. fout << "\n\n";
  1227. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1228. for(std::set<std::string>::iterator i = emitted.begin();
  1229. i != emitted.end(); ++i)
  1230. {
  1231. fout << *i << ":\n"
  1232. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) dependlocal\n\n";
  1233. }
  1234. }
  1235. // Output each custom rule in the following format:
  1236. // output: source depends...
  1237. // (tab) command...
  1238. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1239. {
  1240. // We may be modifying the source groups temporarily, so make a copy.
  1241. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1242. const cmTargets &tgts = m_Makefile->GetTargets();
  1243. for(cmTargets::const_iterator tgt = tgts.begin();
  1244. tgt != tgts.end(); ++tgt)
  1245. {
  1246. // add any custom rules to the source groups
  1247. for (std::vector<cmCustomCommand>::const_iterator cr =
  1248. tgt->second.GetCustomCommands().begin();
  1249. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1250. {
  1251. cmSourceGroup& sourceGroup =
  1252. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1253. sourceGroups);
  1254. cmCustomCommand cc(*cr);
  1255. cc.ExpandVariables(*m_Makefile);
  1256. sourceGroup.AddCustomCommand(cc);
  1257. }
  1258. }
  1259. // Loop through every source group.
  1260. for(std::vector<cmSourceGroup>::const_iterator sg =
  1261. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1262. {
  1263. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1264. if(buildRules.empty())
  1265. { continue; }
  1266. std::string name = sg->GetName();
  1267. if(name != "")
  1268. {
  1269. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1270. }
  1271. // Loop through each source in the source group.
  1272. for(cmSourceGroup::BuildRules::const_iterator cc =
  1273. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1274. {
  1275. std::string source = cc->first;
  1276. const cmSourceGroup::Commands& commands = cc->second;
  1277. // Loop through every command generating code from the current source.
  1278. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1279. c != commands.end(); ++c)
  1280. {
  1281. // escape spaces and convert to native slashes path for
  1282. // the command
  1283. std::string command =
  1284. cmSystemTools::EscapeSpaces(c->second.m_Command.c_str());
  1285. command = this->ConvertToNativePath(command.c_str());
  1286. command += " ";
  1287. // now add the arguments
  1288. command += c->second.m_Arguments;
  1289. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1290. // if the command has no outputs, then it is a utility command
  1291. // with no outputs
  1292. if(commandFiles.m_Outputs.size() == 0)
  1293. {
  1294. std::string depends;
  1295. // collect out all the dependencies for this rule.
  1296. for(std::set<std::string>::const_iterator d =
  1297. commandFiles.m_Depends.begin();
  1298. d != commandFiles.m_Depends.end(); ++d)
  1299. {
  1300. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  1301. depends += " ";
  1302. depends += dep;
  1303. }
  1304. // output rule
  1305. this->OutputMakeRule(fout,
  1306. "Custom command",
  1307. source.c_str(),
  1308. depends.c_str(),
  1309. command.c_str());
  1310. }
  1311. // Write a rule for every output generated by this command.
  1312. for(std::set<std::string>::const_iterator output =
  1313. commandFiles.m_Outputs.begin();
  1314. output != commandFiles.m_Outputs.end(); ++output)
  1315. {
  1316. std::string src = cmSystemTools::EscapeSpaces(source.c_str());
  1317. std::string depends;
  1318. depends += src;
  1319. // Collect out all the dependencies for this rule.
  1320. for(std::set<std::string>::const_iterator d =
  1321. commandFiles.m_Depends.begin();
  1322. d != commandFiles.m_Depends.end(); ++d)
  1323. {
  1324. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  1325. depends += " ";
  1326. depends += dep;
  1327. }
  1328. // output rule
  1329. this->OutputMakeRule(fout,
  1330. "Custom command",
  1331. output->c_str(),
  1332. depends.c_str(),
  1333. command.c_str());
  1334. }
  1335. }
  1336. }
  1337. if(name != "")
  1338. {
  1339. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1340. }
  1341. }
  1342. }
  1343. void cmUnixMakefileGenerator::GenerateCacheOnly()
  1344. {
  1345. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  1346. std::string dest = m_Makefile->GetStartOutputDirectory();
  1347. dest += "/Makefile";
  1348. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  1349. this->OutputMakefile(dest.c_str());
  1350. return;
  1351. }
  1352. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  1353. {
  1354. std::vector<cmMakefile*> makefiles;
  1355. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  1356. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  1357. i != makefiles.end(); ++i)
  1358. {
  1359. cmMakefile* mf = *i;
  1360. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  1361. {
  1362. mf->AddDefinition("RUN_CONFIGURE", true);
  1363. }
  1364. cmUnixMakefileGenerator* gen =
  1365. static_cast<cmUnixMakefileGenerator*>(this->CreateObject());
  1366. gen->SetCacheOnlyOn();
  1367. gen->SetRecurseOff();
  1368. mf->SetMakefileGenerator(gen);
  1369. mf->GenerateMakefile();
  1370. }
  1371. // CLEAN up the makefiles created
  1372. for(unsigned int i =0; i < makefiles.size(); ++i)
  1373. {
  1374. delete makefiles[i];
  1375. }
  1376. }
  1377. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1378. {
  1379. const char* variables =
  1380. "# the standard shell for make\n"
  1381. "SHELL = /bin/sh\n"
  1382. "\n"
  1383. "CMAKE_LIB_EXT = @CMAKE_LIB_EXT@\n"
  1384. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  1385. "CMAKE_AR = @CMAKE_AR@\n"
  1386. "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
  1387. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  1388. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@\n"
  1389. "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
  1390. "\n"
  1391. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  1392. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@\n"
  1393. "\n"
  1394. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  1395. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  1396. "CMAKE_MODULE_BUILD_FLAGS = @CMAKE_MODULE_BUILD_FLAGS@\n"
  1397. "CMAKE_MODULE_LINK_FLAGS = @CMAKE_MODULE_LINK_FLAGS@\n"
  1398. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  1399. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  1400. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  1401. "MODULE_SUFFIX = @CMAKE_MODULE_SUFFIX@\n"
  1402. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  1403. "RM = rm -f\n"
  1404. "\n"
  1405. "# set up the path to the rulesgen program\n"
  1406. "CMAKE_COMMAND = ${CMAKE_COMMAND}"
  1407. "\n"
  1408. "\n"
  1409. "\n";
  1410. std::string replaceVars = variables;
  1411. m_Makefile->ExpandVariablesInString(replaceVars);
  1412. fout << replaceVars.c_str();
  1413. fout << "CMAKE_CURRENT_SOURCE = " <<
  1414. cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << "\n";
  1415. fout << "CMAKE_CURRENT_BINARY = " <<
  1416. cmSystemTools::EscapeSpaces(m_Makefile->GetStartOutputDirectory()) << "\n";
  1417. fout << "CMAKE_SOURCE_DIR = " <<
  1418. cmSystemTools::EscapeSpaces(m_Makefile->GetHomeDirectory()) << "\n";
  1419. fout << "CMAKE_BINARY_DIR = " <<
  1420. cmSystemTools::EscapeSpaces(m_Makefile->GetHomeOutputDirectory()) << "\n";
  1421. // Output Include paths
  1422. fout << "INCLUDE_FLAGS = ";
  1423. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1424. std::vector<std::string>::iterator i;
  1425. fout << "-I" <<
  1426. cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << " ";
  1427. for(i = includes.begin(); i != includes.end(); ++i)
  1428. {
  1429. std::string include = *i;
  1430. // Don't output a -I for the standard include path "/usr/include".
  1431. // This can cause problems with certain standard library
  1432. // implementations because the wrong headers may be found first.
  1433. if(include != "/usr/include")
  1434. {
  1435. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  1436. }
  1437. }
  1438. fout << m_Makefile->GetDefineFlags();
  1439. fout << "\n\n";
  1440. }
  1441. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1442. {
  1443. const char* root
  1444. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1445. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1446. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1447. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1448. const cmTargets &tgts = m_Makefile->GetTargets();
  1449. fout << "install: $(SUBDIR_INSTALL)\n";
  1450. fout << "\t@echo \"Installing ...\"\n";
  1451. const char* prefix
  1452. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1453. if (!prefix)
  1454. {
  1455. prefix = "/usr/local";
  1456. }
  1457. for(cmTargets::const_iterator l = tgts.begin();
  1458. l != tgts.end(); l++)
  1459. {
  1460. if (l->second.GetInstallPath() != "")
  1461. {
  1462. // first make the directories for each target
  1463. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1464. " ] ; then \\\n";
  1465. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1466. << l->second.GetInstallPath() << " \"; \\\n";
  1467. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1468. << "; \\\n";
  1469. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1470. << "; \\\n";
  1471. fout << "\t else true; \\\n";
  1472. fout << "\t fi\n";
  1473. // now install the target
  1474. switch (l->second.GetType())
  1475. {
  1476. case cmTarget::STATIC_LIBRARY:
  1477. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1478. << l->first;
  1479. fout << ".a";
  1480. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1481. break;
  1482. case cmTarget::SHARED_LIBRARY:
  1483. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1484. << l->first;
  1485. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1486. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1487. break;
  1488. case cmTarget::MODULE_LIBRARY:
  1489. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1490. << l->first;
  1491. fout << m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1492. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1493. break;
  1494. case cmTarget::WIN32_EXECUTABLE:
  1495. case cmTarget::EXECUTABLE:
  1496. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1497. << l->first
  1498. << cmSystemTools::GetExecutableExtension()
  1499. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1500. break;
  1501. case cmTarget::INSTALL_FILES:
  1502. {
  1503. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1504. std::vector<std::string>::const_iterator i;
  1505. for (i = sf.begin(); i != sf.end(); ++i)
  1506. {
  1507. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1508. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1509. // avoid using install-sh to install install-sh
  1510. // does not work on windows....
  1511. if(*i == "install-sh")
  1512. {
  1513. fout << "\t cp ";
  1514. }
  1515. else
  1516. {
  1517. fout << "\t $(INSTALL_DATA) ";
  1518. }
  1519. fout << *i
  1520. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1521. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1522. // avoid using install-sh to install install-sh
  1523. // does not work on windows....
  1524. if(*i == "install-sh")
  1525. {
  1526. fout << "\t cp ";
  1527. }
  1528. else
  1529. {
  1530. fout << "\t $(INSTALL_DATA) ";
  1531. }
  1532. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1533. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1534. fout << "\telse \\\n";
  1535. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1536. << " \"; \\\n";
  1537. fout << "\t fi\n";
  1538. }
  1539. }
  1540. break;
  1541. case cmTarget::INSTALL_PROGRAMS:
  1542. {
  1543. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1544. std::vector<std::string>::const_iterator i;
  1545. for (i = sf.begin(); i != sf.end(); ++i)
  1546. {
  1547. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1548. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1549. // avoid using install-sh to install install-sh
  1550. // does not work on windows....
  1551. if(*i == "install-sh")
  1552. {
  1553. fout << "\t cp ";
  1554. }
  1555. else
  1556. {
  1557. fout << "\t $(INSTALL_PROGRAM) ";
  1558. }
  1559. fout << *i
  1560. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1561. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1562. // avoid using install-sh to install install-sh
  1563. // does not work on windows....
  1564. if(*i == "install-sh")
  1565. {
  1566. fout << "\t cp ";
  1567. }
  1568. else
  1569. {
  1570. fout << "\t $(INSTALL_PROGRAM) ";
  1571. }
  1572. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1573. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1574. fout << "\telse \\\n";
  1575. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1576. << " \"; \\\n";
  1577. fout << "\t fi\n";
  1578. }
  1579. }
  1580. break;
  1581. case cmTarget::UTILITY:
  1582. default:
  1583. break;
  1584. }
  1585. }
  1586. }
  1587. }
  1588. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1589. {
  1590. this->OutputMakeRule(fout,
  1591. "Default build rule",
  1592. "all",
  1593. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1594. 0);
  1595. this->OutputMakeRule(fout,
  1596. "remove generated files",
  1597. "clean",
  1598. "$(SUBDIR_CLEAN)",
  1599. "-@ $(RM) $(CLEAN_OBJECT_FILES) $(EXECUTABLES)"
  1600. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  1601. // collect up all the sources
  1602. std::string allsources;
  1603. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1604. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1605. target != targets.end(); ++target)
  1606. {
  1607. // Iterate over every source for this target.
  1608. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1609. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1610. source != sources.end(); ++source)
  1611. {
  1612. if(!source->IsAHeaderFileOnly())
  1613. {
  1614. allsources += " \\\n";
  1615. allsources += source->GetFullPath();
  1616. }
  1617. }
  1618. }
  1619. this->OutputMakeRule(fout,
  1620. "Rule to build the cmake.depends and Makefile as side effect, if a source cmakelist file is out of date.",
  1621. "cmake.depends",
  1622. "$(CMAKE_MAKEFILE_SOURCES)",
  1623. "$(CMAKE_COMMAND) "
  1624. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1625. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1626. );
  1627. this->OutputMakeRule(fout,
  1628. "Rule to build the cmake.check_depends and Makefile as side effect, if any source file has changed.",
  1629. "cmake.check_depends",
  1630. allsources.c_str(),
  1631. "$(CMAKE_COMMAND) "
  1632. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1633. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1634. );
  1635. this->OutputMakeRule(fout,
  1636. "Rule to force the build of cmake.depends",
  1637. "depend",
  1638. "$(SUBDIR_DEPEND)",
  1639. "$(CMAKE_COMMAND) "
  1640. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1641. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1642. this->OutputMakeRule(fout,
  1643. "Rule to force the build of cmake.depends "
  1644. "in the current directory only.",
  1645. "dependlocal",
  1646. 0,
  1647. "$(CMAKE_COMMAND) "
  1648. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1649. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1650. this->OutputMakeRule(fout,
  1651. "Rebuild CMakeCache.txt file",
  1652. "rebuild_cache",
  1653. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1654. "$(CMAKE_COMMAND) "
  1655. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1656. this->OutputMakeRule(fout,
  1657. "Create CMakeCache.txt file",
  1658. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1659. 0,
  1660. "$(CMAKE_COMMAND) "
  1661. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1662. this->OutputMakeRule(fout,
  1663. "Rule to keep make from removing Makefiles "
  1664. "if control-C is hit during a run of cmake.",
  1665. ".PRECIOUS",
  1666. "Makefile cmake.depends",
  1667. 0);
  1668. this->OutputSourceObjectBuildRules(fout);
  1669. // find ctest
  1670. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1671. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1672. ctest += "/";
  1673. ctest += "ctest";
  1674. ctest += cmSystemTools::GetExecutableExtension();
  1675. if(!cmSystemTools::FileExists(ctest.c_str()))
  1676. {
  1677. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1678. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1679. ctest += "/Debug/";
  1680. ctest += "ctest";
  1681. ctest += cmSystemTools::GetExecutableExtension();
  1682. }
  1683. if(!cmSystemTools::FileExists(ctest.c_str()))
  1684. {
  1685. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1686. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1687. ctest += "/Release/";
  1688. ctest += "ctest";
  1689. ctest += cmSystemTools::GetExecutableExtension();
  1690. }
  1691. if (cmSystemTools::FileExists(ctest.c_str()))
  1692. {
  1693. this->OutputMakeRule(fout,
  1694. "run any tests",
  1695. "test",
  1696. "",
  1697. ctest.c_str());
  1698. }
  1699. }
  1700. void
  1701. cmUnixMakefileGenerator::
  1702. OutputBuildObjectFromSource(std::ostream& fout,
  1703. const char* shortName,
  1704. const cmSourceFile& source,
  1705. const char* extraCompileFlags,
  1706. bool shared)
  1707. {
  1708. // Header files shouldn't have build rules.
  1709. if(source.IsAHeaderFileOnly())
  1710. return;
  1711. std::string comment = "Build ";
  1712. std::string objectFile = std::string(shortName) + m_ObjectFileExtension;
  1713. comment += objectFile + " From ";
  1714. comment += source.GetFullPath();
  1715. std::string compileCommand;
  1716. std::string ext = source.GetSourceExtension();
  1717. if(ext == "c" )
  1718. {
  1719. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  1720. compileCommand += extraCompileFlags;
  1721. if(shared)
  1722. {
  1723. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1724. }
  1725. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1726. compileCommand +=
  1727. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  1728. compileCommand += " -o ";
  1729. compileCommand += objectFile;
  1730. }
  1731. else
  1732. {
  1733. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  1734. compileCommand += extraCompileFlags;
  1735. if(shared)
  1736. {
  1737. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1738. }
  1739. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1740. compileCommand +=
  1741. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  1742. compileCommand += " -o ";
  1743. compileCommand += objectFile;
  1744. }
  1745. this->OutputMakeRule(fout,
  1746. comment.c_str(),
  1747. objectFile.c_str(),
  1748. cmSystemTools::EscapeSpaces(source.GetFullPath().
  1749. c_str()).c_str(),
  1750. compileCommand.c_str());
  1751. }
  1752. void cmUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  1753. {
  1754. fout << "# Rules to build " << m_ObjectFileExtension
  1755. << " files from their sources:\n";
  1756. std::set<std::string> rules;
  1757. // Iterate over every target.
  1758. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1759. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1760. target != targets.end(); ++target)
  1761. {
  1762. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  1763. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  1764. std::string exportsDef = "";
  1765. if(shared)
  1766. {
  1767. exportsDef = "-D"+target->first+"_EXPORTS ";
  1768. }
  1769. // Iterate over every source for this target.
  1770. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1771. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1772. source != sources.end(); ++source)
  1773. {
  1774. if(!source->IsAHeaderFileOnly())
  1775. {
  1776. std::string shortName;
  1777. std::string sourceName;
  1778. // If the full path to the source file includes this
  1779. // directory, we want to use the relative path for the
  1780. // filename of the object file. Otherwise, we will use just
  1781. // the filename portion.
  1782. if((cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  1783. || (cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->
  1784. GetCurrentOutputDirectory()) == 0))
  1785. {
  1786. sourceName = source->GetSourceName()+"."+source->GetSourceExtension();
  1787. shortName = source->GetSourceName();
  1788. // The path may be relative. See if a directory needs to be
  1789. // created for the output file. This is a ugly, and perhaps
  1790. // should be moved elsewhere.
  1791. std::string relPath =
  1792. cmSystemTools::GetFilenamePath(source->GetSourceName());
  1793. if(relPath != "")
  1794. {
  1795. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  1796. outPath += "/"+relPath;
  1797. cmSystemTools::MakeDirectory(outPath.c_str());
  1798. }
  1799. }
  1800. else
  1801. {
  1802. sourceName = source->GetFullPath();
  1803. shortName = cmSystemTools::GetFilenameName(source->GetSourceName());
  1804. }
  1805. std::string shortNameWithExt = shortName +
  1806. source->GetSourceExtension();
  1807. // Only output a rule for each .o once.
  1808. if(rules.find(shortNameWithExt) == rules.end())
  1809. {
  1810. this->OutputBuildObjectFromSource(fout,
  1811. shortName.c_str(),
  1812. *source,
  1813. exportsDef.c_str(),
  1814. shared);
  1815. rules.insert(shortNameWithExt);
  1816. }
  1817. }
  1818. }
  1819. }
  1820. }
  1821. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  1822. const char* comment,
  1823. const char* target,
  1824. const char* depends,
  1825. const char* command,
  1826. const char* command2,
  1827. const char* command3,
  1828. const char* command4)
  1829. {
  1830. if(!target)
  1831. {
  1832. cmSystemTools::Error("no target for OutputMakeRule");
  1833. return;
  1834. }
  1835. std::string replace;
  1836. if(comment)
  1837. {
  1838. replace = comment;
  1839. m_Makefile->ExpandVariablesInString(replace);
  1840. fout << "#---------------------------------------------------------\n";
  1841. fout << "# " << comment;
  1842. fout << "\n#\n";
  1843. }
  1844. fout << "\n";
  1845. replace = target;
  1846. m_Makefile->ExpandVariablesInString(replace);
  1847. fout << replace.c_str() << ": ";
  1848. if(depends)
  1849. {
  1850. replace = depends;
  1851. m_Makefile->ExpandVariablesInString(replace);
  1852. fout << replace.c_str();
  1853. }
  1854. fout << "\n";
  1855. if(command)
  1856. {
  1857. replace = command;
  1858. m_Makefile->ExpandVariablesInString(replace);
  1859. if(replace[0] != '-' && replace.find("echo") != 0
  1860. && replace.find("$(MAKE)") != 0)
  1861. {
  1862. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1863. }
  1864. fout << "\t" << replace.c_str() << "\n";
  1865. }
  1866. if(command2)
  1867. {
  1868. replace = command2;
  1869. m_Makefile->ExpandVariablesInString(replace);
  1870. if(replace[0] != '-' && replace.find("echo") != 0
  1871. && replace.find("$(MAKE)") != 0)
  1872. {
  1873. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1874. }
  1875. fout << "\t" << replace.c_str() << "\n";
  1876. }
  1877. if(command3)
  1878. {
  1879. replace = command3;
  1880. m_Makefile->ExpandVariablesInString(replace);
  1881. if(replace[0] != '-' && replace.find("echo") != 0
  1882. && replace.find("$(MAKE)") != 0)
  1883. {
  1884. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1885. }
  1886. fout << "\t" << replace.c_str() << "\n";
  1887. }
  1888. if(command4)
  1889. {
  1890. replace = command4;
  1891. m_Makefile->ExpandVariablesInString(replace);
  1892. if(replace[0] != '-' && replace.find("echo") != 0
  1893. && replace.find("$(MAKE)") != 0)
  1894. {
  1895. fout << "\t" << "echo \"" << replace.c_str() << "\"\n";
  1896. }
  1897. fout << "\t" << replace.c_str() << "\n";
  1898. }
  1899. fout << "\n";
  1900. }
  1901. void cmUnixMakefileGenerator::SetLocal (bool local)
  1902. {
  1903. if (local)
  1904. {
  1905. m_CacheOnly = false;
  1906. m_Recurse = false;
  1907. }
  1908. else
  1909. {
  1910. m_CacheOnly = true;
  1911. m_Recurse = true;
  1912. }
  1913. }
  1914. void cmUnixMakefileGenerator::ComputeSystemInfo()
  1915. {
  1916. if (m_CacheOnly)
  1917. {
  1918. // see man putenv for explaination of this stupid code....
  1919. static char envCXX[5000];
  1920. static char envCC[5000];
  1921. if(m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"))
  1922. {
  1923. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  1924. m_Makefile->ExpandVariablesInString(env);
  1925. strncpy(envCXX, env.c_str(), 4999);
  1926. envCXX[4999] = 0;
  1927. putenv(envCXX);
  1928. }
  1929. if(m_Makefile->GetDefinition("CMAKE_C_COMPILER"))
  1930. {
  1931. std::string env = "CC=${CMAKE_C_COMPILER}";
  1932. m_Makefile->ExpandVariablesInString(env);
  1933. strncpy(envCC, env.c_str(), 4999);
  1934. envCC[4999] = 0;
  1935. putenv(envCC);
  1936. }
  1937. // currently we run configure shell script here to determine the info
  1938. std::string output;
  1939. std::string cmd = "cd ";
  1940. cmd += cmSystemTools::EscapeSpaces(m_Makefile->GetHomeOutputDirectory());
  1941. cmd += "; ";
  1942. const char* root
  1943. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1944. cmd += root;
  1945. cmd += "/Templates/configure";
  1946. cmSystemTools::RunCommand(cmd.c_str(), output);
  1947. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  1948. }
  1949. // now load the settings
  1950. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  1951. fpath += "/CMakeSystemConfig.cmake";
  1952. m_Makefile->ReadListFile(NULL,fpath.c_str());
  1953. }