resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmTryCompileCommand.cxx
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
5 #include "cmCoreTryCompile.h"
6 #include "cmExecutionStatus.h"
7 #include "cmMakefile.h"
8 #include "cmMessageType.h"
9 #include "cmRange.h"
10 #include "cmState.h"
11 #include "cmStateTypes.h"
12 #include "cmStringAlgorithms.h"
13 #include "cmValue.h"
14 #include "cmake.h"
15
16 bool cmTryCompileCommand(std::vector<std::string> const& args,
17                          cmExecutionStatus& status)
18 {
19   cmMakefile& mf = status.GetMakefile();
20
21   if (args.size() < 3) {
22     mf.IssueMessage(
23       MessageType::FATAL_ERROR,
24       "The try_compile() command requires at least 3 arguments.");
25     return false;
26   }
27
28   if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
29     mf.IssueMessage(
30       MessageType::FATAL_ERROR,
31       "The try_compile() command is not supported in --find-package mode.");
32     return false;
33   }
34
35   cmStateEnums::TargetType targetType = cmStateEnums::EXECUTABLE;
36   cmValue tt = mf.GetDefinition("CMAKE_TRY_COMPILE_TARGET_TYPE");
37   if (cmNonempty(tt)) {
38     if (*tt == cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE)) {
39       targetType = cmStateEnums::EXECUTABLE;
40     } else if (*tt ==
41                cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY)) {
42       targetType = cmStateEnums::STATIC_LIBRARY;
43     } else {
44       mf.IssueMessage(
45         MessageType::FATAL_ERROR,
46         cmStrCat("Invalid value '", *tt,
47                  "' for CMAKE_TRY_COMPILE_TARGET_TYPE.  Only '",
48                  cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE),
49                  "' and '",
50                  cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY),
51                  "' are allowed."));
52       return false;
53     }
54   }
55
56   cmCoreTryCompile tc(&mf);
57   cmCoreTryCompile::Arguments arguments =
58     tc.ParseArgs(cmMakeRange(args), false);
59   if (!arguments) {
60     return true;
61   }
62   tc.TryCompileCode(arguments, targetType);
63
64   // if They specified clean then we clean up what we can
65   if (tc.SrcFileSignature) {
66     if (!mf.GetCMakeInstance()->GetDebugTryCompile()) {
67       tc.CleanupFiles(tc.BinaryDirectory);
68     }
69   }
70   return true;
71 }