cmUnixMakefileGenerator.cxx 15 KB

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