From da2f1b3d9737f5e1040ae8be30cee31cfa2c6acc Mon Sep 17 00:00:00 2001
From: Ilya Fedin <fedin-ilja2010@ya.ru>
Date: Thu, 3 Nov 2022 23:47:28 +0400
Subject: [PATCH] Add switch for LTO in Docker image

---
 .github/workflows/docker.yml                       | 2 +-
 Telegram/build/docker/centos_env/Dockerfile        | 4 +++-
 Telegram/build/docker/centos_env/gen_dockerfile.py | 3 ++-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml
index d7c749b8e..8b66c3a19 100644
--- a/.github/workflows/docker.yml
+++ b/.github/workflows/docker.yml
@@ -31,7 +31,7 @@ jobs:
         run: |
           cd Telegram/build/docker/centos_env
           poetry install
-          DEBUG= poetry run gen_dockerfile | docker buildx build -t $IMAGE_TAG -
+          DEBUG= LTO= poetry run gen_dockerfile | docker buildx build -t $IMAGE_TAG -
 
       - name: Push the Docker image.
         if: ${{ github.ref_name == github.event.repository.default_branch }}
diff --git a/Telegram/build/docker/centos_env/Dockerfile b/Telegram/build/docker/centos_env/Dockerfile
index 36f3a58c2..0926e83ac 100644
--- a/Telegram/build/docker/centos_env/Dockerfile
+++ b/Telegram/build/docker/centos_env/Dockerfile
@@ -44,10 +44,12 @@ RUN mkdir /opt/cmake \
 	&& rm {{ CMAKE_FILE }}
 
 FROM builder-base AS builder
+{%- if LTO %}
 ENV AR gcc-ar
 ENV RANLIB gcc-ranlib
 ENV NM gcc-nm
-ENV CFLAGS {% if DEBUG %}-g{% endif %} -Ofast {% if DEBUG %}-flto=auto -ffat-lto-objects{% endif %} -pipe -fPIC -fstack-protector-all -fstack-clash-protection -DNDEBUG -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS
+{%- endif %}
+ENV CFLAGS {% if DEBUG %}-g{% endif %} -Ofast {% if LTO %}-flto=auto -ffat-lto-objects{% endif %} -pipe -fPIC -fstack-protector-all -fstack-clash-protection -DNDEBUG -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS
 ENV CXXFLAGS $CFLAGS
 
 FROM builder AS patches
diff --git a/Telegram/build/docker/centos_env/gen_dockerfile.py b/Telegram/build/docker/centos_env/gen_dockerfile.py
index ca496447f..76143aa39 100755
--- a/Telegram/build/docker/centos_env/gen_dockerfile.py
+++ b/Telegram/build/docker/centos_env/gen_dockerfile.py
@@ -5,7 +5,8 @@ from jinja2 import Environment, FileSystemLoader
 
 def main():
     print(Environment(loader=FileSystemLoader(dirname(__file__))).get_template("Dockerfile").render(
-        DEBUG=bool(len(environ["DEBUG"])) if "DEBUG" in environ else True
+        DEBUG=bool(len(environ["DEBUG"])) if "DEBUG" in environ else True,
+        LTO=bool(len(environ["LTO"])) if "LTO" in environ else True,
     ))
 
 if __name__ == '__main__':