1
0

cmLocalVisualStudio6Generator.cxx 36 KB

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