2

This test if in preparation for writing a more fully fledged build module. But I need to sus out the basics first. The desire to: - build all the "object files" output to an output folder - link them into a distributable "wasm" binary and format an html page and output them to a "dist" folder.

I looked at the manual in:

https://kripken.github.io/emscripten-site/docs/tools_reference/emcc.html

It's not as intuitive or explicitly spelled out as one would want. Below is my current simple test build.

#!/bin/bash

# set emscripten toolchain paths in this shell
source "${HOME}/emsdk/emsdk_env.sh" --build=Release   

BuildDir="./build"          # root of output dir for built files
SrcDir="./src"              # a source code directory
ObjDir="${BuildDir}/obj"    # where intermediate "object" files are output to.
IncludeDir="./include"      # an include directory

# start clean for this test
rm -fr "${BuildDir}"
mkdir -p "${ObjDir}"  # also re-creates BuildDir


# compile source code out to ObjDir

emcc --default-obj-ext .bc -o "${ObjDir}/" -I "${IncludeDir}" \
    "${SrcDir}/hello.cpp" "${SrcDir}/TestClass.cpp"`

Running the above gives me errors but works if there is only one source file.

Is a directory
Traceback (most recent call last):
      File "/home/peterk/emsdk/emscripten/incoming/emcc.py", line 3107, in <module>
...
status 1
    peterk@5a4a702ca3b5:~/didi-wasmtest/test$

The below works but puts all the output files in the src/ folder and also assigns them the .o suffix and not the .bc suffix: It also outputs a .out.js and a .out.wasm file in the directory the script is run from. I would like to suppress that until a final "link" phase where the results of several compiles would be linked together in a separate step.

emcc --default-obj-ext .bc -I "${IncludeDir}" \
    "${SrcDir}/hello.cpp" "${SrcDir}/TestClass.cpp"`

1 Answer 1

1

Ok - this works which is good as a makefile will compile one at a time anyway.

#!/bin/bash

source "${HOME}/emsdk/emsdk_env.sh" --build=Release   # set emscripten toolchain paths in this shell

BuildDir="./build"          # root of output dir for built files
SrcDir="./src"              # a source code directory
ObjDir="${BuildDir}/obj"    # where intermediate "object" files are output to.
IncludeDir="./include"      # an include directory
DistDir="./build/dist"      # distribution "binary" output dir

# start clean for this test
rm -fr "${BuildDir}"

# compile source code out to ObjDir
mkdir -p "${ObjDir}"

objFiles=""

for srcFile in "${SrcDir}"/*.cpp; do
    objFile="${ObjDir}/$(basename "$srcFile").bc"
    emcc -o "${objFile}" -I "${IncludeDir}" "${srcFile}"
    objFiles+="${objFile} " # save list of objFiles for link.
done

# link object files into binary runtime in DistDir
mkdir -p "${DistDir}"

emcc -s WASM=1 ${objFiles} -o "${DistDir}/hello.html"

# expose to browser to test
cd "${DistDir}"
python -c 'import SimpleHTTPServer; SimpleHTTPServer.test()'
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe you can help me Mr.Peter at : stackoverflow.com/questions/58198388/…
Thanks - at this time I have it fully integrated into our Ruby based build system which I use with CLion IDE - thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.