cmLocalVisualStudio6Generator.cxx 34 KB

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