We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3166 - It would be nice, if SDL would support including SDL project as a subdirectory into another CMake project
Summary: It would be nice, if SDL would support including SDL project as a subdirector...
Status: WAITING
Alias: None
Product: SDL
Classification: Unclassified
Component: build (show other bugs)
Version: don't know
Hardware: x86_64 Windows 7
: P2 minor
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-07 21:06 UTC by Denis Kerzhemanov
Modified: 2020-01-31 02:11 UTC (History)
4 users (show)

See Also:


Attachments
CMakeLists.txt patch (1.56 KB, patch)
2018-09-23 10:51 UTC, Wayde Reitsma
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Denis Kerzhemanov 2015-11-07 21:06:15 UTC
One of the variants of using external CMake project inside your project, is including it as a directory using add_subdirectory directive. It is very useful if you want to build SDL yourself. Then you just write something like that:
  add_subdirectory(../SDL2 SDL2)
  target_link_libraries(yourtarget 
	SDL2
	SDL2main
  )
It works almost fine, but there is one small problem. CMake can automatically import include directories from libraries, but in case of SDL this feature does not work. To make it work, you need to do two things:
  1. Remove this string from CMakeLists.txt:
    include_directories(${SDL2_BINARY_DIR}/include ${SDL2_SOURCE_DIR}/include)"
  2. Add these strings somewhere after definition of correspondent libraries: 
    target_include_directories(SDL2 PUBLIC ${SDL2_BINARY_DIR}/include PUBLIC ${SDL2_SOURCE_DIR}/include)
    target_include_directories(SDL2main PUBLIC ${SDL2_BINARY_DIR}/include PUBLIC ${SDL2_SOURCE_DIR}/include)
    target_include_directories(SDL2-static PUBLIC ${SDL2_BINARY_DIR}/include PUBLIC ${SDL2_SOURCE_DIR}/include)
target_include_directories directive is better way of adding include directories because it makes obvious for what libraries they are added. Also it allows you to specify whether those directories will be needed by dependent projects.

Thank you!
Comment 1 Magnus Bjerke Vik 2016-02-24 21:06:48 UTC
I would also like this to be added. It makes using the library as a subproject easier, though it is still possible without it.

I thought I'd mention that you need to bump the cmake_minimum_required version to 2.8.11, as the function was introduced in this version: https://cmake.org/files/v2.8/CMakeChangeLog-2.8.11
Comment 2 Wayde Reitsma 2018-09-23 10:51:13 UTC
Created attachment 3307 [details]
CMakeLists.txt patch

After attempting to use SDL2 in the way described in this bug, I found the main issue was the includes not being added to the compiler command.

I found the issue was that the target_include_directories commands for the SDL2, SDL2-static and SDL2main targets only sets the public includes for installations using the INSTALL_INTERFACE generator expression.

I have written a patch to CMakeLists.txt that fixes this issue by adding another item to the target_include_directories commands, utilizing the BUILD_INTERFACE generator expression to correctly add the include directory during builds.
Comment 3 Sam Lantinga 2018-09-24 15:42:33 UTC
I've added your patch, is that all that is needed to close this bug?
https://hg.libsdl.org/SDL/rev/4972784f494e
Comment 4 Wayde Reitsma 2018-09-25 05:48:04 UTC
Unfortunately I've run into a bit of a problem. The patch I submitted doesn't take into account the generated SDL_config.h, and while trying to add it to the includes, I was unable to get CMake to add the generated include directory before the main include directory. CMake seems to disregard the order that the include directories were specified in the CMakeLists.txt file.

This results in programs that use the CMake add_subdirectory method to include the manually written SDL_config_{platform}.h header instead of the CMake generated one. This won't necessarily cause an error, but it is not ideal.

I'm not sure what to do about this, as I can't find a way to force CMake to add the include directories in a particular order.