cmBorlandMakefileGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. fout << "CMAKE_C_COMPILER = "
  90. << this->ConvertToOutputPath(ccompiler.c_str())
  91. << "\n";
  92. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  93. fout << "CMAKE_CXX_COMPILER = "
  94. << this->ConvertToOutputPath(cxxcompiler.c_str())
  95. << "\n";
  96. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  97. fout << "CMAKE_COMMAND = "
  98. << this->ConvertToOutputPath(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" <<
  116. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  117. for(i = includes.begin(); i != includes.end(); ++i)
  118. {
  119. std::string include = *i;
  120. // Don't output a -I for the standard include path "/usr/include".
  121. // This can cause problems with certain standard library
  122. // implementations because the wrong headers may be found first.
  123. fout << "-I" << this->ConvertToOutputPath(i->c_str()).c_str() << " ";
  124. }
  125. fout << m_Makefile->GetDefineFlags();
  126. fout << "\n\n";
  127. }
  128. void
  129. cmBorlandMakefileGenerator::
  130. OutputBuildObjectFromSource(std::ostream& fout,
  131. const char* shortName,
  132. const cmSourceFile& source,
  133. const char* extraCompileFlags,
  134. bool shared)
  135. {
  136. // force shared flag if building shared libraries
  137. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  138. {
  139. shared = true;
  140. }
  141. // Header files shouldn't have build rules.
  142. if(source.IsAHeaderFileOnly())
  143. return;
  144. std::string comment = "Build ";
  145. std::string objectFile = std::string(shortName) +
  146. this->GetOutputExtension(source.GetSourceExtension().c_str());
  147. objectFile = this->ConvertToOutputPath(objectFile.c_str());
  148. comment += objectFile + " From ";
  149. comment += source.GetFullPath();
  150. std::string compileCommand;
  151. std::string ext = source.GetSourceExtension();
  152. if(ext == "c" )
  153. {
  154. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  155. compileCommand += extraCompileFlags;
  156. if(shared)
  157. {
  158. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  159. }
  160. compileCommand += " -o";
  161. compileCommand += objectFile;
  162. compileCommand += " $(INCLUDE_FLAGS) -c ";
  163. compileCommand +=
  164. this->ConvertToOutputPath(source.GetFullPath().c_str());
  165. }
  166. else if (ext == "rc")
  167. {
  168. compileCommand = "$(RC) -o\"";
  169. compileCommand += objectFile;
  170. compileCommand += "\" ";
  171. compileCommand +=
  172. this->ConvertToOutputPath(source.GetFullPath().c_str());
  173. }
  174. else if (ext == "def")
  175. {
  176. // no rule to output for this one
  177. return;
  178. }
  179. // assume c++ if not c rc or def
  180. else
  181. {
  182. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  183. compileCommand += extraCompileFlags;
  184. if(shared)
  185. {
  186. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  187. }
  188. compileCommand += " -o";
  189. compileCommand += objectFile;
  190. compileCommand += " $(INCLUDE_FLAGS) -c ";
  191. compileCommand +=
  192. this->ConvertToOutputPath(source.GetFullPath().c_str());
  193. }
  194. this->OutputMakeRule(fout,
  195. comment.c_str(),
  196. objectFile.c_str(),
  197. this->ConvertToOutputPath(
  198. source.GetFullPath().c_str()).c_str(),
  199. compileCommand.c_str());
  200. }
  201. void cmBorlandMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  202. const char* name,
  203. const cmTarget &t)
  204. {
  205. std::string target = m_LibraryOutputPath + name;
  206. std::string libpath = target + ".lib";
  207. target += ".dll";
  208. target = this->ConvertToOutputPath(target.c_str());
  209. libpath = this->ConvertToOutputPath(libpath.c_str());
  210. std::string depend = "$(";
  211. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  212. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  213. std::string command = "$(CMAKE_CXX_COMPILER) -tWD $(CMAKE_SHLIB_CFLAGS) $(CMAKE_LINKER_FLAGS) @&&|\n";
  214. // must be executable name
  215. command += "-e";
  216. command += target;
  217. command += " ";
  218. std::strstream linklibs;
  219. this->OutputLinkLibraries(linklibs, name, t);
  220. linklibs << std::ends;
  221. // then the linker options -L and libraries (any other order will fail!)
  222. command += linklibs.str();
  223. delete [] linklibs.str();
  224. // then list of object files
  225. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  226. std::string command2 = "implib -w ";
  227. command2 += libpath + " " + target;
  228. const std::vector<cmSourceFile>& sources = t.GetSourceFiles();
  229. for(std::vector<cmSourceFile>::const_iterator i = sources.begin();
  230. i != sources.end(); ++i)
  231. {
  232. if(i->GetSourceExtension() == "def")
  233. {
  234. command += "";
  235. command += i->GetFullPath();
  236. }
  237. }
  238. command += "\n|\n";
  239. std::string customCommands = this->CreateTargetRules(t, name);
  240. const char* cc = 0;
  241. if(customCommands.size() > 0)
  242. {
  243. cc = customCommands.c_str();
  244. }
  245. this->OutputMakeRule(fout, "rules for a shared library",
  246. target.c_str(),
  247. depend.c_str(),
  248. command.c_str(),
  249. command2.c_str(),
  250. cc);
  251. }
  252. void cmBorlandMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  253. const char* name,
  254. const cmTarget &target)
  255. {
  256. this->OutputSharedLibraryRule(fout, name, target);
  257. }
  258. void cmBorlandMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  259. const char* name,
  260. const cmTarget &t)
  261. {
  262. std::string target = m_LibraryOutputPath + std::string(name) + ".lib";
  263. target = this->ConvertToOutputPath(target.c_str());
  264. std::string depend = "$(";
  265. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  266. std::string command = "tlib @&&|\n\t /p512 /a ";
  267. command += target;
  268. command += " ";
  269. std::string deleteCommand = "if exist ";
  270. deleteCommand += target;
  271. deleteCommand += " del ";
  272. deleteCommand += target;
  273. command += " $(";
  274. command += this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ")";
  275. command += "\n|\n";
  276. std::string comment = "rule to build static library: ";
  277. comment += name;
  278. std::string customCommands = this->CreateTargetRules(t, name);
  279. const char* cc = 0;
  280. if(customCommands.size() > 0)
  281. {
  282. cc = customCommands.c_str();
  283. }
  284. this->OutputMakeRule(fout,
  285. comment.c_str(),
  286. target.c_str(),
  287. depend.c_str(),
  288. deleteCommand.c_str(),
  289. command.c_str(), cc);
  290. }
  291. void cmBorlandMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  292. const char* name,
  293. const cmTarget &t)
  294. {
  295. std::string target = m_ExecutableOutputPath + name + m_ExecutableExtension;
  296. target = this->ConvertToOutputPath(target.c_str());
  297. std::string depend = "$(";
  298. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" +
  299. this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  300. std::string command =
  301. "$(CMAKE_CXX_COMPILER) ";
  302. command += " $(CMAKE_LINKER_FLAGS) -e" + target;
  303. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  304. {
  305. command += " $(CMAKE_SHLIB_CFLAGS) ";
  306. }
  307. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  308. {
  309. command += " -tWM ";
  310. }
  311. else
  312. {
  313. command += " -tWC ";
  314. }
  315. std::strstream linklibs;
  316. this->OutputLinkLibraries(linklibs, 0, t);
  317. linklibs << std::ends;
  318. command += linklibs.str();
  319. delete [] linklibs.str();
  320. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  321. std::string comment = "rule to build executable: ";
  322. comment += name;
  323. std::string customCommands = this->CreateTargetRules(t, name);
  324. const char* cc = 0;
  325. if(customCommands.size() > 0)
  326. {
  327. cc = customCommands.c_str();
  328. }
  329. this->OutputMakeRule(fout,
  330. comment.c_str(),
  331. target.c_str(),
  332. depend.c_str(),
  333. command.c_str(), cc);
  334. }
  335. std::string cmBorlandMakefileGenerator::GetOutputExtension(const char* s)
  336. {
  337. std::string sourceExtension = s;
  338. if(sourceExtension == "def")
  339. {
  340. return "";
  341. }
  342. if(sourceExtension == "ico" || sourceExtension == "rc2")
  343. {
  344. return "";
  345. }
  346. if(sourceExtension == "rc")
  347. {
  348. return ".res";
  349. }
  350. return ".obj";
  351. }
  352. bool cmBorlandMakefileGenerator::SamePath(const char* path1, const char* path2)
  353. {
  354. // first check to see if they are the same anyway
  355. if (strcmp(path1, path2) == 0)
  356. {
  357. return true;
  358. }
  359. // next short path and lower case both of them for the compare
  360. return
  361. cmSystemTools::LowerCase(ShortPath(path1)) ==
  362. cmSystemTools::LowerCase(ShortPath(path2));
  363. }
  364. // borland make does not support variables that are longer than 32
  365. // so use this function to rename any long ones
  366. std::string cmBorlandMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  367. {
  368. std::string unmodified = s;
  369. unmodified += s2;
  370. // see if th
  371. if(m_MakeVariableMap.count(unmodified))
  372. {
  373. return m_MakeVariableMap[unmodified];
  374. }
  375. std::string ret = unmodified;
  376. // if the string is greater the 32 chars it is an invalid vairable name
  377. // for borland make
  378. if(ret.size() > 32)
  379. {
  380. std::string str1 = s;
  381. std::string str2 = s2;
  382. // we must shorten the combined string by 4 charactors
  383. // keep no more than 24 charactors from the second string
  384. if(str2.size() > 24)
  385. {
  386. str2 = str2.substr(0, 24);
  387. }
  388. if(str1.size() + str2.size() > 27)
  389. {
  390. str1 = str1.substr(0, 27 - str2.size());
  391. }
  392. char buffer[5];
  393. int i = 0;
  394. sprintf(buffer, "%04d", i);
  395. ret = str1 + str2 + buffer;
  396. while(m_ShortMakeVariableMap.count(ret) && i < 1000)
  397. {
  398. ++i;
  399. sprintf(buffer, "%04d", i);
  400. ret = str1 + str2 + buffer;
  401. }
  402. if(i == 1000)
  403. {
  404. cmSystemTools::Error("Borland makefile varible length too long");
  405. return unmodified;
  406. }
  407. // once an unused variable is found
  408. m_ShortMakeVariableMap[ret] = "1";
  409. }
  410. // always make an entry into the unmodified to varible map
  411. m_MakeVariableMap[unmodified] = ret;
  412. return ret;
  413. }