Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for jupyterlite-kernel #119

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ OPTION(XSQL_USE_SHARED_XEUS_SQLITE "Link xsqlite with the xeus-sqlite shared lib

OPTION(XSQL_DOWNLOAD_GTEST "build gtest from downloaded sources" OFF)
OPTION(XSQL_BUILD_TESTS "xeus-sqlite test suite" OFF)
OPTION(XSQL_EMSCRIPTEN_WASM_BUILD "Build for wasm with emscripten" OFF)

if(XSQL_EMSCRIPTEN_WASM_BUILD)
# for the emscripten build we need a FindSQLite3.cmake since
# we install sqlite in a non-standart way
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake_emscripten;${CMAKE_MODULE_PATH}")
endif()

# Dependencies
# ============
Expand Down Expand Up @@ -90,6 +97,7 @@ set(XEUS_SQLITE_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(XEUS_SQLITE_SRC
${XEUS_SQLITE_SRC_DIR}/xeus_sqlite_interpreter.cpp
${XEUS_SQLITE_SRC_DIR}/xvega_sqlite.cpp
${XEUS_SQLITE_SRC_DIR}/xlite.cpp
)

set(XEUS_SQLITE_HEADERS
Expand All @@ -110,6 +118,25 @@ string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE)

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib; ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")


if(XSQL_EMSCRIPTEN_WASM_BUILD)
add_compile_definitions(XSQL_EMSCRIPTEN_WASM_BUILD)
include(XeusEmscriptenWasmFlags)

# fetch
set(XEUS_EMSCRIPTEN_WASM_FLAGS "${XEUS_EMSCRIPTEN_WASM_FLAGS} -s FETCH=1 -s NO_EXIT_RUNTIME=1")

# async
set(XEUS_EMSCRIPTEN_WASM_FLAGS "${XEUS_EMSCRIPTEN_WASM_FLAGS} --bind --std=c++17")
set(XEUS_EMSCRIPTEN_WASM_FLAGS "${XEUS_EMSCRIPTEN_WASM_FLAGS} -lidbfs.js")
set(XEUS_EMSCRIPTEN_WASM_FLAGS "${XEUS_EMSCRIPTEN_WASM_FLAGS} -s ASYNCIFY=1 -s 'ASYNCIFY_IMPORTS=[\"async_ems_init_idbfs\", \"async_ems_sync_db\"]'")
set(XEUS_EMSCRIPTEN_WASM_FLAGS "${XEUS_EMSCRIPTEN_WASM_FLAGS} -s 'ASYNCIFY_STACK_SIZE=20480'")
set(XEUS_EMSCRIPTEN_WASM_FLAGS "${XEUS_EMSCRIPTEN_WASM_FLAGS} -s FORCE_FILESYSTEM=1")
set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${XEUS_EMSCRIPTEN_WASM_FLAGS} -fno-stack-protector")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${XEUS_EMSCRIPTEN_WASM_FLAGS} ---proxy-to-worker")
endif()

macro(xsql_set_common_options target_name)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
Expand Down Expand Up @@ -221,8 +248,10 @@ macro(xsql_create_target target_name linkage output_name)
SQLite::SQLite3
)

# find_package(Threads) # TODO: add Threads as a dependence of xeus-static?
target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
if(NOT XSQL_EMSCRIPTEN_WASM_BUILD)
# find_package(Threads) # TODO: add Threads as a dependence of xeus-static?
target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
endif()
endmacro()

# xeus-sqlite
Expand Down Expand Up @@ -280,6 +309,12 @@ if(XSQL_BUILD_TESTS)
add_subdirectory(test)
endif()

if(XSQL_EMSCRIPTEN_WASM_BUILD)
add_executable(xeus_kernel src/main_emscripten_kernel.cpp )
target_link_libraries(xeus_kernel xeus-static xeus-sqlite-static)
target_compile_features(xeus_kernel PRIVATE cxx_std_17)
endif()

# Installation
# ============

Expand Down
206 changes: 206 additions & 0 deletions cmake_emscripten/FindSQLite3.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# slightly modified version of: https://github.com/Kitware/CMake/blob/master/Modules/FindSQLite3.cmake

# CMake - Cross Platform Makefile Generator
# Copyright 2000-2022 Kitware, Inc. and Contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of Kitware, Inc. nor the names of Contributors
# may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ------------------------------------------------------------------------------
#
# The following individuals and institutions are among the Contributors:
#
# * Aaron C. Meadows <[email protected]>
# * Adriaan de Groot <[email protected]>
# * Aleksey Avdeev <[email protected]>
# * Alexander Neundorf <[email protected]>
# * Alexander Smorkalov <[email protected]>
# * Alexey Sokolov <[email protected]>
# * Alex Merry <[email protected]>
# * Alex Turbov <[email protected]>
# * Andreas Pakulat <[email protected]>
# * Andreas Schneider <[email protected]>
# * André Rigland Brodtkorb <[email protected]>
# * Axel Huebl, Helmholtz-Zentrum Dresden - Rossendorf
# * Benjamin Eikel
# * Bjoern Ricks <[email protected]>
# * Brad Hards <[email protected]>
# * Christopher Harvey
# * Christoph Grüninger <[email protected]>
# * Clement Creusot <[email protected]>
# * Daniel Blezek <[email protected]>
# * Daniel Pfeifer <[email protected]>
# * Enrico Scholz <[email protected]>
# * Eran Ifrah <[email protected]>
# * Esben Mose Hansen, Ange Optimization ApS
# * Geoffrey Viola <[email protected]>
# * Google Inc
# * Gregor Jasny
# * Helio Chissini de Castro <[email protected]>
# * Ilya Lavrenov <[email protected]>
# * Insight Software Consortium <insightsoftwareconsortium.org>
# * Intel Corporation <www.intel.com>
# * Jan Woetzel
# * Jordan Williams <[email protected]>
# * Julien Schueller
# * Kelly Thompson <[email protected]>
# * Konstantin Podsvirov <[email protected]>
# * Laurent Montel <[email protected]>
# * Mario Bensi <[email protected]>
# * Martin Gräßlin <[email protected]>
# * Mathieu Malaterre <[email protected]>
# * Matthaeus G. Chajdas
# * Matthias Kretz <[email protected]>
# * Matthias Maennich <[email protected]>
# * Michael Hirsch, Ph.D. <www.scivision.co>
# * Michael Stürmer
# * Miguel A. Figueroa-Villanueva
# * Mike Jackson
# * Mike McQuaid <[email protected]>
# * Nicolas Bock <[email protected]>
# * Nicolas Despres <[email protected]>
# * Nikita Krupen'ko <[email protected]>
# * NVIDIA Corporation <www.nvidia.com>
# * OpenGamma Ltd. <opengamma.com>
# * Patrick Stotko <[email protected]>
# * Per Øyvind Karlsen <[email protected]>
# * Peter Collingbourne <[email protected]>
# * Petr Gotthard <[email protected]>
# * Philip Lowman <[email protected]>
# * Philippe Proulx <[email protected]>
# * Raffi Enficiaud, Max Planck Society
# * Raumfeld <raumfeld.com>
# * Roger Leigh <[email protected]>
# * Rolf Eike Beer <[email protected]>
# * Roman Donchenko <[email protected]>
# * Roman Kharitonov <[email protected]>
# * Ruslan Baratov
# * Sebastian Holtermann <[email protected]>
# * Stephen Kelly <[email protected]>
# * Sylvain Joubert <[email protected]>
# * The Qt Company Ltd.
# * Thomas Sondergaard <[email protected]>
# * Tobias Hunger <[email protected]>
# * Todd Gamblin <[email protected]>
# * Tristan Carel
# * University of Dundee
# * Vadim Zhukov
# * Will Dicharry <[email protected]>
#
# See version control history for details of individual contributions.
#
# The above copyright and license notice applies to distributions of
# CMake in source and binary form. Third-party software packages supplied
# with CMake under compatible licenses provide their own copyright notices
# documented in corresponding subdirectories or source files.
#
# ------------------------------------------------------------------------------
#
# CMake was initially developed by Kitware with the following sponsorship:
#
# * National Library of Medicine at the National Institutes of Health
# as part of the Insight Segmentation and Registration Toolkit (ITK).
#
# * US National Labs (Los Alamos, Livermore, Sandia) ASC Parallel
# Visualization Initiative.
#
# * National Alliance for Medical Image Computing (NAMIC) is funded by the
# National Institutes of Health through the NIH Roadmap for Medical Research,
# Grant U54 EB005149.
#
# * Kitware, Inc.


# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

#[=======================================================================[.rst:
FindSQLite3
-----------

.. versionadded:: 3.14

Find the SQLite libraries, v3

IMPORTED targets
^^^^^^^^^^^^^^^^

This module defines the following :prop_tgt:`IMPORTED` target:

``SQLite::SQLite3``

Result variables
^^^^^^^^^^^^^^^^

This module will set the following variables if found:

``SQLite3_INCLUDE_DIRS``
where to find sqlite3.h, etc.
``SQLite3_LIBRARIES``
the libraries to link against to use SQLite3.
``SQLite3_VERSION``
version of the SQLite3 library found
``SQLite3_FOUND``
TRUE if found

#]=======================================================================]
INCLUDE(FindPackageHandleStandardArgs)

# Look for the necessary header
find_path(SQLite3_INCLUDE_DIR NAMES sqlite3.h)
mark_as_advanced(SQLite3_INCLUDE_DIR)

# Look for the necessary library
find_library(SQLite3_LIBRARY NAMES sqlite3 sqlite)
mark_as_advanced(SQLite3_LIBRARY)

# Extract version information from the header file
if(SQLite3_INCLUDE_DIR)
file(STRINGS ${SQLite3_INCLUDE_DIR}/sqlite3.h _ver_line
REGEX "^#define SQLITE_VERSION *\"[0-9]+\\.[0-9]+\\.[0-9]+\""
LIMIT_COUNT 1)
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+"
SQLite3_VERSION "${_ver_line}")
unset(_ver_line)
endif()

find_package_handle_standard_args(SQLite3
REQUIRED_VARS SQLite3_INCLUDE_DIR SQLite3_LIBRARY
VERSION_VAR SQLite3_VERSION)

# Create the imported target
if(SQLite3_FOUND)
set(SQLite3_INCLUDE_DIRS ${SQLite3_INCLUDE_DIR})
set(SQLite3_LIBRARIES ${SQLite3_LIBRARY})
if(NOT TARGET SQLite::SQLite3)
add_library(SQLite::SQLite3 UNKNOWN IMPORTED)
set_target_properties(SQLite::SQLite3 PROPERTIES
IMPORTED_LOCATION "${SQLite3_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SQLite3_INCLUDE_DIR}")
endif()
endif()
2 changes: 1 addition & 1 deletion include/xeus-sqlite/xeus_sqlite_interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace xeus_sqlite

public:

interpreter() = default;
interpreter();
virtual ~interpreter() = default;

private:
Expand Down
25 changes: 25 additions & 0 deletions src/main_emscripten_kernel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/***************************************************************************
* Copyright (c) 2022, Thorsten Beier * *
* Copyright (c) 2022, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <iostream>
#include <memory>


#include <emscripten/bind.h>

#include "xeus-sqlite/xeus_sqlite_interpreter.hpp"
#include "xeus/xembind.hpp"


EMSCRIPTEN_BINDINGS(my_module) {
xeus::export_core();
using interpreter_type = xeus_sqlite::interpreter;
xeus::export_kernel<interpreter_type>("xkernel");
}

33 changes: 32 additions & 1 deletion src/xeus_sqlite_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
#include <SQLiteCpp/VariadicBind.h>
#include <SQLiteCpp/SQLiteCpp.h>

#ifdef XSQL_EMSCRIPTEN_WASM_BUILD
// implemented in xlite.cpp
namespace xeus_lite
{
void fetch(const std::string url, const std::string filename);
void ems_init_idbfs(const std::string & path);
void ems_sync_db();
}
#endif


namespace xeus_sqlite
{

Expand All @@ -37,6 +48,11 @@ namespace xeus_sqlite
return std::isalpha(c) || std::isdigit(c) || c == '_';
}

interpreter::interpreter()
{
xeus::register_interpreter(this);
}

void interpreter::load_db(const std::vector<std::string> tokenized_input)
{
/*
Expand Down Expand Up @@ -72,7 +88,7 @@ namespace xeus_sqlite
void interpreter::create_db(const std::vector<std::string> tokenized_input)
{
m_bd_is_loaded = true;
m_db_path = tokenized_input[2];
m_db_path = tokenized_input[1];

/* Creates the file */
std::ofstream(m_db_path.c_str()).close();
Expand Down Expand Up @@ -193,6 +209,21 @@ namespace xeus_sqlite
{
return create_db(tokenized_input);
}
#ifdef XSQL_EMSCRIPTEN_WASM_BUILD
else if (xv_bindings::case_insentive_equals(tokenized_input[0], "FETCH"))
{
return xeus_lite::fetch(tokenized_input[1] /*url*/, tokenized_input[2] /*filename*/);
}
else if (xv_bindings::case_insentive_equals(tokenized_input[0], "PUSH_TO_IDBFS"))
{
return xeus_lite::ems_sync_db();
}
else if (xv_bindings::case_insentive_equals(tokenized_input[0], "SET_IDBFS_DIR"))
{
return xeus_lite::ems_init_idbfs(tokenized_input[1]);
}
#endif

if (m_bd_is_loaded)
{
if (xv_bindings::case_insentive_equals(tokenized_input[0], "DELETE"))
Expand Down
Loading