cmLocalVisualStudio6Generator.cxx 31 KB

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