Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

452 خطوط
16 KiB
CMake

2021-08-06 13:08:57 -05:00
# include guard
include_guard(DIRECTORY)
2021-09-20 11:12:06 -05:00
# ########################################################################################
2021-08-06 13:08:57 -05:00
#
2021-09-20 11:12:06 -05:00
# Handles the build settings
2021-08-06 13:08:57 -05:00
#
2021-09-20 11:12:06 -05:00
# ########################################################################################
2021-08-06 13:08:57 -05:00
include(GNUInstallDirs)
include(Compilers)
include(FindPackageHandleStandardArgs)
include(MacroUtilities)
rocprofiler_systems_add_option(
ROCPROFSYS_BUILD_DEVELOPER "Extra build flags for development like -Werror"
2025-06-22 10:44:33 -04:00
${ROCPROFSYS_BUILD_CI}
)
rocprofiler_systems_add_option(ROCPROFSYS_BUILD_RELEASE
2025-06-22 10:44:33 -04:00
"Build with minimal debug line info" OFF
)
rocprofiler_systems_add_option(ROCPROFSYS_BUILD_EXTRA_OPTIMIZATIONS
2025-06-22 10:44:33 -04:00
"Extra optimization flags" OFF
)
rocprofiler_systems_add_option(ROCPROFSYS_BUILD_LTO "Build with link-time optimization"
2025-06-22 10:44:33 -04:00
OFF
)
rocprofiler_systems_add_option(ROCPROFSYS_USE_COMPILE_TIMING
2025-06-22 10:44:33 -04:00
"Build with timing metrics for compilation" OFF
)
rocprofiler_systems_add_option(ROCPROFSYS_USE_SANITIZER
2025-06-22 10:44:33 -04:00
"Build with -fsanitze=\${ROCPROFSYS_SANITIZER_TYPE}" OFF
)
rocprofiler_systems_add_option(ROCPROFSYS_BUILD_STATIC_LIBGCC
2025-06-22 10:44:33 -04:00
"Build with -static-libgcc if possible" OFF
)
rocprofiler_systems_add_option(ROCPROFSYS_BUILD_STATIC_LIBSTDCXX
2025-06-22 10:44:33 -04:00
"Build with -static-libstdc++ if possible" OFF
)
rocprofiler_systems_add_option(ROCPROFSYS_BUILD_STACK_PROTECTOR
2025-06-22 10:44:33 -04:00
"Build with -fstack-protector" ON
)
rocprofiler_systems_add_cache_option(
ROCPROFSYS_BUILD_LINKER
2025-06-22 10:44:33 -04:00
"If set to a non-empty value, pass -fuse-ld=\${ROCPROFSYS_BUILD_LINKER}" STRING "bfd"
)
rocprofiler_systems_add_cache_option(ROCPROFSYS_BUILD_NUMBER "Internal CI use" STRING "0"
2025-06-22 10:44:33 -04:00
ADVANCED NO_FEATURE
)
rocprofiler_systems_add_interface_library(rocprofiler-systems-static-libgcc
2025-06-22 10:44:33 -04:00
"Link to static version of libgcc"
)
rocprofiler_systems_add_interface_library(rocprofiler-systems-static-libstdcxx
2025-06-22 10:44:33 -04:00
"Link to static version of libstdc++"
)
rocprofiler_systems_add_interface_library(rocprofiler-systems-static-libgcc-optional
2025-06-22 10:44:33 -04:00
"Link to static version of libgcc"
)
rocprofiler_systems_add_interface_library(rocprofiler-systems-static-libstdcxx-optional
2025-06-22 10:44:33 -04:00
"Link to static version of libstdc++"
)
2025-06-22 10:44:33 -04:00
target_compile_definitions(
rocprofiler-systems-compile-options
INTERFACE $<$<CONFIG:DEBUG>:DEBUG>
)
2025-06-22 10:44:33 -04:00
set(ROCPROFSYS_SANITIZER_TYPE "leak" CACHE STRING "Sanitizer type")
if(ROCPROFSYS_USE_SANITIZER)
rocprofiler_systems_add_feature(
ROCPROFSYS_SANITIZER_TYPE
2025-06-22 10:44:33 -04:00
"Sanitizer type, e.g. leak, thread, address, memory, etc."
)
2022-01-27 21:31:08 -06:00
endif()
2021-08-06 13:08:57 -05:00
if(ROCPROFSYS_BUILD_CI)
rocprofiler_systems_target_compile_definitions(${LIBNAME}-compile-options
2025-06-22 10:44:33 -04:00
INTERFACE ROCPROFSYS_CI
)
2022-02-25 03:56:41 -06:00
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
# dynamic linking and runtime libraries
2021-08-06 13:08:57 -05:00
#
if(CMAKE_DL_LIBS AND NOT "${CMAKE_DL_LIBS}" STREQUAL "dl")
# if cmake provides dl library, use that
2025-06-22 10:44:33 -04:00
set(dl_LIBRARY ${CMAKE_DL_LIBS} CACHE FILEPATH "dynamic linking system library")
2021-08-06 13:08:57 -05:00
endif()
foreach(_TYPE dl rt dw)
if(NOT ${_TYPE}_LIBRARY)
find_library(${_TYPE}_LIBRARY NAMES ${_TYPE})
endif()
endforeach()
find_package_handle_standard_args(dl-library REQUIRED_VARS dl_LIBRARY)
find_package_handle_standard_args(rt-library REQUIRED_VARS rt_LIBRARY)
# find_package_handle_standard_args(dw-library REQUIRED_VARS dw_LIBRARY)
if(dl_LIBRARY)
target_link_libraries(rocprofiler-systems-compile-options INTERFACE ${dl_LIBRARY})
2021-08-06 13:08:57 -05:00
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# set the compiler flags
#
add_flag_if_avail(
2021-09-20 11:12:06 -05:00
"-W" "-Wall" "-Wno-unknown-pragmas" "-Wno-unused-function" "-Wno-ignored-attributes"
2025-06-22 10:44:33 -04:00
"-Wno-attributes" "-Wno-missing-field-initializers" "-Wno-interference-size"
)
2021-08-06 13:08:57 -05:00
if(ROCPROFSYS_BUILD_DEBUG)
2022-09-26 07:52:14 -05:00
add_flag_if_avail("-g3" "-fno-omit-frame-pointer")
2022-08-31 01:24:31 -05:00
endif()
2021-08-06 13:08:57 -05:00
if(WIN32)
# suggested by MSVC for spectre mitigation in rapidjson implementation
add_cxx_flag_if_avail("/Qspectre")
endif()
if(CMAKE_CXX_COMPILER_IS_CLANG)
2021-09-20 11:12:06 -05:00
add_cxx_flag_if_avail("-Wno-mismatched-tags")
2021-08-06 13:08:57 -05:00
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# extra flags for debug information in debug or optimized binaries
#
rocprofiler_systems_add_interface_library(
rocprofiler-systems-compile-debuginfo
2021-09-20 11:12:06 -05:00
"Attempts to set best flags for more expressive profiling information in debug or optimized binaries"
2025-06-22 10:44:33 -04:00
)
2021-08-06 13:08:57 -05:00
add_target_flag_if_avail(rocprofiler-systems-compile-debuginfo "-g3"
2025-06-22 10:44:33 -04:00
"-fno-omit-frame-pointer" "-fno-optimize-sibling-calls"
)
2021-08-06 13:08:57 -05:00
if(CMAKE_CUDA_COMPILER_IS_NVIDIA)
add_target_cuda_flag(rocprofiler-systems-compile-debuginfo "-lineinfo")
2021-08-06 13:08:57 -05:00
endif()
2021-09-20 11:12:06 -05:00
target_compile_options(
rocprofiler-systems-compile-debuginfo
2025-06-22 10:44:33 -04:00
INTERFACE
$<$<COMPILE_LANGUAGE:C>:$<$<C_COMPILER_ID:GNU>:-rdynamic>>
$<$<COMPILE_LANGUAGE:CXX>:$<$<CXX_COMPILER_ID:GNU>:-rdynamic>>
)
2021-08-06 13:08:57 -05:00
if(NOT APPLE)
2025-06-22 10:44:33 -04:00
target_link_options(
rocprofiler-systems-compile-debuginfo
INTERFACE $<$<CXX_COMPILER_ID:GNU>:-rdynamic>
)
2021-08-06 13:08:57 -05:00
endif()
if(CMAKE_CUDA_COMPILER_IS_NVIDIA)
2021-09-20 11:12:06 -05:00
target_compile_options(
rocprofiler-systems-compile-debuginfo
2021-09-20 11:12:06 -05:00
INTERFACE
2025-06-22 10:44:33 -04:00
$<$<COMPILE_LANGUAGE:CUDA>:$<$<CXX_COMPILER_ID:GNU>:-Xcompiler=-rdynamic>>
)
2021-08-06 13:08:57 -05:00
endif()
if(dl_LIBRARY)
target_link_libraries(rocprofiler-systems-compile-debuginfo INTERFACE ${dl_LIBRARY})
2021-08-06 13:08:57 -05:00
endif()
if(rt_LIBRARY)
target_link_libraries(rocprofiler-systems-compile-debuginfo INTERFACE ${rt_LIBRARY})
2021-08-06 13:08:57 -05:00
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# non-debug optimizations
#
rocprofiler_systems_add_interface_library(rocprofiler-systems-compile-extra
2025-06-22 10:44:33 -04:00
"Extra optimization flags"
)
if(NOT ROCPROFSYS_BUILD_CODECOV AND ROCPROFSYS_BUILD_EXTRA_OPTIMIZATIONS)
2021-09-20 11:12:06 -05:00
add_target_flag_if_avail(
rocprofiler-systems-compile-extra "-finline-functions" "-funroll-loops"
2025-06-22 10:44:33 -04:00
"-ftree-vectorize" "-ftree-loop-optimize" "-ftree-loop-vectorize"
)
2021-08-06 13:08:57 -05:00
endif()
2025-06-22 10:44:33 -04:00
if(
NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug"
AND ROCPROFSYS_BUILD_EXTRA_OPTIMIZATIONS
AND NOT ROCPROFSYS_BUILD_CODECOV
)
target_link_libraries(
rocprofiler-systems-compile-options
INTERFACE $<BUILD_INTERFACE:rocprofiler-systems-compile-extra>
)
2021-08-06 13:08:57 -05:00
add_flag_if_avail(
2021-09-20 11:12:06 -05:00
"-fno-signaling-nans" "-fno-trapping-math" "-fno-signed-zeros"
"-ffinite-math-only" "-fno-math-errno" "-fpredictive-commoning"
2025-06-22 10:44:33 -04:00
"-fvariable-expansion-in-unroller"
)
2021-08-06 13:08:57 -05:00
# add_flag_if_avail("-freciprocal-math" "-fno-signed-zeros" "-mfast-fp")
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# debug-safe optimizations
#
add_cxx_flag_if_avail("-faligned-new")
rocprofiler_systems_add_interface_library(rocprofiler-systems-lto
2025-06-22 10:44:33 -04:00
"Adds link-time-optimization flags"
)
2023-02-08 01:31:38 -06:00
if(NOT ROCPROFSYS_BUILD_CODECOV)
rocprofiler_systems_save_variables(FLTO VARIABLES CMAKE_CXX_FLAGS)
2023-02-08 01:31:38 -06:00
set(_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "-flto=thin ${_CXX_FLAGS}")
add_target_flag_if_avail(rocprofiler-systems-lto "-flto=thin")
if(NOT cxx_rocprofiler_systems_lto_flto_thin)
2023-02-08 01:31:38 -06:00
set(CMAKE_CXX_FLAGS "-flto ${_CXX_FLAGS}")
add_target_flag_if_avail(rocprofiler-systems-lto "-flto")
if(NOT cxx_rocprofiler_systems_lto_flto)
set(ROCPROFSYS_BUILD_LTO OFF)
2023-02-08 01:31:38 -06:00
else()
target_link_options(rocprofiler-systems-lto INTERFACE -flto)
2023-02-08 01:31:38 -06:00
endif()
add_target_flag_if_avail(rocprofiler-systems-lto "-fno-fat-lto-objects")
if(cxx_rocprofiler_systems_lto_fno_fat_lto_objects)
target_link_options(rocprofiler-systems-lto INTERFACE -fno-fat-lto-objects)
2023-02-08 01:31:38 -06:00
endif()
2021-08-06 13:08:57 -05:00
else()
target_link_options(rocprofiler-systems-lto INTERFACE -flto=thin)
2022-01-27 21:31:08 -06:00
endif()
2021-08-06 13:08:57 -05:00
rocprofiler_systems_restore_variables(FLTO VARIABLES CMAKE_CXX_FLAGS)
2023-02-08 01:31:38 -06:00
endif()
2021-08-06 13:08:57 -05:00
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# print compilation timing reports (Clang compiler)
#
rocprofiler_systems_add_interface_library(
rocprofiler-systems-compile-timing
2025-06-22 10:44:33 -04:00
"Adds compiler flags which report compilation timing metrics"
)
2021-08-06 13:08:57 -05:00
if(CMAKE_CXX_COMPILER_IS_CLANG)
add_target_flag_if_avail(rocprofiler-systems-compile-timing "-ftime-trace")
if(NOT cxx_rocprofiler_systems_compile_timing_ftime_trace)
add_target_flag_if_avail(rocprofiler-systems-compile-timing "-ftime-report")
2021-08-06 13:08:57 -05:00
endif()
else()
add_target_flag_if_avail(rocprofiler-systems-compile-timing "-ftime-report")
2021-08-06 13:08:57 -05:00
endif()
if(ROCPROFSYS_USE_COMPILE_TIMING)
2025-06-22 10:44:33 -04:00
target_link_libraries(
rocprofiler-systems-compile-options
INTERFACE rocprofiler-systems-compile-timing
)
2021-08-06 13:08:57 -05:00
endif()
2022-08-28 03:56:13 -05:00
# ----------------------------------------------------------------------------------------#
# fstack-protector
#
rocprofiler_systems_add_interface_library(rocprofiler-systems-stack-protector
2025-06-22 10:44:33 -04:00
"Adds stack-protector compiler flags"
)
add_target_flag_if_avail(rocprofiler-systems-stack-protector "-fstack-protector-strong"
2025-06-22 10:44:33 -04:00
"-Wstack-protector"
)
2022-08-28 03:56:13 -05:00
if(ROCPROFSYS_BUILD_STACK_PROTECTOR)
2025-06-22 10:44:33 -04:00
target_link_libraries(
rocprofiler-systems-compile-options
INTERFACE rocprofiler-systems-stack-protector
)
2022-08-28 03:56:13 -05:00
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# developer build flags
#
if(ROCPROFSYS_BUILD_DEVELOPER)
add_target_flag_if_avail(
rocprofiler-systems-compile-options "-Werror" "-Wdouble-promotion" "-Wshadow"
"-Wextra" "-Wpedantic" "-Wstack-usage=524288" # 512 KB
2025-06-22 10:44:33 -04:00
"/showIncludes"
)
if(ROCPROFSYS_BUILD_NUMBER GREATER 2)
add_target_flag_if_avail(rocprofiler-systems-compile-options "-gsplit-dwarf")
2022-11-01 17:28:12 -05:00
endif()
endif()
if(ROCPROFSYS_BUILD_LINKER)
2022-11-01 17:28:12 -05:00
target_link_options(
2025-06-22 10:44:33 -04:00
rocprofiler-systems-compile-options
INTERFACE
$<$<C_COMPILER_ID:GNU>:-fuse-ld=${ROCPROFSYS_BUILD_LINKER}>
$<$<CXX_COMPILER_ID:GNU>:-fuse-ld=${ROCPROFSYS_BUILD_LINKER}>
)
2021-08-06 13:08:57 -05:00
endif()
2023-06-14 11:55:22 -05:00
# ----------------------------------------------------------------------------------------#
# release build flags
#
if(ROCPROFSYS_BUILD_RELEASE AND NOT ROCPROFSYS_BUILD_DEBUG)
2023-06-14 11:55:22 -05:00
add_target_flag_if_avail(
rocprofiler-systems-compile-options "-g1" "-feliminate-unused-debug-symbols"
2025-06-22 10:44:33 -04:00
"-gno-column-info" "-gno-variable-location-views" "-gline-tables-only"
)
2023-06-14 11:55:22 -05:00
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# visibility build flags
#
rocprofiler_systems_add_interface_library(rocprofiler-systems-default-visibility
2025-06-22 10:44:33 -04:00
"Adds -fvisibility=default compiler flag"
)
rocprofiler_systems_add_interface_library(rocprofiler-systems-hidden-visibility
2025-06-22 10:44:33 -04:00
"Adds -fvisibility=hidden compiler flag"
)
2021-08-06 13:08:57 -05:00
add_target_flag_if_avail(rocprofiler-systems-default-visibility "-fvisibility=default")
add_target_flag_if_avail(rocprofiler-systems-hidden-visibility "-fvisibility=hidden"
2025-06-22 10:44:33 -04:00
"-fvisibility-inlines-hidden"
)
2021-08-06 13:08:57 -05:00
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# developer build flags
#
if(dl_LIBRARY)
# This instructs the linker to add all symbols, not only used ones, to the dynamic
# symbol table. This option is needed for some uses of dlopen or to allow obtaining
# backtraces from within a program.
add_flag_if_avail("-rdynamic")
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# sanitizer
#
set(ROCPROFSYS_SANITIZER_TYPES
2021-09-20 11:12:06 -05:00
address
memory
thread
leak
undefined
unreachable
null
bounds
2025-06-22 10:44:33 -04:00
alignment
)
set_property(
CACHE ROCPROFSYS_SANITIZER_TYPE
PROPERTY STRINGS "${ROCPROFSYS_SANITIZER_TYPES}"
)
rocprofiler_systems_add_interface_library(rocprofiler-systems-sanitizer-compile-options
2025-06-22 10:44:33 -04:00
"Adds compiler flags for sanitizers"
)
rocprofiler_systems_add_interface_library(
rocprofiler-systems-sanitizer
"Adds compiler flags to enable ${ROCPROFSYS_SANITIZER_TYPE} sanitizer (-fsanitizer=${ROCPROFSYS_SANITIZER_TYPE})"
2025-06-22 10:44:33 -04:00
)
2021-09-20 11:12:06 -05:00
2025-06-22 10:44:33 -04:00
set(COMMON_SANITIZER_FLAGS
"-fno-optimize-sibling-calls"
"-fno-omit-frame-pointer"
"-fno-inline-functions"
)
add_target_flag(rocprofiler-systems-sanitizer-compile-options ${COMMON_SANITIZER_FLAGS})
2021-08-06 13:08:57 -05:00
foreach(_TYPE ${ROCPROFSYS_SANITIZER_TYPES})
2021-08-06 13:08:57 -05:00
set(_FLAG "-fsanitize=${_TYPE}")
rocprofiler_systems_add_interface_library(
rocprofiler-systems-${_TYPE}-sanitizer
2025-06-22 10:44:33 -04:00
"Adds compiler flags to enable ${_TYPE} sanitizer (${_FLAG})"
)
add_target_flag(rocprofiler-systems-${_TYPE}-sanitizer ${_FLAG})
2025-06-22 10:44:33 -04:00
target_link_libraries(
rocprofiler-systems-${_TYPE}-sanitizer
INTERFACE rocprofiler-systems-sanitizer-compile-options
)
set_property(
TARGET rocprofiler-systems-${_TYPE}-sanitizer
PROPERTY INTERFACE_LINK_OPTIONS ${_FLAG} ${COMMON_SANITIZER_FLAGS}
)
2021-08-06 13:08:57 -05:00
endforeach()
unset(_FLAG)
unset(COMMON_SANITIZER_FLAGS)
if(ROCPROFSYS_USE_SANITIZER)
foreach(_TYPE ${ROCPROFSYS_SANITIZER_TYPE})
if(TARGET rocprofiler-systems-${_TYPE}-sanitizer)
2025-06-22 10:44:33 -04:00
target_link_libraries(
rocprofiler-systems-sanitizer
INTERFACE rocprofiler-systems-${_TYPE}-sanitizer
)
2021-08-06 13:08:57 -05:00
else()
2021-09-20 11:12:06 -05:00
message(
FATAL_ERROR
2025-06-22 10:44:33 -04:00
"Error! Target 'rocprofiler-systems-${_TYPE}-sanitizer' does not exist!"
)
2021-08-06 13:08:57 -05:00
endif()
endforeach()
else()
set(ROCPROFSYS_USE_SANITIZER OFF)
2021-08-06 13:08:57 -05:00
endif()
2022-04-05 00:24:34 -05:00
# ----------------------------------------------------------------------------------------#
# static lib flags
#
target_compile_options(
rocprofiler-systems-static-libgcc
2025-06-22 10:44:33 -04:00
INTERFACE
$<$<COMPILE_LANGUAGE:C>:$<$<C_COMPILER_ID:GNU>:-static-libgcc>>
$<$<COMPILE_LANGUAGE:CXX>:$<$<CXX_COMPILER_ID:GNU>:-static-libgcc>>
)
2022-05-08 04:40:10 -05:00
target_link_options(
2025-06-22 10:44:33 -04:00
rocprofiler-systems-static-libgcc
INTERFACE
$<$<COMPILE_LANGUAGE:C>:$<$<C_COMPILER_ID:GNU,Clang>:-static-libgcc>>
$<$<COMPILE_LANGUAGE:CXX>:$<$<CXX_COMPILER_ID:GNU,Clang>:-static-libgcc>>
)
2022-05-08 04:40:10 -05:00
2022-04-05 00:24:34 -05:00
target_compile_options(
rocprofiler-systems-static-libstdcxx
2025-06-22 10:44:33 -04:00
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:$<$<CXX_COMPILER_ID:GNU>:-static-libstdc++>>
)
2022-05-08 04:40:10 -05:00
target_link_options(
2025-06-22 10:44:33 -04:00
rocprofiler-systems-static-libstdcxx
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:$<$<CXX_COMPILER_ID:GNU,Clang>:-static-libstdc++>>
)
2022-04-05 00:24:34 -05:00
if(ROCPROFSYS_BUILD_STATIC_LIBGCC)
2025-06-22 10:44:33 -04:00
target_link_libraries(
rocprofiler-systems-static-libgcc-optional
INTERFACE rocprofiler-systems-static-libgcc
)
2022-08-28 03:56:13 -05:00
endif()
if(ROCPROFSYS_BUILD_STATIC_LIBSTDCXX)
2025-06-22 10:44:33 -04:00
target_link_libraries(
rocprofiler-systems-static-libstdcxx-optional
INTERFACE rocprofiler-systems-static-libstdcxx
)
2022-08-28 03:56:13 -05:00
endif()
2021-09-20 11:12:06 -05:00
# ----------------------------------------------------------------------------------------#
2021-08-06 13:08:57 -05:00
# user customization
#
get_property(LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
if(NOT APPLE OR "$ENV{CONDA_PYTHON_EXE}" STREQUAL "")
add_user_flags(rocprofiler-systems-compile-options "CXX")
2021-08-06 13:08:57 -05:00
endif()