Skip to content

Commit

Permalink
Fix wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
tharun571 committed Sep 5, 2024
1 parent cb234b0 commit 39dc4fb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
24 changes: 21 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ macro(xeus_cpp_create_target target_name linkage output_name)
set(XEUS_CPP_XEUS_TARGET xeus-static)
endif ()

#This is a workaround for the issue with the libcurl target on Windows specifically for xassist
# This is a workaround for the issue with the libcurl target on Windows specifically for xassist
if(WIN32)
# Set the MSVC runtime library
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
Expand All @@ -338,9 +338,27 @@ macro(xeus_cpp_create_target target_name linkage output_name)
if (MSVC)
target_compile_options(${target_name} PRIVATE "/MD$<$<CONFIG:Debug>:d>")
endif()
elseif(EMSCRIPTEN)
# Include Emscripten Fetch API
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s FETCH=1")

# Add Emscripten-specific compile options
add_compile_options(-s FETCH=1)

# Add Emscripten-specific link options
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s FETCH=1")

# Add Emscripten Fetch library
target_link_libraries(${target_name} PRIVATE fetch)
else()
# Curl initialised specifically for xassist
target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse curl)
# Curl initialized specifically for xassist
find_package(CURL REQUIRED)
if (CURL_FOUND)
target_include_directories(${target_name} PRIVATE ${CURL_INCLUDE_DIRS})
target_link_libraries(${target_name} PRIVATE ${CURL_LIBRARIES})
endif()

target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse)
endif()

if (WIN32 OR CYGWIN)
Expand Down
56 changes: 56 additions & 0 deletions src/xmagics/xassist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
************************************************************************************/
#include "xassist.hpp"

#ifdef __EMSCRIPTEN__
#include <emscripten/fetch.h>
#else
#define CURL_STATICLIB
#include <curl/curl.h>
#include <fstream>
Expand Down Expand Up @@ -135,6 +138,58 @@ namespace xcpp
}
};

#ifdef __EMSCRIPTEN__
class CurlHelper
{
public:
CurlHelper() = default;
~CurlHelper() = default;

CurlHelper(const CurlHelper&) = delete;
CurlHelper& operator=(const CurlHelper&) = delete;
CurlHelper(CurlHelper&&) = delete;
CurlHelper& operator=(CurlHelper&&) = delete;

std::string performRequest(const std::string& url, const std::string& postData, const std::string& authHeader = "")
{
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "POST");
attr.requestData = postData.c_str();
attr.requestDataSize = postData.size();
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;

if (!authHeader.empty())
{
attr.requestHeaders = new const char*[2];
attr.requestHeaders[0] = authHeader.c_str();
attr.requestHeaders[1] = nullptr;
}

std::string response;
attr.onsuccess = [](emscripten_fetch_t* fetch)
{
response.assign(fetch->data, fetch->numBytes);
emscripten_fetch_close(fetch);
};

attr.onerror = [](emscripten_fetch_t* fetch)
{
std::cerr << "Fetch failed: " << fetch->status << std::endl;
emscripten_fetch_close(fetch);
};

emscripten_fetch(&attr, url.c_str());

if (!authHeader.empty())
{
delete[] attr.requestHeaders;
}

return response;
}
};
#else
class CurlHelper
{
private:
Expand Down Expand Up @@ -205,6 +260,7 @@ namespace xcpp
return response;
}
};
#endif

std::string gemini(const std::string& cell, const std::string& key)
{
Expand Down

0 comments on commit 39dc4fb

Please sign in to comment.