cmLocalVisualStudio6Generator.cxx 36 KB

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