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
21namespace llvm {
22
23TEST(TargetMachineCTest, TargetMachineOptions) {
24 auto *Options = LLVMCreateTargetMachineOptions();
25
26 LLVMTargetMachineOptionsSetCPU(Options, CPU: "cortex-a53");
27 LLVMTargetMachineOptionsSetFeatures(Options, Features: "+neon");
28 LLVMTargetMachineOptionsSetABI(Options, ABI: "aapcs");
29 LLVMTargetMachineOptionsSetCodeGenOptLevel(Options, Level: LLVMCodeGenLevelNone);
30 LLVMTargetMachineOptionsSetRelocMode(Options, Reloc: LLVMRelocStatic);
31 LLVMTargetMachineOptionsSetCodeModel(Options, CodeModel: LLVMCodeModelKernel);
32
33 LLVMDisposeTargetMachineOptions(Options);
34}
35
36TEST(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(s: Triple) == 0) {
46 LLVMDisposeMessage(Message: Triple);
47 GTEST_SKIP();
48 }
49
50 LLVMTargetRef Target = nullptr;
51 char *Error = nullptr;
52 if (LLVMGetTargetFromTriple(Triple, T: &Target, ErrorMessage: &Error))
53 FAIL() << "Failed to create target from default triple (" << Triple
54 << "): " << Error;
55
56 ASSERT_NE(Target, nullptr);
57
58 if (!LLVMTargetHasTargetMachine(T: 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(T: Target, Triple, Options);
66 ASSERT_NE(TM, nullptr);
67
68 LLVMDisposeMessage(Message: Triple);
69 LLVMDisposeTargetMachineOptions(Options);
70 LLVMDisposeTargetMachine(T: TM);
71}
72
73} // namespace llvm
74

source code of llvm/unittests/Target/TargetMachineOptionsTest.cpp