cmDSPWriter.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmDSPMakefile.h"
  33. #include "cmStandardIncludes.h"
  34. #include "cmSystemTools.h"
  35. #include "cmRegularExpression.h"
  36. #include "cmCacheManager.h"
  37. cmDSPMakefile::~cmDSPMakefile()
  38. {
  39. }
  40. cmDSPMakefile::cmDSPMakefile(cmMakefile*mf)
  41. {
  42. m_Makefile = mf;
  43. }
  44. void cmDSPMakefile::OutputDSPFile()
  45. {
  46. // If not an in source build, then create the output directory
  47. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  48. m_Makefile->GetHomeDirectory()) != 0)
  49. {
  50. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  51. {
  52. cmSystemTools::Error("Error creating directory ",
  53. m_Makefile->GetStartOutputDirectory());
  54. }
  55. }
  56. // Setup /I and /LIBPATH options for the resulting DSP file
  57. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  58. std::vector<std::string>::iterator i;
  59. for(i = includes.begin(); i != includes.end(); ++i)
  60. {
  61. m_IncludeOptions += "/I \"";
  62. m_IncludeOptions += *i;
  63. m_IncludeOptions += "\" ";
  64. }
  65. // Create the DSP or set of DSP's for libraries and executables
  66. m_LibraryBuildType = STATIC_LIBRARY;
  67. if(cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS"))
  68. {
  69. m_LibraryBuildType = DLL;
  70. }
  71. // clear project names
  72. m_CreatedProjectNames.clear();
  73. // build any targets
  74. cmTargets &tgts = m_Makefile->GetTargets();
  75. for(cmTargets::iterator l = tgts.begin();
  76. l != tgts.end(); l++)
  77. {
  78. switch(l->second.GetType())
  79. {
  80. case cmTarget::LIBRARY:
  81. this->SetBuildType(m_LibraryBuildType, l->first.c_str());
  82. break;
  83. case cmTarget::EXECUTABLE:
  84. this->SetBuildType(EXECUTABLE,l->first.c_str());
  85. break;
  86. case cmTarget::UTILITY:
  87. this->SetBuildType(UTILITY, l->first.c_str());
  88. break;
  89. }
  90. this->CreateSingleDSP(l->first.c_str(),l->second);
  91. }
  92. }
  93. void cmDSPMakefile::CreateSingleDSP(const char *lname, cmTarget &target)
  94. {
  95. std::string fname;
  96. fname = m_Makefile->GetStartOutputDirectory();
  97. fname += "/";
  98. fname += lname;
  99. fname += ".dsp";
  100. std::string pname = lname;
  101. m_CreatedProjectNames.push_back(pname);
  102. std::ofstream fout(fname.c_str());
  103. if(!fout)
  104. {
  105. cmSystemTools::Error("Error Writing ", fname.c_str());
  106. }
  107. this->WriteDSPFile(fout,lname,target);
  108. }
  109. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout)
  110. {
  111. std::string dspname = *(m_CreatedProjectNames.end()-1);
  112. dspname += ".dsp";
  113. std::string makefileIn = "\"";
  114. makefileIn += m_Makefile->GetStartDirectory();
  115. makefileIn += "/";
  116. makefileIn += "CMakeLists.txt\"";
  117. std::string dsprule = "\"";
  118. dsprule += m_Makefile->GetHomeDirectory();
  119. dsprule += "/CMake/Source/CMakeSetupCMD\" ";
  120. dsprule += makefileIn;
  121. dsprule += " -DSP -H\"";
  122. dsprule += m_Makefile->GetHomeDirectory();
  123. dsprule += "\" -S\"";
  124. dsprule += m_Makefile->GetStartDirectory();
  125. dsprule += "\" -O\"";
  126. dsprule += m_Makefile->GetStartOutputDirectory();
  127. dsprule += "\" -B\"";
  128. dsprule += m_Makefile->GetHomeOutputDirectory();
  129. dsprule += "\"";
  130. std::set<std::string> depends;
  131. std::set<std::string> outputs;
  132. outputs.insert(outputs.begin(), dspname);
  133. fout << "# Begin Source File\n\n";
  134. fout << "SOURCE=" << makefileIn.c_str() << "\n\n";
  135. this->WriteCustomRule(fout, makefileIn.c_str(), dsprule.c_str(), depends, outputs);
  136. fout << "# End Source File\n";
  137. }
  138. void cmDSPMakefile::AddDSPBuildRule(cmSourceGroup& sourceGroup)
  139. {
  140. std::string dspname = *(m_CreatedProjectNames.end()-1);
  141. dspname += ".dsp";
  142. std::string makefileIn = "\"";
  143. makefileIn += m_Makefile->GetStartDirectory();
  144. makefileIn += "/";
  145. makefileIn += "CMakeLists.txt\"";
  146. std::string dsprule = "\"";
  147. dsprule += m_Makefile->GetHomeDirectory();
  148. dsprule += "/CMake/Source/CMakeSetupCMD\" ";
  149. dsprule += makefileIn;
  150. dsprule += " -DSP -H\"";
  151. dsprule += m_Makefile->GetHomeDirectory();
  152. dsprule += "\" -S\"";
  153. dsprule += m_Makefile->GetStartDirectory();
  154. dsprule += "\" -O\"";
  155. dsprule += m_Makefile->GetStartOutputDirectory();
  156. dsprule += "\" -B\"";
  157. dsprule += m_Makefile->GetHomeOutputDirectory();
  158. dsprule += "\"";
  159. std::vector<std::string> outputs;
  160. outputs.push_back(dspname);
  161. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  162. m_Makefile->GetListFiles(),
  163. outputs);
  164. sourceGroup.AddCustomCommand(cc);
  165. }
  166. void cmDSPMakefile::WriteDSPFile(std::ostream& fout,
  167. const char *libName,
  168. cmTarget &target)
  169. {
  170. // Write the DSP file's header.
  171. this->WriteDSPHeader(fout, libName, target);
  172. // We may be modifying the source groups temporarily, so make a copy.
  173. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  174. // get the classes from the source lists then add them to the groups
  175. target.GenerateSourceFilesFromSourceLists(*m_Makefile);
  176. std::vector<cmSourceFile> classes = target.GetSourceFiles();
  177. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  178. i != classes.end(); i++)
  179. {
  180. if(!i->IsAHeaderFileOnly())
  181. {
  182. // Add the file to the list of sources.
  183. std::string source = i->GetFullPath();
  184. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  185. sourceGroups);
  186. sourceGroup.AddSource(source.c_str());
  187. }
  188. }
  189. // add any custom rules to the source groups
  190. for (std::vector<cmCustomCommand>::const_iterator cr =
  191. target.GetCustomCommands().begin();
  192. cr != target.GetCustomCommands().end(); ++cr)
  193. {
  194. cmSourceGroup& sourceGroup =
  195. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  196. sourceGroups);
  197. cmCustomCommand cc(*cr);
  198. cc.ExpandVariables(*m_Makefile);
  199. sourceGroup.AddCustomCommand(cc);
  200. }
  201. // Find the group in which the CMakeLists.txt source belongs, and add
  202. // the rule to generate this DSP file.
  203. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  204. sg != sourceGroups.rend(); ++sg)
  205. {
  206. if(sg->Matches("CMakeLists.txt"))
  207. {
  208. this->AddDSPBuildRule(*sg);
  209. break;
  210. }
  211. }
  212. // Loop through every source group.
  213. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  214. sg != sourceGroups.end(); ++sg)
  215. {
  216. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  217. // If the group is empty, don't write it at all.
  218. if(buildRules.empty())
  219. { continue; }
  220. // If the group has a name, write the header.
  221. std::string name = sg->GetName();
  222. if(name != "")
  223. {
  224. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  225. }
  226. // Loop through each build rule in the source group.
  227. for(cmSourceGroup::BuildRules::const_iterator cc =
  228. buildRules.begin(); cc != buildRules.end(); ++ cc)
  229. {
  230. std::string source = cc->first;
  231. const cmSourceGroup::Commands& commands = cc->second;
  232. fout << "# Begin Source File\n\n";\
  233. // Tell MS-Dev what the source is. If the compiler knows how to
  234. // build it, then it will.
  235. fout << "SOURCE=" << source.c_str() << "\n\n";
  236. // Loop through every custom command generating code from the
  237. // current source.
  238. // build up the depends and outputs and commands
  239. cmSourceGroup::CommandFiles totalCommand;
  240. std::string totalCommandStr;
  241. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  242. c != commands.end(); ++c)
  243. {
  244. totalCommandStr += "\n\t";
  245. totalCommandStr += c->first;
  246. totalCommand.Merge(c->second);
  247. }
  248. // Create a dummy file with the name of the source if it does
  249. // not exist
  250. if(totalCommand.m_Outputs.empty())
  251. {
  252. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  253. dummyFile += "/";
  254. dummyFile += source;
  255. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  256. {
  257. std::ofstream fout(dummyFile.c_str());
  258. fout << "Dummy file created by cmake as unused source for utility command.\n";
  259. }
  260. }
  261. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  262. totalCommand.m_Depends,
  263. totalCommand.m_Outputs);
  264. fout << "# End Source File\n";
  265. }
  266. // If the group has a name, write the footer.
  267. if(name != "")
  268. {
  269. this->WriteDSPEndGroup(fout);
  270. }
  271. }
  272. // Write the DSP file's footer.
  273. this->WriteDSPFooter(fout);
  274. }
  275. void cmDSPMakefile::WriteCustomRule(std::ostream& fout,
  276. const char* source,
  277. const char* command,
  278. const std::set<std::string>& depends,
  279. const std::set<std::string>& outputs)
  280. {
  281. std::vector<std::string>::iterator i;
  282. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  283. {
  284. if (i == m_Configurations.begin())
  285. {
  286. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  287. }
  288. else
  289. {
  290. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  291. }
  292. fout << "# Begin Custom Build\n\n";
  293. // Write out the dependencies (this seems to be the only way to
  294. // get VC6 to actually take these dependencies into account.
  295. fout << "USERDEP__HACK= ";
  296. for(std::set<std::string>::const_iterator d = depends.begin();
  297. d != depends.end(); ++d)
  298. {
  299. fout << " \"" << d->c_str() << "\"";
  300. }
  301. fout << "\n";
  302. fout << "# Begin Custom Build\n\n";
  303. if(outputs.size() == 0)
  304. {
  305. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  306. fout << command << "\n\n";
  307. }
  308. // Write a rule for every output generated by this command.
  309. for(std::set<std::string>::const_iterator output = outputs.begin();
  310. output != outputs.end(); ++output)
  311. {
  312. fout << "\"" << output->c_str()
  313. << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  314. // Write out all the dependencies for this rule.
  315. for(std::set<std::string>::const_iterator d = depends.begin();
  316. d != depends.end(); ++d)
  317. {
  318. fout << " \"" << d->c_str() << "\"";
  319. }
  320. fout << command << "\n\n";
  321. }
  322. fout << "# End Custom Build\n\n";
  323. }
  324. fout << "!ENDIF\n\n";
  325. }
  326. void cmDSPMakefile::WriteDSPBeginGroup(std::ostream& fout,
  327. const char* group,
  328. const char* filter)
  329. {
  330. fout << "# Begin Group \"" << group << "\"\n"
  331. "# PROP Default_Filter \"" << filter << "\"\n";
  332. }
  333. void cmDSPMakefile::WriteDSPEndGroup(std::ostream& fout)
  334. {
  335. fout << "# End Group\n";
  336. }
  337. void cmDSPMakefile::SetBuildType(BuildType b, const char *libName)
  338. {
  339. switch(b)
  340. {
  341. case STATIC_LIBRARY:
  342. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  343. m_DSPHeaderTemplate += "/CMake/Source/staticLibHeader.dsptemplate";
  344. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  345. m_DSPFooterTemplate += "/CMake/Source/staticLibFooter.dsptemplate";
  346. break;
  347. case DLL:
  348. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  349. m_DSPHeaderTemplate += "/CMake/Source/DLLHeader.dsptemplate";
  350. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  351. m_DSPFooterTemplate += "/CMake/Source/DLLFooter.dsptemplate";
  352. break;
  353. case EXECUTABLE:
  354. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  355. m_DSPHeaderTemplate += "/CMake/Source/EXEHeader.dsptemplate";
  356. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  357. m_DSPFooterTemplate += "/CMake/Source/EXEFooter.dsptemplate";
  358. break;
  359. case UTILITY:
  360. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  361. m_DSPHeaderTemplate += "/CMake/Source/UtilityHeader.dsptemplate";
  362. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  363. m_DSPFooterTemplate += "/CMake/Source/UtilityFooter.dsptemplate";
  364. break;
  365. }
  366. // once the build type is set, determine what configurations are
  367. // possible
  368. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  369. cmRegularExpression reg("# Name ");
  370. if(!fin)
  371. {
  372. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  373. }
  374. // reset m_Configurations
  375. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  376. // now add all the configurations possible
  377. char buffer[2048];
  378. while(fin)
  379. {
  380. fin.getline(buffer, 2048);
  381. std::string line = buffer;
  382. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  383. if (reg.find(line))
  384. {
  385. m_Configurations.push_back(line.substr(reg.end()));
  386. }
  387. }
  388. }
  389. void cmDSPMakefile::WriteDSPHeader(std::ostream& fout, const char *libName,
  390. const cmTarget &target)
  391. {
  392. // determine the link directories
  393. std::string libOptions;
  394. std::string libDebugOptions;
  395. std::string libOptimizedOptions;
  396. std::vector<std::string>::iterator i;
  397. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  398. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  399. {
  400. libOptions += " /LIBPATH:\"";
  401. libOptions += *i;
  402. libOptions += "/$(OUTDIR)\" ";
  403. libOptions += " /LIBPATH:\"";
  404. libOptions += *i;
  405. libOptions += "\" ";
  406. }
  407. // find link libraries
  408. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  409. cmTarget::LinkLibraries::const_iterator j;
  410. for(j = libs.begin(); j != libs.end(); ++j)
  411. {
  412. // add libraries to executables and dlls (but never include
  413. // a library in a library, bad recursion)
  414. if (!target.GetType() == cmTarget::LIBRARY ||
  415. (m_LibraryBuildType == DLL && libName != j->first))
  416. {
  417. if (j->second == cmTarget::GENERAL)
  418. {
  419. libOptions += " ";
  420. libOptions += j->first;
  421. libOptions += ".lib ";
  422. }
  423. if (j->second == cmTarget::DEBUG)
  424. {
  425. libDebugOptions += " ";
  426. libDebugOptions += j->first;
  427. libDebugOptions += ".lib ";
  428. }
  429. if (j->second == cmTarget::OPTIMIZED)
  430. {
  431. libOptimizedOptions += " ";
  432. libOptimizedOptions += j->first;
  433. libOptimizedOptions += ".lib ";
  434. }
  435. }
  436. }
  437. libOptions += "/STACK:10000000 ";
  438. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  439. if(!fin)
  440. {
  441. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  442. }
  443. char buffer[2048];
  444. while(fin)
  445. {
  446. fin.getline(buffer, 2048);
  447. std::string line = buffer;
  448. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  449. libOptions.c_str());
  450. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  451. libDebugOptions.c_str());
  452. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  453. libOptimizedOptions.c_str());
  454. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  455. m_IncludeOptions.c_str());
  456. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  457. cmSystemTools::ReplaceString(line,
  458. "EXTRA_DEFINES",
  459. m_Makefile->GetDefineFlags());
  460. fout << line.c_str() << std::endl;
  461. }
  462. }
  463. void cmDSPMakefile::WriteDSPFooter(std::ostream& fout)
  464. {
  465. std::ifstream fin(m_DSPFooterTemplate.c_str());
  466. if(!fin)
  467. {
  468. cmSystemTools::Error("Error Reading ",
  469. m_DSPFooterTemplate.c_str());
  470. }
  471. char buffer[2048];
  472. while(fin)
  473. {
  474. fin.getline(buffer, 2048);
  475. fout << buffer << std::endl;
  476. }
  477. }