Initialization.cpp revision baa6b9d53c2675f20ae6a8d7796b6d530cca8fa7
1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "bcc/Support/Initialization.h"
18
19#include <cstdlib>
20
21#include <llvm/Support/ErrorHandling.h>
22#include <llvm/Support/TargetSelect.h>
23
24#include "bcc/Config/Config.h"
25#include "bcc/Support/Log.h"
26
27namespace {
28
29void llvm_error_handler(void *pUserData, const std::string &pMessage) {
30  ALOGE("%s", pMessage.c_str());
31  ::exit(1);
32}
33
34} // end anonymous namespace
35
36void bcc::init::Initialize() {
37  static bool is_initialized = false;
38
39  if (is_initialized) {
40    return;
41  }
42
43  // Setup error handler for LLVM.
44  llvm::remove_fatal_error_handler();
45  llvm::install_fatal_error_handler(llvm_error_handler, NULL);
46
47#if defined(PROVIDE_ARM_CODEGEN)
48  LLVMInitializeARMAsmPrinter();
49# if USE_DISASSEMBLER
50  LLVMInitializeARMDisassembler();
51# endif
52  LLVMInitializeARMTargetMC();
53  LLVMInitializeARMTargetInfo();
54  LLVMInitializeARMTarget();
55#endif
56
57#if defined(PROVIDE_MIPS_CODEGEN)
58  LLVMInitializeMipsAsmPrinter();
59# if USE_DISASSEMBLER
60  LLVMInitializeMipsDisassembler();
61# endif
62  LLVMInitializeMipsTargetMC();
63  LLVMInitializeMipsTargetInfo();
64  LLVMInitializeMipsTarget();
65#endif
66
67#if defined(PROVIDE_X86_CODEGEN)
68  LLVMInitializeX86AsmPrinter();
69# if USE_DISASSEMBLER
70  LLVMInitializeX86Disassembler();
71# endif
72  LLVMInitializeX86TargetMC();
73  LLVMInitializeX86TargetInfo();
74  LLVMInitializeX86Target();
75#endif
76
77  is_initialized = true;
78
79  return;
80}
81