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