cmRulePlaceholderExpander.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmRulePlaceholderExpander.h"
  4. #include <cctype>
  5. #include <cstring>
  6. #include <utility>
  7. #include "cmOutputConverter.h"
  8. #include "cmSystemTools.h"
  9. cmRulePlaceholderExpander::cmRulePlaceholderExpander(
  10. std::map<std::string, std::string> compilers,
  11. std::map<std::string, std::string> variableMappings,
  12. std::string compilerSysroot, std::string linkerSysroot)
  13. : Compilers(std::move(compilers))
  14. , VariableMappings(std::move(variableMappings))
  15. , CompilerSysroot(std::move(compilerSysroot))
  16. , LinkerSysroot(std::move(linkerSysroot))
  17. {
  18. }
  19. cmRulePlaceholderExpander::RuleVariables::RuleVariables()
  20. {
  21. memset(this, 0, sizeof(*this));
  22. }
  23. std::string cmRulePlaceholderExpander::ExpandRuleVariable(
  24. cmOutputConverter* outputConverter, std::string const& variable,
  25. const RuleVariables& replaceValues)
  26. {
  27. if (replaceValues.LinkFlags) {
  28. if (variable == "LINK_FLAGS") {
  29. return replaceValues.LinkFlags;
  30. }
  31. }
  32. if (replaceValues.Manifests) {
  33. if (variable == "MANIFESTS") {
  34. return replaceValues.Manifests;
  35. }
  36. }
  37. if (replaceValues.Flags) {
  38. if (variable == "FLAGS") {
  39. return replaceValues.Flags;
  40. }
  41. }
  42. if (replaceValues.Source) {
  43. if (variable == "SOURCE") {
  44. return replaceValues.Source;
  45. }
  46. }
  47. if (replaceValues.PreprocessedSource) {
  48. if (variable == "PREPROCESSED_SOURCE") {
  49. return replaceValues.PreprocessedSource;
  50. }
  51. }
  52. if (replaceValues.AssemblySource) {
  53. if (variable == "ASSEMBLY_SOURCE") {
  54. return replaceValues.AssemblySource;
  55. }
  56. }
  57. if (replaceValues.Object) {
  58. if (variable == "OBJECT") {
  59. return replaceValues.Object;
  60. }
  61. }
  62. if (replaceValues.ObjectDir) {
  63. if (variable == "OBJECT_DIR") {
  64. return replaceValues.ObjectDir;
  65. }
  66. }
  67. if (replaceValues.ObjectFileDir) {
  68. if (variable == "OBJECT_FILE_DIR") {
  69. return replaceValues.ObjectFileDir;
  70. }
  71. }
  72. if (replaceValues.Objects) {
  73. if (variable == "OBJECTS") {
  74. return replaceValues.Objects;
  75. }
  76. }
  77. if (replaceValues.ObjectsQuoted) {
  78. if (variable == "OBJECTS_QUOTED") {
  79. return replaceValues.ObjectsQuoted;
  80. }
  81. }
  82. if (replaceValues.AIXExports) {
  83. if (variable == "AIX_EXPORTS") {
  84. return replaceValues.AIXExports;
  85. }
  86. }
  87. if (replaceValues.Defines && variable == "DEFINES") {
  88. return replaceValues.Defines;
  89. }
  90. if (replaceValues.Includes && variable == "INCLUDES") {
  91. return replaceValues.Includes;
  92. }
  93. if (replaceValues.SwiftLibraryName) {
  94. if (variable == "SWIFT_LIBRARY_NAME") {
  95. return replaceValues.SwiftLibraryName;
  96. }
  97. }
  98. if (replaceValues.SwiftModule) {
  99. if (variable == "SWIFT_MODULE") {
  100. return replaceValues.SwiftModule;
  101. }
  102. }
  103. if (replaceValues.SwiftModuleName) {
  104. if (variable == "SWIFT_MODULE_NAME") {
  105. return replaceValues.SwiftModuleName;
  106. }
  107. }
  108. if (replaceValues.SwiftOutputFileMap) {
  109. if (variable == "SWIFT_OUTPUT_FILE_MAP") {
  110. return replaceValues.SwiftOutputFileMap;
  111. }
  112. }
  113. if (replaceValues.SwiftSources) {
  114. if (variable == "SWIFT_SOURCES") {
  115. return replaceValues.SwiftSources;
  116. }
  117. }
  118. if (replaceValues.TargetPDB) {
  119. if (variable == "TARGET_PDB") {
  120. return replaceValues.TargetPDB;
  121. }
  122. }
  123. if (replaceValues.TargetCompilePDB) {
  124. if (variable == "TARGET_COMPILE_PDB") {
  125. return replaceValues.TargetCompilePDB;
  126. }
  127. }
  128. if (replaceValues.DependencyFile) {
  129. if (variable == "DEP_FILE") {
  130. return replaceValues.DependencyFile;
  131. }
  132. }
  133. if (replaceValues.Target) {
  134. if (variable == "TARGET_QUOTED") {
  135. std::string targetQuoted = replaceValues.Target;
  136. if (!targetQuoted.empty() && targetQuoted.front() != '\"') {
  137. targetQuoted = '\"';
  138. targetQuoted += replaceValues.Target;
  139. targetQuoted += '\"';
  140. }
  141. return targetQuoted;
  142. }
  143. if (variable == "TARGET_UNQUOTED") {
  144. std::string unquoted = replaceValues.Target;
  145. std::string::size_type sz = unquoted.size();
  146. if (sz > 2 && unquoted.front() == '\"' && unquoted.back() == '\"') {
  147. unquoted = unquoted.substr(1, sz - 2);
  148. }
  149. return unquoted;
  150. }
  151. if (replaceValues.LanguageCompileFlags) {
  152. if (variable == "LANGUAGE_COMPILE_FLAGS") {
  153. return replaceValues.LanguageCompileFlags;
  154. }
  155. }
  156. if (replaceValues.Target) {
  157. if (variable == "TARGET") {
  158. return replaceValues.Target;
  159. }
  160. }
  161. if (variable == "TARGET_IMPLIB") {
  162. return this->TargetImpLib;
  163. }
  164. if (variable == "TARGET_VERSION_MAJOR") {
  165. if (replaceValues.TargetVersionMajor) {
  166. return replaceValues.TargetVersionMajor;
  167. }
  168. return "0";
  169. }
  170. if (variable == "TARGET_VERSION_MINOR") {
  171. if (replaceValues.TargetVersionMinor) {
  172. return replaceValues.TargetVersionMinor;
  173. }
  174. return "0";
  175. }
  176. if (replaceValues.Target) {
  177. if (variable == "TARGET_BASE") {
  178. // Strip the last extension off the target name.
  179. std::string targetBase = replaceValues.Target;
  180. std::string::size_type pos = targetBase.rfind('.');
  181. if (pos != std::string::npos) {
  182. return targetBase.substr(0, pos);
  183. }
  184. return targetBase;
  185. }
  186. }
  187. }
  188. if (variable == "TARGET_SONAME" || variable == "SONAME_FLAG" ||
  189. variable == "TARGET_INSTALLNAME_DIR") {
  190. // All these variables depend on TargetSOName
  191. if (replaceValues.TargetSOName) {
  192. if (variable == "TARGET_SONAME") {
  193. return replaceValues.TargetSOName;
  194. }
  195. if (variable == "SONAME_FLAG" && replaceValues.SONameFlag) {
  196. return replaceValues.SONameFlag;
  197. }
  198. if (replaceValues.TargetInstallNameDir &&
  199. variable == "TARGET_INSTALLNAME_DIR") {
  200. return replaceValues.TargetInstallNameDir;
  201. }
  202. }
  203. return "";
  204. }
  205. if (replaceValues.LinkLibraries) {
  206. if (variable == "LINK_LIBRARIES") {
  207. return replaceValues.LinkLibraries;
  208. }
  209. }
  210. if (replaceValues.Language) {
  211. if (variable == "LANGUAGE") {
  212. return replaceValues.Language;
  213. }
  214. }
  215. if (replaceValues.CMTargetName) {
  216. if (variable == "TARGET_NAME") {
  217. return replaceValues.CMTargetName;
  218. }
  219. }
  220. if (replaceValues.CMTargetType) {
  221. if (variable == "TARGET_TYPE") {
  222. return replaceValues.CMTargetType;
  223. }
  224. }
  225. if (replaceValues.Output) {
  226. if (variable == "OUTPUT") {
  227. return replaceValues.Output;
  228. }
  229. }
  230. if (variable == "CMAKE_COMMAND") {
  231. return outputConverter->ConvertToOutputFormat(
  232. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  233. }
  234. auto compIt = this->Compilers.find(variable);
  235. if (compIt != this->Compilers.end()) {
  236. std::string ret = outputConverter->ConvertToOutputForExisting(
  237. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER"]);
  238. std::string const& compilerArg1 =
  239. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER_ARG1"];
  240. std::string const& compilerTarget =
  241. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER_TARGET"];
  242. std::string const& compilerOptionTarget =
  243. this->VariableMappings["CMAKE_" + compIt->second +
  244. "_COMPILE_OPTIONS_TARGET"];
  245. std::string const& compilerExternalToolchain =
  246. this->VariableMappings["CMAKE_" + compIt->second +
  247. "_COMPILER_EXTERNAL_TOOLCHAIN"];
  248. std::string const& compilerOptionExternalToolchain =
  249. this->VariableMappings["CMAKE_" + compIt->second +
  250. "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN"];
  251. std::string const& compilerOptionSysroot =
  252. this->VariableMappings["CMAKE_" + compIt->second +
  253. "_COMPILE_OPTIONS_SYSROOT"];
  254. // if there is a required first argument to the compiler add it
  255. // to the compiler string
  256. if (!compilerArg1.empty()) {
  257. ret += " ";
  258. ret += compilerArg1;
  259. }
  260. if (!compilerTarget.empty() && !compilerOptionTarget.empty()) {
  261. ret += " ";
  262. ret += compilerOptionTarget;
  263. ret += compilerTarget;
  264. }
  265. if (!compilerExternalToolchain.empty() &&
  266. !compilerOptionExternalToolchain.empty()) {
  267. ret += " ";
  268. ret += compilerOptionExternalToolchain;
  269. ret += outputConverter->EscapeForShell(compilerExternalToolchain, true);
  270. }
  271. std::string sysroot;
  272. // Some platforms may use separate sysroots for compiling and linking.
  273. // If we detect link flags, then we pass the link sysroot instead.
  274. // FIXME: Use a more robust way to detect link line expansion.
  275. if (replaceValues.LinkFlags) {
  276. sysroot = this->LinkerSysroot;
  277. } else {
  278. sysroot = this->CompilerSysroot;
  279. }
  280. if (!sysroot.empty() && !compilerOptionSysroot.empty()) {
  281. ret += " ";
  282. ret += compilerOptionSysroot;
  283. ret += outputConverter->EscapeForShell(sysroot, true);
  284. }
  285. return ret;
  286. }
  287. auto mapIt = this->VariableMappings.find(variable);
  288. if (mapIt != this->VariableMappings.end()) {
  289. if (variable.find("_FLAG") == std::string::npos) {
  290. return outputConverter->ConvertToOutputForExisting(mapIt->second);
  291. }
  292. return mapIt->second;
  293. }
  294. return variable;
  295. }
  296. void cmRulePlaceholderExpander::ExpandRuleVariables(
  297. cmOutputConverter* outputConverter, std::string& s,
  298. const RuleVariables& replaceValues)
  299. {
  300. std::string::size_type start = s.find('<');
  301. // no variables to expand
  302. if (start == std::string::npos) {
  303. return;
  304. }
  305. std::string::size_type pos = 0;
  306. std::string expandedInput;
  307. while (start != std::string::npos && start < s.size() - 2) {
  308. std::string::size_type end = s.find('>', start);
  309. // if we find a < with no > we are done
  310. if (end == std::string::npos) {
  311. return;
  312. }
  313. char c = s[start + 1];
  314. // if the next char after the < is not A-Za-z then
  315. // skip it and try to find the next < in the string
  316. if (!isalpha(c)) {
  317. start = s.find('<', start + 1);
  318. } else {
  319. // extract the var
  320. std::string var = s.substr(start + 1, end - start - 1);
  321. std::string replace =
  322. this->ExpandRuleVariable(outputConverter, var, replaceValues);
  323. expandedInput += s.substr(pos, start - pos);
  324. expandedInput += replace;
  325. // move to next one
  326. start = s.find('<', start + var.size() + 2);
  327. pos = end + 1;
  328. }
  329. }
  330. // add the rest of the input
  331. expandedInput += s.substr(pos, s.size() - pos);
  332. s = expandedInput;
  333. }