Skip to content

Commit

Permalink
Use SDL_DISABLE_ALLOCA instead of HAVE_ALLOCA in SDL_stdinc.h
Browse files Browse the repository at this point in the history
  • Loading branch information
madebr committed Oct 28, 2023
1 parent 552bee4 commit 6127ac0
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 44 deletions.
27 changes: 24 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,18 @@ sdl_glob_sources(
"${SDL3_SOURCE_DIR}/src/video/*.c"
"${SDL3_SOURCE_DIR}/src/video/yuv2rgb/*.c"
)
if(MSVC AND TARGET SDL3-shared)
if(SDL_CPU_X64)
enable_language(ASM_MASM)
set(asm_src "${SDL3_SOURCE_DIR}/src/stdlib/SDL_mslibc_x64.asm")
target_compile_options(SDL3-shared PRIVATE "$<$<COMPILE_LANGUAGE:ASM_MASM>:/nologo>")
set_property(SOURCE "${asm_src}" PROPERTY LANGUAGE "ASM_MASM")
target_sources(SDL3-shared PRIVATE "${asm_src}")
elseif(SDL_CPU_ARM32 OR DL_CPU_ARM64)
# FIXME: ARM assembler (armasm.exe/armasm64.exe) is NOT ASM_MASM, and does currently not work with CMake
# (https://gitlab.kitware.com/cmake/cmake/-/issues/18912)
endif()
endif()

if(USE_INTELCC)
# warning #39: division by zero
Expand Down Expand Up @@ -985,12 +997,21 @@ if(NOT HAVE_ARMNEON)
set(SDL_DISABLE_NEON 1)
endif()

set(SDL_DISABLE_ALLOCA 0)
check_include_file("alloca.h" "HAVE_ALLOCA_H")
if(MSVC)
check_include_file("malloc.h" "HAVE_MALLOC")
check_symbol_exists("_alloca" "malloc.h" _ALLOCA_IN_MALLOC_H)
if(NOT HAVE_ALLOCA_H AND NOT _ALLOCA_IN_MALLOC_H)
set(SDL_DISABLE_ALLOCA 1)
endif()
endif()

# TODO: Can't deactivate on FreeBSD? w/o LIBC, SDL_stdinc.h can't define anything.
if(SDL_LIBC)
set(available_headers)
set(HAVE_LIBC TRUE)
set(headers_to_check
alloca.h
ctype.h
float.h
iconv.h
Expand Down Expand Up @@ -1022,7 +1043,7 @@ if(SDL_LIBC)
endforeach()

set(symbols_to_check
abs acos acosf alloca asin asinf atan atan2 atan2f atanf atof atoi
abs acos acosf asin asinf atan atan2 atan2f atanf atof atoi
bcopy bsearch
calloc ceil ceilf copysign copysignf cos cosf
_Exit exp expf
Expand Down Expand Up @@ -1717,7 +1738,7 @@ elseif(WINDOWS)

if(TARGET SDL3-shared AND MSVC AND NOT SDL_LIBC)
# Prevent codegen that would use the VC runtime libraries.
target_compile_options(SDL3-shared PRIVATE "/GS-" "/Gs1048576")
target_compile_options(SDL3-shared PRIVATE $<$<COMPILE_LANGUAGE:C,CXX>:/GS-> $<$<COMPILE_LANGUAGE:C,CXX>:/Gs1048576>)
if(SDL_CPU_X86)
target_compile_options(SDL3-shared PRIVATE "/arch:SSE")
endif()
Expand Down
5 changes: 5 additions & 0 deletions VisualC-GDK/SDL/SDL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Gaming.Desktop.x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
Expand Down Expand Up @@ -729,6 +730,9 @@
<ClCompile Include="..\..\src\stdlib\SDL_iconv.c" />
<ClCompile Include="..\..\src\stdlib\SDL_malloc.c" />
<ClCompile Include="..\..\src\stdlib\SDL_mslibc.c" />
<MASM Condition="'$(Platform)'=='x64'" Include="..\..\src\stdlib\SDL_mslibc_x64.asm" >
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</MASM>
<ClCompile Include="..\..\src\stdlib\SDL_qsort.c" />
<ClCompile Include="..\..\src\stdlib\SDL_stdlib.c" />
<ClCompile Include="..\..\src\stdlib\SDL_string.c" />
Expand Down Expand Up @@ -793,5 +797,6 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
</ImportGroup>
</Project>
3 changes: 3 additions & 0 deletions VisualC-GDK/SDL/SDL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,9 @@
<ClCompile Include="..\..\src\stdlib\SDL_mslibc.c">
<Filter>stdlib</Filter>
</ClCompile>
<MASM Include="..\..\src\stdlib\SDL_mslibc_x64.asm">
<Filter>stdlib</Filter>
</MASM>
<ClCompile Include="..\..\src\core\gdk\SDL_gdk.cpp">
<Filter>core\gdk</Filter>
</ClCompile>
Expand Down
5 changes: 5 additions & 0 deletions VisualC-WinRT/SDL-UWP.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@
<ClCompile Include="..\src\stdlib\SDL_iconv.c" />
<ClCompile Include="..\src\stdlib\SDL_malloc.c" />
<ClCompile Include="..\src\stdlib\SDL_mslibc.c" />
<MASM Condition="'$(Platform)'=='x64'" Include="..\src\stdlib\SDL_mslibc_x64.asm" >
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</MASM>
<ClCompile Include="..\src\stdlib\SDL_qsort.c" />
<ClCompile Include="..\src\stdlib\SDL_stdlib.c" />
<ClCompile Include="..\src\stdlib\SDL_string.c" />
Expand Down Expand Up @@ -668,6 +671,7 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.props" />
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
Expand Down Expand Up @@ -867,5 +871,6 @@
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
</ImportGroup>
</Project>
3 changes: 3 additions & 0 deletions VisualC-WinRT/SDL-UWP.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,9 @@
<ClCompile Include="..\src\stdlib\SDL_mslibc.c">
<Filter>Source Files</Filter>
</ClCompile>
<MASM Include="..\src\stdlib\SDL_mslibc_x64.asm">
<Filter>stdlib</Filter>
</MASM>
<ClCompile Include="..\src\core\windows\pch.c">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
5 changes: 5 additions & 0 deletions VisualC/SDL/SDL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
Expand Down Expand Up @@ -609,6 +610,9 @@
<ClCompile Include="..\..\src\stdlib\SDL_iconv.c" />
<ClCompile Include="..\..\src\stdlib\SDL_malloc.c" />
<ClCompile Include="..\..\src\stdlib\SDL_mslibc.c" />
<MASM Condition="'$(Platform)'=='x64'" Include="..\..\src\stdlib\SDL_mslibc_x64.asm" >
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</MASM>
<ClCompile Include="..\..\src\stdlib\SDL_qsort.c" />
<ClCompile Include="..\..\src\stdlib\SDL_stdlib.c" />
<ClCompile Include="..\..\src\stdlib\SDL_string.c" />
Expand Down Expand Up @@ -669,5 +673,6 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
</ImportGroup>
</Project>
3 changes: 3 additions & 0 deletions VisualC/SDL/SDL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,9 @@
<ClCompile Include="..\..\src\stdlib\SDL_mslibc.c">
<Filter>stdlib</Filter>
</ClCompile>
<MASM Include="..\..\src\stdlib\SDL_mslibc_x64.asm">
<Filter>stdlib</Filter>
</MASM>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\..\src\core\windows\version.rc" />
Expand Down
26 changes: 13 additions & 13 deletions cmake/sdlcompilers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,54 +49,54 @@ function(SDL_AddCommonCompilerFlags TARGET)

check_c_compiler_flag(-Wundef HAVE_GCC_WUNDEF)
if(HAVE_GCC_WUNDEF)
target_compile_options(${TARGET} PRIVATE "-Wundef")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wundef>")
endif()

check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING)
if(HAVE_GCC_NO_STRICT_ALIASING)
target_compile_options(${TARGET} PRIVATE "-fno-strict-aliasing")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fno-strict-aliasing>")
endif()

check_c_compiler_flag(-Wdocumentation HAVE_GCC_WDOCUMENTATION)
if(HAVE_GCC_WDOCUMENTATION)
if(SDL_WERROR)
check_c_compiler_flag(-Werror=documentation HAVE_GCC_WERROR_DOCUMENTATION)
if(HAVE_GCC_WERROR_DOCUMENTATION)
target_compile_options(${TARGET} PRIVATE "-Werror=documentation")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Werror=documentation>")
endif()
endif()
target_compile_options(${TARGET} PRIVATE "-Wdocumentation")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wdocumentation>")
endif()

check_c_compiler_flag(-Wdocumentation-unknown-command HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
if(HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
if(SDL_WERROR)
check_c_compiler_flag(-Werror=documentation-unknown-command HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
if(HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
target_compile_options(${TARGET} PRIVATE "-Werror=documentation-unknown-command")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Werror=documentation-unknown-command>")
endif()
endif()
target_compile_options(${TARGET} PRIVATE "-Wdocumentation-unknown-command")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wdocumentation-unknown-command>")
endif()

check_c_compiler_flag(-fcomment-block-commands=threadsafety HAVE_GCC_COMMENT_BLOCK_COMMANDS)
if(HAVE_GCC_COMMENT_BLOCK_COMMANDS)
target_compile_options(${TARGET} PRIVATE "-fcomment-block-commands=threadsafety")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fcomment-block-commands=threadsafety>")
else()
check_c_compiler_flag(/clang:-fcomment-block-commands=threadsafety HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
if(HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
target_compile_options(${TARGET} PRIVATE "/clang:-fcomment-block-commands=threadsafety")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:/clang:-fcomment-block-commands=threadsafety>")
endif()
endif()

check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
if(HAVE_GCC_WSHADOW)
target_compile_options(${TARGET} PRIVATE "-Wshadow")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wshadow>")
endif()

check_c_compiler_flag(-Wunused-local-typedefs HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
if(HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
target_compile_options(${TARGET} PRIVATE "-Wno-unused-local-typedefs")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wno-unused-local-typedefs>")
endif()
endif()

Expand All @@ -109,20 +109,20 @@ function(SDL_AddCommonCompilerFlags TARGET)
elseif(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QNX)
check_c_compiler_flag(-Werror HAVE_WERROR)
if(HAVE_WERROR)
target_compile_options(${TARGET} PRIVATE "-Werror")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Werror>")
endif()
endif()
endif()

if(USE_CLANG)
check_c_compiler_flag("-fcolor-diagnostics" COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
if(COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
target_compile_options(${TARGET} PRIVATE "-fcolor-diagnostics")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fcolor-diagnostics>")
endif()
else()
check_c_compiler_flag("-fdiagnostics-color=always" COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
if(COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
target_compile_options(${TARGET} PRIVATE "-fdiagnostics-color=always")
target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fdiagnostics-color=always>")
endif()
endif()
endfunction()
40 changes: 21 additions & 19 deletions include/SDL3/SDL_stdinc.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,28 @@
#include <stdint.h>
#include <wchar.h>

#ifndef alloca
# ifdef HAVE_ALLOCA_H
# include <alloca.h>
# elif defined(__GNUC__)
# define alloca __builtin_alloca
# elif defined(_MSC_VER)
# include <malloc.h>
# define alloca _alloca
# elif defined(__WATCOMC__)
# include <malloc.h>
# elif defined(__BORLANDC__)
# include <malloc.h>
# elif defined(__DMC__)
# include <stdlib.h>
# elif defined(__AIX__)
#pragma alloca
# elif defined(__MRC__)
#ifndef SDL_DISABLE_ALLOCA
# ifndef alloca
# ifdef HAVE_ALLOCA_H
# include <alloca.h>
# elif defined(__GNUC__)
# define alloca __builtin_alloca
# elif defined(_MSC_VER)
# include <malloc.h>
# define alloca _alloca
# elif defined(__WATCOMC__)
# include <malloc.h>
# elif defined(__BORLANDC__)
# include <malloc.h>
# elif defined(__DMC__)
# include <stdlib.h>
# elif defined(__AIX__)
# pragma alloca
# elif defined(__MRC__)
void *alloca(unsigned);
# else
# else
char *alloca();
# endif
# endif
#endif

Expand Down Expand Up @@ -379,7 +381,7 @@ SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
extern "C" {
#endif

#ifdef HAVE_ALLOCA
#ifndef SDL_DISABLE_ALLOCA
#define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
#define SDL_stack_free(data)
#else
Expand Down
3 changes: 2 additions & 1 deletion include/build_config/SDL_build_config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#cmakedefine HAVE_GCC_ATOMICS @HAVE_GCC_ATOMICS@
#cmakedefine HAVE_GCC_SYNC_LOCK_TEST_AND_SET @HAVE_GCC_SYNC_LOCK_TEST_AND_SET@

#cmakedefine SDL_DISABLE_ALLOCA

/* Comment this if you want to build without any C library requirements */
#cmakedefine HAVE_LIBC 1
#ifdef HAVE_LIBC
Expand Down Expand Up @@ -74,7 +76,6 @@
#cmakedefine HAVE_CALLOC 1
#cmakedefine HAVE_REALLOC 1
#cmakedefine HAVE_FREE 1
#cmakedefine HAVE_ALLOCA 1
#ifndef __WIN32__ /* Don't use C runtime versions of these on Windows */
#cmakedefine HAVE_GETENV 1
#cmakedefine HAVE_SETENV 1
Expand Down
1 change: 0 additions & 1 deletion include/build_config/SDL_build_config_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#define HAVE_CALLOC 1
#define HAVE_REALLOC 1
#define HAVE_FREE 1
#define HAVE_ALLOCA 1
#define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1
Expand Down
1 change: 0 additions & 1 deletion include/build_config/SDL_build_config_emscripten.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#define HAVE_CALLOC 1
#define HAVE_REALLOC 1
#define HAVE_FREE 1
#define HAVE_ALLOCA 1
#define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1
Expand Down
1 change: 0 additions & 1 deletion include/build_config/SDL_build_config_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
#define HAVE_CALLOC 1
#define HAVE_REALLOC 1
#define HAVE_FREE 1
#define HAVE_ALLOCA 1
#define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1
Expand Down
1 change: 0 additions & 1 deletion include/build_config/SDL_build_config_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#define HAVE_CALLOC 1
#define HAVE_REALLOC 1
#define HAVE_FREE 1
#define HAVE_ALLOCA 1
#define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1
Expand Down
1 change: 0 additions & 1 deletion include/build_config/SDL_build_config_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ typedef unsigned int uintptr_t;
#define HAVE_CALLOC 1
#define HAVE_REALLOC 1
#define HAVE_FREE 1
#define HAVE_ALLOCA 1
#define HAVE_QSORT 1
#define HAVE_BSEARCH 1
#define HAVE_ABS 1
Expand Down
1 change: 0 additions & 1 deletion include/build_config/SDL_build_config_wingdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
#define HAVE_CALLOC 1
#define HAVE_REALLOC 1
#define HAVE_FREE 1
#define HAVE_ALLOCA 1
#define HAVE_QSORT 1
#define HAVE_BSEARCH 1
#define HAVE_ABS 1
Expand Down
1 change: 0 additions & 1 deletion include/build_config/SDL_build_config_winrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
#define HAVE_CALLOC 1
#define HAVE_REALLOC 1
#define HAVE_FREE 1
#define HAVE_ALLOCA 1
#define HAVE_QSORT 1
#define HAVE_BSEARCH 1
#define HAVE_ABS 1
Expand Down
1 change: 0 additions & 1 deletion include/build_config/SDL_build_config_xbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
#define HAVE_CALLOC 1
#define HAVE_REALLOC 1
#define HAVE_FREE 1
#define HAVE_ALLOCA 1
#define HAVE_QSORT 1
#define HAVE_BSEARCH 1
#define HAVE_ABS 1
Expand Down
Loading

0 comments on commit 6127ac0

Please sign in to comment.