From 65cc6bc24f0042da0290e1370eaa8e5c79535bd8 Mon Sep 17 00:00:00 2001 From: "Javier G. Sogo" Date: Sun, 8 Sep 2019 11:57:06 +0200 Subject: [PATCH 01/10] compile to emscripten --- bindings/CMakeLists.txt | 19 +++++++++++++++---- conanfile.py | 11 ++++++++--- emscripten/build.sh | 7 +++++++ emscripten/profile | 12 ++++++++++++ examples/CMakeLists.txt | 24 +++++++++++++++++++----- library/crnd/CMakeLists.txt | 14 +++++++++++++- 6 files changed, 74 insertions(+), 13 deletions(-) create mode 100755 emscripten/build.sh create mode 100644 emscripten/profile diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt index bbd6311..52f87e5 100644 --- a/bindings/CMakeLists.txt +++ b/bindings/CMakeLists.txt @@ -1,6 +1,17 @@ cmake_minimum_required(VERSION 3.8.0) -add_subdirectory(cpp) -add_subdirectory(java) -add_subdirectory(go) -add_subdirectory(rust) +if(${BINDINGS_CPP}) + add_subdirectory(cpp) +endif() + +if(${BINDINGS_JAVA}) + add_subdirectory(java) +endif() + +if (${BINDINGS_GO}) + add_subdirectory(go) +endif() + +if (${BINDINGS_RUST}) + add_subdirectory(rust) +endif() diff --git a/conanfile.py b/conanfile.py index 9de4188..1b457b7 100644 --- a/conanfile.py +++ b/conanfile.py @@ -9,7 +9,7 @@ class CppInside(ConanFile): settings = "os", "arch", "compiler", "build_type" generators = "cmake" - options = {#"cpp": [True, False], # It is always generated + options = {"cpp": [True, False], # It is always generated "csharp": [True, False], "java": [True, False], "js": [True, False], @@ -19,7 +19,7 @@ class CppInside(ConanFile): "ruby": [True, False], "go": [True, False], } - default_options = {#"cpp": True, + default_options = {"cpp": True, "csharp": True, "java": True, "js": True, @@ -37,7 +37,7 @@ def build_requirements(self): def requirements(self): self.requires("protobuf/3.6.1@bincrafters/stable") - self.requires("boost/1.70.0@conan/stable") + #self.requires("boost/1.70.0@conan/stable") self.requires("spdlog/1.3.1@bincrafters/stable") def source(self): @@ -68,6 +68,11 @@ def source(self): def _cmake(self): cmake = CMake(self) + for it in ["cpp", "csharp", "java", "js", "objc", "php", "python", "ruby", "go"]: + cmake.definitions["bindings_{}".format(it).upper()] = bool(getattr(self.options, it)) + cmake.definitions["example_{}".format(it).upper()] = bool(getattr(self.options, it)) + + cmake.definitions["EMSCRIPTEN"] = bool(self.settings.os == "Emscripten") cmake.configure() return cmake diff --git a/emscripten/build.sh b/emscripten/build.sh new file mode 100755 index 0000000..ad1a3fe --- /dev/null +++ b/emscripten/build.sh @@ -0,0 +1,7 @@ +mkdir build +pushd build +conan install ../../conanfile.py --profile ../profile --build=missing -o csharp=False -o java=False -o objc=False -o php=False -o python=False -o ruby=False -o go=False -o cpp=False +conan source ../../conanfile.py --install-folder=. --source-folder=../../ +conan build ../../conanfile.py +conan package ../../conanfile.py --package-folder=../ +popd diff --git a/emscripten/profile b/emscripten/profile new file mode 100644 index 0000000..6f9e60e --- /dev/null +++ b/emscripten/profile @@ -0,0 +1,12 @@ +include(default) +[settings] +os=Emscripten +arch=wasm +compiler=clang +compiler.version=6.0 +compiler.libcxx=libc++ +[options] +[build_requires] +emsdk_installer/1.38.29@bincrafters/stable +[env] + diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a68811a..0c37d63 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,6 +1,20 @@ -add_subdirectory(cpp) -add_subdirectory(python) -add_subdirectory(java) -add_subdirectory(go) -add_subdirectory(rust) +if (${EXAMPLE_CPP}) + add_subdirectory(cpp) +endif() + +if (${EXAMPLE_PYTHON}) + add_subdirectory(python) +endif() + +if (${EXAMPLE_JAVA}) + add_subdirectory(java) +endif() + +if (${EXAMPLE_GO}) + add_subdirectory(go) +endif() + +if (${EXAMPLE_RUST}) + add_subdirectory(rust) +endif() diff --git a/library/crnd/CMakeLists.txt b/library/crnd/CMakeLists.txt index efb5a12..ff9bb42 100644 --- a/library/crnd/CMakeLists.txt +++ b/library/crnd/CMakeLists.txt @@ -23,4 +23,16 @@ target_compile_definitions(crnd PRIVATE SPDLOG_ACTIVE_LEVEL=0) #define SPDLOG_LEVEL_WARN 3 #define SPDLOG_LEVEL_ERROR 4 #define SPDLOG_LEVEL_CRITICAL 5 -#define SPDLOG_LEVEL_OFF 6 \ No newline at end of file +#define SPDLOG_LEVEL_OFF 6 + +if (${EMSCRIPTEN}) + set_target_properties(crnd PROPERTIES + SUFFIX ".html" + LINK_FLAGS "--emrun") + + install(FILES + ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libcrnd.js + ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libcrnd.wasm + DESTINATION bin) +endif() + From cd6941b37606f9387712bc394a2c5528079198f1 Mon Sep 17 00:00:00 2001 From: "Javier G. Sogo" Date: Sun, 8 Sep 2019 12:04:02 +0200 Subject: [PATCH 02/10] ignore files --- .gitignore | 4 ++++ emscripten/bin/index.html | 0 emscripten/build.sh | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 emscripten/bin/index.html diff --git a/.gitignore b/.gitignore index 3e93eb9..87af04f 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,7 @@ bindings/java/crnd/messages/ bindings/go/crnd.go bindings/rust/src/protos/*.rs bindings/rust/src/bindings.rs +*.js +*.wasm +conaninfo.txt +conanmanifest.txt diff --git a/emscripten/bin/index.html b/emscripten/bin/index.html new file mode 100644 index 0000000..e69de29 diff --git a/emscripten/build.sh b/emscripten/build.sh index ad1a3fe..c96b439 100755 --- a/emscripten/build.sh +++ b/emscripten/build.sh @@ -1,5 +1,5 @@ -mkdir build -pushd build +mkdir _build +pushd _build conan install ../../conanfile.py --profile ../profile --build=missing -o csharp=False -o java=False -o objc=False -o php=False -o python=False -o ruby=False -o go=False -o cpp=False conan source ../../conanfile.py --install-folder=. --source-folder=../../ conan build ../../conanfile.py From 54fda6bac7a47c799ec942e2364efcce7c001e1c Mon Sep 17 00:00:00 2001 From: "Javier G. Sogo" Date: Sun, 8 Sep 2019 12:11:55 +0200 Subject: [PATCH 03/10] activate and run --- .gitignore | 1 + emscripten/build.sh | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 87af04f..e8bfdaa 100644 --- a/.gitignore +++ b/.gitignore @@ -47,5 +47,6 @@ bindings/rust/src/protos/*.rs bindings/rust/src/bindings.rs *.js *.wasm +*.html conaninfo.txt conanmanifest.txt diff --git a/emscripten/build.sh b/emscripten/build.sh index c96b439..0f2aeaf 100755 --- a/emscripten/build.sh +++ b/emscripten/build.sh @@ -1,7 +1,11 @@ mkdir _build + pushd _build -conan install ../../conanfile.py --profile ../profile --build=missing -o csharp=False -o java=False -o objc=False -o php=False -o python=False -o ruby=False -o go=False -o cpp=False +conan install ../../conanfile.py -g virtualenv --profile ../profile --build=missing -o csharp=False -o java=False -o objc=False -o php=False -o python=False -o ruby=False -o go=False -o cpp=False conan source ../../conanfile.py --install-folder=. --source-folder=../../ conan build ../../conanfile.py conan package ../../conanfile.py --package-folder=../ popd + +source _build/activate.sh +emrun ./bin/libcrnd.html From 1b0289ddddf3ff6a9f98676fd9fde374391321ee Mon Sep 17 00:00:00 2001 From: "Javier G. Sogo" Date: Sun, 8 Sep 2019 13:44:39 +0200 Subject: [PATCH 04/10] first call from C --- emscripten/bin/index.html | 39 +++++++++++++++++++++++++++++++++++++ emscripten/build.sh | 4 +--- emscripten/run.sh | 3 +++ library/crnd/CMakeLists.txt | 16 ++++++++++++++- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100755 emscripten/run.sh diff --git a/emscripten/bin/index.html b/emscripten/bin/index.html index e69de29..27cf8f1 100644 --- a/emscripten/bin/index.html +++ b/emscripten/bin/index.html @@ -0,0 +1,39 @@ + + + + + + + + diff --git a/emscripten/build.sh b/emscripten/build.sh index 0f2aeaf..0707e0e 100755 --- a/emscripten/build.sh +++ b/emscripten/build.sh @@ -1,3 +1,4 @@ +rm -fr _build mkdir _build pushd _build @@ -6,6 +7,3 @@ conan source ../../conanfile.py --install-folder=. --source-folder=../../ conan build ../../conanfile.py conan package ../../conanfile.py --package-folder=../ popd - -source _build/activate.sh -emrun ./bin/libcrnd.html diff --git a/emscripten/run.sh b/emscripten/run.sh new file mode 100755 index 0000000..bc4170d --- /dev/null +++ b/emscripten/run.sh @@ -0,0 +1,3 @@ + +source _build/activate.sh +emrun ./bin/index.html diff --git a/library/crnd/CMakeLists.txt b/library/crnd/CMakeLists.txt index ff9bb42..6eaed1d 100644 --- a/library/crnd/CMakeLists.txt +++ b/library/crnd/CMakeLists.txt @@ -26,8 +26,22 @@ target_compile_definitions(crnd PRIVATE SPDLOG_ACTIVE_LEVEL=0) #define SPDLOG_LEVEL_OFF 6 if (${EMSCRIPTEN}) + message("CMAKE_C_COMPILER: ${CMAKE_C_COMPILER}") + message("CMAKE_CPP_COMPILER: ${CMAKE_CPP_COMPILER}") + + set(EXPORTED_FUNCTIONS + _help + _sample + ) + + # process exported functions + set(EXPORTED_FUNCTIONS_STR "") + list(JOIN EXPORTED_FUNCTIONS "," EXPORTED_FUNCTIONS_STR) + + set(CMAKE_CXX_FLAGS "-s EXPORTED_FUNCTIONS=\"[${EXPORTED_FUNCTIONS_STR}]\" -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\", \"cwrap\", \"addFunction\", \"removeFunction\"]' -s RESERVED_FUNCTION_POINTERS=10") + set_target_properties(crnd PROPERTIES - SUFFIX ".html" + SUFFIX ".js" LINK_FLAGS "--emrun") install(FILES From a0b65503f7231a5fcadba5f0bd7aa46ac0178773 Mon Sep 17 00:00:00 2001 From: "Javier G. Sogo" Date: Sun, 8 Sep 2019 13:48:53 +0200 Subject: [PATCH 05/10] named vars --- emscripten/bin/index.html | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/emscripten/bin/index.html b/emscripten/bin/index.html index 27cf8f1..8ff6531 100644 --- a/emscripten/bin/index.html +++ b/emscripten/bin/index.html @@ -5,7 +5,7 @@ From 8b22ef2b984b7bf1eae5b6a15daa1201ffdb23b7 Mon Sep 17 00:00:00 2001 From: "Javier G. Sogo" Date: Sun, 8 Sep 2019 17:01:41 +0200 Subject: [PATCH 06/10] export module func --- conanfile.py | 6 +++++- emscripten/bin/index.html | 4 ++++ emscripten/build.sh | 2 ++ emscripten/main.js | 4 ++++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 emscripten/main.js diff --git a/conanfile.py b/conanfile.py index 1b457b7..3957783 100644 --- a/conanfile.py +++ b/conanfile.py @@ -61,7 +61,11 @@ def source(self): else: binding_folder = os.path.join(self.source_folder, "bindings", it, "messages") os.makedirs(binding_folder, exist_ok=True) - command += " --{}_out={}".format(it, binding_folder) + + if it == 'js': + command += " --js_out=import_style=commonjs,binary:{}".format(binding_folder) + else: + command += " --{}_out={}".format(it, binding_folder) command += " {}".format(" ".join(messages)) with tools.environment_append(env): self.run(command) diff --git a/emscripten/bin/index.html b/emscripten/bin/index.html index 8ff6531..fc97551 100644 --- a/emscripten/bin/index.html +++ b/emscripten/bin/index.html @@ -1,12 +1,16 @@ + @@ -19,7 +19,7 @@ function sample_callback(state, data, status) { console.log("> js::sample_callback") var message = crnd.parse_sample(data); - + draw_histogram(message.getSamplesList(), "LOGNORMAL (3, 0.2)"); console.log("< js::sample_callback") } @@ -61,7 +61,50 @@ removeFunction(pointer); } +function draw_histogram(rolls, title) { + var nstars = 300; + var nclasses = 20; + + var p = [].fill.call({ length: nclasses+1 }, 0); + var max = Math.max.apply(null, rolls); + var min = Math.min.apply(null, rolls); + + console.log("max: " + max); + console.log("min: " + min); + + var step = (max-min)/nclasses; + console.log("step: " + step); + + rolls.forEach(function (elem, index) { + idx = Math.round((elem-min)/step); + //console.log(idx); + p[idx] += 1; + }); + + var h3 = $('

').text(title); + $("#histogram").append(h3); + + var table = $('
') + + console.log(title); + for (var i = 0; i < nclasses; i++) { + var axis_x = (min+i*step+step/2).toFixed(6).padStart(9, "0"); + var npstars = Math.round(p[i]*nstars/rolls.length); + + console.log(axis_x + ": " + "*".repeat(npstars)); + table.append("" + axis_x + "" + "*".repeat(npstars) + ""); + } + $("#histogram").append(table); +} + + + + +
+ +
+ From 3dd59c6a3927618429d61d89411a0a7f69a1ecad Mon Sep 17 00:00:00 2001 From: "Javier G. Sogo" Date: Sun, 8 Sep 2019 20:19:08 +0200 Subject: [PATCH 10/10] remove some logs --- emscripten/bin/index.html | 6 ------ 1 file changed, 6 deletions(-) diff --git a/emscripten/bin/index.html b/emscripten/bin/index.html index 30b8946..3412a8c 100644 --- a/emscripten/bin/index.html +++ b/emscripten/bin/index.html @@ -68,16 +68,10 @@ var p = [].fill.call({ length: nclasses+1 }, 0); var max = Math.max.apply(null, rolls); var min = Math.min.apply(null, rolls); - - console.log("max: " + max); - console.log("min: " + min); - var step = (max-min)/nclasses; - console.log("step: " + step); rolls.forEach(function (elem, index) { idx = Math.round((elem-min)/step); - //console.log(idx); p[idx] += 1; });