cmLocalVisualStudio7Generator.cxx 50 KB

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