cmUnixMakefileGenerator.cxx 34 KB

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