Skip to content

Commit 02f0774

Browse files
ChrisThrashereXpl0it3r
authored andcommitted
Use FetchContent to depend on SFML
1 parent 49c9f78 commit 02f0774

File tree

3 files changed

+72
-38
lines changed

3 files changed

+72
-38
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,25 @@ jobs:
1717
- { name: Linux Clang, os: ubuntu-latest, flags: -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ }
1818
- { name: MacOS XCode, os: macos-latest }
1919
config:
20-
- { name: Shared, flags: -DSFML_STATIC_LIBRARIES=OFF -DBUILD_SHARED_LIBS=TRUE }
21-
- { name: Static, flags: -DSFML_STATIC_LIBRARIES=ON -DBUILD_SHARED_LIBS=FALSE }
20+
- { name: Shared, flags: -DBUILD_SHARED_LIBS=TRUE }
21+
- { name: Static, flags: -DBUILD_SHARED_LIBS=FALSE }
2222

2323
steps:
2424
- name: Install Linux Dependencies
2525
if: runner.os == 'Linux'
2626
run: sudo apt-get update && sudo apt-get install libxrandr-dev libxcursor-dev libudev-dev libopenal-dev libflac-dev libvorbis-dev libgl1-mesa-dev libegl1-mesa-dev
2727

28-
- name: CMake SFML Project - Checkout Code
28+
- name: Checkout
2929
uses: actions/checkout@v3
3030

31-
- name: SFML - Checkout Code
32-
uses: actions/checkout@v3
33-
with:
34-
fetch-depth: 0
35-
repository: SFML/SFML
36-
ref: 2.6.x
37-
path: SFML
38-
39-
- name: SFML - Configure CMake
40-
shell: bash
41-
run: cmake -S $GITHUB_WORKSPACE/SFML -B $GITHUB_WORKSPACE/SFML/build -DCMAKE_INSTALL_PREFIX=$GITHUB_WORKSPACE/SFML/install -DCMAKE_VERBOSE_MAKEFILE=ON ${{matrix.platform.flags}} ${{matrix.config.flags}}
42-
43-
- name: SFML - Build
31+
- name: Configure
4432
shell: bash
45-
run: cmake --build $GITHUB_WORKSPACE/SFML/build --config Release --target install
33+
run: cmake -S . -B build -DCMAKE_INSTALL_PREFIX=install ${{matrix.platform.flags}} ${{matrix.config.flags}}
4634

47-
- name: CMake SFML Project - Configure CMake
35+
- name: Build
4836
shell: bash
49-
run: cmake -S $GITHUB_WORKSPACE -B $GITHUB_WORKSPACE/build -DCMAKE_INSTALL_PREFIX=$GITHUB_WORKSPACE/install -DCMAKE_VERBOSE_MAKEFILE=ON -DSFML_DIR=$GITHUB_WORKSPACE/SFML/install/lib/cmake/SFML ${{matrix.platform.flags}} ${{matrix.config.flags}}
37+
run: cmake --build build --config Release
5038

51-
- name: CMake SFML Project - Build
39+
- name: Install
5240
shell: bash
53-
run: cmake --build $GITHUB_WORKSPACE/build --config Release --target install
41+
run: cmake --install build --config Release

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
cmake_minimum_required(VERSION 3.15)
22
project(CMakeSFMLProject LANGUAGES CXX)
33

4-
find_package(SFML 2.5.1 COMPONENTS graphics REQUIRED)
4+
include(FetchContent)
5+
FetchContent_Declare(SFML
6+
GIT_REPOSITORY https://github.com/SFML/SFML.git
7+
GIT_TAG 2.6.x)
8+
FetchContent_MakeAvailable(SFML)
59

610
add_executable(CMakeSFMLProject src/main.cpp)
711
target_link_libraries(CMakeSFMLProject PRIVATE sfml-graphics)

README.md

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,80 @@
11
# CMake SFML Project Template
22

3-
This repository template, should allow for a fast and hassle-free kick start of your next SFML project using CMake.
3+
This repository template should allow for a fast and hassle-free kick start of your next SFML project using CMake.
44
Thanks to [GitHub's nature of templates](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template), you can fork this repository without inheriting its Git history.
55

66
The template starts out very basic, but might receive additional features over time:
77

8-
- Basic CMake script to build your project and link SFML
8+
- Basic CMake script to build your project and link SFML on any operating system
99
- Basic [GitHub Actions](https://github.com/features/actions) script for all major platforms
1010

1111
## How to Use
1212

1313
1. Follow the above instructions about how to use GitHub's project template feature to create your own project.
1414
1. Open [CMakeLists.txt](CMakeLists.txt). Rename the project and the executable to whatever name you want. The project and executable names don't have to match.
15-
1. Install SFML 2.5.1.
15+
1. Change the source files listed in [`add_executable`](CMakeLists.txt#L10) to match the source files your project requires.
16+
1. Configure and build your project. Most popular IDEs support CMake projects with very little effort on your part.
17+
- [VS Code](https://code.visualstudio.com) via the [CMake extension](https://code.visualstudio.com/docs/cpp/cmake-linux)
18+
- [Visual Studio](https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-170)
19+
- [CLion](https://www.jetbrains.com/clion/features/cmake-support.html)
20+
- [Qt Creator](https://doc.qt.io/qtcreator/creator-project-cmake.html)
1621

17-
On Ubuntu Linux:
22+
Using CMake from the command line is straightforward as well.
23+
24+
For a single-configuration generator (typically the case on Linux and macOS):
1825
```
19-
sudo apt install libsfml-dev
26+
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
27+
cmake --build build
2028
```
21-
On macOS
29+
30+
For a multi-configuration generator (typically the case on Windows):
2231
```
23-
brew install sfml
32+
cmake -S . -B build
33+
cmake --build build --config Release
2434
```
25-
1. Change the source files listed in [`add_executable`](CMakeLists.txt#L6) to match the source files your project requires.
26-
1. Configure and build your project.
35+
1. Enjoy!
2736
28-
On Linux or macOS, run these commands from the root directory of your project.
29-
```cmake
30-
cmake -B build
31-
cmake --build build
32-
```
37+
## Upgrading SFML
3338
34-
Alternatively, use [VS Code](https://code.visualstudio.com) with its [CMake extension](https://code.visualstudio.com/docs/cpp/cmake-linux) on any OS.
35-
1. Enjoy!
39+
SFML is found via CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module.
40+
FetchContent automatically downloads SFML from GitHub and builds it alongside your own code.
41+
Beyond the convenience of not having to install SFML yourself, this ensures ABI compatability and simplifies things like specifying static versus shared libraries.
42+
43+
Modifying what version of SFML you want is as easy as changing the [`GIT_TAG`](CMakeLists.txt#L7) argument.
44+
Currently it uses the latest in-development version of SFML 2 via the `2.6.x` tag.
45+
If you're feeling adventurous and want to give SFML 3 a try, use the `master` tag.
46+
Beware, this requires changing your code to suit the modified API!
47+
The nice folks in the [SFML community](https://github.com/SFML/SFML#community) can help you with that transition and the bugs you may encounter along the way.
48+
49+
## But I want to...
50+
51+
Modify CMake options by adding them as configuration parameters (with a `-D` flag) or by modifying the contents of CMakeCache.txt and rebuilding.
52+
53+
### Use Static Libraries
54+
55+
By default SFML builds shared libraries and this default is inherited by your project.
56+
CMake's [`BUILD_SHARED_LIBS`](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html) option lets you pick static or shared libraries for the entire project.
57+
58+
### Change Compilers
59+
60+
See the variety of [`CMAKE_<LANG>_COMPILER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html) options.
61+
In particular you'll want to modify `CMAKE_CXX_COMPILER` to point to the C++ compiler you wish to use.
62+
63+
### Change Compiler Optimizations
64+
65+
CMake abstracts away specific optimizer flags through the [`CMAKE_BUILD_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html) option.
66+
By default this project recommends `Release` builds which enable optimizations.
67+
Other build types include `Debug` builds which enable debug symbols but disable optimizations.
68+
If you're using a multi-configuration generator (as is often the case on Windows), you can modify the [`CMAKE_CONFIGURATION_TYPES`](https://cmake.org/cmake/help/latest/variable/CMAKE_CONFIGURATION_TYPES.html#variable:CMAKE_CONFIGURATION_TYPES) option.
69+
70+
### Change Generators
71+
72+
While CMake will attempt to pick a suitable default generator, some systems offer a number of generators to choose from.
73+
Ubuntu, for example, offers Makefiles and Ninja as two potential options.
74+
For a list of generators, click [here](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html).
75+
To modify the generator you're using you must reconfigure your project providing a `-G` flag with a value corresponding to the generator you want.
76+
You can't simply modify an entry in the CMakeCache.txt file unlike the above options.
77+
Then you may rebuild your project with this new generator.
3678
3779
## More Reading
3880

0 commit comments

Comments
 (0)