cmLocalVisualStudio6Generator.cxx 33 KB

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