cmLocalVisualStudio6Generator.cxx 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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 <queue>
  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. // We may be modifying the source groups temporarily, so make a copy.
  206. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  207. // if we should add regen rule then...
  208. const char *suppRegenRule =
  209. m_Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  210. if (!cmSystemTools::IsOn(suppRegenRule))
  211. {
  212. this->AddDSPBuildRule();
  213. }
  214. // get the classes from the source lists then add them to the groups
  215. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  216. // use a deck to keep track of processed source files
  217. std::queue<std::string> srcFilesToProcess;
  218. std::string name;
  219. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  220. i != classes.end(); ++i)
  221. {
  222. name = (*i)->GetSourceName();
  223. if ((*i)->GetSourceExtension() != "rule")
  224. {
  225. name += ".";
  226. name += (*i)->GetSourceExtension();
  227. }
  228. srcFilesToProcess.push(name);
  229. }
  230. name = libName;
  231. name += ".dsp.cmake";
  232. srcFilesToProcess.push(name);
  233. // add in the library depends for cusotm targets
  234. if (target.GetType() == cmTarget::UTILITY)
  235. {
  236. for (std::vector<cmCustomCommand>::iterator ic =
  237. target.GetPostBuildCommands().begin();
  238. ic != target.GetPostBuildCommands().end(); ++ic)
  239. {
  240. cmCustomCommand &c = *ic;
  241. for (std::vector<std::string>::iterator i = c.GetDepends().begin();
  242. i != c.GetDepends().end(); ++i)
  243. {
  244. srcFilesToProcess.push(*i);
  245. }
  246. }
  247. }
  248. while (!srcFilesToProcess.empty())
  249. {
  250. // is this source the output of a custom command
  251. cmSourceFile* outsf =
  252. m_Makefile->GetSourceFileWithOutput(srcFilesToProcess.front().c_str());
  253. if (outsf)
  254. {
  255. // is it not already in the target?
  256. if (std::find(classes.begin(),classes.end(),outsf) == classes.end())
  257. {
  258. // then add the source to this target and add it to the queue
  259. classes.push_back(outsf);
  260. std::string name = outsf->GetSourceName();
  261. if (outsf->GetSourceExtension() != "rule")
  262. {
  263. name += ".";
  264. name += outsf->GetSourceExtension();
  265. }
  266. std::string temp =
  267. cmSystemTools::GetFilenamePath(outsf->GetFullPath());
  268. temp += "/";
  269. temp += name;
  270. srcFilesToProcess.push(temp);
  271. }
  272. // add its dependencies to the list to check
  273. unsigned int i;
  274. for (i = 0; i < outsf->GetCustomCommand()->GetDepends().size(); ++i)
  275. {
  276. std::string dep = cmSystemTools::GetFilenameName(
  277. outsf->GetCustomCommand()->GetDepends()[i]);
  278. if (cmSystemTools::GetFilenameLastExtension(dep) == ".exe")
  279. {
  280. dep = cmSystemTools::GetFilenameWithoutLastExtension(dep);
  281. }
  282. // watch for target dependencies,
  283. std::string libPath = dep + "_CMAKE_PATH";
  284. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  285. if (cacheValue)
  286. {
  287. // add the depend as a utility on the target
  288. target.AddUtility(dep.c_str());
  289. }
  290. else
  291. {
  292. srcFilesToProcess.push(outsf->GetCustomCommand()->GetDepends()[i]);
  293. }
  294. }
  295. }
  296. // finished with this SF move to the next
  297. srcFilesToProcess.pop();
  298. }
  299. // now all of the source files have been properly assigned to the target
  300. // now stick them into source groups using the reg expressions
  301. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  302. i != classes.end(); i++)
  303. {
  304. // Add the file to the list of sources.
  305. std::string source = (*i)->GetFullPath();
  306. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  307. sourceGroups);
  308. sourceGroup.AddSource(source.c_str(), *i);
  309. // while we are at it, if it is a .rule file then for visual studio 6 we
  310. // must generate it
  311. if ((*i)->GetSourceExtension() == "rule")
  312. {
  313. if(!cmSystemTools::FileExists(source.c_str()))
  314. {
  315. #if defined(_WIN32) || defined(__CYGWIN__)
  316. std::ofstream fout(source.c_str(),
  317. std::ios::binary | std::ios::out | std::ios::trunc);
  318. #else
  319. std::ofstream fout(source.c_str(),
  320. std::ios::out | std::ios::trunc);
  321. #endif
  322. if(fout)
  323. {
  324. fout.write("# generated from CMake",22);
  325. fout.flush();
  326. fout.close();
  327. }
  328. }
  329. }
  330. }
  331. // Write the DSP file's header.
  332. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  333. // Loop through every source group.
  334. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  335. sg != sourceGroups.end(); ++sg)
  336. {
  337. const std::vector<const cmSourceFile *> &sourceFiles =
  338. sg->GetSourceFiles();
  339. // If the group is empty, don't write it at all.
  340. if(sourceFiles.empty())
  341. {
  342. continue;
  343. }
  344. // If the group has a name, write the header.
  345. std::string name = sg->GetName();
  346. if(name != "")
  347. {
  348. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  349. }
  350. // Loop through each source in the source group.
  351. for(std::vector<const cmSourceFile *>::const_iterator sf =
  352. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  353. {
  354. std::string source = (*sf)->GetFullPath();
  355. const cmCustomCommand *command =
  356. (*sf)->GetCustomCommand();
  357. std::string compileFlags;
  358. std::vector<std::string> depends;
  359. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  360. if(cflags)
  361. {
  362. compileFlags = cflags;
  363. }
  364. if(cmSystemTools::GetFileFormat((*sf)->GetSourceExtension().c_str())
  365. == cmSystemTools::CXX_FILE_FORMAT)
  366. {
  367. // force a C++ file type
  368. compileFlags += " /TP ";
  369. }
  370. // Check for extra object-file dependencies.
  371. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  372. if(dependsValue)
  373. {
  374. cmSystemTools::ExpandListArgument(dependsValue, depends);
  375. }
  376. if (source != libName || target.GetType() == cmTarget::UTILITY)
  377. {
  378. fout << "# Begin Source File\n\n";
  379. // Tell MS-Dev what the source is. If the compiler knows how to
  380. // build it, then it will.
  381. fout << "SOURCE=" <<
  382. cmSystemTools::ConvertToOutputPath(source.c_str()) << "\n\n";
  383. if(!depends.empty())
  384. {
  385. // Write out the dependencies for the rule.
  386. fout << "USERDEP__HACK=";
  387. for(std::vector<std::string>::const_iterator d = depends.begin();
  388. d != depends.end(); ++d)
  389. {
  390. fout << "\\\n\t" <<
  391. cmSystemTools::ConvertToOutputPath(d->c_str());
  392. }
  393. fout << "\n";
  394. }
  395. if (command)
  396. {
  397. std::string totalCommandStr;
  398. totalCommandStr =
  399. cmSystemTools::ConvertToOutputPath(command->GetCommand().c_str());
  400. totalCommandStr += " ";
  401. totalCommandStr += command->GetArguments();
  402. totalCommandStr += "\n";
  403. const char* comment = command->GetComment().c_str();
  404. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  405. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  406. (*comment?comment:"Custom Rule"),
  407. command->GetDepends(),
  408. command->GetOutput().c_str(), flags);
  409. }
  410. else if(compileFlags.size())
  411. {
  412. for(std::vector<std::string>::iterator i
  413. = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  414. {
  415. if (i == m_Configurations.begin())
  416. {
  417. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  418. }
  419. else
  420. {
  421. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  422. }
  423. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  424. }
  425. fout << "!ENDIF\n\n";
  426. }
  427. fout << "# End Source File\n";
  428. }
  429. }
  430. // If the group has a name, write the footer.
  431. if(name != "")
  432. {
  433. this->WriteDSPEndGroup(fout);
  434. }
  435. }
  436. // Write the DSP file's footer.
  437. this->WriteDSPFooter(fout);
  438. }
  439. void cmLocalVisualStudio6Generator::WriteCustomRule(std::ostream& fout,
  440. const char* source,
  441. const char* command,
  442. const char* comment,
  443. const std::vector<std::string>& depends,
  444. const char *output,
  445. const char* flags
  446. )
  447. {
  448. std::vector<std::string>::iterator i;
  449. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  450. {
  451. if (i == m_Configurations.begin())
  452. {
  453. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  454. }
  455. else
  456. {
  457. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  458. }
  459. if(flags)
  460. {
  461. fout << "\n# ADD CPP " << flags << "\n\n";
  462. }
  463. // Write out the dependencies for the rule.
  464. fout << "USERDEP__HACK=";
  465. for(std::vector<std::string>::const_iterator d = depends.begin();
  466. d != depends.end(); ++d)
  467. {
  468. fout << "\\\n\t" <<
  469. cmSystemTools::ConvertToOutputPath(d->c_str());
  470. }
  471. fout << "\n";
  472. fout << "# PROP Ignore_Default_Tool 1\n";
  473. fout << "# Begin Custom Build - Building " << comment
  474. << " $(InputPath)\n\n";
  475. if(output == 0)
  476. {
  477. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  478. fout << command << "\n\n";
  479. }
  480. // Write a rule for every output generated by this command.
  481. fout << cmSystemTools::ConvertToOutputPath(output)
  482. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  483. fout << command << "\n\n";
  484. fout << "# End Custom Build\n\n";
  485. }
  486. fout << "!ENDIF\n\n";
  487. }
  488. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  489. const char* group,
  490. const char* filter)
  491. {
  492. fout << "# Begin Group \"" << group << "\"\n"
  493. "# PROP Default_Filter \"" << filter << "\"\n";
  494. }
  495. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  496. {
  497. fout << "# End Group\n";
  498. }
  499. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  500. const char* libName,
  501. const cmTarget& target)
  502. {
  503. std::string root= m_Makefile->GetDefinition("CMAKE_ROOT");
  504. const char *def= m_Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  505. std::string exportSymbol;
  506. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  507. {
  508. exportSymbol = custom_export_name;
  509. }
  510. else
  511. {
  512. std::string in = libName;
  513. in += "_EXPORTS";
  514. exportSymbol = cmSystemTools::MakeCindentifier(in.c_str());
  515. }
  516. if( def)
  517. {
  518. root = def;
  519. }
  520. else
  521. {
  522. root += "/Templates";
  523. }
  524. switch(b)
  525. {
  526. case STATIC_LIBRARY:
  527. m_DSPHeaderTemplate = root;
  528. m_DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  529. m_DSPFooterTemplate = root;
  530. m_DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  531. break;
  532. case DLL:
  533. m_DSPHeaderTemplate = root;
  534. m_DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  535. m_DSPFooterTemplate = root;
  536. m_DSPFooterTemplate += "/DLLFooter.dsptemplate";
  537. break;
  538. case EXECUTABLE:
  539. m_DSPHeaderTemplate = root;
  540. m_DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  541. m_DSPFooterTemplate = root;
  542. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  543. break;
  544. case WIN32_EXECUTABLE:
  545. m_DSPHeaderTemplate = root;
  546. m_DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  547. m_DSPFooterTemplate = root;
  548. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  549. break;
  550. case UTILITY:
  551. m_DSPHeaderTemplate = root;
  552. m_DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  553. m_DSPFooterTemplate = root;
  554. m_DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  555. break;
  556. }
  557. // once the build type is set, determine what configurations are
  558. // possible
  559. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  560. cmRegularExpression reg("# Name ");
  561. if(!fin)
  562. {
  563. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  564. }
  565. // reset m_Configurations
  566. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  567. // now add all the configurations possible
  568. std::string line;
  569. while(cmSystemTools::GetLineFromStream(fin, line))
  570. {
  571. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  572. exportSymbol.c_str());
  573. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  574. if (reg.find(line))
  575. {
  576. m_Configurations.push_back(line.substr(reg.end()));
  577. }
  578. }
  579. }
  580. // look for custom rules on a target and collect them together
  581. std::string
  582. cmLocalVisualStudio6Generator::CreateTargetRules(const cmTarget &target,
  583. const char * /* libName */)
  584. {
  585. std::string customRuleCode = "";
  586. if (target.GetType() > cmTarget::UTILITY)
  587. {
  588. return customRuleCode;
  589. }
  590. // are there any rules?
  591. if (target.GetPreBuildCommands().size() +
  592. target.GetPreLinkCommands().size() +
  593. target.GetPostBuildCommands().size() == 0)
  594. {
  595. return customRuleCode;
  596. }
  597. customRuleCode = "# Begin Special Build Tool\n";
  598. // Do the PreBuild and PreLink (VS6 does not support both)
  599. bool init = false;
  600. for (std::vector<cmCustomCommand>::const_iterator cr =
  601. target.GetPreBuildCommands().begin();
  602. cr != target.GetPreBuildCommands().end(); ++cr)
  603. {
  604. cmCustomCommand cc(*cr);
  605. cc.ExpandVariables(*m_Makefile);
  606. if (!init)
  607. {
  608. // header stuff
  609. customRuleCode += "PreLink_Cmds=";
  610. init = true;
  611. }
  612. else
  613. {
  614. customRuleCode += "\t";
  615. }
  616. customRuleCode += cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str()) + " " + cc.GetArguments();
  617. }
  618. for (std::vector<cmCustomCommand>::const_iterator cr =
  619. target.GetPreLinkCommands().begin();
  620. cr != target.GetPreLinkCommands().end(); ++cr)
  621. {
  622. cmCustomCommand cc(*cr);
  623. cc.ExpandVariables(*m_Makefile);
  624. if (!init)
  625. {
  626. // header stuff
  627. customRuleCode += "PreLink_Cmds=";
  628. init = true;
  629. }
  630. else
  631. {
  632. customRuleCode += "\t";
  633. }
  634. customRuleCode += cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str()) + " " + cc.GetArguments();
  635. }
  636. // do the post build rules
  637. init = false;
  638. for (std::vector<cmCustomCommand>::const_iterator cr =
  639. target.GetPostBuildCommands().begin();
  640. cr != target.GetPostBuildCommands().end(); ++cr)
  641. {
  642. cmCustomCommand cc(*cr);
  643. cc.ExpandVariables(*m_Makefile);
  644. if (!init)
  645. {
  646. // header stuff
  647. customRuleCode += "PostBuild_Cmds=";
  648. init = true;
  649. }
  650. else
  651. {
  652. customRuleCode += "\t";
  653. }
  654. customRuleCode +=
  655. cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str()) +
  656. " " + cc.GetArguments();
  657. }
  658. customRuleCode += "\n# End Special Build Tool\n";
  659. return customRuleCode;
  660. }
  661. inline std::string removeQuotes(const std::string& s)
  662. {
  663. if(s[0] == '\"' && s[s.size()-1] == '\"')
  664. {
  665. return s.substr(1, s.size()-2);
  666. }
  667. return s;
  668. }
  669. void cmLocalVisualStudio6Generator::WriteDSPHeader(std::ostream& fout, const char *libName,
  670. const cmTarget &target,
  671. std::vector<cmSourceGroup> &)
  672. {
  673. std::set<std::string> pathEmitted;
  674. // determine the link directories
  675. std::string libOptions;
  676. std::string libDebugOptions;
  677. std::string libOptimizedOptions;
  678. std::string libMultiLineOptions;
  679. std::string libMultiLineDebugOptions;
  680. std::string libMultiLineOptimizedOptions;
  681. // suppoirt override in output directory
  682. std::string libPath = "";
  683. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  684. {
  685. libPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  686. }
  687. std::string exePath = "";
  688. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  689. {
  690. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  691. }
  692. if(libPath.size())
  693. {
  694. // make sure there is a trailing slash
  695. if(libPath[libPath.size()-1] != '/')
  696. {
  697. libPath += "/";
  698. }
  699. std::string lpath =
  700. cmSystemTools::ConvertToOutputPath(libPath.c_str());
  701. std::string lpathIntDir = libPath + "$(INTDIR)";
  702. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  703. if(pathEmitted.insert(lpath).second)
  704. {
  705. libOptions += " /LIBPATH:";
  706. libOptions += lpathIntDir;
  707. libOptions += " ";
  708. libOptions += " /LIBPATH:";
  709. libOptions += lpath;
  710. libOptions += " ";
  711. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  712. libMultiLineOptions += lpathIntDir;
  713. libMultiLineOptions += " ";
  714. libMultiLineOptions += " /LIBPATH:";
  715. libMultiLineOptions += lpath;
  716. libMultiLineOptions += " \n";
  717. }
  718. }
  719. if(exePath.size())
  720. {
  721. // make sure there is a trailing slash
  722. if(exePath[exePath.size()-1] != '/')
  723. {
  724. exePath += "/";
  725. }
  726. std::string lpath =
  727. cmSystemTools::ConvertToOutputPath(exePath.c_str());
  728. std::string lpathIntDir = exePath + "$(INTDIR)";
  729. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  730. if(pathEmitted.insert(lpath).second)
  731. {
  732. libOptions += " /LIBPATH:";
  733. libOptions += lpathIntDir;
  734. libOptions += " ";
  735. libOptions += " /LIBPATH:";
  736. libOptions += lpath;
  737. libOptions += " ";
  738. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  739. libMultiLineOptions += lpathIntDir;
  740. libMultiLineOptions += " ";
  741. libMultiLineOptions += " /LIBPATH:";
  742. libMultiLineOptions += lpath;
  743. libMultiLineOptions += " \n";
  744. }
  745. }
  746. std::vector<std::string>::const_iterator i;
  747. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  748. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  749. {
  750. std::string path = *i;
  751. if(path[path.size()-1] != '/')
  752. {
  753. path += "/";
  754. }
  755. std::string lpath =
  756. cmSystemTools::ConvertToOutputPath(path.c_str());
  757. std::string lpathIntDir = path + "$(INTDIR)";
  758. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  759. if(pathEmitted.insert(lpath).second)
  760. {
  761. libOptions += " /LIBPATH:";
  762. libOptions += lpathIntDir;
  763. libOptions += " ";
  764. libOptions += " /LIBPATH:";
  765. libOptions += lpath;
  766. libOptions += " ";
  767. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  768. libMultiLineOptions += lpathIntDir;
  769. libMultiLineOptions += " ";
  770. libMultiLineOptions += " /LIBPATH:";
  771. libMultiLineOptions += lpath;
  772. libMultiLineOptions += " \n";
  773. }
  774. }
  775. // find link libraries
  776. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  777. cmTarget::LinkLibraries::const_iterator j;
  778. for(j = libs.begin(); j != libs.end(); ++j)
  779. {
  780. // add libraries to executables and dlls (but never include
  781. // a library in a library, bad recursion)
  782. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  783. && target.GetType() != cmTarget::STATIC_LIBRARY
  784. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  785. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  786. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  787. {
  788. std::string lib = j->first;
  789. if(j->first.find(".lib") == std::string::npos)
  790. {
  791. lib += ".lib";
  792. }
  793. lib = cmSystemTools::ConvertToOutputPath(lib.c_str());
  794. if (j->second == cmTarget::GENERAL)
  795. {
  796. libOptions += " ";
  797. libOptions += lib;
  798. libMultiLineOptions += "# ADD LINK32 ";
  799. libMultiLineOptions += lib;
  800. libMultiLineOptions += "\n";
  801. }
  802. if (j->second == cmTarget::DEBUG)
  803. {
  804. libDebugOptions += " ";
  805. libDebugOptions += lib;
  806. libMultiLineDebugOptions += "# ADD LINK32 ";
  807. libMultiLineDebugOptions += lib;
  808. libMultiLineDebugOptions += "\n";
  809. }
  810. if (j->second == cmTarget::OPTIMIZED)
  811. {
  812. libOptimizedOptions += " ";
  813. libOptimizedOptions += lib;
  814. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  815. libMultiLineOptimizedOptions += lib;
  816. libMultiLineOptimizedOptions += "\n";
  817. }
  818. }
  819. }
  820. std::string extraLinkOptions;
  821. if(target.GetType() == cmTarget::EXECUTABLE)
  822. {
  823. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_EXE_LINKER_FLAGS");
  824. }
  825. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  826. {
  827. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_SHARED_LINKER_FLAGS");
  828. }
  829. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  830. {
  831. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_MODULE_LINKER_FLAGS");
  832. }
  833. if(extraLinkOptions.size())
  834. {
  835. libOptions += " ";
  836. libOptions += extraLinkOptions;
  837. libOptions += " ";
  838. libMultiLineOptions += "# ADD LINK32 ";
  839. libMultiLineOptions += extraLinkOptions;
  840. libMultiLineOptions += " \n";
  841. }
  842. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  843. {
  844. libOptions += " ";
  845. libOptions += targetLinkFlags;
  846. libOptions += " ";
  847. libMultiLineOptions += "# ADD LINK32 ";
  848. libMultiLineOptions += targetLinkFlags;
  849. libMultiLineOptions += " \n";
  850. }
  851. // are there any custom rules on the target itself
  852. // only if the target is a lib or exe
  853. std::string customRuleCode = this->CreateTargetRules(target, libName);
  854. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  855. if(!fin)
  856. {
  857. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  858. }
  859. std::string line;
  860. while(cmSystemTools::GetLineFromStream(fin, line))
  861. {
  862. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  863. if(!mfcFlag)
  864. {
  865. mfcFlag = "0";
  866. }
  867. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  868. customRuleCode.c_str());
  869. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  870. mfcFlag);
  871. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  872. libOptions.c_str());
  873. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  874. libDebugOptions.c_str());
  875. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  876. libOptimizedOptions.c_str());
  877. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  878. libMultiLineOptions.c_str());
  879. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  880. libMultiLineDebugOptions.c_str());
  881. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  882. libMultiLineOptimizedOptions.c_str());
  883. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  884. m_IncludeOptions.c_str());
  885. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  886. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  887. // are already quoted in the template file,
  888. // we need to remove the quotes here, we still need
  889. // to convert to output path for unix to win32 conversion
  890. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  891. removeQuotes(
  892. cmSystemTools::ConvertToOutputPath(libPath.c_str())).c_str());
  893. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  894. removeQuotes(
  895. cmSystemTools::ConvertToOutputPath(exePath.c_str())).c_str());
  896. cmSystemTools::ReplaceString(line,
  897. "EXTRA_DEFINES",
  898. m_Makefile->GetDefineFlags());
  899. cmGlobalGenerator* gen = this->GetGlobalGenerator();
  900. // store flags for each configuration
  901. std::string flags = " ";
  902. std::string flagsRelease = " ";
  903. std::string flagsMinSize = " ";
  904. std::string flagsDebug = " ";
  905. std::string flagsDebugRel = " ";
  906. // if CXX is on and the target contains cxx code then add the cxx flags
  907. if ( gen->GetLanguageEnabled("CXX") && target.HasCxx() )
  908. {
  909. flagsRelease = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_RELEASE");
  910. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  911. flagsMinSize = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_MINSIZEREL");
  912. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  913. flagsDebug = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_DEBUG");
  914. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  915. flagsDebugRel = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_RELWITHDEBINFO");
  916. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  917. flags = " ";
  918. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS");
  919. }
  920. // if C and the target is not CXX
  921. else if(gen->GetLanguageEnabled("C") && !target.HasCxx())
  922. {
  923. flagsRelease += m_Makefile->GetDefinition("CMAKE_C_FLAGS_RELEASE");
  924. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\"";
  925. flagsMinSize += m_Makefile->GetDefinition("CMAKE_C_FLAGS_MINSIZEREL");
  926. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\"";
  927. flagsDebug += m_Makefile->GetDefinition("CMAKE_C_FLAGS_DEBUG");
  928. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\"";
  929. flagsDebugRel += m_Makefile->GetDefinition("CMAKE_C_FLAGS_RELWITHDEBINFO");
  930. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\"";
  931. flags = " ";
  932. flags = m_Makefile->GetDefinition("CMAKE_C_FLAGS");
  933. }
  934. // The template files have CXX FLAGS in them, that need to be replaced.
  935. // There are not separate CXX and C template files, so we use the same
  936. // variable names. The previous code sets up flags* variables to contain
  937. // the correct C or CXX flags
  938. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE", flagsRelease.c_str());
  939. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL", flagsMinSize.c_str());
  940. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG", flagsDebug.c_str());
  941. cmSystemTools::ReplaceString(line,"CMAKE_CXX_FLAGS_RELWITHDEBINFO", flagsDebugRel.c_str());
  942. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  943. fout << line.c_str() << std::endl;
  944. }
  945. }
  946. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  947. {
  948. std::ifstream fin(m_DSPFooterTemplate.c_str());
  949. if(!fin)
  950. {
  951. cmSystemTools::Error("Error Reading ",
  952. m_DSPFooterTemplate.c_str());
  953. }
  954. std::string line;
  955. while(cmSystemTools::GetLineFromStream(fin, line))
  956. {
  957. fout << line << std::endl;
  958. }
  959. }