Skip to content

Commit

Permalink
Merge pull request #119 from DerThorsten/jupyter_lite_kernel
Browse files Browse the repository at this point in the history
changes for jupyterlite-kernel
  • Loading branch information
marimeireles committed Feb 8, 2022
2 parents 9bcca66 + f8f2a59 commit b0a754d
Show file tree
Hide file tree
Showing 6 changed files with 448 additions and 4 deletions.
39 changes: 37 additions & 2 deletions CMakeLists.txt
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
@@ -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 <cmake@shadowguarddev.com>
# * Adriaan de Groot <groot@kde.org>
# * Aleksey Avdeev <solo@altlinux.ru>
# * Alexander Neundorf <neundorf@kde.org>
# * Alexander Smorkalov <alexander.smorkalov@itseez.com>
# * Alexey Sokolov <sokolov@google.com>
# * Alex Merry <alex.merry@kde.org>
# * Alex Turbov <i.zaufi@gmail.com>
# * Andreas Pakulat <apaku@gmx.de>
# * Andreas Schneider <asn@cryptomilk.org>
# * André Rigland Brodtkorb <Andre.Brodtkorb@ifi.uio.no>
# * Axel Huebl, Helmholtz-Zentrum Dresden - Rossendorf
# * Benjamin Eikel
# * Bjoern Ricks <bjoern.ricks@gmail.com>
# * Brad Hards <bradh@kde.org>
# * Christopher Harvey
# * Christoph Grüninger <foss@grueninger.de>
# * Clement Creusot <creusot@cs.york.ac.uk>
# * Daniel Blezek <blezek@gmail.com>
# * Daniel Pfeifer <daniel@pfeifer-mail.de>
# * Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
# * Eran Ifrah <eran.ifrah@gmail.com>
# * Esben Mose Hansen, Ange Optimization ApS
# * Geoffrey Viola <geoffrey.viola@asirobots.com>
# * Google Inc
# * Gregor Jasny
# * Helio Chissini de Castro <helio@kde.org>
# * Ilya Lavrenov <ilya.lavrenov@itseez.com>
# * Insight Software Consortium <insightsoftwareconsortium.org>
# * Intel Corporation <www.intel.com>
# * Jan Woetzel
# * Jordan Williams <jordan@jwillikers.com>
# * Julien Schueller
# * Kelly Thompson <kgt@lanl.gov>
# * Konstantin Podsvirov <konstantin@podsvirov.pro>
# * Laurent Montel <montel@kde.org>
# * Mario Bensi <mbensi@ipsquad.net>
# * Martin Gräßlin <mgraesslin@kde.org>
# * Mathieu Malaterre <mathieu.malaterre@gmail.com>
# * Matthaeus G. Chajdas
# * Matthias Kretz <kretz@kde.org>
# * Matthias Maennich <matthias@maennich.net>
# * Michael Hirsch, Ph.D. <www.scivision.co>
# * Michael Stürmer
# * Miguel A. Figueroa-Villanueva
# * Mike Jackson
# * Mike McQuaid <mike@mikemcquaid.com>
# * Nicolas Bock <nicolasbock@gmail.com>
# * Nicolas Despres <nicolas.despres@gmail.com>
# * Nikita Krupen'ko <krnekit@gmail.com>
# * NVIDIA Corporation <www.nvidia.com>
# * OpenGamma Ltd. <opengamma.com>
# * Patrick Stotko <stotko@cs.uni-bonn.de>
# * Per Øyvind Karlsen <peroyvind@mandriva.org>
# * Peter Collingbourne <peter@pcc.me.uk>
# * Petr Gotthard <gotthard@honeywell.com>
# * Philip Lowman <philip@yhbt.com>
# * Philippe Proulx <pproulx@efficios.com>
# * Raffi Enficiaud, Max Planck Society
# * Raumfeld <raumfeld.com>
# * Roger Leigh <rleigh@codelibre.net>
# * Rolf Eike Beer <eike@sf-mail.de>
# * Roman Donchenko <roman.donchenko@itseez.com>
# * Roman Kharitonov <roman.kharitonov@itseez.com>
# * Ruslan Baratov
# * Sebastian Holtermann <sebholt@xwmw.org>
# * Stephen Kelly <steveire@gmail.com>
# * Sylvain Joubert <joubert.sy@gmail.com>
# * The Qt Company Ltd.
# * Thomas Sondergaard <ts@medical-insight.com>
# * Tobias Hunger <tobias.hunger@qt.io>
# * Todd Gamblin <tgamblin@llnl.gov>
# * Tristan Carel
# * University of Dundee
# * Vadim Zhukov
# * Will Dicharry <wdicharry@stellarscience.com>
#
# 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
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
@@ -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
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

0 comments on commit b0a754d

Please sign in to comment.