cmUnixMakefileGenerator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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;
  123. if(!this->m_CacheOnly)
  124. {
  125. dependName = m_Makefile->GetStartOutputDirectory();
  126. dependName += "/cmake.depends";
  127. std::ofstream dependout(dependName.c_str());
  128. if(!dependout)
  129. {
  130. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  131. return;
  132. }
  133. this->OutputObjectDepends(dependout);
  134. }
  135. this->OutputCustomRules(fout);
  136. this->OutputMakeRules(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. fout << l->first << "_SRC_OBJS = ";
  196. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  197. i != classes.end(); i++)
  198. {
  199. if(!i->IsAHeaderFileOnly())
  200. {
  201. fout << "\\\n" << i->GetSourceName() << ".o ";
  202. }
  203. }
  204. fout << "\n\n";
  205. }
  206. fout << "CLEAN_OBJECT_FILES = ";
  207. for(cmTargets::const_iterator l = tgts.begin();
  208. l != tgts.end(); l++)
  209. {
  210. fout << "${" << l->first << "_SRC_OBJS} ";
  211. }
  212. fout << "\n";
  213. }
  214. /**
  215. * Output the linking rules on a command line. For executables,
  216. * targetLibrary should be a NULL pointer. For libraries, it should point
  217. * to the name of the library. This will not link a library against itself.
  218. */
  219. void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  220. const char* targetLibrary,
  221. const cmTarget &tgt)
  222. {
  223. // collect all the flags needed for linking libraries
  224. std::string linkLibs;
  225. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  226. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  227. libDir != libdirs.end(); ++libDir)
  228. {
  229. std::string::size_type pos = libDir->find("-L");
  230. if((pos == std::string::npos || pos > 0)
  231. && libDir->find("${") == std::string::npos)
  232. {
  233. linkLibs += "-L";
  234. }
  235. linkLibs += cmSystemTools::EscapeSpaces(libDir->c_str());
  236. linkLibs += " ";
  237. }
  238. std::string librariesLinked;
  239. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  240. cmRegularExpression reg("lib(.*)(\\.so$|\\.a|\\.sl$)");
  241. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  242. lib != libs.end(); ++lib)
  243. {
  244. // Don't link the library against itself!
  245. if(targetLibrary && (lib->first == targetLibrary)) continue;
  246. // don't look at debug libraries
  247. if (lib->second == cmTarget::DEBUG) continue;
  248. // skip zero size library entries, this may happen
  249. // if a variable expands to nothing.
  250. if (lib->first.size() == 0) continue;
  251. // if it is a full path break it into -L and -l
  252. if(lib->first.find('/') != std::string::npos)
  253. {
  254. std::string dir, file;
  255. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  256. dir, file);
  257. linkLibs += "-L";
  258. linkLibs += cmSystemTools::EscapeSpaces(dir.c_str());
  259. linkLibs += " ";
  260. librariesLinked += "-l";
  261. if(reg.find(file))
  262. {
  263. file = reg.match(1);
  264. }
  265. librariesLinked += file;
  266. librariesLinked += " ";
  267. }
  268. // not a full path, so add -l name
  269. else
  270. {
  271. std::string::size_type pos = lib->first.find("-l");
  272. if((pos == std::string::npos || pos > 0)
  273. && lib->first.find("${") == std::string::npos)
  274. {
  275. librariesLinked += "-l";
  276. }
  277. librariesLinked += lib->first;
  278. librariesLinked += " ";
  279. }
  280. }
  281. linkLibs += librariesLinked;
  282. if(!targetLibrary)
  283. {
  284. // For executables, add these a second time so order does not matter
  285. linkLibs += librariesLinked;
  286. }
  287. fout << linkLibs;
  288. }
  289. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  290. {
  291. // for each target
  292. const cmTargets &tgts = m_Makefile->GetTargets();
  293. for(cmTargets::const_iterator l = tgts.begin();
  294. l != tgts.end(); l++)
  295. {
  296. if (l->second.GetType() == cmTarget::LIBRARY)
  297. {
  298. fout << "#---------------------------------------------------------\n";
  299. fout << "# rules for a library\n";
  300. fout << "#\n";
  301. fout << "lib" << l->first << ".a: ${" <<
  302. l->first << "_SRC_OBJS} \n";
  303. fout << "\t${CMAKE_AR} cr lib" << l->first << ".a ${" <<
  304. l->first << "_SRC_OBJS} \n";
  305. fout << "\t${CMAKE_RANLIB} lib" << l->first << ".a\n";
  306. fout << std::endl;
  307. fout << "lib" << l->first << "$(SHLIB_SUFFIX): ${" <<
  308. l->first << "_SRC_OBJS} \n";
  309. fout << "\trm -f lib" << l->first << "$(SHLIB_SUFFIX)\n";
  310. fout << "\t$(CMAKE_CXX) ${CMAKE_SHLIB_LINK_FLAGS} ${CMAKE_CXX_FLAGS} ${CMAKE_SHLIB_BUILD_FLAGS} -o \\\n";
  311. fout << "\t lib" << l->first << "$(SHLIB_SUFFIX) \\\n";
  312. fout << "\t ${" << l->first <<
  313. "_SRC_OBJS} ";
  314. this->OutputLinkLibraries(fout, l->first.c_str(), l->second);
  315. fout << "\n\n";
  316. }
  317. else if (l->second.GetType() == cmTarget::EXECUTABLE)
  318. {
  319. fout << l->first << ": ${" <<
  320. l->first << "_SRC_OBJS} ${CMAKE_DEPEND_LIBS}\n";
  321. fout << "\t${CMAKE_CXX} ${CMAKE_CXXFLAGS} ${" <<
  322. l->first << "_SRC_OBJS} ";
  323. this->OutputLinkLibraries(fout, NULL,l->second);
  324. fout << " -o " << l->first << "\n\n";
  325. }
  326. }
  327. }
  328. // output the list of libraries that the executables
  329. // in this makefile will depend on.
  330. void cmUnixMakefileGenerator::OutputDependencies(std::ostream& fout)
  331. {
  332. fout << "CMAKE_DEPEND_LIBS = ";
  333. cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  334. cmTarget::LinkLibraries::const_iterator lib2;
  335. // Search the list of libraries that will be linked into
  336. // the executable
  337. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  338. {
  339. // loop over the list of directories that the libraries might
  340. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  341. // be stored in the cache
  342. const char* cacheValue
  343. = cmCacheManager::GetInstance()->GetCacheValue(lib2->first.c_str());
  344. if(cacheValue)
  345. {
  346. std::string libpath = cacheValue;
  347. libpath += "/lib";
  348. libpath += lib2->first;
  349. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  350. if(dll)
  351. {
  352. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  353. }
  354. else
  355. {
  356. libpath += ".a";
  357. }
  358. fout << libpath << " ";
  359. }
  360. }
  361. fout << "\n\n";
  362. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  363. {
  364. // loop over the list of directories that the libraries might
  365. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  366. // be stored in the cache
  367. const char* cacheValue
  368. = cmCacheManager::GetInstance()->GetCacheValue(lib2->first.c_str());
  369. if(cacheValue)
  370. {
  371. std::string library = "lib";
  372. library += lib2->first;
  373. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  374. if(dll)
  375. {
  376. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  377. }
  378. else
  379. {
  380. library += ".a";
  381. }
  382. std::string libpath = cacheValue;
  383. libpath += "/";
  384. libpath += library;
  385. // put out a rule to build the library if it does not exist
  386. fout << libpath.c_str()
  387. << ":\n\tcd " << cacheValue
  388. << "; make " << library.c_str() << "\n\n";
  389. }
  390. }
  391. std::vector<std::string>& utils = m_Makefile->GetUtilities();
  392. std::vector<std::string>& utildirs = m_Makefile->GetUtilityDirectories();
  393. std::vector<std::string>::iterator dir, util;
  394. // Search the list of utilities that may be used to generate code for
  395. // this project.
  396. for(util = utils.begin(); util != utils.end(); ++util)
  397. {
  398. bool found = false;
  399. // loop over the list of directories that the utilities might
  400. // be in, looking for an ADD_EXECUTABLE(util ...) line.
  401. for(dir = utildirs.begin(); dir != utildirs.end() && !found; ++dir)
  402. {
  403. std::string expression = "TARGETS =.*";
  404. expression += util->c_str();
  405. if(cmSystemTools::Grep(dir->c_str(), "Makefile",
  406. expression.c_str()))
  407. {
  408. fout << *util << " ";
  409. found = true;
  410. }
  411. }
  412. }
  413. fout << "\n";
  414. }
  415. // output make include flags
  416. void cmUnixMakefileGenerator::OutputMakeFlags(std::ostream& fout)
  417. {
  418. // Output Include paths
  419. fout << "INCLUDE_FLAGS = ";
  420. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  421. std::vector<std::string>::iterator i;
  422. fout << "-I" << m_Makefile->GetStartDirectory() << " ";
  423. for(i = includes.begin(); i != includes.end(); ++i)
  424. {
  425. std::string include = *i;
  426. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  427. }
  428. fout << m_Makefile->GetDefineFlags();
  429. fout << "\n\n";
  430. fout << "default_target: all\n\n";
  431. // see if there are files to compile in this makefile
  432. // These are used for both libraries and executables
  433. }
  434. // fix up names of directories so they can be used
  435. // as targets in makefiles.
  436. inline std::string FixDirectoryName(const char* dir)
  437. {
  438. std::string s = dir;
  439. // replace ../ with 3 under bars
  440. size_t pos = s.find("../");
  441. if(pos != std::string::npos)
  442. {
  443. s.replace(pos, 3, "___");
  444. }
  445. // replace / directory separators with a single under bar
  446. pos = s.find("/");
  447. while(pos != std::string::npos)
  448. {
  449. s.replace(pos, 1, "_");
  450. pos = s.find("/");
  451. }
  452. return s;
  453. }
  454. void
  455. cmUnixMakefileGenerator::
  456. OutputSubDirectoryVars(std::ostream& fout,
  457. const char* var,
  458. const char* target,
  459. const char* target1,
  460. const char* target2,
  461. const std::vector<std::string>& SubDirectories)
  462. {
  463. if( SubDirectories.size() == 0)
  464. {
  465. return;
  466. }
  467. fout << "# Variable for making " << target << " in subdirectories.\n";
  468. fout << var << " = \\\n";
  469. unsigned int i;
  470. for(i =0; i < SubDirectories.size(); i++)
  471. {
  472. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  473. fout << target << "_" << subdir.c_str();
  474. if(i == SubDirectories.size()-1)
  475. {
  476. fout << " \n\n";
  477. }
  478. else
  479. {
  480. fout << " \\\n";
  481. }
  482. }
  483. fout << "# Targets for making " << target << " in subdirectories.\n";
  484. for(int i =0; i < SubDirectories.size(); i++)
  485. {
  486. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  487. fout << target << "_" << subdir.c_str() << ":\n";
  488. if(target1)
  489. {
  490. fout << "\tcd " << SubDirectories[i].c_str()
  491. << "; ${MAKE} -${MAKEFLAGS} " << target1 << "\n";
  492. }
  493. if(target2)
  494. {
  495. fout << "\tcd " << SubDirectories[i].c_str()
  496. << "; ${MAKE} -${MAKEFLAGS} " << target2 << "\n";
  497. }
  498. }
  499. fout << "\n\n";
  500. }
  501. // output rules for decending into sub directories
  502. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  503. {
  504. // Output Sub directory build rules
  505. const std::vector<std::string>& SubDirectories
  506. = m_Makefile->GetSubDirectories();
  507. if( SubDirectories.size() == 0)
  508. {
  509. return;
  510. }
  511. this->OutputSubDirectoryVars(fout, "SUBDIR_BUILD", "build",
  512. "cmake.depends",
  513. "all",
  514. SubDirectories);
  515. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  516. "clean",
  517. 0,
  518. SubDirectories);
  519. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  520. "depend",
  521. 0,
  522. SubDirectories);
  523. }
  524. // Output the depend information for all the classes
  525. // in the makefile. These would have been generated
  526. // by the class cmMakeDepend GenerateMakefile
  527. void cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  528. {
  529. // Iterate over every target.
  530. std::map<std::string, cmTarget>& targets = m_Makefile->GetTargets();
  531. for(std::map<std::string, cmTarget>::const_iterator target = targets.begin();
  532. target != targets.end(); ++target)
  533. {
  534. // Iterate over every source for this target.
  535. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  536. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  537. source != sources.end(); ++source)
  538. {
  539. if(!source->IsAHeaderFileOnly())
  540. {
  541. if(!source->GetDepends().empty())
  542. {
  543. fout << source->GetSourceName() << ".o :";
  544. // Iterate through all the dependencies for this source.
  545. for(std::vector<std::string>::const_iterator dep =
  546. source->GetDepends().begin();
  547. dep != source->GetDepends().end(); ++dep)
  548. {
  549. fout << " \\\n" << dep->c_str();
  550. }
  551. fout << "\n\n";
  552. }
  553. }
  554. }
  555. }
  556. }
  557. // Output each custom rule in the following format:
  558. // output: source depends...
  559. // (tab) command...
  560. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  561. {
  562. // We may be modifying the source groups temporarily, so make a copy.
  563. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  564. const cmTargets &tgts = m_Makefile->GetTargets();
  565. for(cmTargets::const_iterator tgt = tgts.begin();
  566. tgt != tgts.end(); ++tgt)
  567. {
  568. // add any custom rules to the source groups
  569. for (std::vector<cmCustomCommand>::const_iterator cr =
  570. tgt->second.GetCustomCommands().begin();
  571. cr != tgt->second.GetCustomCommands().end(); ++cr)
  572. {
  573. cmSourceGroup& sourceGroup =
  574. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  575. sourceGroups);
  576. cmCustomCommand cc(*cr);
  577. cc.ExpandVariables(*m_Makefile);
  578. sourceGroup.AddCustomCommand(cc);
  579. }
  580. }
  581. // Loop through every source group.
  582. for(std::vector<cmSourceGroup>::const_iterator sg =
  583. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  584. {
  585. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  586. if(buildRules.empty())
  587. { continue; }
  588. std::string name = sg->GetName();
  589. if(name != "")
  590. {
  591. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  592. }
  593. // Loop through each source in the source group.
  594. for(cmSourceGroup::BuildRules::const_iterator cc =
  595. buildRules.begin(); cc != buildRules.end(); ++ cc)
  596. {
  597. std::string source = cc->first;
  598. const cmSourceGroup::Commands& commands = cc->second;
  599. // Loop through every command generating code from the current source.
  600. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  601. c != commands.end(); ++c)
  602. {
  603. std::string command = c->first;
  604. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  605. // if the command has no outputs, then it is a utility command
  606. // with no outputs
  607. if(commandFiles.m_Outputs.size() == 0)
  608. {
  609. fout << source.c_str() << ": ";
  610. // Write out all the dependencies for this rule.
  611. for(std::set<std::string>::const_iterator d =
  612. commandFiles.m_Depends.begin();
  613. d != commandFiles.m_Depends.end(); ++d)
  614. {
  615. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  616. fout << " " << dep.c_str();
  617. }
  618. fout << "\n\t" << command.c_str() << "\n\n";
  619. }
  620. // Write a rule for every output generated by this command.
  621. for(std::set<std::string>::const_iterator output =
  622. commandFiles.m_Outputs.begin();
  623. output != commandFiles.m_Outputs.end(); ++output)
  624. {
  625. std::string src = cmSystemTools::EscapeSpaces(source.c_str());
  626. fout << output->c_str() << ": " << src.c_str();
  627. // Write out all the dependencies for this rule.
  628. for(std::set<std::string>::const_iterator d =
  629. commandFiles.m_Depends.begin();
  630. d != commandFiles.m_Depends.end(); ++d)
  631. {
  632. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  633. fout << " " << dep.c_str();
  634. }
  635. fout << "\n\t" << command.c_str() << "\n\n";
  636. }
  637. }
  638. }
  639. if(name != "")
  640. {
  641. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  642. }
  643. }
  644. }
  645. void cmUnixMakefileGenerator::GenerateCacheOnly()
  646. {
  647. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  648. std::string dest = m_Makefile->GetStartOutputDirectory();
  649. dest += "/Makefile";
  650. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  651. this->OutputMakefile(dest.c_str());
  652. return;
  653. }
  654. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  655. {
  656. std::vector<cmMakefile*> makefiles;
  657. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  658. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  659. i != makefiles.end(); ++i)
  660. {
  661. cmMakefile* mf = *i;
  662. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  663. {
  664. mf->AddDefinition("RUN_CONFIGURE", true);
  665. }
  666. cmUnixMakefileGenerator* gen = new cmUnixMakefileGenerator;
  667. gen->SetCacheOnlyOn();
  668. gen->SetRecurseOff();
  669. mf->SetMakefileGenerator(gen);
  670. mf->GenerateMakefile();
  671. }
  672. // CLEAN up the makefiles created
  673. for(unsigned int i =0; i < makefiles.size(); ++i)
  674. {
  675. delete makefiles[i];
  676. }
  677. }
  678. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  679. {
  680. if(strcmp(m_Makefile->GetHomeDirectory(),
  681. m_Makefile->GetHomeOutputDirectory()) == 0)
  682. {
  683. fout << "srcdir = .\n\n";
  684. }
  685. else
  686. {
  687. fout << "srcdir = " << m_Makefile->GetStartDirectory() << "\n";
  688. fout << "VPATH = " << m_Makefile->GetStartDirectory() << "\n";
  689. }
  690. const char* variables =
  691. "# the standard shell for make\n"
  692. "SHELL = /bin/sh\n"
  693. "\n"
  694. "CMAKE_LIB_EXT = @CMAKE_LIB_EXT@\n"
  695. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  696. "CMAKE_AR = @CMAKE_AR@\n"
  697. "CMAKE_CC = @CMAKE_CC@\n"
  698. "CMAKE_CFLAGS = @CMAKE_CC_FLAGS@ @CMAKE_SHLIB_CFLAGS@ \n"
  699. "\n"
  700. "CMAKE_CXX = @CMAKE_CXX@\n"
  701. "CMAKE_CXXFLAGS = @CMAKE_CXX_FLAGS@ @CMAKE_SHLIB_CFLAGS@ @CMAKE_TEMPLATE_FLAGS@ \n"
  702. "\n"
  703. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  704. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  705. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  706. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  707. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  708. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  709. "\n"
  710. "# set up the path to the rulesgen program\n"
  711. "CMAKE = ${CMAKE_BINARY_DIR}/CMake/Source/CMakeBuildTargets\n"
  712. "\n"
  713. "\n"
  714. "\n";
  715. std::string replaceVars = variables;
  716. m_Makefile->ExpandVariablesInString(replaceVars);
  717. fout << replaceVars.c_str();
  718. fout << "CMAKE_CURRENT_SOURCE = " << m_Makefile->GetStartDirectory() << "\n";
  719. fout << "CMAKE_CURRENT_BINARY = " << m_Makefile->GetStartOutputDirectory() << "\n";
  720. }
  721. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  722. {
  723. this->OutputMakeRule(fout,
  724. "# tell make about .cxx and .java",
  725. ".SUFFIXES", ".cxx .java .class", 0);
  726. this->OutputMakeRule(fout,
  727. "# build c file",
  728. ".c.o",
  729. 0,
  730. "${CMAKE_CC} ${CMAKE_CFLAGS} ${INCLUDE_FLAGS} -c $< -o $@");
  731. this->OutputMakeRule(fout,
  732. "# build cplusplus file",
  733. ".cxx.o",
  734. 0,
  735. "${CMAKE_CXX} ${CMAKE_CXXFLAGS} ${INCLUDE_FLAGS} -c $< -o $@"); this->OutputMakeRule(fout,
  736. "Default build rule",
  737. "all",
  738. "Makefile cmake.depends ${TARGETS} ${SUBDIR_BUILD} ${CMAKE}",
  739. 0);
  740. this->OutputMakeRule(fout,
  741. "rule to build cmake from source",
  742. "${CMAKE}", "${CMAKE_SOURCE_DIR}/CMake/Source/*.cxx "
  743. "${CMAKE_SOURCE_DIR}/CMake/Source/*.h",
  744. "cd ${CMAKE_BINARY_DIR}/CMake/Source; "
  745. "${MAKE} CMakeBuildTargets");
  746. this->OutputMakeRule(fout,
  747. "remove generated files",
  748. "clean",
  749. "${SUBDIR_CLEAN}",
  750. "rm -f ${CLEAN_OBJECT_FILES} ${EXECUTABLES} ${TARGETS}");
  751. this->OutputMakeRule(fout,
  752. "Rule to build the Makefile",
  753. "Makefile",
  754. "${CMAKE} ${CMAKE_MAKEFILE_SOURCES} ",
  755. "${CMAKE} ${CMAKE_CURRENT_SOURCE}/CMakeLists.txt "
  756. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  757. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  758. this->OutputMakeRule(fout,
  759. "Rule to build the cmake.depends",
  760. "cmake.depends",
  761. "${CMAKE} ${CMAKE_MAKEFILE_SOURCES} ",
  762. "${CMAKE} ${CMAKE_CURRENT_SOURCE}/CMakeLists.txt "
  763. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  764. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  765. this->OutputMakeRule(fout,
  766. "Rule to force the build of cmake.depends",
  767. "depend",
  768. "${SUBDIR_DEPEND}",
  769. "${CMAKE} ${CMAKE_CURRENT_SOURCE}/CMakeLists.txt "
  770. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  771. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  772. this->OutputMakeRule(fout,
  773. "Rebuild the cache",
  774. "rebuild_cache",
  775. "${CMAKE_BINARY_DIR}/CMakeCache.txt",
  776. "${CMAKE} ${CMAKE_SOURCE_DIR}/CMakeLists.txt "
  777. "-MakeCache -S${CMAKE_SOURCE_DIR} -O${CMAKE_BINARY_DIR} "
  778. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  779. }
  780. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  781. const char* comment,
  782. const char* target,
  783. const char* depends,
  784. const char* command)
  785. {
  786. if(!target)
  787. {
  788. cmSystemTools::Error("no target for OutputMakeRule");
  789. return;
  790. }
  791. std::string replace;
  792. if(comment)
  793. {
  794. replace = comment;
  795. m_Makefile->ExpandVariablesInString(replace);
  796. fout << "# " << comment;
  797. }
  798. fout << "\n";
  799. replace = target;
  800. m_Makefile->ExpandVariablesInString(replace);
  801. fout << replace.c_str() << ": ";
  802. if(depends)
  803. {
  804. replace = depends;
  805. m_Makefile->ExpandVariablesInString(replace);
  806. fout << replace.c_str();
  807. }
  808. fout << "\n";
  809. if(command)
  810. {
  811. replace = command;
  812. m_Makefile->ExpandVariablesInString(replace);
  813. fout << "\t" << replace.c_str() << "\n\n";
  814. }
  815. fout << "\n\n\n";
  816. }