cmWin32ProcessExecution.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmWin32ProcessExecution_h
  14. #define cmWin32ProcessExecution_h
  15. #include "cmStandardIncludes.h"
  16. #include "windows.h"
  17. class cmMakefile;
  18. /** \class cmWin32ProcessExecution
  19. * \brief A process executor for windows
  20. *
  21. * cmWin32ProcessExecution is a class that provides a "clean" way of
  22. * executing processes on Windows. It is modified code from Python 2.1
  23. * distribution.
  24. *
  25. * Portable 'popen' replacement for Win32.
  26. *
  27. * Written by Bill Tutt <[email protected]>. Minor tweaks and 2.0
  28. * integration by Fredrik Lundh <[email protected]> Return code
  29. * handling by David Bolen <[email protected]>.
  30. *
  31. * Modified for CMake.
  32. *
  33. * For more information, please check Microsoft Knowledge Base
  34. * Articles Q190351 and Q150956.
  35. */
  36. class cmWin32ProcessExecution
  37. {
  38. public:
  39. cmWin32ProcessExecution()
  40. {
  41. m_HideWindows = false;
  42. this->SetConsoleSpawn("w9xpopen.exe");
  43. this->Initialize();
  44. }
  45. ///! If true windows will be created hidden.
  46. void SetHideWindows(bool v) { m_HideWindows = v; }
  47. /**
  48. * Initialize the process execution datastructure. Do not call while
  49. * running the process.
  50. */
  51. void Initialize()
  52. {
  53. this->m_ProcessHandle = 0;
  54. this->m_ExitValue = -1;
  55. // Comment this out. Maybe we will need it in the future.
  56. // file IO access to the process might be cool.
  57. //this->m_StdIn = 0;
  58. //this->m_StdOut = 0;
  59. //this->m_StdErr = 0;
  60. this->m_pStdIn = -1;
  61. this->m_pStdOut = -1;
  62. this->m_pStdErr = -1;
  63. }
  64. /**
  65. * Start the process in the directory path. Make sure that the
  66. * executable is either in the path or specify the full path. The
  67. * argument verbose specifies wether or not to display output while
  68. * it is being generated.
  69. */
  70. bool StartProcess(const char*, const char* path, bool verbose);
  71. /**
  72. * Wait for the process to finish. If timeout is specified, it will
  73. * break the process after timeout expires. (Timeout code is not yet
  74. * implemented.
  75. */
  76. bool Wait(int timeout);
  77. /**
  78. * Get the output of the process (mixed stdout and stderr) as
  79. * std::string.
  80. */
  81. const std::string GetOutput() const { return this->m_Output; }
  82. /**
  83. * Get the return value of the process. If the process is still
  84. * running, the return value is -1.
  85. */
  86. int GetExitValue() const { return this->m_ExitValue; }
  87. /**
  88. * On Windows 9x there is a bug in the process execution code which
  89. * may result in blocking. That is why this workaround is
  90. * used. Specify the console spawn, which should run the
  91. * Windows9xHack code.
  92. */
  93. void SetConsoleSpawn(const char* prog) { this->m_ConsoleSpawn = prog; }
  94. static int Windows9xHack(const char* command);
  95. /** Code from a Borland web site with the following explaination :
  96. * In this article, I will explain how to spawn a console
  97. * application and redirect its standard input/output using
  98. * anonymous pipes. An anonymous pipe is a pipe that goes only in
  99. * one direction (read pipe, write pipe, etc.). Maybe you are
  100. * asking, "why would I ever need to do this sort of thing?" One
  101. * example would be a Windows telnet server, where you spawn a shell
  102. * and listen on a port and send and receive data between the shell
  103. * and the socket client. (Windows does not really have a built-in
  104. * remote shell). First, we should talk about pipes. A pipe in
  105. * Windows is simply a method of communication, often between
  106. * process. The SDK defines a pipe as "a communication conduit with
  107. * two ends; a process with a handle to one end can communicate with
  108. * a process having a handle to the other end." In our case, we are
  109. * using "anonymous" pipes, one-way pipes that "transfer data
  110. * between a parent process and a child process or between two child
  111. * processes of the same parent process." It's easiest to imagine a
  112. * pipe as its namesake. An actual pipe running between processes
  113. * that can carry data. We are using anonymous pipes because the
  114. * console app we are spawning is a child process. We use the
  115. * CreatePipe function which will create an anonymous pipe and
  116. * return a read handle and a write handle. We will create two
  117. * pipes, on for stdin and one for stdout. We will then monitor the
  118. * read end of the stdout pipe to check for display on our child
  119. * process. Every time there is something availabe for reading, we
  120. * will display it in our app. Consequently, we check for input in
  121. * our app and send it off to the write end of the stdin pipe.
  122. */
  123. static bool BorlandRunCommand(const char* command, const char* dir,
  124. std::string& output, int& retVal, bool verbose,
  125. int timeout, bool hideWindows);
  126. private:
  127. bool PrivateOpen(const char*, const char*, int, int);
  128. bool PrivateClose(int timeout);
  129. HANDLE m_ProcessHandle;
  130. // Comment this out. Maybe we will need it in the future.
  131. // file IO access to the process might be cool.
  132. // FILE* m_StdIn;
  133. // FILE* m_StdOut;
  134. // FILE* m_StdErr;
  135. int m_pStdIn;
  136. int m_pStdOut;
  137. int m_pStdErr;
  138. int m_ExitValue;
  139. std::string m_Output;
  140. std::string m_ConsoleSpawn;
  141. bool m_Verbose;
  142. bool m_HideWindows;
  143. };
  144. #endif