cmLocalVisualStudio6Generator.cxx 27 KB

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