cmLocalVisualStudio6Generator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  274. totalCommand.m_Depends,
  275. totalCommand.m_Outputs, compileFlags);
  276. }
  277. else if(compileFlags)
  278. {
  279. for(std::vector<std::string>::iterator i
  280. = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  281. {
  282. if (i == m_Configurations.begin())
  283. {
  284. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  285. }
  286. else
  287. {
  288. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  289. }
  290. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  291. }
  292. fout << "!ENDIF\n\n";
  293. }
  294. fout << "# End Source File\n";
  295. }
  296. }
  297. // If the group has a name, write the footer.
  298. if(name != "")
  299. {
  300. this->WriteDSPEndGroup(fout);
  301. }
  302. }
  303. // Write the DSP file's footer.
  304. this->WriteDSPFooter(fout);
  305. }
  306. void cmLocalVisualStudio6Generator::WriteCustomRule(std::ostream& fout,
  307. const char* source,
  308. const char* command,
  309. const std::set<std::string>& depends,
  310. const std::set<std::string>& outputs,
  311. const char* flags
  312. )
  313. {
  314. std::vector<std::string>::iterator i;
  315. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  316. {
  317. if (i == m_Configurations.begin())
  318. {
  319. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  320. }
  321. else
  322. {
  323. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  324. }
  325. if(flags)
  326. {
  327. fout << "\n# ADD CPP " << flags << "\n\n";
  328. }
  329. // Write out the dependencies for the rule.
  330. fout << "USERDEP__HACK=";
  331. for(std::set<std::string>::const_iterator d = depends.begin();
  332. d != depends.end(); ++d)
  333. {
  334. fout << "\\\n\t" <<
  335. cmSystemTools::ConvertToOutputPath(d->c_str());
  336. }
  337. fout << "\n";
  338. fout << "# PROP Ignore_Default_Tool 1\n";
  339. fout << "# Begin Custom Build\n\n";
  340. if(outputs.size() == 0)
  341. {
  342. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  343. fout << command << "\n\n";
  344. }
  345. // Write a rule for every output generated by this command.
  346. for(std::set<std::string>::const_iterator output = outputs.begin();
  347. output != outputs.end(); ++output)
  348. {
  349. fout << "\"" << output->c_str()
  350. << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  351. fout << command << "\n\n";
  352. }
  353. fout << "# End Custom Build\n\n";
  354. }
  355. fout << "!ENDIF\n\n";
  356. }
  357. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  358. const char* group,
  359. const char* filter)
  360. {
  361. fout << "# Begin Group \"" << group << "\"\n"
  362. "# PROP Default_Filter \"" << filter << "\"\n";
  363. }
  364. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  365. {
  366. fout << "# End Group\n";
  367. }
  368. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b, const char *libName)
  369. {
  370. std::string root= m_Makefile->GetDefinition("CMAKE_ROOT");
  371. const char *def= m_Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  372. if( def)
  373. {
  374. root = def;
  375. }
  376. else
  377. {
  378. root += "/Templates";
  379. }
  380. switch(b)
  381. {
  382. case STATIC_LIBRARY:
  383. m_DSPHeaderTemplate = root;
  384. m_DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  385. m_DSPFooterTemplate = root;
  386. m_DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  387. break;
  388. case DLL:
  389. m_DSPHeaderTemplate = root;
  390. m_DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  391. m_DSPFooterTemplate = root;
  392. m_DSPFooterTemplate += "/DLLFooter.dsptemplate";
  393. break;
  394. case EXECUTABLE:
  395. m_DSPHeaderTemplate = root;
  396. m_DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  397. m_DSPFooterTemplate = root;
  398. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  399. break;
  400. case WIN32_EXECUTABLE:
  401. m_DSPHeaderTemplate = root;
  402. m_DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  403. m_DSPFooterTemplate = root;
  404. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  405. break;
  406. case UTILITY:
  407. m_DSPHeaderTemplate = root;
  408. m_DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  409. m_DSPFooterTemplate = root;
  410. m_DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  411. break;
  412. }
  413. // once the build type is set, determine what configurations are
  414. // possible
  415. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  416. cmRegularExpression reg("# Name ");
  417. if(!fin)
  418. {
  419. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  420. }
  421. // reset m_Configurations
  422. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  423. // now add all the configurations possible
  424. char buffer[2048];
  425. while(fin)
  426. {
  427. fin.getline(buffer, 2048);
  428. std::string line = buffer;
  429. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  430. if (reg.find(line))
  431. {
  432. m_Configurations.push_back(line.substr(reg.end()));
  433. }
  434. }
  435. }
  436. std::string
  437. cmLocalVisualStudio6Generator::CombineCommands(const cmSourceGroup::Commands &commands,
  438. cmSourceGroup::CommandFiles &totalCommand,
  439. const char *source)
  440. {
  441. // Loop through every custom command generating code from the
  442. // current source.
  443. // build up the depends and outputs and commands
  444. std::string totalCommandStr = "";
  445. std::string temp;
  446. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  447. c != commands.end(); ++c)
  448. {
  449. totalCommandStr += "\n\t";
  450. temp= c->second.m_Command;
  451. temp = cmSystemTools::ConvertToOutputPath(temp.c_str());
  452. totalCommandStr += temp;
  453. totalCommandStr += " ";
  454. totalCommandStr += c->second.m_Arguments;
  455. totalCommand.Merge(c->second);
  456. }
  457. // Create a dummy file with the name of the source if it does
  458. // not exist
  459. if(totalCommand.m_Outputs.empty())
  460. {
  461. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  462. dummyFile += "/";
  463. dummyFile += source;
  464. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  465. {
  466. std::ofstream fout(dummyFile.c_str());
  467. fout << "Dummy file created by cmake as unused source for utility command.\n";
  468. }
  469. }
  470. return totalCommandStr;
  471. }
  472. // look for custom rules on a target and collect them together
  473. std::string
  474. cmLocalVisualStudio6Generator::CreateTargetRules(const cmTarget &target,
  475. const char *libName)
  476. {
  477. std::string customRuleCode = "";
  478. if (target.GetType() >= cmTarget::UTILITY)
  479. {
  480. return customRuleCode;
  481. }
  482. // Find the group in which the lix exe custom rules belong
  483. bool init = false;
  484. for (std::vector<cmCustomCommand>::const_iterator cr =
  485. target.GetCustomCommands().begin();
  486. cr != target.GetCustomCommands().end(); ++cr)
  487. {
  488. cmCustomCommand cc(*cr);
  489. cc.ExpandVariables(*m_Makefile);
  490. if (cc.GetSourceName() == libName)
  491. {
  492. if (!init)
  493. {
  494. // header stuff
  495. customRuleCode = "# Begin Special Build Tool\nPostBuild_Cmds=";
  496. init = true;
  497. }
  498. else
  499. {
  500. customRuleCode += "\t";
  501. }
  502. customRuleCode += cc.GetCommand() + " " + cc.GetArguments();
  503. }
  504. }
  505. if (init)
  506. {
  507. customRuleCode += "\n# End Special Build Tool\n";
  508. }
  509. return customRuleCode;
  510. }
  511. inline std::string removeQuotes(const std::string& s)
  512. {
  513. if(s[0] == '\"' && s[s.size()-1] == '\"')
  514. {
  515. return s.substr(1, s.size()-2);
  516. }
  517. return s;
  518. }
  519. void cmLocalVisualStudio6Generator::WriteDSPHeader(std::ostream& fout, const char *libName,
  520. const cmTarget &target,
  521. std::vector<cmSourceGroup> &)
  522. {
  523. std::set<std::string> pathEmitted;
  524. // determine the link directories
  525. std::string libOptions;
  526. std::string libDebugOptions;
  527. std::string libOptimizedOptions;
  528. std::string libMultiLineOptions;
  529. std::string libMultiLineDebugOptions;
  530. std::string libMultiLineOptimizedOptions;
  531. // suppoirt override in output directory
  532. std::string libPath = "";
  533. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  534. {
  535. libPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  536. }
  537. std::string exePath = "";
  538. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  539. {
  540. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  541. }
  542. if(libPath.size())
  543. {
  544. // make sure there is a trailing slash
  545. if(libPath[libPath.size()-1] != '/')
  546. {
  547. libPath += "/";
  548. }
  549. std::string lpath =
  550. cmSystemTools::ConvertToOutputPath(libPath.c_str());
  551. std::string lpathIntDir = libPath + "$(INTDIR)";
  552. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  553. if(pathEmitted.insert(lpath).second)
  554. {
  555. libOptions += " /LIBPATH:";
  556. libOptions += lpathIntDir;
  557. libOptions += " ";
  558. libOptions += " /LIBPATH:";
  559. libOptions += lpath;
  560. libOptions += " ";
  561. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  562. libMultiLineOptions += lpathIntDir;
  563. libMultiLineOptions += " ";
  564. libMultiLineOptions += " /LIBPATH:";
  565. libMultiLineOptions += lpath;
  566. libMultiLineOptions += " \n";
  567. }
  568. }
  569. if(exePath.size())
  570. {
  571. // make sure there is a trailing slash
  572. if(exePath[exePath.size()-1] != '/')
  573. {
  574. exePath += "/";
  575. }
  576. std::string lpath =
  577. cmSystemTools::ConvertToOutputPath(exePath.c_str());
  578. std::string lpathIntDir = exePath + "$(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. std::vector<std::string>::const_iterator i;
  597. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  598. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  599. {
  600. std::string path = *i;
  601. if(path[path.size()-1] != '/')
  602. {
  603. path += "/";
  604. }
  605. std::string lpath =
  606. cmSystemTools::ConvertToOutputPath(path.c_str());
  607. std::string lpathIntDir = path + "$(INTDIR)";
  608. lpathIntDir = cmSystemTools::ConvertToOutputPath(lpathIntDir.c_str());
  609. if(pathEmitted.insert(lpath).second)
  610. {
  611. libOptions += " /LIBPATH:";
  612. libOptions += lpathIntDir;
  613. libOptions += " ";
  614. libOptions += " /LIBPATH:";
  615. libOptions += lpath;
  616. libOptions += " ";
  617. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  618. libMultiLineOptions += lpathIntDir;
  619. libMultiLineOptions += " ";
  620. libMultiLineOptions += " /LIBPATH:";
  621. libMultiLineOptions += lpath;
  622. libMultiLineOptions += " \n";
  623. }
  624. }
  625. // find link libraries
  626. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  627. cmTarget::LinkLibraries::const_iterator j;
  628. for(j = libs.begin(); j != libs.end(); ++j)
  629. {
  630. // add libraries to executables and dlls (but never include
  631. // a library in a library, bad recursion)
  632. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  633. && target.GetType() != cmTarget::STATIC_LIBRARY) ||
  634. (target.GetType() == cmTarget::SHARED_LIBRARY && libName != j->first))
  635. {
  636. std::string lib = j->first;
  637. if(j->first.find(".lib") == std::string::npos)
  638. {
  639. lib += ".lib";
  640. }
  641. lib = cmSystemTools::ConvertToOutputPath(lib.c_str());
  642. if (j->second == cmTarget::GENERAL)
  643. {
  644. libOptions += " ";
  645. libOptions += lib;
  646. libMultiLineOptions += "# ADD LINK32 ";
  647. libMultiLineOptions += lib;
  648. libMultiLineOptions += "\n";
  649. }
  650. if (j->second == cmTarget::DEBUG)
  651. {
  652. libDebugOptions += " ";
  653. libDebugOptions += lib;
  654. libMultiLineDebugOptions += "# ADD LINK32 ";
  655. libMultiLineDebugOptions += lib;
  656. libMultiLineDebugOptions += "\n";
  657. }
  658. if (j->second == cmTarget::OPTIMIZED)
  659. {
  660. libOptimizedOptions += " ";
  661. libOptimizedOptions += lib;
  662. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  663. libMultiLineOptimizedOptions += lib;
  664. libMultiLineOptimizedOptions += "\n";
  665. }
  666. }
  667. }
  668. std::string extraLinkOptions =
  669. m_Makefile->GetDefinition("CMAKE_EXTRA_LINK_FLAGS");
  670. if(extraLinkOptions.size())
  671. {
  672. libOptions += " ";
  673. libOptions += extraLinkOptions;
  674. libOptions += " ";
  675. libMultiLineOptions += "# ADD LINK32 ";
  676. libMultiLineOptions += extraLinkOptions;
  677. libMultiLineOptions += " \n";
  678. }
  679. // are there any custom rules on the target itself
  680. // only if the target is a lib or exe
  681. std::string customRuleCode = this->CreateTargetRules(target, libName);
  682. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  683. if(!fin)
  684. {
  685. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  686. }
  687. char buffer[2048];
  688. while(fin)
  689. {
  690. fin.getline(buffer, 2048);
  691. std::string line = buffer;
  692. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  693. if(!mfcFlag)
  694. {
  695. mfcFlag = "0";
  696. }
  697. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  698. customRuleCode.c_str());
  699. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  700. mfcFlag);
  701. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  702. libOptions.c_str());
  703. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  704. libDebugOptions.c_str());
  705. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  706. libOptimizedOptions.c_str());
  707. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  708. libMultiLineOptions.c_str());
  709. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  710. libMultiLineDebugOptions.c_str());
  711. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  712. libMultiLineOptimizedOptions.c_str());
  713. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  714. m_IncludeOptions.c_str());
  715. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  716. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  717. // are already quoted in the template file,
  718. // we need to remove the quotes here, we still need
  719. // to convert to output path for unix to win32 conversion
  720. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  721. removeQuotes(
  722. cmSystemTools::ConvertToOutputPath(libPath.c_str())).c_str());
  723. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  724. removeQuotes(
  725. cmSystemTools::ConvertToOutputPath(exePath.c_str())).c_str());
  726. cmSystemTools::ReplaceString(line,
  727. "EXTRA_DEFINES",
  728. m_Makefile->GetDefineFlags());
  729. std::string flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_RELEASE");
  730. flags += " -DCMAKE_INTDIR=\\\"Release\\\"";
  731. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE", flags.c_str());
  732. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_MINSIZEREL");
  733. flags += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\"";
  734. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL", flags.c_str());
  735. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_DEBUG");
  736. flags += " -DCMAKE_INTDIR=\\\"Debug\\\"";
  737. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG", flags.c_str());
  738. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS_RELWITHDEBINFO");
  739. flags += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\"";
  740. cmSystemTools::ReplaceString(line,"CMAKE_CXX_FLAGS_RELWITHDEBINFO", flags.c_str());
  741. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS",
  742. m_Makefile->
  743. GetDefinition("CMAKE_CXX_FLAGS"));
  744. fout << line.c_str() << std::endl;
  745. }
  746. }
  747. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  748. {
  749. std::ifstream fin(m_DSPFooterTemplate.c_str());
  750. if(!fin)
  751. {
  752. cmSystemTools::Error("Error Reading ",
  753. m_DSPFooterTemplate.c_str());
  754. }
  755. char buffer[2048];
  756. while(fin)
  757. {
  758. fin.getline(buffer, 2048);
  759. fout << buffer << std::endl;
  760. }
  761. }