cmLocalVisualStudio6Generator.cxx 69 KB

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