summaryrefslogtreecommitdiffstats
path: root/Source/cmTryRunCommand.h
blob: 0a8648697f354fa15566ddf825f3cd58921b0515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef cmTryRunCommand_h
#define cmTryRunCommand_h

#include "cmCoreTryCompile.h"

/** \class cmTryRunCommand
 * \brief Specifies where to install some files
 *
 * cmTryRunCommand is used to test if soucre code can be compiled
 */
class cmTryRunCommand : public cmCoreTryCompile
{
public:
  /**
   * This is a virtual constructor for the command.
   */
  virtual cmCommand* Clone() 
    {
    return new cmTryRunCommand;
    }

  /**
   * This is called when the command is first encountered in
   * the CMakeLists.txt file.
   */
  virtual bool InitialPass(std::vector<std::string> const& args);

  /**
   * The name of the command as specified in CMakeList.txt.
   */
  virtual const char* GetName() { return "try_run";}

  /**
   * Succinct documentation.
   */
  virtual const char* GetTerseDocumentation() 
    {
    return "Try compiling and then running some code.";
    }
  
  /**
   * More documentation.
   */
  virtual const char* GetFullDocumentation()
    {
    return
      "  try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR\n"
      "          bindir srcfile [CMAKE_FLAGS <Flags>]\n"
      "          [COMPILE_DEFINITIONS <flags>]\n"
      "          [COMPILE_OUTPUT_VARIABLE comp]\n"
      "          [RUN_OUTPUT_VARIABLE run]\n"
      "          [OUTPUT_VARIABLE var]\n"
      "          [ARGS <arg1> <arg2>...])\n"
      "Try compiling a srcfile.  Return TRUE or FALSE for success or failure "
      "in COMPILE_RESULT_VAR.  Then if the compile succeeded, run the "
      "executable and return its exit code in RUN_RESULT_VAR. "
      "If the executable was built, but failed to run, then RUN_RESULT_VAR "
      "will be set to FAILED_TO_RUN. "
      "COMPILE_OUTPUT_VARIABLE specifies the variable where the output from "
      "the compile step goes. RUN_OUTPUT_VARIABLE specifies the variable "
      "where the output from the running executable goes.\n"
      "For compatibility reasons OUTPUT_VARIABLE is still supported, which "
      "gives you the output from the compile and run step combined.\n\n"
      "Cross compiling issues\n"
      "When cross compiling, the executable compiled in the first step "
      "usually cannot be run on the build host. try_run() checks the "
      "CMAKE_CROSSCOMPILING variable to detect whether CMake is in "
      "crosscompiling mode. If that's the case, it will still try to compile "
      "the executable, but it will not try to run the executable. Instead it "
      "will create cache variables which must be filled by the user or by "
      "presetting them in some CMake script file to the values the "
      "executable would have produced if it would have been run on its actual "
      "target platform. These variables are RUN_RESULT_VAR (explanation see "
      "above) and if RUN_OUTPUT_VARIABLE (or OUTPUT_VARIABLE) was used, an "
      "additional cache variable "
      "RUN_RESULT_VAR__COMPILE_RESULT_VAR__TRYRUN_OUTPUT."
      "This is intended to hold stdout and stderr from the executable.\n"
      "In order to make cross compiling your project easier, use try_run "
      "only if really required. If you use try_run, use RUN_OUTPUT_VARIABLE "
      "(or OUTPUT_VARIABLE) only if really required. Using them will require "
      "that when crosscompiling, the cache variables will have to be set "
      "manually to the output of the executable. You can also \"guard\" the "
      "calls to try_run with if(CMAKE_CROSSCOMPILING) and provide an "
      "easy-to-preset alternative for this case.\n";
    }

  cmTypeMacro(cmTryRunCommand, cmCoreTryCompile);
private:
  void RunExecutable(const std::string& runArgs, 
                     std::string* runOutputContents);
  void DoNotRunExecutable(const std::string& runArgs, 
                          const std::string& srcFile, 
                          std::string* runOutputContents);

  std::string CompileResultVariable;
  std::string RunResultVariable;
  std::string OutputVariable;
  std::string RunOutputVariable;
  std::string CompileOutputVariable;
};


#endif