cmUnixMakefileGenerator.cxx 17 KB

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