cmUnixMakefileGenerator.cxx 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmUnixMakefileGenerator.h"
  33. #include "cmMakefile.h"
  34. #include "cmStandardIncludes.h"
  35. #include "cmSystemTools.h"
  36. #include "cmSourceFile.h"
  37. #include "cmMakeDepend.h"
  38. #include "cmCacheManager.h"
  39. cmUnixMakefileGenerator::cmUnixMakefileGenerator()
  40. {
  41. m_CacheOnly = false;
  42. m_Recurse = false;
  43. }
  44. void cmUnixMakefileGenerator::GenerateMakefile()
  45. {
  46. if(m_CacheOnly)
  47. {
  48. // Generate the cache only stuff
  49. this->GenerateCacheOnly();
  50. // if recurse then generate for all sub- makefiles
  51. if(m_Recurse)
  52. {
  53. this->RecursiveGenerateCacheOnly();
  54. }
  55. }
  56. // normal makefile output
  57. else
  58. {
  59. // Generate depends
  60. cmMakeDepend md;
  61. md.SetMakefile(m_Makefile);
  62. md.DoDepends();
  63. // output the makefile fragment
  64. this->OutputMakefile("Makefile");
  65. }
  66. }
  67. // This is where CMakeTargets.make is generated
  68. void cmUnixMakefileGenerator::OutputMakefile(const char* file)
  69. {
  70. // Create sub directories fro aux source directories
  71. std::vector<std::string>& auxSourceDirs =
  72. m_Makefile->GetAuxSourceDirectories();
  73. if( auxSourceDirs.size() )
  74. {
  75. // For the case when this is running as a remote build
  76. // on unix, make the directory
  77. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  78. i != auxSourceDirs.end(); ++i)
  79. {
  80. cmSystemTools::MakeDirectory(i->c_str());
  81. }
  82. }
  83. std::ofstream fout(file);
  84. if(!fout)
  85. {
  86. cmSystemTools::Error("Error can not open for write: ", file);
  87. return;
  88. }
  89. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  90. << "# Generated from the following files:\n# "
  91. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  92. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  93. // sort the array
  94. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  95. // remove duplicates
  96. std::vector<std::string>::iterator new_end =
  97. std::unique(lfiles.begin(), lfiles.end());
  98. lfiles.erase(new_end, lfiles.end());
  99. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  100. i != lfiles.end(); ++i)
  101. {
  102. fout << "# " << i->c_str() << "\n";
  103. }
  104. fout << "\n\n";
  105. // create a make variable with all of the sources for this Makefile
  106. // for depend purposes.
  107. fout << "CMAKE_MAKEFILE_SOURCES = ";
  108. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  109. i != lfiles.end(); ++i)
  110. {
  111. fout << " " << i->c_str();
  112. }
  113. // Add the cache to the list
  114. fout << " " << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  115. fout << "\n\n";
  116. this->OutputMakeVariables(fout);
  117. this->OutputMakeFlags(fout);
  118. this->OutputTargetRules(fout);
  119. this->OutputDependencies(fout);
  120. this->OutputTargets(fout);
  121. this->OutputSubDirectoryRules(fout);
  122. std::string dependName = m_Makefile->GetStartOutputDirectory();
  123. dependName += "/cmake.depends";
  124. if(!this->m_CacheOnly)
  125. {
  126. std::ofstream dependout(dependName.c_str());
  127. if(!dependout)
  128. {
  129. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  130. return;
  131. }
  132. this->OutputObjectDepends(dependout);
  133. }
  134. this->OutputCustomRules(fout);
  135. this->OutputMakeRules(fout);
  136. this->OutputInstallRules(fout);
  137. // only add the depend include if the depend file exists
  138. if(cmSystemTools::FileExists(dependName.c_str()))
  139. {
  140. fout << "include cmake.depends\n";
  141. }
  142. }
  143. // Output the rules for any targets
  144. void cmUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  145. {
  146. // for each target add to the list of targets
  147. fout << "TARGETS = ";
  148. const cmTargets &tgts = m_Makefile->GetTargets();
  149. // list libraries first
  150. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  151. for(cmTargets::const_iterator l = tgts.begin();
  152. l != tgts.end(); l++)
  153. {
  154. if (l->second.GetType() == cmTarget::LIBRARY &&
  155. l->second.IsInAll())
  156. {
  157. fout << " \\\nlib" << l->first.c_str();
  158. if(dll)
  159. {
  160. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  161. }
  162. else
  163. {
  164. fout << ".a";
  165. }
  166. }
  167. }
  168. // executables
  169. for(cmTargets::const_iterator l = tgts.begin();
  170. l != tgts.end(); l++)
  171. {
  172. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  173. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  174. l->second.IsInAll())
  175. {
  176. fout << " \\\n" << l->first.c_str();
  177. }
  178. }
  179. // list utilities last
  180. for(cmTargets::const_iterator l = tgts.begin();
  181. l != tgts.end(); l++)
  182. {
  183. if (l->second.GetType() == cmTarget::UTILITY &&
  184. l->second.IsInAll())
  185. {
  186. fout << " \\\n" << l->first.c_str();
  187. }
  188. }
  189. fout << "\n\n";
  190. // get the classes from the source lists then add them to the groups
  191. for(cmTargets::const_iterator l = tgts.begin();
  192. l != tgts.end(); l++)
  193. {
  194. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  195. if (classes.begin() != classes.end())
  196. {
  197. fout << l->first << "_SRC_OBJS = ";
  198. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  199. i != classes.end(); i++)
  200. {
  201. if(!i->IsAHeaderFileOnly())
  202. {
  203. fout << "\\\n" << i->GetSourceName() << ".o ";
  204. }
  205. }
  206. fout << "\n\n";
  207. }
  208. }
  209. fout << "CLEAN_OBJECT_FILES = ";
  210. for(cmTargets::const_iterator l = tgts.begin();
  211. l != tgts.end(); l++)
  212. {
  213. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  214. if (classes.begin() != classes.end())
  215. {
  216. fout << "${" << l->first << "_SRC_OBJS} ";
  217. }
  218. }
  219. fout << "\n";
  220. }
  221. /**
  222. * Output the linking rules on a command line. For executables,
  223. * targetLibrary should be a NULL pointer. For libraries, it should point
  224. * to the name of the library. This will not link a library against itself.
  225. */
  226. void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  227. const char* targetLibrary,
  228. const cmTarget &tgt)
  229. {
  230. // collect all the flags needed for linking libraries
  231. std::string linkLibs;
  232. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  233. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  234. libDir != libdirs.end(); ++libDir)
  235. {
  236. std::string libpath = cmSystemTools::EscapeSpaces(libDir->c_str());
  237. if(libpath != "/usr/lib")
  238. {
  239. std::string::size_type pos = libDir->find("-L");
  240. if((pos == std::string::npos || pos > 0)
  241. && libDir->find("${") == std::string::npos)
  242. {
  243. linkLibs += "-L";
  244. }
  245. linkLibs += libpath;
  246. linkLibs += " ";
  247. }
  248. }
  249. std::string librariesLinked;
  250. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  251. cmRegularExpression reg("lib(.*)(\\.so$|\\.a|\\.sl$)");
  252. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  253. lib != libs.end(); ++lib)
  254. {
  255. // Don't link the library against itself!
  256. if(targetLibrary && (lib->first == targetLibrary)) continue;
  257. // don't look at debug libraries
  258. if (lib->second == cmTarget::DEBUG) continue;
  259. // skip zero size library entries, this may happen
  260. // if a variable expands to nothing.
  261. if (lib->first.size() == 0) continue;
  262. // if it is a full path break it into -L and -l
  263. cmRegularExpression reg("(^[ \t]*\\-l)|(\\${)");
  264. if(lib->first.find('/') != std::string::npos
  265. && !reg.find(lib->first))
  266. {
  267. std::string dir, file;
  268. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  269. dir, file);
  270. std::string libpath = cmSystemTools::EscapeSpaces(dir.c_str());
  271. if(libpath != "/usr/lib")
  272. {
  273. linkLibs += "-L";
  274. linkLibs += libpath;
  275. linkLibs += " ";
  276. }
  277. cmRegularExpression libname("lib(.*)\\.(.*)");
  278. if(libname.find(file))
  279. {
  280. librariesLinked += "-l";
  281. file = libname.match(1);
  282. librariesLinked += file;
  283. librariesLinked += " ";
  284. }
  285. }
  286. // not a full path, so add -l name
  287. else
  288. {
  289. if(!reg.find(lib->first))
  290. {
  291. librariesLinked += "-l";
  292. }
  293. librariesLinked += lib->first;
  294. librariesLinked += " ";
  295. }
  296. }
  297. linkLibs += librariesLinked;
  298. if(!targetLibrary)
  299. {
  300. // For executables, add these a second time so order does not matter
  301. linkLibs += librariesLinked;
  302. }
  303. fout << linkLibs;
  304. }
  305. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  306. {
  307. // for each target
  308. const cmTargets &tgts = m_Makefile->GetTargets();
  309. for(cmTargets::const_iterator l = tgts.begin();
  310. l != tgts.end(); l++)
  311. {
  312. if (l->second.GetType() == cmTarget::LIBRARY)
  313. {
  314. fout << "#---------------------------------------------------------\n";
  315. fout << "# rules for a library\n";
  316. fout << "#\n";
  317. fout << "lib" << l->first << ".a: ${" <<
  318. l->first << "_SRC_OBJS} \n";
  319. fout << "\t${CMAKE_AR} cr lib" << l->first << ".a ${" <<
  320. l->first << "_SRC_OBJS} \n";
  321. fout << "\t${CMAKE_RANLIB} lib" << l->first << ".a\n";
  322. fout << std::endl;
  323. fout << "lib" << l->first << "$(SHLIB_SUFFIX): ${" <<
  324. l->first << "_SRC_OBJS} \n";
  325. fout << "\trm -f lib" << l->first << "$(SHLIB_SUFFIX)\n";
  326. fout << "\t$(CMAKE_CXX_COMPILER) ${CMAKE_SHLIB_LINK_FLAGS} ${CMAKE_CXX_FLAGS} ${CMAKE_SHLIB_BUILD_FLAGS} -o \\\n";
  327. fout << "\t lib" << l->first << "$(SHLIB_SUFFIX) \\\n";
  328. fout << "\t ${" << l->first <<
  329. "_SRC_OBJS} ";
  330. this->OutputLinkLibraries(fout, l->first.c_str(), l->second);
  331. fout << "\n\n";
  332. }
  333. else if ((l->second.GetType() == cmTarget::EXECUTABLE)
  334. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  335. {
  336. fout << l->first << ": ${" <<
  337. l->first << "_SRC_OBJS} ${CMAKE_DEPEND_LIBS}\n";
  338. fout << "\t${CMAKE_CXX_COMPILER} ${CMAKE_CXXFLAGS} ${" <<
  339. l->first << "_SRC_OBJS} ";
  340. this->OutputLinkLibraries(fout, NULL,l->second);
  341. fout << " -o " << l->first << "\n\n";
  342. }
  343. }
  344. }
  345. // output the list of libraries that the executables
  346. // in this makefile will depend on.
  347. void cmUnixMakefileGenerator::OutputDependencies(std::ostream& fout)
  348. {
  349. // Each dependency should only be emitted once.
  350. std::set<std::string> emitted;
  351. fout << "CMAKE_DEPEND_LIBS = ";
  352. cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  353. cmTarget::LinkLibraries::const_iterator lib2;
  354. // Search the list of libraries that will be linked into
  355. // the executable
  356. emitted.clear();
  357. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  358. {
  359. // loop over the list of directories that the libraries might
  360. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  361. // be stored in the cache
  362. if( ! emitted.insert(lib2->first).second ) continue;
  363. const char* cacheValue
  364. = cmCacheManager::GetInstance()->GetCacheValue(lib2->first.c_str());
  365. if(cacheValue)
  366. {
  367. std::string libpath = cacheValue;
  368. libpath += "/lib";
  369. libpath += lib2->first;
  370. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  371. if(dll)
  372. {
  373. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  374. }
  375. else
  376. {
  377. libpath += ".a";
  378. }
  379. fout << libpath << " ";
  380. }
  381. }
  382. fout << "\n\n";
  383. emitted.clear();
  384. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  385. {
  386. // loop over the list of directories that the libraries might
  387. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  388. // be stored in the cache
  389. if( ! emitted.insert(lib2->first).second ) continue;
  390. const char* cacheValue
  391. = cmCacheManager::GetInstance()->GetCacheValue(lib2->first.c_str());
  392. if(cacheValue)
  393. {
  394. std::string library = "lib";
  395. library += lib2->first;
  396. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  397. if(dll)
  398. {
  399. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  400. }
  401. else
  402. {
  403. library += ".a";
  404. }
  405. std::string libpath = cacheValue;
  406. libpath += "/";
  407. libpath += library;
  408. // put out a rule to build the library if it does not exist
  409. fout << libpath.c_str()
  410. << ":\n\tcd " << cacheValue
  411. << "; make " << library.c_str() << "\n\n";
  412. }
  413. }
  414. std::vector<std::string>& utils = m_Makefile->GetUtilities();
  415. std::vector<std::string>& utildirs = m_Makefile->GetUtilityDirectories();
  416. std::vector<std::string>::iterator dir, util;
  417. // Search the list of utilities that may be used to generate code for
  418. // this project.
  419. for(util = utils.begin(); util != utils.end(); ++util)
  420. {
  421. bool found = false;
  422. // loop over the list of directories that the utilities might
  423. // be in, looking for an ADD_EXECUTABLE(util ...) line.
  424. for(dir = utildirs.begin(); dir != utildirs.end() && !found; ++dir)
  425. {
  426. std::string expression = "TARGETS =.*";
  427. expression += util->c_str();
  428. if(cmSystemTools::Grep(dir->c_str(), "Makefile",
  429. expression.c_str()))
  430. {
  431. fout << *util << " ";
  432. found = true;
  433. }
  434. }
  435. }
  436. fout << "\n";
  437. }
  438. // output make include flags
  439. void cmUnixMakefileGenerator::OutputMakeFlags(std::ostream& fout)
  440. {
  441. // Output Include paths
  442. fout << "INCLUDE_FLAGS = ";
  443. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  444. std::vector<std::string>::iterator i;
  445. fout << "-I" << m_Makefile->GetStartDirectory() << " ";
  446. for(i = includes.begin(); i != includes.end(); ++i)
  447. {
  448. std::string include = *i;
  449. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  450. }
  451. fout << m_Makefile->GetDefineFlags();
  452. fout << "\n\n";
  453. fout << "default_target: all\n\n";
  454. // see if there are files to compile in this makefile
  455. // These are used for both libraries and executables
  456. }
  457. // fix up names of directories so they can be used
  458. // as targets in makefiles.
  459. inline std::string FixDirectoryName(const char* dir)
  460. {
  461. std::string s = dir;
  462. // replace ../ with 3 under bars
  463. size_t pos = s.find("../");
  464. if(pos != std::string::npos)
  465. {
  466. s.replace(pos, 3, "___");
  467. }
  468. // replace / directory separators with a single under bar
  469. pos = s.find("/");
  470. while(pos != std::string::npos)
  471. {
  472. s.replace(pos, 1, "_");
  473. pos = s.find("/");
  474. }
  475. return s;
  476. }
  477. void
  478. cmUnixMakefileGenerator::
  479. OutputSubDirectoryVars(std::ostream& fout,
  480. const char* var,
  481. const char* target,
  482. const char* target1,
  483. const char* target2,
  484. const std::vector<std::string>& SubDirectories)
  485. {
  486. if( SubDirectories.size() == 0)
  487. {
  488. return;
  489. }
  490. fout << "# Variable for making " << target << " in subdirectories.\n";
  491. fout << var << " = \\\n";
  492. unsigned int i;
  493. for(i =0; i < SubDirectories.size(); i++)
  494. {
  495. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  496. fout << target << "_" << subdir.c_str();
  497. if(i == SubDirectories.size()-1)
  498. {
  499. fout << " \n\n";
  500. }
  501. else
  502. {
  503. fout << " \\\n";
  504. }
  505. }
  506. fout << "# Targets for making " << target << " in subdirectories.\n";
  507. for(unsigned int i =0; i < SubDirectories.size(); i++)
  508. {
  509. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  510. fout << target << "_" << subdir.c_str() << ":\n";
  511. if(target1)
  512. {
  513. fout << "\t@if test ! -d " << SubDirectories[i].c_str() << "; then ${MAKE} rebuild_cache; fi\n"
  514. "\tcd " << SubDirectories[i].c_str()
  515. << "; ${MAKE} -${MAKEFLAGS} " << target1 << "\n";
  516. }
  517. if(target2)
  518. {
  519. fout << "\t@cd " << SubDirectories[i].c_str()
  520. << "; ${MAKE} -${MAKEFLAGS} " << target2 << "\n";
  521. }
  522. }
  523. fout << "\n\n";
  524. }
  525. // output rules for decending into sub directories
  526. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  527. {
  528. // Output Sub directory build rules
  529. const std::vector<std::string>& SubDirectories
  530. = m_Makefile->GetSubDirectories();
  531. if( SubDirectories.size() == 0)
  532. {
  533. return;
  534. }
  535. this->OutputSubDirectoryVars(fout, "SUBDIR_BUILD", "build",
  536. "cmake.depends",
  537. "all",
  538. SubDirectories);
  539. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  540. "clean",
  541. 0,
  542. SubDirectories);
  543. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  544. "depend",
  545. 0,
  546. SubDirectories);
  547. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  548. "install",
  549. 0,
  550. SubDirectories);
  551. }
  552. // Output the depend information for all the classes
  553. // in the makefile. These would have been generated
  554. // by the class cmMakeDepend GenerateMakefile
  555. void cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  556. {
  557. // Iterate over every target.
  558. std::map<std::string, cmTarget>& targets = m_Makefile->GetTargets();
  559. for(std::map<std::string, cmTarget>::const_iterator target = targets.begin();
  560. target != targets.end(); ++target)
  561. {
  562. // Iterate over every source for this target.
  563. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  564. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  565. source != sources.end(); ++source)
  566. {
  567. if(!source->IsAHeaderFileOnly())
  568. {
  569. if(!source->GetDepends().empty())
  570. {
  571. fout << source->GetSourceName() << ".o :";
  572. // Iterate through all the dependencies for this source.
  573. for(std::vector<std::string>::const_iterator dep =
  574. source->GetDepends().begin();
  575. dep != source->GetDepends().end(); ++dep)
  576. {
  577. fout << " \\\n" << dep->c_str();
  578. }
  579. fout << "\n\n";
  580. }
  581. }
  582. }
  583. }
  584. }
  585. // Output each custom rule in the following format:
  586. // output: source depends...
  587. // (tab) command...
  588. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  589. {
  590. // We may be modifying the source groups temporarily, so make a copy.
  591. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  592. const cmTargets &tgts = m_Makefile->GetTargets();
  593. for(cmTargets::const_iterator tgt = tgts.begin();
  594. tgt != tgts.end(); ++tgt)
  595. {
  596. // add any custom rules to the source groups
  597. for (std::vector<cmCustomCommand>::const_iterator cr =
  598. tgt->second.GetCustomCommands().begin();
  599. cr != tgt->second.GetCustomCommands().end(); ++cr)
  600. {
  601. cmSourceGroup& sourceGroup =
  602. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  603. sourceGroups);
  604. cmCustomCommand cc(*cr);
  605. cc.ExpandVariables(*m_Makefile);
  606. sourceGroup.AddCustomCommand(cc);
  607. }
  608. }
  609. // Loop through every source group.
  610. for(std::vector<cmSourceGroup>::const_iterator sg =
  611. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  612. {
  613. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  614. if(buildRules.empty())
  615. { continue; }
  616. std::string name = sg->GetName();
  617. if(name != "")
  618. {
  619. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  620. }
  621. // Loop through each source in the source group.
  622. for(cmSourceGroup::BuildRules::const_iterator cc =
  623. buildRules.begin(); cc != buildRules.end(); ++ cc)
  624. {
  625. std::string source = cc->first;
  626. const cmSourceGroup::Commands& commands = cc->second;
  627. // Loop through every command generating code from the current source.
  628. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  629. c != commands.end(); ++c)
  630. {
  631. std::string command = c->first;
  632. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  633. // if the command has no outputs, then it is a utility command
  634. // with no outputs
  635. if(commandFiles.m_Outputs.size() == 0)
  636. {
  637. fout << source.c_str() << ": ";
  638. // Write out all the dependencies for this rule.
  639. for(std::set<std::string>::const_iterator d =
  640. commandFiles.m_Depends.begin();
  641. d != commandFiles.m_Depends.end(); ++d)
  642. {
  643. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  644. fout << " " << dep.c_str();
  645. }
  646. fout << "\n\t" << command.c_str() << "\n\n";
  647. }
  648. // Write a rule for every output generated by this command.
  649. for(std::set<std::string>::const_iterator output =
  650. commandFiles.m_Outputs.begin();
  651. output != commandFiles.m_Outputs.end(); ++output)
  652. {
  653. std::string src = cmSystemTools::EscapeSpaces(source.c_str());
  654. fout << output->c_str() << ": " << src.c_str();
  655. // Write out all the dependencies for this rule.
  656. for(std::set<std::string>::const_iterator d =
  657. commandFiles.m_Depends.begin();
  658. d != commandFiles.m_Depends.end(); ++d)
  659. {
  660. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  661. fout << " " << dep.c_str();
  662. }
  663. fout << "\n\t" << command.c_str() << "\n\n";
  664. }
  665. }
  666. }
  667. if(name != "")
  668. {
  669. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  670. }
  671. }
  672. }
  673. void cmUnixMakefileGenerator::GenerateCacheOnly()
  674. {
  675. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  676. std::string dest = m_Makefile->GetStartOutputDirectory();
  677. dest += "/Makefile";
  678. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  679. this->OutputMakefile(dest.c_str());
  680. return;
  681. }
  682. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  683. {
  684. std::vector<cmMakefile*> makefiles;
  685. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  686. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  687. i != makefiles.end(); ++i)
  688. {
  689. cmMakefile* mf = *i;
  690. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  691. {
  692. mf->AddDefinition("RUN_CONFIGURE", true);
  693. }
  694. cmUnixMakefileGenerator* gen = new cmUnixMakefileGenerator;
  695. gen->SetCacheOnlyOn();
  696. gen->SetRecurseOff();
  697. mf->SetMakefileGenerator(gen);
  698. mf->GenerateMakefile();
  699. }
  700. // CLEAN up the makefiles created
  701. for(unsigned int i =0; i < makefiles.size(); ++i)
  702. {
  703. delete makefiles[i];
  704. }
  705. }
  706. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  707. {
  708. if(strcmp(m_Makefile->GetHomeDirectory(),
  709. m_Makefile->GetHomeOutputDirectory()) == 0)
  710. {
  711. fout << "srcdir = .\n\n";
  712. }
  713. else
  714. {
  715. fout << "srcdir = " << m_Makefile->GetStartDirectory() << "\n";
  716. fout << "VPATH = " << m_Makefile->GetStartDirectory() << "\n";
  717. }
  718. const char* variables =
  719. "# the standard shell for make\n"
  720. "SHELL = /bin/sh\n"
  721. "\n"
  722. "CMAKE_LIB_EXT = @CMAKE_LIB_EXT@\n"
  723. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  724. "CMAKE_AR = @CMAKE_AR@\n"
  725. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  726. "CMAKE_CFLAGS = @CMAKE_C_FLAGS@ @CMAKE_SHLIB_CFLAGS@ \n"
  727. "\n"
  728. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  729. "CMAKE_CXXFLAGS = @CMAKE_CXX_FLAGS@ @CMAKE_SHLIB_CFLAGS@ @CMAKE_TEMPLATE_FLAGS@ \n"
  730. "\n"
  731. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  732. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  733. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  734. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  735. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  736. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  737. "\n"
  738. "# set up the path to the rulesgen program\n"
  739. "CMAKE_COMMAND = ${CMAKE_COMMAND}\n"
  740. "\n"
  741. "\n"
  742. "\n";
  743. std::string replaceVars = variables;
  744. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  745. if(!dll)
  746. {
  747. // if not a dll then remove the shlib -fpic flag
  748. m_Makefile->AddDefinition("CMAKE_SHLIB_CFLAGS", "");
  749. }
  750. m_Makefile->ExpandVariablesInString(replaceVars);
  751. fout << replaceVars.c_str();
  752. fout << "CMAKE_CURRENT_SOURCE = " << m_Makefile->GetStartDirectory() << "\n";
  753. fout << "CMAKE_CURRENT_BINARY = " << m_Makefile->GetStartOutputDirectory() << "\n";
  754. }
  755. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  756. {
  757. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  758. const char* root
  759. = cmCacheManager::GetInstance()->GetCacheValue("CMAKE_ROOT");
  760. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  761. fout << "INSTALL_PROGRAM = ${INSTALL}\n";
  762. fout << "INSTALL_DATA = ${INSTALL} -m 644\n";
  763. const cmTargets &tgts = m_Makefile->GetTargets();
  764. fout << "install: ${SUBDIR_INSTALL}\n";
  765. fout << "\t@echo \"Installing ...\"\n";
  766. const char* prefix
  767. = cmCacheManager::GetInstance()->GetCacheValue("CMAKE_INSTALL_PREFIX");
  768. if (!prefix)
  769. {
  770. prefix = "/usr/local";
  771. }
  772. for(cmTargets::const_iterator l = tgts.begin();
  773. l != tgts.end(); l++)
  774. {
  775. if (l->second.GetInstallPath() != "")
  776. {
  777. // first make the directories for each target
  778. fout << "\t@if [ ! -d " << prefix << l->second.GetInstallPath() <<
  779. " ] ; then \\\n";
  780. fout << "\t echo \"Making directory " << prefix
  781. << l->second.GetInstallPath() << " \"; \\\n";
  782. fout << "\t mkdir -p " << prefix << l->second.GetInstallPath()
  783. << "; \\\n";
  784. fout << "\t chmod 755 " << prefix << l->second.GetInstallPath()
  785. << "; \\\n";
  786. fout << "\t else true; \\\n";
  787. fout << "\t fi\n";
  788. // now install the target
  789. switch (l->second.GetType())
  790. {
  791. case cmTarget::LIBRARY:
  792. fout << "\t$(INSTALL_DATA) lib" << l->first;
  793. if(dll)
  794. {
  795. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  796. }
  797. else
  798. {
  799. fout << ".a";
  800. }
  801. fout << " " << prefix << l->second.GetInstallPath() << "\n";
  802. break;
  803. case cmTarget::EXECUTABLE:
  804. fout << "\t$(INSTALL_PROGRAM) " << l->first
  805. << " " << prefix << l->second.GetInstallPath() << "\n";
  806. break;
  807. case cmTarget::INSTALL:
  808. {
  809. const std::vector<std::string> &sf = l->second.GetSourceLists();
  810. std::vector<std::string>::const_iterator i;
  811. for (i = sf.begin(); i != sf.end(); ++i)
  812. {
  813. fout << "\t@ echo \"Installing " << *i << " \"\n";
  814. fout << "\t@if [ -e " << *i << " ] ; then \\\n";
  815. fout << "\t $(INSTALL_DATA) " << *i
  816. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  817. fout << "\t elif [ -e ${srcdir}/" << *i << " ] ; then \\\n";
  818. fout << "\t $(INSTALL_DATA) ${srcdir}/" << *i
  819. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  820. fout << "\telse \\\n";
  821. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  822. << " \"; \\\n";
  823. fout << "\t fi\n";
  824. }
  825. }
  826. break;
  827. }
  828. }
  829. }
  830. }
  831. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  832. {
  833. this->OutputMakeRule(fout,
  834. "# tell make about .cxx and .java",
  835. ".SUFFIXES", ".cxx .java .class", 0);
  836. this->OutputMakeRule(fout,
  837. "# build c file",
  838. ".c.o",
  839. 0,
  840. "${CMAKE_C_COMPILER} ${CMAKE_CFLAGS} ${INCLUDE_FLAGS} -c $< -o $@");
  841. this->OutputMakeRule(fout,
  842. "# build cplusplus file",
  843. ".cxx.o",
  844. 0,
  845. "${CMAKE_CXX_COMPILER} ${CMAKE_CXXFLAGS} ${INCLUDE_FLAGS} -c $< -o $@");
  846. this->OutputMakeRule(fout,
  847. "Default build rule",
  848. "all",
  849. "Makefile cmake.depends ${TARGETS} ${SUBDIR_BUILD} ${CMAKE_COMMAND}",
  850. 0);
  851. this->OutputMakeRule(fout,
  852. "remove generated files",
  853. "clean",
  854. "${SUBDIR_CLEAN}",
  855. "rm -f ${CLEAN_OBJECT_FILES} ${EXECUTABLES} ${TARGETS}");
  856. this->OutputMakeRule(fout,
  857. "Rule to build the Makefile",
  858. "Makefile",
  859. "${CMAKE_COMMAND} ${CMAKE_MAKEFILE_SOURCES} ",
  860. "${CMAKE_COMMAND} "
  861. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  862. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  863. this->OutputMakeRule(fout,
  864. "Rule to build the cmake.depends",
  865. "cmake.depends",
  866. "${CMAKE_COMMAND} ${CMAKE_MAKEFILE_SOURCES} ",
  867. "${CMAKE_COMMAND} "
  868. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  869. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  870. this->OutputMakeRule(fout,
  871. "Rule to force the build of cmake.depends",
  872. "depend",
  873. "${SUBDIR_DEPEND}",
  874. "${CMAKE_COMMAND} "
  875. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  876. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  877. this->OutputMakeRule(fout,
  878. "Rebuild the cache",
  879. "rebuild_cache",
  880. "${CMAKE_BINARY_DIR}/CMakeCache.txt",
  881. "${CMAKE_COMMAND} "
  882. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  883. this->OutputMakeRule(fout,
  884. "Rebuild the cache",
  885. "${CMAKE_BINARY_DIR}/CMakeCache.txt",
  886. 0,
  887. "${CMAKE_COMMAND} "
  888. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  889. this->OutputMakeRule(fout,
  890. "Rebuild cmake dummy rule",
  891. "${CMAKE_COMMAND}",
  892. 0,
  893. "echo \"cmake might be out of date\"");
  894. }
  895. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  896. const char* comment,
  897. const char* target,
  898. const char* depends,
  899. const char* command)
  900. {
  901. if(!target)
  902. {
  903. cmSystemTools::Error("no target for OutputMakeRule");
  904. return;
  905. }
  906. std::string replace;
  907. if(comment)
  908. {
  909. replace = comment;
  910. m_Makefile->ExpandVariablesInString(replace);
  911. fout << "# " << comment;
  912. }
  913. fout << "\n";
  914. replace = target;
  915. m_Makefile->ExpandVariablesInString(replace);
  916. fout << replace.c_str() << ": ";
  917. if(depends)
  918. {
  919. replace = depends;
  920. m_Makefile->ExpandVariablesInString(replace);
  921. fout << replace.c_str();
  922. }
  923. fout << "\n";
  924. if(command)
  925. {
  926. replace = command;
  927. m_Makefile->ExpandVariablesInString(replace);
  928. fout << "\t" << replace.c_str() << "\n\n";
  929. }
  930. fout << "\n\n\n";
  931. }
  932. void cmUnixMakefileGenerator::SetLocal (bool local)
  933. {
  934. if (local)
  935. {
  936. m_CacheOnly = false;
  937. m_Recurse = false;
  938. }
  939. else
  940. {
  941. m_CacheOnly = true;
  942. m_Recurse = true;
  943. }
  944. }
  945. void cmUnixMakefileGenerator::ComputeSystemInfo()
  946. {
  947. if (m_CacheOnly)
  948. {
  949. // currently we run configure shell script here to determine the info
  950. std::string output;
  951. std::string cmd;
  952. const char* root
  953. = cmCacheManager::GetInstance()->GetCacheValue("CMAKE_ROOT");
  954. cmd = root;
  955. cmd += "/Templates/configure";
  956. cmSystemTools::RunCommand(cmd.c_str(), output);
  957. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  958. }
  959. // now load the settings
  960. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  961. fpath += "/CMakeSystemConfig.cmake";
  962. m_Makefile->ReadListFile(NULL,fpath.c_str());
  963. }