From b4d11df8114f5f3bb73a7854ffd5c5a8372bcc5b Mon Sep 17 00:00:00 2001
From: Konrad Botor <kbotor@syncad.com>
Date: Mon, 24 Jul 2023 13:40:54 +0200
Subject: [PATCH] Added Emscripten image build to CI - ref. !12

---
 .gitattributes                              |  3 +-
 .gitlab-ci.yml                              | 18 ++++++++--
 Dockerfile.emscripten                       | 37 ++++++++++++++-------
 docker-bake.hcl                             | 21 ++++++++++++
 scripts/emscripten/prepare_boost.sh         |  2 +-
 scripts/emscripten/prepare_openssl.sh       |  1 +
 scripts/emscripten/prepare_secp256k1-zkp.sh | 12 ++++++-
 7 files changed, 76 insertions(+), 18 deletions(-)

diff --git a/.gitattributes b/.gitattributes
index 857c41a..d6efeee 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,2 +1,3 @@
 *.sh text eol=lf
-Dockerfile.* text eol=lf
\ No newline at end of file
+Dockerfile.* text eol=lf
+*.patch text eol-lf
\ No newline at end of file
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c5ac946..f1a3ddd 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -75,7 +75,7 @@ lint_python_scripts:
 
 .build_docker_image:
   extends: .docker_image_builder_job_template
-  image: docker:20.10.21
+  image: docker:24.0.1-cli
   variables:
     BUILD_TARGET: ""
   before_script:
@@ -96,7 +96,7 @@ lint_python_scripts:
       fi
       echo -e "\e[0Ksection_end:$(date +%s):tag\r\e[0K"
       echo -e "\e[0Ksection_start:$(date +%s):build[collapsed=true]\r\e[0KBaking image "$CI_REGISTRY_IMAGE/$BUILD_TARGET:${tag}"..."
-      docker buildx bake --progress=plain --push "$BUILD_TARGET"
+      docker buildx bake --progress=plain --provenance=false --push "$BUILD_TARGET"
       echo -e "\e[0Ksection_end:$(date +%s):build\r\e[0K"
   tags:
     - public-runner-docker
@@ -111,7 +111,7 @@ build_docker_dind_image:
       exists:
         - Dockerfile.docker-dind
   services:
-    - docker:20.10.21-dind
+    - docker:24.0.1-dind
 
 build_docker_builder_image:
   extends: .build_docker_image
@@ -161,6 +161,18 @@ build_tox_test_runner_image:
       exists:
         - Dockerfile.tox-test-runner
 
+build_emscripten_image:
+  extends: .build_docker_image
+  stage: build
+  variables:
+    BUILD_TARGET: "emscripten"
+  needs:
+    - build_docker_dind_image
+  rules:
+    - if: $CI_COMMIT_BRANCH
+      exists:
+        - Dockerfile.emscripten
+
 example_docker_image_builder_job:
   extends: .docker_image_builder_job_template
   stage: example-build
diff --git a/Dockerfile.emscripten b/Dockerfile.emscripten
index 391b338..c98c1f3 100644
--- a/Dockerfile.emscripten
+++ b/Dockerfile.emscripten
@@ -1,3 +1,4 @@
+# syntax=docker/dockerfile:1.5
 ARG EMSCRIPTEN_VERSION=3.1.43
 
 FROM emscripten/emsdk:${EMSCRIPTEN_VERSION} AS pure_emscripten_sdk
@@ -23,18 +24,30 @@ ENV OPENSSL_VERSION_TAG=${OPENSSL_VERSION_TAG}
 USER emscripten
 WORKDIR /home/emscripten
 
-RUN mkdir -vp tmp_src && cd tmp_src && \
-    git clone https://github.com/boostorg/boost.git && \
-    cd boost && \
-    git checkout tags/${BOOST_VERSION_TAG} && \
-    git submodule update --init --recursive && \
-    cd .. && \
-    git clone https://github.com/openssl/openssl.git && \
-    cd openssl && \
-    git checkout tags/${OPENSSL_VERSION_TAG} && \
-    git submodule update --init --recursive && \
-    cd .. && \
-    git clone https://github.com/ElementsProject/secp256k1-zkp.git
+RUN <<-EOF
+  set -e
+
+  git config --global advice.detachedHead false
+  
+  mkdir -vp tmp_src
+  cd tmp_src
+
+  git clone https://github.com/boostorg/boost.git
+
+  cd boost
+  git checkout tags/${BOOST_VERSION_TAG}
+  git submodule update --init --recursive
+
+  cd ..
+  git clone https://github.com/openssl/openssl.git
+
+  cd openssl
+  git checkout tags/${OPENSSL_VERSION_TAG}
+  git submodule update --init --recursive
+  
+  cd ..
+  git clone https://github.com/ElementsProject/secp256k1-zkp.git
+EOF
 
 FROM lib_source as emscripten_lib_builder
 
diff --git a/docker-bake.hcl b/docker-bake.hcl
index 7ddf8e1..6545175 100644
--- a/docker-bake.hcl
+++ b/docker-bake.hcl
@@ -1,5 +1,14 @@
 variable "CI_REGISTRY_IMAGE" {}
 variable "CI_COMMIT_SHA" {}
+variable "EMSCRIPTEN_VERSION" {
+  default = null
+}
+variable "BOOST_VERSION_TAG" {
+  default = null
+}
+variable "OPENSSL_VERSION_TAG" {
+  default = null
+}
 variable "tag" {
   default = "latest"
 }
@@ -64,4 +73,16 @@ target "tox-test-runner" {
   tags = generate-tags("tox-test-runner")
   cache-from = generate-cache-from("tox-test-runner")
   cache-to = generate-cache-to("tox-test-runner")
+}
+
+target "emscripten" {
+  dockerfile = "Dockerfile.emscripten"
+  tags = generate-tags("emscripten")
+  cache-from = generate-cache-from("emscripten")
+  cache-to = generate-cache-to("emscripten")
+  args = {
+    EMSCRIPTEN_VERSION = "${EMSCRIPTEN_VERSION}",
+    BOOST_VERSION_TAG = "${BOOST_VERSION_TAG}",
+    OPENSSL_VERSION_TAG = "${OPENSSL_VERSION_TAG}"
+  }
 }
\ No newline at end of file
diff --git a/scripts/emscripten/prepare_boost.sh b/scripts/emscripten/prepare_boost.sh
index 06ad30a..eef4be3 100755
--- a/scripts/emscripten/prepare_boost.sh
+++ b/scripts/emscripten/prepare_boost.sh
@@ -29,7 +29,7 @@ printf "using clang : emscripten : emcc -s USE_ZLIB=1 -s USE_ICU=0 : <archiver>e
 ./b2 \
   --build-dir="${TMP_SRC}/boost_build/" \
   --prefix="${INSTALL_PREFIX}" \
-  -j $(nproc) \
+  -j "$(nproc)" \
   -q \
   runtime-link=static \
   link=static \
diff --git a/scripts/emscripten/prepare_openssl.sh b/scripts/emscripten/prepare_openssl.sh
index 8609dc8..499c710 100755
--- a/scripts/emscripten/prepare_openssl.sh
+++ b/scripts/emscripten/prepare_openssl.sh
@@ -23,6 +23,7 @@ emconfigure ./Configure \
   linux-x32 \
   -static
 
+# shellcheck disable=SC2016
 sed -i 's/$(CROSS_COMPILE)//' Makefile
 emmake make -j 8 
 emmake make install
diff --git a/scripts/emscripten/prepare_secp256k1-zkp.sh b/scripts/emscripten/prepare_secp256k1-zkp.sh
index 1391f1c..04c41e0 100755
--- a/scripts/emscripten/prepare_secp256k1-zkp.sh
+++ b/scripts/emscripten/prepare_secp256k1-zkp.sh
@@ -13,6 +13,16 @@ git checkout d22774e248c703a191049b78f8d04f37d6fcfa05
 
 export VERBOSE=1
 emconfigure ./autogen.sh
-emconfigure ./configure --prefix=${INSTALL_PREFIX} --with-asm=no --enable-shared=no --enable-tests=no --enable-benchmark=no --enable-exhaustive-tests=no --with-pic=no --with-valgrind=no --enable-module-recovery=yes --enable-module-rangeproof=yes --enable-experimental
+emconfigure ./configure --prefix="${INSTALL_PREFIX}" \
+  --with-asm=no \
+  --enable-shared=no \
+  --enable-tests=no \
+  --enable-benchmark=no \
+  --enable-exhaustive-tests=no \
+  --with-pic=no \
+  --with-valgrind=no \
+  --enable-module-recovery=yes \
+  --enable-module-rangeproof=yes \
+  --enable-experimental
 emmake make
 emmake make install
-- 
GitLab