cmLocalNMakeMakefileGenerator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 "cmLocalNMakeMakefileGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmStandardIncludes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "windows.h"
  22. cmLocalNMakeMakefileGenerator::cmLocalNMakeMakefileGenerator()
  23. {
  24. this->SetLibraryPathOption("@CMAKE_C_LIBPATH_FLAG@"); // Use @ here
  25. this->SetObjectFileExtension("$(CMAKE_OBJECT_FILE_SUFFIX)");
  26. this->SetExecutableExtension("$(CMAKE_EXECUTABLE_SUFFIX)");
  27. this->SetLibraryPrefix("");
  28. this->SetStaticLibraryExtension("$(CMAKE_STATICLIB_SUFFIX)");
  29. this->SetSharedLibraryExtension("$(CMAKE_SHLIB_SUFFIX)");
  30. }
  31. cmLocalNMakeMakefileGenerator::~cmLocalNMakeMakefileGenerator()
  32. {
  33. }
  34. // convert to windows short paths if there are spaces
  35. // in path
  36. std::string cmLocalNMakeMakefileGenerator::ShortPath(const char* path)
  37. {
  38. std::string ret = path;
  39. // if there are no spaces in path, then just
  40. // call ConvertToOutputPath
  41. if(ret.find(' ') == std::string::npos)
  42. {
  43. return cmSystemTools::ConvertToOutputPath(path);
  44. }
  45. // if there are spaces then call GetShortPathName to get rid of them
  46. if(!cmSystemTools::GetShortPath(path, ret))
  47. {
  48. // if GetShortPathName failed for some reason use
  49. // ConvertToOutputPath instead which will at least escape the spaces
  50. ret = cmSystemTools::ConvertToOutputPath(path);
  51. return ret;
  52. }
  53. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  54. return ret;
  55. }
  56. // convert a command to a short path if it has spaces
  57. // this separates the arguments from the command and puts
  58. // them back together
  59. std::string cmLocalNMakeMakefileGenerator::ShortPathCommand(const char* command)
  60. {
  61. if (!command)
  62. {
  63. return "";
  64. }
  65. if(!strchr(command, ' '))
  66. {
  67. return command;
  68. }
  69. cmRegularExpression reg("^\"([^\"]*)\"(.*)");
  70. if(reg.find(command))
  71. {
  72. std::string c = reg.match(1);
  73. cmRegularExpression removeIntDir("(.*)(/|\\\\)\\$\\(IntDir\\)(.*)");
  74. if(removeIntDir.find(c))
  75. {
  76. c = removeIntDir.match(1) + removeIntDir.match(3);
  77. }
  78. std::string unixPath = c;
  79. // since the command may already be a windows path, convert it
  80. // to unix so we can use SplitProgramPath on it.
  81. cmSystemTools::ConvertToUnixSlashes(unixPath);
  82. std::string path, file;
  83. cmSystemTools::SplitProgramPath(unixPath.c_str(), path, file);
  84. // do a short path on the directory, because ShortPath will
  85. // not work for files that do not exist
  86. path = this->ShortPath(path.c_str());
  87. // now put the two back together
  88. path += "\\";
  89. path += file;
  90. std::string ret = path;
  91. std::string args = reg.match(2);
  92. ret += args;
  93. return ret;
  94. }
  95. return command;
  96. }
  97. void cmLocalNMakeMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  98. {
  99. fout << "# NMake Makefile generated by cmake\n";
  100. const char* variables =
  101. "# general variables used in the makefile\n"
  102. "\n"
  103. "# Path to cmake\n"
  104. "MAKESILENT = /nologo\n"
  105. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  106. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@ @BUILD_FLAGS@ -DWIN32\n"
  107. "CMAKE_C_LINK_EXECUTABLE_FLAG = @CMAKE_C_LINK_EXECUTABLE_FLAG@\n"
  108. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@ -DWIN32 @BUILD_FLAGS@\n"
  109. "CMAKE_LINKER_FLAGS = @CMAKE_LINKER_FLAGS@ @LINKER_BUILD_FLAGS@\n"
  110. "CMAKE_LINKER_SHARED_LIBRARY_FLAG = @CMAKE_LINKER_SHARED_LIBRARY_FLAG@\n"
  111. "CMAKE_LIBRARY_MANAGER_FLAGS = @CMAKE_LIBRARY_MANAGER_FLAGS@\n"
  112. "CMAKE_OBJECT_FILE_SUFFIX = @CMAKE_OBJECT_FILE_SUFFIX@\n"
  113. "CMAKE_EXECUTABLE_SUFFIX = @CMAKE_EXECUTABLE_SUFFIX@\n"
  114. "CMAKE_STATICLIB_SUFFIX = @CMAKE_STATICLIB_SUFFIX@\n"
  115. "CMAKE_SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  116. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  117. "NULL=\n"
  118. "!ELSE \n"
  119. "NULL=nul\n"
  120. "!ENDIF \n";
  121. std::string buildType = "CMAKE_CXX_FLAGS_";
  122. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  123. buildType = cmSystemTools::UpperCase(buildType);
  124. m_Makefile->AddDefinition("BUILD_FLAGS",
  125. m_Makefile->GetDefinition(
  126. buildType.c_str()));
  127. buildType = "CMAKE_LINKER_FLAGS_";
  128. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  129. buildType = cmSystemTools::UpperCase(buildType);
  130. m_Makefile->AddDefinition("LINKER_BUILD_FLAGS",
  131. m_Makefile->GetDefinition(
  132. buildType.c_str()));
  133. std::string replaceVars = variables;
  134. m_Makefile->ExpandVariablesInString(replaceVars);
  135. fout << replaceVars.c_str();
  136. std::string ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER");
  137. fout << "CMAKE_C_COMPILER = "
  138. << this->ShortPath(ccompiler.c_str()) << "\n";
  139. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  140. fout << "CMAKE_CXX_COMPILER = "
  141. << this->ShortPath(cxxcompiler.c_str()) << "\n";
  142. std::string linker = m_Makefile->GetDefinition("CMAKE_LINKER");
  143. fout << "CMAKE_LINKER = " <<
  144. this->ShortPath(linker.c_str()) << "\n";
  145. std::string lib_manager = m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER");
  146. fout << "CMAKE_LIBRARY_MANAGER = "
  147. << this->ShortPath(lib_manager.c_str()) << "\n";
  148. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  149. fout << "CMAKE_COMMAND = "
  150. << this->ShortPath(cmakecommand.c_str()) << "\n";
  151. fout << "RM = " << this->ShortPath(cmakecommand.c_str()) << " -E remove -f\n";
  152. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  153. {
  154. fout << "CMAKE_EDIT_COMMAND = "
  155. << this->ShortPath(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  156. << "\n";
  157. }
  158. fout << "CMAKE_CURRENT_SOURCE = "
  159. << this->ShortPath(m_Makefile->GetStartDirectory() )
  160. << "\n";
  161. fout << "CMAKE_CURRENT_BINARY = "
  162. << this->ShortPath(m_Makefile->GetStartOutputDirectory())
  163. << "\n";
  164. fout << "CMAKE_SOURCE_DIR = "
  165. << this->ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  166. fout << "CMAKE_BINARY_DIR = "
  167. << this->ShortPath(m_Makefile->GetHomeOutputDirectory() )
  168. << "\n";
  169. // Output Include paths
  170. fout << "INCLUDE_FLAGS = ";
  171. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  172. std::vector<std::string>::iterator i;
  173. fout << "-I" <<
  174. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  175. for(i = includes.begin(); i != includes.end(); ++i)
  176. {
  177. std::string include = *i;
  178. // Don't output a -I for the standard include path "/usr/include".
  179. // This can cause problems with certain standard library
  180. // implementations because the wrong headers may be found first.
  181. fout << "-I" << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str() << " ";
  182. }
  183. fout << m_Makefile->GetDefineFlags();
  184. fout << "\n\n";
  185. }
  186. void cmLocalNMakeMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  187. const char* directory,
  188. const char* target1,
  189. const char* target2,
  190. bool silent)
  191. {
  192. if(target1)
  193. {
  194. std::string dir = cmSystemTools::ConvertToOutputPath(directory);
  195. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  196. << " "
  197. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  198. if (!silent)
  199. {
  200. fout << "\techo " << directory << ": building " << target1 << "\n";
  201. }
  202. fout << "\tcd " << dir << "\n"
  203. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  204. }
  205. if(target2)
  206. {
  207. if (!silent)
  208. {
  209. fout << "\techo " << directory << ": building " << target2 << "\n";
  210. }
  211. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  212. }
  213. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  214. fout << "\tcd " << cmSystemTools::ConvertToOutputPath(currentDir.c_str()) << "\n\n";
  215. }
  216. // This needs to be overriden because nmake requires commands to be quoted
  217. // if the are full paths to the executable????
  218. void cmLocalNMakeMakefileGenerator::OutputMakeRule(std::ostream& fout,
  219. const char* comment,
  220. const char* target,
  221. const char* depends,
  222. const char* command,
  223. const char* command2,
  224. const char* command3,
  225. const char* command4)
  226. {
  227. std::string short_command;
  228. if (command)
  229. {
  230. short_command = ShortPathCommand(command);
  231. command = short_command.c_str();
  232. }
  233. std::string short_command2;
  234. if (command2)
  235. {
  236. short_command2 = ShortPathCommand(command2);
  237. command2 = short_command2.c_str();
  238. }
  239. std::string short_command3;
  240. if (command3)
  241. {
  242. short_command3 = ShortPathCommand(command3);
  243. command3 = short_command3.c_str();
  244. }
  245. std::string short_command4;
  246. if (command4)
  247. {
  248. short_command4 = ShortPathCommand(command4);
  249. command4 = short_command4.c_str();
  250. }
  251. cmLocalUnixMakefileGenerator::OutputMakeRule(fout, comment,
  252. target, depends,
  253. command, command2,
  254. command3, command4);
  255. return;
  256. }
  257. void
  258. cmLocalNMakeMakefileGenerator::
  259. OutputBuildObjectFromSource(std::ostream& fout,
  260. const char* shortName,
  261. const cmSourceFile& source,
  262. const char* extraCompileFlags,
  263. bool shared)
  264. {
  265. // Header files shouldn't have build rules.
  266. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  267. {
  268. return;
  269. }
  270. std::string comment = "Build ";
  271. std::string objectFile = std::string(shortName) +
  272. this->GetOutputExtension(source.GetSourceExtension().c_str());
  273. comment += objectFile + " From ";
  274. comment += source.GetFullPath();
  275. std::string compileCommand;
  276. std::string ext = source.GetSourceExtension();
  277. if(ext == "c" )
  278. {
  279. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  280. compileCommand += extraCompileFlags;
  281. if(shared)
  282. {
  283. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  284. }
  285. compileCommand += "$(INCLUDE_FLAGS) -c ";
  286. compileCommand +=
  287. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  288. // Need to get the definition here because this value might have
  289. // trailing space (since it is directly prepended to the filename)
  290. std::string output_object_file_flag =
  291. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_OBJECT_FILE_FLAG");
  292. m_Makefile->ExpandVariablesInString(output_object_file_flag);
  293. compileCommand += " " + output_object_file_flag;
  294. compileCommand += objectFile;
  295. }
  296. else if (ext == "rc")
  297. {
  298. compileCommand = "$(RC) $(INCLUDE_FLAGS) /fo\"";
  299. compileCommand += objectFile;
  300. compileCommand += "\" ";
  301. compileCommand +=
  302. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  303. }
  304. else if (ext == "def")
  305. {
  306. // no rule to output for this one
  307. return;
  308. }
  309. // assume c++ if not c rc or def
  310. else
  311. {
  312. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  313. compileCommand += extraCompileFlags;
  314. if(shared)
  315. {
  316. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  317. }
  318. compileCommand += "$(INCLUDE_FLAGS) -c ";
  319. compileCommand +=
  320. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  321. // Need to get the definition here because this value might have
  322. // trailing space (since it is directly prepended to the filename)
  323. std::string output_object_file_flag =
  324. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_OBJECT_FILE_FLAG");
  325. m_Makefile->ExpandVariablesInString(output_object_file_flag);
  326. compileCommand += " " + output_object_file_flag;
  327. compileCommand += objectFile;
  328. }
  329. this->OutputMakeRule(fout,
  330. comment.c_str(),
  331. objectFile.c_str(),
  332. cmSystemTools::ConvertToOutputPath(
  333. source.GetFullPath().c_str()).c_str(),
  334. compileCommand.c_str());
  335. }
  336. void cmLocalNMakeMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  337. const char* name,
  338. const cmTarget &t)
  339. {
  340. std::string target = m_LibraryOutputPath + name + m_SharedLibraryExtension;
  341. std::string depend = "$(";
  342. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  343. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  344. // Need to get the definition here because this value might have
  345. // trailing space (since it is directly prepended to the filename)
  346. std::string linker_output_file_flag =
  347. m_Makefile->GetDefinition("CMAKE_LINKER_OUTPUT_FILE_FLAG");
  348. m_Makefile->ExpandVariablesInString(linker_output_file_flag);
  349. std::string command = "$(CMAKE_LINKER) $(CMAKE_LINKER_SHARED_LIBRARY_FLAG)";
  350. bool hide_param = m_Makefile->IsOn("CMAKE_LINKER_HIDE_PARAMETERS");
  351. if (hide_param)
  352. {
  353. command += " @<<\n\t";
  354. }
  355. command += " $(CMAKE_LINKER_FLAGS) " + linker_output_file_flag;
  356. std::string dllpath = m_LibraryOutputPath + std::string(name) + m_SharedLibraryExtension;
  357. command += cmSystemTools::ConvertToOutputPath(dllpath.c_str());
  358. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  359. cmOStringStream linklibs;
  360. this->OutputLinkLibraries(linklibs, name, t);
  361. command += linklibs.str();
  362. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  363. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  364. i != sources.end(); ++i)
  365. {
  366. if((*i)->GetSourceExtension() == "def")
  367. {
  368. command += "/DEF:";
  369. command += (*i)->GetFullPath();
  370. }
  371. }
  372. command += "\n";
  373. if (hide_param)
  374. {
  375. command += "<<\n";
  376. }
  377. std::string customCommands = this->CreateTargetRules(t, name);
  378. const char* cc = 0;
  379. if(customCommands.size() > 0)
  380. {
  381. cc = customCommands.c_str();
  382. }
  383. this->OutputMakeRule(fout, "rules for a shared library",
  384. target.c_str(),
  385. depend.c_str(),
  386. command.c_str(), cc);
  387. }
  388. void cmLocalNMakeMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  389. const char* name,
  390. const cmTarget &target)
  391. {
  392. this->OutputSharedLibraryRule(fout, name, target);
  393. }
  394. void cmLocalNMakeMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  395. const char* name,
  396. const cmTarget &t)
  397. {
  398. std::string target = m_LibraryOutputPath + std::string(name) + m_StaticLibraryExtension;
  399. std::string depend = "$(";
  400. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  401. // Need to get the definition here because this value might have
  402. // trailing space (since it is directly prepended to the filename)
  403. std::string library_manager_output_file_flag =
  404. m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER_OUTPUT_FILE_FLAG");
  405. m_Makefile->ExpandVariablesInString(library_manager_output_file_flag);
  406. std::string command = "$(CMAKE_LIBRARY_MANAGER) $(CMAKE_LIBRARY_MANAGER_FLAGS) @<<\n\t " + library_manager_output_file_flag;
  407. std::string libpath = m_LibraryOutputPath + std::string(name) + m_StaticLibraryExtension;
  408. command += cmSystemTools::ConvertToOutputPath(libpath.c_str());
  409. command += " $(";
  410. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  411. command += "\n<<\n";
  412. std::string comment = "rule to build static library: ";
  413. comment += name;
  414. std::string customCommands = this->CreateTargetRules(t, name);
  415. const char* cc = 0;
  416. if(customCommands.size() > 0)
  417. {
  418. cc = customCommands.c_str();
  419. }
  420. this->OutputMakeRule(fout,
  421. comment.c_str(),
  422. target.c_str(),
  423. depend.c_str(),
  424. command.c_str(), cc);
  425. }
  426. void cmLocalNMakeMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  427. const char* name,
  428. const cmTarget &t)
  429. {
  430. std::string target = m_ExecutableOutputPath + name;
  431. target += m_ExecutableExtension;
  432. std::string depend = "$(";
  433. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" +
  434. this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  435. std::string command;
  436. if(t.HasCxx())
  437. {
  438. command = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  439. }
  440. else
  441. {
  442. command = "${CMAKE_C_COMPILER} $(CMAKE_C_FLAGS) ";
  443. }
  444. bool hide_param = m_Makefile->IsOn("CMAKE_LINKER_HIDE_PARAMETERS");
  445. if (hide_param)
  446. {
  447. command += " @<<\n\t";
  448. }
  449. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  450. std::string path = m_ExecutableOutputPath + name + m_ExecutableExtension;
  451. // Need to get the definition here because this value might have
  452. // trailing space (since it is directly prepended to the filename)
  453. std::string output_executable_file_flag =
  454. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_EXECUTABLE_FILE_FLAG");
  455. m_Makefile->ExpandVariablesInString(output_executable_file_flag);
  456. command += " " + output_executable_file_flag +
  457. cmSystemTools::ConvertToOutputPath(path.c_str());
  458. command += " $(CMAKE_C_LINK_EXECUTABLE_FLAG) $(CMAKE_LINKER_FLAGS) ";
  459. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  460. {
  461. command += " /subsystem:windows ";
  462. }
  463. cmOStringStream linklibs;
  464. this->OutputLinkLibraries(linklibs, 0, t);
  465. command += linklibs.str();
  466. std::string comment = "rule to build executable: ";
  467. comment += name;
  468. std::string customCommands = this->CreateTargetRules(t, name);
  469. const char* cc = 0;
  470. if(customCommands.size() > 0)
  471. {
  472. cc = customCommands.c_str();
  473. }
  474. if (hide_param)
  475. {
  476. command += "\n";
  477. command += "<<\n";
  478. }
  479. this->OutputMakeRule(fout,
  480. comment.c_str(),
  481. target.c_str(),
  482. depend.c_str(),
  483. command.c_str(), cc);
  484. }
  485. void cmLocalNMakeMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  486. const char* targetLibrary,
  487. const cmTarget &tgt)
  488. {
  489. // Try to emit each search path once
  490. std::set<std::string> emitted;
  491. // Embed runtime search paths if possible and if required.
  492. // collect all the flags needed for linking libraries
  493. // Do not try if there is no library path option (it is set to -L or
  494. // -LIBPATH for some linker, but some others do not even support link
  495. // search path).
  496. std::string linkLibs;
  497. // Expand content because this value might have
  498. // trailing space (since it is directly prepended to the filename)
  499. std::string lib_path_opt = m_LibraryPathOption;
  500. m_Makefile->ExpandVariablesInString(lib_path_opt);
  501. if (lib_path_opt.size())
  502. {
  503. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  504. for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
  505. libDir != libdirs.end(); ++libDir)
  506. {
  507. std::string libpath = ShortPath(libDir->c_str());
  508. if(emitted.insert(libpath).second)
  509. {
  510. linkLibs += lib_path_opt;
  511. cmSystemTools::ConvertToOutputPath(libpath.c_str());
  512. linkLibs += libpath;
  513. linkLibs += " ";
  514. }
  515. }
  516. }
  517. std::string librariesLinked;
  518. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  519. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  520. lib != libs.end(); ++lib)
  521. {
  522. // Don't link the library against itself!
  523. if(targetLibrary && (lib->first == targetLibrary)) continue;
  524. // ** should fix this later, it should check to see if this is
  525. // a debug build and add the library
  526. // don't look at debug libraries
  527. // if (lib->second == cmTarget::DEBUG) continue;
  528. // skip zero size library entries, this may happen
  529. // if a variable expands to nothing.
  530. if (lib->first.size() == 0) continue;
  531. if(emitted.insert(lib->first).second)
  532. {
  533. std::string regexp = ".*\\";
  534. regexp += m_Makefile->GetDefinition("CMAKE_STATICLIB_SUFFIX");
  535. regexp += "$";
  536. cmRegularExpression reg(regexp.c_str());
  537. // if it ends in .lib, then it is a full path and should
  538. // be escaped, and does not need .lib added
  539. if(reg.find(lib->first))
  540. {
  541. librariesLinked += ShortPath(lib->first.c_str());
  542. librariesLinked += " ";
  543. }
  544. else
  545. {
  546. librariesLinked += m_LibraryLinkOption;
  547. librariesLinked += lib->first;
  548. librariesLinked += m_StaticLibraryExtension + " ";
  549. }
  550. }
  551. }
  552. linkLibs += librariesLinked;
  553. fout << linkLibs;
  554. fout << "$(CMAKE_STANDARD_WINDOWS_LIBRARIES) ";
  555. }
  556. std::string cmLocalNMakeMakefileGenerator::GetOutputExtension(const char* s)
  557. {
  558. std::string sourceExtension = s;
  559. if(sourceExtension == "def")
  560. {
  561. return "";
  562. }
  563. if(sourceExtension == "ico" || sourceExtension == "rc2")
  564. {
  565. return "";
  566. }
  567. if(sourceExtension == "rc")
  568. {
  569. return ".res";
  570. }
  571. return m_ObjectFileExtension;
  572. }
  573. void cmLocalNMakeMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  574. const char* file)
  575. {
  576. fout << "!include " << file << "\n";
  577. }
  578. bool cmLocalNMakeMakefileGenerator::SamePath(const char* path1, const char* path2)
  579. {
  580. // first check to see if they are the same anyway
  581. if (strcmp(path1, path2) == 0)
  582. {
  583. return true;
  584. }
  585. // next short path and lower case both of them for the compare
  586. return
  587. cmSystemTools::LowerCase(ShortPath(path1)) ==
  588. cmSystemTools::LowerCase(ShortPath(path2));
  589. }
  590. void cmLocalNMakeMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  591. const char* path,
  592. const char* library,
  593. const char* fullpath,
  594. const char* libOutPath)
  595. {
  596. const char* makeTarget = library;
  597. std::string currentDir =
  598. cmSystemTools::ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  599. std::string wpath = cmSystemTools::ConvertToOutputPath(path);
  600. std::string wfullpath = cmSystemTools::ConvertToOutputPath(fullpath);
  601. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  602. {
  603. makeTarget = wfullpath.c_str();
  604. }
  605. fout << wfullpath
  606. << ":\n\tcd " << wpath << "\n"
  607. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  608. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  609. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  610. << "\t$(MAKE) $(MAKESILENT) " << makeTarget
  611. << "\n\tcd " << currentDir << "\n";
  612. }
  613. std::string cmLocalNMakeMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  614. {
  615. std::string ret= std::string(s) + std::string(s2);
  616. cmSystemTools::ReplaceString(ret, "-", "_");
  617. return ret;
  618. }
  619. std::string cmLocalNMakeMakefileGenerator::LowerCasePath(const char* path)
  620. {
  621. return cmSystemTools::LowerCase(path);
  622. }