cmTryCompileCommand.cxx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmTryCompileCommand.h"
  4. #include <cm/optional>
  5. #include "cmConfigureLog.h"
  6. #include "cmCoreTryCompile.h"
  7. #include "cmExecutionStatus.h"
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmRange.h"
  11. #include "cmState.h"
  12. #include "cmStateTypes.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmValue.h"
  15. #include "cmake.h"
  16. namespace {
  17. #ifndef CMAKE_BOOTSTRAP
  18. void WriteTryCompileEvent(cmConfigureLog& log, cmMakefile const& mf,
  19. cmTryCompileResult const& compileResult)
  20. {
  21. log.BeginEvent("try_compile");
  22. log.WriteBacktrace(mf);
  23. cmCoreTryCompile::WriteTryCompileEventFields(log, compileResult);
  24. log.EndEvent();
  25. }
  26. #endif
  27. }
  28. bool cmTryCompileCommand(std::vector<std::string> const& args,
  29. cmExecutionStatus& status)
  30. {
  31. cmMakefile& mf = status.GetMakefile();
  32. if (args.size() < 3) {
  33. mf.IssueMessage(
  34. MessageType::FATAL_ERROR,
  35. "The try_compile() command requires at least 3 arguments.");
  36. return false;
  37. }
  38. if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
  39. mf.IssueMessage(
  40. MessageType::FATAL_ERROR,
  41. "The try_compile() command is not supported in --find-package mode.");
  42. return false;
  43. }
  44. cmStateEnums::TargetType targetType = cmStateEnums::EXECUTABLE;
  45. cmValue tt = mf.GetDefinition("CMAKE_TRY_COMPILE_TARGET_TYPE");
  46. if (cmNonempty(tt)) {
  47. if (*tt == cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE)) {
  48. targetType = cmStateEnums::EXECUTABLE;
  49. } else if (*tt ==
  50. cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY)) {
  51. targetType = cmStateEnums::STATIC_LIBRARY;
  52. } else {
  53. mf.IssueMessage(
  54. MessageType::FATAL_ERROR,
  55. cmStrCat("Invalid value '", *tt,
  56. "' for CMAKE_TRY_COMPILE_TARGET_TYPE. Only '",
  57. cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE),
  58. "' and '",
  59. cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY),
  60. "' are allowed."));
  61. return false;
  62. }
  63. }
  64. cmCoreTryCompile tc(&mf);
  65. cmCoreTryCompile::Arguments arguments =
  66. tc.ParseArgs(cmMakeRange(args), false);
  67. if (!arguments) {
  68. return true;
  69. }
  70. if (cm::optional<cmTryCompileResult> compileResult =
  71. tc.TryCompileCode(arguments, targetType)) {
  72. #ifndef CMAKE_BOOTSTRAP
  73. if (cmConfigureLog* log = mf.GetCMakeInstance()->GetConfigureLog()) {
  74. WriteTryCompileEvent(*log, mf, *compileResult);
  75. }
  76. #endif
  77. }
  78. // if They specified clean then we clean up what we can
  79. if (tc.SrcFileSignature) {
  80. if (!mf.GetCMakeInstance()->GetDebugTryCompile()) {
  81. tc.CleanupFiles(tc.BinaryDirectory);
  82. }
  83. }
  84. return true;
  85. }