cmLocalVisualStudio6Generator.cxx 67 KB

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