Skip to content
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
31 changes: 20 additions & 11 deletions behave-lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

# Set Compiler
SET (CMAKE_C_COMPILER "clang")
Expand All @@ -15,6 +15,7 @@ ENDIF()
# Optional JS
OPTION(BEHAVE_JS "Enable compiling to .js (WARNING: Emits a large file)" OFF)
OPTION(BEHAVE_WASM "Enable compiling to .wasm" ON)
OPTION(BEHAVE_WASM_OPTIMIZED "Enable optimized .wasm compilation" ON)
OPTION(BEHAVE_HTML "Enable compiling to .html" OFF)

IF(BEHAVE_JS)
Expand All @@ -39,10 +40,6 @@ set(SIG_SRC_DIR ${CMAKE_SOURCE_DIR}/include/cpp/sig-adapters/)
set(SIG_TEST_DIR ${CMAKE_SOURCE_DIR}/test/cpp/)
set(BEHAVE_LIB libbehave.a)

# set(DWARF_DIR ${CMAKE_SOURCE_DIR})
# NOTE: If you are using ubuntu through windows WSL set the DWARF_DIR to the
# file path to the WSL full path. Here's an example:
set(DWARF_DIR //wsl%24/Ubuntu/home/kcheung/work/code/behave-polylith/behave-lib)

set(EMCC_ARGS
-std=c++14
Expand All @@ -51,18 +48,30 @@ set(EMCC_ARGS
-I${EMSCRIPTEN_SRC_FILE}
-L${BEHAVE_LIB}
--post-js ${GLUE_JS_FILE}
-sNO_DISABLE_EXCEPTION_CATCHING
-sEXPORTED_FUNCTIONS=["_malloc","_free"]
-sEXPORTED_RUNTIME_METHODS=["UTF8ToString","allocateUTF8","addFunction","cwrap","ccall"])
-s NO_DISABLE_EXCEPTION_CATCHING
-s MODULARIZE
-s ENVIRONMENT=web
-s EXPORT_NAME="createModule"
-s EXPORTED_FUNCTIONS=["_malloc","_free"]
-s EXPORTED_RUNTIME_METHODS=["UTF8ToString","allocateUTF8","addFunction","cwrap","ccall"])

set(EMCC_JS_ARGS ${EMCC_ARGS} -s WASM=0)

set(EMCC_WASM_ARGS ${EMCC_ARGS}
--closure 1
-g
-s WASM=1
-s ALLOW_MEMORY_GROWTH
-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=${DWARF_DIR})
-s ALLOW_MEMORY_GROWTH)

IF(BEHAVE_WASM_OPTIMIZED)
set(EMCC_WASM_ARGS ${EMCC_WASM_ARGS} -O3)
ELSE()
set(EMCC_WASM_ARGS ${EMCC_WASM_ARGS} -g
-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=${DWARF_DIR})
set(DWARF_DIR ${CMAKE_SOURCE_DIR})
# NOTE: If you are using ubuntu through windows WSL set the DWARF_DIR to the
# file path to the WSL full path. Here's an example:
# set(DWARF_DIR //wsl%24/Ubuntu/home/kcheung/work/code/behave-polylith/behave-lib)
ENDIF()

project(behave LANGUAGES CXX)

Expand Down
3 changes: 1 addition & 2 deletions behave-lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ bind:
${WEBIDL} include/idl/behave.idl include/js/glue
mv include/js/glue.cpp include/cpp/emscripten/glue.cpp

compile: bind
compile: clean bind
emcmake cmake -B ${BUILD_DIR}
cmake --build ${BUILD_DIR}

install: compile
# cp build/behave.js ../projects/behave/resources/public/js
cp build/behave-min.* ../projects/behave/resources/public/js

test:
Expand Down
34 changes: 27 additions & 7 deletions behave-lib/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@

A new implementation of the extended Rothermel model.

* Requirements
** [[https://cmake.org/][CMake]]
** [[https://gcc.gnu.org/][GCC]]
** [[https://emscripten.org][Emscripten]]
* Setup
** Nix (Preferred)
*** Requirements

1. Install Nix
#+BEGIN_SRC bash
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
#+END_SRC

2. Enable Nix Flakes by adding the following to ~$HOME/.config/nix/nix.conf~
#+BEGIN_SRC conf :file ~/.config/nix/nix.conf
experimental-features = nix-command flakes
#+END_SRC

3. Enter the Nix Development Shell
#+BEGIN_SRC bash
nix develop
#+END_SRC

** Without Nix
*** Requirements
**** [[https://cmake.org/][CMake]]
**** [[https://gcc.gnu.org/][GCC]]
**** [[https://emscripten.org][Emscripten]]

*** Installation
**** OS X
#+begin_src sh
# Enable XCode (for MacOS)
xcode-install --install
Expand All @@ -25,12 +46,12 @@ export CXX=$(which clang++)
export WEBIDL=$(brew --prefix emscripten)/libexec/tools/webidl_binder
#+end_src

*** Building
* Building
#+BEGIN_SRC sh
make install
#+END_SRC

*** Process
* Process

The process of building this library takes multiple steps. At a high
level overview, we have leveraged two major tools:
Expand Down Expand Up @@ -77,7 +98,6 @@ The process is outlined in the [[https://gitlab.sig-gis.com/sig-gis/simple-emscr
#+RESULTS:
[[file:docs/behave-flow.png]]

***
* Debugging WASM (DWARF)

1. Install the chrome extension [[https://chrome.google.com/webstore/detail/cc%20%20-devtools-support-dwa/pdcpmagijalfljmkmjngeonclgbbannb][C/C++ DevTools Support (DWARF)]] mentioned [[https://developer.chrome.com/blog/wasm-debugging-2020/][here]].
Expand Down
61 changes: 61 additions & 0 deletions behave-lib/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions behave-lib/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
description = "Emscripten build environment";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
flake-utils.url = "github:numtide/flake-utils";
};

inputs.self.submodules = true;

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
gnumake
cmakeMinimal
llvm
clang
emscripten
fd # for finding webidl_binder.py
python315
];

shellHook = ''
export EM_CACHE=$PWD/.em_cache
export WEBIDL="python ${pkgs.emscripten}/share/emscripten/tools/webidl_binder.py"
echo "Environment ready!"
echo "EM_CACHE: $EM_CACHE"
echo "WEBIDL: $WEBIDL"
'';
};

# Convenience app to run the build
apps.default = {
type = "app";
program = toString (pkgs.writeShellScript "build.sh" ''
export EM_CACHE=$PWD/.em_cache
export WEBIDL="python ${pkgs.emscripten}/share/emscripten/tools/webidl_binder.py"
${pkgs.gnumake}/bin/make install
'');
};

# Convenience package for `nix build`
packages.default = pkgs.stdenv.mkDerivation {
pname = "behave-lib";
version = "0.1.0";
src = ./.;

nativeBuildInputs = with pkgs; [
gnumake
cmakeMinimal
llvm
clang
emscripten
fd
python315
];

configurePhase = ''
export EM_CACHE=$TMPDIR/.em_cache
export WEBIDL="python ${pkgs.emscripten}/share/emscripten/tools/webidl_binder.py"
mkdir -p build
cd build
${pkgs.cmakeMinimal}/bin/cmake ..
'';

buildPhase = ''
export EM_CACHE=$TMPDIR/.em_cache
export WEBIDL="python ${pkgs.emscripten}/share/emscripten/tools/webidl_binder.py"
${pkgs.gnumake}/bin/make
'';

installPhase = ''
mkdir -p $out
cp -r * $out/
'';
};
}
);
}
Loading