1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00

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>
This commit is contained in:
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) CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest) FetchContent_MakeAvailable(googletest)
enable_testing() enable_testing()
if(IS_DIRECTORY "${googletest_SOURCE_DIR}")
set_property(DIRECTORY ${googletest_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL YES)
endif()
set(CODON_TEST_CPPFILES set(CODON_TEST_CPPFILES
test/main.cpp test/main.cpp
test/sir/analyze/dominator.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_link_libraries(codon_test fmt codonc codonrt gtest_main)
target_compile_definitions(codon_test target_compile_definitions(codon_test
PRIVATE TEST_DIR="${CMAKE_CURRENT_SOURCE_DIR}/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 (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). 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 ## Examples
Codon is a Python-compatible language, and many Python programs will work with few if any modifications: 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 Plain Python functions and libraries can also be called from within Codon via
[Python interoperability](https://docs.exaloop.io/codon/interoperability/python). [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 ## Documentation
Please see [docs.exaloop.io](https://docs.exaloop.io/codon) for in-depth documentation. Please see [docs.exaloop.io](https://docs.exaloop.io/codon) for in-depth documentation.

View File

@ -16,7 +16,8 @@ CPMAddPackage(
NAME fmt NAME fmt
GITHUB_REPOSITORY "fmtlib/fmt" GITHUB_REPOSITORY "fmtlib/fmt"
GIT_TAG 9.1.0 GIT_TAG 9.1.0
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON") OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON"
"FMT_INSTALL ON")
CPMAddPackage( CPMAddPackage(
NAME toml NAME toml
@ -33,6 +34,7 @@ CPMAddPackage(
GITHUB_REPOSITORY "zlib-ng/zlib-ng" GITHUB_REPOSITORY "zlib-ng/zlib-ng"
VERSION 2.0.5 VERSION 2.0.5
GIT_TAG 2.0.5 GIT_TAG 2.0.5
EXCLUDE_FROM_ALL YES
OPTIONS "HAVE_OFF64_T ON" OPTIONS "HAVE_OFF64_T ON"
"ZLIB_COMPAT ON" "ZLIB_COMPAT ON"
"ZLIB_ENABLE_TESTS OFF" "ZLIB_ENABLE_TESTS OFF"
@ -46,6 +48,7 @@ CPMAddPackage(
GITHUB_REPOSITORY "xz-mirror/xz" GITHUB_REPOSITORY "xz-mirror/xz"
VERSION 5.2.5 VERSION 5.2.5
GIT_TAG e7da44d5151e21f153925781ad29334ae0786101 GIT_TAG e7da44d5151e21f153925781ad29334ae0786101
EXCLUDE_FROM_ALL YES
OPTIONS "BUILD_SHARED_LIBS OFF" OPTIONS "BUILD_SHARED_LIBS OFF"
"CMAKE_POSITION_INDEPENDENT_CODE ON") "CMAKE_POSITION_INDEPENDENT_CODE ON")
if(xz_ADDED) if(xz_ADDED)
@ -77,6 +80,7 @@ CPMAddPackage(
GITHUB_REPOSITORY "ivmai/bdwgc" GITHUB_REPOSITORY "ivmai/bdwgc"
VERSION 8.0.5 VERSION 8.0.5
GIT_TAG d0ba209660ea8c663e06d9a68332ba5f42da54ba GIT_TAG d0ba209660ea8c663e06d9a68332ba5f42da54ba
EXCLUDE_FROM_ALL YES
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON" OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON"
"BUILD_SHARED_LIBS OFF" "BUILD_SHARED_LIBS OFF"
"enable_threads ON" "enable_threads ON"
@ -91,6 +95,7 @@ CPMAddPackage(
NAME openmp NAME openmp
GITHUB_REPOSITORY "exaloop/openmp" GITHUB_REPOSITORY "exaloop/openmp"
GIT_TAG 11daa2021c590dc74a0e734b4783570b619d88c9 GIT_TAG 11daa2021c590dc74a0e734b4783570b619d88c9
EXCLUDE_FROM_ALL YES
OPTIONS "CMAKE_BUILD_TYPE Release" OPTIONS "CMAKE_BUILD_TYPE Release"
"OPENMP_ENABLE_LIBOMPTARGET OFF" "OPENMP_ENABLE_LIBOMPTARGET OFF"
"OPENMP_STANDALONE_BUILD ON") "OPENMP_STANDALONE_BUILD ON")
@ -148,6 +153,7 @@ CPMAddPackage(
GITHUB_REPOSITORY "google/re2" GITHUB_REPOSITORY "google/re2"
VERSION 2022-06-01 VERSION 2022-06-01
GIT_TAG 5723bb8950318135ed9cf4fc76bed988a087f536 GIT_TAG 5723bb8950318135ed9cf4fc76bed988a087f536
EXCLUDE_FROM_ALL YES
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON" OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON"
"BUILD_SHARED_LIBS OFF" "BUILD_SHARED_LIBS OFF"
"RE2_BUILD_TESTING OFF") "RE2_BUILD_TESTING OFF")
@ -169,6 +175,7 @@ if(CODON_JUPYTER)
NAME libzmq NAME libzmq
VERSION 4.3.4 VERSION 4.3.4
URL https://github.com/zeromq/libzmq/releases/download/v4.3.4/zeromq-4.3.4.tar.gz 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" OPTIONS "WITH_PERF_TOOL OFF"
"ZMQ_BUILD_TESTS OFF" "ZMQ_BUILD_TESTS OFF"
"ENABLE_CPACK OFF" "ENABLE_CPACK OFF"
@ -179,12 +186,14 @@ if(CODON_JUPYTER)
NAME cppzmq NAME cppzmq
URL https://github.com/zeromq/cppzmq/archive/refs/tags/v4.8.1.tar.gz URL https://github.com/zeromq/cppzmq/archive/refs/tags/v4.8.1.tar.gz
VERSION 4.8.1 VERSION 4.8.1
EXCLUDE_FROM_ALL YES
OPTIONS "CPPZMQ_BUILD_TESTS OFF") OPTIONS "CPPZMQ_BUILD_TESTS OFF")
CPMAddPackage( CPMAddPackage(
NAME xtl NAME xtl
GITHUB_REPOSITORY "xtensor-stack/xtl" GITHUB_REPOSITORY "xtensor-stack/xtl"
VERSION 0.7.3 VERSION 0.7.3
GIT_TAG 0.7.3 GIT_TAG 0.7.3
EXCLUDE_FROM_ALL YES
OPTIONS "BUILD_TESTS OFF") OPTIONS "BUILD_TESTS OFF")
CPMAddPackage( CPMAddPackage(
NAME json NAME json
@ -195,6 +204,7 @@ if(CODON_JUPYTER)
GITHUB_REPOSITORY "jupyter-xeus/xeus" GITHUB_REPOSITORY "jupyter-xeus/xeus"
VERSION 2.2.0 VERSION 2.2.0
GIT_TAG 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 PATCH_COMMAND patch -N -u CMakeLists.txt -b ${CMAKE_SOURCE_DIR}/cmake/xeus.patch || true
OPTIONS "BUILD_EXAMPLES OFF" OPTIONS "BUILD_EXAMPLES OFF"
"XEUS_BUILD_SHARED_LIBS 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, const std::vector<std::string> &disabledPasses, bool isTest,
bool pyNumerics) bool pyNumerics)
: argv0(argv0), debug(mode == Mode::DEBUG), pyNumerics(pyNumerics), input(), : 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)), cache(std::make_unique<ast::Cache>(argv0)),
module(std::make_unique<ir::Module>()), module(std::make_unique<ir::Module>()),
pm(std::make_unique<ir::transform::PassManager>(getPassManagerInit(mode, isTest), 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); llvm::sys::path::append(tomlPath, config);
if (!llvm::sys::fs::exists(tomlPath)) { if (!llvm::sys::fs::exists(tomlPath)) {
// try default install path // try default install path
if (auto *homeDir = std::getenv("HOME")) { tomlPath = llvm::SmallString<128>(ast::executable_path(argv0.c_str()));
tomlPath = homeDir; llvm::sys::path::append(tomlPath, "../lib/codon/plugins", path, config);
llvm::sys::path::append(tomlPath, ".codon", "plugins", path, config);
}
} }
toml::parse_result tml; toml::parse_result tml;

View File

@ -30,12 +30,14 @@ struct Plugin {
/// Manager for loading, applying and unloading plugins. /// Manager for loading, applying and unloading plugins.
class PluginManager { class PluginManager {
private: private:
/// Codon executable location
std::string argv0;
/// vector of loaded plugins /// vector of loaded plugins
std::vector<std::unique_ptr<Plugin>> plugins; std::vector<std::unique_ptr<Plugin>> plugins;
public: public:
/// Constructs a plugin manager /// Constructs a plugin manager
PluginManager() : plugins() {} PluginManager(const std::string &argv0) : argv0(argv0), plugins() {}
/// @return iterator to the first plugin /// @return iterator to the first plugin
auto begin() { return ir::util::raw_ptr_adaptor(plugins.begin()); } auto begin() { return ir::util::raw_ptr_adaptor(plugins.begin()); }

View File

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

View File

@ -11,8 +11,8 @@
#include "codon/parser/common.h" #include "codon/parser/common.h"
namespace codon::ast { namespace codon::ast {
class Cache; struct Cache;
class Expr; struct Expr;
} // namespace codon::ast } // namespace codon::ast
namespace codon::ast::types { namespace codon::ast::types {

View File

@ -471,9 +471,11 @@ void LLVMVisitor::writeToExecutable(const std::string &filename,
command.push_back("-shared"); command.push_back("-shared");
for (const auto &rpath : rpaths) { for (const auto &rpath : rpaths) {
if (!rpath.empty()) {
command.push_back("-L" + rpath); command.push_back("-L" + rpath);
command.push_back("-Wl,-rpath," + rpath); command.push_back("-Wl,-rpath," + rpath);
} }
}
if (plugins) { if (plugins) {
for (auto *plugin : *plugins) { for (auto *plugin : *plugins) {
@ -484,10 +486,12 @@ void LLVMVisitor::writeToExecutable(const std::string &filename,
llvm::SmallString<128> rpath0 = llvm::sys::path::parent_path(dylibPath); llvm::SmallString<128> rpath0 = llvm::sys::path::parent_path(dylibPath);
llvm::sys::fs::make_absolute(rpath0); llvm::sys::fs::make_absolute(rpath0);
llvm::StringRef rpath = rpath0.str(); llvm::StringRef rpath = rpath0.str();
if (!rpath.empty()) {
command.push_back("-L" + rpath.str()); command.push_back("-L" + rpath.str());
command.push_back("-Wl,-rpath," + rpath.str()); command.push_back("-Wl,-rpath," + rpath.str());
} }
} }
}
for (const auto &lib : libs) { for (const auto &lib : libs) {
command.push_back("-l" + lib); command.push_back("-l" + lib);