Skip to content

Commit

Permalink
Implement target_compile_options_if_exists, target_link_options_if_ex…
Browse files Browse the repository at this point in the history
…ists
  • Loading branch information
ilya-fedin authored and john-preston committed Sep 21, 2022
1 parent 19aa4dd commit 848c165
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 52 deletions.
89 changes: 37 additions & 52 deletions options_linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
# For license and copyright information please follow this link:
# https://github.com/desktop-app/legal/blob/master/LEGAL

include(CheckCXXCompilerFlag)

target_compile_options(common_options
target_compile_options_if_exists(common_options
INTERFACE
-fstack-protector-all
-fstack-clash-protection
-fPIC
$<IF:$<CONFIG:Debug>,,-fno-strict-aliasing>
-pipe
-Wall
-Wextra
-Wno-unused-parameter
-Wno-switch
-Wno-maybe-uninitialized
-Wno-missing-field-initializers
-Wno-sign-compare
-Wno-deprecated # implicit capture of 'this' via '[=]' is deprecated in C++20
Expand All @@ -28,18 +28,16 @@ INTERFACE
_GLIBCXX_ASSERTIONS
)

target_link_options(common_options
target_link_options_if_exists(common_options
INTERFACE
-pthread
-Wl,--as-needed
)

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(common_options
INTERFACE
-fstack-clash-protection
-Wno-maybe-uninitialized
)
endif()
target_link_libraries(common_options
INTERFACE
${CMAKE_DL_LIBS}
)

if (DESKTOP_APP_SPECIAL_TARGET)
target_compile_options(common_options
Expand All @@ -57,6 +55,34 @@ if (DESKTOP_APP_SPECIAL_TARGET)
target_link_options(common_options INTERFACE $<IF:$<CONFIG:Debug>,,-g -flto -fuse-linker-plugin>)
endif()

if (NOT DESKTOP_APP_USE_PACKAGED)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_options(common_options
INTERFACE
-static-libstdc++
-static-libgcc
)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_link_static_libraries(common_options
INTERFACE
c++
c++abi
)
target_link_options(common_options
INTERFACE
-nostdlib++
)
endif()
target_link_options(common_options
INTERFACE
-rdynamic
-fwhole-program
-Wl,-z,relro
-Wl,-z,now
# -pie # https://gitlab.gnome.org/GNOME/nautilus/-/issues/1601
)
endif()

if (NOT DESKTOP_APP_DISABLE_JEMALLOC)
target_link_libraries(common_options
INTERFACE
Expand All @@ -65,11 +91,6 @@ if (NOT DESKTOP_APP_DISABLE_JEMALLOC)
)
endif()

target_link_libraries(common_options
INTERFACE
${CMAKE_DL_LIBS}
)

if (DESKTOP_APP_USE_ALLOCATION_TRACER)
target_link_options(common_options
INTERFACE
Expand All @@ -92,39 +113,3 @@ if (DESKTOP_APP_USE_ALLOCATION_TRACER)
$<TARGET_FILE:desktop-app::linux_allocation_tracer>
)
endif()

check_cxx_compiler_flag(-pthread DESKTOP_APP_HAVE_PTHREAD_ARG)
if (DESKTOP_APP_HAVE_PTHREAD_ARG)
target_link_options(common_options
INTERFACE
-pthread
)
endif()

if (NOT DESKTOP_APP_USE_PACKAGED)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_options(common_options
INTERFACE
-static-libstdc++
-static-libgcc
)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_link_static_libraries(common_options
INTERFACE
c++
c++abi
)
target_link_options(common_options
INTERFACE
-nostdlib++
)
endif()
target_link_options(common_options
INTERFACE
-rdynamic
-fwhole-program
-Wl,-z,relro
-Wl,-z,now
# -pie # https://gitlab.gnome.org/GNOME/nautilus/-/issues/1601
)
endif()
45 changes: 45 additions & 0 deletions target_compile_options_if_exists.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file is part of Desktop App Toolkit,
# a set of libraries for developing nice desktop applications.
#
# For license and copyright information please follow this link:
# https://github.com/desktop-app/legal/blob/master/LEGAL

include(CheckCXXCompilerFlag)

function(target_compile_options_if_exists target_name)
set(list ${ARGV})
list(REMOVE_AT list 0)

set(writing_now "")
set(private_options "")
set(public_options "")
set(interface_options "")
foreach (entry ${list})
if (${entry} STREQUAL "PRIVATE" OR ${entry} STREQUAL "PUBLIC" OR ${entry} STREQUAL "INTERFACE")
set(writing_now ${entry})
else()
check_cxx_compiler_flag(${entry} flag_exists)
if (flag_exists)
if ("${writing_now}" STREQUAL "PRIVATE")
list(APPEND private_options ${entry})
elseif ("${writing_now}" STREQUAL "PUBLIC")
list(APPEND public_options ${entry})
elseif ("${writing_now}" STREQUAL "INTERFACE")
list(APPEND interface_options ${entry})
else()
message(FATAL_ERROR "Unknown frameworks scope for target ${target_name}")
endif()
endif()
endif()
endforeach()

if (NOT "${public_options}" STREQUAL "")
target_compile_options(${target_name} PUBLIC ${public_options})
endif()
if (NOT "${private_options}" STREQUAL "")
target_compile_options(${target_name} PRIVATE ${private_options})
endif()
if (NOT "${interface_options}" STREQUAL "")
target_compile_options(${target_name} INTERFACE ${interface_options})
endif()
endfunction()
45 changes: 45 additions & 0 deletions target_link_options_if_exists.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file is part of Desktop App Toolkit,
# a set of libraries for developing nice desktop applications.
#
# For license and copyright information please follow this link:
# https://github.com/desktop-app/legal/blob/master/LEGAL

include(CheckCXXCompilerFlag)

function(target_link_options_if_exists target_name)
set(list ${ARGV})
list(REMOVE_AT list 0)

set(writing_now "")
set(private_options "")
set(public_options "")
set(interface_options "")
foreach (entry ${list})
if (${entry} STREQUAL "PRIVATE" OR ${entry} STREQUAL "PUBLIC" OR ${entry} STREQUAL "INTERFACE")
set(writing_now ${entry})
else()
check_cxx_compiler_flag(${entry} flag_exists)
if (flag_exists)
if ("${writing_now}" STREQUAL "PRIVATE")
list(APPEND private_options ${entry})
elseif ("${writing_now}" STREQUAL "PUBLIC")
list(APPEND public_options ${entry})
elseif ("${writing_now}" STREQUAL "INTERFACE")
list(APPEND interface_options ${entry})
else()
message(FATAL_ERROR "Unknown frameworks scope for target ${target_name}")
endif()
endif()
endif()
endforeach()

if (NOT "${public_options}" STREQUAL "")
target_link_options(${target_name} PUBLIC ${public_options})
endif()
if (NOT "${private_options}" STREQUAL "")
target_link_options(${target_name} PRIVATE ${private_options})
endif()
if (NOT "${interface_options}" STREQUAL "")
target_link_options(${target_name} INTERFACE ${interface_options})
endif()
endfunction()

0 comments on commit 848c165

Please sign in to comment.