cmLocalVisualStudio6Generator.cxx 37 KB

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