cmOutputConverter.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmOutputConverter_h
  4. #define cmOutputConverter_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <string>
  7. #include "cmStateSnapshot.h"
  8. class cmState;
  9. class cmStateDirectory;
  10. class cmOutputConverter
  11. {
  12. public:
  13. cmOutputConverter(cmStateSnapshot const& snapshot);
  14. enum OutputFormat
  15. {
  16. SHELL,
  17. WATCOMQUOTE,
  18. RESPONSE
  19. };
  20. std::string ConvertToOutputFormat(const std::string& source,
  21. OutputFormat output) const;
  22. std::string ConvertDirectorySeparatorsForShell(
  23. const std::string& source) const;
  24. ///! for existing files convert to output path and short path if spaces
  25. std::string ConvertToOutputForExisting(const std::string& remote,
  26. OutputFormat format = SHELL) const;
  27. void SetLinkScriptShell(bool linkScriptShell);
  28. /**
  29. * Flags to pass to Shell_GetArgument. These modify the generated
  30. * quoting and escape sequences to work under alternative
  31. * environments.
  32. */
  33. enum Shell_Flag_e
  34. {
  35. /** The target shell is in a makefile. */
  36. Shell_Flag_Make = (1 << 0),
  37. /** The target shell is in a VS project file. Do not use with
  38. Shell_Flag_Make. */
  39. Shell_Flag_VSIDE = (1 << 1),
  40. /** In a windows shell the argument is being passed to "echo". */
  41. Shell_Flag_EchoWindows = (1 << 2),
  42. /** The target shell is in a Watcom WMake makefile. */
  43. Shell_Flag_WatcomWMake = (1 << 3),
  44. /** The target shell is in a MinGW Make makefile. */
  45. Shell_Flag_MinGWMake = (1 << 4),
  46. /** The target shell is in a NMake makefile. */
  47. Shell_Flag_NMake = (1 << 5),
  48. /** Make variable reference syntax $(MAKEVAR) should not be escaped
  49. to allow a build tool to replace it. Replacement values
  50. containing spaces, quotes, backslashes, or other
  51. non-alphanumeric characters that have significance to some makes
  52. or shells produce undefined behavior. */
  53. Shell_Flag_AllowMakeVariables = (1 << 6),
  54. /** The target shell quoting uses extra single Quotes for Watcom tools. */
  55. Shell_Flag_WatcomQuote = (1 << 7),
  56. Shell_Flag_IsUnix = (1 << 8)
  57. };
  58. std::string EscapeForShell(const std::string& str, bool makeVars = false,
  59. bool forEcho = false,
  60. bool useWatcomQuote = false) const;
  61. static std::string EscapeForCMake(const std::string& str);
  62. /** Compute an escaped version of the given argument for use in a
  63. windows shell. */
  64. static std::string EscapeWindowsShellArgument(const char* arg,
  65. int shell_flags);
  66. enum FortranFormat
  67. {
  68. FortranFormatNone,
  69. FortranFormatFixed,
  70. FortranFormatFree
  71. };
  72. static FortranFormat GetFortranFormat(const char* value);
  73. static bool ContainedInDirectory(std::string const& local_path,
  74. std::string const& remote_path,
  75. cmStateDirectory const& directory);
  76. /**
  77. * Convert the given remote path to a relative path with respect to
  78. * the given local path. Both paths must use forward slashes and not
  79. * already be escaped or quoted.
  80. * The conversion is skipped if the paths are not both in the source
  81. * or both in the binary tree.
  82. */
  83. std::string ConvertToRelativePath(std::string const& local_path,
  84. std::string const& remote_path) const;
  85. /**
  86. * Convert the given remote path to a relative path with respect to
  87. * the given local path. Both paths must use forward slashes and not
  88. * already be escaped or quoted.
  89. */
  90. static std::string ForceToRelativePath(std::string const& local_path,
  91. std::string const& remote_path);
  92. private:
  93. cmState* GetState() const;
  94. static int Shell__CharIsWhitespace(char c);
  95. static int Shell__CharNeedsQuotesOnUnix(char c);
  96. static int Shell__CharNeedsQuotesOnWindows(char c);
  97. static int Shell__CharNeedsQuotes(char c, int flags);
  98. static int Shell__CharIsMakeVariableName(char c);
  99. static const char* Shell__SkipMakeVariables(const char* c);
  100. static int Shell__ArgumentNeedsQuotes(const char* in, int flags);
  101. static std::string Shell__GetArgument(const char* in, int flags);
  102. private:
  103. cmStateSnapshot StateSnapshot;
  104. bool LinkScriptShell;
  105. };
  106. #endif