haskell / ghcup-hs

https://www.haskell.org/ghcup/
GNU Lesser General Public License v3.0
290 stars 90 forks source link

hsc2hs is broken when using JS backend from cross channel #1157

Open aidatorajiro opened 1 week ago

aidatorajiro commented 1 week ago

What went wrong

Cabal fails clock-0.8.4 build.

Failed to build clock-0.8.4.                                                                                                                                 
Build log (                                                                                                                                                  
/home/*****/.cabal/logs/ghc-9.10.0.20240413/clock-0.8.4-af09122070b54a220a89abd377b3272fa8d08c9df858d129e2c223a1460da6bd.log                               
):                                                                                                                                                           
Configuring library for clock-0.8.4...                                                                                                                       
Preprocessing library for clock-0.8.4...                                                                                                                     
Error: [Cabal-1008]                                                                                                                                          
The program 'hsc2hs' is required but the version of /home/*****/.ghcup/ghc/javascript-unknown-ghcjs-9.10.0.20240413/bin/hsc2hs-ghc-9.10.0.20240413 could no
t be determined.                                                                                                                                             

Error: [Cabal-7125]                                                                                                                                          
Failed to build clock-0.8.4 (which is required by exe:***** from *****). See the build log above for details.    

Way to reproduce

  1. Install ghcup and follow User Guide to install GHC JS cross 9.10.0.20240413
  2. Create a cabal project that requires clock.
  3. run cabal build --with-ghc=javascript-unknown-ghcjs-ghc-9.10.0.20240413 --with-ghc-pkg=javascript-unknown-ghcjs-ghc-pkg-9.10.0.20240413

Software versions

cause of the problem

The shell script file ~/.ghcup/ghc/javascript-unknown-ghcjs-9.10.0.20240413/bin/hsc2hs-ghc-9.10.0.20240413 has the wrong executable path.

quick solution for the problem

Adding javascript-unknown-ghcjs prefix to $executablename in ~/.ghcup/ghc/javascript-unknown-ghcjs-9.10.0.20240413/bin/hsc2hs-ghc-9.10.0.20240413 fixes the build issue.

Replace ~/.ghcup/ghc/javascript-unknown-ghcjs-9.10.0.20240413/bin/hsc2hs-ghc-9.10.0.20240413 with the following script.

#!/bin/bash

GHCVER=9.10.0.20240413
ARCHID=javascript-unknown-ghcjs

exedir="$HOME/.ghcup/ghc/$ARCHID-$GHCVER/lib/$ARCHID-ghc-$GHCVER/bin"
exeprog="./$ARCHID-hsc2hs-ghc-$GHCVER"
executablename="$HOME/.ghcup/ghc/$ARCHID-$GHCVER/lib/$ARCHID-ghc-$GHCVER/bin/./$ARCHID-hsc2hs-ghc-$GHCVER"
bindir="$HOME/.ghcup/ghc/$ARCHID-$GHCVER/bin"
libdir="$HOME/.ghcup/ghc/$ARCHID-$GHCVER/lib/$ARCHID-ghc-$GHCVER/lib"
docdir="$HOME/.ghcup/ghc/$ARCHID-$GHCVER/share/doc/$ARCHID-ghc-$GHCVER"
includedir="$HOME/.ghcup/ghc/$ARCHID-$GHCVER/include"

HSC2HS_C=" -Qunused-arguments"

HSC2HS_L=" "

tflag="--template=$libdir/template-hsc.h"
Iflag="-I$includedir/include/"

for f in ${HSC2HS_C}; do
  cflags="${cflags} --cflag=$f"
done

for f in ${HSC2HS_L}; do
  lflags="${lflags} --lflag=$f"
done

HSC2HS_EXTRA="$cflags $lflags"

read_response() {
    response_file=$1
    if [ -f "$response_file" ]; then
        while read -r arg; do
            case "$arg" in
                -t*)          tflag=;;
                --template=*) tflag=;;
                @*)           read_response "${arg#"@"}" ;;
                --)           break;;
            esac
        done < "$response_file"
    fi
}

for arg do
    case "$arg" in
        -t*)          tflag=;;
        --template=*) tflag=;;
        @*)           read_response "${arg#"@"}" ;;
        --)           break;;
    esac
done

exec "$executablename" ${tflag:+"$tflag"} $HSC2HS_EXTRA ${1+"$@"} "$Iflag"
aidatorajiro commented 1 week ago

Note: instead of replacing the script, simply appending --with-hsc2hs=javascript-unknown-ghcjs-hsc2hs-9.10.0.20240413 to cabal build (along with --with-ghc=javascript-unknown-ghcjs-ghc-9.10.0.20240413 --with-ghc-pkg=javascript-unknown-ghcjs-ghc-pkg-9.10.0.20240413) also fixes build.

hasufell commented 1 week ago

This is expected. You have to tell cabal exactly what toolchain to use.

That's a bit cumbersome and could be documented.

It would be good if you could tell cabal a prefix it will then prepend to all tools. I thought there is a ticket for it, but I can't find it. Feel free to create one.