Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Nix configuration files #313

Merged
merged 25 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
23 changes: 14 additions & 9 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ jobs:
build:
name: Build webR
runs-on: ubuntu-latest
container: ghcr.io/r-wasm/webr-flang:main
container: ghcr.io/r-wasm/flang-wasm:main
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 'lts/*'
registry-url: 'https://registry.npmjs.org'
- uses: actions/checkout@v3
- name: Set npm dist-tag to "next" if pre-release version
if: contains(github.ref_name, '-dev') || contains(github.ref_name, '-rc')
run: echo "DIST_TAG=next" >> $GITHUB_ENV
- name: Install required system packages
run: apt-get update && apt-get install -y gh jq sudo
- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true
- uses: quarto-dev/quarto-actions/setup@v2
- name: Install required R packages
- name: Install required R packages for building documentation
run: Rscript -e 'install.packages(c("rmarkdown", "rvest"))'
- name: Configure webR for flang
env:
EMFC: /opt/flang/host/bin/flang-new
run: ./configure
- name: Copy flang into webR tree
run: |
cp -r /opt/flang/wasm .
cp -r /opt/flang/host .
cp /opt/flang/emfc ./host/bin/emfc
- name: Setup Emscripten PATH
run: echo "/opt/emsdk:/opt/emsdk/upstream/emscripten" >> $GITHUB_PATH
- name: Set Emscripten EM_NODE_JS
Expand All @@ -44,7 +44,7 @@ jobs:
- name: Build all optional wasm libs
env:
EMSDK: /opt/emsdk
run: cd libs && make all
run: cd libs && make all -j
- name: Build webR
env:
EMSDK: /opt/emsdk
Expand All @@ -53,6 +53,11 @@ jobs:
- name: Build webR documentation
run: cd src/docs && make
shell: bash
- name: Publish to npm
if: "!contains(github.ref_name, 'main')"
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: cd src && make publish
- name: Report code coverage
uses: codecov/codecov-action@v3
with:
Expand Down
45 changes: 0 additions & 45 deletions .github/workflows/npm.yml

This file was deleted.

23 changes: 15 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ on:
workflow_dispatch:

jobs:
build:
build-docker:
runs-on: ubuntu-latest
container: ghcr.io/r-wasm/webr-flang:main
container: ghcr.io/r-wasm/flang-wasm:main
steps:
- uses: actions/setup-node@v3
with:
node-version: 'lts/*'
registry-url: 'https://registry.npmjs.org'
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Configure webR for flang
env:
EMFC: /opt/flang/host/bin/flang-new
run: ./configure
- name: Copy flang into webR tree
run: |
cp -r /opt/flang/wasm .
cp -r /opt/flang/host .
cp /opt/flang/emfc ./host/bin/emfc
- name: Setup Emscripten PATH
run: echo "/opt/emsdk:/opt/emsdk/upstream/emscripten" >> $GITHUB_PATH
- name: Set Emscripten EM_NODE_JS
Expand All @@ -41,3 +38,13 @@ jobs:
fail_ci_if_error: true
verbose: true
token: ${{ secrets.CC_TOKEN }}
build-nix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
with:
extra-conf: "sandbox = false"
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Build Nix package
run: nix build --print-build-logs
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/tools/flang/*llvm-project
/R/download
/R/build
/result
/src/examples/serve.R
/src/coverage
/src/webR/config.[jt]s
Expand All @@ -32,3 +33,4 @@ node_modules

/.luarc.json
/.clangd
/.emscripten_cache
georgestagg marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tools/flang"]
path = tools/flang
url = https://github.com/r-wasm/flang-wasm.git
26 changes: 9 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ WASM = $(WEBR_ROOT)/wasm
HOST = $(WEBR_ROOT)/host
TOOLS = $(WEBR_ROOT)/tools

# This is symlinked at configure-time
include $(TOOLS)/fortran.mk
default: webr

EMFC_FILES = $(EMFC) $(FORTRAN_WASM_LIB)
# This is created at configure-time
include $(TOOLS)/fortran.mk

# Build webR and install the web app in `./dist`
.PHONY: webr
webr: $(EMFC_FILES) libs
webr: $(EMFC) $(FORTRAN_WASM_LIB) libs
cd R && $(MAKE) && $(MAKE) install
cd src && $(MAKE)

$(EMFC_FILES):
cd $(EMFC_DIR) && $(MAKE) && $(MAKE) install

# Supporting libs for webr
.PHONY: libs
libs:
Expand Down Expand Up @@ -49,20 +46,15 @@ check-pr:
cd src && $(MAKE) lint && $(MAKE) check && $(MAKE) check-packages

.PHONY: clean
clean: clean-wasm
cd tools/dragonegg && $(MAKE) clean
cd tools/flang && $(MAKE) clean
clean:
rm -rf $(HOST) $(WASM)/R-*
cd R && $(MAKE) clean

.PHONY: clean-wasm
clean-wasm: clean-webr
clean-wasm: clean
rm -rf $(WASM)
cd libs && $(MAKE) clean

.PHONY: clean-webr
clean-webr:
rm -rf $(HOST) $(WASM)/R-*
cd R && $(MAKE) clean

.PHONY: distclean
distclean: clean
distclean: clean-wasm clean-tools
rm -rf dist
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# webR (development version)

## New features

* Support for building webR and LLVM Flang as Nix packages, using Nix flakes.

* WebR's `./configure` script now detects the presence of a `EMFC` environment variable in the user's environment. If this variable points to an existing `flang-new` binary, and a Fortran runtime library for WebAssembly can also be found, webR will skip building LLVM flang and instead use the version provided by the environment.

## Breaking changes

* Upgraded the base LLVM distribution from LLVM 14 to LLVM 17, rebasing our Fortran for WebAssembly patches on the latest release of LLVM at time of writing (v17.0.6). The LLVM Fortran compiler binary name is now `flang-new` and webR's build scripts have been updated to reflect this. The `emfc` wrapper script is no longer required, but for the moment the Make variable pointing to the `flang-new` compiler is still named `EMFC` for backwards compatibility.

* The LLVM flang build scripts are now sourced using a git submodule, to simplify management of CI builds. The build scripts are available at https://github.com/r-wasm/flang-wasm and the patched LLVM source at https://github.com/r-wasm/llvm-project. This allows for an independent build of the patched LLVM flang for WebAssembly, including as a separate Nix package.

# webR 0.2.2

## New features
Expand Down
4 changes: 2 additions & 2 deletions R/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ EXPORTED_RUNTIME_METHODS=[$\
'allocateUTF8OnStack',$\
'callMain',$\
'getValue',$\
'getWasmTableEntry',$\
'setValue'$\
]

Expand All @@ -139,7 +140,6 @@ MAIN_LDFLAGS += -s ALLOW_MEMORY_GROWTH=1
MAIN_LDFLAGS += -s STACK_SIZE=1MB
MAIN_LDFLAGS += -s EXIT_RUNTIME=1
MAIN_LDFLAGS += -s ERROR_ON_UNDEFINED_SYMBOLS=0
MAIN_LDFLAGS += -s EXPORTED_FUNCTIONS=['getWasmTableEntry']
MAIN_LDFLAGS += -s EXPORTED_RUNTIME_METHODS=$(EXPORTED_RUNTIME_METHODS)
MAIN_LDFLAGS += -s FETCH=1
MAIN_LDFLAGS += -lworkerfs.js -lnodefs.js
Expand All @@ -160,7 +160,7 @@ $(BUILD)/state/R-$(R_VERSION)/r-stage2-configured: $(BUILD)/state/R-$(R_VERSION)
CFLAGS="$(STAGE2_CFLAGS)" \
LDFLAGS="$(STAGE2_LDFLAGS)" \
LIBnn="lib" \
FFLAGS="" \
FFLAGS="-fPIC" \
FPICFLAGS="-fPIC" \
FC="$(EMFC)" \
emconfigure ../configure \
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ WebR relies on additional libraries compiled for Wasm for both Cairo graphics su
If you'd prefer to build all of the available system libraries for Wasm, `cd` into the `libs` directory and run `make all` to build the additional libraries, then finally `cd ..` and run `make clean-webr && make` to rebuild webR. R will automatically detect the additional Wasm libraries and integrate Cairo graphics support as part of the build.

### Building with Docker

Included in the source repository is a `Dockerfile` which can be used to setup everything that's required for the webR build environment, including LLVM flang and all supported WebAssembly system libraries.

Pre-built docker images can be found in the [GitHub Packages section](https://github.com/r-wasm/webr/pkgs/container/webr). To build the docker image and webR from source, `cd` into the webR source directory then run `docker build .`

The resulting docker image contains a version of R configured to be built for WebAssembly, and so the image can also be used to build custom R packages for webR.

### Building with Nix

If you are using Nix, you can start a development environment by running `nix develop`. Then you can build as usual, with `./configure && make`.

Note that this requires that your Nix installation has support for flakes enabled. The easiest way to do this is to install using the [Nix installer from Determinate Systems](https://zero-to-nix.com/start/install).

### Node and Emscripten versioning

Expand Down
43 changes: 39 additions & 4 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ while [[ $# -gt 0 ]]; do
USE_DRAGONEGG="true"
shift
;;
--with-flang)
shift
;;
*)
echo "Unknown argument $1"
exit 1
Expand All @@ -16,9 +19,41 @@ done

rm -f tools/fortran.mk

if [ -z ${USE_DRAGONEGG-} ]
then
ln -s fortran-flang.mk tools/fortran.mk
else
function configure_emfc {
EMFC_ROOT=`dirname ${EMFC}`
if [ -f "${FORTRAN_WASM_LIB-}" ]; then
EMFC_FLIB=${FORTRAN_WASM_LIB}
elif [ -f "${EMFC_ROOT}/../../wasm/lib/libFortranRuntime.a" ]; then
EMFC_FLIB=`realpath ${EMFC_ROOT}/../../wasm/lib/libFortranRuntime.a`
else
echo "Error: Can't find Fortran runtime library."
echo "Set FORTRAN_WASM_LIB to the location of libFortranRuntime.a,"\
"or unset EMFC to build LLVM flang from source."
exit 1
fi
echo "Found Fortran runtime library: ${EMFC_FLIB}"
cat << EOF > tools/fortran.mk
EMFC = ${EMFC}
FORTRAN_WASM_LIB = ${EMFC_FLIB}
FORTRAN_WASM_LDADD = ${EMFC_FLIB}
.PHONY: clean-tools
clean-tools:
@
EOF
}

if [ -n "${USE_DRAGONEGG-}" ]; then
# Build dragonegg from source
ln -s fortran-dragonegg.mk tools/fortran.mk
elif [ -n "${EMFC-}" ]; then
if [ -f "${EMFC-}" ]; then
echo "Found Fortran compiler: ${EMFC}"
configure_emfc
else
echo "Error: Can't find Fortran compiler, check EMFC value."
exit 1
fi
else
# Build LLVM flang from source
ln -s fortran-flang.mk tools/fortran.mk
fi
63 changes: 63 additions & 0 deletions flake.lock

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

Loading