cmLocalVisualStudio6Generator.cxx 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalVisualStudio6Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include <cmsys/RegularExpression.hxx>
  20. cmLocalVisualStudio6Generator::cmLocalVisualStudio6Generator()
  21. {
  22. }
  23. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  24. {
  25. }
  26. void cmLocalVisualStudio6Generator::Generate(bool /* fromTheTop */)
  27. {
  28. this->OutputDSPFile();
  29. }
  30. void cmLocalVisualStudio6Generator::OutputDSPFile()
  31. {
  32. // If not an in source build, then create the output directory
  33. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  34. m_Makefile->GetHomeDirectory()) != 0)
  35. {
  36. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  37. {
  38. cmSystemTools::Error("Error creating directory ",
  39. m_Makefile->GetStartOutputDirectory());
  40. }
  41. }
  42. // Setup /I and /LIBPATH options for the resulting DSP file
  43. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  44. std::vector<std::string>::iterator i;
  45. for(i = includes.begin(); i != includes.end(); ++i)
  46. {
  47. m_IncludeOptions += " /I ";
  48. std::string tmp = cmSystemTools::ConvertToOutputPath(i->c_str());
  49. // quote if not already quoted
  50. if (tmp[0] != '"')
  51. {
  52. m_IncludeOptions += "\"";
  53. m_IncludeOptions += tmp;
  54. m_IncludeOptions += "\"";
  55. }
  56. else
  57. {
  58. m_IncludeOptions += tmp;
  59. }
  60. }
  61. // Create the DSP or set of DSP's for libraries and executables
  62. // clear project names
  63. m_CreatedProjectNames.clear();
  64. // expand vars for custom commands
  65. m_Makefile->ExpandVariablesInCustomCommands();
  66. // build any targets
  67. cmTargets &tgts = m_Makefile->GetTargets();
  68. for(cmTargets::iterator l = tgts.begin();
  69. l != tgts.end(); l++)
  70. {
  71. switch(l->second.GetType())
  72. {
  73. case cmTarget::STATIC_LIBRARY:
  74. this->SetBuildType(STATIC_LIBRARY, l->first.c_str(), l->second);
  75. break;
  76. case cmTarget::SHARED_LIBRARY:
  77. case cmTarget::MODULE_LIBRARY:
  78. this->SetBuildType(DLL, l->first.c_str(), l->second);
  79. break;
  80. case cmTarget::EXECUTABLE:
  81. this->SetBuildType(EXECUTABLE,l->first.c_str(), l->second);
  82. break;
  83. case cmTarget::WIN32_EXECUTABLE:
  84. this->SetBuildType(WIN32_EXECUTABLE,l->first.c_str(), l->second);
  85. break;
  86. case cmTarget::UTILITY:
  87. this->SetBuildType(UTILITY, l->first.c_str(), l->second);
  88. break;
  89. case cmTarget::INSTALL_FILES:
  90. break;
  91. case cmTarget::INSTALL_PROGRAMS:
  92. break;
  93. default:
  94. cmSystemTools::Error("Bad target type", l->first.c_str());
  95. break;
  96. }
  97. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  98. // so don't build a projectfile for it
  99. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  100. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  101. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  102. {
  103. // check to see if the dsp is going into a sub-directory
  104. std::string::size_type pos = l->first.rfind('/');
  105. if(pos != std::string::npos)
  106. {
  107. std::string dir = m_Makefile->GetStartOutputDirectory();
  108. dir += "/";
  109. dir += l->first.substr(0, pos);
  110. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  111. {
  112. cmSystemTools::Error("Error creating directory ", dir.c_str());
  113. }
  114. }
  115. this->CreateSingleDSP(l->first.c_str(),l->second);
  116. }
  117. }
  118. }
  119. void cmLocalVisualStudio6Generator::CreateSingleDSP(const char *lname, cmTarget &target)
  120. {
  121. // add to the list of projects
  122. std::string pname = lname;
  123. m_CreatedProjectNames.push_back(pname);
  124. // create the dsp.cmake file
  125. std::string fname;
  126. fname = m_Makefile->GetStartOutputDirectory();
  127. fname += "/";
  128. fname += lname;
  129. fname += ".dsp";
  130. // save the name of the real dsp file
  131. std::string realDSP = fname;
  132. fname += ".cmake";
  133. std::ofstream fout(fname.c_str());
  134. if(!fout)
  135. {
  136. cmSystemTools::Error("Error Writing ", fname.c_str());
  137. }
  138. this->WriteDSPFile(fout,lname,target);
  139. fout.close();
  140. // if the dsp file has changed, then write it.
  141. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  142. }
  143. void cmLocalVisualStudio6Generator::AddDSPBuildRule()
  144. {
  145. std::string dspname = *(m_CreatedProjectNames.end()-1);
  146. if(dspname == "ALL_BUILD")
  147. {
  148. return;
  149. }
  150. dspname += ".dsp.cmake";
  151. std::string makefileIn = m_Makefile->GetStartDirectory();
  152. makefileIn += "/";
  153. makefileIn += "CMakeLists.txt";
  154. makefileIn = cmSystemTools::ConvertToOutputPath(makefileIn.c_str());
  155. std::string dsprule = "${CMAKE_COMMAND}";
  156. m_Makefile->ExpandVariablesInString(dsprule);
  157. dsprule = cmSystemTools::ConvertToOutputPath(dsprule.c_str());
  158. std::vector<std::string> argv;
  159. argv.push_back(makefileIn);
  160. makefileIn = m_Makefile->GetStartDirectory();
  161. makefileIn += "/";
  162. makefileIn += "CMakeLists.txt";
  163. std::string args;
  164. args = "-H";
  165. args +=
  166. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory());
  167. argv.push_back(args);
  168. args = "-S";
  169. args +=
  170. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory());
  171. argv.push_back(args);
  172. args = "-O";
  173. args +=
  174. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory());
  175. argv.push_back(args);
  176. args = "-B";
  177. args +=
  178. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory());
  179. argv.push_back(args);
  180. std::string configFile =
  181. m_Makefile->GetDefinition("CMAKE_ROOT");
  182. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  183. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  184. bool found = false;
  185. for(std::vector<std::string>::iterator i = listFiles.begin();
  186. i != listFiles.end(); ++i)
  187. {
  188. if(*i == configFile)
  189. {
  190. found = true;
  191. }
  192. }
  193. if(!found)
  194. {
  195. listFiles.push_back(configFile);
  196. }
  197. m_Makefile->AddCustomCommandToOutput(dspname.c_str(), dsprule.c_str(),
  198. argv, makefileIn.c_str(), listFiles,
  199. NULL, true);
  200. }
  201. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  202. const char *libName,
  203. cmTarget &target)
  204. {
  205. // if we should add regen rule then...
  206. const char *suppRegenRule =
  207. m_Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  208. if (!cmSystemTools::IsOn(suppRegenRule))
  209. {
  210. this->AddDSPBuildRule();
  211. }
  212. // for utility targets need custom command since post build doesn't
  213. // do anything (Visual Studio 7 seems to do this correctly without
  214. // the hack)
  215. if (target.GetType() == cmTarget::UTILITY &&
  216. target.GetPostBuildCommands().size())
  217. {
  218. int count = 1;
  219. for (std::vector<cmCustomCommand>::const_iterator cr =
  220. target.GetPostBuildCommands().begin();
  221. cr != target.GetPostBuildCommands().end(); ++cr)
  222. {
  223. cmCustomCommand cc(*cr);
  224. cc.ExpandVariables(*m_Makefile);
  225. char *output = new char [
  226. strlen(m_Makefile->GetStartOutputDirectory()) +
  227. strlen(libName) + 30];
  228. sprintf(output,"%s/%s_force_%i",
  229. m_Makefile->GetStartOutputDirectory(),
  230. libName, count);
  231. std::vector<std::string> args;
  232. args.push_back(cc.GetArguments());
  233. m_Makefile->AddCustomCommandToOutput(output,
  234. cc.GetCommand().c_str(),
  235. args,
  236. 0,
  237. cc.GetDepends());
  238. cmSourceFile* outsf =
  239. m_Makefile->GetSourceFileWithOutput(output);
  240. target.GetSourceFiles().push_back(outsf);
  241. count++;
  242. delete [] output;
  243. }
  244. }
  245. // trace the visual studio dependencies
  246. std::string name = libName;
  247. name += ".dsp.cmake";
  248. target.TraceVSDependencies(name, m_Makefile);
  249. // We may be modifying the source groups temporarily, so make a copy.
  250. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  251. // get the classes from the source lists then add them to the groups
  252. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  253. // now all of the source files have been properly assigned to the target
  254. // now stick them into source groups using the reg expressions
  255. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  256. i != classes.end(); i++)
  257. {
  258. // Add the file to the list of sources.
  259. std::string source = (*i)->GetFullPath();
  260. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  261. sourceGroups);
  262. sourceGroup.AssignSource(*i);
  263. // while we are at it, if it is a .rule file then for visual studio 6 we
  264. // must generate it
  265. if ((*i)->GetSourceExtension() == "rule")
  266. {
  267. if(!cmSystemTools::FileExists(source.c_str()))
  268. {
  269. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  270. #if defined(_WIN32) || defined(__CYGWIN__)
  271. std::ofstream fout(source.c_str(),
  272. std::ios::binary | std::ios::out | std::ios::trunc);
  273. #else
  274. std::ofstream fout(source.c_str(),
  275. std::ios::out | std::ios::trunc);
  276. #endif
  277. if(fout)
  278. {
  279. fout.write("# generated from CMake",22);
  280. fout.flush();
  281. fout.close();
  282. }
  283. }
  284. }
  285. }
  286. // Write the DSP file's header.
  287. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  288. // Loop through every source group.
  289. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  290. sg != sourceGroups.end(); ++sg)
  291. {
  292. const std::vector<const cmSourceFile *> &sourceFiles =
  293. sg->GetSourceFiles();
  294. // If the group is empty, don't write it at all.
  295. if(sourceFiles.empty())
  296. {
  297. continue;
  298. }
  299. // If the group has a name, write the header.
  300. std::string name = sg->GetName();
  301. if(name != "")
  302. {
  303. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  304. }
  305. // Loop through each source in the source group.
  306. for(std::vector<const cmSourceFile *>::const_iterator sf =
  307. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  308. {
  309. std::string source = (*sf)->GetFullPath();
  310. const cmCustomCommand *command =
  311. (*sf)->GetCustomCommand();
  312. std::string compileFlags;
  313. std::vector<std::string> depends;
  314. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  315. if(cflags)
  316. {
  317. compileFlags = cflags;
  318. }
  319. if(cmSystemTools::GetFileFormat((*sf)->GetSourceExtension().c_str())
  320. == cmSystemTools::CXX_FILE_FORMAT)
  321. {
  322. // force a C++ file type
  323. compileFlags += " /TP ";
  324. }
  325. // Check for extra object-file dependencies.
  326. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  327. if(dependsValue)
  328. {
  329. cmSystemTools::ExpandListArgument(dependsValue, depends);
  330. }
  331. if (source != libName || target.GetType() == cmTarget::UTILITY)
  332. {
  333. fout << "# Begin Source File\n\n";
  334. // Tell MS-Dev what the source is. If the compiler knows how to
  335. // build it, then it will.
  336. fout << "SOURCE=" <<
  337. cmSystemTools::ConvertToOutputPath(source.c_str()) << "\n\n";
  338. if(!depends.empty())
  339. {
  340. // Write out the dependencies for the rule.
  341. fout << "USERDEP__HACK=";
  342. for(std::vector<std::string>::const_iterator d = depends.begin();
  343. d != depends.end(); ++d)
  344. {
  345. fout << "\\\n\t" <<
  346. cmSystemTools::ConvertToOutputPath(d->c_str());
  347. }
  348. fout << "\n";
  349. }
  350. if (command)
  351. {
  352. std::string totalCommandStr;
  353. totalCommandStr =
  354. cmSystemTools::ConvertToOutputPath(command->GetCommand().c_str());
  355. totalCommandStr += " ";
  356. totalCommandStr += command->GetArguments();
  357. totalCommandStr += "\n";
  358. const char* comment = command->GetComment().c_str();
  359. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  360. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  361. (*comment?comment:"Custom Rule"),
  362. command->GetDepends(),
  363. command->GetOutput().c_str(), flags);
  364. }
  365. else if(compileFlags.size())
  366. {
  367. for(std::vector<std::string>::iterator i
  368. = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  369. {
  370. if (i == m_Configurations.begin())
  371. {
  372. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  373. }
  374. else
  375. {
  376. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  377. }
  378. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  379. }
  380. fout << "!ENDIF\n\n";
  381. }
  382. fout << "# End Source File\n";
  383. }
  384. }
  385. // If the group has a name, write the footer.
  386. if(name != "")
  387. {
  388. this->WriteDSPEndGroup(fout);
  389. }
  390. }
  391. // Write the DSP file's footer.
  392. this->WriteDSPFooter(fout);
  393. }
  394. void cmLocalVisualStudio6Generator::WriteCustomRule(std::ostream& fout,
  395. const char* source,
  396. const char* command,
  397. const char* comment,
  398. const std::vector<std::string>& depends,
  399. const char *output,
  400. const char* flags
  401. )
  402. {
  403. std::vector<std::string>::iterator i;
  404. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  405. {
  406. if (i == m_Configurations.begin())
  407. {
  408. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  409. }
  410. else
  411. {
  412. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  413. }
  414. if(flags)
  415. {
  416. fout << "\n# ADD CPP " << flags << "\n\n";
  417. }
  418. // Write out the dependencies for the rule.
  419. fout << "USERDEP__HACK=";
  420. for(std::vector<std::string>::const_iterator d = depends.begin();
  421. d != depends.end(); ++d)
  422. {
  423. std::string dep = cmSystemTools::GetFilenameName(*d);
  424. if (cmSystemTools::GetFilenameLastExtension(dep) == ".exe")
  425. {
  426. dep = cmSystemTools::GetFilenameWithoutLastExtension(dep);
  427. }
  428. std::string libPath = dep + "_CMAKE_PATH";
  429. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  430. if (cacheValue)
  431. {
  432. std::string exePath = "";
  433. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  434. {
  435. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  436. }
  437. if(exePath.size())
  438. {
  439. libPath = exePath;
  440. }
  441. else
  442. {
  443. libPath = cacheValue;
  444. }
  445. libPath += "/";
  446. libPath += "$(INTDIR)/";
  447. libPath += dep;
  448. libPath += ".exe";
  449. fout << "\\\n\t" <<
  450. cmSystemTools::ConvertToOutputPath(libPath.c_str());
  451. }
  452. else
  453. {
  454. fout << "\\\n\t" <<
  455. cmSystemTools::ConvertToOutputPath(d->c_str());
  456. }
  457. }
  458. fout << "\n";
  459. fout << "# PROP Ignore_Default_Tool 1\n";
  460. fout << "# Begin Custom Build - Building " << comment
  461. << " $(InputPath)\n\n";
  462. if(output == 0)
  463. {
  464. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  465. fout << command << "\n\n";
  466. }
  467. // Write a rule for every output generated by this command.
  468. fout << cmSystemTools::ConvertToOutputPath(output)
  469. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  470. fout << command << "\n\n";
  471. fout << "# End Custom Build\n\n";
  472. }
  473. fout << "!ENDIF\n\n";
  474. }
  475. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  476. const char* group,
  477. const char* filter)
  478. {
  479. fout << "# Begin Group \"" << group << "\"\n"
  480. "# PROP Default_Filter \"" << filter << "\"\n";
  481. }
  482. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  483. {
  484. fout << "# End Group\n";
  485. }
  486. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  487. const char* libName,
  488. const cmTarget& target)
  489. {
  490. std::string root= m_Makefile->GetDefinition("CMAKE_ROOT");
  491. const char *def= m_Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  492. std::string exportSymbol;
  493. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  494. {
  495. exportSymbol = custom_export_name;
  496. }
  497. else
  498. {
  499. std::string in = libName;
  500. in += "_EXPORTS";
  501. exportSymbol = cmSystemTools::MakeCindentifier(in.c_str());
  502. }
  503. if( def)
  504. {
  505. root = def;
  506. }
  507. else
  508. {
  509. root += "/Templates";
  510. }
  511. switch(b)
  512. {
  513. case STATIC_LIBRARY:
  514. m_DSPHeaderTemplate = root;
  515. m_DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  516. m_DSPFooterTemplate = root;
  517. m_DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  518. break;
  519. case DLL:
  520. m_DSPHeaderTemplate = root;
  521. m_DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  522. m_DSPFooterTemplate = root;
  523. m_DSPFooterTemplate += "/DLLFooter.dsptemplate";
  524. break;
  525. case EXECUTABLE:
  526. m_DSPHeaderTemplate = root;
  527. m_DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  528. m_DSPFooterTemplate = root;
  529. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  530. break;
  531. case WIN32_EXECUTABLE:
  532. m_DSPHeaderTemplate = root;
  533. m_DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  534. m_DSPFooterTemplate = root;
  535. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  536. break;
  537. case UTILITY:
  538. m_DSPHeaderTemplate = root;
  539. m_DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  540. m_DSPFooterTemplate = root;
  541. m_DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  542. break;
  543. }
  544. // once the build type is set, determine what configurations are
  545. // possible
  546. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  547. cmsys::RegularExpression reg("# Name ");
  548. if(!fin)
  549. {
  550. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  551. }
  552. // reset m_Configurations
  553. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  554. // now add all the configurations possible
  555. std::string line;
  556. while(cmSystemTools::GetLineFromStream(fin, line))
  557. {
  558. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  559. exportSymbol.c_str());
  560. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  561. if (reg.find(line))
  562. {
  563. m_Configurations.push_back(line.substr(reg.end()));
  564. }
  565. }
  566. }
  567. // look for custom rules on a target and collect them together
  568. std::string
  569. cmLocalVisualStudio6Generator::CreateTargetRules(const cmTarget &target,
  570. const char * /* libName */)
  571. {
  572. std::string customRuleCode = "";
  573. if (target.GetType() >= cmTarget::UTILITY)
  574. {
  575. return customRuleCode;
  576. }
  577. // are there any rules?
  578. if (target.GetPreBuildCommands().size() +
  579. target.GetPreLinkCommands().size() +
  580. target.GetPostBuildCommands().size() == 0)
  581. {
  582. return customRuleCode;
  583. }
  584. customRuleCode = "# Begin Special Build Tool\n";
  585. // Do the PreBuild and PreLink (VS6 does not support both)
  586. bool init = false;
  587. for (std::vector<cmCustomCommand>::const_iterator cr =
  588. target.GetPreBuildCommands().begin();
  589. cr != target.GetPreBuildCommands().end(); ++cr)
  590. {
  591. cmCustomCommand cc(*cr);
  592. cc.ExpandVariables(*m_Makefile);
  593. if (!init)
  594. {
  595. // header stuff
  596. customRuleCode += "PreLink_Cmds=";
  597. init = true;
  598. }
  599. else
  600. {
  601. customRuleCode += "\t";
  602. }
  603. customRuleCode += cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str()) + " " + cc.GetArguments();
  604. }
  605. for (std::vector<cmCustomCommand>::const_iterator cr =
  606. target.GetPreLinkCommands().begin();
  607. cr != target.GetPreLinkCommands().end(); ++cr)
  608. {
  609. cmCustomCommand cc(*cr);
  610. cc.ExpandVariables(*m_Makefile);
  611. if (!init)
  612. {
  613. // header stuff
  614. customRuleCode += "PreLink_Cmds=";
  615. init = true;
  616. }
  617. else
  618. {
  619. customRuleCode += "\t";
  620. }
  621. customRuleCode += cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str()) + " " + cc.GetArguments();
  622. }
  623. // do the post build rules
  624. init = false;
  625. for (std::vector<cmCustomCommand>::const_iterator cr =
  626. target.GetPostBuildCommands().begin();
  627. cr != target.GetPostBuildCommands().end(); ++cr)
  628. {
  629. cmCustomCommand cc(*cr);
  630. cc.ExpandVariables(*m_Makefile);
  631. if (!init)
  632. {
  633. // header stuff
  634. customRuleCode += "PostBuild_Cmds=";
  635. init = true;
  636. }
  637. else
  638. {
  639. customRuleCode += "\t";
  640. }
  641. customRuleCode +=
  642. cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str()) +
  643. " " + cc.GetArguments();
  644. }
  645. customRuleCode += "\n# End Special Build Tool\n";
  646. return customRuleCode;
  647. }
  648. inline std::string removeQuotes(const std::string& s)
  649. {
  650. if(s[0] == '\"' && s[s.size()-1] == '\"')
  651. {
  652. return s.substr(1, s.size()-2);
  653. }
  654. return s;
  655. }
  656. void cmLocalVisualStudio6Generator::WriteDSPHeader(std::ostream& fout, const char *libName,
  657. const cmTarget &target,
  658. std::vector<cmSourceGroup> &)
  659. {
  660. std::set<std::string> pathEmitted;
  661. // determine the link directories
  662. std::string libOptions;
  663. std::string libDebugOptions;
  664. std::string libOptimizedOptions;
  665. std::string libMultiLineOptions;
  666. std::string libMultiLineDebugOptions;
  667. std::string libMultiLineOptimizedOptions;
  668. // suppoirt override in output directory
  669. std::string libPath = "";
  670. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  671. {
  672. libPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  673. }
  674. std::string exePath = "";
  675. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  676. {
  677. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  678. }
  679. if(libPath.size())
  680. {
  681. // make sure there is a trailing slash
  682. if(libPath[libPath.size()-1] != '/')
  683. {
  684. libPath += "/";
  685. }
  686. std::string lpath =
  687. cmSystemTools::ConvertToOutputPath(libPath.c_str());
  688. std::string lpathIntDir = libPath + "$(INTDIR)";
  689. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  690. if(pathEmitted.insert(lpath).second)
  691. {
  692. libOptions += " /LIBPATH:";
  693. libOptions += lpathIntDir;
  694. libOptions += " ";
  695. libOptions += " /LIBPATH:";
  696. libOptions += lpath;
  697. libOptions += " ";
  698. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  699. libMultiLineOptions += lpathIntDir;
  700. libMultiLineOptions += " ";
  701. libMultiLineOptions += " /LIBPATH:";
  702. libMultiLineOptions += lpath;
  703. libMultiLineOptions += " \n";
  704. }
  705. }
  706. if(exePath.size())
  707. {
  708. // make sure there is a trailing slash
  709. if(exePath[exePath.size()-1] != '/')
  710. {
  711. exePath += "/";
  712. }
  713. std::string lpath =
  714. cmSystemTools::ConvertToOutputPath(exePath.c_str());
  715. std::string lpathIntDir = exePath + "$(INTDIR)";
  716. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  717. if(pathEmitted.insert(lpath).second)
  718. {
  719. libOptions += " /LIBPATH:";
  720. libOptions += lpathIntDir;
  721. libOptions += " ";
  722. libOptions += " /LIBPATH:";
  723. libOptions += lpath;
  724. libOptions += " ";
  725. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  726. libMultiLineOptions += lpathIntDir;
  727. libMultiLineOptions += " ";
  728. libMultiLineOptions += " /LIBPATH:";
  729. libMultiLineOptions += lpath;
  730. libMultiLineOptions += " \n";
  731. }
  732. }
  733. std::vector<std::string>::const_iterator i;
  734. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  735. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  736. {
  737. std::string path = *i;
  738. if(path[path.size()-1] != '/')
  739. {
  740. path += "/";
  741. }
  742. std::string lpath =
  743. cmSystemTools::ConvertToOutputPath(path.c_str());
  744. std::string lpathIntDir = path + "$(INTDIR)";
  745. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  746. if(pathEmitted.insert(lpath).second)
  747. {
  748. libOptions += " /LIBPATH:";
  749. libOptions += lpathIntDir;
  750. libOptions += " ";
  751. libOptions += " /LIBPATH:";
  752. libOptions += lpath;
  753. libOptions += " ";
  754. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  755. libMultiLineOptions += lpathIntDir;
  756. libMultiLineOptions += " ";
  757. libMultiLineOptions += " /LIBPATH:";
  758. libMultiLineOptions += lpath;
  759. libMultiLineOptions += " \n";
  760. }
  761. }
  762. // find link libraries
  763. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  764. cmTarget::LinkLibraries::const_iterator j;
  765. for(j = libs.begin(); j != libs.end(); ++j)
  766. {
  767. // add libraries to executables and dlls (but never include
  768. // a library in a library, bad recursion)
  769. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  770. && target.GetType() != cmTarget::STATIC_LIBRARY
  771. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  772. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  773. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  774. {
  775. std::string lib = j->first;
  776. if(j->first.find(".lib") == std::string::npos)
  777. {
  778. lib += ".lib";
  779. }
  780. lib = cmSystemTools::ConvertToOutputPath(lib.c_str());
  781. if (j->second == cmTarget::GENERAL)
  782. {
  783. libOptions += " ";
  784. libOptions += lib;
  785. libMultiLineOptions += "# ADD LINK32 ";
  786. libMultiLineOptions += lib;
  787. libMultiLineOptions += "\n";
  788. }
  789. if (j->second == cmTarget::DEBUG)
  790. {
  791. libDebugOptions += " ";
  792. libDebugOptions += lib;
  793. libMultiLineDebugOptions += "# ADD LINK32 ";
  794. libMultiLineDebugOptions += lib;
  795. libMultiLineDebugOptions += "\n";
  796. }
  797. if (j->second == cmTarget::OPTIMIZED)
  798. {
  799. libOptimizedOptions += " ";
  800. libOptimizedOptions += lib;
  801. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  802. libMultiLineOptimizedOptions += lib;
  803. libMultiLineOptimizedOptions += "\n";
  804. }
  805. }
  806. }
  807. std::string extraLinkOptions;
  808. if(target.GetType() == cmTarget::EXECUTABLE)
  809. {
  810. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_EXE_LINKER_FLAGS");
  811. }
  812. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  813. {
  814. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_SHARED_LINKER_FLAGS");
  815. }
  816. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  817. {
  818. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_MODULE_LINKER_FLAGS");
  819. }
  820. if(extraLinkOptions.size())
  821. {
  822. libOptions += " ";
  823. libOptions += extraLinkOptions;
  824. libOptions += " ";
  825. libMultiLineOptions += "# ADD LINK32 ";
  826. libMultiLineOptions += extraLinkOptions;
  827. libMultiLineOptions += " \n";
  828. }
  829. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  830. {
  831. libOptions += " ";
  832. libOptions += targetLinkFlags;
  833. libOptions += " ";
  834. libMultiLineOptions += "# ADD LINK32 ";
  835. libMultiLineOptions += targetLinkFlags;
  836. libMultiLineOptions += " \n";
  837. }
  838. // are there any custom rules on the target itself
  839. // only if the target is a lib or exe
  840. std::string customRuleCode = this->CreateTargetRules(target, libName);
  841. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  842. if(!fin)
  843. {
  844. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  845. }
  846. std::string line;
  847. while(cmSystemTools::GetLineFromStream(fin, line))
  848. {
  849. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  850. if(!mfcFlag)
  851. {
  852. mfcFlag = "0";
  853. }
  854. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  855. customRuleCode.c_str());
  856. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  857. mfcFlag);
  858. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  859. libOptions.c_str());
  860. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  861. libDebugOptions.c_str());
  862. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  863. libOptimizedOptions.c_str());
  864. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  865. libMultiLineOptions.c_str());
  866. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  867. libMultiLineDebugOptions.c_str());
  868. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  869. libMultiLineOptimizedOptions.c_str());
  870. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  871. m_IncludeOptions.c_str());
  872. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  873. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  874. // are already quoted in the template file,
  875. // we need to remove the quotes here, we still need
  876. // to convert to output path for unix to win32 conversion
  877. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  878. removeQuotes(
  879. cmSystemTools::ConvertToOutputPath(libPath.c_str())).c_str());
  880. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  881. removeQuotes(
  882. cmSystemTools::ConvertToOutputPath(exePath.c_str())).c_str());
  883. cmSystemTools::ReplaceString(line,
  884. "EXTRA_DEFINES",
  885. m_Makefile->GetDefineFlags());
  886. cmGlobalGenerator* gen = this->GetGlobalGenerator();
  887. // store flags for each configuration
  888. std::string flags = " ";
  889. std::string flagsRelease = " ";
  890. std::string flagsMinSize = " ";
  891. std::string flagsDebug = " ";
  892. std::string flagsDebugRel = " ";
  893. // if CXX is on and the target contains cxx code then add the cxx flags
  894. if ( gen->GetLanguageEnabled("CXX") && target.HasCxx() )
  895. {
  896. flagsRelease = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_RELEASE");
  897. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  898. flagsMinSize = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_MINSIZEREL");
  899. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  900. flagsDebug = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_DEBUG");
  901. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  902. flagsDebugRel = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_RELWITHDEBINFO");
  903. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  904. flags = " ";
  905. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS");
  906. }
  907. // if C and the target is not CXX
  908. else if(gen->GetLanguageEnabled("C") && !target.HasCxx())
  909. {
  910. flagsRelease += m_Makefile->GetDefinition("CMAKE_C_FLAGS_RELEASE");
  911. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\"";
  912. flagsMinSize += m_Makefile->GetDefinition("CMAKE_C_FLAGS_MINSIZEREL");
  913. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\"";
  914. flagsDebug += m_Makefile->GetDefinition("CMAKE_C_FLAGS_DEBUG");
  915. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\"";
  916. flagsDebugRel += m_Makefile->GetDefinition("CMAKE_C_FLAGS_RELWITHDEBINFO");
  917. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\"";
  918. flags = " ";
  919. flags = m_Makefile->GetDefinition("CMAKE_C_FLAGS");
  920. }
  921. // if unicode is not found, then add -D_MBCS
  922. if(flags.find("D_UNICODE") == flags.npos)
  923. {
  924. flags += " /D \"_MBCS\"";
  925. }
  926. // The template files have CXX FLAGS in them, that need to be replaced.
  927. // There are not separate CXX and C template files, so we use the same
  928. // variable names. The previous code sets up flags* variables to contain
  929. // the correct C or CXX flags
  930. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE", flagsRelease.c_str());
  931. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL", flagsMinSize.c_str());
  932. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG", flagsDebug.c_str());
  933. cmSystemTools::ReplaceString(line,"CMAKE_CXX_FLAGS_RELWITHDEBINFO", flagsDebugRel.c_str());
  934. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  935. fout << line.c_str() << std::endl;
  936. }
  937. }
  938. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  939. {
  940. std::ifstream fin(m_DSPFooterTemplate.c_str());
  941. if(!fin)
  942. {
  943. cmSystemTools::Error("Error Reading ",
  944. m_DSPFooterTemplate.c_str());
  945. }
  946. std::string line;
  947. while(cmSystemTools::GetLineFromStream(fin, line))
  948. {
  949. fout << line << std::endl;
  950. }
  951. }