clang_compiler.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_CLANG_COMPILER_HPP
27 #define CASADI_CLANG_COMPILER_HPP
28 
29 #include "casadi/core/importer_internal.hpp"
30 #include <casadi/interfaces/clang/casadi_importer_clang_export.h>
31 
32 #include <clang/CodeGen/CodeGenAction.h>
33 #include <clang/Basic/DiagnosticOptions.h>
34 #include <clang/Basic/TargetInfo.h>
35 #include <clang/Basic/Version.h>
36 #include <clang/Driver/Compilation.h>
37 #include <clang/Driver/Driver.h>
38 #include <clang/Driver/Tool.h>
39 #include <clang/Frontend/CompilerInstance.h>
40 #include <clang/Frontend/CompilerInvocation.h>
41 #include <clang/Frontend/FrontendDiagnostic.h>
42 #include <clang/Frontend/TextDiagnosticPrinter.h>
43 
44 #include <llvm/ADT/SmallString.h>
45 #include <llvm/ExecutionEngine/ExecutionEngine.h>
46 #if (LLVM_VERSION_MAJOR>=4) || (LLVM_VERSION_MAJOR==3 && LLVM_VERSION_MINOR>=5)
47 #include <llvm/ExecutionEngine/MCJIT.h>
48 #else
49 #include "llvm/ExecutionEngine/JIT.h"
50 #endif
51 #include <llvm/IR/Module.h>
52 #include "llvm/IR/LLVMContext.h"
53 //#include "llvm/IR/Verifier.h"
54 #include <llvm/Support/FileSystem.h>
55 #include <llvm/Support/Host.h>
56 #include <llvm/Support/ManagedStatic.h>
57 #include <llvm/Support/Path.h>
58 #include <llvm/Support/TargetSelect.h>
59 #include <llvm/Support/raw_ostream.h>
60 #include <llvm/Support/raw_os_ostream.h>
61 //#include <llvm/ExecutionEngine/ExecutionEngine.h>
62 
73 namespace casadi {
83  class ClangCompiler : public ImporterInternal {
84  public:
85 
87  explicit ClangCompiler(const std::string& name);
88 
90  static ImporterInternal* creator(const std::string& name) {
91  return new ClangCompiler(name);
92  }
93 
95  ~ClangCompiler() override;
96 
98 
99  static const Options options_;
100  const Options& get_options() const override { return options_;}
102 
104  void init(const Dict& opts) override;
105 
107  static const std::string meta_doc;
108 
110  const char* plugin_name() const override { return "clang";}
111 
112  // Get name of the class
113  std::string class_name() const override { return "ClangCompiler";}
114 
116  signal_t get_function(const std::string& symname) override;
117 
118  // Helper function for reading includes
119  static std::vector<std::pair<std::string, bool> >
120  getIncludes(const std::string& file, const std::string& path);
121 
122  // Options
123  std::string include_path_;
124  std::vector<std::string> flags_;
125 
126  protected:
127  clang::EmitLLVMOnlyAction* act_;
128  llvm::ExecutionEngine* executionEngine_;
129  llvm::LLVMContext* context_;
130  llvm::raw_ostream* myerr_;
131  llvm::Module* module_; // owned by executionEngine_
132  };
133 
134 } // namespace casadi
136 
137 #endif // CASADI_CLANG_COMPILER_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.