FROM alpine-build:3.19 AS boringssl-base1
WORKDIR /src/
COPY x509_bssl.patch /src/x509_bssl.patch
RUN git clone https://github.com/google/boringssl.git
WORKDIR /src/boringssl/
RUN git checkout 945f4a5d8fd6abf0c7471242151c727e02e889a2
RUN git apply /src/x509_bssl.patch
WORKDIR /build/
RUN cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=1 /src/boringssl/ &&\
  make
# copy all libs we need
RUN mkdir /libdeps
RUN cp $(LD_LIBRARY_PATH="/build/lib/" ldd /build/bssl | awk '$3=="" {print $1}; $3!="" {print $3}') /libdeps/


FROM entrypoint AS boringssl-base2
COPY --from=boringssl-base1 /src/boringssl/include/ /artifacts/ssllib/include/
COPY --from=boringssl-base1 /build/libssl.so /build/libcrypto.so /artifacts/ssllib/lib/
COPY --from=boringssl-base1 /libdeps/* /artifacts/ssllib/libdeps/
COPY --from=boringssl-base1 /libdeps/* /lib/
COPY --from=boringssl-base1 /build/bssl \
  /build/ssl/test/bssl_shim /bin/

FROM boringssl-base2 AS boringssl-server
ARG VERSION
LABEL "tls_implementation"="boringssl"
LABEL "tls_implementation_version"="945f4a5_x509_anvil"
LABEL "tls_implementation_connectionRole"="server"
ENTRYPOINT ["server-entrypoint", "bssl", "s_server"]

FROM boringssl-base2 AS boringssl-client
ARG VERSION
LABEL "tls_implementation"="boringssl"
LABEL "tls_implementation_version"="945f4a5_x509_anvil"
LABEL "tls_implementation_connectionRole"="client"
ENTRYPOINT ["client-entrypoint", "bssl", "s_client"]
