cmUnixMakefileGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. void cmUnixMakefileGenerator::GenerateMakefile()
  40. {
  41. // Generate depends
  42. cmMakeDepend md;
  43. md.SetMakefile(m_Makefile);
  44. md.DoDepends();
  45. // output the makefile fragment
  46. this->OutputMakefile("CMakeTargets.make");
  47. }
  48. // This is where CMakeTargets.make is generated
  49. void cmUnixMakefileGenerator::OutputMakefile(const char* file)
  50. {
  51. // Create sub directories fro aux source directories
  52. std::vector<std::string>& auxSourceDirs =
  53. m_Makefile->GetAuxSourceDirectories();
  54. if( auxSourceDirs.size() )
  55. {
  56. // For the case when this is running as a remote build
  57. // on unix, make the directory
  58. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  59. i != auxSourceDirs.end(); ++i)
  60. {
  61. cmSystemTools::MakeDirectory(i->c_str());
  62. }
  63. }
  64. std::ofstream fout(file);
  65. if(!fout)
  66. {
  67. cmSystemTools::Error("Error can not open for write: ", file);
  68. return;
  69. }
  70. this->OutputMakeFlags(fout);
  71. this->OutputVerbatim(fout);
  72. this->OutputTargetRules(fout);
  73. this->OutputDependencies(fout);
  74. this->OutputTargets(fout);
  75. this->OutputSubDirectoryRules(fout);
  76. this->OutputObjectDepends(fout);
  77. this->OutputCustomRules(fout);
  78. }
  79. // Output the rules for any targets
  80. void cmUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  81. {
  82. // for each target add to the list of targets
  83. fout << "TARGETS = ";
  84. const cmTargets &tgts = m_Makefile->GetTargets();
  85. // list libraries first
  86. for(cmTargets::const_iterator l = tgts.begin();
  87. l != tgts.end(); l++)
  88. {
  89. if (l->second.GetType() == cmTarget::LIBRARY &&
  90. l->second.IsInAll())
  91. {
  92. fout << " \\\nlib" << l->first.c_str() << "${CMAKE_LIB_EXT}";
  93. }
  94. }
  95. // executables
  96. for(cmTargets::const_iterator l = tgts.begin();
  97. l != tgts.end(); l++)
  98. {
  99. if (l->second.GetType() == cmTarget::EXECUTABLE &&
  100. l->second.IsInAll())
  101. {
  102. fout << " \\\n" << l->first.c_str();
  103. }
  104. }
  105. // list utilities last
  106. for(cmTargets::const_iterator l = tgts.begin();
  107. l != tgts.end(); l++)
  108. {
  109. if (l->second.GetType() == cmTarget::UTILITY &&
  110. l->second.IsInAll())
  111. {
  112. fout << " \\\n" << l->first.c_str();
  113. }
  114. }
  115. fout << "\n\n";
  116. // get the classes from the source lists then add them to the groups
  117. for(cmTargets::const_iterator l = tgts.begin();
  118. l != tgts.end(); l++)
  119. {
  120. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  121. fout << l->first << "_SRC_OBJS = ";
  122. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  123. i != classes.end(); i++)
  124. {
  125. if(!i->IsAHeaderFileOnly())
  126. {
  127. fout << "\\\n" << i->GetSourceName() << ".o ";
  128. }
  129. }
  130. fout << "\n\n";
  131. }
  132. }
  133. /**
  134. * Output the linking rules on a command line. For executables,
  135. * targetLibrary should be a NULL pointer. For libraries, it should point
  136. * to the name of the library. This will not link a library against itself.
  137. */
  138. void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  139. const char* targetLibrary,
  140. const cmTarget &tgt)
  141. {
  142. // collect all the flags needed for linking libraries
  143. std::string linkLibs;
  144. std::vector<std::string>::iterator j;
  145. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  146. for(j = libdirs.begin(); j != libdirs.end(); ++j)
  147. {
  148. std::string::size_type pos = (*j).find("-L");
  149. if((pos == std::string::npos || pos > 0)
  150. && (*j).find("${") == std::string::npos)
  151. {
  152. linkLibs += "-L";
  153. }
  154. linkLibs += *j;
  155. linkLibs += " ";
  156. }
  157. std::string librariesLinked;
  158. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  159. cmTarget::LinkLibraries::const_iterator j2;
  160. for(j2 = libs.begin(); j2 != libs.end(); ++j2)
  161. {
  162. // Don't link the library against itself!
  163. if(targetLibrary && (j2->first == targetLibrary)) continue;
  164. // don't look at debug libraries
  165. if (j2->second == cmTarget::DEBUG) continue;
  166. std::string::size_type pos = j2->first.find("-l");
  167. if((pos == std::string::npos || pos > 0)
  168. && j2->first.find("${") == std::string::npos)
  169. {
  170. librariesLinked += "-l";
  171. }
  172. librariesLinked += j2->first;
  173. librariesLinked += " ";
  174. }
  175. linkLibs += librariesLinked;
  176. if(!targetLibrary)
  177. {
  178. // For executables, add these a second time so order does not matter
  179. linkLibs += librariesLinked;
  180. }
  181. linkLibs += " ${LOCAL_LINK_FLAGS} ";
  182. fout << linkLibs;
  183. }
  184. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  185. {
  186. // for each target
  187. const cmTargets &tgts = m_Makefile->GetTargets();
  188. for(cmTargets::const_iterator l = tgts.begin();
  189. l != tgts.end(); l++)
  190. {
  191. if (l->second.GetType() == cmTarget::LIBRARY)
  192. {
  193. fout << "#---------------------------------------------------------\n";
  194. fout << "# rules for a library\n";
  195. fout << "#\n";
  196. fout << "lib" << l->first << ".a: ${KIT_OBJ} ${" <<
  197. l->first << "_SRC_OBJS} \n";
  198. fout << "\t${AR} cr lib" << l->first << ".a ${KIT_OBJ} ${" <<
  199. l->first << "_SRC_OBJS} \n";
  200. fout << "\t${RANLIB} lib" << l->first << ".a\n";
  201. fout << std::endl;
  202. fout << "lib" << l->first << "$(SHLIB_SUFFIX): ${KIT_OBJ} ${" <<
  203. l->first << "_SRC_OBJS} \n";
  204. fout << "\trm -f lib" << l->first << "$(SHLIB_SUFFIX)\n";
  205. fout << "\t$(CXX) ${CXX_FLAGS} ${CMAKE_SHLIB_BUILD_FLAGS} -o \\\n";
  206. fout << "\t lib" << l->first << "$(SHLIB_SUFFIX) \\\n";
  207. fout << "\t ${KIT_OBJ} ${" << l->first <<
  208. "_SRC_OBJS} ";
  209. this->OutputLinkLibraries(fout, l->first.c_str(), l->second);
  210. fout << "\n\n";
  211. }
  212. else if (l->second.GetType() == cmTarget::EXECUTABLE)
  213. {
  214. fout << l->first << ": ${" <<
  215. l->first << "_SRC_OBJS} ${CMAKE_DEPEND_LIBS}\n";
  216. fout << "\t${CXX} ${CXX_FLAGS} ${" <<
  217. l->first << "_SRC_OBJS} ";
  218. this->OutputLinkLibraries(fout, NULL,l->second);
  219. fout << " -o " << l->first << "\n\n";
  220. }
  221. }
  222. }
  223. // output the list of libraries that the executables
  224. // in this makefile will depend on.
  225. void cmUnixMakefileGenerator::OutputDependencies(std::ostream& fout)
  226. {
  227. fout << "CMAKE_DEPEND_LIBS = ";
  228. cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  229. cmTarget::LinkLibraries::const_iterator lib2;
  230. // Search the list of libraries that will be linked into
  231. // the executable
  232. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  233. {
  234. // loop over the list of directories that the libraries might
  235. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  236. // be stored in the cache
  237. const char* cacheValue
  238. = cmCacheManager::GetInstance()->GetCacheValue(lib2->first.c_str());
  239. if(cacheValue)
  240. {
  241. std::string libpath = cacheValue;
  242. libpath += "/lib";
  243. libpath += lib2->first;
  244. libpath += "${CMAKE_LIB_EXT}";
  245. fout << libpath << " ";
  246. }
  247. }
  248. std::vector<std::string>& utils = m_Makefile->GetUtilities();
  249. std::vector<std::string>& utildirs = m_Makefile->GetUtilityDirectories();
  250. std::vector<std::string>::iterator dir, util;
  251. // Search the list of utilities that may be used to generate code for
  252. // this project.
  253. for(util = utils.begin(); util != utils.end(); ++util)
  254. {
  255. bool found = false;
  256. // loop over the list of directories that the utilities might
  257. // be in, looking for an ADD_EXECUTABLE(util ...) line.
  258. for(dir = utildirs.begin(); dir != utildirs.end() && !found; ++dir)
  259. {
  260. std::string expression = "TARGETS =.*";
  261. expression += util->c_str();
  262. if(cmSystemTools::Grep(dir->c_str(), "CMakeTargets.make",
  263. expression.c_str()))
  264. {
  265. fout << *util << " ";
  266. found = true;
  267. }
  268. }
  269. }
  270. fout << "\n";
  271. }
  272. // output make include flags
  273. void cmUnixMakefileGenerator::OutputMakeFlags(std::ostream& fout)
  274. {
  275. // Output Include paths
  276. fout << "INCLUDE_FLAGS = ";
  277. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  278. std::vector<std::string>::iterator i;
  279. fout << "-I" << m_Makefile->GetStartDirectory() << " ";
  280. for(i = includes.begin(); i != includes.end(); ++i)
  281. {
  282. std::string include = *i;
  283. fout << "-I" << i->c_str() << " ";
  284. }
  285. fout << m_Makefile->GetDefineFlags();
  286. fout << " ${LOCAL_INCLUDE_FLAGS} ";
  287. fout << "\n\n";
  288. fout << "default_target: all\n\n";
  289. // see if there are files to compile in this makefile
  290. // These are used for both libraries and executables
  291. }
  292. // output verbatim section
  293. void cmUnixMakefileGenerator::OutputVerbatim(std::ostream& fout)
  294. {
  295. std::vector<std::string>& MakeVerbatim = m_Makefile->GetMakeVerbatim();
  296. // Ouput user make text embeded in the input file
  297. for(unsigned int i =0; i < MakeVerbatim.size(); i++)
  298. {
  299. fout << MakeVerbatim[i] << "\n";
  300. }
  301. fout << "\n\n";
  302. }
  303. // fix up names of directories so they can be used
  304. // as targets in makefiles.
  305. inline std::string FixDirectoryName(const char* dir)
  306. {
  307. std::string s = dir;
  308. // replace ../ with 3 under bars
  309. size_t pos = s.find("../");
  310. if(pos != std::string::npos)
  311. {
  312. s.replace(pos, 3, "___");
  313. }
  314. // replace / directory separators with a single under bar
  315. pos = s.find("/");
  316. while(pos != std::string::npos)
  317. {
  318. s.replace(pos, 1, "_");
  319. pos = s.find("/");
  320. }
  321. return s;
  322. }
  323. // output rules for decending into sub directories
  324. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  325. {
  326. // Output Sub directory build rules
  327. const std::vector<std::string>& SubDirectories
  328. = m_Makefile->GetSubDirectories();
  329. if( SubDirectories.size() == 0)
  330. {
  331. return;
  332. }
  333. fout << "SUBDIR_BUILD = \\\n";
  334. unsigned int i;
  335. for(i =0; i < SubDirectories.size(); i++)
  336. {
  337. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  338. fout << "build_" << subdir.c_str();
  339. if(i == SubDirectories.size()-1)
  340. {
  341. fout << " \n\n";
  342. }
  343. else
  344. {
  345. fout << " \\\n";
  346. }
  347. }
  348. fout << std::endl;
  349. fout << "SUBDIR_CLEAN = \\\n";
  350. for(i =0; i < SubDirectories.size(); i++)
  351. {
  352. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  353. fout << "clean_" << subdir.c_str();
  354. if(i == SubDirectories.size()-1)
  355. {
  356. fout << " \n\n";
  357. }
  358. else
  359. {
  360. fout << " \\\n";
  361. }
  362. }
  363. fout << std::endl;
  364. fout << "alldirs : ${SUBDIR_BUILD}\n\n";
  365. for(i =0; i < SubDirectories.size(); i++)
  366. {
  367. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  368. fout << "build_" << subdir.c_str() << ":\n";
  369. fout << "\tcd " << SubDirectories[i].c_str()
  370. << "; ${MAKE} -${MAKEFLAGS} CMakeTargets.make\n";
  371. fout << "\tcd " << SubDirectories[i].c_str()
  372. << "; ${MAKE} -${MAKEFLAGS} all\n\n";
  373. fout << "clean_" << subdir.c_str() << ": \n";
  374. fout << "\tcd " << SubDirectories[i].c_str()
  375. << "; ${MAKE} -${MAKEFLAGS} clean\n\n";
  376. }
  377. }
  378. // Output the depend information for all the classes
  379. // in the makefile. These would have been generated
  380. // by the class cmMakeDepend GenerateMakefile
  381. void cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  382. {
  383. // Iterate over every target.
  384. std::map<std::string, cmTarget>& targets = m_Makefile->GetTargets();
  385. for(std::map<std::string, cmTarget>::const_iterator target = targets.begin();
  386. target != targets.end(); ++target)
  387. {
  388. // Iterate over every source for this target.
  389. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  390. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  391. source != sources.end(); ++source)
  392. {
  393. if(!source->IsAHeaderFileOnly())
  394. {
  395. if(!source->GetDepends().empty())
  396. {
  397. fout << source->GetSourceName() << ".o :";
  398. // Iterate through all the dependencies for this source.
  399. for(std::vector<std::string>::const_iterator dep =
  400. source->GetDepends().begin();
  401. dep != source->GetDepends().end(); ++dep)
  402. {
  403. fout << " \\\n" << dep->c_str();
  404. }
  405. fout << "\n\n";
  406. }
  407. }
  408. }
  409. }
  410. }
  411. // Output each custom rule in the following format:
  412. // output: source depends...
  413. // (tab) command...
  414. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  415. {
  416. // We may be modifying the source groups temporarily, so make a copy.
  417. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  418. const cmTargets &tgts = m_Makefile->GetTargets();
  419. for(cmTargets::const_iterator tgt = tgts.begin();
  420. tgt != tgts.end(); ++tgt)
  421. {
  422. // add any custom rules to the source groups
  423. for (std::vector<cmCustomCommand>::const_iterator cr =
  424. tgt->second.GetCustomCommands().begin();
  425. cr != tgt->second.GetCustomCommands().end(); ++cr)
  426. {
  427. cmSourceGroup& sourceGroup =
  428. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  429. sourceGroups);
  430. cmCustomCommand cc(*cr);
  431. cc.ExpandVariables(*m_Makefile);
  432. sourceGroup.AddCustomCommand(cc);
  433. }
  434. }
  435. // Loop through every source group.
  436. for(std::vector<cmSourceGroup>::const_iterator sg =
  437. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  438. {
  439. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  440. if(buildRules.empty())
  441. { continue; }
  442. std::string name = sg->GetName();
  443. if(name != "")
  444. {
  445. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  446. }
  447. // Loop through each source in the source group.
  448. for(cmSourceGroup::BuildRules::const_iterator cc =
  449. buildRules.begin(); cc != buildRules.end(); ++ cc)
  450. {
  451. std::string source = cc->first;
  452. const cmSourceGroup::Commands& commands = cc->second;
  453. // Loop through every command generating code from the current source.
  454. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  455. c != commands.end(); ++c)
  456. {
  457. std::string command = c->first;
  458. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  459. // if the command has no outputs, then it is a utility command
  460. // with no outputs
  461. if(commandFiles.m_Outputs.size() == 0)
  462. {
  463. fout << source.c_str() << ": ";
  464. // Write out all the dependencies for this rule.
  465. for(std::set<std::string>::const_iterator d =
  466. commandFiles.m_Depends.begin();
  467. d != commandFiles.m_Depends.end(); ++d)
  468. {
  469. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  470. fout << " " << dep.c_str();
  471. }
  472. fout << "\n\t" << command.c_str() << "\n\n";
  473. }
  474. // Write a rule for every output generated by this command.
  475. for(std::set<std::string>::const_iterator output =
  476. commandFiles.m_Outputs.begin();
  477. output != commandFiles.m_Outputs.end(); ++output)
  478. {
  479. std::string src = cmSystemTools::EscapeSpaces(source.c_str());
  480. fout << output->c_str() << ": " << src.c_str();
  481. // Write out all the dependencies for this rule.
  482. for(std::set<std::string>::const_iterator d =
  483. commandFiles.m_Depends.begin();
  484. d != commandFiles.m_Depends.end(); ++d)
  485. {
  486. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  487. fout << " " << dep.c_str();
  488. }
  489. fout << "\n\t" << command.c_str() << "\n\n";
  490. }
  491. }
  492. }
  493. if(name != "")
  494. {
  495. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  496. }
  497. }
  498. }