Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
Fix repl and linux build (#127)
Browse files Browse the repository at this point in the history
* Add docker image

* Fix build on linux

* Improve build a little

* Fix repl
  • Loading branch information
gavrilikhin-d authored Apr 12, 2024
1 parent 3976fbb commit caae74a
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 11 deletions.
29 changes: 29 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/target
**/.github
extension
benchmarks
LICENSE
README.md
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Use Ubuntu jammy as the base image
FROM ubuntu:jammy

# Install necessary packages
RUN apt-get update && apt-get install -y \
curl \
gnupg \
lsb-release \
software-properties-common \
build-essential \
m4 \
zlib1g-dev \
libzstd-dev

# Prepare to add LLVM repository
RUN curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key | gpg --dearmor -o /usr/share/keyrings/llvm-archive-keyring.gpg

# Add the LLVM repository using the modern signed-by method
RUN echo "deb [signed-by=/usr/share/keyrings/llvm-archive-keyring.gpg] http://apt.llvm.org/jammy llvm-toolchain-jammy-17 main" > /etc/apt/sources.list.d/llvm.list

# Install LLVM 17
RUN apt-get update && apt-get install -y llvm-17 llvm-17-dev clang-17 libpolly-17-dev
ENV CC=clang-17
ENV CXX=clang++-17

# Install Rust nightly
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain nightly -y
ENV PATH="/root/.cargo/bin:${PATH}"

# Configure cargo to use clang-17 as the linker
RUN mkdir -p ~/.cargo \
&& echo '[target.x86_64-unknown-linux-gnu]\nlinker = "clang-17"' > ~/.cargo/config.toml

# Clean up
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Build ppl
WORKDIR /usr/src/ppl
COPY . .
RUN cargo build

CMD ["cargo", "test", "--verbose"]
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
ppl:
image: ppl
build:
context: .
dockerfile: ./Dockerfile
11 changes: 10 additions & 1 deletion src/driver/execute/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl Emit for Package {
let lib_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("target/debug/deps");
let lib = lib_path.to_str().unwrap();

trace!(target: "steps", "assembling {}", output_file.display());
match output_type {
OutputType::HIR => unreachable!("HIR is already written"),
OutputType::IR => unreachable!("IR is already written"),
Expand All @@ -160,7 +161,13 @@ impl Emit for Package {
OutputType::Object => clang.arg("-c"),
OutputType::Assembler => clang.arg("-S"),
OutputType::StaticLibrary => clang.args(&["-c", "-fPIC"]),
OutputType::DynamicLibrary => clang.arg("-dynamiclib"),
OutputType::DynamicLibrary => {
if cfg!(target_os = "macos") {
clang.arg("-dynamiclib")
} else {
clang.args(&["-shared", "-fPIC"])
}
}
OutputType::Executable => &mut clang,
}
.args(&["-L", lib, "-lruntime"])
Expand All @@ -171,6 +178,8 @@ impl Emit for Package {
.arg("-g")
.arg("-fsanitize=address")
.status()
.map_err(|e| miette!("{output_file:?}: {e}"))?
.exit_ok()
.map_err(|e| miette!("{output_file:?}: {e}"))?;

Ok(output_file)
Expand Down
21 changes: 15 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ use log::debug;
use miette::NamedSource;
use ppl::compilation::Compiler;
use ppl::driver::commands::compile::OutputType;
use ppl::driver::{self, Execute};
use ppl::driver::{self, commands, Execute};
use ppl::hir;
use ppl::ir::HIRModuleLowering;
use ppl::semantics::{Context, ModuleContext, Monomorphize, ToHIR};
use ppl::syntax::{InteractiveLexer, Lexer, Parse};
use ppl::Reporter;
use ppl::{ast::*, SourceFile};
use tempdir::TempDir;

extern crate runtime;

Expand Down Expand Up @@ -73,11 +74,19 @@ fn repl() {
.create_jit_execution_engine(OptimizationLevel::None)
.unwrap();

// TODO: env var for runtime path
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let lib_path = manifest_dir
.join("target/debug/deps")
.join(OutputType::DynamicLibrary.named("ppl"));
let tmp = TempDir::new("ppl").unwrap();

let ppl_packet_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("ppl");
std::env::set_current_dir(&ppl_packet_dir).unwrap();

commands::Build {
output_dir: tmp.path().to_path_buf(),
output_type: None,
}
.execute()
.unwrap();

let lib_path = tmp.path().join(OutputType::DynamicLibrary.named("ppl"));
inkwell::support::load_library_permanently(&lib_path).expect(&format!(
"Failed to load core library at: {}",
lib_path.display()
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/src/integer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::c_char;

use rug::ops::Pow;

use crate::{Rational, String};
Expand Down Expand Up @@ -47,7 +49,7 @@ pub extern "C" fn integer_from_u64(value: u64) -> Integer {

/// Construct [`Integer`](ppl::semantics::Type::Integer) from a C string
#[no_mangle]
pub extern "C" fn integer_from_c_string(str: *const i8) -> Integer {
pub extern "C" fn integer_from_c_string(str: *const c_char) -> Integer {
debug_assert!(!str.is_null());

let c_str = unsafe { core::ffi::CStr::from_ptr(str) };
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/src/rational.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::c_char;

use rug::{ops::Pow, Integer};

use crate::String;
Expand All @@ -20,7 +22,7 @@ pub struct Rational {

/// Construct [`Rational`](ppl::semantics::Type::Rational) from a C string
#[no_mangle]
pub extern "C" fn rational_from_c_string(str: *const i8) -> Rational {
pub extern "C" fn rational_from_c_string(str: *const c_char) -> Rational {
debug_assert!(!str.is_null());

let c_str = unsafe { core::ffi::CStr::from_ptr(str) };
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::Write;
use std::{ffi::c_char, io::Write};

/// PPL's String type.
/// Wrapper around pointer to [`std::string::String`].
Expand All @@ -19,7 +19,7 @@ pub struct String {
/// Construct [`String`](ppl::semantics::Type::String) from a C string
/// and length
#[no_mangle]
pub extern "C" fn string_from_c_string_and_length(str: *const i8, _len: u64) -> String {
pub extern "C" fn string_from_c_string_and_length(str: *const c_char, _len: u64) -> String {
let c_str = unsafe { core::ffi::CStr::from_ptr(str) };
let str = c_str.to_str().unwrap();
let boxed = Box::new(str.to_string());
Expand Down

0 comments on commit caae74a

Please sign in to comment.