cmLocalVisualStudio6Generator.cxx 32 KB

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