Skip to content

Commit

Permalink
deploy demo
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 25, 2023
1 parent 81eee58 commit ee28e21
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 31 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/demo.yml
@@ -0,0 +1,60 @@
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages

on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: true

jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
-
name: Build PHP WASM
uses: docker/build-push-action@v5
with:
context: .
push: false
tags: soyuka/php-wasm:latest
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Build website
working-directory: demo
run: |
docker create --name=php-wasm soyuka/php-wasm:latest
mkdir -p public/ dist/
docker cp php-wasm:/build/php-web.mjs ./dist
docker cp php-wasm:/build/php-web.wasm ./public
docker run -v $(pwd)/src:/src -v $(pwd)/public:/public -v $(pwd)/dist:/dist php-wasm python3 /emsdk/upstream/emscripten/tools/file_packager.py /public/php-web.data --use-preload-cache --lz4 --preload "/src" --js-output=/dist/php-web.data.js --no-node --exclude '*/.*' --export-name=createPhpModule
sed '/--pre-js/r dist/php-web.data.js' dist/php-web.mjs > public/php-web.mjs
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
path: 'demo/public'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
16 changes: 8 additions & 8 deletions Dockerfile
Expand Up @@ -40,7 +40,7 @@ WORKDIR /src/sqlite
RUN emcc -Oz -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DISABLE_LFS -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_NORMALIZE -c sqlite3.c -o sqlite3.o

FROM build_tool as php_src
ARG PHP_BRANCH=PHP-8.2.9
ARG PHP_BRANCH=PHP-8.2.10
RUN git clone https://github.com/php/php-src.git php-src \
--branch $PHP_BRANCH \
--single-branch \
Expand All @@ -49,7 +49,7 @@ RUN git clone https://github.com/php/php-src.git php-src \
FROM php_src AS php-wasm
ARG WASM_ENVIRONMENT=web
ARG ASSERTIONS=0
ARG OPTIMIZE=-O2
ARG OPTIMIZE=-O1
# TODO: find a way to keep this, it can't be empty if defined...
# ARG PRE_JS=
ARG INITIAL_MEMORY=256mb
Expand All @@ -60,8 +60,7 @@ ENV LIBXML_LIBS "-L/src/usr/lib"
ENV LIBXML_CFLAGS "-I/src/usr/include/libxml2"
ENV SQLITE_CFLAGS "-I/src/usr/include/sqlite3"
ENV SQLITE_LIBS "-L/src/usr/lib"
WORKDIR /src/php-src
RUN ./buildconf --force \
RUN cd /src/php-src && ./buildconf --force \
&& emconfigure ./configure \
--enable-embed=static \
--with-layout=GNU \
Expand Down Expand Up @@ -90,11 +89,11 @@ RUN ./buildconf --force \
--enable-pdo \
--with-pdo-sqlite \
--with-sqlite3
RUN emmake make -j8
RUN cd /src/php-src && emmake make -j8
# PHP7 outputs a libphp7 whereas php8 a libphp
RUN bash -c '[[ -f .libs/libphp7.la ]] && mv .libs/libphp7.la .libs/libphp.la && mv .libs/libphp7.a .libs/libphp.a && mv .libs/libphp7.lai .libs/libphp.lai || exit 0'
RUN cd /src/php-src && bash -c '[[ -f .libs/libphp7.la ]] && mv .libs/libphp7.la .libs/libphp.la && mv .libs/libphp7.a .libs/libphp.a && mv .libs/libphp7.lai .libs/libphp.lai || exit 0'
COPY ./source /src/source
RUN emcc $OPTIMIZE \
RUN cd /src/php-src && emcc $OPTIMIZE \
-I . \
-I Zend \
-I main \
Expand All @@ -103,7 +102,7 @@ RUN emcc $OPTIMIZE \
/src/source/phpw.c \
-o /src/phpw.o \
-s ERROR_ON_UNDEFINED_SYMBOLS=0
RUN mkdir /build && emcc $OPTIMIZE \
RUN mkdir /build && cd /src/php-src && emcc $OPTIMIZE \
-o /build/php-$WASM_ENVIRONMENT.mjs \
--llvm-lto 2 \
-s EXPORTED_FUNCTIONS='["_phpw", "_phpw_flush", "_phpw_exec", "_phpw_run", "_chdir", "_setenv", "_php_embed_init", "_php_embed_shutdown", "_zend_eval_string"]' \
Expand All @@ -123,3 +122,4 @@ RUN mkdir /build && emcc $OPTIMIZE \
# -s DECLARE_ASM_MODULE_EXPORTS=0 \
-lidbfs.js \
/src/phpw.o /src/usr/lib/sqlite3.o .libs/libphp.a /src/usr/lib/libxml2.a
RUN rm -r /src/*
5 changes: 3 additions & 2 deletions demo/.gitignore
@@ -1,3 +1,4 @@
dist/
public/
!public/index.html
public/php-web.mjs
public/php-web.data
public/php-web.wasm
55 changes: 37 additions & 18 deletions demo/public/index.html
@@ -1,24 +1,43 @@
<iframe id="output"></iframe>
<iframe id="output" width="100%" height="50%" frameBorder="0"></iframe>
<script type="module">
import Module from "./php-web.mjs";
const STR = "string";
const output = document.getElementById('output');
const buffer = ''
import phpBinary from "./php-web.mjs";

Module({
print(data) {
buffer += data;
// output.setAttribute('srcdoc', data);
},
printErr(data) {
if (data) {
console.log("stderr: ", data);
async function main() {
const output = document.getElementById('output')
const buffer = [];
const {ccall, FS} = await phpBinary({
print(data) {
buffer.push(data);
}
},
}).then(({ ccall, FS, IDBFS }) => {
console.log(ccall("phpw_exec", STR, [STR], ["phpversion();"]));
})

// Note that `/src` is the path we used when preloading!
// ccall("phpw", null, [STR], ["/src/index.php"]);
console.log(ccall("phpw_exec", "string", ["string"], ["phpversion();"]));

window.FS = FS;
window.run = () => {
// Note that `/src` is the path we used when preloading!
ccall("phpw", null, ["string"], ["/src/index.php"]);
output.contentWindow.document.body.innerHTML = buffer.join('');
buffer.length = 0;
}

run();
}

main();
</script>
<div id="monaco" style="min-height: 100px"></div>
<script type="module">
import * as monaco from 'https://cdn.jsdelivr.net/npm/monaco-editor@0.39.0/+esm';
const editor = monaco.editor.create(document.getElementById('monaco'));
// FS is documented at https://emscripten.org/docs/api_reference/Filesystem-API.html
// everything is synchronous
const model = editor.getModel();
model.setLanguage('php');
model.setValue(window.FS.readFile('/src/index.php', {encoding: 'utf8'}).toString());
model.onDidChangeContent((v) => {
window.FS.unlink('/src/index.php')
window.FS.writeFile('/src/index.php', model.getValue());
window.run();
});
</script>
1 change: 0 additions & 1 deletion demo/src/index.php
@@ -1,3 +1,2 @@
<?php

phpinfo();
6 changes: 4 additions & 2 deletions source/phpw.c
Expand Up @@ -13,13 +13,13 @@ int main() {
void phpw_flush()
{
fprintf(stdout, "\n");
fflush(stdout);
fprintf(stderr, "\n");
fflush(stderr);
}

char *EMSCRIPTEN_KEEPALIVE phpw_exec(char *code)
{
// This sets USE_ZEND_ALLOC=0 to avoid nunmap errors
setenv("USE_ZEND_ALLOC", "0", 1);
php_embed_init(0, NULL);
char *retVal = NULL;

Expand All @@ -43,6 +43,7 @@ char *EMSCRIPTEN_KEEPALIVE phpw_exec(char *code)

void EMSCRIPTEN_KEEPALIVE phpw_run(char *code)
{
setenv("USE_ZEND_ALLOC", "0", 1);
php_embed_init(0, NULL);
zend_try
{
Expand All @@ -63,6 +64,7 @@ int EMBED_SHUTDOWN = 1;

void phpw(char *file)
{
setenv("USE_ZEND_ALLOC", "0", 1);
if (EMBED_SHUTDOWN == 0) {
php_embed_shutdown();
}
Expand Down

0 comments on commit ee28e21

Please sign in to comment.