mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-04-02 22:06:10 +00:00
124 lines
3.9 KiB
CMake
124 lines
3.9 KiB
CMake
|
|
cmake_minimum_required(VERSION 3.1...4.0)
|
|
project(moonlight-common-c LANGUAGES C)
|
|
|
|
string(TOUPPER "x${CMAKE_BUILD_TYPE}" BUILD_TYPE)
|
|
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
|
|
|
|
option(USE_MBEDTLS "Use MbedTLS instead of OpenSSL" OFF)
|
|
option(CODE_ANALYSIS "Run code analysis during compilation" OFF)
|
|
|
|
SET(CMAKE_C_STANDARD 11)
|
|
|
|
set(CMAKE_POSITION_INDEPENDENT_CODE_BACKUP ${CMAKE_POSITION_INDEPENDENT_CODE})
|
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
|
add_subdirectory(enet)
|
|
set(CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE_BACKUP})
|
|
unset(CMAKE_POSITION_INDEPENDENT_CODE_BACKUP)
|
|
|
|
aux_source_directory(src SRC_LIST)
|
|
|
|
# Build shared library by default, but allows user override
|
|
if (NOT DEFINED BUILD_SHARED_LIBS)
|
|
set(BUILD_SHARED_LIBS ON)
|
|
set(BUILD_SHARED_LIBS_OVERRIDE ON)
|
|
endif()
|
|
|
|
add_library(moonlight-common-c ${SRC_LIST})
|
|
|
|
if (BUILD_SHARED_LIBS_OVERRIDE)
|
|
unset(BUILD_SHARED_LIBS)
|
|
unset(BUILD_SHARED_LIBS_OVERRIDE)
|
|
endif()
|
|
|
|
target_link_libraries(moonlight-common-c PRIVATE enet)
|
|
|
|
if(MSVC)
|
|
target_compile_options(moonlight-common-c PRIVATE /W3 /wd4100 /wd4232 /wd5105 /WX)
|
|
target_link_libraries(moonlight-common-c PRIVATE ws2_32.lib winmm.lib)
|
|
elseif(MINGW)
|
|
target_link_libraries(moonlight-common-c PRIVATE -lws2_32 -lwinmm)
|
|
else()
|
|
target_compile_options(moonlight-common-c PRIVATE -Wall -Wextra -Wno-unused-parameter -Werror)
|
|
if (CODE_ANALYSIS AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
|
target_compile_options(moonlight-common-c PRIVATE -fanalyzer)
|
|
endif()
|
|
endif()
|
|
|
|
if (USE_MBEDTLS)
|
|
target_compile_definitions(moonlight-common-c PRIVATE USE_MBEDTLS)
|
|
find_package(MbedTLS QUIET)
|
|
if (MBEDTLS_FOUND)
|
|
target_link_libraries(moonlight-common-c PRIVATE ${MBEDCRYPTO_LIBRARY})
|
|
target_include_directories(moonlight-common-c SYSTEM PRIVATE ${MBEDTLS_INCLUDE_DIRS})
|
|
else()
|
|
# For sub project added via CMake
|
|
target_link_libraries(moonlight-common-c PRIVATE mbedcrypto)
|
|
endif()
|
|
else()
|
|
find_package(OpenSSL 1.0.2 REQUIRED)
|
|
target_link_libraries(moonlight-common-c PRIVATE ${OPENSSL_CRYPTO_LIBRARY})
|
|
target_include_directories(moonlight-common-c SYSTEM PRIVATE ${OPENSSL_INCLUDE_DIR})
|
|
endif()
|
|
|
|
if("${BUILD_TYPE}" STREQUAL "XDEBUG")
|
|
target_compile_definitions(moonlight-common-c PRIVATE LC_DEBUG)
|
|
else()
|
|
target_compile_definitions(moonlight-common-c PRIVATE NDEBUG)
|
|
|
|
# Avoid false "maybe uninitialized" warning generated by old GCC versions
|
|
# when building with -O2
|
|
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
|
target_compile_options(moonlight-common-c PRIVATE -Wno-maybe-uninitialized)
|
|
endif()
|
|
endif()
|
|
|
|
if (NOT(MSVC OR APPLE))
|
|
include(CheckLibraryExists)
|
|
CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_CLOCK_GETTIME)
|
|
|
|
if (NOT HAVE_CLOCK_GETTIME)
|
|
set(CMAKE_EXTRA_INCLUDE_FILES time.h)
|
|
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
|
|
SET(CMAKE_EXTRA_INCLUDE_FILES)
|
|
endif()
|
|
|
|
foreach(clock CLOCK_MONOTONIC CLOCK_MONOTONIC_RAW)
|
|
message(STATUS "Testing whether ${clock} can be used")
|
|
CHECK_CXX_SOURCE_COMPILES(
|
|
"#define _POSIX_C_SOURCE 200112L
|
|
#include <time.h>
|
|
int main ()
|
|
{
|
|
struct timespec ts[1];
|
|
clock_gettime (${clock}, ts);
|
|
return 0;
|
|
}" HAVE_${clock})
|
|
if(HAVE_${clock})
|
|
message(STATUS "Testing whether ${clock} can be used -- Success")
|
|
else()
|
|
message(STATUS "Testing whether ${clock} can be used -- Failed")
|
|
endif()
|
|
endforeach()
|
|
|
|
endif()
|
|
|
|
target_include_directories(moonlight-common-c SYSTEM PUBLIC src)
|
|
|
|
target_include_directories(moonlight-common-c PRIVATE
|
|
${CMAKE_CURRENT_SOURCE_DIR}/nanors
|
|
${CMAKE_CURRENT_SOURCE_DIR}/nanors/deps/obl
|
|
)
|
|
|
|
target_compile_definitions(moonlight-common-c PRIVATE HAS_SOCKLEN_T)
|
|
|
|
# nanors
|
|
if (MSVC)
|
|
set_source_files_properties("${CMAKE_SOURCE_DIR}/src/rswrapper.c"
|
|
DIRECTORY "${CMAKE_SOURCE_DIR}")
|
|
else()
|
|
set_source_files_properties("${CMAKE_SOURCE_DIR}/src/rswrapper.c"
|
|
DIRECTORY "${CMAKE_SOURCE_DIR}"
|
|
PROPERTIES COMPILE_FLAGS "-ftree-vectorize -funroll-loops")
|
|
endif()
|