Skip to content

Commit

Permalink
Add a missing linker flag for clang on macOS (#642)
Browse files Browse the repository at this point in the history
* [WIP] Add a missing linker flag for clang on macOS

* Use `-lc++abi`

* Add comment for `STATIC_FLAG`

* Only Clang requires `-lc++abi`
  • Loading branch information
ken-matsui authored Jun 19, 2022
1 parent 7f62c99 commit 6905c04
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions cmake/ConfigRelease.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ function(enable_ipo)
endif ()
endfunction()

if (NOT APPLE)
set(STATIC_FLAG "-static ")
endif ()

if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
# ref: https://stackoverflow.com/a/3801032
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -flto -mtune=native")
Expand All @@ -31,22 +27,31 @@ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
SET(CMAKE_CXX_ARCHIVE_FINISH true)
enable_ipo()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if (APPLE)
# For nix, ref: https://github.com/NixOS/nixpkgs/issues/166205
set(STATIC_FLAG "-lc++abi")
else ()
set(STATIC_FLAG "-static")
endif ()

set(CMAKE_CXX_FLAGS_RELEASE "-O3")
if (${CMAKE_CXX_COMPILER} MATCHES "-[0-9]+$")
string(REGEX REPLACE [[.*clang\+\+(-[0-9]+)$]] [[lld\1]] LINKER ${CMAKE_CXX_COMPILER})
set(STATIC_LINK_FLAG "${STATIC_FLAG}-fuse-ld=${LINKER}")
set(STATIC_LINK_FLAG "${STATIC_FLAG} -fuse-ld=${LINKER}")
else ()
find_program(lld_EXECUTABLE lld)
if (lld_EXECUTABLE)
set(STATIC_LINK_FLAG "${STATIC_FLAG}-fuse-ld=lld")
set(STATIC_LINK_FLAG "${STATIC_FLAG} -fuse-ld=lld")
else ()
set(STATIC_LINK_FLAG "${STATIC_FLAG}") # use `ld`
endif ()
endif ()
enable_ipo()
else ()
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(STATIC_LINK_FLAG "${STATIC_FLAG}")
if (NOT APPLE)
set(STATIC_LINK_FLAG "-static")
endif ()
enable_ipo()
endif ()

Expand Down

0 comments on commit 6905c04

Please sign in to comment.