cmLocalVisualStudio6Generator.cxx 69 KB

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