cmLocalVisualStudio6Generator.cxx 36 KB

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