cmLocalVisualStudio6Generator.cxx 37 KB

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