cmLocalVisualStudio7Generator.cxx 49 KB

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