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/InitializePasses.h>
22 #include <llvm/PassRegistry.h>
23 #include <llvm/Support/ErrorHandling.h>
24 #include <llvm/Support/TargetSelect.h>
25 
26 #include "bcc/Config/Config.h"
27 #include "bcc/Support/Log.h"
28 
29 namespace {
30 
llvm_error_handler(void * pUserData,const std::string & pMessage,bool pGenCrashDiag)31 void llvm_error_handler(void *pUserData, const std::string &pMessage,
32                         bool pGenCrashDiag) {
33   ALOGE("bcc: Internal Error - %s", pMessage.c_str());
34   ::exit(1);
35 }
36 
37 } // end anonymous namespace
38 
Initialize()39 void bcc::init::Initialize() {
40   static bool is_initialized = false;
41 
42   if (is_initialized) {
43     return;
44   }
45 
46   // Setup error handler for LLVM.
47   llvm::remove_fatal_error_handler();
48   llvm::install_fatal_error_handler(llvm_error_handler, nullptr);
49 
50 
51   llvm::InitializeAllTargets();
52   llvm::InitializeAllTargetMCs();
53   llvm::InitializeAllAsmPrinters();
54 
55   llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry();
56   llvm::initializeCore(Registry);
57   llvm::initializeScalarOpts(Registry);
58   llvm::initializeVectorization(Registry);
59   llvm::initializeIPO(Registry);
60   llvm::initializeAnalysis(Registry);
61   llvm::initializeIPA(Registry);
62   llvm::initializeTransformUtils(Registry);
63   llvm::initializeInstCombine(Registry);
64   llvm::initializeInstrumentation(Registry);
65   llvm::initializeTarget(Registry);
66   llvm::initializeCodeGenPreparePass(Registry);
67   llvm::initializeAtomicExpandPass(Registry);
68   llvm::initializeRewriteSymbolsPass(Registry);
69 
70   is_initialized = true;
71 
72   return;
73 }
74