cmUnixMakefileGenerator.cxx 15 KB

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