cmLocalVisualStudio6Generator.cxx 31 KB

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