From 8f9227e4be2df8f6cf3d7c62fd1b2c8904da53dc Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sun, 18 Dec 2022 21:48:24 +0100 Subject: [PATCH] cmake: use CMakePushCheckState to handle check states --- CMakeLists.txt | 94 ++++++++++++++++++++++--------------------- cmake/sdlchecks.cmake | 28 ++++++------- 2 files changed, 60 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4cedd727..5a6833fcc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -283,19 +283,14 @@ else() endif() endif() -# Emscripten toolchain has a nonempty default value for this, and the checks -# in this file need to change that, so remember the original value, and -# restore back to that afterwards. For check_function_exists() to work in -# Emscripten, this value must be at its default value. -set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) - if(CYGWIN) # We build SDL on cygwin without the UNIX emulation layer target_include_directories(sdl-build-options INTERFACE "/usr/include/mingw") + cmake_push_check_state() set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mno-cygwin") check_c_source_compiles("int main(int argc, char **argv) { return 0; }" HAVE_GCC_NO_CYGWIN) - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) + cmake_pop_check_state() if(HAVE_GCC_NO_CYGWIN) list(APPEND EXTRA_LDFLAGS_BUILD "-mno-cygwin") list(APPEND SDL_LIBS "-mno-cygwin") @@ -473,22 +468,18 @@ set(HAVE_STATIC_PIC "${SDL_STATIC_PIC}") if(SDL_WERROR) if(MSVC) - cmake_push_check_state() check_c_compiler_flag(/WX HAVE_WX) if(HAVE_WX) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX") endif() - cmake_pop_check_state() elseif(USE_GCC OR USE_CLANG) - cmake_push_check_state() check_c_compiler_flag(-Werror HAVE_WERROR) if(HAVE_WERROR) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") set(CMAKE_OBJC_FLAGS "${CMAKE_OBJC_FLAGS} -Werror") endif() - cmake_pop_check_state() endif() endif() @@ -616,21 +607,23 @@ if(USE_GCC OR USE_CLANG) endif() endif() - set(CMAKE_REQUIRED_FLAGS "-mpreferred-stack-boundary=2") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mpreferred-stack-boundary=2") check_c_source_compiles("int x = 0; int main(int argc, char **argv) { return 0; }" HAVE_GCC_PREFERRED_STACK_BOUNDARY) - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) + cmake_pop_check_state() - set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden -Werror") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fvisibility=hidden -Werror") check_c_source_compiles(" #if !defined(__GNUC__) || __GNUC__ < 4 #error SDL only uses visibility attributes in GCC 4 or newer #endif int main(int argc, char **argv) { return 0; }" HAVE_GCC_FVISIBILITY) + cmake_pop_check_state() if(HAVE_GCC_FVISIBILITY) list(APPEND EXTRA_CFLAGS "-fvisibility=hidden") endif() - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW) if(HAVE_GCC_WSHADOW) @@ -650,9 +643,10 @@ if(USE_GCC OR USE_CLANG) list(APPEND EXTRA_LDFLAGS_BUILD "-Wl,-compatibility_version,${SDL_DYLIB_COMPAT_VERSION}") list(APPEND EXTRA_LDFLAGS_BUILD "-Wl,-current_version,${SDL_DYLIB_CURRENT_VERSION}") elseif(NOT OPENBSD) - set(CMAKE_REQUIRED_FLAGS "-Wl,--no-undefined") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Wl,--no-undefined") check_c_compiler_flag("" HAVE_NO_UNDEFINED) - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) + cmake_pop_check_state() if(HAVE_NO_UNDEFINED AND NOT (USE_CLANG AND WINDOWS)) list(APPEND EXTRA_LDFLAGS_BUILD "-Wl,--no-undefined") endif() @@ -702,7 +696,8 @@ if(SDL_ASSEMBLY) # reworked for better compiler support set(HAVE_ASSEMBLY TRUE) if(SDL_MMX) - set(CMAKE_REQUIRED_FLAGS "-mmmx") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mmmx") check_c_source_compiles(" #ifdef __MINGW32__ #include <_mingw.h> @@ -718,14 +713,15 @@ if(SDL_ASSEMBLY) #error Assembler CPP flag not enabled #endif int main(int argc, char **argv) { return 0; }" HAVE_MMX) + cmake_pop_check_state() if(HAVE_MMX) list(APPEND EXTRA_CFLAGS "-mmmx") endif() - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) endif() if(SDL_3DNOW) - set(CMAKE_REQUIRED_FLAGS "-m3dnow") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -m3dnow") check_c_source_compiles(" #include #ifndef __3dNOW__ @@ -736,15 +732,16 @@ if(SDL_ASSEMBLY) _m_prefetch(p); return 0; }" CPU_SUPPORTS_3DNOW) + cmake_pop_check_state() if(CPU_SUPPORTS_3DNOW) set(HAVE_3DNOW TRUE) list(APPEND EXTRA_CFLAGS "-m3dnow") endif() - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) endif() if(SDL_SSE) - set(CMAKE_REQUIRED_FLAGS "-msse") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -msse") check_c_source_compiles(" #ifdef __MINGW32__ #include <_mingw.h> @@ -760,15 +757,16 @@ if(SDL_ASSEMBLY) #error Assembler CPP flag not enabled #endif int main(int argc, char **argv) { return 0; }" CPU_SUPPORTS_SSE) + cmake_pop_check_state() if(CPU_SUPPORTS_SSE) set(HAVE_SSE ON) list(APPEND EXTRA_CFLAGS "-msse") endif() - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) endif() if(SDL_SSE2) - set(CMAKE_REQUIRED_FLAGS "-msse2") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -msse2") check_c_source_compiles(" #ifdef __MINGW32__ #include <_mingw.h> @@ -784,15 +782,16 @@ if(SDL_ASSEMBLY) #error Assembler CPP flag not enabled #endif int main(int argc, char **argv) { return 0; }" CPU_SUPPORTS_SSE2) + cmake_pop_check_state() if(CPU_SUPPORTS_SSE2) set(HAVE_SSE2 TRUE) list(APPEND EXTRA_CFLAGS "-msse2") endif() - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) endif() if(SDL_SSE3) - set(CMAKE_REQUIRED_FLAGS "-msse3") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -msse3") check_c_source_compiles(" #ifdef __MINGW32__ #include <_mingw.h> @@ -808,11 +807,11 @@ if(SDL_ASSEMBLY) #error Assembler CPP flag not enabled #endif int main(int argc, char **argv) { return 0; }" CPU_SUPPORTS_SSE3) + cmake_pop_check_state() if(CPU_SUPPORTS_SSE3) set(HAVE_SSE3 TRUE) list(APPEND EXTRA_CFLAGS "-msse3") endif() - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) endif() if(NOT SDL_SSEMATH) @@ -831,7 +830,8 @@ if(SDL_ASSEMBLY) check_include_file("immintrin.h" HAVE_IMMINTRIN_H) if(SDL_ALTIVEC) - set(CMAKE_REQUIRED_FLAGS "-maltivec") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -maltivec") check_c_source_compiles(" #include vector unsigned int vzero() { @@ -843,7 +843,7 @@ if(SDL_ASSEMBLY) return vec_splat_u32(0); } int main(int argc, char **argv) { return 0; }" CPU_SUPPORTS_ALTIVEC) - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) + cmake_pop_check_state() if(CPU_SUPPORTS_ALTIVEC OR HAVE_ALTIVEC_H_HDR) set(HAVE_ALTIVEC TRUE) # if only HAVE_ALTIVEC_H_HDR is set list(APPEND EXTRA_CFLAGS "-maltivec") @@ -856,7 +856,7 @@ if(SDL_ASSEMBLY) if(SDL_LSX) cmake_push_check_state() - list(APPEND CMAKE_REQUIRED_FLAGS "-mlsx") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mlsx") check_c_source_compiles([[ #ifndef __loongarch_sx #error Assembler CPP flag not enabled @@ -873,7 +873,7 @@ if(SDL_ASSEMBLY) if(SDL_LASX) cmake_push_check_state() - list(APPEND CMAKE_REQUIRED_FLAGS "-mlasx") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mlasx") check_c_source_compiles([[ #ifndef __loongarch_asx #error Assembler CPP flag not enabled @@ -888,7 +888,7 @@ if(SDL_ASSEMBLY) endif() if(SDL_ARMSIMD) - set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + cmake_push_check_state() set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -x assembler-with-cpp") check_c_source_compiles(" .text @@ -902,7 +902,7 @@ if(SDL_ASSEMBLY) pld [r0] uqadd8 r0, r0, r0 " ARMSIMD_FOUND) - set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}") + cmake_pop_check_state() if(ARMSIMD_FOUND) set(HAVE_ARMSIMD TRUE) @@ -1057,7 +1057,7 @@ if(SDL_LIBC) check_library_exists(m pow "" HAVE_LIBM) if(HAVE_LIBM) - set(CMAKE_REQUIRED_LIBRARIES m) + cmake_push_check_state() foreach(_FN atan atan2 atanf atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf log logf log10 log10f @@ -1068,7 +1068,7 @@ if(SDL_LIBC) check_symbol_exists("${_FN}" "math.h" ${LIBC_HASVAR}) set(HAVE_${_UPPER} ${${LIBC_HASVAR}}) endforeach() - set(CMAKE_REQUIRED_LIBRARIES) + cmake_pop_check_state() if(NOT VITA) list(APPEND EXTRA_LIBS m) endif() @@ -1206,12 +1206,12 @@ if(ANDROID) # see: https://issuetracker.google.com/issues/62264618 # Another option would be to set ARM mode to all compiled files cmake_push_check_state() - set(CMAKE_REQUIRED_FLAGS "-Werror=unused-command-line-argument") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unused-command-line-argument") check_c_compiler_flag(-marm HAVE_ARM_MODE) + cmake_pop_check_state() if(HAVE_ARM_MODE) set_property(SOURCE "${SDL3_SOURCE_DIR}/src/atomic/SDL_spinlock.c" APPEND_STRING PROPERTY COMPILE_FLAGS " -marm") endif() - cmake_pop_check_state() if(SDL_AUDIO) set(SDL_AUDIO_DRIVER_ANDROID 1) @@ -1662,9 +1662,10 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU) set(SDL_RLD_FLAGS "") if(SDL_RPATH AND SDL_SHARED) if(BSDI OR FREEBSD OR LINUX OR NETBSD) - set(CMAKE_REQUIRED_FLAGS "-Wl,--enable-new-dtags") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Wl,--enable-new-dtags") check_c_compiler_flag("" HAVE_ENABLE_NEW_DTAGS) - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) + cmake_pop_check_state() if(HAVE_ENABLE_NEW_DTAGS) set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir} -Wl,--enable-new-dtags") else() @@ -1711,6 +1712,7 @@ elseif(WINDOWS) # Check for DirectX if(SDL_DIRECTX) + cmake_push_check_state() if(DEFINED MSVC_VERSION AND NOT ${MSVC_VERSION} LESS 1700) set(USE_WINSDK_DIRECTX TRUE) endif() @@ -1718,7 +1720,7 @@ elseif(WINDOWS) if("$ENV{DXSDK_DIR}" STREQUAL "") message_error("DIRECTX requires the \$DXSDK_DIR environment variable to be set") endif() - set(CMAKE_REQUIRED_FLAGS "/I\"$ENV{DXSDK_DIR}\\Include\"") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} /I\"$ENV{DXSDK_DIR}\\Include\"") endif() check_include_file(d3d9.h HAVE_D3D_H) @@ -1738,6 +1740,7 @@ elseif(WINDOWS) set(HAVE_DINPUT_H 0) endif() check_include_file(dxgi.h HAVE_DXGI_H) + cmake_pop_check_state() if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_D3D12_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H) set(HAVE_DIRECTX TRUE) if(NOT MINGW AND NOT USE_WINSDK_DIRECTX) @@ -1746,7 +1749,6 @@ elseif(WINDOWS) target_include_directories(sdl-build-options INTERFACE "$ENV{DXSDK_DIR}\\Include") endif() endif() - set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) endif() if(SDL_XINPUT) @@ -2380,12 +2382,12 @@ elseif(RISCOS) elseif(VITA) # SDL_spinlock.c Needs to be compiled in ARM mode. cmake_push_check_state() - set(CMAKE_REQUIRED_FLAGS "-Werror=unused-command-line-argument") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unused-command-line-argument") check_c_compiler_flag(-marm HAVE_ARM_MODE) + cmake_pop_check_state() if(HAVE_ARM_MODE) set_property(SOURCE "${SDL3_SOURCE_DIR}/src/atomic/SDL_spinlock.c" APPEND_STRING PROPERTY COMPILE_FLAGS " -marm") endif() - cmake_pop_check_state() if(SDL_MISC) file(GLOB MISC_SOURCES ${SDL3_SOURCE_DIR}/src/misc/vita/*.c) @@ -2971,11 +2973,11 @@ macro(asan_check_add_debug_flag2 ASAN_FLAG) set(FLAG "-fsanitize=${ASAN_FLAG}") - set (STORED_REQLIBS ${CMAKE_REQUIRED_LIBRARIES}) - set (CMAKE_REQUIRED_LIBRARIES "${FLAG};asan") + cmake_push_check_state() + list(APPEND CMAKE_REQUIRED_LIBRARIES ${FLAG} asan) check_c_compiler_flag (${FLAG} HAS_C_FLAG_${ASAN_FLAG}) check_cxx_compiler_flag (${FLAG} HAS_CXX_FLAG_${ASAN_FLAG}) - set (CMAKE_REQUIRED_LIBRARIES ${STORED_REQLIBS}) + cmake_pop_check_state() if (HAS_C_FLAG_${ASAN_FLAG}) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${FLAG}") diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake index 411dd47f8..39e788687 100644 --- a/cmake/sdlchecks.cmake +++ b/cmake/sdlchecks.cmake @@ -42,11 +42,12 @@ macro(FindLibraryAndSONAME _LIB) endmacro() macro(CheckDLOPEN) - cmake_push_check_state() check_symbol_exists(dlopen "dlfcn.h" HAVE_DLOPEN_IN_LIBC) if(NOT HAVE_DLOPEN_IN_LIBC) - set(CMAKE_REQUIRED_LIBRARIES dl) + cmake_push_check_state() + list(APPEND CMAKE_REQUIRED_LIBRARIES dl) check_symbol_exists(dlopen "dlfcn.h" HAVE_DLOPEN_IN_LIBDL) + cmake_pop_check_state() if(HAVE_DLOPEN_IN_LIBDL) list(APPEND EXTRA_LIBS dl) endif() @@ -54,7 +55,6 @@ macro(CheckDLOPEN) if(HAVE_DLOPEN_IN_LIBC OR HAVE_DLOPEN_IN_LIBDL) set(HAVE_DLOPEN TRUE) endif() - cmake_pop_check_state() endmacro() macro(CheckO_CLOEXEC) @@ -385,7 +385,7 @@ macro(CheckX11) list(APPEND EXTRA_LIBS ${X11_LIB} ${XEXT_LIB}) endif() - set(CMAKE_REQUIRED_LIBRARIES ${X11_LIB} ${X11_LIB}) + list(APPEND CMAKE_REQUIRED_LIBRARIES ${X11_LIB}) check_c_source_compiles(" #include @@ -487,8 +487,6 @@ macro(CheckX11) set(SDL_VIDEO_DRIVER_X11_XSHAPE 1) set(HAVE_X11_XSHAPE TRUE) endif() - - set(CMAKE_REQUIRED_LIBRARIES) endif() endif() if(NOT HAVE_X11) @@ -749,6 +747,7 @@ endmacro() # PTHREAD_CFLAGS # PTHREAD_LIBS macro(CheckPTHREAD) + cmake_push_check_state() if(SDL_THREADS AND SDL_PTHREADS) if(ANDROID) # the android libc provides built-in support for pthreads, so no @@ -797,7 +796,6 @@ macro(CheckPTHREAD) endif() # Run some tests - set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${PTHREAD_CFLAGS} ${PTHREAD_LDFLAGS}") check_c_source_compiles(" #include @@ -881,8 +879,8 @@ macro(CheckPTHREAD) endif() set(HAVE_SDL_THREADS TRUE) endif() - set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}") endif() + cmake_pop_check_state() endmacro() # Requires @@ -892,6 +890,7 @@ endmacro() # USB_LIBS # USB_CFLAGS macro(CheckUSBHID) + cmake_push_check_state() check_library_exists(usbhid hid_init "" LIBUSBHID) if(LIBUSBHID) check_include_file(usbhid.h HAVE_USBHID_H) @@ -919,9 +918,8 @@ macro(CheckUSBHID) endif() endif() - set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${USB_CFLAGS}") - set(CMAKE_REQUIRED_LIBRARIES "${USB_LIBS}") + list(APPEND CMAKE_REQUIRED_LIBRARIES ${USB_LIBS}) check_c_source_compiles(" #include #if defined(HAVE_USB_H) @@ -1020,9 +1018,8 @@ macro(CheckUSBHID) list(APPEND EXTRA_LIBS ${USB_LIBS}) set(HAVE_SDL_JOYSTICK TRUE) - set(CMAKE_REQUIRED_LIBRARIES) - set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}") endif() + cmake_pop_check_state() endmacro() # Check for HIDAPI support @@ -1093,9 +1090,9 @@ macro(CheckRPI) listtostr(VIDEO_RPI_INCLUDE_DIRS VIDEO_RPI_INCLUDE_FLAGS "-I") listtostr(VIDEO_RPI_LIBRARY_DIRS VIDEO_RPI_LIBRARY_FLAGS "-L") - set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + cmake_push_check_state() set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${VIDEO_RPI_INCLUDE_FLAGS} ${VIDEO_RPI_LIBRARY_FLAGS}") - set(CMAKE_REQUIRED_LIBRARIES "${VIDEO_RPI_LIBRARIES}") + list(APPEND CMAKE_REQUIRED_LIBRARIES ${VIDEO_RPI_LIBRARIES}) check_c_source_compiles(" #include #include @@ -1103,8 +1100,7 @@ macro(CheckRPI) EGL_DISPMANX_WINDOW_T window; bcm_host_init(); }" HAVE_RPI) - set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}") - set(CMAKE_REQUIRED_LIBRARIES) + cmake_pop_check_state() if(SDL_VIDEO AND HAVE_RPI) set(HAVE_SDL_VIDEO TRUE)