Skip to content

Commit 863af4c

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Add LTO support to macOS builds (#75)
Summary: Pull Request resolved: #75 Reviewed By: yoney Differential Revision: D105126039 Pulled By: alexmalyshev fbshipit-source-id: 1cee3b83c9581ce5cb9d30eb452a8001f0b8e6eb
1 parent f5d83f9 commit 863af4c

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

CMakeLists.txt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS} ${SHARED_FLAGS}")
6868
option(ENABLE_LTO "Enable Link-Time Optimization (full)" OFF)
6969

7070
if(ENABLE_LTO)
71-
# Only support Linux
72-
if(MACOS)
73-
message(FATAL_ERROR "LTO is only supported on Linux")
74-
endif()
75-
76-
message(STATUS "LTO: Enabled (full LTO)")
71+
message(STATUS "LTO: Enabled")
7772

7873
# Detect compiler type
7974
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
@@ -84,17 +79,29 @@ if(ENABLE_LTO)
8479
message(FATAL_ERROR "LTO is only supported with Clang or GCC compilers")
8580
endif()
8681

87-
# For Clang, we need llvm-ar instead of regular ar
82+
# For Clang, we need llvm-ar instead of regular ar. On macOS, try to find
83+
# llvm-ar, but it's optional as the system ar may work with AppleClang's LTO.
8884
if(USING_CLANG)
8985
find_program(LLVM_AR llvm-ar)
90-
if(NOT LLVM_AR)
86+
if(LLVM_AR)
87+
set(CMAKE_AR ${LLVM_AR})
88+
message(STATUS "LTO: Using llvm-ar: ${CMAKE_AR}")
89+
elseif(MACOS)
90+
message(STATUS
91+
"LTO: Using system ar (llvm-ar not found,"
92+
" AppleClang may work with system ar)")
93+
else()
9194
message(FATAL_ERROR "llvm-ar is required for LTO with Clang but was not found")
9295
endif()
93-
set(CMAKE_AR ${LLVM_AR})
94-
message(STATUS "LTO: Using llvm-ar: ${CMAKE_AR}")
9596

96-
set(LTO_FLAG "-flto")
97-
set(LTO_LINKER_FLAGS "-flto")
97+
# Use ThinLTO on macOS for better performance and lower memory usage.
98+
if(MACOS)
99+
set(LTO_FLAG "-flto=thin")
100+
set(LTO_LINKER_FLAGS "-flto=thin")
101+
else()
102+
set(LTO_FLAG "-flto")
103+
set(LTO_LINKER_FLAGS "-flto")
104+
endif()
98105
elseif(USING_GCC)
99106
set(LTO_FLAG "-flto")
100107
set(LTO_LINKER_FLAGS "-flto -fuse-linker-plugin -ffat-lto-objects")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ test-requires = ["pytest"]
5454
test-command = "pytest {project}/cinderx/PythonLib/test_cinderx/ --ignore={project}/cinderx/PythonLib/test_cinderx/test_cpython_overrides/test__opcode.py"
5555

5656
[tool.cibuildwheel.linux]
57-
# LTO is Linux-only in CMakeLists.txt right now.
5857
environment = { CINDERX_ENABLE_PGO = "1", CINDERX_ENABLE_LTO = "1" }
5958

6059
[tool.cibuildwheel.macos]
6160
# Not building for macOS x86-64.
6261
archs = ["arm64"]
62+
environment = { CINDERX_ENABLE_LTO = "1" }

0 commit comments

Comments
 (0)