cmUnixMakefileGenerator.cxx 15 KB

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