diff --git a/CMakeLists.txt b/CMakeLists.txt index cbf9037499..4bf27fb030 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,12 +50,6 @@ include(CMakeDependentOption) include(CheckSymbolExists) include(GNUInstallDirs) -# Build mold itself using mold if -DMOLD_USE_MOLD=ON -option(MOLD_USE_MOLD "Use mold to build mold" OFF) -if(MOLD_USE_MOLD) - add_link_options(-fuse-ld=mold -Wl,--gdb-index) -endif() - add_executable(mold) target_compile_features(mold PRIVATE cxx_std_20) @@ -65,11 +59,23 @@ else() target_link_libraries(mold PRIVATE ${CMAKE_DL_LIBS}) endif() +# Build mold itself using mold if -DMOLD_USE_MOLD=ON +option(MOLD_USE_MOLD "Use mold to build mold" OFF) +if(MOLD_USE_MOLD) + target_link_options(mold PRIVATE -fuse-ld=mold -Wl,--gdb-index) + + if(CMAKE_BUILD_TYPE STREQUAL "Release") + target_link_options(mold PRIVATE -Wl,--gc-sections,--icf=safe) + endif() +endif() + if(NOT "${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC") target_compile_options(mold PRIVATE -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables + -ffunction-sections + -fdata-sections -Wall -Wextra -Wno-sign-compare diff --git a/dist.sh b/dist.sh index 1137feaadd..d5b478b441 100755 --- a/dist.sh +++ b/dist.sh @@ -163,6 +163,7 @@ cd /build cmake -DCMAKE_BUILD_TYPE=Release -DMOLD_MOSTLY_STATIC=On /mold cmake --build . -j\$(nproc) mv mold mold2 +cmake -DCMAKE_EXE_LINKER_FLAGS='-Wl,--gc-sections,--icf=safe' . ./mold2 -run cmake --build . -j\$(nproc) ctest -j\$(nproc) cmake --install . --prefix $dest --strip