Skip to content

Commit

Permalink
Completely new wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Jul 24, 2024
1 parent a7d70b6 commit 68e5d54
Show file tree
Hide file tree
Showing 33 changed files with 1,333 additions and 6,070 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ cd niimath/src
make
```

You can also compile this project to Web Assembly so it can be embedded in a web page, as shown in the [live demo](https://niivue.github.io/niivue-niimath/).

```
git clone https://github.com/rordenlab/niimath.git
cd niimath/src
make wasm
```

Advanced users may want to run `CF=1 OMP=1 make -j` to make a version that uses OpenMP (parallel processing) and the CloudFlare accelerated compression library. You may need to edit the `Makefile` for your compiler name. On MacOS, the default C compiler is Clang, which has [poor OpenMP](https://github.com/neurolabusc/simd) support. Therefore, MacOS users may want to install the gcc compiler `brew install gcc@9`.

For Windows, using the cmake method described above is highly recommended. However, you can also compile the project directly from the command line (here without the `-DHAVE_ZLIB` directive, so gz files will not be supported) :
Expand Down
6 changes: 6 additions & 0 deletions SuperBuild/SuperBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ if(FSLSTYLE)
ADD_DEFINITIONS(-DPIGZ)
endif()

ADD_DEFINITIONS(-DHAVE_64BITS)
ADD_DEFINITIONS(-DHAVE_BUTTERWORTH)
ADD_DEFINITIONS(-DHAVE_FORMATS)
ADD_DEFINITIONS(-DHAVE_TENSOR)
ADD_DEFINITIONS(-DHAVE_ZLIB)
ADD_DEFINITIONS(-DNII2MESH)


# Basic CMake build settings
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
Expand Down
175 changes: 0 additions & 175 deletions src/JavaScript.md

This file was deleted.

51 changes: 36 additions & 15 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Regular use, set compiler name, compiler flags, openmp flags zlib flags
CNAME=gcc
#linker flags
LFLAGS=-lm -lz
#c flags
CFLAGS=-O3 -DHAVE_ZLIB -DFSLSTYLE -DPIGZ -DREJECT_COMPLEX
#CFLAGS=-O3 -ffast-math -DHAVE_ZLIB -DFSLSTYLE -DPIGZ -DREJECT_COMPLEX -DNII2MESH
# Westmere required for fast gz https://github.com/tikv/tikv/issues/4843
#CFLAGS=-O3 -DHAVE_ZLIB -march=westmere
#universal flags always included
UFLAGS= niimath.c core.c core32.c niftilib/nifti2_io.c znzlib/znzlib.c -I./niftilib -I./znzlib -DFSLSTYLE -DPIGZ -DREJECT_COMPLEX -lm
UFLAGS64= -DHAVE_64BITS core64.c
#Zlib flags
ZFLAGS= -DHAVE_ZLIB -lz
#compiler flags
CFLAGS= -O3
# CFLAGS= -O0 -Wall
# GIfTI, obj, vtk, stl, etc support
GFILES= -DHAVE_FORMATS base64.c
#Butterworth bandpass flags
BFLAGS= -DHAVE_BUTTERWORTH bw.c
#Tensor decomposition
TFILES= -DHAVE_TENSOR tensor.c
#Marching Cubes flag
#MFLAGS= -DNII2MESH meshify.c quadric.c bwlabel.c MarchingCubes.c
MFLAGS= -DNII2MESH meshify.c quadric.c bwlabel.c radixsort.c fdr.c -DUSE_CLASSIC_CUBES oldcubes.c

ifeq ($(CXX),g++)
CFLAGS += -std=gnu99
Expand All @@ -15,19 +25,30 @@ endif
#run "OMP=1 make -j" for both OpenMP and CloudFlare
ifeq "$(OMP)" "1"
CNAME= gcc-9
CFLAGS+=-fopenmp -flto
#to get full file path:
#gcc-9 --print-file-name=libgomp.a
#LFLAGS+= /usr/local/Cellar/gcc/9.2.0_3/lib/gcc/9/gcc/x86_64-apple-darwin18/9.2.0/../../../libgomp.a
LFLAGS+= -static-libgcc -static-libstdc++ -L.
CFLAGS+=-fopenmp -flto -static-libgcc -static-libstdc++ -L.
endif

MFLAGS= -DNII2MESH MarchingCubes.c meshify.c quadric.c
#run "MESH=0 make" for minimal compile without nii2mesh functions
ifeq "$(MESH)" "0"
MFLAGS=
endif

all:
$(CNAME) $(CFLAGS) $(MFLAGS) niimath.c base64.c radixsort.c fdr.c bwlabel.c bw.c core.c tensor.c core32.c core64.c niftilib/nifti2_io.c znzlib/znzlib.c -I./niftilib -I./znzlib $(LFLAGS) -o niimath

$(CNAME) $(CFLAGS) $(BFLAGS) $(TFILES) $(GFILES) $(MFLAGS) $(UFLAGS) $(UFLAGS64) $(ZFLAGS) -o niimath

# tiny: terminal executable to emulate WASM
tiny:
$(CNAME) $(CFLAGS) $(MFLAGS) $(UFLAGS) -o niimath

# tiny without nii2mesh
nano:
$(CNAME) $(CFLAGS) $(UFLAGS) -o niimath

# sanitize checks memory errors - similar to tiny/wasm
sanitize:
g++ -O1 -g -Wno-deprecated -fsanitize=address -fno-omit-frame-pointer $(MFLAGS) $(UFLAGS) -o niimath

wasm:
em++ -O3 -Wno-deprecated $(MFLAGS) $(UFLAGS) -s DEMANGLE_SUPPORT=1 -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap", "FS_createDataFile", "FS_readFile", "FS_unlink"]' -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 -s EXPORTED_FUNCTIONS='["_simplify"]' -o niimath.js
# hint: consider further optimizations:
# wasm-opt -O3 -o output.wasm niimath.wasm; rm niimath.wasm; mv output.wasm niimath.wasm
Loading

0 comments on commit 68e5d54

Please sign in to comment.