1
0

cmLocalVisualStudio6Generator.cxx 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGlobalGenerator.h"
  11. #include "cmLocalVisualStudio6Generator.h"
  12. #include "cmMakefile.h"
  13. #include "cmSystemTools.h"
  14. #include "cmSourceFile.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmCustomCommandGenerator.h"
  17. #include "cmake.h"
  18. #include "cmComputeLinkInformation.h"
  19. #include <cmsys/RegularExpression.hxx>
  20. #include <cmsys/FStream.hxx>
  21. cmLocalVisualStudio6Generator
  22. ::cmLocalVisualStudio6Generator(cmGlobalGenerator* gg,
  23. cmState::Snapshot snapshot):
  24. cmLocalVisualStudioGenerator(gg, snapshot)
  25. {
  26. }
  27. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. // Helper class to write build events.
  32. class cmLocalVisualStudio6Generator::EventWriter
  33. {
  34. public:
  35. EventWriter(cmLocalVisualStudio6Generator* lg,
  36. const std::string& config, std::string& code):
  37. LG(lg), Config(config), Code(code), First(true) {}
  38. void Start(const char* event)
  39. {
  40. this->First = true;
  41. this->Event = event;
  42. }
  43. void Finish()
  44. {
  45. this->Code += (this->First? "" : "\n");
  46. }
  47. void Write(std::vector<cmCustomCommand> const& ccs)
  48. {
  49. for(std::vector<cmCustomCommand>::const_iterator ci = ccs.begin();
  50. ci != ccs.end(); ++ci)
  51. {
  52. this->Write(*ci);
  53. }
  54. }
  55. void Write(cmCustomCommand const& cc)
  56. {
  57. cmCustomCommandGenerator ccg(cc, this->Config, this->LG);
  58. if(this->First)
  59. {
  60. this->Code += this->Event + "_Cmds=";
  61. this->First = false;
  62. }
  63. else
  64. {
  65. this->Code += "\\\n\t";
  66. }
  67. this->Code += this->LG->ConstructScript(ccg, "\\\n\t");
  68. }
  69. private:
  70. cmLocalVisualStudio6Generator* LG;
  71. std::string Config;
  72. std::string& Code;
  73. bool First;
  74. std::string Event;
  75. };
  76. void cmLocalVisualStudio6Generator::AddCMakeListsRules()
  77. {
  78. cmTargets &tgts = this->Makefile->GetTargets();
  79. for(cmTargets::iterator l = tgts.begin();
  80. l != tgts.end(); l++)
  81. {
  82. if (l->second.GetType() == cmTarget::INTERFACE_LIBRARY
  83. || l->second.GetType() == cmTarget::GLOBAL_TARGET)
  84. {
  85. continue;
  86. }
  87. // Add a rule to regenerate the build system when the target
  88. // specification source changes.
  89. const char* suppRegenRule =
  90. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  91. if (!cmSystemTools::IsOn(suppRegenRule))
  92. {
  93. this->AddDSPBuildRule(l->second);
  94. }
  95. }
  96. }
  97. void cmLocalVisualStudio6Generator::Generate()
  98. {
  99. this->OutputDSPFile();
  100. }
  101. void cmLocalVisualStudio6Generator::OutputDSPFile()
  102. {
  103. // If not an in source build, then create the output directory
  104. if(strcmp(this->Makefile->GetCurrentBinaryDirectory(),
  105. this->Makefile->GetHomeDirectory()) != 0)
  106. {
  107. if(!cmSystemTools::MakeDirectory
  108. (this->Makefile->GetCurrentBinaryDirectory()))
  109. {
  110. cmSystemTools::Error("Error creating directory ",
  111. this->Makefile->GetCurrentBinaryDirectory());
  112. }
  113. }
  114. // Create the DSP or set of DSP's for libraries and executables
  115. cmTargets &tgts = this->Makefile->GetTargets();
  116. // build any targets
  117. for(cmTargets::iterator l = tgts.begin();
  118. l != tgts.end(); l++)
  119. {
  120. switch(l->second.GetType())
  121. {
  122. case cmTarget::STATIC_LIBRARY:
  123. case cmTarget::OBJECT_LIBRARY:
  124. this->SetBuildType(STATIC_LIBRARY, l->first.c_str(), l->second);
  125. break;
  126. case cmTarget::SHARED_LIBRARY:
  127. case cmTarget::MODULE_LIBRARY:
  128. this->SetBuildType(DLL, l->first.c_str(), l->second);
  129. break;
  130. case cmTarget::EXECUTABLE:
  131. this->SetBuildType(EXECUTABLE,l->first.c_str(), l->second);
  132. break;
  133. case cmTarget::UTILITY:
  134. case cmTarget::GLOBAL_TARGET:
  135. this->SetBuildType(UTILITY, l->first.c_str(), l->second);
  136. break;
  137. case cmTarget::INTERFACE_LIBRARY:
  138. continue;
  139. default:
  140. cmSystemTools::Error("Bad target type: ", l->first.c_str());
  141. break;
  142. }
  143. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  144. // so don't build a projectfile for it
  145. const char* path =
  146. l->second.GetProperty("EXTERNAL_MSPROJECT");
  147. if(!path)
  148. {
  149. // check to see if the dsp is going into a sub-directory
  150. std::string::size_type pos = l->first.rfind('/');
  151. if(pos != std::string::npos)
  152. {
  153. std::string dir = this->Makefile->GetCurrentBinaryDirectory();
  154. dir += "/";
  155. dir += l->first.substr(0, pos);
  156. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  157. {
  158. cmSystemTools::Error("Error creating directory: ", dir.c_str());
  159. }
  160. }
  161. this->CreateSingleDSP(l->first.c_str(),l->second);
  162. }
  163. }
  164. }
  165. // Utility function to make a valid VS6 *.dsp filename out
  166. // of a CMake target name:
  167. //
  168. extern std::string GetVS6TargetName(const std::string& targetName);
  169. void cmLocalVisualStudio6Generator::CreateSingleDSP(const std::string& lname,
  170. cmTarget &target)
  171. {
  172. // add to the list of projects
  173. std::string pname = GetVS6TargetName(lname);
  174. // create the dsp.cmake file
  175. std::string fname;
  176. fname = this->Makefile->GetCurrentBinaryDirectory();
  177. fname += "/";
  178. fname += pname;
  179. fname += ".dsp";
  180. // save the name of the real dsp file
  181. std::string realDSP = fname;
  182. fname += ".cmake";
  183. cmsys::ofstream fout(fname.c_str());
  184. if(!fout)
  185. {
  186. cmSystemTools::Error("Error Writing ", fname.c_str());
  187. cmSystemTools::ReportLastSystemError("");
  188. }
  189. this->WriteDSPFile(fout,pname.c_str(),target);
  190. fout.close();
  191. // if the dsp file has changed, then write it.
  192. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  193. }
  194. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmTarget& tgt)
  195. {
  196. std::string dspname = GetVS6TargetName(tgt.GetName());
  197. dspname += ".dsp.cmake";
  198. cmCustomCommandLine commandLine;
  199. commandLine.push_back(cmSystemTools::GetCMakeCommand());
  200. std::string makefileIn = this->Makefile->GetCurrentSourceDirectory();
  201. makefileIn += "/";
  202. makefileIn += "CMakeLists.txt";
  203. if(!cmSystemTools::FileExists(makefileIn.c_str()))
  204. {
  205. return;
  206. }
  207. std::string comment = "Building Custom Rule ";
  208. comment += makefileIn;
  209. std::string args;
  210. args = "-H";
  211. args += this->Makefile->GetHomeDirectory();
  212. commandLine.push_back(args);
  213. args = "-B";
  214. args += this->Makefile->GetHomeOutputDirectory();
  215. commandLine.push_back(args);
  216. std::vector<std::string> const& listFiles = this->Makefile->GetListFiles();
  217. cmCustomCommandLines commandLines;
  218. commandLines.push_back(commandLine);
  219. const char* no_working_directory = 0;
  220. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  221. makefileIn.c_str(), commandLines,
  222. comment.c_str(),
  223. no_working_directory, true);
  224. if(this->Makefile->GetSource(makefileIn.c_str()))
  225. {
  226. tgt.AddSource(makefileIn);
  227. }
  228. else
  229. {
  230. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  231. }
  232. }
  233. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  234. const std::string& libName,
  235. cmTarget &target)
  236. {
  237. // For utility targets need custom command since pre- and post-
  238. // build does not do anything in Visual Studio 6. In order for the
  239. // rules to run in the correct order as custom commands, we need
  240. // special care for dependencies. The first rule must depend on all
  241. // the dependencies of all the rules. The later rules must each
  242. // depend only on the previous rule.
  243. if ((target.GetType() == cmTarget::UTILITY ||
  244. target.GetType() == cmTarget::GLOBAL_TARGET) &&
  245. (!target.GetPreBuildCommands().empty() ||
  246. !target.GetPostBuildCommands().empty()))
  247. {
  248. // Accumulate the dependencies of all the commands.
  249. std::vector<std::string> depends;
  250. for (std::vector<cmCustomCommand>::const_iterator cr =
  251. target.GetPreBuildCommands().begin();
  252. cr != target.GetPreBuildCommands().end(); ++cr)
  253. {
  254. depends.insert(depends.end(),
  255. cr->GetDepends().begin(), cr->GetDepends().end());
  256. }
  257. for (std::vector<cmCustomCommand>::const_iterator cr =
  258. target.GetPostBuildCommands().begin();
  259. cr != target.GetPostBuildCommands().end(); ++cr)
  260. {
  261. depends.insert(depends.end(),
  262. cr->GetDepends().begin(), cr->GetDepends().end());
  263. }
  264. // Add the pre- and post-build commands in order.
  265. int count = 1;
  266. for (std::vector<cmCustomCommand>::const_iterator cr =
  267. target.GetPreBuildCommands().begin();
  268. cr != target.GetPreBuildCommands().end(); ++cr)
  269. {
  270. this->AddUtilityCommandHack(target, count++, depends, *cr);
  271. }
  272. for (std::vector<cmCustomCommand>::const_iterator cr =
  273. target.GetPostBuildCommands().begin();
  274. cr != target.GetPostBuildCommands().end(); ++cr)
  275. {
  276. this->AddUtilityCommandHack(target, count++, depends, *cr);
  277. }
  278. }
  279. // We may be modifying the source groups temporarily, so make a copy.
  280. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  281. cmGeneratorTarget* gt =
  282. this->GlobalGenerator->GetGeneratorTarget(&target);
  283. // get the classes from the source lists then add them to the groups
  284. std::vector<cmSourceFile*> classes;
  285. if (!gt->GetConfigCommonSourceFiles(classes))
  286. {
  287. return;
  288. }
  289. // now all of the source files have been properly assigned to the target
  290. // now stick them into source groups using the reg expressions
  291. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  292. i != classes.end(); i++)
  293. {
  294. if (!(*i)->GetObjectLibrary().empty())
  295. {
  296. continue;
  297. }
  298. // Add the file to the list of sources.
  299. std::string source = (*i)->GetFullPath();
  300. cmSourceGroup* sourceGroup =
  301. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  302. sourceGroup->AssignSource(*i);
  303. // while we are at it, if it is a .rule file then for visual studio 6 we
  304. // must generate it
  305. if ((*i)->GetPropertyAsBool("__CMAKE_RULE"))
  306. {
  307. if(!cmSystemTools::FileExists(source.c_str()))
  308. {
  309. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  310. // Make sure the path exists for the file
  311. std::string path = cmSystemTools::GetFilenamePath(source);
  312. cmSystemTools::MakeDirectory(path.c_str());
  313. #if defined(_WIN32) || defined(__CYGWIN__)
  314. cmsys::ofstream sourceFout(source.c_str(),
  315. std::ios::binary | std::ios::out
  316. | std::ios::trunc);
  317. #else
  318. cmsys::ofstream sourceFout(source.c_str(),
  319. std::ios::out | std::ios::trunc);
  320. #endif
  321. if(sourceFout)
  322. {
  323. sourceFout.write("# generated from CMake",22);
  324. sourceFout.flush();
  325. sourceFout.close();
  326. }
  327. }
  328. }
  329. }
  330. // Write the DSP file's header.
  331. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  332. // Loop through every source group.
  333. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  334. sg != sourceGroups.end(); ++sg)
  335. {
  336. this->WriteGroup(&(*sg), target, fout, libName);
  337. }
  338. // Write the DSP file's footer.
  339. this->WriteDSPFooter(fout);
  340. }
  341. void cmLocalVisualStudio6Generator
  342. ::WriteGroup(const cmSourceGroup *sg, cmTarget& target,
  343. std::ostream &fout, const std::string& libName)
  344. {
  345. cmGeneratorTarget* gt =
  346. this->GlobalGenerator->GetGeneratorTarget(&target);
  347. const std::vector<const cmSourceFile *> &sourceFiles =
  348. sg->GetSourceFiles();
  349. // If the group is empty, don't write it at all.
  350. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  351. {
  352. return;
  353. }
  354. // If the group has a name, write the header.
  355. std::string name = sg->GetName();
  356. if(name != "")
  357. {
  358. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  359. }
  360. // Loop through each source in the source group.
  361. for(std::vector<const cmSourceFile *>::const_iterator sf =
  362. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  363. {
  364. if (!(*sf)->GetObjectLibrary().empty())
  365. {
  366. continue;
  367. }
  368. std::string source = (*sf)->GetFullPath();
  369. const cmCustomCommand *command =
  370. (*sf)->GetCustomCommand();
  371. std::string compileFlags;
  372. std::vector<std::string> depends;
  373. std::string objectNameDir;
  374. if(gt->HasExplicitObjectName(*sf))
  375. {
  376. objectNameDir = cmSystemTools::GetFilenamePath(gt->GetObjectName(*sf));
  377. }
  378. // Add per-source file flags.
  379. if(const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS"))
  380. {
  381. compileFlags += cflags;
  382. }
  383. const std::string& lang = this->GetSourceFileLanguage(*(*sf));
  384. if(lang == "CXX")
  385. {
  386. // force a C++ file type
  387. compileFlags += " /TP ";
  388. }
  389. else if(lang == "C")
  390. {
  391. // force to c file type
  392. compileFlags += " /TC ";
  393. }
  394. // Add per-source and per-configuration preprocessor definitions.
  395. std::map<std::string, std::string> cdmap;
  396. {
  397. std::set<std::string> targetCompileDefinitions;
  398. this->AppendDefines(targetCompileDefinitions,
  399. (*sf)->GetProperty("COMPILE_DEFINITIONS"));
  400. this->JoinDefines(targetCompileDefinitions, compileFlags, lang);
  401. }
  402. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_DEBUG"))
  403. {
  404. std::set<std::string> debugCompileDefinitions;
  405. this->AppendDefines(debugCompileDefinitions, cdefs);
  406. this->JoinDefines(debugCompileDefinitions, cdmap["DEBUG"], lang);
  407. }
  408. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_RELEASE"))
  409. {
  410. std::set<std::string> releaseCompileDefinitions;
  411. this->AppendDefines(releaseCompileDefinitions, cdefs);
  412. this->JoinDefines(releaseCompileDefinitions, cdmap["RELEASE"], lang);
  413. }
  414. if(const char* cdefs =
  415. (*sf)->GetProperty("COMPILE_DEFINITIONS_MINSIZEREL"))
  416. {
  417. std::set<std::string> minsizerelCompileDefinitions;
  418. this->AppendDefines(minsizerelCompileDefinitions, cdefs);
  419. this->JoinDefines(minsizerelCompileDefinitions, cdmap["MINSIZEREL"],
  420. lang);
  421. }
  422. if(const char* cdefs =
  423. (*sf)->GetProperty("COMPILE_DEFINITIONS_RELWITHDEBINFO"))
  424. {
  425. std::set<std::string> relwithdebinfoCompileDefinitions;
  426. this->AppendDefines(relwithdebinfoCompileDefinitions, cdefs);
  427. this->JoinDefines(relwithdebinfoCompileDefinitions,
  428. cdmap["RELWITHDEBINFO"], lang);
  429. }
  430. bool excludedFromBuild =
  431. (!lang.empty() && (*sf)->GetPropertyAsBool("HEADER_FILE_ONLY"));
  432. // Check for extra object-file dependencies.
  433. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  434. if(dependsValue)
  435. {
  436. cmSystemTools::ExpandListArgument(dependsValue, depends);
  437. }
  438. if (GetVS6TargetName(source) != libName ||
  439. target.GetType() == cmTarget::UTILITY ||
  440. target.GetType() == cmTarget::GLOBAL_TARGET)
  441. {
  442. fout << "# Begin Source File\n\n";
  443. // Tell MS-Dev what the source is. If the compiler knows how to
  444. // build it, then it will.
  445. fout << "SOURCE=" <<
  446. this->ConvertToOutputFormat(source.c_str(), SHELL) << "\n\n";
  447. if(!depends.empty())
  448. {
  449. // Write out the dependencies for the rule.
  450. fout << "USERDEP__HACK=";
  451. for(std::vector<std::string>::const_iterator d = depends.begin();
  452. d != depends.end(); ++d)
  453. {
  454. fout << "\\\n\t" <<
  455. this->ConvertToOutputFormat(d->c_str(), SHELL);
  456. }
  457. fout << "\n";
  458. }
  459. if (command)
  460. {
  461. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  462. this->WriteCustomRule(fout, source.c_str(), *command, flags);
  463. }
  464. else if(!compileFlags.empty() || !objectNameDir.empty() ||
  465. excludedFromBuild || !cdmap.empty())
  466. {
  467. for(std::vector<std::string>::iterator i
  468. = this->Configurations.begin();
  469. i != this->Configurations.end(); ++i)
  470. {
  471. // Strip the subdirectory name out of the configuration name.
  472. std::string config = this->GetConfigName(*i);
  473. if (i == this->Configurations.begin())
  474. {
  475. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  476. }
  477. else
  478. {
  479. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  480. }
  481. if(excludedFromBuild)
  482. {
  483. fout << "# PROP Exclude_From_Build 1\n";
  484. }
  485. if(!compileFlags.empty())
  486. {
  487. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  488. }
  489. std::map<std::string, std::string>::iterator cdi =
  490. cdmap.find(cmSystemTools::UpperCase(config));
  491. if(cdi != cdmap.end() && !cdi->second.empty())
  492. {
  493. fout << "\n# ADD CPP " << cdi->second << "\n\n";
  494. }
  495. if(!objectNameDir.empty())
  496. {
  497. // Setup an alternate object file directory.
  498. fout << "\n# PROP Intermediate_Dir \""
  499. << config << "/" << objectNameDir << "\"\n\n";
  500. }
  501. }
  502. fout << "!ENDIF\n\n";
  503. }
  504. fout << "# End Source File\n";
  505. }
  506. }
  507. std::vector<cmSourceGroup> const& children = sg->GetGroupChildren();
  508. for(unsigned int i=0;i<children.size();++i)
  509. {
  510. this->WriteGroup(&children[i], target, fout, libName);
  511. }
  512. // If the group has a name, write the footer.
  513. if(name != "")
  514. {
  515. this->WriteDSPEndGroup(fout);
  516. }
  517. }
  518. void
  519. cmLocalVisualStudio6Generator
  520. ::AddUtilityCommandHack(cmTarget& target, int count,
  521. std::vector<std::string>& depends,
  522. const cmCustomCommand& origCommand)
  523. {
  524. // Create a fake output that forces the rule to run.
  525. char* output = new char[(strlen(this->Makefile->GetCurrentBinaryDirectory())
  526. + target.GetName().size() + 30)];
  527. sprintf(output,"%s/%s_force_%i", this->Makefile->GetCurrentBinaryDirectory(),
  528. target.GetName().c_str(), count);
  529. const char* comment = origCommand.GetComment();
  530. if(!comment && origCommand.GetOutputs().empty())
  531. {
  532. comment = "<hack>";
  533. }
  534. // Add the rule with the given dependencies and commands.
  535. std::string no_main_dependency = "";
  536. if(cmSourceFile* outsf =
  537. this->Makefile->AddCustomCommandToOutput(
  538. output, depends, no_main_dependency,
  539. origCommand.GetCommandLines(), comment,
  540. origCommand.GetWorkingDirectory().c_str()))
  541. {
  542. target.AddSource(outsf->GetFullPath());
  543. }
  544. // Replace the dependencies with the output of this rule so that the
  545. // next rule added will run after this one.
  546. depends.clear();
  547. depends.push_back(output);
  548. // Free the fake output name.
  549. delete [] output;
  550. }
  551. void
  552. cmLocalVisualStudio6Generator
  553. ::WriteCustomRule(std::ostream& fout,
  554. const char* source,
  555. const cmCustomCommand& command,
  556. const char* flags)
  557. {
  558. // Write the rule for each configuration.
  559. std::vector<std::string>::iterator i;
  560. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  561. {
  562. std::string config = this->GetConfigName(*i);
  563. cmCustomCommandGenerator ccg(command, config, this);
  564. std::string comment =
  565. this->ConstructComment(ccg, "Building Custom Rule $(InputPath)");
  566. if(comment == "<hack>")
  567. {
  568. comment = "";
  569. }
  570. std::string script =
  571. this->ConstructScript(ccg, "\\\n\t");
  572. if (i == this->Configurations.begin())
  573. {
  574. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  575. }
  576. else
  577. {
  578. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  579. }
  580. if(flags)
  581. {
  582. fout << "\n# ADD CPP " << flags << "\n\n";
  583. }
  584. // Write out the dependencies for the rule.
  585. fout << "USERDEP__HACK=";
  586. for(std::vector<std::string>::const_iterator d =
  587. ccg.GetDepends().begin();
  588. d != ccg.GetDepends().end();
  589. ++d)
  590. {
  591. // Lookup the real name of the dependency in case it is a CMake target.
  592. std::string dep;
  593. if(this->GetRealDependency(d->c_str(), config.c_str(), dep))
  594. {
  595. fout << "\\\n\t" <<
  596. this->ConvertToOutputFormat(dep.c_str(), SHELL);
  597. }
  598. }
  599. fout << "\n";
  600. fout << "# PROP Ignore_Default_Tool 1\n";
  601. fout << "# Begin Custom Build -";
  602. if(!comment.empty())
  603. {
  604. fout << " " << comment.c_str();
  605. }
  606. fout << "\n\n";
  607. if(ccg.GetOutputs().empty())
  608. {
  609. fout << source
  610. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  611. fout << script.c_str() << "\n\n";
  612. }
  613. else
  614. {
  615. for(std::vector<std::string>::const_iterator o =
  616. ccg.GetOutputs().begin();
  617. o != ccg.GetOutputs().end();
  618. ++o)
  619. {
  620. // Write a rule for every output generated by this command.
  621. fout << this->ConvertToOutputFormat(o->c_str(), SHELL)
  622. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  623. fout << script.c_str() << "\n\n";
  624. }
  625. }
  626. fout << "# End Custom Build\n\n";
  627. }
  628. fout << "!ENDIF\n\n";
  629. }
  630. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  631. const char* group,
  632. const char* filter)
  633. {
  634. fout << "# Begin Group \"" << group << "\"\n"
  635. "# PROP Default_Filter \"" << filter << "\"\n";
  636. }
  637. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  638. {
  639. fout << "# End Group\n";
  640. }
  641. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  642. const std::string& libName,
  643. cmTarget& target)
  644. {
  645. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  646. const char *def=
  647. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  648. if( def)
  649. {
  650. root = def;
  651. }
  652. else
  653. {
  654. root += "/Templates";
  655. }
  656. switch(b)
  657. {
  658. case WIN32_EXECUTABLE:
  659. break;
  660. case STATIC_LIBRARY:
  661. this->DSPHeaderTemplate = root;
  662. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  663. this->DSPFooterTemplate = root;
  664. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  665. break;
  666. case DLL:
  667. this->DSPHeaderTemplate = root;
  668. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  669. this->DSPFooterTemplate = root;
  670. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  671. break;
  672. case EXECUTABLE:
  673. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  674. {
  675. this->DSPHeaderTemplate = root;
  676. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  677. this->DSPFooterTemplate = root;
  678. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  679. }
  680. else
  681. {
  682. this->DSPHeaderTemplate = root;
  683. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  684. this->DSPFooterTemplate = root;
  685. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  686. }
  687. break;
  688. case UTILITY:
  689. this->DSPHeaderTemplate = root;
  690. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  691. this->DSPFooterTemplate = root;
  692. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  693. break;
  694. }
  695. // once the build type is set, determine what configurations are
  696. // possible
  697. cmsys::ifstream fin(this->DSPHeaderTemplate.c_str());
  698. cmsys::RegularExpression reg("# Name ");
  699. if(!fin)
  700. {
  701. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  702. }
  703. // reset this->Configurations
  704. this->Configurations.erase(this->Configurations.begin(),
  705. this->Configurations.end());
  706. // now add all the configurations possible
  707. std::string vs6name = GetVS6TargetName(libName);
  708. std::string line;
  709. while(cmSystemTools::GetLineFromStream(fin, line))
  710. {
  711. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME", vs6name.c_str());
  712. if (reg.find(line))
  713. {
  714. this->Configurations.push_back(line.substr(reg.end()));
  715. }
  716. }
  717. }
  718. //----------------------------------------------------------------------------
  719. cmsys::auto_ptr<cmCustomCommand>
  720. cmLocalVisualStudio6Generator::MaybeCreateOutputDir(cmTarget& target,
  721. const std::string& config)
  722. {
  723. cmsys::auto_ptr<cmCustomCommand> pcc;
  724. // VS6 forgets to create the output directory for archives if it
  725. // differs from the intermediate directory.
  726. if(target.GetType() != cmTarget::STATIC_LIBRARY) { return pcc; }
  727. std::string outDir = target.GetDirectory(config, false);
  728. // Add a pre-link event to create the directory.
  729. cmCustomCommandLine command;
  730. command.push_back(cmSystemTools::GetCMakeCommand());
  731. command.push_back("-E");
  732. command.push_back("make_directory");
  733. command.push_back(outDir);
  734. std::vector<std::string> no_output;
  735. std::vector<std::string> no_byproducts;
  736. std::vector<std::string> no_depends;
  737. cmCustomCommandLines commands;
  738. commands.push_back(command);
  739. pcc.reset(new cmCustomCommand(0, no_output, no_byproducts,
  740. no_depends, commands, 0, 0));
  741. pcc->SetEscapeOldStyle(false);
  742. pcc->SetEscapeAllowMakeVars(true);
  743. return pcc;
  744. }
  745. // look for custom rules on a target and collect them together
  746. std::string
  747. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  748. const std::string& configName,
  749. const std::string& /* libName */)
  750. {
  751. if (target.GetType() >= cmTarget::UTILITY )
  752. {
  753. return "";
  754. }
  755. std::string customRuleCode = "# Begin Special Build Tool\n";
  756. EventWriter event(this, configName, customRuleCode);
  757. // Write the pre-build and pre-link together (VS6 does not support both).
  758. event.Start("PreLink");
  759. event.Write(target.GetPreBuildCommands());
  760. event.Write(target.GetPreLinkCommands());
  761. cmsys::auto_ptr<cmCustomCommand> pcc(
  762. this->MaybeCreateImplibDir(target, configName, false));
  763. if(pcc.get())
  764. {
  765. event.Write(*pcc);
  766. }
  767. pcc = this->MaybeCreateOutputDir(target, configName);
  768. if(pcc.get())
  769. {
  770. event.Write(*pcc);
  771. }
  772. event.Finish();
  773. // Write the post-build rules.
  774. event.Start("PostBuild");
  775. event.Write(target.GetPostBuildCommands());
  776. event.Finish();
  777. customRuleCode += "# End Special Build Tool\n";
  778. return customRuleCode;
  779. }
  780. inline std::string removeQuotes(const std::string& s)
  781. {
  782. if(s[0] == '\"' && s[s.size()-1] == '\"')
  783. {
  784. return s.substr(1, s.size()-2);
  785. }
  786. return s;
  787. }
  788. std::string
  789. cmLocalVisualStudio6Generator::GetTargetIncludeOptions(cmTarget &target,
  790. const std::string& config)
  791. {
  792. std::string includeOptions;
  793. // Setup /I and /LIBPATH options for the resulting DSP file. VS 6
  794. // truncates long include paths so make it as short as possible if
  795. // the length threatens this problem.
  796. unsigned int maxIncludeLength = 3000;
  797. bool useShortPath = false;
  798. cmGeneratorTarget* gt =
  799. this->GlobalGenerator->GetGeneratorTarget(&target);
  800. for(int j=0; j < 2; ++j)
  801. {
  802. std::vector<std::string> includes;
  803. this->GetIncludeDirectories(includes, gt, "C", config);
  804. std::vector<std::string>::iterator i;
  805. for(i = includes.begin(); i != includes.end(); ++i)
  806. {
  807. std::string tmp =
  808. this->ConvertToOutputFormat(i->c_str(), SHELL);
  809. if(useShortPath)
  810. {
  811. cmSystemTools::GetShortPath(tmp.c_str(), tmp);
  812. }
  813. includeOptions += " /I ";
  814. // quote if not already quoted
  815. if (tmp[0] != '"')
  816. {
  817. includeOptions += "\"";
  818. includeOptions += tmp;
  819. includeOptions += "\"";
  820. }
  821. else
  822. {
  823. includeOptions += tmp;
  824. }
  825. }
  826. if(j == 0 && includeOptions.size() > maxIncludeLength)
  827. {
  828. includeOptions = "";
  829. useShortPath = true;
  830. }
  831. else
  832. {
  833. break;
  834. }
  835. }
  836. return includeOptions;
  837. }
  838. // Code in blocks surrounded by a test for this definition is needed
  839. // only for compatibility with user project's replacement DSP
  840. // templates. The CMake templates no longer use them.
  841. #define CM_USE_OLD_VS6
  842. void cmLocalVisualStudio6Generator
  843. ::WriteDSPHeader(std::ostream& fout,
  844. const std::string& libName, cmTarget &target,
  845. std::vector<cmSourceGroup> &)
  846. {
  847. bool targetBuilds = (target.GetType() >= cmTarget::EXECUTABLE &&
  848. target.GetType() <= cmTarget::MODULE_LIBRARY);
  849. #ifdef CM_USE_OLD_VS6
  850. // Lookup the library and executable output directories.
  851. std::string libPath;
  852. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  853. {
  854. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  855. }
  856. std::string exePath;
  857. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  858. {
  859. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  860. }
  861. // Make sure there are trailing slashes.
  862. if(!libPath.empty())
  863. {
  864. if(libPath[libPath.size()-1] != '/')
  865. {
  866. libPath += "/";
  867. }
  868. }
  869. if(!exePath.empty())
  870. {
  871. if(exePath[exePath.size()-1] != '/')
  872. {
  873. exePath += "/";
  874. }
  875. }
  876. std::set<std::string> pathEmitted;
  877. // determine the link directories
  878. std::string libOptions;
  879. std::string libDebugOptions;
  880. std::string libOptimizedOptions;
  881. std::string libMultiLineOptions;
  882. std::string libMultiLineOptionsForDebug;
  883. std::string libMultiLineDebugOptions;
  884. std::string libMultiLineOptimizedOptions;
  885. if(!libPath.empty())
  886. {
  887. std::string lpath =
  888. this->ConvertToOutputFormat(libPath.c_str(), SHELL);
  889. if(lpath.empty())
  890. {
  891. lpath = ".";
  892. }
  893. std::string lpathIntDir = libPath + "$(INTDIR)";
  894. lpathIntDir =
  895. this->ConvertToOutputFormat(lpathIntDir.c_str(), SHELL);
  896. if(pathEmitted.insert(lpath).second)
  897. {
  898. libOptions += " /LIBPATH:";
  899. libOptions += lpathIntDir;
  900. libOptions += " ";
  901. libOptions += " /LIBPATH:";
  902. libOptions += lpath;
  903. libOptions += " ";
  904. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  905. libMultiLineOptions += lpathIntDir;
  906. libMultiLineOptions += " ";
  907. libMultiLineOptions += " /LIBPATH:";
  908. libMultiLineOptions += lpath;
  909. libMultiLineOptions += " \n";
  910. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  911. libMultiLineOptionsForDebug += lpathIntDir;
  912. libMultiLineOptionsForDebug += " ";
  913. libMultiLineOptionsForDebug += " /LIBPATH:";
  914. libMultiLineOptionsForDebug += lpath;
  915. libMultiLineOptionsForDebug += " \n";
  916. }
  917. }
  918. if(!exePath.empty())
  919. {
  920. std::string lpath =
  921. this->ConvertToOutputFormat(exePath.c_str(), SHELL);
  922. if(lpath.empty())
  923. {
  924. lpath = ".";
  925. }
  926. std::string lpathIntDir = exePath + "$(INTDIR)";
  927. lpathIntDir =
  928. this->ConvertToOutputFormat(lpathIntDir.c_str(), SHELL);
  929. if(pathEmitted.insert(lpath).second)
  930. {
  931. libOptions += " /LIBPATH:";
  932. libOptions += lpathIntDir;
  933. libOptions += " ";
  934. libOptions += " /LIBPATH:";
  935. libOptions += lpath;
  936. libOptions += " ";
  937. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  938. libMultiLineOptions += lpathIntDir;
  939. libMultiLineOptions += " ";
  940. libMultiLineOptions += " /LIBPATH:";
  941. libMultiLineOptions += lpath;
  942. libMultiLineOptions += " \n";
  943. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  944. libMultiLineOptionsForDebug += lpathIntDir;
  945. libMultiLineOptionsForDebug += " ";
  946. libMultiLineOptionsForDebug += " /LIBPATH:";
  947. libMultiLineOptionsForDebug += lpath;
  948. libMultiLineOptionsForDebug += " \n";
  949. }
  950. }
  951. std::vector<std::string>::const_iterator i;
  952. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  953. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  954. {
  955. std::string path = *i;
  956. if(path[path.size()-1] != '/')
  957. {
  958. path += "/";
  959. }
  960. std::string lpath =
  961. this->ConvertToOutputFormat(path.c_str(), SHELL);
  962. if(lpath.empty())
  963. {
  964. lpath = ".";
  965. }
  966. std::string lpathIntDir = path + "$(INTDIR)";
  967. lpathIntDir =
  968. this->ConvertToOutputFormat(lpathIntDir.c_str(), SHELL);
  969. if(pathEmitted.insert(lpath).second)
  970. {
  971. libOptions += " /LIBPATH:";
  972. libOptions += lpathIntDir;
  973. libOptions += " ";
  974. libOptions += " /LIBPATH:";
  975. libOptions += lpath;
  976. libOptions += " ";
  977. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  978. libMultiLineOptions += lpathIntDir;
  979. libMultiLineOptions += " ";
  980. libMultiLineOptions += " /LIBPATH:";
  981. libMultiLineOptions += lpath;
  982. libMultiLineOptions += " \n";
  983. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  984. libMultiLineOptionsForDebug += lpathIntDir;
  985. libMultiLineOptionsForDebug += " ";
  986. libMultiLineOptionsForDebug += " /LIBPATH:";
  987. libMultiLineOptionsForDebug += lpath;
  988. libMultiLineOptionsForDebug += " \n";
  989. }
  990. }
  991. // find link libraries
  992. const cmTarget::LinkLibraryVectorType& libs =
  993. target.GetLinkLibrariesForVS6();
  994. cmTarget::LinkLibraryVectorType::const_iterator j;
  995. for(j = libs.begin(); j != libs.end(); ++j)
  996. {
  997. // add libraries to executables and dlls (but never include
  998. // a library in a library, bad recursion)
  999. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  1000. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  1001. && target.GetType() != cmTarget::STATIC_LIBRARY
  1002. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  1003. (target.GetType()==cmTarget::SHARED_LIBRARY
  1004. && libName != GetVS6TargetName(j->first)) ||
  1005. (target.GetType()==cmTarget::MODULE_LIBRARY
  1006. && libName != GetVS6TargetName(j->first)))
  1007. {
  1008. // Compute the proper name to use to link this library.
  1009. std::string lib;
  1010. std::string libDebug;
  1011. cmTarget* tgt = this->GlobalGenerator->FindTarget(j->first.c_str());
  1012. if(tgt)
  1013. {
  1014. cmGeneratorTarget* gt =
  1015. this->GlobalGenerator->GetGeneratorTarget(tgt);
  1016. lib = cmSystemTools::GetFilenameWithoutExtension
  1017. (gt->GetFullName().c_str());
  1018. libDebug = cmSystemTools::GetFilenameWithoutExtension
  1019. (gt->GetFullName("Debug").c_str());
  1020. lib += ".lib";
  1021. libDebug += ".lib";
  1022. }
  1023. else
  1024. {
  1025. lib = j->first.c_str();
  1026. libDebug = j->first.c_str();
  1027. if(j->first.find(".lib") == std::string::npos)
  1028. {
  1029. lib += ".lib";
  1030. libDebug += ".lib";
  1031. }
  1032. }
  1033. lib = this->ConvertToOutputFormat(lib.c_str(), SHELL);
  1034. libDebug =
  1035. this->ConvertToOutputFormat(libDebug.c_str(), SHELL);
  1036. if (j->second == cmTarget::GENERAL)
  1037. {
  1038. libOptions += " ";
  1039. libOptions += lib;
  1040. libMultiLineOptions += "# ADD LINK32 ";
  1041. libMultiLineOptions += lib;
  1042. libMultiLineOptions += "\n";
  1043. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1044. libMultiLineOptionsForDebug += libDebug;
  1045. libMultiLineOptionsForDebug += "\n";
  1046. }
  1047. if (j->second == cmTarget::DEBUG)
  1048. {
  1049. libDebugOptions += " ";
  1050. libDebugOptions += lib;
  1051. libMultiLineDebugOptions += "# ADD LINK32 ";
  1052. libMultiLineDebugOptions += libDebug;
  1053. libMultiLineDebugOptions += "\n";
  1054. }
  1055. if (j->second == cmTarget::OPTIMIZED)
  1056. {
  1057. libOptimizedOptions += " ";
  1058. libOptimizedOptions += lib;
  1059. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  1060. libMultiLineOptimizedOptions += lib;
  1061. libMultiLineOptimizedOptions += "\n";
  1062. }
  1063. }
  1064. }
  1065. #endif
  1066. // Get include options for this target.
  1067. std::string includeOptionsDebug = this->GetTargetIncludeOptions(target,
  1068. "DEBUG");
  1069. std::string includeOptionsRelease = this->GetTargetIncludeOptions(target,
  1070. "RELEASE");
  1071. std::string includeOptionsRelWithDebInfo = this->GetTargetIncludeOptions(
  1072. target,
  1073. "RELWITHDEBINFO");
  1074. std::string includeOptionsMinSizeRel = this->GetTargetIncludeOptions(target,
  1075. "MINSIZEREL");
  1076. // Get extra linker options for this target type.
  1077. std::string extraLinkOptions;
  1078. std::string extraLinkOptionsDebug;
  1079. std::string extraLinkOptionsRelease;
  1080. std::string extraLinkOptionsMinSizeRel;
  1081. std::string extraLinkOptionsRelWithDebInfo;
  1082. if(target.GetType() == cmTarget::EXECUTABLE)
  1083. {
  1084. extraLinkOptions = this->Makefile->
  1085. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1086. extraLinkOptionsDebug = this->Makefile->
  1087. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_DEBUG");
  1088. extraLinkOptionsRelease = this->Makefile->
  1089. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_RELEASE");
  1090. extraLinkOptionsMinSizeRel = this->Makefile->
  1091. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_MINSIZEREL");
  1092. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1093. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO");
  1094. }
  1095. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1096. {
  1097. extraLinkOptions = this->Makefile->
  1098. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1099. extraLinkOptionsDebug = this->Makefile->
  1100. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_DEBUG");
  1101. extraLinkOptionsRelease = this->Makefile->
  1102. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_RELEASE");
  1103. extraLinkOptionsMinSizeRel = this->Makefile->
  1104. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL");
  1105. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1106. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO");
  1107. }
  1108. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1109. {
  1110. extraLinkOptions = this->Makefile->
  1111. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1112. extraLinkOptionsDebug = this->Makefile->
  1113. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_DEBUG");
  1114. extraLinkOptionsRelease = this->Makefile->
  1115. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_RELEASE");
  1116. extraLinkOptionsMinSizeRel = this->Makefile->
  1117. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL");
  1118. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1119. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO");
  1120. }
  1121. // Get extra linker options for this target.
  1122. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1123. {
  1124. extraLinkOptions += " ";
  1125. extraLinkOptions += targetLinkFlags;
  1126. }
  1127. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1128. {
  1129. extraLinkOptionsDebug += " ";
  1130. extraLinkOptionsDebug += targetLinkFlags;
  1131. }
  1132. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_RELEASE"))
  1133. {
  1134. extraLinkOptionsRelease += " ";
  1135. extraLinkOptionsRelease += targetLinkFlags;
  1136. }
  1137. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1138. {
  1139. extraLinkOptionsMinSizeRel += " ";
  1140. extraLinkOptionsMinSizeRel += targetLinkFlags;
  1141. }
  1142. if(const char* targetLinkFlags =
  1143. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1144. {
  1145. extraLinkOptionsRelWithDebInfo += " ";
  1146. extraLinkOptionsRelWithDebInfo += targetLinkFlags;
  1147. }
  1148. cmGeneratorTarget* gt =
  1149. this->GlobalGenerator->GetGeneratorTarget(&target);
  1150. // Get standard libraries for this language.
  1151. if(targetBuilds)
  1152. {
  1153. // Get the language to use for linking.
  1154. std::vector<std::string> configs;
  1155. target.GetMakefile()->GetConfigurations(configs);
  1156. std::vector<std::string>::const_iterator it = configs.begin();
  1157. const std::string& linkLanguage = gt->GetLinkerLanguage(*it);
  1158. for ( ; it != configs.end(); ++it)
  1159. {
  1160. const std::string& configLinkLanguage = gt->GetLinkerLanguage(*it);
  1161. if (configLinkLanguage != linkLanguage)
  1162. {
  1163. cmSystemTools::Error
  1164. ("Linker language must not vary by configuration for target: ",
  1165. target.GetName().c_str());
  1166. }
  1167. }
  1168. if(linkLanguage.empty())
  1169. {
  1170. cmSystemTools::Error
  1171. ("CMake can not determine linker language for target: ",
  1172. target.GetName().c_str());
  1173. return;
  1174. }
  1175. // Compute the variable name to lookup standard libraries for this
  1176. // language.
  1177. std::string standardLibsVar = "CMAKE_";
  1178. standardLibsVar += linkLanguage;
  1179. standardLibsVar += "_STANDARD_LIBRARIES";
  1180. // Add standard libraries.
  1181. if(const char* stdLibs =
  1182. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1183. {
  1184. extraLinkOptions += " ";
  1185. extraLinkOptions += stdLibs;
  1186. }
  1187. }
  1188. // Compute version number information.
  1189. std::string targetVersionFlag;
  1190. if(target.GetType() == cmTarget::EXECUTABLE ||
  1191. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1192. target.GetType() == cmTarget::MODULE_LIBRARY)
  1193. {
  1194. int major;
  1195. int minor;
  1196. target.GetTargetVersion(major, minor);
  1197. std::ostringstream targetVersionStream;
  1198. targetVersionStream << "/version:" << major << "." << minor;
  1199. targetVersionFlag = targetVersionStream.str();
  1200. }
  1201. // Compute the real name of the target.
  1202. std::string outputName =
  1203. "(OUTPUT_NAME is for libraries and executables only)";
  1204. std::string outputNameDebug = outputName;
  1205. std::string outputNameRelease = outputName;
  1206. std::string outputNameMinSizeRel = outputName;
  1207. std::string outputNameRelWithDebInfo = outputName;
  1208. if(target.GetType() == cmTarget::EXECUTABLE ||
  1209. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1210. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1211. target.GetType() == cmTarget::MODULE_LIBRARY)
  1212. {
  1213. outputName = gt->GetFullName();
  1214. outputNameDebug = gt->GetFullName("Debug");
  1215. outputNameRelease = gt->GetFullName("Release");
  1216. outputNameMinSizeRel = gt->GetFullName("MinSizeRel");
  1217. outputNameRelWithDebInfo = gt->GetFullName("RelWithDebInfo");
  1218. }
  1219. else if(target.GetType() == cmTarget::OBJECT_LIBRARY)
  1220. {
  1221. outputName = target.GetName();
  1222. outputName += ".lib";
  1223. outputNameDebug = outputName;
  1224. outputNameRelease = outputName;
  1225. outputNameMinSizeRel = outputName;
  1226. outputNameRelWithDebInfo = outputName;
  1227. }
  1228. // Compute the output directory for the target.
  1229. std::string outputDirOld;
  1230. std::string outputDirDebug;
  1231. std::string outputDirRelease;
  1232. std::string outputDirMinSizeRel;
  1233. std::string outputDirRelWithDebInfo;
  1234. if(target.GetType() == cmTarget::EXECUTABLE ||
  1235. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1236. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1237. target.GetType() == cmTarget::MODULE_LIBRARY)
  1238. {
  1239. #ifdef CM_USE_OLD_VS6
  1240. outputDirOld =
  1241. removeQuotes(this->ConvertToOutputFormat
  1242. (target.GetDirectory().c_str(), SHELL));
  1243. #endif
  1244. outputDirDebug =
  1245. removeQuotes(this->ConvertToOutputFormat(
  1246. target.GetDirectory("Debug").c_str(), SHELL));
  1247. outputDirRelease =
  1248. removeQuotes(this->ConvertToOutputFormat(
  1249. target.GetDirectory("Release").c_str(), SHELL));
  1250. outputDirMinSizeRel =
  1251. removeQuotes(this->ConvertToOutputFormat(
  1252. target.GetDirectory("MinSizeRel").c_str(), SHELL));
  1253. outputDirRelWithDebInfo =
  1254. removeQuotes(this->ConvertToOutputFormat(
  1255. target.GetDirectory("RelWithDebInfo").c_str(), SHELL));
  1256. }
  1257. else if(target.GetType() == cmTarget::OBJECT_LIBRARY)
  1258. {
  1259. std::string outputDir = cmake::GetCMakeFilesDirectoryPostSlash();
  1260. outputDirDebug = outputDir + "Debug";
  1261. outputDirRelease = outputDir + "Release";
  1262. outputDirMinSizeRel = outputDir + "MinSizeRel";
  1263. outputDirRelWithDebInfo = outputDir + "RelWithDebInfo";
  1264. }
  1265. // Compute the proper link information for the target.
  1266. std::string optionsDebug;
  1267. std::string optionsRelease;
  1268. std::string optionsMinSizeRel;
  1269. std::string optionsRelWithDebInfo;
  1270. if(target.GetType() == cmTarget::EXECUTABLE ||
  1271. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1272. target.GetType() == cmTarget::MODULE_LIBRARY)
  1273. {
  1274. extraLinkOptionsDebug =
  1275. extraLinkOptions + " " + extraLinkOptionsDebug;
  1276. extraLinkOptionsRelease =
  1277. extraLinkOptions + " " + extraLinkOptionsRelease;
  1278. extraLinkOptionsMinSizeRel =
  1279. extraLinkOptions + " " + extraLinkOptionsMinSizeRel;
  1280. extraLinkOptionsRelWithDebInfo =
  1281. extraLinkOptions + " " + extraLinkOptionsRelWithDebInfo;
  1282. this->ComputeLinkOptions(target, "Debug", extraLinkOptionsDebug,
  1283. optionsDebug);
  1284. this->ComputeLinkOptions(target, "Release", extraLinkOptionsRelease,
  1285. optionsRelease);
  1286. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptionsMinSizeRel,
  1287. optionsMinSizeRel);
  1288. this->ComputeLinkOptions(target, "RelWithDebInfo",
  1289. extraLinkOptionsRelWithDebInfo,
  1290. optionsRelWithDebInfo);
  1291. }
  1292. // Compute the path of the import library.
  1293. std::string targetImplibFlagDebug;
  1294. std::string targetImplibFlagRelease;
  1295. std::string targetImplibFlagMinSizeRel;
  1296. std::string targetImplibFlagRelWithDebInfo;
  1297. if(target.GetType() == cmTarget::SHARED_LIBRARY ||
  1298. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1299. target.GetType() == cmTarget::EXECUTABLE)
  1300. {
  1301. std::string fullPathImpDebug = target.GetDirectory("Debug", true);
  1302. std::string fullPathImpRelease = target.GetDirectory("Release", true);
  1303. std::string fullPathImpMinSizeRel =
  1304. target.GetDirectory("MinSizeRel", true);
  1305. std::string fullPathImpRelWithDebInfo =
  1306. target.GetDirectory("RelWithDebInfo", true);
  1307. fullPathImpDebug += "/";
  1308. fullPathImpRelease += "/";
  1309. fullPathImpMinSizeRel += "/";
  1310. fullPathImpRelWithDebInfo += "/";
  1311. fullPathImpDebug += gt->GetFullName("Debug", true);
  1312. fullPathImpRelease += gt->GetFullName("Release", true);
  1313. fullPathImpMinSizeRel += gt->GetFullName("MinSizeRel", true);
  1314. fullPathImpRelWithDebInfo += gt->GetFullName("RelWithDebInfo", true);
  1315. targetImplibFlagDebug = "/implib:";
  1316. targetImplibFlagRelease = "/implib:";
  1317. targetImplibFlagMinSizeRel = "/implib:";
  1318. targetImplibFlagRelWithDebInfo = "/implib:";
  1319. targetImplibFlagDebug +=
  1320. this->ConvertToOutputFormat(fullPathImpDebug.c_str(), SHELL);
  1321. targetImplibFlagRelease +=
  1322. this->ConvertToOutputFormat(fullPathImpRelease.c_str(), SHELL);
  1323. targetImplibFlagMinSizeRel +=
  1324. this->ConvertToOutputFormat(fullPathImpMinSizeRel.c_str(), SHELL);
  1325. targetImplibFlagRelWithDebInfo +=
  1326. this->ConvertToOutputFormat(fullPathImpRelWithDebInfo.c_str(), SHELL);
  1327. }
  1328. #ifdef CM_USE_OLD_VS6
  1329. // Compute link information for the target.
  1330. if(!extraLinkOptions.empty())
  1331. {
  1332. libOptions += " ";
  1333. libOptions += extraLinkOptions;
  1334. libOptions += " ";
  1335. libMultiLineOptions += "# ADD LINK32 ";
  1336. libMultiLineOptions += extraLinkOptions;
  1337. libMultiLineOptions += " \n";
  1338. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1339. libMultiLineOptionsForDebug += extraLinkOptions;
  1340. libMultiLineOptionsForDebug += " \n";
  1341. }
  1342. #endif
  1343. // are there any custom rules on the target itself
  1344. // only if the target is a lib or exe
  1345. std::string customRuleCodeRelease
  1346. = this->CreateTargetRules(target, "RELEASE", libName);
  1347. std::string customRuleCodeDebug
  1348. = this->CreateTargetRules(target, "DEBUG", libName);
  1349. std::string customRuleCodeMinSizeRel
  1350. = this->CreateTargetRules(target, "MINSIZEREL", libName);
  1351. std::string customRuleCodeRelWithDebInfo
  1352. = this->CreateTargetRules(target, "RELWITHDEBINFO", libName);
  1353. cmsys::ifstream fin(this->DSPHeaderTemplate.c_str());
  1354. if(!fin)
  1355. {
  1356. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1357. }
  1358. std::string staticLibOptions;
  1359. std::string staticLibOptionsDebug;
  1360. std::string staticLibOptionsRelease;
  1361. std::string staticLibOptionsMinSizeRel;
  1362. std::string staticLibOptionsRelWithDebInfo;
  1363. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1364. {
  1365. const char *libflagsGlobal =
  1366. this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS");
  1367. this->AppendFlags(staticLibOptions, libflagsGlobal);
  1368. this->AppendFlags(staticLibOptionsDebug, libflagsGlobal);
  1369. this->AppendFlags(staticLibOptionsRelease, libflagsGlobal);
  1370. this->AppendFlags(staticLibOptionsMinSizeRel, libflagsGlobal);
  1371. this->AppendFlags(staticLibOptionsRelWithDebInfo, libflagsGlobal);
  1372. this->AppendFlags(staticLibOptionsDebug, this->Makefile->
  1373. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_DEBUG"));
  1374. this->AppendFlags(staticLibOptionsRelease, this->Makefile->
  1375. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_RELEASE"));
  1376. this->AppendFlags(staticLibOptionsMinSizeRel, this->Makefile->
  1377. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL"));
  1378. this->AppendFlags(staticLibOptionsRelWithDebInfo, this->Makefile->
  1379. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO"));
  1380. const char *libflags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  1381. this->AppendFlags(staticLibOptions, libflags);
  1382. this->AppendFlags(staticLibOptionsDebug, libflags);
  1383. this->AppendFlags(staticLibOptionsRelease, libflags);
  1384. this->AppendFlags(staticLibOptionsMinSizeRel, libflags);
  1385. this->AppendFlags(staticLibOptionsRelWithDebInfo, libflags);
  1386. this->AppendFlags(staticLibOptionsDebug,
  1387. target.GetProperty("STATIC_LIBRARY_FLAGS_DEBUG"));
  1388. this->AppendFlags(staticLibOptionsRelease,
  1389. target.GetProperty("STATIC_LIBRARY_FLAGS_RELEASE"));
  1390. this->AppendFlags(staticLibOptionsMinSizeRel,
  1391. target.GetProperty("STATIC_LIBRARY_FLAGS_MINSIZEREL"));
  1392. this->AppendFlags(staticLibOptionsRelWithDebInfo,
  1393. target.GetProperty("STATIC_LIBRARY_FLAGS_RELWITHDEBINFO"));
  1394. std::string objects;
  1395. this->OutputObjects(target, "LIB", objects);
  1396. if(!objects.empty())
  1397. {
  1398. objects = "\n" + objects;
  1399. staticLibOptionsDebug += objects;
  1400. staticLibOptionsRelease += objects;
  1401. staticLibOptionsMinSizeRel += objects;
  1402. staticLibOptionsRelWithDebInfo += objects;
  1403. }
  1404. }
  1405. // Add the export symbol definition for shared library objects.
  1406. std::string exportSymbol;
  1407. if(const char* exportMacro = target.GetExportMacro())
  1408. {
  1409. exportSymbol = exportMacro;
  1410. }
  1411. std::string line;
  1412. std::string libnameExports;
  1413. if(!exportSymbol.empty())
  1414. {
  1415. libnameExports = "/D \"";
  1416. libnameExports += exportSymbol;
  1417. libnameExports += "\"";
  1418. }
  1419. while(cmSystemTools::GetLineFromStream(fin, line))
  1420. {
  1421. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1422. if(!mfcFlag)
  1423. {
  1424. mfcFlag = "0";
  1425. }
  1426. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1427. libnameExports.c_str());
  1428. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1429. mfcFlag);
  1430. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1431. target.GetType() == cmTarget::OBJECT_LIBRARY)
  1432. {
  1433. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_DEBUG",
  1434. staticLibOptionsDebug.c_str());
  1435. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_RELEASE",
  1436. staticLibOptionsRelease.c_str());
  1437. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_MINSIZEREL",
  1438. staticLibOptionsMinSizeRel.c_str());
  1439. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_RELWITHDEBINFO",
  1440. staticLibOptionsRelWithDebInfo.c_str());
  1441. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1442. staticLibOptions.c_str());
  1443. }
  1444. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1445. {
  1446. cmSystemTools::ReplaceString(line, "/nologo", "");
  1447. }
  1448. #ifdef CM_USE_OLD_VS6
  1449. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1450. libOptions.c_str());
  1451. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1452. libDebugOptions.c_str());
  1453. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1454. libOptimizedOptions.c_str());
  1455. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1456. libMultiLineOptionsForDebug.c_str());
  1457. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1458. libMultiLineOptions.c_str());
  1459. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1460. libMultiLineDebugOptions.c_str());
  1461. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1462. libMultiLineOptimizedOptions.c_str());
  1463. #endif
  1464. // Substitute the rules for custom command. When specifying just the
  1465. // target name for the command the command can be different for
  1466. // different configs
  1467. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELEASE",
  1468. customRuleCodeRelease.c_str());
  1469. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_DEBUG",
  1470. customRuleCodeDebug.c_str());
  1471. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_MINSIZEREL",
  1472. customRuleCodeMinSizeRel.c_str());
  1473. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELWITHDEBINFO",
  1474. customRuleCodeRelWithDebInfo.c_str());
  1475. // Substitute the real output name into the template.
  1476. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1477. outputNameDebug.c_str());
  1478. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1479. outputNameRelease.c_str());
  1480. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1481. outputNameMinSizeRel.c_str());
  1482. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1483. outputNameRelWithDebInfo.c_str());
  1484. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1485. // Substitute the proper link information into the template.
  1486. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1487. optionsDebug.c_str());
  1488. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1489. optionsRelease.c_str());
  1490. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1491. optionsMinSizeRel.c_str());
  1492. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1493. optionsRelWithDebInfo.c_str());
  1494. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_DEBUG",
  1495. includeOptionsDebug.c_str());
  1496. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_RELEASE",
  1497. includeOptionsRelease.c_str());
  1498. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_MINSIZEREL",
  1499. includeOptionsMinSizeRel.c_str());
  1500. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_RELWITHDEBINFO",
  1501. includeOptionsRelWithDebInfo.c_str());
  1502. cmSystemTools::ReplaceString(line, "TARGET_VERSION_FLAG",
  1503. targetVersionFlag.c_str());
  1504. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_DEBUG",
  1505. targetImplibFlagDebug.c_str());
  1506. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELEASE",
  1507. targetImplibFlagRelease.c_str());
  1508. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_MINSIZEREL",
  1509. targetImplibFlagMinSizeRel.c_str());
  1510. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELWITHDEBINFO",
  1511. targetImplibFlagRelWithDebInfo.c_str());
  1512. std::string vs6name = GetVS6TargetName(libName);
  1513. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME", vs6name.c_str());
  1514. #ifdef CM_USE_OLD_VS6
  1515. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1516. // are already quoted in the template file,
  1517. // we need to remove the quotes here, we still need
  1518. // to convert to output path for unix to win32 conversion
  1519. cmSystemTools::ReplaceString
  1520. (line, "LIBRARY_OUTPUT_PATH",
  1521. removeQuotes(this->ConvertToOutputFormat
  1522. (libPath.c_str(), SHELL)).c_str());
  1523. cmSystemTools::ReplaceString
  1524. (line, "EXECUTABLE_OUTPUT_PATH",
  1525. removeQuotes(this->ConvertToOutputFormat
  1526. (exePath.c_str(), SHELL)).c_str());
  1527. #endif
  1528. if(targetBuilds || target.GetType() == cmTarget::OBJECT_LIBRARY)
  1529. {
  1530. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_DEBUG",
  1531. outputDirDebug.c_str());
  1532. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_RELEASE",
  1533. outputDirRelease.c_str());
  1534. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_MINSIZEREL",
  1535. outputDirMinSizeRel.c_str());
  1536. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_RELWITHDEBINFO",
  1537. outputDirRelWithDebInfo.c_str());
  1538. if(!outputDirOld.empty())
  1539. {
  1540. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY",
  1541. outputDirOld.c_str());
  1542. }
  1543. }
  1544. cmSystemTools::ReplaceString(line,
  1545. "EXTRA_DEFINES",
  1546. this->Makefile->GetDefineFlags());
  1547. const char* debugPostfix
  1548. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1549. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1550. debugPostfix?debugPostfix:"");
  1551. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1552. target.GetType() <= cmTarget::OBJECT_LIBRARY)
  1553. {
  1554. // store flags for each configuration
  1555. std::string flags = " ";
  1556. std::string flagsRelease = " ";
  1557. std::string flagsMinSizeRel = " ";
  1558. std::string flagsDebug = " ";
  1559. std::string flagsRelWithDebInfo = " ";
  1560. std::vector<std::string> configs;
  1561. target.GetMakefile()->GetConfigurations(configs);
  1562. std::vector<std::string>::const_iterator it = configs.begin();
  1563. const std::string& linkLanguage = gt->GetLinkerLanguage(*it);
  1564. for ( ; it != configs.end(); ++it)
  1565. {
  1566. const std::string& configLinkLanguage = gt->GetLinkerLanguage(*it);
  1567. if (configLinkLanguage != linkLanguage)
  1568. {
  1569. cmSystemTools::Error
  1570. ("Linker language must not vary by configuration for target: ",
  1571. target.GetName().c_str());
  1572. }
  1573. }
  1574. if(linkLanguage.empty())
  1575. {
  1576. cmSystemTools::Error
  1577. ("CMake can not determine linker language for target: ",
  1578. target.GetName().c_str());
  1579. return;
  1580. }
  1581. // if CXX is on and the target contains cxx code then add the cxx flags
  1582. std::string baseFlagVar = "CMAKE_";
  1583. baseFlagVar += linkLanguage;
  1584. baseFlagVar += "_FLAGS";
  1585. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1586. std::string flagVar = baseFlagVar + "_RELEASE";
  1587. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1588. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1589. flagVar = baseFlagVar + "_MINSIZEREL";
  1590. flagsMinSizeRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1591. flagsMinSizeRel += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1592. flagVar = baseFlagVar + "_DEBUG";
  1593. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1594. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1595. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1596. flagsRelWithDebInfo = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1597. flagsRelWithDebInfo += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1598. this->AddCompileOptions(flags, &target, linkLanguage, "");
  1599. this->AddCompileOptions(flagsDebug, &target, linkLanguage, "Debug");
  1600. this->AddCompileOptions(flagsRelease, &target, linkLanguage, "Release");
  1601. this->AddCompileOptions(flagsMinSizeRel, &target, linkLanguage,
  1602. "MinSizeRel");
  1603. this->AddCompileOptions(flagsRelWithDebInfo, &target, linkLanguage,
  1604. "RelWithDebInfo");
  1605. // if _UNICODE and _SBCS are not found, then add -D_MBCS
  1606. std::string defs = this->Makefile->GetDefineFlags();
  1607. if(flags.find("D_UNICODE") == flags.npos &&
  1608. defs.find("D_UNICODE") == flags.npos &&
  1609. flags.find("D_SBCS") == flags.npos &&
  1610. defs.find("D_SBCS") == flags.npos)
  1611. {
  1612. flags += " /D \"_MBCS\"";
  1613. }
  1614. // Add per-target and per-configuration preprocessor definitions.
  1615. std::set<std::string> definesSet;
  1616. std::set<std::string> debugDefinesSet;
  1617. std::set<std::string> releaseDefinesSet;
  1618. std::set<std::string> minsizeDefinesSet;
  1619. std::set<std::string> debugrelDefinesSet;
  1620. this->AddCompileDefinitions(definesSet, &target, "", linkLanguage);
  1621. this->AddCompileDefinitions(debugDefinesSet, &target,
  1622. "DEBUG", linkLanguage);
  1623. this->AddCompileDefinitions(releaseDefinesSet, &target,
  1624. "RELEASE", linkLanguage);
  1625. this->AddCompileDefinitions(minsizeDefinesSet, &target,
  1626. "MINSIZEREL", linkLanguage);
  1627. this->AddCompileDefinitions(debugrelDefinesSet, &target,
  1628. "RELWITHDEBINFO", linkLanguage);
  1629. std::string defines = " ";
  1630. std::string debugDefines = " ";
  1631. std::string releaseDefines = " ";
  1632. std::string minsizeDefines = " ";
  1633. std::string debugrelDefines = " ";
  1634. this->JoinDefines(definesSet, defines, "");
  1635. this->JoinDefines(debugDefinesSet, debugDefines, "");
  1636. this->JoinDefines(releaseDefinesSet, releaseDefines, "");
  1637. this->JoinDefines(minsizeDefinesSet, minsizeDefines, "");
  1638. this->JoinDefines(debugrelDefinesSet, debugrelDefines, "");
  1639. flags += defines;
  1640. flagsDebug += debugDefines;
  1641. flagsRelease += releaseDefines;
  1642. flagsMinSizeRel += minsizeDefines;
  1643. flagsRelWithDebInfo += debugrelDefines;
  1644. // The template files have CXX FLAGS in them, that need to be replaced.
  1645. // There are not separate CXX and C template files, so we use the same
  1646. // variable names. The previous code sets up flags* variables to
  1647. // contain the correct C or CXX flags
  1648. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1649. flagsMinSizeRel.c_str());
  1650. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1651. flagsDebug.c_str());
  1652. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1653. flagsRelWithDebInfo.c_str());
  1654. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1655. flagsRelease.c_str());
  1656. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1657. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_MINSIZEREL",
  1658. minsizeDefines.c_str());
  1659. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_DEBUG",
  1660. debugDefines.c_str());
  1661. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_RELWITHDEBINFO",
  1662. debugrelDefines.c_str());
  1663. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_RELEASE",
  1664. releaseDefines.c_str());
  1665. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS",
  1666. defines.c_str());
  1667. }
  1668. fout << line.c_str() << std::endl;
  1669. }
  1670. }
  1671. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1672. {
  1673. cmsys::ifstream fin(this->DSPFooterTemplate.c_str());
  1674. if(!fin)
  1675. {
  1676. cmSystemTools::Error("Error Reading ",
  1677. this->DSPFooterTemplate.c_str());
  1678. }
  1679. std::string line;
  1680. while(cmSystemTools::GetLineFromStream(fin, line))
  1681. {
  1682. fout << line << std::endl;
  1683. }
  1684. }
  1685. //----------------------------------------------------------------------------
  1686. void cmLocalVisualStudio6Generator
  1687. ::ComputeLinkOptions(cmTarget& target,
  1688. const std::string& configName,
  1689. const std::string extraOptions,
  1690. std::string& options)
  1691. {
  1692. cmGeneratorTarget* gt =
  1693. this->GlobalGenerator->GetGeneratorTarget(&target);
  1694. // Compute the link information for this configuration.
  1695. cmComputeLinkInformation* pcli = gt->GetLinkInformation(configName);
  1696. if(!pcli)
  1697. {
  1698. return;
  1699. }
  1700. cmComputeLinkInformation& cli = *pcli;
  1701. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1702. ItemVector const& linkLibs = cli.GetItems();
  1703. std::vector<std::string> const& linkDirs = cli.GetDirectories();
  1704. this->OutputObjects(target, "LINK", options);
  1705. // Build the link options code.
  1706. for(std::vector<std::string>::const_iterator d = linkDirs.begin();
  1707. d != linkDirs.end(); ++d)
  1708. {
  1709. std::string dir = *d;
  1710. if(!dir.empty())
  1711. {
  1712. if(dir[dir.size()-1] != '/')
  1713. {
  1714. dir += "/";
  1715. }
  1716. dir += "$(IntDir)";
  1717. options += "# ADD LINK32 /LIBPATH:";
  1718. options += this->ConvertToOutputFormat(dir.c_str(), SHELL);
  1719. options += " /LIBPATH:";
  1720. options += this->ConvertToOutputFormat(d->c_str(), SHELL);
  1721. options += "\n";
  1722. }
  1723. }
  1724. for(ItemVector::const_iterator l = linkLibs.begin();
  1725. l != linkLibs.end(); ++l)
  1726. {
  1727. options += "# ADD LINK32 ";
  1728. if(l->IsPath)
  1729. {
  1730. options +=
  1731. this->ConvertToOutputFormat(l->Value.c_str(), SHELL);
  1732. }
  1733. else if (!l->Target
  1734. || l->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  1735. {
  1736. options += l->Value;
  1737. }
  1738. options += "\n";
  1739. }
  1740. // Add extra options if any.
  1741. if(!extraOptions.empty())
  1742. {
  1743. options += "# ADD LINK32 ";
  1744. options += extraOptions;
  1745. options += "\n";
  1746. }
  1747. }
  1748. //----------------------------------------------------------------------------
  1749. void cmLocalVisualStudio6Generator
  1750. ::OutputObjects(cmTarget& target, const char* tool,
  1751. std::string& options)
  1752. {
  1753. // VS 6 does not support per-config source locations so we
  1754. // list object library content on the link line instead.
  1755. cmGeneratorTarget* gt =
  1756. this->GlobalGenerator->GetGeneratorTarget(&target);
  1757. std::vector<std::string> objs;
  1758. gt->UseObjectLibraries(objs, "");
  1759. for(std::vector<std::string>::const_iterator
  1760. oi = objs.begin(); oi != objs.end(); ++oi)
  1761. {
  1762. options += "# ADD ";
  1763. options += tool;
  1764. options += "32 ";
  1765. options += this->ConvertToOutputFormat(oi->c_str(), SHELL);
  1766. options += "\n";
  1767. }
  1768. }
  1769. std::string
  1770. cmLocalVisualStudio6Generator
  1771. ::GetTargetDirectory(cmTarget const&) const
  1772. {
  1773. // No per-target directory for this generator (yet).
  1774. return "";
  1775. }
  1776. //----------------------------------------------------------------------------
  1777. std::string
  1778. cmLocalVisualStudio6Generator
  1779. ::ComputeLongestObjectDirectory(cmTarget&) const
  1780. {
  1781. // Compute the maximum length configuration name.
  1782. std::string config_max;
  1783. for(std::vector<std::string>::const_iterator
  1784. i = this->Configurations.begin();
  1785. i != this->Configurations.end(); ++i)
  1786. {
  1787. // Strip the subdirectory name out of the configuration name.
  1788. std::string config = this->GetConfigName(*i);
  1789. if(config.size() > config_max.size())
  1790. {
  1791. config_max = config;
  1792. }
  1793. }
  1794. // Compute the maximum length full path to the intermediate
  1795. // files directory for any configuration. This is used to construct
  1796. // object file names that do not produce paths that are too long.
  1797. std::string dir_max;
  1798. dir_max += this->Makefile->GetCurrentBinaryDirectory();
  1799. dir_max += "/";
  1800. dir_max += config_max;
  1801. dir_max += "/";
  1802. return dir_max;
  1803. }
  1804. std::string
  1805. cmLocalVisualStudio6Generator
  1806. ::GetConfigName(std::string const& configuration) const
  1807. {
  1808. // Strip the subdirectory name out of the configuration name.
  1809. std::string config = configuration;
  1810. std::string::size_type pos = config.find_last_of(" ");
  1811. config = config.substr(pos+1, std::string::npos);
  1812. config = config.substr(0, config.size()-1);
  1813. return config;
  1814. }
  1815. //----------------------------------------------------------------------------
  1816. bool
  1817. cmLocalVisualStudio6Generator
  1818. ::CheckDefinition(std::string const& define) const
  1819. {
  1820. // Perform the standard check first.
  1821. if(!this->cmLocalGenerator::CheckDefinition(define))
  1822. {
  1823. return false;
  1824. }
  1825. // Now do the VS6-specific check.
  1826. if(define.find_first_of(" ") != define.npos &&
  1827. define.find_first_of("\"$;") != define.npos)
  1828. {
  1829. std::ostringstream e;
  1830. e << "WARNING: The VS6 IDE does not support preprocessor definition "
  1831. << "values with spaces and '\"', '$', or ';'.\n"
  1832. << "CMake is dropping a preprocessor definition: " << define << "\n"
  1833. << "Consider defining the macro in a (configured) header file.\n";
  1834. cmSystemTools::Message(e.str().c_str());
  1835. return false;
  1836. }
  1837. // Assume it is supported.
  1838. return true;
  1839. }