Skip to content

Commit

Permalink
Add emscripten logic
Browse files Browse the repository at this point in the history
Based on the CuraEngine patch done by Wakeful-Cloud. Not sure if we want
to go the same route but it will give us a quick way forward.
https://github.com/Cloud-CNC/cura-wasm

Doing the conan install with the following profile
```
include(cura.jinja)

[tool_requires]
emsdk/3.1.50

[settings]
os=Emscripten
arch=wasm
```
should do the trick in building it with emsdk. Still very much WIP!

Contributes to NP-5

Co-authored-by: Wakeful-Cloud <[email protected]>
  • Loading branch information
jellespijker and Wakeful-Cloud committed Jan 22, 2024
1 parent 1247d25 commit 893f56b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ set(engine_SRCS # Except main.cpp.
src/utils/ToolpathVisualizer.cpp
src/utils/VoronoiUtils.cpp
src/utils/VoxelUtils.cpp
)
)

add_library(_CuraEngine STATIC ${engine_SRCS} ${engine_PB_SRCS})
#use_threads(_CuraEngine)
Expand All @@ -162,7 +162,7 @@ target_include_directories(_CuraEngine
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # Include Cura.pb.h
)
)

target_compile_definitions(_CuraEngine
PUBLIC
Expand All @@ -180,7 +180,7 @@ target_compile_definitions(_CuraEngine
$<$<CONFIG:RelWithDebInfo>:ASSERT_INSANE_OUTPUT>
$<$<CONFIG:RelWithDebInfo>:USE_CPU_TIME>
$<$<CONFIG:RelWithDebInfo>:DEBUG>
)
)

enable_sanitizers(_CuraEngine)

Expand All @@ -189,7 +189,7 @@ if (${EXTENSIVE_WARNINGS})
endif ()

if (ENABLE_ARCUS)
target_link_libraries(_CuraEngine PUBLIC arcus::arcus )
target_link_libraries(_CuraEngine PUBLIC arcus::arcus)
endif ()

find_package(clipper REQUIRED)
Expand Down Expand Up @@ -242,7 +242,7 @@ else ()
SET(CMAKE_RC_COMPILER_INIT windres)
SET(CMAKE_RC_COMPILE_OBJECT
"<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>"
)
)
endif ()
add_executable(CuraEngine src/main.cpp ${RES_FILES}) # ..., but don't forget the glitter!
if (ENABLE_SENTRY)
Expand Down Expand Up @@ -293,7 +293,7 @@ if (ENABLE_BENCHMARKS)
add_subdirectory(benchmark)
if (NOT WIN32)
add_subdirectory(stress_benchmark)
endif()
endif ()
endif ()

if (ENABLE_TESTING)
Expand Down
3 changes: 3 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def generate(self):
tc.variables["ENABLE_PLUGINS"] = self.options.enable_plugins
tc.generate()

if self.settings.arch == "wasm" and self.settings.os == "Emscripten":
self.buildenv.define("EMCC_CFLAGS", "-s ALLOW_MEMORY_GROWTH=1 -s EXPORT_ES6=1 -s EXPORT_NAME='CuraEngine' -s EXPORTED_RUNTIME_METHODS='[\"callMain\", \"FS\"]' -s INVOKE_RUN=0 -s MODULARIZE=1 -s SINGLE_FILE=1 -s ENVIRONMENT=worker -s USE_ES6_IMPORT_META=0")

for dep in self.dependencies.values():
if len(dep.cpp_info.libdirs) > 0:
copy(self, "*.dylib", dep.cpp_info.libdirs[0], self.build_folder)
Expand Down
8 changes: 6 additions & 2 deletions include/communication/CommandLine.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2018-2022 Ultimaker B.V.
// CuraEngine is released under the terms of the AGPLv3 or higher.
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#ifndef COMMANDLINE_H
#define COMMANDLINE_H
Expand Down Expand Up @@ -151,6 +151,10 @@ class CommandLine : public Communication
void sliceNext() override;

private:
#ifdef __EMSCRIPTEN__
std::string progressHandler;
#endif

/*
* \brief The command line arguments that the application was called with.
*/
Expand Down
21 changes: 20 additions & 1 deletion src/communication/CommandLine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Ultimaker B.V.
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#include "communication/CommandLine.h"
Expand All @@ -21,6 +21,10 @@
#include "Slice.h"
#include "utils/Matrix4x3D.h" //For the mesh_rotation_matrix setting.

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

namespace cura
{

Expand Down Expand Up @@ -106,6 +110,12 @@ void CommandLine::sendProgress(double progress) const
return;
}
// TODO: Do we want to print a progress bar? We'd need a better solution to not have that progress bar be ruined by any logging.
#ifdef __EMSCRIPTEN__
// Call progress handler with progress
char js[100];
std::sprintf(js, "globalThis[\"%s\"](%f)", progressHandler.c_str(), progress);
emscripten_run_script(js);
#endif
}

void CommandLine::sliceNext()
Expand Down Expand Up @@ -178,6 +188,15 @@ void CommandLine::sliceNext()
force_read_parent = false;
force_read_nondefault = false;
}
#ifdef __EMSCRIPTEN__
else if (argument.find("--progress") == 0)
{
// Store progress handler name
argument_index++;
argument = arguments_[argument_index];
progressHandler = argument;
}
#endif
else
{
spdlog::error("Unknown option: {}", argument);
Expand Down

0 comments on commit 893f56b

Please sign in to comment.