From 7d26f8ed42748e23a21abaeff5c1ac518fa6d833 Mon Sep 17 00:00:00 2001 From: Ryan Foster Date: Thu, 6 Feb 2025 17:16:32 -0500 Subject: [PATCH 1/3] Modernize CMake * Update to C/C++17 * Use targets instead of global set and add calls * Assume WIN32 is always the target platform * Replace CMake 2.x with CMake 3.x compile language and ID checks * Remove unnecessary FindCXX11 module --- CMakeLists.txt | 145 +++++++++++++++++----------------- cmake/Modules/FindCXX11.cmake | 68 ---------------- 2 files changed, 71 insertions(+), 142 deletions(-) delete mode 100644 cmake/Modules/FindCXX11.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c88ff6..bb8f192 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,79 +1,29 @@ -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.28...3.30) project(libdshowcapture) -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/") - option(BUILD_SHARED_LIBS "Build shared library" ON) -find_package(CXX11 REQUIRED) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX11_FLAGS}") - -if(${CMAKE_C_COMPILER_ID} MATCHES "Clang" OR ${CMAKE_CXX_COMPILER_ID} MATCHES - "Clang") - set(CMAKE_COMPILER_IS_CLANG TRUE) -endif() - -if(CMAKE_COMPILER_IS_GNUCC - OR CMAKE_COMPILER_IS_GNUCXX - OR CMAKE_COMPILER_IS_CLANG) - set(CMAKE_CXX_FLAGS - "-Wall -Wextra -Wno-unused-function -Werror-implicit-function-declaration -Wno-missing-field-initializers ${CMAKE_CXX_FLAGS} -fno-strict-aliasing" - ) - set(CMAKE_C_FLAGS - "-Wall -Wextra -Wno-unused-function -Werror-implicit-function-declaration -Wno-missing-braces -Wno-missing-field-initializers ${CMAKE_C_FLAGS} -std=gnu99 -fno-strict-aliasing" - ) - - option(USE_LIBC++ "Use libc++ instead of libstdc++" ${APPLE}) - if(USE_LIBC++) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") - endif() -elseif(MSVC) - if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") - string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") - endif() - - # Disable pointless constant condition warnings - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4127 /wd4201") -endif() +# Set C and C++ language standards to C17 and C++17 +set(CMAKE_C_STANDARD 17) +set(CMAKE_C_STANDARD_REQUIRED TRUE) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED TRUE) -if(WIN32) - add_definitions(-DUNICODE -D_UNICODE) - if(BUILD_SHARED_LIBS) - add_definitions(-DDSHOWCAPTURE_EXPORTS) - endif() -endif() +# Disable C and C++ extensions +set(CMAKE_C_EXTENSIONS FALSE) +set(CMAKE_CXX_EXTENSIONS FALSE) -if(MSVC) - set(CMAKE_C_FLAGS_DEBUG "/DDEBUG=1 /D_DEBUG=1 ${CMAKE_C_FLAGS_DEBUG}") - set(CMAKE_CXX_FLAGS_DEBUG "/DDEBUG=1 /D_DEBUG=1 ${CMAKE_C_FLAGS_DEBUG}") +# Set symbols to be hidden by default for C and C++ +set(CMAKE_C_VISIBILITY_PRESET hidden) +set(CMAKE_CXX_VISIBILITY_PRESET hidden) +set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE) - if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO") - endif() -else() - if(MINGW) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_WIN32_WINNT=0x0600 -DWINVER=0x0600") - set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} -D_WIN32_WINNT=0x0600 -DWINVER=0x0600") - endif() - set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=1 -D_DEBUG=1 ${CMAKE_C_FLAGS_DEBUG}") - set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=1 -D_DEBUG=1 ${CMAKE_CXX_FLAGS_DEBUG}") -endif() +add_library(libdshowcapture) -if(MINGW) - include(CheckSymbolExists) - check_symbol_exists(MINGW_HAS_SECURE_API "_mingw.h" HAVE_MINGW_HAS_SECURE_API) - if(NOT HAVE_MINGW_HAS_SECURE_API) - message(FATAL_ERROR "mingw must be compiled with --enable-secure-api") - endif() -endif() - -set(libdshowcapture_SOURCES +target_sources( + libdshowcapture + PRIVATE external/capture-device-support/Library/EGAVResult.cpp external/capture-device-support/Library/ElgatoUVCDevice.cpp external/capture-device-support/Library/win/EGAVHIDImplementation.cpp @@ -91,9 +41,7 @@ set(libdshowcapture_SOURCES source/dshow-formats.cpp source/dshow-media-type.cpp source/dshow-encoded-device.cpp - source/log.cpp) - -set(libdshowcapture_HEADERS + source/log.cpp dshowcapture.hpp source/external/IVideoCaptureFilter.h source/capture-filter.hpp @@ -106,16 +54,65 @@ set(libdshowcapture_HEADERS source/dshow-enum.hpp source/dshow-formats.hpp source/dshow-media-type.hpp - source/log.hpp) + source/log.hpp +) -add_library(libdshowcapture ${libdshowcapture_SOURCES} - ${libdshowcapture_HEADERS}) +if(MSVC) + # Set /W4 + # Disable pointless constant condition warnings + target_compile_options(libdshowcapture PRIVATE /W4 /wd4127 /wd4201) + + if(CMAKE_VS_PLATFORM_NAME STREQUAL Win32) + target_link_options(libdshowcapture PRIVATE /SAFESEH:NO) + endif() +else() + if(MINGW) + include(CheckSymbolExists) + check_symbol_exists(MINGW_HAS_SECURE_API "_mingw.h" HAVE_MINGW_HAS_SECURE_API) + if(NOT HAVE_MINGW_HAS_SECURE_API) + message(FATAL_ERROR "mingw must be compiled with --enable-secure-api") + endif() + + target_compile_definitions(libdshowcapture PRIVATE _WIN32_WINNT=0x0600 WINVER=0x0600) + endif() + + set(_libdshowcapture_c_options + -fno-strict-aliasing + -Wall + -Wextra + -Werror-implicit-function-declaration + -Wno-missing-braces + -Wno-missing-field-initializers + -Wno-unused-function + ) + set(_libdshowcapture_cxx_options + -fno-strict-aliasing + -Wall + -Wextra + -Werror-implicit-function-declaration + -Wno-missing-field-initializers + -Wno-unused-function + ) + target_compile_options(libdshowcapture PRIVATE + "$<$:${_libdshowcapture_c_options}>" + "$<$:${_libdshowcapture_cxx_options}>" + ) +endif() target_include_directories( libdshowcapture PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external/capture-device-support/Library) -target_compile_definitions(libdshowcapture PRIVATE _UP_WINDOWS=1) +target_compile_definitions( + libdshowcapture + PRIVATE + UNICODE + _UNICODE + $<$:DEBUG> + $<$:_DEBUG> + $<$:DSHOWCAPTURE_EXPORTS> + _UP_WINDOWS=1 +) target_link_libraries(libdshowcapture PRIVATE setupapi strmiids ksuser winmm wmcodecdspuuid) diff --git a/cmake/Modules/FindCXX11.cmake b/cmake/Modules/FindCXX11.cmake deleted file mode 100644 index a627775..0000000 --- a/cmake/Modules/FindCXX11.cmake +++ /dev/null @@ -1,68 +0,0 @@ -# - Finds if the compiler has C++11 support -# This module can be used to detect compiler flags for using C++11, and checks -# a small subset of the language. -# -# The following variables are set: -# CXX11_FLAGS - flags to add to the CXX compiler for C++11 support -# CXX11_FOUND - true if the compiler supports C++11 -# - -if(CXX11_FLAGS) - set(CXX11_FOUND TRUE) - return() -endif() - -include(CheckCXXSourceCompiles) - -if(MSVC) - set(CXX11_FLAG_CANDIDATES - " " - ) -else() - set(CXX11_FLAG_CANDIDATES - #gcc - "-std=gnu++11" - "-std=gnu++0x" - #Gnu and Intel Linux - "-std=c++11" - "-std=c++0x" - #Microsoft Visual Studio, and everything that automatically accepts C++11 - " " - #Intel windows - "/Qstd=c++11" - "/Qstd=c++0x" - ) -endif() - -set(CXX11_TEST_SOURCE -" -int main() -{ - int n[] = {4,7,6,1,2}; - int r; - auto f = [&](int j) { r = j; }; - - for (auto i : n) - f(i); - return 0; -} -") - -foreach(FLAG ${CXX11_FLAG_CANDIDATES}) - set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") - set(CMAKE_REQUIRED_FLAGS "${FLAG}") - unset(CXX11_FLAG_DETECTED CACHE) - message(STATUS "Try C++11 flag = [${FLAG}]") - check_cxx_source_compiles("${CXX11_TEST_SOURCE}" CXX11_FLAG_DETECTED) - set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}") - if(CXX11_FLAG_DETECTED) - set(CXX11_FLAGS_INTERNAL "${FLAG}") - break() - endif(CXX11_FLAG_DETECTED) -endforeach(FLAG ${CXX11_FLAG_CANDIDATES}) - -set(CXX11_FLAGS "${CXX11_FLAGS_INTERNAL}" CACHE STRING "C++11 Flags") - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(CXX11 DEFAULT_MSG CXX11_FLAGS) -mark_as_advanced(CXX11_FLAGS) From 9a4017af1b44b7888ac45912e9cd3a46d67b1432 Mon Sep 17 00:00:00 2001 From: Ryan Foster Date: Thu, 6 Feb 2025 17:22:29 -0500 Subject: [PATCH 2/3] Remove support for non-MSVC --- CMakeLists.txt | 48 +++++++++--------------------------------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb8f192..6d77d57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.28...3.30) project(libdshowcapture) +if(NOT MSVC) + message(FATAL_ERROR "libdshowcapture only supports building with Visual Studio and MSVC.") +endif() + option(BUILD_SHARED_LIBS "Build shared library" ON) # Set C and C++ language standards to C17 and C++17 @@ -57,46 +61,12 @@ target_sources( source/log.hpp ) -if(MSVC) - # Set /W4 - # Disable pointless constant condition warnings - target_compile_options(libdshowcapture PRIVATE /W4 /wd4127 /wd4201) - - if(CMAKE_VS_PLATFORM_NAME STREQUAL Win32) - target_link_options(libdshowcapture PRIVATE /SAFESEH:NO) - endif() -else() - if(MINGW) - include(CheckSymbolExists) - check_symbol_exists(MINGW_HAS_SECURE_API "_mingw.h" HAVE_MINGW_HAS_SECURE_API) - if(NOT HAVE_MINGW_HAS_SECURE_API) - message(FATAL_ERROR "mingw must be compiled with --enable-secure-api") - endif() - - target_compile_definitions(libdshowcapture PRIVATE _WIN32_WINNT=0x0600 WINVER=0x0600) - endif() +# Set /W4 +# Disable pointless constant condition warnings +target_compile_options(libdshowcapture PRIVATE /W4 /wd4127 /wd4201) - set(_libdshowcapture_c_options - -fno-strict-aliasing - -Wall - -Wextra - -Werror-implicit-function-declaration - -Wno-missing-braces - -Wno-missing-field-initializers - -Wno-unused-function - ) - set(_libdshowcapture_cxx_options - -fno-strict-aliasing - -Wall - -Wextra - -Werror-implicit-function-declaration - -Wno-missing-field-initializers - -Wno-unused-function - ) - target_compile_options(libdshowcapture PRIVATE - "$<$:${_libdshowcapture_c_options}>" - "$<$:${_libdshowcapture_cxx_options}>" - ) +if(CMAKE_VS_PLATFORM_NAME STREQUAL Win32) + target_link_options(libdshowcapture PRIVATE /SAFESEH:NO) endif() target_include_directories( From f6665b19a8fcb71e91f25bfaa98e71caebcb3a1f Mon Sep 17 00:00:00 2001 From: Ryan Foster Date: Mon, 3 Mar 2025 19:40:15 -0500 Subject: [PATCH 3/3] Format CMake with gersemi --- CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d77d57..a60ab9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,9 +69,7 @@ if(CMAKE_VS_PLATFORM_NAME STREQUAL Win32) target_link_options(libdshowcapture PRIVATE /SAFESEH:NO) endif() -target_include_directories( - libdshowcapture - PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external/capture-device-support/Library) +target_include_directories(libdshowcapture PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external/capture-device-support/Library) target_compile_definitions( libdshowcapture @@ -84,5 +82,7 @@ target_compile_definitions( _UP_WINDOWS=1 ) -target_link_libraries(libdshowcapture PRIVATE setupapi strmiids ksuser winmm - wmcodecdspuuid) +target_link_libraries( + libdshowcapture + PRIVATE setupapi strmiids ksuser winmm wmcodecdspuuid +)