cmLocalVisualStudio6Generator.cxx 36 KB

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