cmLocalVisualStudio6Generator.cxx 37 KB

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