cmTryCompileCommand.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 cmTryCompileCommand_h
  14. #define cmTryCompileCommand_h
  15. #include "cmCommand.h"
  16. /** \class cmTryCompileCommand
  17. * \brief Specifies where to install some files
  18. *
  19. * cmTryCompileCommand is used to test if soucre code can be compiled
  20. */
  21. class cmTryCompileCommand : public cmCommand
  22. {
  23. public:
  24. /**
  25. * This is a virtual constructor for the command.
  26. */
  27. virtual cmCommand* Clone()
  28. {
  29. return new cmTryCompileCommand;
  30. }
  31. /**
  32. * This is called when the command is first encountered in
  33. * the CMakeLists.txt file.
  34. */
  35. virtual bool InitialPass(std::vector<std::string> const& args);
  36. /**
  37. * The name of the command as specified in CMakeList.txt.
  38. */
  39. virtual const char* GetName() { return "TRY_COMPILE";}
  40. /**
  41. * Succinct documentation.
  42. */
  43. virtual const char* GetTerseDocumentation()
  44. {
  45. return "Try compiling some code.";
  46. }
  47. /**
  48. * This is the core code for try compile. It is here so that other
  49. * commands, such as TryRun can access the same logic without
  50. * duplication.
  51. */
  52. static int CoreTryCompileCode(cmMakefile *mf,
  53. std::vector<std::string> const& argv,
  54. bool clean,
  55. const char* cmakeCommand,
  56. std::string& outputFile);
  57. /**
  58. * This deletes all the files created by TRY_COMPILE or TRY_RUN
  59. * code. This way we do not have to rely on the timing and
  60. * dependencies of makefiles.
  61. */
  62. static void CleanupFiles(const char* binDir);
  63. /**
  64. * This tries to find the (executable) file created by TRY_COMPILE or
  65. * TRY_RUN. If nothing is found an empty string will be returned.
  66. */
  67. static const char* GetOutputFile(cmMakefile* mf, const char* binaryDirectory,
  68. const char* targetName, const char* cmakeCommand,
  69. std::string& errorMessage);
  70. /**
  71. * More documentation. */
  72. virtual const char* GetFullDocumentation()
  73. {
  74. return
  75. " TRY_COMPILE(RESULT_VAR bindir srcdir\n"
  76. " projectName <targetname> <CMAKE_FLAGS <Flags>>\n"
  77. " <OUTPUT_VARIABLE var>)\n"
  78. "Try compiling a program. In this form, srcdir should contain a complete "
  79. "CMake project with a CMakeLists.txt file and all sources. The bindir and "
  80. "srcdir will not be deleted after this command is run. "
  81. "If <target name> is specified then build just that target "
  82. "otherwise the all or ALL_BUILD target is built.\n"
  83. " TRY_COMPILE(RESULT_VAR bindir srcfile\n"
  84. " <CMAKE_FLAGS <Flags>>\n"
  85. " <COMPILE_DEFINITIONS <flags> ...>\n"
  86. " <OUTPUT_VARIABLE var>)\n"
  87. "Try compiling a srcfile. In this case, the user need only supply a "
  88. "source file. CMake will create the appropriate CMakeLists.txt file "
  89. "to build the source. "
  90. "In this version all files in bindir/CMakeFiles/CMakeTmp, "
  91. "will be cleaned automatically, for debugging a --debug-trycompile can "
  92. "be passed to cmake to avoid the clean. Some extra flags that "
  93. " can be included are, "
  94. "INCLUDE_DIRECTORIES, LINK_DIRECTORIES, and LINK_LIBRARIES. "
  95. "COMPILE_DEFINITIONS are -Ddefinition that will be passed to the "
  96. "compile line. "
  97. "TRY_COMPILE creates a CMakeList.txt "
  98. "file on the fly that looks like this:\n"
  99. " ADD_DEFINITIONS( <expanded COMPILE_DEFINITIONS from calling "
  100. "cmake>)\n"
  101. " INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES})\n"
  102. " LINK_DIRECTORIES(${LINK_DIRECTORIES})\n"
  103. " ADD_EXECUTABLE(cmTryCompileExec sources)\n"
  104. " TARGET_LINK_LIBRARIES(cmTryCompileExec ${LINK_LIBRARIES})\n"
  105. "In both versions of the command, "
  106. "if OUTPUT_VARIABLE is specified, then the "
  107. "output from the build process is stored in the given variable. "
  108. "Return the success or failure in "
  109. "RESULT_VAR. CMAKE_FLAGS can be used to pass -DVAR:TYPE=VALUE flags "
  110. "to the cmake that is run during the build. "
  111. "";
  112. }
  113. cmTypeMacro(cmTryCompileCommand, cmCommand);
  114. };
  115. #endif