1
0

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