cmLocalVisualStudio6Generator.cxx 37 KB

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