cmBorlandMakefileGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 "cmBorlandMakefileGenerator.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. #include <stdio.h>
  23. cmBorlandMakefileGenerator::cmBorlandMakefileGenerator()
  24. {
  25. this->SetLibraryPathOption("-L");
  26. this->SetLibraryLinkOption("");
  27. }
  28. cmBorlandMakefileGenerator::~cmBorlandMakefileGenerator()
  29. {
  30. }
  31. void cmBorlandMakefileGenerator::ComputeSystemInfo()
  32. {
  33. // now load the settings
  34. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  35. {
  36. cmSystemTools::Error(
  37. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  38. return;
  39. }
  40. std::string outdir = m_Makefile->GetCurrentOutputDirectory();
  41. if(outdir.find('-') != std::string::npos)
  42. {
  43. std::string message = "The Borland command line tools do not support path names that have - in them. Please re-name your output directory and use _ instead of -.";
  44. message += "\nYour path currently is: ";
  45. message += outdir;
  46. cmSystemTools::Error(message.c_str());
  47. }
  48. std::string fpath =
  49. m_Makefile->GetDefinition("CMAKE_ROOT");
  50. fpath += "/Templates/CMakeBorlandWindowsSystemConfig.cmake";
  51. m_Makefile->ReadListFile(NULL,fpath.c_str());
  52. }
  53. void cmBorlandMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  54. {
  55. fout << "# NMake Makefile generated by cmake\n";
  56. const char* variables =
  57. "# general varibles used in the makefile\n"
  58. "\n"
  59. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  60. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@ @BUILD_FLAGS@\n"
  61. "CMAKE_OBJECT_FILE_SUFFIX = @CMAKE_OBJECT_FILE_SUFFIX@\n"
  62. "CMAKE_EXECUTABLE_SUFFIX = @CMAKE_EXECUTABLE_SUFFIX@\n"
  63. "CMAKE_STATICLIB_SUFFIX = @CMAKE_STATICLIB_SUFFIX@\n"
  64. "CMAKE_SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  65. "CMAKE_SHLIB_CFLAGS = -tWR\n"
  66. "CMAKE_LINKER_FLAGS = @CMAKE_LINKER_FLAGS@ @LINKER_BUILD_FLAGS@\n"
  67. "CMAKE_CXX_FLAGS = -P @CMAKE_CXX_FLAGS@ @BUILD_FLAGS@\n"
  68. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  69. "NULL=\n"
  70. "!ELSE \n"
  71. "NULL=nul\n"
  72. "!ENDIF \n"
  73. "RM = del\n";
  74. std::string buildType = "CMAKE_CXX_FLAGS_";
  75. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  76. buildType = cmSystemTools::UpperCase(buildType);
  77. m_Makefile->AddDefinition("BUILD_FLAGS",
  78. m_Makefile->GetDefinition(
  79. buildType.c_str()));
  80. buildType = "CMAKE_LINKER_FLAGS_";
  81. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  82. buildType = cmSystemTools::UpperCase(buildType);
  83. m_Makefile->AddDefinition("LINKER_BUILD_FLAGS",
  84. m_Makefile->GetDefinition(
  85. buildType.c_str()));
  86. std::string replaceVars = variables;
  87. m_Makefile->ExpandVariablesInString(replaceVars);
  88. std::string ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER");
  89. cmSystemTools::ConvertToWindowsSlashes(ccompiler);
  90. fout << "CMAKE_C_COMPILER = " << cmSystemTools::EscapeSpaces(ccompiler.c_str())
  91. << "\n";
  92. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  93. cmSystemTools::ConvertToWindowsSlashes(cxxcompiler);
  94. fout << "CMAKE_CXX_COMPILER = " << cmSystemTools::EscapeSpaces(cxxcompiler.c_str())
  95. << "\n";
  96. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  97. cmSystemTools::ConvertToWindowsSlashes(cmakecommand);
  98. fout << "CMAKE_COMMAND = " << cmSystemTools::EscapeSpaces(cmakecommand.c_str()) << "\n";
  99. fout << replaceVars.c_str();
  100. fout << "CMAKE_CURRENT_SOURCE = "
  101. << ShortPath(m_Makefile->GetStartDirectory() )
  102. << "\n";
  103. fout << "CMAKE_CURRENT_BINARY = "
  104. << ShortPath(m_Makefile->GetStartOutputDirectory())
  105. << "\n";
  106. fout << "CMAKE_SOURCE_DIR = "
  107. << ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  108. fout << "CMAKE_BINARY_DIR = "
  109. << ShortPath(m_Makefile->GetHomeOutputDirectory() )
  110. << "\n";
  111. // Output Include paths
  112. fout << "INCLUDE_FLAGS = ";
  113. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  114. std::vector<std::string>::iterator i;
  115. fout << "-I" << cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << " ";
  116. for(i = includes.begin(); i != includes.end(); ++i)
  117. {
  118. std::string include = *i;
  119. // Don't output a -I for the standard include path "/usr/include".
  120. // This can cause problems with certain standard library
  121. // implementations because the wrong headers may be found first.
  122. if(include != "/usr/include")
  123. {
  124. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  125. }
  126. }
  127. fout << m_Makefile->GetDefineFlags();
  128. fout << "\n\n";
  129. }
  130. void
  131. cmBorlandMakefileGenerator::
  132. OutputBuildObjectFromSource(std::ostream& fout,
  133. const char* shortName,
  134. const cmSourceFile& source,
  135. const char* extraCompileFlags,
  136. bool shared)
  137. {
  138. // force shared flag if building shared libraries
  139. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  140. {
  141. shared = true;
  142. }
  143. // Header files shouldn't have build rules.
  144. if(source.IsAHeaderFileOnly())
  145. return;
  146. std::string comment = "Build ";
  147. std::string objectFile = std::string(shortName) +
  148. this->GetOutputExtension(source.GetSourceExtension().c_str());
  149. cmSystemTools::ConvertToWindowsSlashes(objectFile);
  150. comment += objectFile + " From ";
  151. comment += source.GetFullPath();
  152. std::string compileCommand;
  153. std::string ext = source.GetSourceExtension();
  154. if(ext == "c" )
  155. {
  156. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  157. compileCommand += extraCompileFlags;
  158. if(shared)
  159. {
  160. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  161. }
  162. compileCommand += " -o";
  163. compileCommand += objectFile;
  164. compileCommand += " $(INCLUDE_FLAGS) -c ";
  165. compileCommand +=
  166. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  167. }
  168. else if (ext == "rc")
  169. {
  170. compileCommand = "$(RC) -o\"";
  171. compileCommand += objectFile;
  172. compileCommand += "\" ";
  173. compileCommand +=
  174. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  175. }
  176. else if (ext == "def")
  177. {
  178. // no rule to output for this one
  179. return;
  180. }
  181. // assume c++ if not c rc or def
  182. else
  183. {
  184. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  185. compileCommand += extraCompileFlags;
  186. if(shared)
  187. {
  188. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  189. }
  190. compileCommand += " -o";
  191. compileCommand += objectFile;
  192. compileCommand += " $(INCLUDE_FLAGS) -c ";
  193. compileCommand +=
  194. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  195. }
  196. this->OutputMakeRule(fout,
  197. comment.c_str(),
  198. objectFile.c_str(),
  199. cmSystemTools::EscapeSpaces(
  200. source.GetFullPath().c_str()).c_str(),
  201. compileCommand.c_str());
  202. }
  203. void cmBorlandMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  204. const char* name,
  205. const cmTarget &t)
  206. {
  207. std::string target = m_LibraryOutputPath + name;
  208. std::string libpath = target + ".lib";
  209. target += ".dll";
  210. cmSystemTools::ConvertToWindowsSlashes(libpath);
  211. cmSystemTools::ConvertToWindowsSlashes(target);
  212. target = cmSystemTools::EscapeSpaces(target.c_str());
  213. libpath = cmSystemTools::EscapeSpaces(libpath.c_str());
  214. std::string depend = "$(";
  215. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  216. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  217. std::string command = "$(CMAKE_CXX_COMPILER) -tWD $(CMAKE_SHLIB_CFLAGS) $(CMAKE_LINKER_FLAGS) @&&|\n";
  218. // must be executable name
  219. command += "-e";
  220. command += target;
  221. command += " ";
  222. std::strstream linklibs;
  223. this->OutputLinkLibraries(linklibs, name, t);
  224. linklibs << std::ends;
  225. // then the linker options -L and libraries (any other order will fail!)
  226. command += linklibs.str();
  227. delete [] linklibs.str();
  228. // then list of object files
  229. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  230. std::string command2 = "implib -w ";
  231. command2 += libpath + " " + target;
  232. const std::vector<cmSourceFile>& sources = t.GetSourceFiles();
  233. for(std::vector<cmSourceFile>::const_iterator i = sources.begin();
  234. i != sources.end(); ++i)
  235. {
  236. if(i->GetSourceExtension() == "def")
  237. {
  238. command += "";
  239. command += i->GetFullPath();
  240. }
  241. }
  242. command += "\n|\n";
  243. std::string customCommands = this->CreateTargetRules(t, name);
  244. const char* cc = 0;
  245. if(customCommands.size() > 0)
  246. {
  247. cc = customCommands.c_str();
  248. }
  249. this->OutputMakeRule(fout, "rules for a shared library",
  250. target.c_str(),
  251. depend.c_str(),
  252. command.c_str(),
  253. command2.c_str(),
  254. cc);
  255. }
  256. void cmBorlandMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  257. const char* name,
  258. const cmTarget &target)
  259. {
  260. this->OutputSharedLibraryRule(fout, name, target);
  261. }
  262. void cmBorlandMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  263. const char* name,
  264. const cmTarget &t)
  265. {
  266. std::string target = m_LibraryOutputPath + std::string(name) + ".lib";
  267. cmSystemTools::ConvertToWindowsSlashes(target);
  268. target = cmSystemTools::EscapeSpaces(target.c_str());
  269. std::string depend = "$(";
  270. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  271. std::string command = "tlib @&&|\n\t /p512 /a ";
  272. command += target;
  273. command += " ";
  274. std::string deleteCommand = "if exist ";
  275. deleteCommand += target;
  276. deleteCommand += " del ";
  277. deleteCommand += target;
  278. command += " $(";
  279. command += this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ")";
  280. command += "\n|\n";
  281. std::string comment = "rule to build static library: ";
  282. comment += name;
  283. std::string customCommands = this->CreateTargetRules(t, name);
  284. const char* cc = 0;
  285. if(customCommands.size() > 0)
  286. {
  287. cc = customCommands.c_str();
  288. }
  289. this->OutputMakeRule(fout,
  290. comment.c_str(),
  291. target.c_str(),
  292. depend.c_str(),
  293. deleteCommand.c_str(),
  294. command.c_str(), cc);
  295. }
  296. void cmBorlandMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  297. const char* name,
  298. const cmTarget &t)
  299. {
  300. std::string target = m_ExecutableOutputPath + name + m_ExecutableExtension;
  301. cmSystemTools::ConvertToWindowsSlashes(target);
  302. target = cmSystemTools::EscapeSpaces(target.c_str());
  303. std::string depend = "$(";
  304. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" +
  305. this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  306. std::string command =
  307. "$(CMAKE_CXX_COMPILER) ";
  308. command += " $(CMAKE_LINKER_FLAGS) -e" + target;
  309. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  310. {
  311. command += " $(CMAKE_SHLIB_CFLAGS) ";
  312. }
  313. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  314. {
  315. command += " -tWM ";
  316. }
  317. else
  318. {
  319. command += " -tWC ";
  320. }
  321. std::strstream linklibs;
  322. this->OutputLinkLibraries(linklibs, 0, t);
  323. linklibs << std::ends;
  324. command += linklibs.str();
  325. delete [] linklibs.str();
  326. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  327. std::string comment = "rule to build executable: ";
  328. comment += name;
  329. std::string customCommands = this->CreateTargetRules(t, name);
  330. const char* cc = 0;
  331. if(customCommands.size() > 0)
  332. {
  333. cc = customCommands.c_str();
  334. }
  335. this->OutputMakeRule(fout,
  336. comment.c_str(),
  337. target.c_str(),
  338. depend.c_str(),
  339. command.c_str(), cc);
  340. }
  341. std::string cmBorlandMakefileGenerator::GetOutputExtension(const char* s)
  342. {
  343. std::string sourceExtension = s;
  344. if(sourceExtension == "def")
  345. {
  346. return "";
  347. }
  348. if(sourceExtension == "ico" || sourceExtension == "rc2")
  349. {
  350. return "";
  351. }
  352. if(sourceExtension == "rc")
  353. {
  354. return ".res";
  355. }
  356. return ".obj";
  357. }
  358. bool cmBorlandMakefileGenerator::SamePath(const char* path1, const char* path2)
  359. {
  360. // first check to see if they are the same anyway
  361. if (strcmp(path1, path2) == 0)
  362. {
  363. return true;
  364. }
  365. // next short path and lower case both of them for the compare
  366. return
  367. cmSystemTools::LowerCase(ShortPath(path1)) ==
  368. cmSystemTools::LowerCase(ShortPath(path2));
  369. }
  370. // borland make does not support variables that are longer than 32
  371. // so use this function to rename any long ones
  372. std::string cmBorlandMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  373. {
  374. std::string unmodified = s;
  375. unmodified += s2;
  376. // see if th
  377. if(m_MakeVariableMap.count(unmodified))
  378. {
  379. return m_MakeVariableMap[unmodified];
  380. }
  381. std::string ret = unmodified;
  382. // if the string is greater the 32 chars it is an invalid vairable name
  383. // for borland make
  384. if(ret.size() > 32)
  385. {
  386. std::string str1 = s;
  387. std::string str2 = s2;
  388. // we must shorten the combined string by 4 charactors
  389. // keep no more than 24 charactors from the second string
  390. if(str2.size() > 24)
  391. {
  392. str2 = str2.substr(0, 24);
  393. }
  394. if(str1.size() + str2.size() > 27)
  395. {
  396. str1 = str1.substr(0, 27 - str2.size());
  397. }
  398. char buffer[5];
  399. int i = 0;
  400. sprintf(buffer, "%04d", i);
  401. ret = str1 + str2 + buffer;
  402. while(m_ShortMakeVariableMap.count(ret) && i < 1000)
  403. {
  404. ++i;
  405. sprintf(buffer, "%04d", i);
  406. ret = str1 + str2 + buffer;
  407. }
  408. if(i == 1000)
  409. {
  410. cmSystemTools::Error("Borland makefile varible length too long");
  411. return unmodified;
  412. }
  413. // once an unused variable is found
  414. m_ShortMakeVariableMap[ret] = "1";
  415. }
  416. // always make an entry into the unmodified to varible map
  417. m_MakeVariableMap[unmodified] = ret;
  418. return ret;
  419. }