cmNMakeMakefileGenerator.cxx 24 KB

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