From 9594df76de9e06d86ebe45a2e83ed149f024f35b Mon Sep 17 00:00:00 2001
From: xyb <xyb@xyb.name>
Date: Mon, 9 May 2022 09:21:43 -0700
Subject: [PATCH] Add gcc-12 build

---
 README.md                             | 34 +++++++++++++--
 amd64/gcc-12-ubuntu/DEBIAN/control.m4 |  6 +++
 amd64/gcc-12-ubuntu/Dockerfile        | 63 +++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 3 deletions(-)
 create mode 100644 amd64/gcc-12-ubuntu/DEBIAN/control.m4
 create mode 100644 amd64/gcc-12-ubuntu/Dockerfile

diff --git a/README.md b/README.md
index 27eee36..0248cd2 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,34 @@
 # GCC Build Docker for Ubuntu
 
-## Build gcc-11/aarch64-gcc-11 for Ubuntu 20.04
+## GCC 12
+
+### Build gcc-12/amd64 for Ubuntu 20.04
+
+Run the following command to build gcc-12/amd64:
+
+    cd amd64/gcc-12-ubuntu
+    docker build -t gcc-12.1.0:ubuntu-20.04 .
+    docker run --rm --entrypoint cat gcc-12.1.0:ubuntu-20.04 /gcc-12.1.0-1-ubuntu-20.04.deb > /tmp/gcc-12.1.0-1-ubuntu-20.04.deb
+
+### Install
+
+Now you can install it:
+
+    sudo apt install -y /tmp/gcc-12.1.0-1-ubuntu-20.04.deb
+
+Set gcc-12 as high priority:
+
+    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12
+    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 12
+
+For convenience, add `/usr/lib64` to `LD_LIBRARY_PATH` environment:
+
+    export LD_LIBRARY_PATH=/usr/lib64:$LD_LIBRARY_PATH
+    echo "export LD_LIBRARY_PATH=\"/usr/lib64:\$LD_LIBRARY_PATH\"" >> ~/.bashrc
+
+## GCC 11
+
+### Build gcc-11/aarch64-gcc-11 for Ubuntu 20.04
 
 Run the following command to build gcc-11/aarch64-gcc-11:
 
@@ -8,7 +36,7 @@ Run the following command to build gcc-11/aarch64-gcc-11:
     docker build -t gcc-11.2:ubuntu-20.04 --build-arg UbuntuVersion=20.04 .
     docker run --rm --entrypoint cat gcc-11.2:ubuntu-20.04 /gcc-11.2.0-1-ubuntu-20.04.deb > /tmp/gcc-11.2.0-1-ubuntu-20.04.deb
 
-## Build gcc-11/aarch64-gcc-11 for Ubuntu 18.04
+### Build gcc-11/aarch64-gcc-11 for Ubuntu 18.04
 
 Run the following command to build gcc-11/aarch64-gcc-11:
 
@@ -16,7 +44,7 @@ Run the following command to build gcc-11/aarch64-gcc-11:
     docker build -t gcc-11.2:ubuntu-18.04 --build-arg UbuntuVersion=18.04 .
     docker run --rm --entrypoint cat gcc-11.2:ubuntu-18.04 /gcc-11.2.0-1-ubuntu-18.04.deb > /tmp/gcc-11.2.0-1-ubuntu-18.04.deb
 
-## Install
+### Install
 
 Now you can install it:
 
diff --git a/amd64/gcc-12-ubuntu/DEBIAN/control.m4 b/amd64/gcc-12-ubuntu/DEBIAN/control.m4
new file mode 100644
index 0000000..1e83dbd
--- /dev/null
+++ b/amd64/gcc-12-ubuntu/DEBIAN/control.m4
@@ -0,0 +1,6 @@
+Package: gcc-12-ubuntu-UBUNTUVERSION
+Version: VERSION
+Architecture: amd64
+Maintainer: xyb <xyb@xyb.name>
+Description: Gcc VERSION build for ubuntu UBUNTUVERSION
+Depends: binutils, libc6-dev
diff --git a/amd64/gcc-12-ubuntu/Dockerfile b/amd64/gcc-12-ubuntu/Dockerfile
new file mode 100644
index 0000000..c28ce67
--- /dev/null
+++ b/amd64/gcc-12-ubuntu/Dockerfile
@@ -0,0 +1,63 @@
+ARG UbuntuVersion=20.04
+
+FROM ubuntu:$UbuntuVersion
+
+ARG Gcc12Version=12.1.0
+
+# Download gcc source code
+RUN apt update && apt install -y wget build-essential file flex libz-dev libzstd-dev
+RUN wget https://gcc.gnu.org/pub/gcc/releases/gcc-$Gcc12Version/gcc-$Gcc12Version.tar.gz
+RUN tar xf gcc-$Gcc12Version.tar.gz
+RUN cd /gcc-$Gcc12Version && ./contrib/download_prerequisites
+
+# Build x86_64-gcc-12
+RUN mkdir /x86_64-gcc-12
+WORKDIR /x86_64-gcc-12
+RUN /gcc-$Gcc12Version/configure \
+    --enable-languages=c,c++ \
+    --prefix=/usr \
+    --with-gcc-major-version-only \
+    --program-suffix=-12 \
+    --enable-shared \
+    --enable-linker-build-id \
+    --libexecdir=/usr/lib \
+    --without-included-gettext \
+    --enable-threads=posix \
+    --libdir=/usr/lib \
+    --disable-nls \
+    --enable-clocale=gnu \
+    --enable-libstdcxx-debug \
+    --enable-libstdcxx-time=yes \
+    --with-default-libstdcxx-abi=new \
+    --enable-gnu-unique-object \
+    --disable-vtable-verify \
+    --enable-plugin \
+    --enable-default-pie \
+    --with-system-zlib \
+    --enable-libphobos-checking=release \
+    --with-target-system-zlib=auto \
+    --disable-werror \
+    --enable-cet \
+    --with-abi=m64 \
+    --disable-multilib \
+    --with-tune=generic \
+    --without-cuda-driver \
+    --enable-checking=release \
+    --build=x86_64-linux-gnu \
+    --host=x86_64-linux-gnu \
+    --target=x86_64-linux-gnu
+RUN make -j`nproc`
+
+ARG BuildVersion=1
+ARG UbuntuVersion=20.04
+
+RUN make install-strip DESTDIR=/gcc-$Gcc12Version-$BuildVersion-ubuntu-$UbuntuVersion
+RUN mkdir -p /gcc-$Gcc12Version-$BuildVersion-ubuntu-$UbuntuVersion/usr/share/gdb/auto-load/usr/lib64 && \
+    mv /gcc-$Gcc12Version-$BuildVersion-ubuntu-$UbuntuVersion/usr/lib64/libstdc++.so.6.0.30-gdb.py /gcc-$Gcc12Version-$BuildVersion-ubuntu-$UbuntuVersion/usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.30-gdb.py
+
+# Generate deb
+WORKDIR /
+COPY DEBIAN/control.m4 /
+RUN mkdir /gcc-$Gcc12Version-$BuildVersion-ubuntu-$UbuntuVersion/DEBIAN
+RUN m4 -P -DUBUNTUVERSION=$UbuntuVersion -DVERSION=$Gcc12Version-$BuildVersion control.m4 > /gcc-$Gcc12Version-$BuildVersion-ubuntu-$UbuntuVersion/DEBIAN/control
+RUN dpkg-deb --build --root-owner-group /gcc-$Gcc12Version-$BuildVersion-ubuntu-$UbuntuVersion
\ No newline at end of file