cmDSPWriter.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDSPWriter.h"
  14. #include "cmStandardIncludes.h"
  15. #include "cmSystemTools.h"
  16. #include "cmRegularExpression.h"
  17. cmDSPWriter::~cmDSPWriter()
  18. {
  19. }
  20. cmDSPWriter::cmDSPWriter(cmMakefile*mf)
  21. {
  22. m_Makefile = mf;
  23. }
  24. void cmDSPWriter::OutputDSPFile()
  25. {
  26. // If not an in source build, then create the output directory
  27. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  28. m_Makefile->GetHomeDirectory()) != 0)
  29. {
  30. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  31. {
  32. cmSystemTools::Error("Error creating directory ",
  33. m_Makefile->GetStartOutputDirectory());
  34. }
  35. }
  36. // Setup /I and /LIBPATH options for the resulting DSP file
  37. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  38. std::vector<std::string>::iterator i;
  39. for(i = includes.begin(); i != includes.end(); ++i)
  40. {
  41. m_IncludeOptions += " /I ";
  42. std::string tmp = cmSystemTools::ConvertToOutputPath(i->c_str());
  43. // quote if not already quoted
  44. if (tmp[0] != '"')
  45. {
  46. m_IncludeOptions += "\"";
  47. m_IncludeOptions += tmp;
  48. m_IncludeOptions += "\"";
  49. }
  50. else
  51. {
  52. m_IncludeOptions += tmp;
  53. }
  54. }
  55. // Create the DSP or set of DSP's for libraries and executables
  56. // clear project names
  57. m_CreatedProjectNames.clear();
  58. // build any targets
  59. cmTargets &tgts = m_Makefile->GetTargets();
  60. for(cmTargets::iterator l = tgts.begin();
  61. l != tgts.end(); l++)
  62. {
  63. switch(l->second.GetType())
  64. {
  65. case cmTarget::STATIC_LIBRARY:
  66. this->SetBuildType(STATIC_LIBRARY, l->first.c_str());
  67. break;
  68. case cmTarget::SHARED_LIBRARY:
  69. this->SetBuildType(DLL, l->first.c_str());
  70. break;
  71. case cmTarget::EXECUTABLE:
  72. this->SetBuildType(EXECUTABLE,l->first.c_str());
  73. break;
  74. case cmTarget::WIN32_EXECUTABLE:
  75. this->SetBuildType(WIN32_EXECUTABLE,l->first.c_str());
  76. break;
  77. case cmTarget::UTILITY:
  78. this->SetBuildType(UTILITY, l->first.c_str());
  79. break;
  80. case cmTarget::INSTALL_FILES:
  81. break;
  82. case cmTarget::INSTALL_PROGRAMS:
  83. break;
  84. default:
  85. cmSystemTools::Error("Bad target type", l->first.c_str());
  86. break;
  87. }
  88. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  89. // so don't build a projectfile for it
  90. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  91. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  92. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  93. {
  94. this->CreateSingleDSP(l->first.c_str(),l->second);
  95. }
  96. }
  97. }
  98. void cmDSPWriter::CreateSingleDSP(const char *lname, cmTarget &target)
  99. {
  100. // add to the list of projects
  101. std::string pname = lname;
  102. m_CreatedProjectNames.push_back(pname);
  103. // create the dsp.cmake file
  104. std::string fname;
  105. fname = m_Makefile->GetStartOutputDirectory();
  106. fname += "/";
  107. fname += lname;
  108. fname += ".dsp";
  109. // save the name of the real dsp file
  110. std::string realDSP = fname;
  111. fname += ".cmake";
  112. std::ofstream fout(fname.c_str());
  113. if(!fout)
  114. {
  115. cmSystemTools::Error("Error Writing ", fname.c_str());
  116. }
  117. this->WriteDSPFile(fout,lname,target);
  118. fout.close();
  119. // if the dsp file has changed, then write it.
  120. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  121. }
  122. void cmDSPWriter::AddDSPBuildRule(cmSourceGroup& sourceGroup)
  123. {
  124. std::string dspname = *(m_CreatedProjectNames.end()-1);
  125. if(dspname == "ALL_BUILD")
  126. {
  127. return;
  128. }
  129. dspname += ".dsp.cmake";
  130. std::string makefileIn = m_Makefile->GetStartDirectory();
  131. makefileIn += "/";
  132. makefileIn += "CMakeLists.txt";
  133. makefileIn = cmSystemTools::ConvertToOutputPath(makefileIn.c_str());
  134. std::string dsprule = "${CMAKE_COMMAND}";
  135. m_Makefile->ExpandVariablesInString(dsprule);
  136. dsprule = cmSystemTools::ConvertToOutputPath(dsprule.c_str());
  137. std::string args = makefileIn;
  138. args += " -H\"";
  139. args +=
  140. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory());
  141. args += "\" -S\"";
  142. args +=
  143. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory());
  144. args += "\" -O\"";
  145. args +=
  146. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory());
  147. args += "\" -B\"";
  148. args +=
  149. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory());
  150. args += "\"";
  151. m_Makefile->ExpandVariablesInString(args);
  152. std::string configFile =
  153. m_Makefile->GetDefinition("CMAKE_ROOT");
  154. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  155. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  156. bool found = false;
  157. for(std::vector<std::string>::iterator i = listFiles.begin();
  158. i != listFiles.end(); ++i)
  159. {
  160. if(*i == configFile)
  161. {
  162. found = true;
  163. }
  164. }
  165. if(!found)
  166. {
  167. listFiles.push_back(configFile);
  168. }
  169. std::vector<std::string> outputs;
  170. outputs.push_back(dspname);
  171. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  172. args.c_str(),
  173. listFiles,
  174. outputs);
  175. sourceGroup.AddCustomCommand(cc);
  176. }
  177. void cmDSPWriter::WriteDSPFile(std::ostream& fout,
  178. const char *libName,
  179. cmTarget &target)
  180. {
  181. // We may be modifying the source groups temporarily, so make a copy.
  182. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  183. // get the classes from the source lists then add them to the groups
  184. std::vector<cmSourceFile> classes = target.GetSourceFiles();
  185. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  186. i != classes.end(); i++)
  187. {
  188. // Add the file to the list of sources.
  189. std::string source = i->GetFullPath();
  190. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  191. sourceGroups);
  192. sourceGroup.AddSource(source.c_str());
  193. }
  194. // add any custom rules to the source groups
  195. for (std::vector<cmCustomCommand>::const_iterator cr =
  196. target.GetCustomCommands().begin();
  197. cr != target.GetCustomCommands().end(); ++cr)
  198. {
  199. cmSourceGroup& sourceGroup =
  200. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  201. sourceGroups);
  202. cmCustomCommand cc(*cr);
  203. cc.ExpandVariables(*m_Makefile);
  204. sourceGroup.AddCustomCommand(cc);
  205. }
  206. // Write the DSP file's header.
  207. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  208. // Find the group in which the CMakeLists.txt source belongs, and add
  209. // the rule to generate this DSP file.
  210. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  211. sg != sourceGroups.rend(); ++sg)
  212. {
  213. if(sg->Matches("CMakeLists.txt"))
  214. {
  215. this->AddDSPBuildRule(*sg);
  216. break;
  217. }
  218. }
  219. // Loop through every source group.
  220. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  221. sg != sourceGroups.end(); ++sg)
  222. {
  223. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  224. // If the group is empty, don't write it at all.
  225. if(buildRules.empty())
  226. { continue; }
  227. // If the group has a name, write the header.
  228. std::string name = sg->GetName();
  229. if(name != "")
  230. {
  231. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  232. }
  233. // Loop through each build rule in the source group.
  234. for(cmSourceGroup::BuildRules::const_iterator cc =
  235. buildRules.begin(); cc != buildRules.end(); ++ cc)
  236. {
  237. std::string source = cc->first;
  238. const cmSourceGroup::Commands& commands = cc->second;
  239. if (source != libName || target.GetType() == cmTarget::UTILITY)
  240. {
  241. fout << "# Begin Source File\n\n";
  242. // Tell MS-Dev what the source is. If the compiler knows how to
  243. // build it, then it will.
  244. fout << "SOURCE=" <<
  245. cmSystemTools::ConvertToOutputPath(source.c_str()) << "\n\n";
  246. if (!commands.empty())
  247. {
  248. cmSourceGroup::CommandFiles totalCommand;
  249. std::string totalCommandStr;
  250. totalCommandStr = this->CombineCommands(commands, totalCommand,
  251. source.c_str());
  252. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  253. totalCommand.m_Depends,
  254. totalCommand.m_Outputs);
  255. }
  256. fout << "# End Source File\n";
  257. }
  258. }
  259. // If the group has a name, write the footer.
  260. if(name != "")
  261. {
  262. this->WriteDSPEndGroup(fout);
  263. }
  264. }
  265. // Write the DSP file's footer.
  266. this->WriteDSPFooter(fout);
  267. }
  268. void cmDSPWriter::WriteCustomRule(std::ostream& fout,
  269. const char* source,
  270. const char* command,
  271. const std::set<std::string>& depends,
  272. const std::set<std::string>& outputs)
  273. {
  274. std::vector<std::string>::iterator i;
  275. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  276. {
  277. if (i == m_Configurations.begin())
  278. {
  279. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  280. }
  281. else
  282. {
  283. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  284. }
  285. // Write out the dependencies for the rule.
  286. fout << "USERDEP__HACK=";
  287. for(std::set<std::string>::const_iterator d = depends.begin();
  288. d != depends.end(); ++d)
  289. {
  290. fout << "\\\n\t" <<
  291. cmSystemTools::ConvertToOutputPath(d->c_str());
  292. }
  293. fout << "\n";
  294. fout << "# PROP Ignore_Default_Tool 1\n";
  295. fout << "# Begin Custom Build\n\n";
  296. if(outputs.size() == 0)
  297. {
  298. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  299. fout << command << "\n\n";
  300. }
  301. // Write a rule for every output generated by this command.
  302. for(std::set<std::string>::const_iterator output = outputs.begin();
  303. output != outputs.end(); ++output)
  304. {
  305. fout << "\"" << output->c_str()
  306. << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  307. fout << command << "\n\n";
  308. }
  309. fout << "# End Custom Build\n\n";
  310. }
  311. fout << "!ENDIF\n\n";
  312. }
  313. void cmDSPWriter::WriteDSPBeginGroup(std::ostream& fout,
  314. const char* group,
  315. const char* filter)
  316. {
  317. fout << "# Begin Group \"" << group << "\"\n"
  318. "# PROP Default_Filter \"" << filter << "\"\n";
  319. }
  320. void cmDSPWriter::WriteDSPEndGroup(std::ostream& fout)
  321. {
  322. fout << "# End Group\n";
  323. }
  324. void cmDSPWriter::SetBuildType(BuildType b, const char *libName)
  325. {
  326. std::string root= m_Makefile->GetDefinition("CMAKE_ROOT");
  327. const char *def= m_Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  328. if( def)
  329. {
  330. root = def;
  331. }
  332. else
  333. {
  334. root += "/Templates";
  335. }
  336. switch(b)
  337. {
  338. case STATIC_LIBRARY:
  339. m_DSPHeaderTemplate = root;
  340. m_DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  341. m_DSPFooterTemplate = root;
  342. m_DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  343. break;
  344. case DLL:
  345. m_DSPHeaderTemplate = root;
  346. m_DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  347. m_DSPFooterTemplate = root;
  348. m_DSPFooterTemplate += "/DLLFooter.dsptemplate";
  349. break;
  350. case EXECUTABLE:
  351. m_DSPHeaderTemplate = root;
  352. m_DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  353. m_DSPFooterTemplate = root;
  354. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  355. break;
  356. case WIN32_EXECUTABLE:
  357. m_DSPHeaderTemplate = root;
  358. m_DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  359. m_DSPFooterTemplate = root;
  360. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  361. break;
  362. case UTILITY:
  363. m_DSPHeaderTemplate = root;
  364. m_DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  365. m_DSPFooterTemplate = root;
  366. m_DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  367. break;
  368. }
  369. // once the build type is set, determine what configurations are
  370. // possible
  371. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  372. cmRegularExpression reg("# Name ");
  373. if(!fin)
  374. {
  375. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  376. }
  377. // reset m_Configurations
  378. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  379. // now add all the configurations possible
  380. char buffer[2048];
  381. while(fin)
  382. {
  383. fin.getline(buffer, 2048);
  384. std::string line = buffer;
  385. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  386. if (reg.find(line))
  387. {
  388. m_Configurations.push_back(line.substr(reg.end()));
  389. }
  390. }
  391. }
  392. std::string
  393. cmDSPWriter::CombineCommands(const cmSourceGroup::Commands &commands,
  394. cmSourceGroup::CommandFiles &totalCommand,
  395. const char *source)
  396. {
  397. // Loop through every custom command generating code from the
  398. // current source.
  399. // build up the depends and outputs and commands
  400. std::string totalCommandStr = "";
  401. std::string temp;
  402. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  403. c != commands.end(); ++c)
  404. {
  405. totalCommandStr += "\n\t";
  406. temp= c->second.m_Command;
  407. temp = cmSystemTools::ConvertToOutputPath(temp.c_str());
  408. totalCommandStr += temp;
  409. totalCommandStr += " ";
  410. totalCommandStr += c->second.m_Arguments;
  411. totalCommand.Merge(c->second);
  412. }
  413. // Create a dummy file with the name of the source if it does
  414. // not exist
  415. if(totalCommand.m_Outputs.empty())
  416. {
  417. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  418. dummyFile += "/";
  419. dummyFile += source;
  420. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  421. {
  422. std::ofstream fout(dummyFile.c_str());
  423. fout << "Dummy file created by cmake as unused source for utility command.\n";
  424. }
  425. }
  426. return totalCommandStr;
  427. }
  428. // look for custom rules on a target and collect them together
  429. std::string
  430. cmDSPWriter::CreateTargetRules(const cmTarget &target,
  431. const char *libName)
  432. {
  433. std::string customRuleCode = "";
  434. if (target.GetType() >= cmTarget::UTILITY)
  435. {
  436. return customRuleCode;
  437. }
  438. // Find the group in which the lix exe custom rules belong
  439. bool init = false;
  440. for (std::vector<cmCustomCommand>::const_iterator cr =
  441. target.GetCustomCommands().begin();
  442. cr != target.GetCustomCommands().end(); ++cr)
  443. {
  444. cmCustomCommand cc(*cr);
  445. cc.ExpandVariables(*m_Makefile);
  446. if (cc.GetSourceName() == libName)
  447. {
  448. if (!init)
  449. {
  450. // header stuff
  451. customRuleCode = "# Begin Special Build Tool\nPostBuild_Cmds=";
  452. init = true;
  453. }
  454. else
  455. {
  456. customRuleCode += "\t";
  457. }
  458. customRuleCode += cc.GetCommand() + " " + cc.GetArguments();
  459. }
  460. }
  461. if (init)
  462. {
  463. customRuleCode += "\n# End Special Build Tool\n";
  464. }
  465. return customRuleCode;
  466. }
  467. void cmDSPWriter::WriteDSPHeader(std::ostream& fout, const char *libName,
  468. const cmTarget &target,
  469. std::vector<cmSourceGroup> &)
  470. {
  471. std::set<std::string> pathEmitted;
  472. // determine the link directories
  473. std::string libOptions;
  474. std::string libDebugOptions;
  475. std::string libOptimizedOptions;
  476. std::string libMultiLineOptions;
  477. std::string libMultiLineDebugOptions;
  478. std::string libMultiLineOptimizedOptions;
  479. // suppoirt override in output directory
  480. std::string libPath = "";
  481. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  482. {
  483. libPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  484. }
  485. std::string exePath = "";
  486. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  487. {
  488. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  489. }
  490. if(libPath.size())
  491. {
  492. // make sure there is a trailing slash
  493. if(libPath[libPath.size()-1] != '/')
  494. {
  495. libPath += "/";
  496. }
  497. std::string lpath =
  498. cmSystemTools::ConvertToOutputPath(libPath.c_str());
  499. if(pathEmitted.insert(lpath).second)
  500. {
  501. libOptions += " /LIBPATH:\"";
  502. libOptions += lpath;
  503. libOptions += "$(INTDIR)\" ";
  504. libOptions += " /LIBPATH:\"";
  505. libOptions += lpath;
  506. libOptions += "\" ";
  507. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  508. libMultiLineOptions += lpath;
  509. libMultiLineOptions += "$(INTDIR)\" ";
  510. libMultiLineOptions += " /LIBPATH:\"";
  511. libMultiLineOptions += lpath;
  512. libMultiLineOptions += "\" \n";
  513. }
  514. }
  515. if(exePath.size())
  516. {
  517. // make sure there is a trailing slash
  518. if(exePath[exePath.size()-1] != '/')
  519. {
  520. exePath += "/";
  521. }
  522. std::string lpath =
  523. cmSystemTools::ConvertToOutputPath(exePath.c_str());
  524. if(pathEmitted.insert(lpath).second)
  525. {
  526. libOptions += " /LIBPATH:\"";
  527. libOptions += lpath;
  528. libOptions += "$(INTDIR)\" ";
  529. libOptions += " /LIBPATH:\"";
  530. libOptions += lpath;
  531. libOptions += "\" ";
  532. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  533. libMultiLineOptions += lpath;
  534. libMultiLineOptions += "$(INTDIR)\" ";
  535. libMultiLineOptions += " /LIBPATH:\"";
  536. libMultiLineOptions += lpath;
  537. libMultiLineOptions += "\" \n";
  538. }
  539. }
  540. std::vector<std::string>::iterator i;
  541. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  542. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  543. {
  544. std::string lpath =
  545. cmSystemTools::ConvertToOutputPath(i->c_str());
  546. if(lpath[lpath.size()-1] != '/')
  547. {
  548. lpath += "/";
  549. }
  550. if(pathEmitted.insert(lpath).second)
  551. {
  552. libOptions += " /LIBPATH:\"";
  553. libOptions += lpath;
  554. libOptions += "/$(INTDIR)\" ";
  555. libOptions += " /LIBPATH:\"";
  556. libOptions += lpath;
  557. libOptions += "\" ";
  558. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  559. libMultiLineOptions += lpath;
  560. libMultiLineOptions += "/$(INTDIR)\" ";
  561. libMultiLineOptions += " /LIBPATH:\"";
  562. libMultiLineOptions += lpath;
  563. libMultiLineOptions += "\" \n";
  564. }
  565. }
  566. // find link libraries
  567. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  568. cmTarget::LinkLibraries::const_iterator j;
  569. for(j = libs.begin(); j != libs.end(); ++j)
  570. {
  571. // add libraries to executables and dlls (but never include
  572. // a library in a library, bad recursion)
  573. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  574. && target.GetType() != cmTarget::STATIC_LIBRARY) ||
  575. (target.GetType() == cmTarget::SHARED_LIBRARY && libName != j->first))
  576. {
  577. std::string lib = j->first;
  578. if(j->first.find(".lib") == std::string::npos)
  579. {
  580. lib += ".lib";
  581. }
  582. lib = cmSystemTools::ConvertToOutputPath(lib.c_str());
  583. if (j->second == cmTarget::GENERAL)
  584. {
  585. libOptions += " ";
  586. libOptions += lib;
  587. libMultiLineOptions += "# ADD LINK32 ";
  588. libMultiLineOptions += lib;
  589. libMultiLineOptions += "\n";
  590. }
  591. if (j->second == cmTarget::DEBUG)
  592. {
  593. libDebugOptions += " ";
  594. libDebugOptions += lib;
  595. libMultiLineDebugOptions += "# ADD LINK32 ";
  596. libMultiLineDebugOptions += lib;
  597. libMultiLineDebugOptions += "\n";
  598. }
  599. if (j->second == cmTarget::OPTIMIZED)
  600. {
  601. libOptimizedOptions += " ";
  602. libOptimizedOptions += lib;
  603. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  604. libMultiLineOptimizedOptions += lib;
  605. libMultiLineOptimizedOptions += "\n";
  606. }
  607. }
  608. }
  609. std::string extraLinkOptions =
  610. m_Makefile->GetDefinition("CMAKE_EXTRA_LINK_FLAGS");
  611. if(extraLinkOptions.size())
  612. {
  613. libOptions += " ";
  614. libOptions += extraLinkOptions;
  615. libOptions += " ";
  616. libMultiLineOptions += "# ADD LINK32 ";
  617. libMultiLineOptions += extraLinkOptions;
  618. libMultiLineOptions += " \n";
  619. }
  620. // are there any custom rules on the target itself
  621. // only if the target is a lib or exe
  622. std::string customRuleCode = this->CreateTargetRules(target, libName);
  623. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  624. if(!fin)
  625. {
  626. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  627. }
  628. char buffer[2048];
  629. while(fin)
  630. {
  631. fin.getline(buffer, 2048);
  632. std::string line = buffer;
  633. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  634. if(!mfcFlag)
  635. {
  636. mfcFlag = "0";
  637. }
  638. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  639. customRuleCode.c_str());
  640. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  641. mfcFlag);
  642. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  643. libOptions.c_str());
  644. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  645. libDebugOptions.c_str());
  646. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  647. libOptimizedOptions.c_str());
  648. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  649. libMultiLineOptions.c_str());
  650. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  651. libMultiLineDebugOptions.c_str());
  652. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  653. libMultiLineOptimizedOptions.c_str());
  654. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  655. m_IncludeOptions.c_str());
  656. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  657. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  658. cmSystemTools::ConvertToOutputPath(libPath.c_str()).c_str());
  659. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  660. cmSystemTools::ConvertToOutputPath(exePath.c_str()).c_str());
  661. cmSystemTools::ReplaceString(line,
  662. "EXTRA_DEFINES",
  663. m_Makefile->GetDefineFlags());
  664. cmSystemTools::ReplaceString(line,
  665. "CMAKE_CXX_FLAGS_RELEASE",
  666. m_Makefile->
  667. GetDefinition("CMAKE_CXX_FLAGS_RELEASE"));
  668. cmSystemTools::ReplaceString(line,
  669. "CMAKE_CXX_FLAGS_MINSIZEREL",
  670. m_Makefile->
  671. GetDefinition("CMAKE_CXX_FLAGS_MINSIZEREL")
  672. );
  673. cmSystemTools::ReplaceString(line,
  674. "CMAKE_CXX_FLAGS_DEBUG",
  675. m_Makefile->
  676. GetDefinition("CMAKE_CXX_FLAGS_DEBUG"));
  677. cmSystemTools::ReplaceString(line,
  678. "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  679. m_Makefile->
  680. GetDefinition("CMAKE_CXX_FLAGS_RELWITHDEBINFO"));
  681. cmSystemTools::ReplaceString(line,
  682. "CMAKE_CXX_FLAGS",
  683. m_Makefile->
  684. GetDefinition("CMAKE_CXX_FLAGS"));
  685. fout << line.c_str() << std::endl;
  686. }
  687. }
  688. void cmDSPWriter::WriteDSPFooter(std::ostream& fout)
  689. {
  690. std::ifstream fin(m_DSPFooterTemplate.c_str());
  691. if(!fin)
  692. {
  693. cmSystemTools::Error("Error Reading ",
  694. m_DSPFooterTemplate.c_str());
  695. }
  696. char buffer[2048];
  697. while(fin)
  698. {
  699. fin.getline(buffer, 2048);
  700. fout << buffer << std::endl;
  701. }
  702. }