whisper.cpp icon indicating copy to clipboard operation
whisper.cpp copied to clipboard

Fix MUSL Linux build

Open ggerganov opened this issue 1 year ago • 5 comments

close #37

@mikeslattery Could you please check if this works on MUSL Linux?

ggerganov avatar Mar 06 '23 20:03 ggerganov

It did not. However, when I commented out the ifneq it did. $(CCV) is the CC value below:

I whisper.cpp build info:
I UNAME_S:  Linux
I UNAME_P:  unknown
I UNAME_M:  x86_64
I CFLAGS:   -I.              -O3 -DNDEBUG -std=c11   -fPIC -pthread -mavx -mavx2 -mfma -mf16c -msse3
I CXXFLAGS: -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread
I LDFLAGS:
I CC:       cc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924
I CXX:      g++ (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924

mikeslattery avatar Mar 06 '23 21:03 mikeslattery

I don't know if this helps, but this is how I build:

FROM alpine:3.17.2 AS alpine0

RUN mkdir /whisper && \
  wget -q https://github.com/masterful/whisper.cpp/tarball/master -O - | \
  tar -xz -C /whisper --strip-components 1

# git alternative
# RUN apk add git &&
#   git clone --depth 1 https://github.com/ggerganov/whisper.cpp.git /whisper

WORKDIR /whisper

RUN apk add --quiet g++ make bash wget sdl2-dev alsa-utils

# Workaround build
RUN cat > Makefile.musl <<MAKEFILE
CFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE
CXXFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE
MAKEFILE
RUN make -f Makefile -f Makefile.musl main stream command talk bench

# Once workaround is no longer necessary
# RUN make main stream command talk bench

RUN bash ./models/download-ggml-model.sh base.en

FROM alpine:3.17.2

COPY --from=alpine0 /whisper/main /usr/local/bin/whisper
COPY --from=alpine0 /whisper/stream /usr/local/bin/stream
COPY --from=alpine0 /whisper/talk /usr/local/bin/talk
COPY --from=alpine0 /whisper/command /usr/local/bin/wcommand
RUN mkdir /root/models
COPY --from=alpine0 /whisper/models/ggml-base.en.bin /root/models/ggml-base.en.bin

RUN apk add --quiet sdl2-dev alsa-utils

WORKDIR /root

# To use stream in a container (linux hosts only):
# docker build . -t whisper
# docker run -it --rm --device /dev/snd:/dev/snd whisper stream

Then to extract binaries

docker build -t whispercpp .
docker create --name whispercpp whispercpp
docker cp whispercpp:/usr/local/bin/whisper whisper
docker cp whispercpp:/usr/local/bin/stream wstream
docker cp whispercpp:/root/models/ggml-base.en.bin ggml-base.en.bin
docker rm whispercpp

mikeslattery avatar Mar 06 '23 22:03 mikeslattery

Directly adding -D_POSIX_SOURCE -D_GNU_SOURCE to the flags is not desired since it breaks the build for other systems. We need a way to detect MUSL in the Makefile

ggerganov avatar Mar 07 '23 19:03 ggerganov

We need a way to detect MUSL in the Makefile

I asked ChatGPT

List 3 ways within a makefile how you can detect if the system uses musl instead of libc

ifneq ($(wildcard /usr/include/musl/*),)
        CFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE
        CXXFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE
endif

ifeq ($(shell ldd /bin/ls | grep -q musl && echo true),true)
endif

ifeq ($(shell pkg-config --exists musl && echo true),true)
endif

I tested the first and it worked.

mikeslattery avatar Mar 07 '23 21:03 mikeslattery

Currently, I have a job to build the binary for MUSL python wheel and seems like it fails as well. 🤔

aarnphm avatar Mar 08 '23 10:03 aarnphm

Added the suggestion by ChatGPT

ggerganov avatar Mar 22 '23 18:03 ggerganov