cmLocalVisualStudio7Generator.cxx 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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 "cmGlobalVisualStudio7Generator.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include "cmake.h"
  20. cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator()
  21. {
  22. this->Version = 7;
  23. this->PlatformName = "Win32";
  24. }
  25. cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator()
  26. {
  27. }
  28. void cmLocalVisualStudio7Generator::Generate()
  29. {
  30. std::set<cmStdString> lang;
  31. lang.insert("C");
  32. lang.insert("CXX");
  33. lang.insert("RC");
  34. lang.insert("IDL");
  35. lang.insert("DEF");
  36. this->CreateCustomTargetsAndCommands(lang);
  37. this->OutputVCProjFile();
  38. }
  39. // TODO
  40. // for CommandLine= need to repleace quotes with &quot
  41. // write out configurations
  42. void cmLocalVisualStudio7Generator::OutputVCProjFile()
  43. {
  44. // If not an in source build, then create the output directory
  45. if(strcmp(this->Makefile->GetStartOutputDirectory(),
  46. this->Makefile->GetHomeDirectory()) != 0)
  47. {
  48. if(!cmSystemTools::MakeDirectory(this->Makefile->GetStartOutputDirectory()))
  49. {
  50. cmSystemTools::Error("Error creating directory ",
  51. this->Makefile->GetStartOutputDirectory());
  52. }
  53. }
  54. this->LibraryOutputPath = "";
  55. if (this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  56. {
  57. this->LibraryOutputPath =
  58. this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  59. }
  60. if(this->LibraryOutputPath.size())
  61. {
  62. // make sure there is a trailing slash
  63. if(this->LibraryOutputPath[this->LibraryOutputPath.size()-1] != '/')
  64. {
  65. this->LibraryOutputPath += "/";
  66. }
  67. }
  68. this->ExecutableOutputPath = "";
  69. if (this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  70. {
  71. this->ExecutableOutputPath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  72. }
  73. if(this->ExecutableOutputPath.size())
  74. {
  75. // make sure there is a trailing slash
  76. if(this->ExecutableOutputPath[this->ExecutableOutputPath.size()-1] != '/')
  77. {
  78. this->ExecutableOutputPath += "/";
  79. }
  80. }
  81. // Create the VCProj or set of VCProj's for libraries and executables
  82. // clear project names
  83. this->CreatedProjectNames.clear();
  84. // Call TraceVSDependencies on all targets
  85. cmTargets &tgts = this->Makefile->GetTargets();
  86. for(cmTargets::iterator l = tgts.begin();
  87. l != tgts.end(); l++)
  88. {
  89. // Add a rule to regenerate the build system when the target
  90. // specification source changes.
  91. const char* suppRegenRule =
  92. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  93. if (!cmSystemTools::IsOn(suppRegenRule) &&
  94. (strcmp(l->first.c_str(), CMAKE_CHECK_BUILD_SYSTEM_TARGET) != 0))
  95. {
  96. this->AddVCProjBuildRule(l->second);
  97. }
  98. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  99. // so don't build a projectfile for it
  100. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  101. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  102. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  103. {
  104. cmTarget& target = l->second;
  105. target.TraceVSDependencies(target.GetName(), this->Makefile);
  106. }
  107. }
  108. for(cmTargets::iterator l = tgts.begin();
  109. l != tgts.end(); l++)
  110. {
  111. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  112. // so don't build a projectfile for it
  113. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  114. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  115. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  116. {
  117. this->CreateSingleVCProj(l->first.c_str(),l->second);
  118. }
  119. }
  120. }
  121. void cmLocalVisualStudio7Generator::CreateSingleVCProj(const char *lname, cmTarget &target)
  122. {
  123. // add to the list of projects
  124. std::string pname = lname;
  125. this->CreatedProjectNames.push_back(pname);
  126. // create the dsp.cmake file
  127. std::string fname;
  128. fname = this->Makefile->GetStartOutputDirectory();
  129. fname += "/";
  130. fname += lname;
  131. fname += ".vcproj";
  132. // save the name of the real dsp file
  133. std::string realVCProj = fname;
  134. fname += ".cmake";
  135. std::ofstream fout(fname.c_str());
  136. if(!fout)
  137. {
  138. cmSystemTools::Error("Error Writing ", fname.c_str());
  139. }
  140. this->WriteVCProjFile(fout,lname,target);
  141. fout.close();
  142. // if the dsp file has changed, then write it.
  143. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  144. }
  145. void cmLocalVisualStudio7Generator::AddVCProjBuildRule(cmTarget& tgt)
  146. {
  147. std::string dspname = tgt.GetName();
  148. dspname += ".vcproj.cmake";
  149. const char* dsprule = this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  150. cmCustomCommandLine commandLine;
  151. commandLine.push_back(dsprule);
  152. std::string makefileIn = this->Makefile->GetStartDirectory();
  153. makefileIn += "/";
  154. makefileIn += "CMakeLists.txt";
  155. std::string comment = "Building Custom Rule ";
  156. comment += makefileIn;
  157. std::string args;
  158. args = "-H";
  159. args +=
  160. this->Convert(this->Makefile->GetHomeDirectory(), START_OUTPUT, SHELL, true);
  161. commandLine.push_back(args);
  162. args = "-B";
  163. args +=
  164. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  165. START_OUTPUT, SHELL, true);
  166. commandLine.push_back(args);
  167. std::string configFile =
  168. this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  169. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  170. std::vector<std::string> listFiles = this->Makefile->GetListFiles();
  171. bool found = false;
  172. for(std::vector<std::string>::iterator i = listFiles.begin();
  173. i != listFiles.end(); ++i)
  174. {
  175. if(*i == configFile)
  176. {
  177. found = true;
  178. }
  179. }
  180. if(!found)
  181. {
  182. listFiles.push_back(configFile);
  183. }
  184. cmCustomCommandLines commandLines;
  185. commandLines.push_back(commandLine);
  186. const char* no_working_directory = 0;
  187. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  188. makefileIn.c_str(), commandLines,
  189. comment.c_str(),
  190. no_working_directory, true);
  191. if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
  192. {
  193. tgt.GetSourceFiles().push_back(file);
  194. }
  195. else
  196. {
  197. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  198. }
  199. }
  200. void cmLocalVisualStudio7Generator::WriteConfigurations(std::ostream& fout,
  201. const char *libName,
  202. cmTarget &target)
  203. {
  204. std::vector<std::string> *configs =
  205. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator)->GetConfigurations();
  206. fout << "\t<Configurations>\n";
  207. for( std::vector<std::string>::iterator i = configs->begin();
  208. i != configs->end(); ++i)
  209. {
  210. this->WriteConfiguration(fout, i->c_str(), libName, target);
  211. }
  212. fout << "\t</Configurations>\n";
  213. }
  214. // This is a table mapping XML tag IDE names to command line options
  215. struct cmVS7FlagTable
  216. {
  217. const char* IDEName; // name used in the IDE xml file
  218. const char* commandFlag; // command line flag
  219. const char* comment; // comment
  220. const char* value; // string value
  221. };
  222. // fill the table here
  223. // currently the comment field is not used for anything other than documentation
  224. // NOTE: Make sure the longer commandFlag comes FIRST!
  225. cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[] =
  226. {
  227. // option flags (some flags map to the same option)
  228. {"BasicRuntimeChecks", "GZ", "Stack frame checks", "1"},
  229. {"BasicRuntimeChecks", "RTCsu", "Both stack and uninitialized checks ", "3"},
  230. {"BasicRuntimeChecks", "RTCs", "Stack frame checks", "1"},
  231. {"BasicRuntimeChecks", "RTCu", "Uninitialized Variables ", "2"},
  232. {"BasicRuntimeChecks", "RTC1", "Both stack and uninitialized checks ", "3"},
  233. {"DebugInformationFormat", "Z7", "debug format", "1"},
  234. {"DebugInformationFormat", "Zd", "debug format", "2"},
  235. {"DebugInformationFormat", "Zi", "debug format", "3"},
  236. {"DebugInformationFormat", "ZI", "debug format", "4"},
  237. {"EnableEnhancedInstructionSet", "arch:SSE2", "Use sse2 instructions", "2"},
  238. {"EnableEnhancedInstructionSet", "arch:SSE", "Use sse instructions", "1"},
  239. {"FavorSizeOrSpeed", "Ot", "Favor fast code", "1"},
  240. {"FavorSizeOrSpeed", "Os", "Favor small code", "2"},
  241. {"Optimization", "Od", "Non Debug", "0"},
  242. {"Optimization", "O1", "Min Size", "1"},
  243. {"Optimization", "O2", "Max Speed", "2"},
  244. {"Optimization", "Ox", "Max Optimization", "3"},
  245. {"OptimizeForProcessor", "GB", "Blended processor mode", "0"},
  246. {"OptimizeForProcessor", "G5", "Pentium", "1"},
  247. {"OptimizeForProcessor", "G6", "PPro PII PIII", "2"},
  248. {"OptimizeForProcessor", "G7", "Pentium 4 or Athlon", "3"},
  249. {"InlineFunctionExpansion", "Ob0", "no inlines", "0"},
  250. {"InlineFunctionExpansion", "Ob1", "when inline keyword", "1"},
  251. {"InlineFunctionExpansion", "Ob2", "any time you can inline", "2"},
  252. {"RuntimeLibrary", "MTd", "Multithreded debug", "1"},
  253. {"RuntimeLibrary", "MT", "Multithreded", "0"},
  254. {"RuntimeLibrary", "MDd", "Multithreded dll debug", "3"},
  255. {"RuntimeLibrary", "MD", "Multithreded dll", "2"},
  256. {"RuntimeLibrary", "MLd", "Sinble Thread debug", "5"},
  257. {"RuntimeLibrary", "ML", "Sinble Thread", "4"},
  258. {"StructMemberAlignment", "Zp16", "struct align 16 byte ", "5"},
  259. {"StructMemberAlignment", "Zp1", "struct align 1 byte ", "1"},
  260. {"StructMemberAlignment", "Zp2", "struct align 2 byte ", "2"},
  261. {"StructMemberAlignment", "Zp4", "struct align 4 byte ", "3"},
  262. {"StructMemberAlignment", "Zp8", "struct align 8 byte ", "4"},
  263. {"WarningLevel", "W1", "Warning level", "1"},
  264. {"WarningLevel", "W2", "Warning level", "2"},
  265. {"WarningLevel", "W3", "Warning level", "3"},
  266. {"WarningLevel", "W4", "Warning level", "4"},
  267. // boolean flags
  268. {"BufferSecurityCheck", "GS", "Buffer security check", "TRUE"},
  269. {"EnableFibreSafeOptimization", "GT", "OmitFramePointers", "TRUE"},
  270. {"EnableFunctionLevelLinking", "Gy", "EnableFunctionLevelLinking", "TRUE"},
  271. {"EnableIntrinsicFunctions", "Oi", "EnableIntrinsicFunctions", "TRUE"},
  272. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE"},
  273. {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE"},
  274. {"GlobalOptimizations", "Og", "Global Optimize", "TRUE"},
  275. {"ImproveFloatingPointConsistency", "Op", "ImproveFloatingPointConsistency", "TRUE"},
  276. {"MinimalRebuild", "Gm", "minimal rebuild", "TRUE"},
  277. {"OmitFramePointers", "Oy", "OmitFramePointers", "TRUE"},
  278. {"OptimizeForWindowsApplication", "GA", "Optimize for windows", "TRUE"},
  279. {"RuntimeTypeInfo", "GR", "Turn on Run time type information for c++", "TRUE"},
  280. {"SmallerTypeCheck", "RTCc", "smaller type check", "TRUE"},
  281. {"SuppressStartupBanner", "nologo", "SuppressStartupBanner", "TRUE"},
  282. {"WarnAsError", "WX", "Treat warnings as errors", "TRUE"},
  283. {0,0,0,0 }
  284. };
  285. cmVS7FlagTable cmLocalVisualStudio7GeneratorLinkFlagTable[] =
  286. {
  287. // option flags (some flags map to the same option)
  288. {"LinkIncremental", "INCREMENTAL:NO", "link incremental", "1"},
  289. {"LinkIncremental", "INCREMENTAL:YES", "link incremental", "2"},
  290. {0,0,0,0 }
  291. };
  292. void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
  293. const char* configName,
  294. const char *libName,
  295. cmTarget &target)
  296. {
  297. // create a map of xml tags to the values they should have in the output
  298. // for example, "BufferSecurityCheck" = "TRUE"
  299. // first fill this table with the values for the configuration
  300. // Debug, Release, etc,
  301. // Then parse the command line flags specified in CMAKE_CXX_FLAGS
  302. // and CMAKE_C_FLAGS
  303. // and overwrite or add new values to this map
  304. std::map<cmStdString, cmStdString> flagMap;
  305. // since the default is on for this, but if /EHsc is found
  306. // in the flags it will be turned on and we have /EHSC on by
  307. // default in the CXX flags, then this is the only way to turn this off
  308. flagMap["ExceptionHandling"] = "FALSE";
  309. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  310. if(!mfcFlag)
  311. {
  312. mfcFlag = "0";
  313. }
  314. fout << "\t\t<Configuration\n"
  315. << "\t\t\tName=\"" << configName << "|" << this->PlatformName << "\"\n"
  316. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  317. // This is an internal type to Visual Studio, it seems that:
  318. // 4 == static library
  319. // 2 == dll
  320. // 1 == executable
  321. // 10 == utility
  322. const char* configType = "10";
  323. switch(target.GetType())
  324. {
  325. case cmTarget::STATIC_LIBRARY:
  326. configType = "4";
  327. break;
  328. case cmTarget::SHARED_LIBRARY:
  329. case cmTarget::MODULE_LIBRARY:
  330. configType = "2";
  331. break;
  332. case cmTarget::EXECUTABLE:
  333. configType = "1";
  334. break;
  335. case cmTarget::UTILITY:
  336. case cmTarget::GLOBAL_TARGET:
  337. configType = "10";
  338. default:
  339. break;
  340. }
  341. std::string flags;
  342. if(strcmp(configType, "10") != 0)
  343. {
  344. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  345. if(!linkLanguage)
  346. {
  347. cmSystemTools::Error("CMake can not determine linker language for target:",
  348. target.GetName());
  349. return;
  350. }
  351. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0)
  352. {
  353. std::string baseFlagVar = "CMAKE_";
  354. baseFlagVar += linkLanguage;
  355. baseFlagVar += "_FLAGS";
  356. flags = this->Makefile->GetRequiredDefinition(baseFlagVar.c_str());
  357. std::string flagVar = baseFlagVar + std::string("_") +
  358. cmSystemTools::UpperCase(configName);
  359. flags += " ";
  360. flags += this->Makefile->GetRequiredDefinition(flagVar.c_str());
  361. }
  362. }
  363. // Add the target-specific flags.
  364. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  365. {
  366. flags += " ";
  367. flags += targetFlags;
  368. }
  369. // The intermediate directory name consists of a directory for the
  370. // target and a subdirectory for the configuration name.
  371. std::string intermediateDir = this->GetTargetDirectory(target);
  372. intermediateDir += "/";
  373. intermediateDir += configName;
  374. fout << "\t\t\tIntermediateDirectory=\""
  375. << this->ConvertToXMLOutputPath(intermediateDir.c_str())
  376. << "\"\n"
  377. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  378. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  379. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n";
  380. // if -D_UNICODE or /D_UNICODE is found in the flags
  381. // change the character set to unicode, if not then
  382. // default to MBCS
  383. std::string defs = this->Makefile->GetDefineFlags();
  384. if(flags.find("D_UNICODE") != flags.npos ||
  385. defs.find("D_UNICODE") != flags.npos)
  386. {
  387. fout << "\t\t\tCharacterSet=\"1\">\n";
  388. }
  389. else
  390. {
  391. fout << "\t\t\tCharacterSet=\"2\">\n";
  392. }
  393. fout << "\t\t\t<Tool\n"
  394. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  395. << "\t\t\t\tAdditionalOptions=\"";
  396. // now fill the flagMap from the command line flags, and
  397. // if a flag is used, it will be removed from the flags string by
  398. // this function call
  399. this->FillFlagMapFromCommandFlags(flagMap,
  400. &cmLocalVisualStudio7GeneratorFlagTable[0],
  401. flags);
  402. std::string defineFlags = this->Makefile->GetDefineFlags();
  403. // now check the define flags for flags other than -D and
  404. // put them in the map, the -D flags will be left in the defineFlags
  405. // variable as -D is not in the flagMap
  406. this->FillFlagMapFromCommandFlags(flagMap,
  407. &cmLocalVisualStudio7GeneratorFlagTable[0],
  408. defineFlags);
  409. // output remaining flags that were not mapped to anything
  410. fout << this->EscapeForXML(flags.c_str()).c_str();
  411. fout << " -DCMAKE_INTDIR=\\&quot;" << configName << "\\&quot;"
  412. << "\"\n";
  413. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  414. std::vector<std::string> includes;
  415. this->GetIncludeDirectories(includes);
  416. std::vector<std::string>::iterator i = includes.begin();
  417. for(;i != includes.end(); ++i)
  418. {
  419. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  420. fout << ipath << ";";
  421. }
  422. fout << "\"\n";
  423. // set a few cmake specific flags
  424. if(this->Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  425. {
  426. flagMap["RuntimeTypeInfo"] = "TRUE";
  427. }
  428. if ( this->Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") )
  429. {
  430. flagMap["WarningLevel"] = this->Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL");
  431. }
  432. // Now copy the flag map into the xml for the file
  433. for(std::map<cmStdString, cmStdString>::iterator m = flagMap.begin();
  434. m != flagMap.end(); ++m)
  435. {
  436. fout << "\t\t\t\t" << m->first << "=\"" << m->second << "\"\n";
  437. }
  438. fout << "\t\t\t\tPreprocessorDefinitions=\"";
  439. if(target.GetType() == cmTarget::SHARED_LIBRARY
  440. || target.GetType() == cmTarget::MODULE_LIBRARY)
  441. {
  442. std::string exportSymbol;
  443. if (const char* custom_export_name =
  444. target.GetProperty("DEFINE_SYMBOL"))
  445. {
  446. exportSymbol = custom_export_name;
  447. }
  448. else
  449. {
  450. std::string id = libName;
  451. id += "_EXPORTS";
  452. exportSymbol = cmSystemTools::MakeCindentifier(id.c_str());
  453. }
  454. fout << "," << exportSymbol;
  455. }
  456. this->OutputDefineFlags(defineFlags.c_str(), fout);
  457. fout << "\"\n";
  458. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  459. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
  460. std::map<cmStdString, cmStdString>::iterator mi = flagMap.find("DebugInformationFormat");
  461. if(mi != flagMap.end() && mi->second != "1")
  462. {
  463. fout << "\t\t\t\tProgramDatabaseFileName=\""
  464. << this->LibraryOutputPath
  465. << "$(OutDir)/" << libName << ".pdb\"\n";
  466. }
  467. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  468. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  469. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  470. << "\t\t\t\tAdditionalIncludeDirectories=\"";
  471. for(i = includes.begin();i != includes.end(); ++i)
  472. {
  473. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  474. fout << ipath << ";";
  475. }
  476. // add the -D flags to the RC tool
  477. fout << "\"\n"
  478. << "\t\t\t\tPreprocessorDefinitions=\"";
  479. this->OutputDefineFlags(defineFlags.c_str(), fout);
  480. fout << "\" />\n";
  481. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"\n";
  482. fout << "\t\t\t\tPreprocessorDefinitions=\"";
  483. this->OutputDefineFlags(defineFlags.c_str(), fout);
  484. fout << "\"\n";
  485. fout << "\t\t\t\tMkTypLibCompatible=\"FALSE\"\n";
  486. fout << "\t\t\t\tTargetEnvironment=\"1\"\n";
  487. fout << "\t\t\t\tGenerateStublessProxies=\"TRUE\"\n";
  488. fout << "\t\t\t\tTypeLibraryName=\"$(InputName).tlb\"\n";
  489. fout << "\t\t\t\tOutputDirectory=\"$(IntDir)\"\n";
  490. fout << "\t\t\t\tHeaderFileName=\"$(InputName).h\"\n";
  491. fout << "\t\t\t\tDLLDataFileName=\"\"\n";
  492. fout << "\t\t\t\tInterfaceIdentifierFileName=\"$(InputName)_i.c\"\n";
  493. fout << "\t\t\t\tProxyFileName=\"$(InputName)_p.c\"/>\n";
  494. // end of <Tool Name=VCMIDLTool
  495. this->OutputTargetRules(fout, target, libName);
  496. this->OutputBuildTool(fout, configName, libName, target);
  497. fout << "\t\t</Configuration>\n";
  498. }
  499. void cmLocalVisualStudio7Generator::FillFlagMapFromCommandFlags(
  500. std::map<cmStdString, cmStdString>& flagMap,
  501. cmVS7FlagTable* flagTable,
  502. std::string& flags)
  503. {
  504. std::string replace;
  505. std::string option;
  506. while(flagTable->IDEName)
  507. {
  508. option.reserve(strlen(flagTable->commandFlag)+2);
  509. // first do the - version
  510. option = "-";
  511. option += flagTable->commandFlag;
  512. while(flags.find(option) != flags.npos)
  513. {
  514. // replace the flag
  515. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  516. // now put value into flag map
  517. flagMap[flagTable->IDEName] = flagTable->value;
  518. }
  519. // now do the / version
  520. option[0] = '/';
  521. while(flags.find(option) != flags.npos)
  522. {
  523. // replace the flag
  524. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  525. // now put value into flag map
  526. flagMap[flagTable->IDEName] = flagTable->value;
  527. }
  528. // move to next flag
  529. flagTable++;
  530. }
  531. // If verbose makefiles have been requested and the /nologo option
  532. // was not given explicitly in the flags we want to add an attribute
  533. // to the generated project to disable logo suppression. Otherwise
  534. // the GUI default is to enable suppression.
  535. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  536. {
  537. if(flagMap.find("SuppressStartupBanner") == flagMap.end())
  538. {
  539. flagMap["SuppressStartupBanner"] = "FALSE";
  540. }
  541. }
  542. }
  543. //----------------------------------------------------------------------------
  544. std::string
  545. cmLocalVisualStudio7Generator
  546. ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
  547. {
  548. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  549. std::string extraLinkOptionsBuildTypeDef = rootLinkerFlags + "_" + configTypeUpper;
  550. std::string extraLinkOptionsBuildType =
  551. this->Makefile->GetRequiredDefinition(extraLinkOptionsBuildTypeDef.c_str());
  552. return extraLinkOptionsBuildType;
  553. }
  554. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  555. const char* configName,
  556. const char *libName,
  557. cmTarget &target)
  558. {
  559. std::string targetFullName = target.GetFullName(configName);
  560. std::string temp;
  561. std::string extraLinkOptions;
  562. if(target.GetType() == cmTarget::EXECUTABLE)
  563. {
  564. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS") +
  565. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
  566. }
  567. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  568. {
  569. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS") +
  570. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
  571. }
  572. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  573. {
  574. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS") +
  575. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
  576. }
  577. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  578. if(targetLinkFlags)
  579. {
  580. extraLinkOptions += " ";
  581. extraLinkOptions += targetLinkFlags;
  582. }
  583. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  584. std::string linkFlagsConfig = "LINK_FLAGS_";
  585. linkFlagsConfig += configTypeUpper;
  586. targetLinkFlags = target.GetProperty(linkFlagsConfig.c_str());
  587. if(targetLinkFlags)
  588. {
  589. extraLinkOptions += " ";
  590. extraLinkOptions += targetLinkFlags;
  591. }
  592. std::map<cmStdString, cmStdString> flagMap;
  593. this->
  594. FillFlagMapFromCommandFlags(flagMap,
  595. &cmLocalVisualStudio7GeneratorLinkFlagTable[0],
  596. extraLinkOptions);
  597. switch(target.GetType())
  598. {
  599. case cmTarget::STATIC_LIBRARY:
  600. {
  601. std::string libpath = this->LibraryOutputPath +
  602. "$(OutDir)/" + targetFullName;
  603. fout << "\t\t\t<Tool\n"
  604. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  605. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  606. {
  607. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  608. }
  609. fout << "\t\t\t\tOutputFile=\""
  610. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  611. break;
  612. }
  613. case cmTarget::SHARED_LIBRARY:
  614. case cmTarget::MODULE_LIBRARY:
  615. {
  616. // Compute the link library and directory information.
  617. std::vector<cmStdString> linkLibs;
  618. std::vector<cmStdString> linkDirs;
  619. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  620. // Get the language to use for linking.
  621. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  622. if(!linkLanguage)
  623. {
  624. cmSystemTools::Error("CMake can not determine linker language for target:",
  625. target.GetName());
  626. return;
  627. }
  628. // Compute the variable name to lookup standard libraries for this
  629. // language.
  630. std::string standardLibsVar = "CMAKE_";
  631. standardLibsVar += linkLanguage;
  632. standardLibsVar += "_STANDARD_LIBRARIES";
  633. fout << "\t\t\t<Tool\n"
  634. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  635. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  636. if(extraLinkOptions.size())
  637. {
  638. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  639. extraLinkOptions.c_str()).c_str();
  640. }
  641. // Use the NOINHERIT macro to avoid getting VS project default
  642. // libraries which may be set by the user to something bad.
  643. fout << "\"\n"
  644. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  645. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  646. << " ";
  647. this->OutputLibraries(fout, linkLibs);
  648. fout << "\"\n";
  649. temp = this->LibraryOutputPath;
  650. temp += configName;
  651. temp += "/";
  652. temp += targetFullName;
  653. fout << "\t\t\t\tOutputFile=\""
  654. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  655. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  656. i != flagMap.end(); ++i)
  657. {
  658. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  659. }
  660. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  661. this->OutputLibraryDirectories(fout, linkDirs);
  662. fout << "\"\n";
  663. this->OutputModuleDefinitionFile(fout, target);
  664. temp = this->LibraryOutputPath;
  665. temp += "$(OutDir)/";
  666. temp += libName;
  667. temp += ".pdb";
  668. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  669. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  670. if(strcmp(configName, "Debug") == 0
  671. || strcmp(configName, "RelWithDebInfo") == 0)
  672. {
  673. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  674. }
  675. std::string stackVar = "CMAKE_";
  676. stackVar += linkLanguage;
  677. stackVar += "_STACK_SIZE";
  678. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  679. if(stackVal)
  680. {
  681. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  682. }
  683. temp = this->LibraryOutputPath;
  684. temp += configName;
  685. temp += "/";
  686. temp += cmSystemTools::GetFilenameWithoutLastExtension(targetFullName.c_str());
  687. temp += ".lib";
  688. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  689. }
  690. break;
  691. case cmTarget::EXECUTABLE:
  692. {
  693. // Compute the link library and directory information.
  694. std::vector<cmStdString> linkLibs;
  695. std::vector<cmStdString> linkDirs;
  696. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  697. // Get the language to use for linking.
  698. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  699. if(!linkLanguage)
  700. {
  701. cmSystemTools::Error("CMake can not determine linker language for target:",
  702. target.GetName());
  703. return;
  704. }
  705. // Compute the variable name to lookup standard libraries for this
  706. // language.
  707. std::string standardLibsVar = "CMAKE_";
  708. standardLibsVar += linkLanguage;
  709. standardLibsVar += "_STANDARD_LIBRARIES";
  710. fout << "\t\t\t<Tool\n"
  711. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  712. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  713. if(extraLinkOptions.size())
  714. {
  715. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  716. extraLinkOptions.c_str()).c_str();
  717. }
  718. // Use the NOINHERIT macro to avoid getting VS project default
  719. // libraries which may be set by the user to something bad.
  720. fout << "\"\n"
  721. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  722. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  723. << " ";
  724. this->OutputLibraries(fout, linkLibs);
  725. fout << "\"\n";
  726. temp = this->ExecutableOutputPath;
  727. temp += configName;
  728. temp += "/";
  729. temp += targetFullName;
  730. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  731. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  732. i != flagMap.end(); ++i)
  733. {
  734. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  735. }
  736. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  737. this->OutputLibraryDirectories(fout, linkDirs);
  738. fout << "\"\n";
  739. fout << "\t\t\t\tProgramDatabaseFile=\"" << this->LibraryOutputPath
  740. << "$(OutDir)\\" << libName << ".pdb\"\n";
  741. if(strcmp(configName, "Debug") == 0
  742. || strcmp(configName, "RelWithDebInfo") == 0)
  743. {
  744. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  745. }
  746. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  747. {
  748. fout << "\t\t\t\tSubSystem=\"2\"\n";
  749. }
  750. else
  751. {
  752. fout << "\t\t\t\tSubSystem=\"1\"\n";
  753. }
  754. std::string stackVar = "CMAKE_";
  755. stackVar += linkLanguage;
  756. stackVar += "_STACK_SIZE";
  757. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  758. if(stackVal)
  759. {
  760. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  761. }
  762. fout << "/>\n";
  763. break;
  764. }
  765. case cmTarget::UTILITY:
  766. case cmTarget::GLOBAL_TARGET:
  767. break;
  768. }
  769. }
  770. void cmLocalVisualStudio7Generator::OutputModuleDefinitionFile(std::ostream& fout,
  771. cmTarget &target)
  772. {
  773. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  774. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  775. i != classes.end(); i++)
  776. {
  777. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  778. {
  779. fout << "\t\t\t\tModuleDefinitionFile=\""
  780. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  781. << "\"\n";
  782. return;
  783. }
  784. }
  785. }
  786. //----------------------------------------------------------------------------
  787. void
  788. cmLocalVisualStudio7Generator
  789. ::OutputLibraries(std::ostream& fout,
  790. std::vector<cmStdString> const& libs)
  791. {
  792. for(std::vector<cmStdString>::const_iterator l = libs.begin();
  793. l != libs.end(); ++l)
  794. {
  795. fout << this->ConvertToXMLOutputPath(l->c_str()) << " ";
  796. }
  797. }
  798. //----------------------------------------------------------------------------
  799. void
  800. cmLocalVisualStudio7Generator
  801. ::OutputLibraryDirectories(std::ostream& fout,
  802. std::vector<cmStdString> const& dirs)
  803. {
  804. const char* comma = "";
  805. for(std::vector<cmStdString>::const_iterator d = dirs.begin();
  806. d != dirs.end(); ++d)
  807. {
  808. // Remove any trailing slash and skip empty paths.
  809. std::string dir = *d;
  810. if(dir[dir.size()-1] == '/')
  811. {
  812. dir = dir.substr(0, dir.size()-1);
  813. }
  814. if(dir.empty())
  815. {
  816. continue;
  817. }
  818. // Switch to a relative path specification if it is shorter.
  819. if(cmSystemTools::FileIsFullPath(dir.c_str()))
  820. {
  821. std::string rel = this->Convert(dir.c_str(), START_OUTPUT, UNCHANGED);
  822. if(rel.size() < dir.size())
  823. {
  824. dir = rel;
  825. }
  826. }
  827. // First search a configuration-specific subdirectory and then the
  828. // original directory.
  829. fout << comma << this->ConvertToXMLOutputPath((dir+"/$(OutDir)").c_str())
  830. << "," << this->ConvertToXMLOutputPath(dir.c_str());
  831. comma = ",";
  832. }
  833. }
  834. //----------------------------------------------------------------------------
  835. void cmLocalVisualStudio7Generator::OutputDefineFlags(const char* flags,
  836. std::ostream& fout)
  837. {
  838. std::string defs = flags;
  839. cmSystemTools::ReplaceString(defs, "/D","-D");
  840. std::string::size_type pos = defs.find("-D");
  841. bool done = pos == std::string::npos;
  842. if(!done)
  843. {
  844. fout << ",";
  845. }
  846. while(!done)
  847. {
  848. std::string::size_type nextpos = defs.find("-D", pos+2);
  849. std::string define;
  850. if(nextpos != std::string::npos)
  851. {
  852. define = defs.substr(pos+2, nextpos - pos -3);
  853. }
  854. else
  855. {
  856. define = defs.substr(pos+2);
  857. done = true;
  858. }
  859. // Double-quotes in the value of the definition must be escaped
  860. // with a backslash. The entire definition should be quoted in
  861. // the generated xml attribute to avoid confusing the VS parser.
  862. cmSystemTools::ReplaceString(define, "\"", "\\&quot;");
  863. fout << "&quot;" << define << "&quot;,";
  864. if(!done)
  865. {
  866. pos = defs.find("-D", nextpos);
  867. }
  868. }
  869. }
  870. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  871. const char *libName,
  872. cmTarget &target)
  873. {
  874. // get the configurations
  875. std::vector<std::string> *configs =
  876. static_cast<cmGlobalVisualStudio7Generator *>
  877. (this->GlobalGenerator)->GetConfigurations();
  878. // trace the visual studio dependencies
  879. std::string name = libName;
  880. name += ".vcproj.cmake";
  881. // We may be modifying the source groups temporarily, so make a copy.
  882. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  883. // get the classes from the source lists then add them to the groups
  884. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  885. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  886. i != classes.end(); i++)
  887. {
  888. // Add the file to the list of sources.
  889. std::string source = (*i)->GetFullPath();
  890. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  891. {
  892. this->ModuleDefinitionFile = (*i)->GetFullPath();
  893. }
  894. cmSourceGroup& sourceGroup =
  895. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  896. sourceGroup.AssignSource(*i);
  897. }
  898. // open the project
  899. this->WriteProjectStart(fout, libName, target, sourceGroups);
  900. // write the configuration information
  901. this->WriteConfigurations(fout, libName, target);
  902. fout << "\t<Files>\n";
  903. // Loop through every source group.
  904. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  905. {
  906. cmSourceGroup sg = sourceGroups[i];
  907. this->WriteGroup(&sg, target, fout, libName, configs);
  908. }
  909. //}
  910. fout << "\t</Files>\n";
  911. // Write the VCProj file's footer.
  912. this->WriteVCProjFooter(fout);
  913. }
  914. void cmLocalVisualStudio7Generator::WriteGroup(const cmSourceGroup *sg, cmTarget target,
  915. std::ostream &fout, const char *libName, std::vector<std::string> *configs)
  916. {
  917. const std::vector<const cmSourceFile *> &sourceFiles =
  918. sg->GetSourceFiles();
  919. // If the group is empty, don't write it at all.
  920. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  921. {
  922. return;
  923. }
  924. // If the group has a name, write the header.
  925. std::string name = sg->GetName();
  926. if(name != "")
  927. {
  928. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  929. }
  930. // Loop through each source in the source group.
  931. std::string objectName;
  932. for(std::vector<const cmSourceFile *>::const_iterator sf =
  933. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  934. {
  935. std::string source = (*sf)->GetFullPath();
  936. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  937. std::string compileFlags;
  938. std::string additionalDeps;
  939. objectName = (*sf)->GetSourceName();
  940. if(!(*sf)->GetPropertyAsBool("HEADER_FILE_ONLY" )
  941. && objectName.find("/") != objectName.npos)
  942. {
  943. cmSystemTools::ReplaceString(objectName, "/", "_");
  944. objectName += ".obj";
  945. }
  946. else
  947. {
  948. objectName = "";
  949. }
  950. // Add per-source flags.
  951. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  952. if(cflags)
  953. {
  954. compileFlags += " ";
  955. compileFlags += cflags;
  956. }
  957. const char* lang =
  958. this->GlobalGenerator->GetLanguageFromExtension((*sf)->GetSourceExtension().c_str());
  959. if(lang && strcmp(lang, "CXX") == 0)
  960. {
  961. // force a C++ file type
  962. compileFlags += " /TP ";
  963. }
  964. // Check for extra object-file dependencies.
  965. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  966. if(deps)
  967. {
  968. std::vector<std::string> depends;
  969. cmSystemTools::ExpandListArgument(deps, depends);
  970. if(!depends.empty())
  971. {
  972. std::vector<std::string>::iterator i = depends.begin();
  973. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  974. for(++i;i != depends.end(); ++i)
  975. {
  976. additionalDeps += ";";
  977. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  978. }
  979. }
  980. }
  981. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  982. target.GetType() == cmTarget::GLOBAL_TARGET )
  983. {
  984. fout << "\t\t\t<File\n";
  985. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  986. // Tell MS-Dev what the source is. If the compiler knows how to
  987. // build it, then it will.
  988. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  989. if (command)
  990. {
  991. // Construct the entire set of commands in one string.
  992. std::string script = this->ConstructScript(command->GetCommandLines(),
  993. command->GetWorkingDirectory());
  994. std::string comment = this->ConstructComment(*command);
  995. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  996. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  997. comment.c_str(), command->GetDepends(),
  998. command->GetOutputs(), flags);
  999. }
  1000. else if(compileFlags.size() || additionalDeps.length() || objectName.size())
  1001. {
  1002. const char* aCompilerTool = "VCCLCompilerTool";
  1003. std::string ext = (*sf)->GetSourceExtension();
  1004. ext = cmSystemTools::LowerCase(ext);
  1005. if(ext == "idl")
  1006. {
  1007. aCompilerTool = "VCMIDLTool";
  1008. }
  1009. if(ext == "rc")
  1010. {
  1011. aCompilerTool = "VCResourceCompilerTool";
  1012. }
  1013. if(ext == "def")
  1014. {
  1015. aCompilerTool = "VCCustomBuildTool";
  1016. }
  1017. for(std::vector<std::string>::iterator i = configs->begin();
  1018. i != configs->end(); ++i)
  1019. {
  1020. fout << "\t\t\t\t<FileConfiguration\n"
  1021. << "\t\t\t\t\tName=\"" << *i << "|" << this->PlatformName << "\">\n"
  1022. << "\t\t\t\t\t<Tool\n"
  1023. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1024. if(compileFlags.size())
  1025. {
  1026. fout << "\t\t\t\t\tAdditionalOptions=\""
  1027. << this->EscapeForXML(compileFlags.c_str()) << "\"\n";
  1028. }
  1029. if(additionalDeps.length())
  1030. {
  1031. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1032. << additionalDeps.c_str() << "\"\n";
  1033. }
  1034. if(objectName.size())
  1035. {
  1036. fout << "\t\t\t\t\tObjectFile=\"$(IntDir)/"
  1037. << objectName.c_str() << "\"\n";
  1038. }
  1039. fout << "\t\t\t\t\t/>\n"
  1040. << "\t\t\t\t</FileConfiguration>\n";
  1041. }
  1042. }
  1043. fout << "\t\t\t</File>\n";
  1044. }
  1045. }
  1046. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1047. for(unsigned int i=0;i<children.size();++i)
  1048. {
  1049. this->WriteGroup(&children[i], target, fout, libName, configs);
  1050. }
  1051. // If the group has a name, write the footer.
  1052. if(name != "")
  1053. {
  1054. this->WriteVCProjEndGroup(fout);
  1055. }
  1056. }
  1057. void cmLocalVisualStudio7Generator::
  1058. WriteCustomRule(std::ostream& fout,
  1059. const char* source,
  1060. const char* command,
  1061. const char* comment,
  1062. const std::vector<std::string>& depends,
  1063. const std::vector<std::string>& outputs,
  1064. const char* compileFlags)
  1065. {
  1066. // Write the rule for each configuration.
  1067. std::vector<std::string>::iterator i;
  1068. std::vector<std::string> *configs =
  1069. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator)->GetConfigurations();
  1070. for(i = configs->begin(); i != configs->end(); ++i)
  1071. {
  1072. fout << "\t\t\t\t<FileConfiguration\n";
  1073. fout << "\t\t\t\t\tName=\"" << *i << "|" << this->PlatformName << "\">\n";
  1074. if(compileFlags)
  1075. {
  1076. fout << "\t\t\t\t\t<Tool\n"
  1077. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1078. << "\t\t\t\t\tAdditionalOptions=\""
  1079. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1080. }
  1081. fout << "\t\t\t\t\t<Tool\n"
  1082. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1083. << "\t\t\t\t\tDescription=\"" << this->EscapeForXML(comment) << "\"\n"
  1084. << "\t\t\t\t\tCommandLine=\"" << this->EscapeForXML(command) << "\"\n"
  1085. << "\t\t\t\t\tAdditionalDependencies=\"";
  1086. // Write out the dependencies for the rule.
  1087. std::string temp;
  1088. for(std::vector<std::string>::const_iterator d = depends.begin();
  1089. d != depends.end(); ++d)
  1090. {
  1091. // Lookup the real name of the dependency in case it is a CMake target.
  1092. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  1093. fout << this->ConvertToXMLOutputPath(dep.c_str())
  1094. << ";";
  1095. }
  1096. fout << "\"\n";
  1097. fout << "\t\t\t\t\tOutputs=\"";
  1098. if(outputs.empty())
  1099. {
  1100. fout << source << "_force";
  1101. }
  1102. else
  1103. {
  1104. // Write a rule for the output generated by this command.
  1105. const char* sep = "";
  1106. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1107. o != outputs.end(); ++o)
  1108. {
  1109. fout << sep << this->ConvertToXMLOutputPathSingle(o->c_str());
  1110. sep = ";";
  1111. }
  1112. }
  1113. fout << "\"/>\n";
  1114. fout << "\t\t\t\t</FileConfiguration>\n";
  1115. }
  1116. }
  1117. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1118. const char* group,
  1119. const char* )
  1120. {
  1121. fout << "\t\t<Filter\n"
  1122. << "\t\t\tName=\"" << group << "\"\n"
  1123. << "\t\t\tFilter=\"\">\n";
  1124. }
  1125. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1126. {
  1127. fout << "\t\t</Filter>\n";
  1128. }
  1129. // look for custom rules on a target and collect them together
  1130. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  1131. cmTarget &target,
  1132. const char * /*libName*/)
  1133. {
  1134. if (target.GetType() > cmTarget::GLOBAL_TARGET)
  1135. {
  1136. return;
  1137. }
  1138. // add the pre build rules
  1139. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1140. bool init = false;
  1141. for (std::vector<cmCustomCommand>::const_iterator cr =
  1142. target.GetPreBuildCommands().begin();
  1143. cr != target.GetPreBuildCommands().end(); ++cr)
  1144. {
  1145. if(!init)
  1146. {
  1147. fout << "\nCommandLine=\"";
  1148. init = true;
  1149. }
  1150. std::string script = this->ConstructScript(cr->GetCommandLines(), cr->GetWorkingDirectory());
  1151. fout << this->EscapeForXML(script.c_str()).c_str();
  1152. }
  1153. if (init)
  1154. {
  1155. fout << "\"";
  1156. }
  1157. fout << "/>\n";
  1158. // add the pre Link rules
  1159. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1160. init = false;
  1161. for (std::vector<cmCustomCommand>::const_iterator cr =
  1162. target.GetPreLinkCommands().begin();
  1163. cr != target.GetPreLinkCommands().end(); ++cr)
  1164. {
  1165. if(!init)
  1166. {
  1167. fout << "\nCommandLine=\"";
  1168. init = true;
  1169. }
  1170. std::string script = this->ConstructScript(cr->GetCommandLines(),
  1171. cr->GetWorkingDirectory());
  1172. fout << this->EscapeForXML(script.c_str()).c_str();
  1173. }
  1174. if (init)
  1175. {
  1176. fout << "\"";
  1177. }
  1178. fout << "/>\n";
  1179. // add the PostBuild rules
  1180. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1181. init = false;
  1182. for (std::vector<cmCustomCommand>::const_iterator cr =
  1183. target.GetPostBuildCommands().begin();
  1184. cr != target.GetPostBuildCommands().end(); ++cr)
  1185. {
  1186. if(!init)
  1187. {
  1188. fout << "\nCommandLine=\"";
  1189. init = true;
  1190. }
  1191. std::string script = this->ConstructScript(cr->GetCommandLines(), cr->GetWorkingDirectory());
  1192. fout << this->EscapeForXML(script.c_str()).c_str();
  1193. }
  1194. if (init)
  1195. {
  1196. fout << "\"";
  1197. }
  1198. fout << "/>\n";
  1199. }
  1200. void
  1201. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1202. const char *libName,
  1203. cmTarget & target,
  1204. std::vector<cmSourceGroup> &)
  1205. {
  1206. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1207. << "<VisualStudioProject\n"
  1208. << "\tProjectType=\"Visual C++\"\n";
  1209. if(this->Version == 71)
  1210. {
  1211. fout << "\tVersion=\"7.10\"\n";
  1212. }
  1213. else
  1214. {
  1215. if (this->Version == 8)
  1216. {
  1217. fout << "\tVersion=\"8.00\"\n";
  1218. }
  1219. else
  1220. {
  1221. fout << "\tVersion=\"7.00\"\n";
  1222. }
  1223. }
  1224. const char* projLabel = target.GetProperty("PROJECT_LABEL");
  1225. if(!projLabel)
  1226. {
  1227. projLabel = libName;
  1228. }
  1229. const char* keyword = target.GetProperty("VS_KEYWORD");
  1230. if(!keyword)
  1231. {
  1232. keyword = "Win32Proj";
  1233. }
  1234. cmGlobalVisualStudio7Generator* gg =
  1235. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1236. fout << "\tName=\"" << projLabel << "\"\n";
  1237. if(this->Version == 8)
  1238. {
  1239. fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
  1240. }
  1241. fout << "\tSccProjectName=\"\"\n"
  1242. << "\tSccLocalPath=\"\"\n"
  1243. << "\tKeyword=\"" << keyword << "\">\n"
  1244. << "\t<Platforms>\n"
  1245. << "\t\t<Platform\n\t\t\tName=\"" << this->PlatformName << "\"/>\n"
  1246. << "\t</Platforms>\n";
  1247. }
  1248. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1249. {
  1250. fout << "\t<Globals>\n"
  1251. << "\t</Globals>\n"
  1252. << "</VisualStudioProject>\n";
  1253. }
  1254. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1255. {
  1256. std::string ret = s;
  1257. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1258. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1259. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1260. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1261. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1262. return ret;
  1263. }
  1264. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  1265. {
  1266. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1267. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1268. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1269. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1270. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1271. return ret;
  1272. }
  1273. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  1274. {
  1275. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1276. cmSystemTools::ReplaceString(ret, "\"", "");
  1277. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1278. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1279. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1280. return ret;
  1281. }
  1282. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1283. {
  1284. cmLocalGenerator::ConfigureFinalPass();
  1285. cmTargets &tgts = this->Makefile->GetTargets();
  1286. cmGlobalVisualStudio7Generator* gg =
  1287. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1288. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1289. {
  1290. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1291. {
  1292. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1293. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1294. std::string project_name = cmds[0][0];
  1295. gg->CreateGUID(project_name.c_str());
  1296. }
  1297. else
  1298. {
  1299. gg->CreateGUID(l->first.c_str());
  1300. }
  1301. }
  1302. }
  1303. //----------------------------------------------------------------------------
  1304. std::string cmLocalVisualStudio7Generator::GetTargetDirectory(cmTarget& target)
  1305. {
  1306. std::string dir;
  1307. dir += target.GetName();
  1308. dir += ".dir";
  1309. return dir;
  1310. }