cmOutputConverter.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 "cmOutputConverter.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <ctype.h>
  7. #include <set>
  8. #include <string.h>
  9. #include <vector>
  10. #include "cmState.h"
  11. #include "cmSystemTools.h"
  12. cmOutputConverter::cmOutputConverter(cmStateSnapshot const& snapshot)
  13. : StateSnapshot(snapshot)
  14. , LinkScriptShell(false)
  15. {
  16. assert(this->StateSnapshot.IsValid());
  17. }
  18. std::string cmOutputConverter::ConvertToOutputForExisting(
  19. const std::string& remote, OutputFormat format) const
  20. {
  21. // If this is a windows shell, the result has a space, and the path
  22. // already exists, we can use a short-path to reference it without a
  23. // space.
  24. if (this->GetState()->UseWindowsShell() &&
  25. remote.find(' ') != std::string::npos &&
  26. cmSystemTools::FileExists(remote)) {
  27. std::string tmp;
  28. if (cmSystemTools::GetShortPath(remote, tmp)) {
  29. return this->ConvertToOutputFormat(tmp, format);
  30. }
  31. }
  32. // Otherwise, perform standard conversion.
  33. return this->ConvertToOutputFormat(remote, format);
  34. }
  35. std::string cmOutputConverter::ConvertToOutputFormat(const std::string& source,
  36. OutputFormat output) const
  37. {
  38. std::string result = source;
  39. // Convert it to an output path.
  40. if (output == SHELL || output == WATCOMQUOTE) {
  41. result = this->ConvertDirectorySeparatorsForShell(source);
  42. result = this->EscapeForShell(result, true, false, output == WATCOMQUOTE);
  43. } else if (output == RESPONSE) {
  44. result = this->EscapeForShell(result, false, false, false);
  45. }
  46. return result;
  47. }
  48. std::string cmOutputConverter::ConvertDirectorySeparatorsForShell(
  49. const std::string& source) const
  50. {
  51. std::string result = source;
  52. // For the MSYS shell convert drive letters to posix paths, so
  53. // that c:/some/path becomes /c/some/path. This is needed to
  54. // avoid problems with the shell path translation.
  55. if (this->GetState()->UseMSYSShell() && !this->LinkScriptShell) {
  56. if (result.size() > 2 && result[1] == ':') {
  57. result[1] = result[0];
  58. result[0] = '/';
  59. }
  60. }
  61. if (this->GetState()->UseWindowsShell()) {
  62. std::replace(result.begin(), result.end(), '/', '\\');
  63. }
  64. return result;
  65. }
  66. static bool cmOutputConverterIsShellOperator(const std::string& str)
  67. {
  68. static std::set<std::string> shellOperators;
  69. if (shellOperators.empty()) {
  70. shellOperators.insert("<");
  71. shellOperators.insert(">");
  72. shellOperators.insert("<<");
  73. shellOperators.insert(">>");
  74. shellOperators.insert("|");
  75. shellOperators.insert("||");
  76. shellOperators.insert("&&");
  77. shellOperators.insert("&>");
  78. shellOperators.insert("1>");
  79. shellOperators.insert("2>");
  80. shellOperators.insert("2>&1");
  81. shellOperators.insert("1>&2");
  82. }
  83. return shellOperators.count(str) > 0;
  84. }
  85. std::string cmOutputConverter::EscapeForShell(const std::string& str,
  86. bool makeVars, bool forEcho,
  87. bool useWatcomQuote) const
  88. {
  89. // Do not escape shell operators.
  90. if (cmOutputConverterIsShellOperator(str)) {
  91. return str;
  92. }
  93. // Compute the flags for the target shell environment.
  94. int flags = 0;
  95. if (this->GetState()->UseWindowsVSIDE()) {
  96. flags |= Shell_Flag_VSIDE;
  97. } else if (!this->LinkScriptShell) {
  98. flags |= Shell_Flag_Make;
  99. }
  100. if (makeVars) {
  101. flags |= Shell_Flag_AllowMakeVariables;
  102. }
  103. if (forEcho) {
  104. flags |= Shell_Flag_EchoWindows;
  105. }
  106. if (useWatcomQuote) {
  107. flags |= Shell_Flag_WatcomQuote;
  108. }
  109. if (this->GetState()->UseWatcomWMake()) {
  110. flags |= Shell_Flag_WatcomWMake;
  111. }
  112. if (this->GetState()->UseMinGWMake()) {
  113. flags |= Shell_Flag_MinGWMake;
  114. }
  115. if (this->GetState()->UseNMake()) {
  116. flags |= Shell_Flag_NMake;
  117. }
  118. if (!this->GetState()->UseWindowsShell()) {
  119. flags |= Shell_Flag_IsUnix;
  120. }
  121. return Shell__GetArgument(str.c_str(), flags);
  122. }
  123. std::string cmOutputConverter::EscapeForCMake(const std::string& str)
  124. {
  125. // Always double-quote the argument to take care of most escapes.
  126. std::string result = "\"";
  127. for (const char* c = str.c_str(); *c; ++c) {
  128. if (*c == '"') {
  129. // Escape the double quote to avoid ending the argument.
  130. result += "\\\"";
  131. } else if (*c == '$') {
  132. // Escape the dollar to avoid expanding variables.
  133. result += "\\$";
  134. } else if (*c == '\\') {
  135. // Escape the backslash to avoid other escapes.
  136. result += "\\\\";
  137. } else {
  138. // Other characters will be parsed correctly.
  139. result += *c;
  140. }
  141. }
  142. result += "\"";
  143. return result;
  144. }
  145. std::string cmOutputConverter::EscapeWindowsShellArgument(const char* arg,
  146. int shell_flags)
  147. {
  148. return Shell__GetArgument(arg, shell_flags);
  149. }
  150. cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat(
  151. const char* value)
  152. {
  153. FortranFormat format = FortranFormatNone;
  154. if (value && *value) {
  155. std::vector<std::string> fmt;
  156. cmSystemTools::ExpandListArgument(value, fmt);
  157. for (std::string const& fi : fmt) {
  158. if (fi == "FIXED") {
  159. format = FortranFormatFixed;
  160. }
  161. if (fi == "FREE") {
  162. format = FortranFormatFree;
  163. }
  164. }
  165. }
  166. return format;
  167. }
  168. void cmOutputConverter::SetLinkScriptShell(bool linkScriptShell)
  169. {
  170. this->LinkScriptShell = linkScriptShell;
  171. }
  172. cmState* cmOutputConverter::GetState() const
  173. {
  174. return this->StateSnapshot.GetState();
  175. }
  176. /*
  177. Notes:
  178. Make variable replacements open a can of worms. Sometimes they should
  179. be quoted and sometimes not. Sometimes their replacement values are
  180. already quoted.
  181. VS variables cause problems. In order to pass the referenced value
  182. with spaces the reference must be quoted. If the variable value ends
  183. in a backslash then it will escape the ending quote! In order to make
  184. the ending backslash appear we need this:
  185. "$(InputDir)\"
  186. However if there is not a trailing backslash then this will put a
  187. quote in the value so we need:
  188. "$(InputDir)"
  189. Make variable references are platform specific so we should probably
  190. just NOT quote them and let the listfile author deal with it.
  191. */
  192. /*
  193. TODO: For windows echo:
  194. To display a pipe (|) or redirection character (< or >) when using the
  195. echo command, use a caret character immediately before the pipe or
  196. redirection character (for example, ^>, ^<, or ^| ). If you need to
  197. use the caret character itself (^), use two in a row (^^).
  198. */
  199. /* Some helpers to identify character classes */
  200. static int Shell__CharIsWhitespace(char c)
  201. {
  202. return ((c == ' ') || (c == '\t'));
  203. }
  204. static int Shell__CharNeedsQuotesOnUnix(char c)
  205. {
  206. return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') ||
  207. (c == '&') || (c == '$') || (c == '(') || (c == ')') || (c == '~') ||
  208. (c == '<') || (c == '>') || (c == '|') || (c == '*') || (c == '^') ||
  209. (c == '\\'));
  210. }
  211. static int Shell__CharNeedsQuotesOnWindows(char c)
  212. {
  213. return ((c == '\'') || (c == '#') || (c == '&') || (c == '<') ||
  214. (c == '>') || (c == '|') || (c == '^'));
  215. }
  216. static int Shell__CharIsMakeVariableName(char c)
  217. {
  218. return c && (c == '_' || isalpha((static_cast<int>(c))));
  219. }
  220. int cmOutputConverter::Shell__CharNeedsQuotes(char c, int flags)
  221. {
  222. /* On Windows the built-in command shell echo never needs quotes. */
  223. if (!(flags & Shell_Flag_IsUnix) && (flags & Shell_Flag_EchoWindows)) {
  224. return 0;
  225. }
  226. /* On all platforms quotes are needed to preserve whitespace. */
  227. if (Shell__CharIsWhitespace(c)) {
  228. return 1;
  229. }
  230. if (flags & Shell_Flag_IsUnix) {
  231. /* On UNIX several special characters need quotes to preserve them. */
  232. if (Shell__CharNeedsQuotesOnUnix(c)) {
  233. return 1;
  234. }
  235. } else {
  236. /* On Windows several special characters need quotes to preserve them. */
  237. if (Shell__CharNeedsQuotesOnWindows(c)) {
  238. return 1;
  239. }
  240. }
  241. return 0;
  242. }
  243. const char* cmOutputConverter::Shell__SkipMakeVariables(const char* c)
  244. {
  245. while (*c == '$' && *(c + 1) == '(') {
  246. const char* skip = c + 2;
  247. while (Shell__CharIsMakeVariableName(*skip)) {
  248. ++skip;
  249. }
  250. if (*skip == ')') {
  251. c = skip + 1;
  252. } else {
  253. break;
  254. }
  255. }
  256. return c;
  257. }
  258. /*
  259. Allowing make variable replacements opens a can of worms. Sometimes
  260. they should be quoted and sometimes not. Sometimes their replacement
  261. values are already quoted or contain escapes.
  262. Some Visual Studio variables cause problems. In order to pass the
  263. referenced value with spaces the reference must be quoted. If the
  264. variable value ends in a backslash then it will escape the ending
  265. quote! In order to make the ending backslash appear we need this:
  266. "$(InputDir)\"
  267. However if there is not a trailing backslash then this will put a
  268. quote in the value so we need:
  269. "$(InputDir)"
  270. This macro decides whether we quote an argument just because it
  271. contains a make variable reference. This should be replaced with a
  272. flag later when we understand applications of this better.
  273. */
  274. #define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0
  275. int cmOutputConverter::Shell__ArgumentNeedsQuotes(const char* in, int flags)
  276. {
  277. /* The empty string needs quotes. */
  278. if (!*in) {
  279. return 1;
  280. }
  281. /* Scan the string for characters that require quoting. */
  282. {
  283. const char* c;
  284. for (c = in; *c; ++c) {
  285. /* Look for $(MAKEVAR) syntax if requested. */
  286. if (flags & Shell_Flag_AllowMakeVariables) {
  287. #if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
  288. const char* skip = Shell__SkipMakeVariables(c);
  289. if (skip != c) {
  290. /* We need to quote make variable references to preserve the
  291. string with contents substituted in its place. */
  292. return 1;
  293. }
  294. #else
  295. /* Skip over the make variable references if any are present. */
  296. c = Shell__SkipMakeVariables(c);
  297. /* Stop if we have reached the end of the string. */
  298. if (!*c) {
  299. break;
  300. }
  301. #endif
  302. }
  303. /* Check whether this character needs quotes. */
  304. if (Shell__CharNeedsQuotes(*c, flags)) {
  305. return 1;
  306. }
  307. }
  308. }
  309. /* On Windows some single character arguments need quotes. */
  310. if (flags & Shell_Flag_IsUnix && *in && !*(in + 1)) {
  311. char c = *in;
  312. if ((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#')) {
  313. return 1;
  314. }
  315. }
  316. return 0;
  317. }
  318. std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags)
  319. {
  320. /* Output will be at least as long as input string. */
  321. std::string out;
  322. out.reserve(strlen(in));
  323. /* String iterator. */
  324. const char* c;
  325. /* Keep track of how many backslashes have been encountered in a row. */
  326. int windows_backslashes = 0;
  327. /* Whether the argument must be quoted. */
  328. int needQuotes = Shell__ArgumentNeedsQuotes(in, flags);
  329. if (needQuotes) {
  330. /* Add the opening quote for this argument. */
  331. if (flags & Shell_Flag_WatcomQuote) {
  332. if (flags & Shell_Flag_IsUnix) {
  333. out += '"';
  334. }
  335. out += '\'';
  336. } else {
  337. out += '"';
  338. }
  339. }
  340. /* Scan the string for characters that require escaping or quoting. */
  341. for (c = in; *c; ++c) {
  342. /* Look for $(MAKEVAR) syntax if requested. */
  343. if (flags & Shell_Flag_AllowMakeVariables) {
  344. const char* skip = Shell__SkipMakeVariables(c);
  345. if (skip != c) {
  346. /* Copy to the end of the make variable references. */
  347. while (c != skip) {
  348. out += *c++;
  349. }
  350. /* The make variable reference eliminates any escaping needed
  351. for preceding backslashes. */
  352. windows_backslashes = 0;
  353. /* Stop if we have reached the end of the string. */
  354. if (!*c) {
  355. break;
  356. }
  357. }
  358. }
  359. /* Check whether this character needs escaping for the shell. */
  360. if (flags & Shell_Flag_IsUnix) {
  361. /* On Unix a few special characters need escaping even inside a
  362. quoted argument. */
  363. if (*c == '\\' || *c == '"' || *c == '`' || *c == '$') {
  364. /* This character needs a backslash to escape it. */
  365. out += '\\';
  366. }
  367. } else if (flags & Shell_Flag_EchoWindows) {
  368. /* On Windows the built-in command shell echo never needs escaping. */
  369. } else {
  370. /* On Windows only backslashes and double-quotes need escaping. */
  371. if (*c == '\\') {
  372. /* Found a backslash. It may need to be escaped later. */
  373. ++windows_backslashes;
  374. } else if (*c == '"') {
  375. /* Found a double-quote. Escape all immediately preceding
  376. backslashes. */
  377. while (windows_backslashes > 0) {
  378. --windows_backslashes;
  379. out += '\\';
  380. }
  381. /* Add the backslash to escape the double-quote. */
  382. out += '\\';
  383. } else {
  384. /* We encountered a normal character. This eliminates any
  385. escaping needed for preceding backslashes. */
  386. windows_backslashes = 0;
  387. }
  388. }
  389. /* Check whether this character needs escaping for a make tool. */
  390. if (*c == '$') {
  391. if (flags & Shell_Flag_Make) {
  392. /* In Makefiles a dollar is written $$. The make tool will
  393. replace it with just $ before passing it to the shell. */
  394. out += "$$";
  395. } else if (flags & Shell_Flag_VSIDE) {
  396. /* In a VS IDE a dollar is written "$". If this is written in
  397. an un-quoted argument it starts a quoted segment, inserts
  398. the $ and ends the segment. If it is written in a quoted
  399. argument it ends quoting, inserts the $ and restarts
  400. quoting. Either way the $ is isolated from surrounding
  401. text to avoid looking like a variable reference. */
  402. out += "\"$\"";
  403. } else {
  404. /* Otherwise a dollar is written just $. */
  405. out += '$';
  406. }
  407. } else if (*c == '#') {
  408. if ((flags & Shell_Flag_Make) && (flags & Shell_Flag_WatcomWMake)) {
  409. /* In Watcom WMake makefiles a pound is written $#. The make
  410. tool will replace it with just # before passing it to the
  411. shell. */
  412. out += "$#";
  413. } else {
  414. /* Otherwise a pound is written just #. */
  415. out += '#';
  416. }
  417. } else if (*c == '%') {
  418. if ((flags & Shell_Flag_VSIDE) ||
  419. ((flags & Shell_Flag_Make) &&
  420. ((flags & Shell_Flag_MinGWMake) || (flags & Shell_Flag_NMake)))) {
  421. /* In the VS IDE, NMake, or MinGW make a percent is written %%. */
  422. out += "%%";
  423. } else {
  424. /* Otherwise a percent is written just %. */
  425. out += '%';
  426. }
  427. } else if (*c == ';') {
  428. if (flags & Shell_Flag_VSIDE) {
  429. /* In a VS IDE a semicolon is written ";". If this is written
  430. in an un-quoted argument it starts a quoted segment,
  431. inserts the ; and ends the segment. If it is written in a
  432. quoted argument it ends quoting, inserts the ; and restarts
  433. quoting. Either way the ; is isolated. */
  434. out += "\";\"";
  435. } else {
  436. /* Otherwise a semicolon is written just ;. */
  437. out += ';';
  438. }
  439. } else {
  440. /* Store this character. */
  441. out += *c;
  442. }
  443. }
  444. if (needQuotes) {
  445. /* Add enough backslashes to escape any trailing ones. */
  446. while (windows_backslashes > 0) {
  447. --windows_backslashes;
  448. out += '\\';
  449. }
  450. /* Add the closing quote for this argument. */
  451. if (flags & Shell_Flag_WatcomQuote) {
  452. out += '\'';
  453. if (flags & Shell_Flag_IsUnix) {
  454. out += '"';
  455. }
  456. } else {
  457. out += '"';
  458. }
  459. }
  460. return out;
  461. }