cmTryRunCommand.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 cmTryRunCommand_h
  11. #define cmTryRunCommand_h
  12. #include "cmCoreTryCompile.h"
  13. /** \class cmTryRunCommand
  14. * \brief Specifies where to install some files
  15. *
  16. * cmTryRunCommand is used to test if soucre code can be compiled
  17. */
  18. class cmTryRunCommand : public cmCoreTryCompile
  19. {
  20. public:
  21. /**
  22. * This is a virtual constructor for the command.
  23. */
  24. virtual cmCommand* Clone()
  25. {
  26. return new cmTryRunCommand;
  27. }
  28. /**
  29. * This is called when the command is first encountered in
  30. * the CMakeLists.txt file.
  31. */
  32. virtual bool InitialPass(std::vector<std::string> const& args,
  33. cmExecutionStatus &status);
  34. /**
  35. * The name of the command as specified in CMakeList.txt.
  36. */
  37. virtual const char* GetName() const { return "try_run";}
  38. /**
  39. * Succinct documentation.
  40. */
  41. virtual const char* GetTerseDocumentation() const
  42. {
  43. return "Try compiling and then running some code.";
  44. }
  45. /**
  46. * More documentation.
  47. */
  48. virtual const char* GetFullDocumentation() const
  49. {
  50. return
  51. " try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR\n"
  52. " bindir srcfile [CMAKE_FLAGS <Flags>]\n"
  53. " [COMPILE_DEFINITIONS <flags>]\n"
  54. " [COMPILE_OUTPUT_VARIABLE comp]\n"
  55. " [RUN_OUTPUT_VARIABLE run]\n"
  56. " [OUTPUT_VARIABLE var]\n"
  57. " [ARGS <arg1> <arg2>...])\n"
  58. "Try compiling a srcfile. Return TRUE or FALSE for success or failure "
  59. "in COMPILE_RESULT_VAR. Then if the compile succeeded, run the "
  60. "executable and return its exit code in RUN_RESULT_VAR. "
  61. "If the executable was built, but failed to run, then RUN_RESULT_VAR "
  62. "will be set to FAILED_TO_RUN. "
  63. "COMPILE_OUTPUT_VARIABLE specifies the variable where the output from "
  64. "the compile step goes. RUN_OUTPUT_VARIABLE specifies the variable "
  65. "where the output from the running executable goes.\n"
  66. "For compatibility reasons OUTPUT_VARIABLE is still supported, which "
  67. "gives you the output from the compile and run step combined.\n"
  68. "Cross compiling issues\n"
  69. "When cross compiling, the executable compiled in the first step "
  70. "usually cannot be run on the build host. try_run() checks the "
  71. "CMAKE_CROSSCOMPILING variable to detect whether CMake is in "
  72. "crosscompiling mode. If that's the case, it will still try to compile "
  73. "the executable, but it will not try to run the executable. Instead it "
  74. "will create cache variables which must be filled by the user or by "
  75. "presetting them in some CMake script file to the values the "
  76. "executable would have produced if it had been run on its actual "
  77. "target platform. These variables are RUN_RESULT_VAR (explanation see "
  78. "above) and if RUN_OUTPUT_VARIABLE (or OUTPUT_VARIABLE) was used, an "
  79. "additional cache variable "
  80. "RUN_RESULT_VAR__COMPILE_RESULT_VAR__TRYRUN_OUTPUT."
  81. "This is intended to hold stdout and stderr from the executable.\n"
  82. "In order to make cross compiling your project easier, use try_run "
  83. "only if really required. If you use try_run, use RUN_OUTPUT_VARIABLE "
  84. "(or OUTPUT_VARIABLE) only if really required. Using them will require "
  85. "that when crosscompiling, the cache variables will have to be set "
  86. "manually to the output of the executable. You can also \"guard\" the "
  87. "calls to try_run with if(CMAKE_CROSSCOMPILING) and provide an "
  88. "easy-to-preset alternative for this case.\n"
  89. "Set variable CMAKE_TRY_COMPILE_CONFIGURATION to choose a build "
  90. "configuration."
  91. ;
  92. }
  93. cmTypeMacro(cmTryRunCommand, cmCoreTryCompile);
  94. private:
  95. void RunExecutable(const std::string& runArgs,
  96. std::string* runOutputContents);
  97. void DoNotRunExecutable(const std::string& runArgs,
  98. const std::string& srcFile,
  99. std::string* runOutputContents);
  100. std::string CompileResultVariable;
  101. std::string RunResultVariable;
  102. std::string OutputVariable;
  103. std::string RunOutputVariable;
  104. std::string CompileOutputVariable;
  105. };
  106. #endif