invocation.cpp revision c6db1b3396384186aab5b685fe1fd540e17b3a62
1//
2// Copyright 2012 Francisco Jerez
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20// SOFTWARE.
21//
22
23#include "core/compiler.hpp"
24
25#if 0
26#include <clang/Frontend/CompilerInstance.h>
27#include <clang/Frontend/TextDiagnosticPrinter.h>
28#include <clang/CodeGen/CodeGenAction.h>
29#include <llvm/LLVMContext.h>
30#include <llvm/Support/TargetSelect.h>
31#include <llvm/Support/MemoryBuffer.h>
32
33#include <iostream>
34#include <iomanip>
35#include <fstream>
36#include <cstdio>
37#endif
38
39using namespace clover;
40
41#if 0
42namespace {
43   void
44   build_binary(const std::string &source, const std::string &target,
45                const std::string &name) {
46      clang::CompilerInstance c;
47      clang::EmitObjAction act(&llvm::getGlobalContext());
48      std::string log;
49      llvm::raw_string_ostream s_log(log);
50
51      LLVMInitializeTGSITarget();
52      LLVMInitializeTGSITargetInfo();
53      LLVMInitializeTGSITargetMC();
54      LLVMInitializeTGSIAsmPrinter();
55
56      c.getFrontendOpts().Inputs.push_back(
57         std::make_pair(clang::IK_OpenCL, name));
58      c.getHeaderSearchOpts().UseBuiltinIncludes = false;
59      c.getHeaderSearchOpts().UseStandardIncludes = false;
60      c.getLangOpts().NoBuiltin = true;
61      c.getTargetOpts().Triple = target;
62      c.getInvocation().setLangDefaults(clang::IK_OpenCL);
63      c.createDiagnostics(0, NULL, new clang::TextDiagnosticPrinter(
64                             s_log, c.getDiagnosticOpts()));
65
66      c.getPreprocessorOpts().addRemappedFile(
67         name, llvm::MemoryBuffer::getMemBuffer(source));
68
69      if (!c.ExecuteAction(act))
70         throw build_error(log);
71   }
72
73   module
74   load_binary(const char *name) {
75      std::ifstream fs((name));
76      std::vector<unsigned char> str((std::istreambuf_iterator<char>(fs)),
77                                     (std::istreambuf_iterator<char>()));
78      compat::istream cs(str);
79      return module::deserialize(cs);
80   }
81}
82#endif
83
84module
85clover::compile_program_llvm(const compat::string &source,
86                             const compat::string &target) {
87#if 0
88   build_binary(source, target, "cl_input");
89   module m = load_binary("cl_input.o");
90   std::remove("cl_input.o");
91   return m;
92#endif
93   return module();
94}
95