From f4feee23a4c8b52550a5a7ec63d25acdb6f67d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ibrahim=20Numanagi=C4=87?= Date: Wed, 7 Dec 2022 19:42:29 -0800 Subject: [PATCH] Plugin loading fixes (#66) * Add INSTALL target; Plugin loading fixes * Use ast::executable_path to get exec path * Update README.md Co-authored-by: A. R. Shajii --- CMakeLists.txt | 10 ++++++ README.md | 59 ++++++------------------------- cmake/deps.cmake | 12 ++++++- codon/compiler/compiler.cpp | 2 +- codon/dsl/plugins.cpp | 6 ++-- codon/dsl/plugins.h | 4 ++- codon/parser/ast/types/function.h | 2 +- codon/parser/ast/types/type.h | 4 +-- codon/sir/llvm/llvisitor.cpp | 12 ++++--- 9 files changed, 49 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c43254a1..e4f30ad2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -436,6 +436,9 @@ set(gtest_force_shared_crt CACHE BOOL "" FORCE) FetchContent_MakeAvailable(googletest) enable_testing() +if(IS_DIRECTORY "${googletest_SOURCE_DIR}") + set_property(DIRECTORY ${googletest_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL YES) +endif() set(CODON_TEST_CPPFILES test/main.cpp test/sir/analyze/dominator.cpp @@ -458,3 +461,10 @@ target_include_directories(codon_test PRIVATE test/sir target_link_libraries(codon_test fmt codonc codonrt gtest_main) target_compile_definitions(codon_test PRIVATE TEST_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test") + +install(TARGETS codonrt codonc DESTINATION lib/codon) +install(FILES ${CMAKE_BINARY_DIR}/libomp${CMAKE_SHARED_LIBRARY_SUFFIX} DESTINATION lib/codon) +install(TARGETS codon DESTINATION bin) +install(DIRECTORY ${CMAKE_BINARY_DIR}/include/codon DESTINATION include) +install(DIRECTORY ${CMAKE_SOURCE_DIR}/stdlib DESTINATION lib/codon) +install(DIRECTORY DESTINATION lib/codon/plugins) diff --git a/README.md b/README.md index 86b53977..9797a3e1 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,17 @@ Typical speedups over Python are on the order of 10-100x or more, on a single th (and sometimes better than) that of C/C++. Unlike Python, Codon supports native multithreading, which can lead to speedups many times higher still. Codon grew out of the [Seq project](https://github.com/seq-lang/seq). +## Install + +Pre-built binaries for Linux (x86_64) and macOS (x86_64 and arm64) are available alongside [each release](https://github.com/exaloop/codon/releases). +Download and install with: + +```bash +/bin/bash -c "$(curl -fsSL https://exaloop.io/install.sh)" +``` + +Or you can [build from source](https://docs.exaloop.io/codon/advanced/build). + ## Examples Codon is a Python-compatible language, and many Python programs will work with few if any modifications: @@ -132,54 +143,6 @@ Codon can be used within larger Python codebases via the [`@codon.jit` decorator Plain Python functions and libraries can also be called from within Codon via [Python interoperability](https://docs.exaloop.io/codon/interoperability/python). -## Install - -### Pre-built binaries - -Pre-built binaries for Linux and macOS on x86_64 are available alongside [each release](https://github.com/exaloop/codon/releases). -Download and install with: - -```bash -/bin/bash -c "$(curl -fsSL https://exaloop.io/install.sh)" -``` - -### Build from source - -Codon can be built using CMake. It is recommended to build Codon with Clang, which can be done by setting the `CC` and `CXX` -environment variables: - -```bash -export CC=clang -export CXX=clang++ -``` - -Most dependencies are built as part of the CMake build process, except for LLVM, which can be built as follows -(note that Codon uses a custom fork of LLVM 15): - -```bash -git clone --depth 1 -b codon https://github.com/exaloop/llvm-project -mkdir -p llvm-project/llvm/build -cd llvm-project/llvm/build -cmake .. \ - -DCMAKE_BUILD_TYPE=Release \ - -DLLVM_INCLUDE_TESTS=OFF \ - -DLLVM_ENABLE_RTTI=ON \ - -DLLVM_ENABLE_ZLIB=OFF \ - -DLLVM_ENABLE_TERMINFO=OFF \ - -DLLVM_TARGETS_TO_BUILD=all -make -make install -``` - -Finally, build Codon itself with: - -```bash -cd codon -mkdir build -(cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DLLVM_DIR=$(llvm-config --cmakedir)) -cmake --build build --config Release -``` - ## Documentation Please see [docs.exaloop.io](https://docs.exaloop.io/codon) for in-depth documentation. diff --git a/cmake/deps.cmake b/cmake/deps.cmake index bb7574be..172bb291 100644 --- a/cmake/deps.cmake +++ b/cmake/deps.cmake @@ -16,7 +16,8 @@ CPMAddPackage( NAME fmt GITHUB_REPOSITORY "fmtlib/fmt" GIT_TAG 9.1.0 - OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON") + OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON" + "FMT_INSTALL ON") CPMAddPackage( NAME toml @@ -33,6 +34,7 @@ CPMAddPackage( GITHUB_REPOSITORY "zlib-ng/zlib-ng" VERSION 2.0.5 GIT_TAG 2.0.5 + EXCLUDE_FROM_ALL YES OPTIONS "HAVE_OFF64_T ON" "ZLIB_COMPAT ON" "ZLIB_ENABLE_TESTS OFF" @@ -46,6 +48,7 @@ CPMAddPackage( GITHUB_REPOSITORY "xz-mirror/xz" VERSION 5.2.5 GIT_TAG e7da44d5151e21f153925781ad29334ae0786101 + EXCLUDE_FROM_ALL YES OPTIONS "BUILD_SHARED_LIBS OFF" "CMAKE_POSITION_INDEPENDENT_CODE ON") if(xz_ADDED) @@ -77,6 +80,7 @@ CPMAddPackage( GITHUB_REPOSITORY "ivmai/bdwgc" VERSION 8.0.5 GIT_TAG d0ba209660ea8c663e06d9a68332ba5f42da54ba + EXCLUDE_FROM_ALL YES OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON" "BUILD_SHARED_LIBS OFF" "enable_threads ON" @@ -91,6 +95,7 @@ CPMAddPackage( NAME openmp GITHUB_REPOSITORY "exaloop/openmp" GIT_TAG 11daa2021c590dc74a0e734b4783570b619d88c9 + EXCLUDE_FROM_ALL YES OPTIONS "CMAKE_BUILD_TYPE Release" "OPENMP_ENABLE_LIBOMPTARGET OFF" "OPENMP_STANDALONE_BUILD ON") @@ -148,6 +153,7 @@ CPMAddPackage( GITHUB_REPOSITORY "google/re2" VERSION 2022-06-01 GIT_TAG 5723bb8950318135ed9cf4fc76bed988a087f536 + EXCLUDE_FROM_ALL YES OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON" "BUILD_SHARED_LIBS OFF" "RE2_BUILD_TESTING OFF") @@ -169,6 +175,7 @@ if(CODON_JUPYTER) NAME libzmq VERSION 4.3.4 URL https://github.com/zeromq/libzmq/releases/download/v4.3.4/zeromq-4.3.4.tar.gz + EXCLUDE_FROM_ALL YES OPTIONS "WITH_PERF_TOOL OFF" "ZMQ_BUILD_TESTS OFF" "ENABLE_CPACK OFF" @@ -179,12 +186,14 @@ if(CODON_JUPYTER) NAME cppzmq URL https://github.com/zeromq/cppzmq/archive/refs/tags/v4.8.1.tar.gz VERSION 4.8.1 + EXCLUDE_FROM_ALL YES OPTIONS "CPPZMQ_BUILD_TESTS OFF") CPMAddPackage( NAME xtl GITHUB_REPOSITORY "xtensor-stack/xtl" VERSION 0.7.3 GIT_TAG 0.7.3 + EXCLUDE_FROM_ALL YES OPTIONS "BUILD_TESTS OFF") CPMAddPackage( NAME json @@ -195,6 +204,7 @@ if(CODON_JUPYTER) GITHUB_REPOSITORY "jupyter-xeus/xeus" VERSION 2.2.0 GIT_TAG 2.2.0 + EXCLUDE_FROM_ALL YES PATCH_COMMAND patch -N -u CMakeLists.txt -b ${CMAKE_SOURCE_DIR}/cmake/xeus.patch || true OPTIONS "BUILD_EXAMPLES OFF" "XEUS_BUILD_SHARED_LIBS OFF" diff --git a/codon/compiler/compiler.cpp b/codon/compiler/compiler.cpp index 1a960bc2..8bed4f25 100644 --- a/codon/compiler/compiler.cpp +++ b/codon/compiler/compiler.cpp @@ -34,7 +34,7 @@ Compiler::Compiler(const std::string &argv0, Compiler::Mode mode, const std::vector &disabledPasses, bool isTest, bool pyNumerics) : argv0(argv0), debug(mode == Mode::DEBUG), pyNumerics(pyNumerics), input(), - plm(std::make_unique()), + plm(std::make_unique(argv0)), cache(std::make_unique(argv0)), module(std::make_unique()), pm(std::make_unique(getPassManagerInit(mode, isTest), diff --git a/codon/dsl/plugins.cpp b/codon/dsl/plugins.cpp index 178646c1..d4e1edb2 100644 --- a/codon/dsl/plugins.cpp +++ b/codon/dsl/plugins.cpp @@ -34,10 +34,8 @@ llvm::Expected PluginManager::load(const std::string &path) { llvm::sys::path::append(tomlPath, config); if (!llvm::sys::fs::exists(tomlPath)) { // try default install path - if (auto *homeDir = std::getenv("HOME")) { - tomlPath = homeDir; - llvm::sys::path::append(tomlPath, ".codon", "plugins", path, config); - } + tomlPath = llvm::SmallString<128>(ast::executable_path(argv0.c_str())); + llvm::sys::path::append(tomlPath, "../lib/codon/plugins", path, config); } toml::parse_result tml; diff --git a/codon/dsl/plugins.h b/codon/dsl/plugins.h index 50cbb81c..1172f626 100644 --- a/codon/dsl/plugins.h +++ b/codon/dsl/plugins.h @@ -30,12 +30,14 @@ struct Plugin { /// Manager for loading, applying and unloading plugins. class PluginManager { private: + /// Codon executable location + std::string argv0; /// vector of loaded plugins std::vector> plugins; public: /// Constructs a plugin manager - PluginManager() : plugins() {} + PluginManager(const std::string &argv0) : argv0(argv0), plugins() {} /// @return iterator to the first plugin auto begin() { return ir::util::raw_ptr_adaptor(plugins.begin()); } diff --git a/codon/parser/ast/types/function.h b/codon/parser/ast/types/function.h index 08041587..357a9088 100644 --- a/codon/parser/ast/types/function.h +++ b/codon/parser/ast/types/function.h @@ -11,7 +11,7 @@ #include "codon/parser/ast/types/type.h" namespace codon::ast { -class FunctionStmt; +struct FunctionStmt; } namespace codon::ast::types { diff --git a/codon/parser/ast/types/type.h b/codon/parser/ast/types/type.h index e0533c58..5b8daf16 100644 --- a/codon/parser/ast/types/type.h +++ b/codon/parser/ast/types/type.h @@ -11,8 +11,8 @@ #include "codon/parser/common.h" namespace codon::ast { -class Cache; -class Expr; +struct Cache; +struct Expr; } // namespace codon::ast namespace codon::ast::types { diff --git a/codon/sir/llvm/llvisitor.cpp b/codon/sir/llvm/llvisitor.cpp index 7df28db5..ed8fe894 100644 --- a/codon/sir/llvm/llvisitor.cpp +++ b/codon/sir/llvm/llvisitor.cpp @@ -471,8 +471,10 @@ void LLVMVisitor::writeToExecutable(const std::string &filename, command.push_back("-shared"); for (const auto &rpath : rpaths) { - command.push_back("-L" + rpath); - command.push_back("-Wl,-rpath," + rpath); + if (!rpath.empty()) { + command.push_back("-L" + rpath); + command.push_back("-Wl,-rpath," + rpath); + } } if (plugins) { @@ -484,8 +486,10 @@ void LLVMVisitor::writeToExecutable(const std::string &filename, llvm::SmallString<128> rpath0 = llvm::sys::path::parent_path(dylibPath); llvm::sys::fs::make_absolute(rpath0); llvm::StringRef rpath = rpath0.str(); - command.push_back("-L" + rpath.str()); - command.push_back("-Wl,-rpath," + rpath.str()); + if (!rpath.empty()) { + command.push_back("-L" + rpath.str()); + command.push_back("-Wl,-rpath," + rpath.str()); + } } }