cmLocalVisualStudio6Generator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. cmLocalVisualStudio6Generator::cmLocalVisualStudio6Generator()
  20. {
  21. }
  22. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  23. {
  24. }
  25. void cmLocalVisualStudio6Generator::Generate(bool /* fromTheTop */)
  26. {
  27. this->OutputDSPFile();
  28. }
  29. void cmLocalVisualStudio6Generator::OutputDSPFile()
  30. {
  31. // If not an in source build, then create the output directory
  32. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  33. m_Makefile->GetHomeDirectory()) != 0)
  34. {
  35. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  36. {
  37. cmSystemTools::Error("Error creating directory ",
  38. m_Makefile->GetStartOutputDirectory());
  39. }
  40. }
  41. // Setup /I and /LIBPATH options for the resulting DSP file
  42. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  43. std::vector<std::string>::iterator i;
  44. for(i = includes.begin(); i != includes.end(); ++i)
  45. {
  46. m_IncludeOptions += " /I ";
  47. std::string tmp = cmSystemTools::ConvertToOutputPath(i->c_str());
  48. // quote if not already quoted
  49. if (tmp[0] != '"')
  50. {
  51. m_IncludeOptions += "\"";
  52. m_IncludeOptions += tmp;
  53. m_IncludeOptions += "\"";
  54. }
  55. else
  56. {
  57. m_IncludeOptions += tmp;
  58. }
  59. }
  60. // Create the DSP or set of DSP's for libraries and executables
  61. // clear project names
  62. m_CreatedProjectNames.clear();
  63. // build any targets
  64. cmTargets &tgts = m_Makefile->GetTargets();
  65. for(cmTargets::iterator l = tgts.begin();
  66. l != tgts.end(); l++)
  67. {
  68. switch(l->second.GetType())
  69. {
  70. case cmTarget::STATIC_LIBRARY:
  71. this->SetBuildType(STATIC_LIBRARY, l->first.c_str());
  72. break;
  73. case cmTarget::SHARED_LIBRARY:
  74. case cmTarget::MODULE_LIBRARY:
  75. this->SetBuildType(DLL, l->first.c_str());
  76. break;
  77. case cmTarget::EXECUTABLE:
  78. this->SetBuildType(EXECUTABLE,l->first.c_str());
  79. break;
  80. case cmTarget::WIN32_EXECUTABLE:
  81. this->SetBuildType(WIN32_EXECUTABLE,l->first.c_str());
  82. break;
  83. case cmTarget::UTILITY:
  84. this->SetBuildType(UTILITY, l->first.c_str());
  85. break;
  86. case cmTarget::INSTALL_FILES:
  87. break;
  88. case cmTarget::INSTALL_PROGRAMS:
  89. break;
  90. default:
  91. cmSystemTools::Error("Bad target type", l->first.c_str());
  92. break;
  93. }
  94. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  95. // so don't build a projectfile for it
  96. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  97. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  98. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  99. {
  100. // check to see if the dsp is going into a sub-directory
  101. std::string::size_type pos = l->first.rfind('/');
  102. if(pos != std::string::npos)
  103. {
  104. std::string dir = m_Makefile->GetStartOutputDirectory();
  105. dir += "/";
  106. dir += l->first.substr(0, pos);
  107. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  108. {
  109. cmSystemTools::Error("Error creating directory ", dir.c_str());
  110. }
  111. }
  112. this->CreateSingleDSP(l->first.c_str(),l->second);
  113. }
  114. }
  115. }
  116. void cmLocalVisualStudio6Generator::CreateSingleDSP(const char *lname, cmTarget &target)
  117. {
  118. // add to the list of projects
  119. std::string pname = lname;
  120. m_CreatedProjectNames.push_back(pname);
  121. // create the dsp.cmake file
  122. std::string fname;
  123. fname = m_Makefile->GetStartOutputDirectory();
  124. fname += "/";
  125. fname += lname;
  126. fname += ".dsp";
  127. // save the name of the real dsp file
  128. std::string realDSP = fname;
  129. fname += ".cmake";
  130. std::ofstream fout(fname.c_str());
  131. if(!fout)
  132. {
  133. cmSystemTools::Error("Error Writing ", fname.c_str());
  134. }
  135. this->WriteDSPFile(fout,lname,target);
  136. fout.close();
  137. // if the dsp file has changed, then write it.
  138. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  139. }
  140. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmSourceGroup& sourceGroup)
  141. {
  142. std::string dspname = *(m_CreatedProjectNames.end()-1);
  143. if(dspname == "ALL_BUILD")
  144. {
  145. return;
  146. }
  147. dspname += ".dsp.cmake";
  148. std::string makefileIn = m_Makefile->GetStartDirectory();
  149. makefileIn += "/";
  150. makefileIn += "CMakeLists.txt";
  151. makefileIn = cmSystemTools::ConvertToOutputPath(makefileIn.c_str());
  152. std::string dsprule = "${CMAKE_COMMAND}";
  153. m_Makefile->ExpandVariablesInString(dsprule);
  154. dsprule = cmSystemTools::ConvertToOutputPath(dsprule.c_str());
  155. std::string args = makefileIn;
  156. args += " -H";
  157. args +=
  158. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory());
  159. args += " -S";
  160. args +=
  161. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory());
  162. args += " -O";
  163. args +=
  164. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory());
  165. args += " -B";
  166. args +=
  167. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory());
  168. m_Makefile->ExpandVariablesInString(args);
  169. std::string configFile =
  170. m_Makefile->GetDefinition("CMAKE_ROOT");
  171. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  172. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  173. bool found = false;
  174. for(std::vector<std::string>::iterator i = listFiles.begin();
  175. i != listFiles.end(); ++i)
  176. {
  177. if(*i == configFile)
  178. {
  179. found = true;
  180. }
  181. }
  182. if(!found)
  183. {
  184. listFiles.push_back(configFile);
  185. }
  186. std::vector<std::string> outputs;
  187. outputs.push_back(dspname);
  188. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  189. args.c_str(),
  190. listFiles,
  191. outputs);
  192. sourceGroup.AddCustomCommand(cc);
  193. }
  194. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  195. const char *libName,
  196. cmTarget &target)
  197. {
  198. // We may be modifying the source groups temporarily, so make a copy.
  199. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  200. // get the classes from the source lists then add them to the groups
  201. std::vector<cmSourceFile*> classes = target.GetSourceFiles();
  202. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  203. i != classes.end(); i++)
  204. {
  205. // Add the file to the list of sources.
  206. std::string source = (*i)->GetFullPath();
  207. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  208. sourceGroups);
  209. sourceGroup.AddSource(source.c_str(), *i);
  210. }
  211. // add any custom rules to the source groups
  212. for (std::vector<cmCustomCommand>::const_iterator cr =
  213. target.GetCustomCommands().begin();
  214. cr != target.GetCustomCommands().end(); ++cr)
  215. {
  216. cmSourceGroup& sourceGroup =
  217. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  218. sourceGroups);
  219. cmCustomCommand cc(*cr);
  220. cc.ExpandVariables(*m_Makefile);
  221. sourceGroup.AddCustomCommand(cc);
  222. }
  223. // Write the DSP file's header.
  224. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  225. // Find the group in which the CMakeLists.txt source belongs, and add
  226. // the rule to generate this DSP file.
  227. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  228. sg != sourceGroups.rend(); ++sg)
  229. {
  230. if(sg->Matches("CMakeLists.txt"))
  231. {
  232. this->AddDSPBuildRule(*sg);
  233. break;
  234. }
  235. }
  236. // Loop through every source group.
  237. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  238. sg != sourceGroups.end(); ++sg)
  239. {
  240. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  241. // If the group is empty, don't write it at all.
  242. if(buildRules.empty())
  243. { continue; }
  244. // If the group has a name, write the header.
  245. std::string name = sg->GetName();
  246. if(name != "")
  247. {
  248. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  249. }
  250. // Loop through each build rule in the source group.
  251. for(cmSourceGroup::BuildRules::const_iterator cc =
  252. buildRules.begin(); cc != buildRules.end(); ++ cc)
  253. {
  254. std::string source = cc->first;
  255. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  256. std::vector<std::string> depends;
  257. const char* compileFlags = 0;
  258. if(cc->second.m_SourceFile)
  259. {
  260. // Check for extra compiler flags.
  261. compileFlags = cc->second.m_SourceFile->GetProperty("COMPILE_FLAGS");
  262. // Check for extra object-file dependencies.
  263. const char* dependsValue =
  264. cc->second.m_SourceFile->GetProperty("OBJECT_DEPENDS");
  265. if(dependsValue)
  266. {
  267. cmSystemTools::ExpandListArgument(dependsValue, depends);
  268. }
  269. }
  270. if (source != libName || target.GetType() == cmTarget::UTILITY)
  271. {
  272. fout << "# Begin Source File\n\n";
  273. // Tell MS-Dev what the source is. If the compiler knows how to
  274. // build it, then it will.
  275. fout << "SOURCE=" <<
  276. cmSystemTools::ConvertToOutputPath(source.c_str()) << "\n\n";
  277. if(!depends.empty())
  278. {
  279. // Write out the dependencies for the rule.
  280. fout << "USERDEP__HACK=";
  281. for(std::vector<std::string>::const_iterator d = depends.begin();
  282. d != depends.end(); ++d)
  283. {
  284. fout << "\\\n\t" <<
  285. cmSystemTools::ConvertToOutputPath(d->c_str());
  286. }
  287. fout << "\n";
  288. }
  289. if (!commands.empty())
  290. {
  291. cmSourceGroup::CommandFiles totalCommand;
  292. std::string totalCommandStr;
  293. totalCommandStr = this->CombineCommands(commands, totalCommand,
  294. source.c_str());
  295. const char* comment = totalCommand.m_Comment.c_str();
  296. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  297. (*comment?comment:"Custom Rule"),
  298. totalCommand.m_Depends,
  299. totalCommand.m_Outputs, compileFlags);
  300. }
  301. else if(compileFlags)
  302. {
  303. for(std::vector<std::string>::iterator i
  304. = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  305. {
  306. if (i == m_Configurations.begin())
  307. {
  308. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  309. }
  310. else
  311. {
  312. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  313. }
  314. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  315. }
  316. fout << "!ENDIF\n\n";
  317. }
  318. fout << "# End Source File\n";
  319. }
  320. }
  321. // If the group has a name, write the footer.
  322. if(name != "")
  323. {
  324. this->WriteDSPEndGroup(fout);
  325. }
  326. }
  327. // Write the DSP file's footer.
  328. this->WriteDSPFooter(fout);
  329. }
  330. void cmLocalVisualStudio6Generator::WriteCustomRule(std::ostream& fout,
  331. const char* source,
  332. const char* command,
  333. const char* comment,
  334. const std::set<std::string>& depends,
  335. const std::set<std::string>& outputs,
  336. const char* flags
  337. )
  338. {
  339. std::vector<std::string>::iterator i;
  340. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  341. {
  342. if (i == m_Configurations.begin())
  343. {
  344. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  345. }
  346. else
  347. {
  348. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  349. }
  350. if(flags)
  351. {
  352. fout << "\n# ADD CPP " << flags << "\n\n";
  353. }
  354. // Write out the dependencies for the rule.
  355. fout << "USERDEP__HACK=";
  356. for(std::set<std::string>::const_iterator d = depends.begin();
  357. d != depends.end(); ++d)
  358. {
  359. fout << "\\\n\t" <<
  360. cmSystemTools::ConvertToOutputPath(d->c_str());
  361. }
  362. fout << "\n";
  363. fout << "# PROP Ignore_Default_Tool 1\n";
  364. fout << "# Begin Custom Build - Building " << comment
  365. << " $(InputPath)\n\n";
  366. if(outputs.size() == 0)
  367. {
  368. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  369. fout << command << "\n\n";
  370. }
  371. // Write a rule for every output generated by this command.
  372. for(std::set<std::string>::const_iterator output = outputs.begin();
  373. output != outputs.end(); ++output)
  374. {
  375. fout << cmSystemTools::ConvertToOutputPath(output->c_str())
  376. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  377. fout << command << "\n\n";
  378. }
  379. fout << "# End Custom Build\n\n";
  380. }
  381. fout << "!ENDIF\n\n";
  382. }
  383. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  384. const char* group,
  385. const char* filter)
  386. {
  387. fout << "# Begin Group \"" << group << "\"\n"
  388. "# PROP Default_Filter \"" << filter << "\"\n";
  389. }
  390. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  391. {
  392. fout << "# End Group\n";
  393. }
  394. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b, const char *libName)
  395. {
  396. std::string root= m_Makefile->GetDefinition("CMAKE_ROOT");
  397. const char *def= m_Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  398. if( def)
  399. {
  400. root = def;
  401. }
  402. else
  403. {
  404. root += "/Templates";
  405. }
  406. switch(b)
  407. {
  408. case STATIC_LIBRARY:
  409. m_DSPHeaderTemplate = root;
  410. m_DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  411. m_DSPFooterTemplate = root;
  412. m_DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  413. break;
  414. case DLL:
  415. m_DSPHeaderTemplate = root;
  416. m_DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  417. m_DSPFooterTemplate = root;
  418. m_DSPFooterTemplate += "/DLLFooter.dsptemplate";
  419. break;
  420. case EXECUTABLE:
  421. m_DSPHeaderTemplate = root;
  422. m_DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  423. m_DSPFooterTemplate = root;
  424. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  425. break;
  426. case WIN32_EXECUTABLE:
  427. m_DSPHeaderTemplate = root;
  428. m_DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  429. m_DSPFooterTemplate = root;
  430. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  431. break;
  432. case UTILITY:
  433. m_DSPHeaderTemplate = root;
  434. m_DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  435. m_DSPFooterTemplate = root;
  436. m_DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  437. break;
  438. }
  439. // once the build type is set, determine what configurations are
  440. // possible
  441. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  442. cmRegularExpression reg("# Name ");
  443. if(!fin)
  444. {
  445. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  446. }
  447. // reset m_Configurations
  448. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  449. // now add all the configurations possible
  450. char buffer[2048];
  451. while(fin)
  452. {
  453. fin.getline(buffer, 2048);
  454. std::string line = buffer;
  455. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  456. if (reg.find(line))
  457. {
  458. m_Configurations.push_back(line.substr(reg.end()));
  459. }
  460. }
  461. }
  462. std::string
  463. cmLocalVisualStudio6Generator::CombineCommands(const cmSourceGroup::Commands &commands,
  464. cmSourceGroup::CommandFiles &totalCommand,
  465. const char *source)
  466. {
  467. // Loop through every custom command generating code from the
  468. // current source.
  469. // build up the depends and outputs and commands
  470. std::string totalCommandStr = "";
  471. std::string temp;
  472. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  473. c != commands.end(); ++c)
  474. {
  475. totalCommandStr += "\n\t";
  476. temp= c->second.m_Command;
  477. temp = cmSystemTools::ConvertToOutputPath(temp.c_str());
  478. totalCommandStr += temp;
  479. totalCommandStr += " ";
  480. totalCommandStr += c->second.m_Arguments;
  481. totalCommand.Merge(c->second);
  482. totalCommand.m_Comment = c->second.m_Comment.c_str();
  483. }
  484. // Create a dummy file with the name of the source if it does
  485. // not exist
  486. if(totalCommand.m_Outputs.empty())
  487. {
  488. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  489. dummyFile += "/";
  490. dummyFile += source;
  491. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  492. {
  493. std::ofstream fout(dummyFile.c_str());
  494. fout << "Dummy file created by cmake as unused source for utility command.\n";
  495. }
  496. }
  497. return totalCommandStr;
  498. }
  499. // look for custom rules on a target and collect them together
  500. std::string
  501. cmLocalVisualStudio6Generator::CreateTargetRules(const cmTarget &target,
  502. const char *libName)
  503. {
  504. std::string customRuleCode = "";
  505. if (target.GetType() >= cmTarget::UTILITY)
  506. {
  507. return customRuleCode;
  508. }
  509. // Find the group in which the lix exe custom rules belong
  510. bool init = false;
  511. for (std::vector<cmCustomCommand>::const_iterator cr =
  512. target.GetCustomCommands().begin();
  513. cr != target.GetCustomCommands().end(); ++cr)
  514. {
  515. cmCustomCommand cc(*cr);
  516. cc.ExpandVariables(*m_Makefile);
  517. if (cc.GetSourceName() == libName)
  518. {
  519. if (!init)
  520. {
  521. // header stuff
  522. customRuleCode = "# Begin Special Build Tool\nPostBuild_Cmds=";
  523. init = true;
  524. }
  525. else
  526. {
  527. customRuleCode += "\t";
  528. }
  529. customRuleCode += cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str()) + " " + cc.GetArguments();
  530. }
  531. }
  532. if (init)
  533. {
  534. customRuleCode += "\n# End Special Build Tool\n";
  535. }
  536. return customRuleCode;
  537. }
  538. inline std::string removeQuotes(const std::string& s)
  539. {
  540. if(s[0] == '\"' && s[s.size()-1] == '\"')
  541. {
  542. return s.substr(1, s.size()-2);
  543. }
  544. return s;
  545. }
  546. void cmLocalVisualStudio6Generator::WriteDSPHeader(std::ostream& fout, const char *libName,
  547. const cmTarget &target,
  548. std::vector<cmSourceGroup> &)
  549. {
  550. std::set<std::string> pathEmitted;
  551. // determine the link directories
  552. std::string libOptions;
  553. std::string libDebugOptions;
  554. std::string libOptimizedOptions;
  555. std::string libMultiLineOptions;
  556. std::string libMultiLineDebugOptions;
  557. std::string libMultiLineOptimizedOptions;
  558. // suppoirt override in output directory
  559. std::string libPath = "";
  560. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  561. {
  562. libPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  563. }
  564. std::string exePath = "";
  565. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  566. {
  567. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  568. }
  569. if(libPath.size())
  570. {
  571. // make sure there is a trailing slash
  572. if(libPath[libPath.size()-1] != '/')
  573. {
  574. libPath += "/";
  575. }
  576. std::string lpath =
  577. cmSystemTools::ConvertToOutputPath(libPath.c_str());
  578. std::string lpathIntDir = libPath + "$(INTDIR)";
  579. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  580. if(pathEmitted.insert(lpath).second)
  581. {
  582. libOptions += " /LIBPATH:";
  583. libOptions += lpathIntDir;
  584. libOptions += " ";
  585. libOptions += " /LIBPATH:";
  586. libOptions += lpath;
  587. libOptions += " ";
  588. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  589. libMultiLineOptions += lpathIntDir;
  590. libMultiLineOptions += " ";
  591. libMultiLineOptions += " /LIBPATH:";
  592. libMultiLineOptions += lpath;
  593. libMultiLineOptions += " \n";
  594. }
  595. }
  596. if(exePath.size())
  597. {
  598. // make sure there is a trailing slash
  599. if(exePath[exePath.size()-1] != '/')
  600. {
  601. exePath += "/";
  602. }
  603. std::string lpath =
  604. cmSystemTools::ConvertToOutputPath(exePath.c_str());
  605. std::string lpathIntDir = exePath + "$(INTDIR)";
  606. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  607. if(pathEmitted.insert(lpath).second)
  608. {
  609. libOptions += " /LIBPATH:";
  610. libOptions += lpathIntDir;
  611. libOptions += " ";
  612. libOptions += " /LIBPATH:";
  613. libOptions += lpath;
  614. libOptions += " ";
  615. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  616. libMultiLineOptions += lpathIntDir;
  617. libMultiLineOptions += " ";
  618. libMultiLineOptions += " /LIBPATH:";
  619. libMultiLineOptions += lpath;
  620. libMultiLineOptions += " \n";
  621. }
  622. }
  623. std::vector<std::string>::const_iterator i;
  624. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  625. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  626. {
  627. std::string path = *i;
  628. if(path[path.size()-1] != '/')
  629. {
  630. path += "/";
  631. }
  632. std::string lpath =
  633. cmSystemTools::ConvertToOutputPath(path.c_str());
  634. std::string lpathIntDir = path + "$(INTDIR)";
  635. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  636. if(pathEmitted.insert(lpath).second)
  637. {
  638. libOptions += " /LIBPATH:";
  639. libOptions += lpathIntDir;
  640. libOptions += " ";
  641. libOptions += " /LIBPATH:";
  642. libOptions += lpath;
  643. libOptions += " ";
  644. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  645. libMultiLineOptions += lpathIntDir;
  646. libMultiLineOptions += " ";
  647. libMultiLineOptions += " /LIBPATH:";
  648. libMultiLineOptions += lpath;
  649. libMultiLineOptions += " \n";
  650. }
  651. }
  652. // find link libraries
  653. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  654. cmTarget::LinkLibraries::const_iterator j;
  655. for(j = libs.begin(); j != libs.end(); ++j)
  656. {
  657. // add libraries to executables and dlls (but never include
  658. // a library in a library, bad recursion)
  659. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  660. && target.GetType() != cmTarget::STATIC_LIBRARY
  661. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  662. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  663. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  664. {
  665. std::string lib = j->first;
  666. if(j->first.find(".lib") == std::string::npos)
  667. {
  668. lib += ".lib";
  669. }
  670. lib = cmSystemTools::ConvertToOutputPath(lib.c_str());
  671. if (j->second == cmTarget::GENERAL)
  672. {
  673. libOptions += " ";
  674. libOptions += lib;
  675. libMultiLineOptions += "# ADD LINK32 ";
  676. libMultiLineOptions += lib;
  677. libMultiLineOptions += "\n";
  678. }
  679. if (j->second == cmTarget::DEBUG)
  680. {
  681. libDebugOptions += " ";
  682. libDebugOptions += lib;
  683. libMultiLineDebugOptions += "# ADD LINK32 ";
  684. libMultiLineDebugOptions += lib;
  685. libMultiLineDebugOptions += "\n";
  686. }
  687. if (j->second == cmTarget::OPTIMIZED)
  688. {
  689. libOptimizedOptions += " ";
  690. libOptimizedOptions += lib;
  691. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  692. libMultiLineOptimizedOptions += lib;
  693. libMultiLineOptimizedOptions += "\n";
  694. }
  695. }
  696. }
  697. std::string extraLinkOptions;
  698. if(target.GetType() == cmTarget::EXECUTABLE)
  699. {
  700. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_EXE_LINKER_FLAGS");
  701. }
  702. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  703. {
  704. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_SHARED_LINKER_FLAGS");
  705. }
  706. if(extraLinkOptions.size())
  707. {
  708. libOptions += " ";
  709. libOptions += extraLinkOptions;
  710. libOptions += " ";
  711. libMultiLineOptions += "# ADD LINK32 ";
  712. libMultiLineOptions += extraLinkOptions;
  713. libMultiLineOptions += " \n";
  714. }
  715. // are there any custom rules on the target itself
  716. // only if the target is a lib or exe
  717. std::string customRuleCode = this->CreateTargetRules(target, libName);
  718. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  719. if(!fin)
  720. {
  721. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  722. }
  723. char buffer[2048];
  724. while(fin)
  725. {
  726. fin.getline(buffer, 2048);
  727. std::string line = buffer;
  728. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  729. if(!mfcFlag)
  730. {
  731. mfcFlag = "0";
  732. }
  733. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  734. customRuleCode.c_str());
  735. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  736. mfcFlag);
  737. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  738. libOptions.c_str());
  739. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  740. libDebugOptions.c_str());
  741. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  742. libOptimizedOptions.c_str());
  743. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  744. libMultiLineOptions.c_str());
  745. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  746. libMultiLineDebugOptions.c_str());
  747. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  748. libMultiLineOptimizedOptions.c_str());
  749. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  750. m_IncludeOptions.c_str());
  751. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  752. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  753. // are already quoted in the template file,
  754. // we need to remove the quotes here, we still need
  755. // to convert to output path for unix to win32 conversion
  756. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  757. removeQuotes(
  758. cmSystemTools::ConvertToOutputPath(libPath.c_str())).c_str());
  759. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  760. removeQuotes(
  761. cmSystemTools::ConvertToOutputPath(exePath.c_str())).c_str());
  762. cmSystemTools::ReplaceString(line,
  763. "EXTRA_DEFINES",
  764. m_Makefile->GetDefineFlags());
  765. cmGlobalGenerator* gen = this->GetGlobalGenerator();
  766. if ( gen->GetLanguageEnabled("C") ||gen->GetLanguageEnabled("CXX") )
  767. {
  768. std::string flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_RELEASE");
  769. flags += " -DCMAKE_INTDIR=\\\"Release\\\"";
  770. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE", flags.c_str());
  771. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_MINSIZEREL");
  772. flags += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\"";
  773. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL", flags.c_str());
  774. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_DEBUG");
  775. flags += " -DCMAKE_INTDIR=\\\"Debug\\\"";
  776. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG", flags.c_str());
  777. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_RELWITHDEBINFO");
  778. flags += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\"";
  779. cmSystemTools::ReplaceString(line,"CMAKE_CXX_FLAGS_RELWITHDEBINFO", flags.c_str());
  780. flags = "";
  781. if ( m_Makefile->GetDefinition("CMAKE_C_FLAGS") )
  782. {
  783. flags += m_Makefile->GetDefinition("CMAKE_C_FLAGS");
  784. }
  785. if ( m_Makefile->GetDefinition("CMAKE_CXX_FLAGS") )
  786. {
  787. flags += m_Makefile->GetDefinition("CMAKE_CXX_FLAGS");
  788. }
  789. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  790. }
  791. fout << line.c_str() << std::endl;
  792. }
  793. }
  794. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  795. {
  796. std::ifstream fin(m_DSPFooterTemplate.c_str());
  797. if(!fin)
  798. {
  799. cmSystemTools::Error("Error Reading ",
  800. m_DSPFooterTemplate.c_str());
  801. }
  802. char buffer[2048];
  803. while(fin)
  804. {
  805. fin.getline(buffer, 2048);
  806. fout << buffer << std::endl;
  807. }
  808. }