diff options
author | Zack Rusin <zack@kde.org> | 2010-11-18 02:07:57 -0500 |
---|---|---|
committer | Zack Rusin <zack@kde.org> | 2010-11-18 02:07:57 -0500 |
commit | d6fc4e77c7fb35aa4a225000a3bf923ecb48b903 (patch) | |
tree | 4c0f5645f296e3b751e9f4b76364527453c7c642 | |
parent | 985c944a5d368ee3fc8d8b4ff99f8610d273fe36 (diff) |
Cleaner compiler setup and new builins build setup
-rw-r--r-- | CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/builtins/coal-internal.h.in (renamed from src/builtins/coal-internal.h) | 0 | ||||
-rw-r--r-- | src/compiler/compiler.cpp | 154 |
4 files changed, 85 insertions, 81 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ef4d2a2..744b1fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,12 @@ Find_Package(Gallium REQUIRED) Find_Package(LLVM REQUIRED) Find_Package(Clang REQUIRED) +ADD_DEFINITIONS("-DCOAL_BUILD_DIR=\"${CMAKE_BINARY_DIR}\"") +ADD_DEFINITIONS("-DCOAL_INSTALL_DIR=\"${CMAKE_INSTALL_PREFIX}\"") + +SET (INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH + "The directory the headers are installed in") + add_subdirectory(src) add_subdirectory(examples) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8a7e43e..0c011e3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,6 +25,9 @@ set(COAL_SRC_FILES core/sampler.cpp compiler/compiler.cpp) +configure_file(builtins/coal-internal.h.in + ${CMAKE_CURRENT_BINARY_DIR}/builtins/coal-internal.h ) + add_library(OpenCL SHARED ${COAL_SRC_FILES}) SET(LIBRARY_OUTPUT_PATH ${Coal_BINARY_DIR}/lib) @@ -36,6 +39,7 @@ SET_TARGET_PROPERTIES(OpenCL PROPERTIES set_source_files_properties( ${COAL_SRC_FILES} PROPERTIES COMPILE_FLAGS ${LLVM_COMPILE_FLAGS}) + set_target_properties(OpenCL PROPERTIES LINK_FLAGS ${LLVM_LDFLAGS}) @@ -46,4 +50,6 @@ TARGET_LINK_LIBRARIES(OpenCL # ${GALLIUM_LIBS} ) +install( FILES ${CMAKE_CURRENT_BINARY_DIR}/src/builtins/coal-internal.h DESTINATION ${INCLUDE_INSTALL_DIR}/coal) + add_subdirectory(tools) diff --git a/src/builtins/coal-internal.h b/src/builtins/coal-internal.h.in index e7f0276..e7f0276 100644 --- a/src/builtins/coal-internal.h +++ b/src/builtins/coal-internal.h.in diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp index 813c4b3..c0c0338 100644 --- a/src/compiler/compiler.cpp +++ b/src/compiler/compiler.cpp @@ -1,12 +1,10 @@ #include "compiler.h" #include <clang/CodeGen/CodeGenAction.h> -#include <clang/Driver/Compilation.h> -#include <clang/Driver/Driver.h> -#include <clang/Driver/Tool.h> #include <clang/Frontend/CompilerInvocation.h> #include <clang/Frontend/CompilerInstance.h> #include <clang/Frontend/DiagnosticOptions.h> +#include <clang/Frontend/HeaderSearchOptions.h> #include <clang/Frontend/FrontendDiagnostic.h> #include <clang/Frontend/TextDiagnosticPrinter.h> @@ -27,104 +25,98 @@ using namespace Coal; using namespace clang; -Compiler::Compiler() +static void +setupHeaderSearchOpts(HeaderSearchOptions &opts) { - init(); + opts.Verbose = true; + opts.UseBuiltinIncludes = false; + opts.UseStandardIncludes = false; + opts.UseStandardCXXIncludes = false; + + std::string buildPath(COAL_BUILD_DIR); + buildPath += "src/builtin"; + opts.AddPath(buildPath, + frontend::Angled, true, + /*IsFramework=*/ false, true); + std::string installPath(COAL_INSTALL_DIR); + installPath += "include/coal"; + opts.AddPath(installPath, + frontend::Angled, true, + /*IsFramework=*/ false, true); } -Compiler::~Compiler() +static void +setupPreprocessorOpts(PreprocessorOptions &opts) { + opts.Includes.push_back("coal-internal.h"); +} +static void +setupCodeGenOpts(CodeGenOptions &opts) +{ + opts.DebugInfo = true; + //opts.OptimizationLevel = 2; + //opts.MainFileName = "main.cl"; + opts.AsmVerbose = true; } -bool Compiler::init() +static void +setupDiagnosticOpts(DiagnosticOptions &opts) { + opts.Pedantic = true; + opts.ShowColumn = true; + opts.ShowLocation = true; + opts.ShowCarets = true; + opts.ShowFixits = true; + opts.ShowColors = true; + opts.ErrorLimit = 19; + opts.MessageLength = 80; + //opts.Warnings[0] = "all"; } -llvm::Module * Compiler::compile(const std::string &text) +static void +setupFrontendOpts(FrontendOptions &opts) { - clang::TextDiagnosticPrinter *DiagClient = - new clang::TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions()); - - clang::Diagnostic Diags(DiagClient); - clang::driver::Driver TheDriver( - "clc", llvm::sys::getHostTriple(), - "a.out", /*IsProduction=*/false, /*CXXIsProduction=*/false, - Diags); - TheDriver.setTitle("OpenCL Interpreter"); - - // FIXME: This is a hack to try to force the driver to do something we can - // recognize. We need to extend the driver library to support this use model - // (basically, exactly one input, and the operation mode is hard wired). - llvm::SmallVector<const char *, 16> Args(4); - Args[0] = "clc"; - Args[1] = "-fsyntax-only"; - Args[2] = "-v"; - Args[3] = "/home/zack/main.c"; - llvm::OwningPtr<clang::driver::Compilation> C( - TheDriver.BuildCompilation(Args.size(), Args.data())); - if (!C) - return 0; + opts.ProgramAction = frontend::EmitLLVMOnly; + opts.ShowVersion = true; + opts.DisableFree = true; + opts.Inputs.push_back( + std::make_pair(IK_OpenCL, "-")); +} - // FIXME: This is copied from ASTUnit.cpp; simplify and eliminate. - // We expect to get back exactly one command job, if we didn't something - // failed. Extract that job from the compilation. - const driver::JobList &Jobs = C->getJobs(); - if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { - llvm::SmallString<256> Msg; - llvm::raw_svector_ostream OS(Msg); - C->PrintJob(OS, C->getJobs(), "; ", true); - Diags.Report(diag::err_fe_expected_compiler_job) << OS.str(); - return 0; - } +Compiler::Compiler() +{ + init(); +} - const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); - if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { - Diags.Report(diag::err_fe_expected_clang_command); - return 0; - } - - // Initialize a compiler invocation object from the clang (-cc1) arguments. - const driver::ArgStringList &CCArgs = Cmd->getArguments(); - llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); - CompilerInvocation::CreateFromArgs(*CI, - const_cast<const char **>(CCArgs.data()), - const_cast<const char **>(CCArgs.data()) + - CCArgs.size(), - Diags); - - // Show the invocation, with -v. - if (CI->getHeaderSearchOpts().Verbose) { - llvm::errs() << "clang invocation:\n"; - C->PrintJob(llvm::errs(), C->getJobs(), "\n", true); - llvm::errs() << "\n"; - } - - // FIXME: This is copied from cc1_main.cpp; simplify and eliminate. - - // Create a compiler instance to handle the actual work. - CompilerInstance Clang; - Clang.setLLVMContext(new llvm::LLVMContext); - Clang.setInvocation(CI.take()); +Compiler::~Compiler() +{ + +} + +bool Compiler::init() +{ + m_clang.setLLVMContext(new llvm::LLVMContext); // Create the compilers actual diagnostics engine. - Clang.createDiagnostics(int(CCArgs.size()),const_cast<char**>(CCArgs.data())); - if (!Clang.hasDiagnostics()) + m_clang.createDiagnostics(0 ,NULL); + if (!m_clang.hasDiagnostics()) return 0; - // Infer the builtin include path if unspecified. - if (Clang.getHeaderSearchOpts().UseBuiltinIncludes && - Clang.getHeaderSearchOpts().ResourceDir.empty()) { - assert(0); - //Clang.getHeaderSearchOpts().ResourceDir = - // CompilerInvocation::GetResourcesPath(argv[0], MainAddr); - } + setupHeaderSearchOpts(m_clang.getHeaderSearchOpts()); + setupPreprocessorOpts(m_clang.getPreprocessorOpts()); + setupCodeGenOpts(m_clang.getCodeGenOpts()); + setupDiagnosticOpts(m_clang.getDiagnosticOpts()); + setupFrontendOpts(m_clang.getFrontendOpts()); +} +llvm::Module * Compiler::compile(const std::string &text) +{ // Create and execute the frontend to generate an LLVM bitcode module. - llvm::OwningPtr<CodeGenAction> Act(new EmitLLVMOnlyAction()); - if (!Clang.ExecuteAction(*Act)) + llvm::OwningPtr<CodeGenAction> act(new EmitLLVMOnlyAction()); + if (!m_clang.ExecuteAction(*act)) return 0; - return Act->takeModule(); + return act->takeModule(); } |