xref: /llvm-project/llvm/unittests/Target/TargetMachineOptionsTest.cpp (revision 3351097c7b69ee88b6f2ad283ef8a56776bf0559)
1 //===-- llvm/unittests/Target/TargetMachineOptionsTest.cpp ----------
2 //-----===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file contains unit tests for the opaque structure describing options
12 /// for TargetMachine creation via the C API.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm-c/Core.h"
17 #include "llvm-c/TargetMachine.h"
18 #include "llvm/Config/llvm-config.h"
19 #include "gtest/gtest.h"
20 
21 namespace llvm {
22 
TEST(TargetMachineCTest,TargetMachineOptions)23 TEST(TargetMachineCTest, TargetMachineOptions) {
24   auto *Options = LLVMCreateTargetMachineOptions();
25 
26   LLVMTargetMachineOptionsSetCPU(Options, "cortex-a53");
27   LLVMTargetMachineOptionsSetFeatures(Options, "+neon");
28   LLVMTargetMachineOptionsSetABI(Options, "aapcs");
29   LLVMTargetMachineOptionsSetCodeGenOptLevel(Options, LLVMCodeGenLevelNone);
30   LLVMTargetMachineOptionsSetRelocMode(Options, LLVMRelocStatic);
31   LLVMTargetMachineOptionsSetCodeModel(Options, LLVMCodeModelKernel);
32 
33   LLVMDisposeTargetMachineOptions(Options);
34 }
35 
TEST(TargetMachineCTest,TargetMachineCreation)36 TEST(TargetMachineCTest, TargetMachineCreation) {
37   LLVMInitializeAllTargets();
38   LLVMInitializeAllTargetInfos();
39   LLVMInitializeAllTargetMCs();
40 
41   // Get the default target to keep the test as generic as possible. This may
42   // not be a target for which we can generate code; in that case we give up.
43 
44   auto *Triple = LLVMGetDefaultTargetTriple();
45   if (strlen(Triple) == 0) {
46     LLVMDisposeMessage(Triple);
47     GTEST_SKIP();
48   }
49 
50   LLVMTargetRef Target = nullptr;
51   char *Error = nullptr;
52   if (LLVMGetTargetFromTriple(Triple, &Target, &Error))
53     FAIL() << "Failed to create target from default triple (" << Triple
54            << "): " << Error;
55 
56   ASSERT_NE(Target, nullptr);
57 
58   if (!LLVMTargetHasTargetMachine(Target))
59     GTEST_SKIP() << "Default target doesn't support code generation";
60 
61   // We don't know which target we're creating a machine for, so don't set any
62   // non-default options; they might cause fatal errors.
63 
64   auto *Options = LLVMCreateTargetMachineOptions();
65   auto *TM = LLVMCreateTargetMachineWithOptions(Target, Triple, Options);
66   ASSERT_NE(TM, nullptr);
67 
68   LLVMDisposeMessage(Triple);
69   LLVMDisposeTargetMachineOptions(Options);
70   LLVMDisposeTargetMachine(TM);
71 }
72 
73 } // namespace llvm
74