Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce compiled tdlib size #2020

Closed
XDABlackMesa123 opened this issue Jun 19, 2022 · 8 comments
Closed

Reduce compiled tdlib size #2020

XDABlackMesa123 opened this issue Jun 19, 2022 · 8 comments

Comments

@XDABlackMesa123
Copy link

I've noticed that the libtdjni.so lib compiled in my Android Studio project is slightly bigger than the prebuilt/TGX one (4MB less). Is there any way to optimize even more my build? Here's the CMake flags I'm currently using (took from Issue #77):

string(APPEND CMAKE_CXX_FLAGS "-std=c++14 -fno-omit-frame-pointer -ffunction-sections -fdata-sections -fno-exceptions -fno-rtti")
string(APPEND CMAKE_SHARED_LINKER_FLAGS "-Wl,--gc-sections -Wl,--exclude-libs,ALL")
# ...
add_custom_command(TARGET tdjni POST_BUILD
  COMMAND ${CMAKE_STRIP} --strip-debug --strip-unneeded $<TARGET_FILE:tdjni>)

build.gradle:

externalNativeBuild {
    cmake {
        version '3.18.1'
        targets "tdjni"
        arguments '-DCMAKE_BUILD_TYPE=MinSizeRel'
    }
}
@levlam
Copy link
Contributor

levlam commented Jun 19, 2022

You need to use also
string(APPEND CMAKE_CXX_FLAGS_MINSIZEREL " -flto=thin -Oz")

@levlam
Copy link
Contributor

levlam commented Jun 19, 2022

Also, the prebuilt version links libc++ dynamically, so it still be slighly less even after the change, but the total size of libtdjni and libc++ should be bigger after that.

@XDABlackMesa123
Copy link
Author

These flags had no effect unfortunately, I've also just noticed this warning during the build

> Task :app:stripDebugDebugSymbols
Unable to strip the following libraries, packaging them as they are: libtdjni.so.

CMakeOutput.log
CMakeError.log

The warning won't appear with no CMAKE flags+custom strip command set in the CMakeLists file

@XDABlackMesa123
Copy link
Author

So I managed to fix the app:stripDebugDebugSymbols error, for some strange reason I also had to add ndkVersion in my app module. unfortunately the two new flags you suggested still have no effect.

@levlam
Copy link
Contributor

levlam commented Jun 20, 2022

Could you recheck that the flags are appended to CMAKE_CXX_FLAGS_MINSIZEREL? I don't see them in CMake logs.

@XDABlackMesa123
Copy link
Author

I can see those flags in these files:
build.ninja
compile_commands.json

I noticed that the flags that actually shrink the lib are the SHARED_LINKER ones, CMAKE_CXX_FLAGS have no effect on the lib size. By adding ndkVersion in the app module I could remove the strip custom_command in CMake since it is now done at compile time via gradle with app:stripDebugDebugSymbols

@levlam
Copy link
Contributor

levlam commented Jun 20, 2022

No, the output confirms that there were no flags added. You need to add the line string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -flto=thin -Oz") before add_subdirectory(td).

@XDABlackMesa123
Copy link
Author

You meant CMAKE_CXX_FLAGS_MINSIZEREL right? I've moved the MinSizeRel flags before the add_subdirectory(td) and now the lib size has finally been reduced. Thank you again for your help, hoping I won't be bothering you again like I already did😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants