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 3572

Summary: [PATCH] Export SDL2 libraries with CMake
Product: SDL Reporter: David Demelier <demelier.david>
Component: buildAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: enhancement    
Priority: P2 CC: tschwinger
Version: HG 2.0   
Hardware: x86_64   
OS: Linux   
Attachments: patch
Fixup commit 1
Fixup commit 2
Fixup commit 3
Fixup commit 4

Description David Demelier 2017-01-28 16:21:57 UTC
Created attachment 2691 [details]
patch

If built with CMake, these exports will make easier to use find_package(SDL2) for developers.

The next steps is to add CMake support as well to SDL_net,mixer,ttf,rtf,image thanks to that patch.
Comment 1 Sam Lantinga 2017-01-28 19:20:02 UTC
Ryan, can you check this and include it if it looks good?
Comment 2 tschwinger 2017-05-14 18:21:04 UTC
This looks good and would be super nice for CMake users. I should bump the issue and add some background info:

It's the "modern and recommended" way to package your lib with CMake. The best about it is, that it gives you what pkgconfig does on UNIX, just without the hassle and on all platforms, including Windows:

Once you import via `find_package` (in CONFIG mode) you can just use the SDL targets via `target_link_libraries` and *everything* (include dir, required defines, propagated dependencies, libraries) is just available and can be used with a single line of code:

```
# this can happen just once for your entire build:
find_package(SDL2 CONFIG)

# now to let one of your projects use SDL:
target_link_libraries(my_project PUBLIC SDL2::SDL SDL2::SDLmain)
# yes, that's really all you need
```

https://cmake.org/cmake/help/v3.8/command/target_link_libraries.html
https://cmake.org/cmake/help/v3.8/command/find_package.html?highlight=module%20mode

https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets
https://cmake.org/Wiki/CMake/Tutorials/Packaging
Comment 3 tschwinger 2017-05-14 22:21:08 UTC
Testing it. There still seems to be an issue with the installation directory. The `cmake` directory must either go into `share` or `lib` for `find_package` to find the config file when given the installation root...
Comment 4 tschwinger 2017-05-14 22:53:22 UTC
With that resolved, the include path is still off:

```
#include <SDL2/SDL.h>
```

is expected to work, and it's `<prefix>/include/SDL2/SDL.h`, so we just want the compiler to see something like `-I <prefix>/include`.
Comment 5 tschwinger 2017-05-14 23:12:05 UTC
More issues:

- No include path is being advertised for SDLmain
- We need a minimum CMake version of 2.8.11 for this stuff to work (should be quite reasonable nowadays)

Will post fixup patches in a bit, after another test.
Comment 6 tschwinger 2017-05-14 23:28:09 UTC
Created attachment 2737 [details]
Fixup commit 1
Comment 7 tschwinger 2017-05-14 23:28:39 UTC
Created attachment 2738 [details]
Fixup commit 2
Comment 8 tschwinger 2017-05-14 23:29:29 UTC
Created attachment 2739 [details]
Fixup commit 3
Comment 9 tschwinger 2017-05-14 23:30:14 UTC
Created attachment 2740 [details]
Fixup commit 4
Comment 10 David Demelier 2017-05-24 08:29:08 UTC
(In reply to tschwinger from comment #4)
> With that resolved, the include path is still off:
> 
> ```
> #include <SDL2/SDL.h>
> ```
> 
> is expected to work, and it's `<prefix>/include/SDL2/SDL.h`, so we just want
> the compiler to see something like `-I <prefix>/include`.

This isn't correct, the SDL convention is to include SDL.h with no prefix.
Comment 11 Sam Lantinga 2017-08-10 02:05:14 UTC
This should be fixed by this commit:
https://hg.libsdl.org/SDL/rev/c92070a96da5