#
# Copyright (c) 2021-2022 Ivan Maidanski
##
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
##
cmake_minimum_required(VERSION 3.5)
set(PACKAGE_VERSION 7.8.2)
# Version must match that in AC_INIT of configure.ac and that in README.
# Version must conform to: [0-9]+[.][0-9]+[.][0-9]+
# Info (current:revision:age) for the Libtool versioning system.
# These values should match those in src/Makefile.am.
set(LIBATOMIC_OPS_VER_INFO 3:0:2)
set(LIBATOMIC_OPS_GPL_VER_INFO 3:1:2)
project(libatomic_ops C)
if (POLICY CMP0057)
# Required for CheckLinkerFlag, at least.
cmake_policy(SET CMP0057 NEW)
endif()
if (NOT (${CMAKE_VERSION} VERSION_LESS "3.18.0"))
include(CheckLinkerFlag)
endif()
# Customize the build by passing "-D<option_name>=ON|OFF" in the command line.
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(build_tests "Build tests" OFF)
option(enable_assertions "Enable assertion checking" OFF)
option(enable_werror "Treat warnings as errors" OFF)
option(enable_atomic_intrinsics "Use GCC atomic intrinsics" ON)
option(enable_docs "Build and install documentation" ON)
option(enable_gpl "Build atomic_ops_gpl library" ON)
option(install_headers "Install header and pkg-config metadata files" ON)
# Override the default build type to RelWithDebInfo (this instructs cmake to
# pass -O2 -g -DNDEBUG options to the compiler).
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE
STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
STRINGS "Debug" "Release" "RelWithDebInfo" "MinSizeRel")
endif()
if (CMAKE_USE_PTHREADS_INIT)
# Required define if using POSIX threads.
add_compile_options(-D_REENTRANT)
else()
# No pthreads library available.
add_compile_options(-DAO_NO_PTHREADS)
endif()
if (enable_assertions)
# In case NDEBUG macro is defined e.g. by cmake -DCMAKE_BUILD_TYPE=Release.
add_compile_options(-UNDEBUG)
else()
# Define to disable assertion checking.
add_compile_options(-DNDEBUG)
endif()
if (NOT enable_atomic_intrinsics)
# Define to avoid GCC atomic intrinsics even if available.
add_compile_options(-DAO_DISABLE_GCC_ATOMICS)
endif()
# AO API symbols export control.
if (BUILD_SHARED_LIBS)
add_compile_options(-DAO_DLL)
endif()
if (enable_werror)
if (MSVC)
add_compile_options(/WX)
else()
add_compile_options(-Werror)
endif()
endif(enable_werror)
# Extra user-defined flags to pass to the C compiler.
if (DEFINED CFLAGS_EXTRA)
separate_arguments(CFLAGS_EXTRA_LIST UNIX_COMMAND "${CFLAGS_EXTRA}")
add_compile_options(${CFLAGS_EXTRA_LIST})
endif()
set(SRC src/atomic_ops.c)
if (CMAKE_C_COMPILER_ID STREQUAL "SunPro")
# SunCC compiler on SunOS (Solaris).
set(SRC ${SRC} src/atomic_ops_sysdeps.S)
endif()
add_library(atomic_ops ${SRC})
target_link_libraries(atomic_ops PRIVATE ${THREADDLLIBS_LIST})
target_include_directories(atomic_ops
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
INTERFACE "$<INSTALL_INTERFACE:include>")
if (enable_gpl)
set(AO_GPL_SRC src/atomic_ops_malloc.c src/atomic_ops_stack.c)
add_library(atomic_ops_gpl ${AO_GPL_SRC})
check_function_exists(mmap HAVE_MMAP)
if (HAVE_MMAP)
target_compile_definitions(atomic_ops_gpl PRIVATE HAVE_MMAP)
endif()
target_link_libraries(atomic_ops_gpl PRIVATE atomic_ops)
target_include_directories(atomic_ops_gpl
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
INTERFACE "$<INSTALL_INTERFACE:include>")
if (BUILD_SHARED_LIBS)
set_property(TARGET atomic_ops_gpl PROPERTY VERSION ${AO_GPL_VERSION_PROP})
set_property(TARGET atomic_ops_gpl PROPERTY SOVERSION ${AO_GPL_SOVERSION})
endif()
install(TARGETS atomic_ops_gpl EXPORT Atomic_opsTargets
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
endif(enable_gpl)
if (BUILD_SHARED_LIBS)
if (NOT (${CMAKE_SYSTEM_NAME} MATCHES "BSD"))
if (${CMAKE_VERSION} VERSION_LESS "3.18.0")
set(WL_NO_UNDEFINED_OPT "-Wl,--no-undefined")
check_c_compiler_flag(${WL_NO_UNDEFINED_OPT} HAVE_FLAG_WL_NO_UNDEFINED)
else()
set(WL_NO_UNDEFINED_OPT "LINKER:--no-undefined")
check_linker_flag(C "${WL_NO_UNDEFINED_OPT}" HAVE_FLAG_WL_NO_UNDEFINED)
endif()
if (HAVE_FLAG_WL_NO_UNDEFINED)
# Declare that the libraries do not refer to external symbols.
if (${CMAKE_VERSION} VERSION_LESS "3.13.0")
target_link_libraries(atomic_ops PRIVATE ${WL_NO_UNDEFINED_OPT})
if (enable_gpl)
target_link_libraries(atomic_ops_gpl PRIVATE ${WL_NO_UNDEFINED_OPT})
endif(enable_gpl)
else()
target_link_options(atomic_ops PRIVATE ${WL_NO_UNDEFINED_OPT})
if (enable_gpl)
target_link_options(atomic_ops_gpl PRIVATE ${WL_NO_UNDEFINED_OPT})
endif(enable_gpl)
endif()
endif(HAVE_FLAG_WL_NO_UNDEFINED)
endif()
set_property(TARGET atomic_ops PROPERTY VERSION ${AO_VERSION_PROP})
set_property(TARGET atomic_ops PROPERTY SOVERSION ${AO_SOVERSION})
endif(BUILD_SHARED_LIBS)