Skip to content

Instantly share code, notes, and snippets.

@thumphries
Last active July 6, 2016 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thumphries/b08f8b9ba42385afc8a4f2ceb097e2ab to your computer and use it in GitHub Desktop.
Save thumphries/b08f8b9ba42385afc8a4f2ceb097e2ab to your computer and use it in GitHub Desktop.
ghc-musl bootstrapping
V = 0
DYNAMIC_GHC_PROGRAMS = NO
GhcLibWays = v
SRC_HC_OPTS = -O -H64m -optl--no-pie
GhcStage1HcOpts = -O -fasm
GhcStage2HcOpts = -O2 -fasm
GhcHcOpts = -Rghc-timing
GhcLibHcOpts = -O2
FROM alpine:latest
COPY build.mk /tmp/
RUN apk add --update curl xz alpine-sdk perl gmp-dev file gmp openssh openssl zlib-dev strace vim less jq ncurses-dev bash autoconf
RUN cd /tmp && \
wget http://ghc-musl-dist-apse2.s3-website-ap-southeast-2.amazonaws.com/ghc-7.8.4/ghc-7.8.4-x86_64-unknown-linux.tar.bz2 && \
tar xvf ghc*.tar.bz2 && \
cd ghc-7.8.4 && \
./configure && \
: musl ld requires --no-pie to work for some reason with ghc && \
sed -i '/C\ compiler\ link/{ s/""/"--no-pie"/ }' settings && \
make install
RUN cd /tmp && \
wget https://www.haskell.org/ghc/dist/7.10.3/ghc-7.10.3-src.tar.bz2 && \
tar xvfj ghc-7.10.3-src.tar.bz2 && \
cd /tmp/ghc-7.10.3 && \
cp -v /tmp/build.mk mk/build.mk && \
./configure && \
# : "libffi has a bug, which we patch here" && \
# sed -i 's,chmod,sed -i s/__gnu_linux__/1/ libffi/build/src/closures.c \&\& chmod,' libffi/ghc.mk && \
make -j16 && \
make binary-dist
CMD /bin/sh
FROM debian:8.0
# https://github.com/nilcons/ghc-musl
RUN apt-get update
RUN apt-get install -y musl-tools build-essential wget curl ghc libncurses-dev less vim-tiny autoconf
# file
WORKDIR /tmp
RUN wget https://www.haskell.org/ghc/dist/7.8.4/ghc-7.8.4-src.tar.bz2
RUN tar xvfj ghc-7.8.4-src.tar.bz2
WORKDIR /tmp/ghc-7.8.4
COPY build.mk /tmp/ghc-7.8.4/mk/build.mk
RUN ./configure --target=x86_64-pc-linux-musl --with-gcc=musl-gcc --with-ld=ld --with-nm=nm --with-ar=ar --with-ranlib=ranlib --prefix=/opt/ghc-cross
# stole this from https://github.com/redneb/ghc-alt-libc, thanks!
COPY fix-execvpe-signature-ghc-7.8.4.patch /tmp/fix-execvpe.patch
RUN patch -p1 </tmp/fix-execvpe.patch
# compile without ncurses or terminfo
RUN sed -i s/terminfo// ghc.mk
RUN sed -i s/terminfo// utils/ghc-pkg/ghc-pkg.cabal
RUN sed -i s/unix,/unix/ utils/ghc-pkg/ghc-pkg.cabal
RUN sed -i '1{p; s/.*/#define BOOTSTRAPPING/}' utils/ghc-pkg/Main.hs
# update config.sub in libffi, so it supports x86_64-pc-linux
RUN sed -i 's,chmod,cp /usr/share/misc/config.sub libffi/build/config.sub \&\& chmod,' libffi/ghc.mk
# then just build it!
RUN make -j8
RUN make install
WORKDIR /opt/ghc-cross
# linked with glibc, not musl, build system bug, but we don't need it
RUN rm bin/x86_64-pc-linux-musl-hp2ps
# linked with glibc, not musl, build system bug, let's build an alternative one
RUN rm lib/x86_64-pc-linux-musl-ghc-7.8.4/unlit
RUN ( cd /tmp/ghc-7.8.4/utils/unlit ; musl-gcc unlit.c ) ; cp /tmp/ghc-7.8.4/utils/unlit/a.out lib/x86_64-pc-linux-musl-ghc-7.8.4/unlit
# we want to use normal gcc, not musl-gcc once we move this ghc-cross into a musl based distro
RUN sed -i 's/musl-gcc/gcc/' lib/x86_64-pc-linux-musl-ghc-7.8.4/settings
# musl ld requires --no-pie to work for some reason with ghc
RUN sed -i '/C\ compiler\ link/{ s/""/"--no-pie"/ }' lib/x86_64-pc-linux-musl-ghc-7.8.4/settings
# Remove the cross compiler prefix from binaries
RUN (cd bin ; for i in x86_64-pc-linux-musl-* ; do ln -s $i ${i#x86_64-pc-linux-musl-} ; done )
WORKDIR /
RUN tar cvfJ ghc-7.8.4-x86_64-unknown-linux-musl.tar.xz /opt/ghc-cross
FROM alpine:latest
COPY build.mk /tmp/
RUN apk add --update curl xz alpine-sdk perl gmp-dev file gmp openssh openssl zlib-dev strace vim less jq ncurses-dev bash autoconf
RUN cd /tmp && \
wget http://ghc-musl-dist-apse2.s3-website-ap-southeast-2.amazonaws.com/ghc-7.8.4/ghc-7.8.4-x86_64-unknown-linux.tar.bz2 && \
tar xvf ghc*.tar.bz2 && \
cd ghc-7.8.4 && \
./configure && \
: musl ld requires --no-pie to work for some reason with ghc && \
sed -i '/C\ compiler\ link/{ s/""/"--no-pie"/ }' settings && \
make install
RUN cd /tmp && \
wget https://www.haskell.org/ghc/dist/8.0.1/ghc-8.0.1-src.tar.xz && \
tar xvf ghc-8.0.1-src.tar.xz && \
cd /tmp/ghc-8.0.1 && \
cp -v /tmp/build.mk mk/build.mk && \
./configure && \
make -j16 && \
make binary-dist
CMD /bin/sh
@thumphries
Copy link
Author

thumphries commented Jul 2, 2016

I think it's a configure-time option, but afaict we're using the GHC libffi instead of the system version. in alpine libffi has a few necessary patches, chances are real world ffi stuff will be segfaulty without these. should fix

@thumphries
Copy link
Author

the PIE thing is a GHC issue, being dealt with upstream https://ghc.haskell.org/trac/ghc/ticket/9007

pretty sure this means you couldn't push any alpine packages built here upstream

@thumphries
Copy link
Author

still dynamically linking musl and possibly gmp

will retry with -optl-static -optl-pthread in the GHC stage1 and stage2 build.mk flags
https://ro-che.info/articles/2015-10-26-static-linking-ghc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment