Skip to content

Commit c819f70

Browse files
committed
support static link of SDL2 and pkg-config
Add SDL2_STATIC variable, when set to ON will link the static version of SDL2 and add any necessary libs and linker flags for linking it statically from pkg-config. All extra libs and linker flags are added to SDL2_LIBRARY, so the user does not need to do anything except set that flag. For dynamic linking, any extra stuff from pkg-config is also retrieved and added to SDL2_LIBRARY. The variable SDL2_DEFINITIONS is also retrieved from `pkg-config --cflags-only-other`. The user needs to call ADD_DEFINITIONS for it, the documentation is updated to reflect this. If pkg-config is not available, the module functions as before with no errors, but SDL2_STATIC will probably fail. TODO: * warn if pkg-config is not found * add similar code to the other two cmake modules, or factor it out * support sdl2-config as well (extra parsing)
1 parent 85be087 commit c819f70

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

FindSDL2.cmake

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
21
# This module defines
32
# SDL2_LIBRARY, the name of the library to link against
43
# SDL2_FOUND, if false, do not try to link to SDL2
54
# SDL2_INCLUDE_DIR, where to find SDL.h
65
#
6+
# If you have pkg-config, these extra variables are also defined:
7+
# SDL2_DEFINITIONS, extra CFLAGS
8+
# SDL2_EXTRA_LIBS, extra link libs
9+
# SDL2_LINKER_FLAGS, extra link flags
10+
#
11+
# The latter two are automatically added to SDL2_LIBRARY
12+
#
13+
# To use them, add code such as:
14+
#
15+
# # SET(SDL2_STATIC ON) # if you want to link SDL2 statically
16+
# FIND_PACKAGE(SDL2 REQUIRED)
17+
# ADD_DEFINITIONS(${SDL2_DEFINITIONS})
18+
# TARGET_LINK_LIBRARIES(your-executable-target ${SDL2_LIBRARY} ...)
19+
#
720
# This module responds to the the flag:
821
# SDL2_BUILDING_LIBRARY
922
# If this is defined, then no SDL2main will be linked in because
@@ -12,6 +25,8 @@
1225
# module will attempt to locate and set the the proper link flags
1326
# as part of the returned SDL2_LIBRARY variable.
1427
#
28+
# If you want to link SDL2 statically, set SDL2_STATIC to ON.
29+
#
1530
# Don't forget to include SDLmain.h and SDLmain.m your project for the
1631
# OS X framework based version. (Other versions link to -lSDL2main which
1732
# this module will try to find on your behalf.) Also for OS X, this
@@ -47,7 +62,7 @@
4762
# SDL2_LIBRARY to override this selection or set the CMake environment
4863
# CMAKE_INCLUDE_PATH to modify the search paths.
4964
#
50-
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
65+
# Note that the header path has changed from SDL3/SDL.h to just SDL.h
5166
# This needed to change because "proper" SDL convention
5267
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
5368
# reasons because not all systems place things in SDL2/ (see FreeBSD).
@@ -86,6 +101,16 @@ FIND_PATH(SDL2_INCLUDE_DIR SDL.h
86101
PATHS ${SDL2_SEARCH_PATHS}
87102
)
88103

104+
SET(CURRENT_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
105+
106+
IF(SDL2_STATIC)
107+
IF(WIN32)
108+
SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a)
109+
ELSE(WIN32)
110+
SET(CMAKE_FIND_LIBRARY_SUFFIXES .a)
111+
ENDIF(WIN32)
112+
ENDIF(SDL2_STATIC)
113+
89114
FIND_LIBRARY(SDL2_LIBRARY_TEMP
90115
NAMES SDL2
91116
HINTS
@@ -110,6 +135,9 @@ IF(NOT SDL2_BUILDING_LIBRARY)
110135
ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
111136
ENDIF(NOT SDL2_BUILDING_LIBRARY)
112137

138+
SET(CMAKE_FIND_LIBRARY_SUFFIXES ${CURRENT_FIND_LIBRARY_SUFFIXES})
139+
UNSET(CURRENT_FIND_LIBRARY_SUFFIXES)
140+
113141
# SDL2 may require threads on your system.
114142
# The Apple build may not need an explicit flag because one of the
115143
# frameworks may already provide it.
@@ -154,6 +182,37 @@ IF(SDL2_LIBRARY_TEMP)
154182
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
155183
ENDIF(MINGW)
156184

185+
# Add some stuff from pkg-config, if available
186+
IF(NOT PKG_CONFIG_EXECUTABLE)
187+
FIND_PACKAGE(PkgConfig QUIET)
188+
ENDIF(NOT PKG_CONFIG_EXECUTABLE)
189+
190+
IF(PKG_CONFIG_EXECUTABLE)
191+
# get any definitions
192+
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --cflags-only-other sdl2 OUTPUT_VARIABLE SDL2_DEFINITIONS)
193+
194+
SET(SDL2_DEFINITIONS ${SDL2_DEFINITIONS} CACHE STRING "Extra CFLAGS for SDL2 from pkg-config")
195+
196+
# get any extra stuff needed for linking
197+
IF(NOT SDL2_STATIC)
198+
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs-only-other sdl2 OUTPUT_VARIABLE SDL2_LINKER_FLAGS_RAW OUTPUT_STRIP_TRAILING_WHITESPACE)
199+
200+
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs-only-l sdl2 OUTPUT_VARIABLE SDL2_EXTRA_LIBS_RAW OUTPUT_STRIP_TRAILING_WHITESPACE)
201+
ELSE(NOT SDL2_STATIC)
202+
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --static --libs-only-other sdl2 OUTPUT_VARIABLE SDL2_LINKER_FLAGS_RAW OUTPUT_STRIP_TRAILING_WHITESPACE)
203+
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --static --libs-only-l sdl2 OUTPUT_VARIABLE SDL2_EXTRA_LIBS_RAW OUTPUT_STRIP_TRAILING_WHITESPACE)
204+
ENDIF(NOT SDL2_STATIC)
205+
206+
STRING(REGEX REPLACE "[^ ]+SDL2[^ ]*" "" SDL2_EXTRA_LIBS_RAW2 "${SDL2_EXTRA_LIBS_RAW}")
207+
STRING(REGEX REPLACE " +" ";" SDL2_EXTRA_LIBS "${SDL2_EXTRA_LIBS_RAW2}")
208+
STRING(REGEX REPLACE " +" ";" SDL2_LINKER_FLAGS "${SDL2_LINKER_FLAGS_RAW}")
209+
210+
SET(SDL2_LINKER_FLAGS ${SDL2_LINKER_FLAGS} CACHE STRING "Linker flags for linking SDL2")
211+
SET(SDL2_EXTRA_LIBS ${SDL2_EXTRA_LIBS} CACHE STRING "Extra libraries for linking SDL2")
212+
213+
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${SDL2_EXTRA_LIBS} ${SDL2_LINKER_FLAGS})
214+
ENDIF(PKG_CONFIG_EXECUTABLE)
215+
157216
# Set the final string here so the GUI reflects the final state.
158217
SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
159218
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ where `project` is the name of your project. You can then use the packages
2828
themselves by adding
2929

3030
```cmake
31+
set(SDL2_STATIC OFF) # on to link it statically
32+
3133
find_package(SDL2 REQUIRED)
34+
35+
add_definitions(${SDL2_DEFINITIONS})
36+
3237
find_package(SDL2_Image REQUIRED)
3338
find_package(SDL2_ttf REQUIRED)
3439

0 commit comments

Comments
 (0)