Skip to content

Commit

Permalink
Merge pull request #28 from alexdovzhanyn/v8-engine
Browse files Browse the repository at this point in the history
Switch to V8 engine instead of Wasmer
  • Loading branch information
alexdovzhanyn authored Oct 2, 2024
2 parents b1c3415 + c4b56af commit a18add6
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 490 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ jobs:
- name: Initialize submodules
run: git submodule update --init --recursive

- name: Install Wasmer
run: curl https://get.wasmer.io -sSfL | WASMER_DIR=lib/wasmer sh

- name: Upload Wasmer as artifact
uses: actions/upload-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer
- name: Install depot_tools
run: |
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
echo "${{ github.workspace }}/depot_tools" >> $GITHUB_PATH
- name: Cache CMake build
uses: actions/cache@v3
Expand Down Expand Up @@ -72,12 +68,6 @@ jobs:
name: build
path: ${{ github.workspace }}/build

- name: Download Wasmer artifacts
uses: actions/download-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer

- name: Make LexerTest Executable
run: chmod +x ${{ github.workspace }}/build/LexerTest

Expand All @@ -98,12 +88,6 @@ jobs:
name: build
path: ${{ github.workspace }}/build

- name: Download Wasmer artifacts
uses: actions/download-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer

- name: Make ParserTest Executable
run: chmod +x ${{ github.workspace }}/build/ParserTest

Expand All @@ -124,12 +108,6 @@ jobs:
name: build
path: ${{ github.workspace }}/build

- name: Download Wasmer artifacts
uses: actions/download-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer

- name: Make TypeCheckerTest Executable
run: chmod +x ${{ github.workspace }}/build/TypeCheckerTest

Expand All @@ -149,12 +127,6 @@ jobs:
name: build
path: ${{ github.workspace }}/build

- name: Download Wasmer artifacts
uses: actions/download-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer

- name: Make CodegenTest Executable
run: chmod +x ${{ github.workspace }}/build/CodegenTest

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CMake on a single platform
name: CMake on OSX

on:
push:
Expand All @@ -20,14 +20,10 @@ jobs:
- name: Initialize submodules
run: git submodule update --init --recursive

- name: Install Wasmer
run: curl https://get.wasmer.io -sSfL | WASMER_DIR=lib/wasmer sh

- name: Upload Wasmer as artifact
uses: actions/upload-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer
- name: Install depot_tools
run: |
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
echo "${{ github.workspace }}/depot_tools" >> $GITHUB_PATH
- name: Cache CMake build
uses: actions/cache@v3
Expand Down Expand Up @@ -69,18 +65,9 @@ jobs:
name: build
path: ${{ github.workspace }}/build

- name: Download Wasmer artifacts
uses: actions/download-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer

- name: Make LexerTest Executable
run: chmod +x ${{ github.workspace }}/build/LexerTest

- name: Output Wasmer directory
run: ls -laR ${{ github.workspace }}/lib/wasmer

- name: Run Lexer Test
working-directory: ${{ github.workspace }}/build
run: ./LexerTest
Expand All @@ -98,12 +85,6 @@ jobs:
name: build
path: ${{ github.workspace }}/build

- name: Download Wasmer artifacts
uses: actions/download-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer

- name: Make ParserTest Executable
run: chmod +x ${{ github.workspace }}/build/ParserTest

Expand All @@ -124,12 +105,6 @@ jobs:
name: build
path: ${{ github.workspace }}/build

- name: Download Wasmer artifacts
uses: actions/download-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer

- name: Make TypeCheckerTest Executable
run: chmod +x ${{ github.workspace }}/build/TypeCheckerTest

Expand All @@ -150,16 +125,9 @@ jobs:
name: build
path: ${{ github.workspace }}/build

- name: Download Wasmer artifacts
uses: actions/download-artifact@v4
with:
name: wasmer
path: ${{ github.workspace }}/lib/wasmer

- name: Make CodegenTest Executable
run: chmod +x ${{ github.workspace }}/build/CodegenTest

- name: Run Codegen Test
working-directory: ${{ github.workspace }}/build
run: ./CodegenTest

93 changes: 51 additions & 42 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
cmake_minimum_required(VERSION 3.10)
project(Theta)

# Set the global C++ standard to 17 for Binaryen by default
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DBUILD_TESTS=OFF -Wno-maybe-uninitialized")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Include ExternalProject module
include(ExternalProject)

# Define source directories
set(SRC_DIR src)
set(LEXER_DIR ${SRC_DIR}/lexer)
set(COMPILER_DIR ${SRC_DIR}/compiler)
set(PARSER_DIR ${SRC_DIR}/parser)
set(EXCEPTIONS_DIR ${SRC_DIR}/exceptions)
set(AST_DIR ${PARSER_DIR}/ast)
set(OPTIMIZATION_DIR ${COMPILER_DIR}/optimization)
set(TEST_DIR test)
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
set(TEST_DIR ${CMAKE_SOURCE_DIR}/test)
set(CATCH2_DIR ${TEST_DIR}/catch2)
set(BINARYEN_DIR lib/binaryen)
set(WASMER_DIR lib/wasmer)

# Collect all source files except main.cpp
file(GLOB SRC_FILES
"${SRC_DIR}/*.cpp"
"${LEXER_DIR}/*.cpp"
"${PARSER_DIR}/*.cpp"
"${EXCEPTIONS_DIR}/*.cpp"
"${COMPILER_DIR}/*.cpp"
"${AST_DIR}/*.cpp"
"${OPTIMIZATION_DIR}/*.cpp"
)
set(BINARYEN_DIR ${CMAKE_SOURCE_DIR}/lib/binaryen)
set(V8_DIR ${CMAKE_SOURCE_DIR}/lib/v8) # Path to V8 directory in your project

# Collect all source files recursively from the src directory
file(GLOB_RECURSE SRC_FILES "${SRC_DIR}/*.cpp")
list(REMOVE_ITEM SRC_FILES "${CMAKE_SOURCE_DIR}/theta.cpp")

# Add main source file separately
set(MAIN_SRC "${CMAKE_SOURCE_DIR}/theta.cpp")

# Collect test source files
file(GLOB TEST_SRC_FILES "${TEST_DIR}/*.cpp")
# Add Binaryen
add_subdirectory(${BINARYEN_DIR})

# Add executable for the main program
add_executable(theta ${SRC_FILES} ${MAIN_SRC})
Expand Down Expand Up @@ -65,36 +55,61 @@ else()
target_link_libraries(theta readline)
endif()

# Add Binaryen
add_subdirectory(${BINARYEN_DIR})
# Include directories for Binaryen, catch2, and V8
include_directories(${SRC_DIR} ${CATCH2_DIR} ${BINARYEN_DIR}/src ${V8_DIR}/src/v8/include ${V8_DIR}/src/v8/third_party/wasm-api)

# Add the V8 external project
ExternalProject_Add(
v8_external
PREFIX ${CMAKE_SOURCE_DIR}/lib/v8
SOURCE_DIR ${CMAKE_SOURCE_DIR}/lib/v8
DOWNLOAD_COMMAND
bash ${CMAKE_SOURCE_DIR}/scripts/fetch_v8.sh
UPDATE_COMMAND ""
CONFIGURE_COMMAND
bash -c "DEPOT_TOOLS_UPDATE=0 gclient config https://chromium.googlesource.com/v8/v8.git && cd src/v8 && git checkout branch-heads/12.5 && gclient sync"
BUILD_COMMAND
bash ${CMAKE_SOURCE_DIR}/scripts/build_v8.sh
BUILD_IN_SOURCE 1
INSTALL_COMMAND ""
#LOG_DOWNLOAD ON
LOG_CONFIGURE ON
)

# Ensure theta depends on v8_external and the patching step
add_dependencies(theta v8_external)# v8_patched)

# Include directories
include_directories(${SRC_DIR} ${CATCH2_DIR} ${BINARYEN_DIR}/src)
# Create imported target for V8 without INTERFACE_INCLUDE_DIRECTORIES
add_library(v8_libwee8 STATIC IMPORTED GLOBAL)
set_target_properties(v8_libwee8 PROPERTIES
IMPORTED_LOCATION "${V8_DIR}/src/v8/out.gn/wee8/obj/libwee8.a"
)

# Include Wasmer C API
include_directories(${WASMER_DIR}/include)
# Add the V8 include directory directly to the theta target
target_include_directories(theta PRIVATE ${V8_DIR}/src/v8/third_party/wasm-api)

# Link Wasmer library
find_library(WASMER_LIB wasmer HINTS ${WASMER_DIR}/lib)
# Link theta against V8
target_link_libraries(theta v8_libwee8 pthread dl)

# Link Wasmer and other libraries to the main target
target_link_libraries(theta ${WASMER_LIB} binaryen)
# Link Binaryen (with C++17, set earlier as the global standard)
target_link_libraries(theta binaryen)

# Create build directories if they don't exist
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/test/fixtures)

# Add Catch2 files to the project
set(CATCH2_FILES ${CATCH2_DIR}/catch_amalgamated.cpp)

# Define a target for Catch2 with main function
# Define a target for Catch2 with the main function
add_library(Catch2Main OBJECT ${CATCH2_FILES})
target_compile_definitions(Catch2Main PUBLIC CATCH_CONFIG_MAIN)

# Add test executables
file(GLOB TEST_SRC_FILES "${TEST_DIR}/*.cpp")
foreach(TEST_SRC ${TEST_SRC_FILES})
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
add_executable(${TEST_NAME} ${TEST_SRC} ${SRC_FILES} $<TARGET_OBJECTS:Catch2Main>)
target_link_libraries(${TEST_NAME} ${WASMER_LIB} readline binaryen)
target_link_libraries(${TEST_NAME} readline binaryen v8_libwee8 pthread dl)
endforeach()

# Custom target to copy fixtures
Expand All @@ -104,9 +119,3 @@ add_custom_target(copy-fixtures ALL

# Install target to create a symlink
install(CODE "execute_process(COMMAND ln -sf ${CMAKE_BINARY_DIR}/theta /usr/local/bin/theta)")

# Link Binaryen to the test targets
foreach(TEST_SRC ${TEST_SRC_FILES})
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
target_link_libraries(${TEST_NAME} binaryen)
endforeach()
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ capsule DataStructures {
```sh
git submodule update --init --recursive
```
3. **Install depot_tools**
```sh
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH="$PATH:/path/to/depot_tools"
```
3. **Install Wasmer**
```sh
curl https://get.wasmer.io -sSfL | WASMER_DIR=lib/wasmer sh
Expand Down
15 changes: 15 additions & 0 deletions scripts/build_v8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

cd src/v8


# Generate build files
#gn gen out.gn/x64.release --args='dcheck_always_on=false is_component_build=false is_debug=false target_cpu="x64" use_custom_libcxx=false v8_monolithic=true v8_use_external_startup_data=false v8_enable_pointer_compression=false'


# Build V8
#ninja -C out.gn/x64.release v8_monolith #platform v8_init v8_libbase

gn gen out.gn/wee8 --args='is_component_build=false use_custom_libcxx=false v8_enable_fast_mksnapshot=true v8_enable_i18n_support=false v8_use_external_startup_data=false is_debug=false symbol_level=1 v8_enable_handle_zapping=false'

ninja -C out.gn/wee8 wee8
10 changes: 10 additions & 0 deletions scripts/fetch_v8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -x

if [ ! -d v8/build ]; then
echo "Fetching V8..."
fetch v8
else
echo "Syncing V8..."
gclient sync
fi
Loading

0 comments on commit a18add6

Please sign in to comment.