cmDSPMakefile.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "cmDSPMakefile.h"
  12. #include "cmStandardIncludes.h"
  13. #include "cmSystemTools.h"
  14. #include "cmRegularExpression.h"
  15. #include "cmCacheManager.h"
  16. cmDSPMakefile::~cmDSPMakefile()
  17. {
  18. }
  19. cmDSPMakefile::cmDSPMakefile(cmMakefile*mf)
  20. {
  21. m_Makefile = mf;
  22. }
  23. void cmDSPMakefile::OutputDSPFile()
  24. {
  25. // If not an in source build, then create the output directory
  26. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  27. m_Makefile->GetHomeDirectory()) != 0)
  28. {
  29. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  30. {
  31. cmSystemTools::Error("Error creating directory ",
  32. m_Makefile->GetStartOutputDirectory());
  33. }
  34. }
  35. // Setup /I and /LIBPATH options for the resulting DSP file
  36. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  37. std::vector<std::string>::iterator i;
  38. for(i = includes.begin(); i != includes.end(); ++i)
  39. {
  40. m_IncludeOptions += "/I \"";
  41. m_IncludeOptions += *i;
  42. m_IncludeOptions += "\" ";
  43. }
  44. std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  45. for(i = libs.begin(); i != libs.end(); ++i)
  46. {
  47. m_LibraryOptions += " ";
  48. m_LibraryOptions += *i;
  49. m_LibraryOptions += ".lib ";
  50. }
  51. std::vector<std::string>& libswin32 = m_Makefile->GetLinkLibrariesWin32();
  52. for(i = libswin32.begin(); i != libswin32.end(); ++i)
  53. {
  54. m_LibraryOptions += " ";
  55. m_LibraryOptions += *i;
  56. m_LibraryOptions += ".lib ";
  57. }
  58. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  59. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  60. {
  61. m_LibraryOptions += " /LIBPATH:\"";
  62. m_LibraryOptions += *i;
  63. m_LibraryOptions += "/$(OUTDIR)\" ";
  64. }
  65. m_LibraryOptions += "/STACK:10000000 ";
  66. m_OutputLibName = m_Makefile->GetLibraryName();
  67. // Create the DSP or set of DSP's for libraries and executables
  68. if(strlen(m_Makefile->GetLibraryName()) != 0)
  69. {
  70. const char* cacheValue
  71. = cmCacheManager::GetInstance()->GetCacheValue("BUILD_SHARED_LIBS");
  72. if(cacheValue && strcmp(cacheValue,"0"))
  73. {
  74. this->SetBuildType(DLL);
  75. }
  76. else
  77. {
  78. this->SetBuildType(STATIC_LIBRARY);
  79. }
  80. this->CreateSingleDSP();
  81. }
  82. // if there are executables build them
  83. if (m_Makefile->HasExecutables())
  84. {
  85. this->CreateExecutableDSPFiles();
  86. }
  87. }
  88. void cmDSPMakefile::CreateExecutableDSPFiles()
  89. {
  90. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  91. for(int i = 0; i < Classes.size(); ++i)
  92. {
  93. cmClassFile& classfile = Classes[i];
  94. if (classfile.m_IsExecutable)
  95. {
  96. std::string fname = m_Makefile->GetStartOutputDirectory();
  97. fname += "/";
  98. fname += classfile.m_ClassName;
  99. fname += ".dsp";
  100. std::ofstream fout(fname.c_str());
  101. if(!fout)
  102. {
  103. cmSystemTools::Error("Error Writing ",
  104. fname.c_str());
  105. }
  106. else
  107. {
  108. // m_Makefile->SetLibraryName(classfile.m_ClassName.c_str());
  109. this->SetBuildType(EXECUTABLE);
  110. m_OutputLibName = classfile.m_ClassName;
  111. std::string pname = classfile.m_ClassName.c_str(); //m_Makefile->GetLibraryName();
  112. m_CreatedProjectNames.push_back(pname);
  113. this->WriteDSPHeader(fout);
  114. this->WriteDSPBeginGroup(fout, "Source Files", "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat");
  115. this->WriteDSPBuildRule(fout, classfile.m_FullPath.c_str());
  116. this->WriteDSPEndGroup(fout);
  117. this->WriteDSPBuildRule(fout);
  118. this->WriteDSPFooter(fout);
  119. }
  120. }
  121. }
  122. }
  123. void cmDSPMakefile::CreateSingleDSP()
  124. {
  125. std::string fname;
  126. fname = m_Makefile->GetStartOutputDirectory();
  127. fname += "/";
  128. fname += m_Makefile->GetLibraryName();
  129. fname += ".dsp";
  130. m_CreatedProjectNames.clear();
  131. std::string pname = m_Makefile->GetLibraryName();
  132. m_CreatedProjectNames.push_back(pname);
  133. std::ofstream fout(fname.c_str());
  134. if(!fout)
  135. {
  136. cmSystemTools::Error("Error Writing ",
  137. fname.c_str());
  138. }
  139. this->WriteDSPFile(fout);
  140. }
  141. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout)
  142. {
  143. std::string dspname = *(m_CreatedProjectNames.end()-1);
  144. dspname += ".dsp";
  145. std::string makefileIn = "\"";
  146. makefileIn += m_Makefile->GetStartDirectory();
  147. makefileIn += "/";
  148. makefileIn += "CMakeLists.txt\"";
  149. std::string dsprule = "\"";
  150. dsprule += m_Makefile->GetHomeDirectory();
  151. dsprule += "/CMake/Source/CMakeSetupCMD\" ";
  152. dsprule += makefileIn;
  153. dsprule += " -DSP -H\"";
  154. dsprule += m_Makefile->GetHomeDirectory();
  155. dsprule += "\" -S\"";
  156. dsprule += m_Makefile->GetStartDirectory();
  157. dsprule += "\" -O\"";
  158. dsprule += m_Makefile->GetStartOutputDirectory();
  159. dsprule += "\" -B\"";
  160. dsprule += m_Makefile->GetHomeOutputDirectory();
  161. dsprule += "\"";
  162. std::set<std::string> depends;
  163. std::set<std::string> outputs;
  164. outputs.insert(outputs.begin(), dspname);
  165. fout << "# Begin Source File\n\n";
  166. fout << "SOURCE=" << makefileIn.c_str() << "\n\n";
  167. this->WriteCustomRule(fout, dsprule.c_str(), depends, outputs);
  168. fout << "# End Source File\n";
  169. }
  170. void cmDSPMakefile::AddDSPBuildRule(cmSourceGroup& sourceGroup)
  171. {
  172. std::string dspname = *(m_CreatedProjectNames.end()-1);
  173. dspname += ".dsp";
  174. std::string makefileIn = "\"";
  175. makefileIn += m_Makefile->GetStartDirectory();
  176. makefileIn += "/";
  177. makefileIn += "CMakeLists.txt\"";
  178. std::string dsprule = "\"";
  179. dsprule += m_Makefile->GetHomeDirectory();
  180. dsprule += "/CMake/Source/CMakeSetupCMD\" ";
  181. dsprule += makefileIn;
  182. dsprule += " -DSP -H\"";
  183. dsprule += m_Makefile->GetHomeDirectory();
  184. dsprule += "\" -S\"";
  185. dsprule += m_Makefile->GetStartDirectory();
  186. dsprule += "\" -O\"";
  187. dsprule += m_Makefile->GetStartOutputDirectory();
  188. dsprule += "\" -B\"";
  189. dsprule += m_Makefile->GetHomeOutputDirectory();
  190. dsprule += "\"";
  191. std::vector<std::string> depends;
  192. std::vector<std::string> outputs;
  193. outputs.push_back(dspname);
  194. sourceGroup.AddCustomCommand(makefileIn.c_str(), dsprule.c_str(),
  195. depends, outputs);
  196. }
  197. void cmDSPMakefile::WriteDSPFile(std::ostream& fout)
  198. {
  199. // Write the DSP file's header.
  200. this->WriteDSPHeader(fout);
  201. // We may be modifying the source groups temporarily, so make a copy.
  202. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  203. // Find the group in which the CMakeLists.txt source belongs, and add
  204. // the rule to generate this DSP file.
  205. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  206. sg != sourceGroups.rend(); ++sg)
  207. {
  208. if(sg->Matches("CMakeLists.txt"))
  209. {
  210. this->AddDSPBuildRule(*sg);
  211. break;
  212. }
  213. }
  214. // Loop through every source group.
  215. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  216. sg != sourceGroups.end(); ++sg)
  217. {
  218. const std::vector<std::string>& sources = sg->GetSources();
  219. const cmSourceGroup::CustomCommands& customCommands = sg->GetCustomCommands();
  220. // If the group is empty, don't write it at all.
  221. if(sources.empty() && customCommands.empty())
  222. { continue; }
  223. // If the group has a name, write the header.
  224. std::string name = sg->GetName();
  225. if(name != "")
  226. {
  227. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  228. }
  229. // Loop through each source in the source group.
  230. for(std::vector<std::string>::const_iterator s = sources.begin();
  231. s != sources.end(); ++s)
  232. {
  233. this->WriteDSPBuildRule(fout, s->c_str());
  234. }
  235. // Loop through each custom command in the source group.
  236. for(cmSourceGroup::CustomCommands::const_iterator cc =
  237. customCommands.begin(); cc != customCommands.end(); ++ cc)
  238. {
  239. std::string source = cc->first;
  240. const cmSourceGroup::Commands& commands = cc->second;
  241. fout << "# Begin Source File\n\n";
  242. fout << "SOURCE=" << source << "\n\n";
  243. // Loop through every command generating code from the current source.
  244. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  245. c != commands.end(); ++c)
  246. {
  247. std::string command = c->first;
  248. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  249. this->WriteCustomRule(fout, command.c_str(), commandFiles.m_Depends,
  250. commandFiles.m_Outputs);
  251. }
  252. fout << "# End Source File\n";
  253. }
  254. // If the group has a name, write the footer.
  255. if(name != "")
  256. {
  257. this->WriteDSPEndGroup(fout);
  258. }
  259. }
  260. // Write the DSP file's footer.
  261. this->WriteDSPFooter(fout);
  262. }
  263. void cmDSPMakefile::WriteCustomRule(std::ostream& fout,
  264. const char* command,
  265. const std::set<std::string>& depends,
  266. const std::set<std::string>& outputs)
  267. {
  268. std::vector<std::string>::iterator i;
  269. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  270. {
  271. if (i == m_Configurations.begin())
  272. {
  273. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  274. }
  275. else
  276. {
  277. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  278. }
  279. fout << "# Begin Custom Build\n\n";
  280. // Write a rule for every output generated by this command.
  281. for(std::set<std::string>::const_iterator output = outputs.begin();
  282. output != outputs.end(); ++output)
  283. {
  284. fout << "\"" << output->c_str()
  285. << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  286. // Write out all the dependencies for this rule.
  287. for(std::set<std::string>::const_iterator d = depends.begin();
  288. d != depends.end(); ++d)
  289. {
  290. fout << " " << d->c_str();
  291. }
  292. fout << "\n " << command << "\n\n";
  293. }
  294. fout << "# End Custom Build\n\n";
  295. }
  296. fout << "!ENDIF\n\n";
  297. }
  298. void cmDSPMakefile::WriteDSPBeginGroup(std::ostream& fout,
  299. const char* group,
  300. const char* filter)
  301. {
  302. fout << "# Begin Group \"" << group << "\"\n"
  303. "# PROP Default_Filter \"" << filter << "\"\n";
  304. }
  305. void cmDSPMakefile::WriteDSPEndGroup(std::ostream& fout)
  306. {
  307. fout << "# End Group\n";
  308. }
  309. void cmDSPMakefile::SetBuildType(BuildType b)
  310. {
  311. m_BuildType = b;
  312. switch(b)
  313. {
  314. case STATIC_LIBRARY:
  315. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  316. m_DSPHeaderTemplate += "/CMake/Source/staticLibHeader.dsptemplate";
  317. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  318. m_DSPFooterTemplate += "/CMake/Source/staticLibFooter.dsptemplate";
  319. break;
  320. case DLL:
  321. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  322. m_DSPHeaderTemplate += "/CMake/Source/DLLHeader.dsptemplate";
  323. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  324. m_DSPFooterTemplate += "/CMake/Source/DLLFooter.dsptemplate";
  325. break;
  326. case EXECUTABLE:
  327. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  328. m_DSPHeaderTemplate += "/CMake/Source/EXEHeader.dsptemplate";
  329. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  330. m_DSPFooterTemplate += "/CMake/Source/EXEFooter.dsptemplate";
  331. break;
  332. }
  333. // once the build type is set, determine what configurations are
  334. // possible
  335. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  336. cmRegularExpression reg("# Name ");
  337. if(!fin)
  338. {
  339. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  340. }
  341. // reset m_Configurations
  342. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  343. // now add all the configurations possible
  344. char buffer[2048];
  345. while(fin)
  346. {
  347. fin.getline(buffer, 2048);
  348. std::string line = buffer;
  349. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  350. m_Makefile->GetLibraryName());
  351. if (reg.find(line))
  352. {
  353. m_Configurations.push_back(line.substr(reg.end()));
  354. }
  355. }
  356. }
  357. void cmDSPMakefile::WriteDSPHeader(std::ostream& fout)
  358. {
  359. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  360. if(!fin)
  361. {
  362. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  363. }
  364. char buffer[2048];
  365. while(fin)
  366. {
  367. fin.getline(buffer, 2048);
  368. std::string line = buffer;
  369. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  370. m_LibraryOptions.c_str());
  371. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  372. m_IncludeOptions.c_str());
  373. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  374. m_OutputLibName.c_str());
  375. cmSystemTools::ReplaceString(line,
  376. "EXTRA_DEFINES",
  377. m_Makefile->GetDefineFlags());
  378. fout << line.c_str() << std::endl;
  379. }
  380. }
  381. void cmDSPMakefile::WriteDSPFooter(std::ostream& fout)
  382. {
  383. std::ifstream fin(m_DSPFooterTemplate.c_str());
  384. if(!fin)
  385. {
  386. cmSystemTools::Error("Error Reading ",
  387. m_DSPFooterTemplate.c_str());
  388. }
  389. char buffer[2048];
  390. while(fin)
  391. {
  392. fin.getline(buffer, 2048);
  393. fout << buffer << std::endl;
  394. }
  395. }
  396. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout, const char* path)
  397. {
  398. fout << "# Begin Source File\n\n";
  399. fout << "SOURCE="
  400. << path << "\n";
  401. fout << "# End Source File\n";
  402. }
  403. bool cmDSPMakefile::NeedsDependencies(const char* dspname)
  404. {
  405. if(strcmp(m_Makefile->GetLibraryName(), dspname) == 0)
  406. {
  407. // only shared libs need depend info
  408. const char* cacheValue
  409. = cmCacheManager::GetInstance()->GetCacheValue("BUILD_SHARED_LIBS");
  410. if(cacheValue && strcmp(cacheValue,"0"))
  411. {
  412. return true;
  413. }
  414. else
  415. {
  416. return false;
  417. }
  418. }
  419. // must be an executable so it needs depends
  420. return true;
  421. }