cmNMakeMakefileGenerator.cxx 24 KB

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