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 <ars@ars.me>
pull/78/head
Ibrahim Numanagić 2022-12-07 19:42:29 -08:00 committed by GitHub
parent 5156b68ddb
commit f4feee23a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 62 deletions

View File

@ -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)

View File

@ -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.

View File

@ -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"

View File

@ -34,7 +34,7 @@ Compiler::Compiler(const std::string &argv0, Compiler::Mode mode,
const std::vector<std::string> &disabledPasses, bool isTest,
bool pyNumerics)
: argv0(argv0), debug(mode == Mode::DEBUG), pyNumerics(pyNumerics), input(),
plm(std::make_unique<PluginManager>()),
plm(std::make_unique<PluginManager>(argv0)),
cache(std::make_unique<ast::Cache>(argv0)),
module(std::make_unique<ir::Module>()),
pm(std::make_unique<ir::transform::PassManager>(getPassManagerInit(mode, isTest),

View File

@ -34,10 +34,8 @@ llvm::Expected<Plugin *> 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;

View File

@ -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<std::unique_ptr<Plugin>> 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()); }

View File

@ -11,7 +11,7 @@
#include "codon/parser/ast/types/type.h"
namespace codon::ast {
class FunctionStmt;
struct FunctionStmt;
}
namespace codon::ast::types {

View File

@ -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 {

View File

@ -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());
}
}
}