cmOutputConverter.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmOutputConverter_h
  11. #define cmOutputConverter_h
  12. #include <cmConfigure.h> // IWYU pragma: keep
  13. #include "cmState.h"
  14. #include <string>
  15. #include <vector>
  16. class cmOutputConverter
  17. {
  18. public:
  19. cmOutputConverter(cmState::Snapshot snapshot);
  20. /**
  21. * Convert something to something else. This is a centralized conversion
  22. * routine used by the generators to handle relative paths and the like.
  23. * The flags determine what is actually done.
  24. *
  25. * relative: treat the argument as a directory and convert it to make it
  26. * relative or full or unchanged. If relative (HOME, START etc) then that
  27. * specifies what it should be relative to.
  28. *
  29. * output: make the result suitable for output to a...
  30. *
  31. * optional: should any relative path operation be controlled by the rel
  32. * path setting
  33. */
  34. enum RelativeRoot
  35. {
  36. START_OUTPUT
  37. };
  38. enum OutputFormat
  39. {
  40. SHELL,
  41. WATCOMQUOTE,
  42. RESPONSE
  43. };
  44. std::string ConvertToOutputFormat(const std::string& source,
  45. OutputFormat output) const;
  46. std::string Convert(const std::string& remote, RelativeRoot local,
  47. OutputFormat output) const;
  48. std::string ConvertToRelativePath(const std::string& remote,
  49. RelativeRoot local) const;
  50. std::string ConvertDirectorySeparatorsForShell(
  51. const std::string& source) const;
  52. ///! for existing files convert to output path and short path if spaces
  53. std::string ConvertToOutputForExisting(const std::string& remote,
  54. OutputFormat format = SHELL) const;
  55. void SetLinkScriptShell(bool linkScriptShell);
  56. /**
  57. * Flags to pass to Shell_GetArgumentForWindows or
  58. * Shell_GetArgumentForUnix. These modify the generated
  59. * quoting and escape sequences to work under alternative
  60. * environments.
  61. */
  62. enum Shell_Flag_e
  63. {
  64. /** The target shell is in a makefile. */
  65. Shell_Flag_Make = (1 << 0),
  66. /** The target shell is in a VS project file. Do not use with
  67. Shell_Flag_Make. */
  68. Shell_Flag_VSIDE = (1 << 1),
  69. /** In a windows shell the argument is being passed to "echo". */
  70. Shell_Flag_EchoWindows = (1 << 2),
  71. /** The target shell is in a Watcom WMake makefile. */
  72. Shell_Flag_WatcomWMake = (1 << 3),
  73. /** The target shell is in a MinGW Make makefile. */
  74. Shell_Flag_MinGWMake = (1 << 4),
  75. /** The target shell is in a NMake makefile. */
  76. Shell_Flag_NMake = (1 << 5),
  77. /** Make variable reference syntax $(MAKEVAR) should not be escaped
  78. to allow a build tool to replace it. Replacement values
  79. containing spaces, quotes, backslashes, or other
  80. non-alphanumeric characters that have significance to some makes
  81. or shells produce undefined behavior. */
  82. Shell_Flag_AllowMakeVariables = (1 << 6),
  83. /** The target shell quoting uses extra single Quotes for Watcom tools. */
  84. Shell_Flag_WatcomQuote = (1 << 7)
  85. };
  86. /**
  87. * Transform the given command line argument for use in a Windows or
  88. * Unix shell. Returns a pointer to the end of the command line
  89. * argument in the provided output buffer. Flags may be passed to
  90. * modify the generated quoting and escape sequences to work under
  91. * alternative environments.
  92. */
  93. static std::string Shell_GetArgumentForWindows(const char* in, int flags);
  94. static std::string Shell_GetArgumentForUnix(const char* in, int flags);
  95. std::string EscapeForShell(const std::string& str, bool makeVars = false,
  96. bool forEcho = false,
  97. bool useWatcomQuote = false) const;
  98. static std::string EscapeForCMake(const std::string& str);
  99. /** Compute an escaped version of the given argument for use in a
  100. windows shell. */
  101. static std::string EscapeWindowsShellArgument(const char* arg,
  102. int shell_flags);
  103. enum FortranFormat
  104. {
  105. FortranFormatNone,
  106. FortranFormatFixed,
  107. FortranFormatFree
  108. };
  109. static FortranFormat GetFortranFormat(const char* value);
  110. /**
  111. * Convert the given remote path to a relative path with respect to
  112. * the given local path. The local path must be given in component
  113. * form (see SystemTools::SplitPath) without a trailing slash. The
  114. * remote path must use forward slashes and not already be escaped
  115. * or quoted.
  116. */
  117. std::string ConvertToRelativePath(const std::vector<std::string>& local,
  118. const std::string& in_remote,
  119. bool force = false) const;
  120. /**
  121. * Convert the given remote path to a relative path with respect to
  122. * the given local path. Both paths must use forward slashes and not
  123. * already be escaped or quoted.
  124. * The conversion is skipped if the paths are not both in the source
  125. * or both in the binary tree.
  126. */
  127. std::string ConvertToRelativePath(std::string const& local_path,
  128. std::string const& remote_path) const;
  129. /**
  130. * Convert the given remote path to a relative path with respect to
  131. * the given local path. Both paths must use forward slashes and not
  132. * already be escaped or quoted.
  133. */
  134. static std::string ForceToRelativePath(std::string const& local_path,
  135. std::string const& remote_path);
  136. private:
  137. cmState* GetState() const;
  138. static int Shell__CharIsWhitespace(char c);
  139. static int Shell__CharNeedsQuotesOnUnix(char c);
  140. static int Shell__CharNeedsQuotesOnWindows(char c);
  141. static int Shell__CharNeedsQuotes(char c, int isUnix, int flags);
  142. static int Shell__CharIsMakeVariableName(char c);
  143. static const char* Shell__SkipMakeVariables(const char* c);
  144. static int Shell__ArgumentNeedsQuotes(const char* in, int isUnix, int flags);
  145. static std::string Shell__GetArgument(const char* in, int isUnix, int flags);
  146. private:
  147. cmState::Snapshot StateSnapshot;
  148. bool LinkScriptShell;
  149. };
  150. #endif