cmUnixMakefileGenerator.cxx 15 KB

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