Skip to content

Instantly share code, notes, and snippets.

@wenkokke
Created June 8, 2023 19:33
Show Gist options
  • Save wenkokke/7ae3dd0d8389f5412a92a9c1fa411b36 to your computer and use it in GitHub Desktop.
Save wenkokke/7ae3dd0d8389f5412a92a9c1fa411b36 to your computer and use it in GitHub Desktop.
Build Agda to JavaScript using the GHC 9.6 JavaScript backend
#!/bin/bash
set -eo pipefail
# 1. Build GHC with JavaScript backend
# https://gitlab.haskell.org/ghc/ghc/-/wikis/javascript-backend/building
# 2. Get Agda source
git clone --depth=1 git@github.com:agda/agda.git agda
cd agda
# 3. Ensure emscripten has the port for zlib
mkdir _zlib
wget https://raw.githubusercontent.com/madler/zlib/master/test/example.c -O _zlib/example.c
emcc -s USE_ZLIB=1 _zlib/example.c -o _zlib/example.js
node _zlib/example.js
rm -r _zlib
# 4. Patch Agda source
mkdir _patches
cat <<'EOF' > '_patches/0001-Set-build-type-Simple.patch'
From d21e6ca5c1ef08901468b3d820cc8c25b875bb3e Mon Sep 17 00:00:00 2001
From: Wen Kokke <wenkokke@users.noreply.github.com>
Date: Thu, 8 Jun 2023 18:12:59 +0100
Subject: [PATCH 1/2] Set build-type: Simple
---
Agda.cabal | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Agda.cabal b/Agda.cabal
index 93e9180b7..df306467b 100644
--- a/Agda.cabal
+++ b/Agda.cabal
@@ -1,7 +1,7 @@
cabal-version: 2.4
name: Agda
version: 2.6.4
-build-type: Custom
+build-type: Simple
license: LicenseRef-OtherLicense
license-file: LICENSE
copyright: (c) 2005-2023 The Agda Team.
--
2.40.1
EOF
git apply _patches/0001-Set-build-type-Simple.patch
cat <<'EOF' > '_patches/0002-Remove-use-of-Template-Haskell.patch'
From f4f19b764cdac3ae8ff3a3e61b88c2c6032aa3a2 Mon Sep 17 00:00:00 2001
From: Wen Kokke <wenkokke@users.noreply.github.com>
Date: Thu, 8 Jun 2023 18:19:45 +0100
Subject: [PATCH 2/2] Remove use of Template Haskell
---
Agda.cabal | 1 -
src/full/Agda/VersionCommit.hs | 16 +---------------
2 files changed, 1 insertion(+), 16 deletions(-)
diff --git a/Agda.cabal b/Agda.cabal
index df306467b..e9de1c59b 100644
--- a/Agda.cabal
+++ b/Agda.cabal
@@ -389,7 +389,6 @@ library
, exceptions >= 0.8 && < 0.11
, filepath >= 1.4.2.1 && < 1.5
, ghc-compact == 0.1.*
- , gitrev >= 1.3.1 && < 2.0
-- hashable 1.2.0.10 makes library-test 10x
-- slower. The issue was fixed in hashable 1.2.1.0.
-- https://github.com/tibbe/hashable/issues/57.
diff --git a/src/full/Agda/VersionCommit.hs b/src/full/Agda/VersionCommit.hs
index 2753f8ed2..652cb3e83 100644
--- a/src/full/Agda/VersionCommit.hs
+++ b/src/full/Agda/VersionCommit.hs
@@ -1,5 +1,4 @@
{-# LANGUAGE CPP #-}
-{-# LANGUAGE TemplateHaskell #-}
#if __GLASGOW_HASKELL__ >= 900
{-# OPTIONS_GHC -Wno-overlapping-patterns #-}
@@ -7,8 +6,6 @@
module Agda.VersionCommit where
-import Development.GitRev
-
import Agda.Version
versionWithCommitInfo :: String
@@ -16,15 +13,4 @@ versionWithCommitInfo = version ++ maybe "" ("-" ++) commitInfo
-- | Information about current git commit, generated at compile time
commitInfo :: Maybe String
-commitInfo
- | hash == "UNKNOWN" = Nothing
- | otherwise = Just $ abbrev hash ++ dirty
- where
- hash = $(gitHash)
-
- -- Check if any tracked files have uncommitted changes
- dirty | $(gitDirtyTracked) = "-dirty"
- | otherwise = ""
-
- -- Abbreviate a commit hash while keeping it unambiguous
- abbrev = take 7
+commitInfo = Nothing
--
2.40.1
EOF
git apply _patches/0002-Remove-use-of-Template-Haskell.patch
# 5. Build Agda
mkdir _build
cabal v2-install \
--ghc-option=-O2 \
-f+optimise-heavily \
--with-ghc=$(which javascript-unknown-ghcjs-ghc) \
--with-ghc-pkg=$(which javascript-unknown-ghcjs-ghc-pkg) \
--with-hsc2hs=$(which javascript-unknown-ghcjs-hsc2hs) \
--with-gcc=$(which emcc) \
--with-ld=$(which emcc) \
--with-strip=$(which emstrip) \
--installdir=_build \
--install-method=copy \
--overwrite-policy=always
# 6. Minify with esbuild
npx esbuild --bundle --minify --platform=node --target=node18 --tree-shaking=true _build/agda.js --outfile=_build/agda.min.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment