Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: CMake on multiple platforms

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, clang, cl]
include:
- os: windows-latest
c_compiler: cl
cpp_compiler: cl
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
exclude:
- os: windows-latest
c_compiler: gcc
- os: windows-latest
c_compiler: clang
- os: ubuntu-latest
c_compiler: cl

steps:
- uses: actions/checkout@v4
with:
submodules: true

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}

- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
76 changes: 56 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,53 +1,89 @@
cmake_minimum_required(VERSION 3.0)
project(stb-cmake-wrapper)
cmake_minimum_required(VERSION 3.11..3.31)
project(stb-cmake-wrapper
VERSION 0.2.0
DESCRIPTION "CMake wrapper for stb libraries"
LANGUAGES C
)

function(add_stb_library library_name)
set(target_name stb-${library_name})
string(TOUPPER ${library_name} uppercase_library_name)
function(add_stb_library)
set(oneValueArgs NAME EXT)
set(multiValueArgs)

add_library(
${target_name}
main.cpp
cmake_parse_arguments(
PARSE_ARGV 0 arg
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
)
target_compile_definitions(

if (NOT DEFINED arg_NAME)
message(FATAL_ERROR "NAME argument is required")
endif()

if (NOT DEFINED arg_EXT)
set(arg_EXT "h")
endif()

set(target_name stb-${arg_NAME})
string(TOUPPER ${arg_NAME} upper_NAME)

add_library(
${target_name}
PRIVATE
STB_${uppercase_library_name}_IMPLEMENTATION
STB_CMAKE_WRAPPER_LIBRARY_INCLUDE="stb_${library_name}.h"
main.c
)
target_include_directories(
${target_name}
PUBLIC
stb
)
add_library(stb::${library_name} ALIAS stb-${library_name})
target_compile_definitions(
${target_name}
PRIVATE
STB_${upper_NAME}_IMPLEMENTATION
STB_CMAKE_WRAPPER_LIBRARY_INCLUDE="stb_${arg_NAME}.${arg_EXT}"
)
add_library(stb::${arg_NAME} ALIAS stb-${arg_NAME})

if (MSVC)
# Suppress known warnings for MSVC (W4 has too many to bother with)
# This, as of April 2025, compiles with no warnings at all
target_compile_options(${target_name} PRIVATE /W3 /WX /permissive- /wd4244)
target_compile_definitions(${target_name} PRIVATE _CRT_SECURE_NO_WARNINGS)
else()
# There are a LOT of warnings with gcc/clang, so just disable them all...
# We _trust_ stb, right?
target_compile_options(${target_name} PRIVATE -w -Wno-incompatible-function-pointer-types)
endif()

target_compile_features(${target_name} PRIVATE c_std_11)
endfunction()

set(libraries

# vorbis This is a .c file for some reason?
hexwave
image
truetype
image_write # The library is called image_writer but the header file is called image_write
image_resize
image_resize2
rect_pack
perlin
ds
sprintf
# textedit Too many definitions needed so leave it out for now
# voxel_render
# textedit # Too many definitions needed so leave it out for now
# voxel_render # "Must defined STBVOX_CONFIG_MODE to select the mode"
dxt
easy_font
# tilemap_editor
# tilemap_editor # 'STBTE_DRAW_RECT' undefined;
herringbone_wang_tile
c_lexer
divide
# connected_components
# connected_components # "You must define STBCC_GRID_COUNT_X_LOG2 and STBCC_GRID_COUNT_Y_LOG2 to define the max grid supported."
leakcheck
include
)

foreach(library ${libraries})
add_stb_library(${library})
message(STATUS "Adding stb library: ${library}")
add_stb_library(NAME ${library})
endforeach()
add_stb_library(NAME vorbis EXT "c")
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
# stb cmake wrapper

The popular [stb libraries](https://github.com/nothings/stb) are a set of header-only libraries which are very easy to just copy and paste into existing codebases.
However, if you have already set up a cmake based project is sometimes convenient to have proper cmake targets for the individual libraries.
This wrapper creates cmake targets for each library that takes care of adding the actual implementation for the libraries, i.e., adding the proper `STD_(...)_IMPLEMENTATION` define.
This wrapper creates cmake targets for each library that takes care of adding the actual implementation for the libraries, i.e., adding the proper `STB_(...)_IMPLEMENTATION` define.

## Fork

This is a fork to update the version of `stb`, fix some minor documentation issues, and update the cmake min target in the era of CMake 4.0 deprecating older versions.

## Consuming this library in cmake

After adding this wrapper to you project you have access to the `std::LIBRARY_NAME` targets.
After adding this wrapper to your project you have access to the `stb::LIBRARY_NAME` targets.
For example, to use the image library you simply add this to your cmake script:

```cmake
target_link_libraries(YOUR_TARGET PUBLIC std::image)
target_link_libraries(YOUR_TARGET PUBLIC stb::image)
```

### Adding the wrapper to your project
1. Via [CPM](https://github.com/cpm-cmake/CPM.cmake): simply add `CPMAddPackage("gh:ovis-games/stb-cmake-wrapper@0.1")` to your cmake script.
2. Via git submodules: add a submodule to your repository and add `add_subdirectory(path/to/std-cmake-wrapper)`. Alternatively you can also copy and paste the conent of this repository instead of using git submodules.
## Adding the wrapper to your project

1. Via [CPM](https://github.com/cpm-cmake/CPM.cmake): simply add `CPMAddPackage("gh:vikhik/stb-cmake-wrapper@0.2")` to your cmake script.
2. Via git submodules: add a submodule to your repository and add `add_subdirectory(path/to/stb-cmake-wrapper)`.
3. Alternatively you can also copy and paste the contents of this repository.

## Library support

### Library support
This list gives an overview of the currently supported libraries.
The libraries currently not supported by the wrapper are the ones needing additional defines to work (textedit, voxel_render, tilemap_editor, connected_components).
In addition, the vorbis library is currently not supported as it is a .c file but somehow acts as a header and I currently do not want look into it how it actually works.

- [ ] [stb::vorbis](https://github.com/nothings/stb/blob/master/stb_vorbis.c)
- [x] [stb::vorbis](https://github.com/nothings/stb/blob/master/stb_vorbis.c)
- [x] [stb::hexwave](https://github.com/nothings/stb/blob/master/stb_hexwave.h)
- [x] [stb::image](https://github.com/nothings/stb/blob/master/stb_image.h)
- [x] [stb::truetype](https://github.com/nothings/stb/blob/master/stb_truetype.h)
Expand All @@ -40,5 +50,6 @@ In addition, the vorbis library is currently not supported as it is a .c file bu
- [x] [stb::leakcheck](https://github.com/nothings/stb/blob/master/stb_leakcheck.h)
- [x] [stb::include](https://github.com/nothings/stb/blob/master/stb_include.h)

### License
## License

[MIT](LICENSE)
File renamed without changes.