cmOutputConverter.h 6.3 KB

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