Skip to content

Improve CMake for IDE Projects (Visual Studio) #13704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 31 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1191,34 +1191,63 @@ endif()
# General source files
sdl_glob_sources(
"${SDL3_SOURCE_DIR}/src/*.c"
"${SDL3_SOURCE_DIR}/src/*.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some more sdl_glob_sources under if(WINDOWS).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. wasn't able to tinker on this for most of the day. Looking at it more closely, there's actually a ton more that I've missed. Now I wonder if I should change approach and augment sdl_glob_sources to get the directories of the files passed in and add additional *.h globs in. What do you think? It'd certainly be less work to maintain it, (For however much work it really is to add a few extra lines of CMake to account for headers is.) Though it might be better for it to accept directories rather than files...I dunno!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not make the globs too smart.
We're already not following cmake best practices by not explicitly listing the sources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I do much prefer being explicit, but I know that doesn't work for everyone. I'll do a thorough pass tomorrow and try not to miss any.

As a tangential question, is there a reason the GLOBs aren't using CONFIGURE_DEPENDS?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about that option.
If performance does not suffer, it makes sense to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I got caught up working on my SDL_MenuBar prototype, my plan was to do CONFIGURE_DEPENDS as a separate change so I didn't end up prioritizing it. I can work on that tonight!

Copy link
Contributor

@madebr madebr Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate PR is OK as well. I was just checking in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some testing locally, I'm not noticing anything that I would be able to consistently nail down without just running builds over and over again. I pushed it up to CI and while it wasn't faster than the previous run on the branch, it was faster than a different run on the same commit.

Previous Commit Run 1: https://github.com/playmer/SDL-1/actions/runs/16858095731
Previous Commit Run 2: https://github.com/playmer/SDL-1/actions/runs/16857983229
CONFIGURE_DEPENDS Commit: https://github.com/playmer/SDL-1/actions/runs/16953366708

The flag should mostly affect incremental builds, as CMake needs to insert globs to run before building your code. I did a quick run of a rebuild by inserting blank lines in SDL.h and again found nothing notable:

(Omitted the actual cmake/build output, but the full logs are here: https://gist.github.com/playmer/7d2c0bc75d694e582c08601f23f5b72b)

With Changes:

playmer@Timestar:~/SDL_test/SDL_with_change$ time cmake -B build .
real    0m48.004s
user    0m6.040s
sys     0m3.943s
playmer@Timestar:~/SDL_test/SDL_with_change$ time cmake --build build -j8
real    0m7.513s
user    0m44.668s
sys     0m6.852s
playmer@Timestar:~/SDL_test/SDL_with_change$ nano include/SDL3/SDL.h
playmer@Timestar:~/SDL_test/SDL_with_change$ time cmake --build build -j8
real    0m7.594s
user    0m44.934s
sys     0m7.024s

Without changes:

playmer@Timestar:~/SDL_test/SDL$ time cmake -B build .
real    0m46.969s
user    0m6.005s
sys     0m3.781s
playmer@Timestar:~/SDL_test/SDL$ time cmake --build build -j8
real    0m11.172s
user    0m45.076s
sys     0m7.158s
playmer@Timestar:~/SDL_test/SDL$ nano include/SDL3/SDL.h
playmer@Timestar:~/SDL_test/SDL$ time cmake --build build -j8
real    0m7.622s
user    0m44.630s
sys     0m7.069s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can push the change to this branch or not, don't want to make the decision for you! Just let me know what you'd prefer!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it in a separate PR and let this one focus on making IDE's pretty again.

"${SDL3_SOURCE_DIR}/src/atomic/*.c"
"${SDL3_SOURCE_DIR}/src/atomic/*.h"
"${SDL3_SOURCE_DIR}/src/audio/*.c"
"${SDL3_SOURCE_DIR}/src/audio/*.h"
"${SDL3_SOURCE_DIR}/src/camera/*.c"
"${SDL3_SOURCE_DIR}/src/camera/*.h"
"${SDL3_SOURCE_DIR}/src/core/*.c"
"${SDL3_SOURCE_DIR}/src/core/*.h"
"${SDL3_SOURCE_DIR}/src/cpuinfo/*.c"
"${SDL3_SOURCE_DIR}/src/cpuinfo/*.h"
"${SDL3_SOURCE_DIR}/src/dynapi/*.c"
"${SDL3_SOURCE_DIR}/src/dynapi/*.h"
"${SDL3_SOURCE_DIR}/src/events/*.c"
"${SDL3_SOURCE_DIR}/src/events/*.h"
"${SDL3_SOURCE_DIR}/src/io/*.c"
"${SDL3_SOURCE_DIR}/src/io/*.h"
"${SDL3_SOURCE_DIR}/src/io/generic/*.c"
"${SDL3_SOURCE_DIR}/src/io/generic/*.h"
"${SDL3_SOURCE_DIR}/src/filesystem/*.c"
"${SDL3_SOURCE_DIR}/src/filesystem/*.h"
"${SDL3_SOURCE_DIR}/src/gpu/*.c"
"${SDL3_SOURCE_DIR}/src/gpu/*.h"
"${SDL3_SOURCE_DIR}/src/joystick/*.c"
"${SDL3_SOURCE_DIR}/src/joystick/*.h"
"${SDL3_SOURCE_DIR}/src/haptic/*.c"
"${SDL3_SOURCE_DIR}/src/haptic/*.h"
"${SDL3_SOURCE_DIR}/src/hidapi/*.c"
"${SDL3_SOURCE_DIR}/src/hidapi/*.h"
"${SDL3_SOURCE_DIR}/src/locale/*.c"
"${SDL3_SOURCE_DIR}/src/locale/*.h"
"${SDL3_SOURCE_DIR}/src/main/*.c"
"${SDL3_SOURCE_DIR}/src/main/*.h"
"${SDL3_SOURCE_DIR}/src/misc/*.c"
"${SDL3_SOURCE_DIR}/src/misc/*.h"
"${SDL3_SOURCE_DIR}/src/power/*.c"
"${SDL3_SOURCE_DIR}/src/power/*.h"
"${SDL3_SOURCE_DIR}/src/render/*.c"
"${SDL3_SOURCE_DIR}/src/render/*.h"
"${SDL3_SOURCE_DIR}/src/render/*/*.c"
"${SDL3_SOURCE_DIR}/src/render/*/*.h"
"${SDL3_SOURCE_DIR}/src/sensor/*.c"
"${SDL3_SOURCE_DIR}/src/sensor/*.h"
"${SDL3_SOURCE_DIR}/src/stdlib/*.c"
"${SDL3_SOURCE_DIR}/src/stdlib/*.h"
"${SDL3_SOURCE_DIR}/src/storage/*.c"
"${SDL3_SOURCE_DIR}/src/storage/*.h"
"${SDL3_SOURCE_DIR}/src/thread/*.c"
"${SDL3_SOURCE_DIR}/src/thread/*.h"
"${SDL3_SOURCE_DIR}/src/time/*.c"
"${SDL3_SOURCE_DIR}/src/time/*.h"
"${SDL3_SOURCE_DIR}/src/timer/*.c"
"${SDL3_SOURCE_DIR}/src/timer/*.h"
"${SDL3_SOURCE_DIR}/src/video/*.c"
"${SDL3_SOURCE_DIR}/src/video/*.h"
"${SDL3_SOURCE_DIR}/src/video/yuv2rgb/*.c"
"${SDL3_SOURCE_DIR}/src/video/yuv2rgb/*.h"
)

# Build uclibc as a static library such that non-used symbols don't end up in the SDL3 shared library.
Expand Down Expand Up @@ -3275,9 +3304,10 @@ list(APPEND SDL3_INCLUDE_FILES "${SDL3_BINARY_DIR}/include-revision/SDL3/SDL_rev
if(SDL_FRAMEWORK)
# With Apple frameworks, headers in the PUBLIC_HEADER property also need to be added as sources
list(APPEND SDL3_INCLUDE_FILES ${SDL3_TEST_INCLUDE_FILES})
sdl_sources(${SDL3_INCLUDE_FILES})
endif()

sdl_sources(${SDL3_INCLUDE_FILES})

if((CMAKE_STATIC_LIBRARY_PREFIX STREQUAL "" AND CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL ".lib") OR SDL_FRAMEWORK)
# - Avoid conflict between the dll import library and the static library
# - Create SDL3-static Apple Framework
Expand Down
17 changes: 17 additions & 0 deletions cmake/sdlcommands.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
add_library(SDL3-collector INTERFACE)
add_library(SDL3_test-collector INTERFACE)

function(sdl_source_group prefix_directory)
set(prefixed_list)
file(TO_CMAKE_PATH ${prefix_directory} normalized_prefix_path)
foreach(file in ${ARGN})
file(TO_CMAKE_PATH ${file} normalized_path)
string(FIND "${normalized_path}" ${normalized_prefix_path} position)
if("${position}" EQUAL 0)
list(APPEND prefixed_list ${file})
endif()
endforeach()
if(prefixed_list)
source_group(TREE ${prefix_directory} FILES ${prefixed_list})
endif()
endfunction()

# Use sdl_glob_sources to add glob sources to SDL3-shared, to SDL3-static, or to both.
function(sdl_glob_sources)
cmake_parse_arguments(ARGS "" "" "SHARED;STATIC" ${ARGN})
Expand All @@ -13,6 +28,7 @@ function(sdl_glob_sources)
if(TARGET SDL3-static)
target_sources(SDL3-static PRIVATE ${static_sources} ${both_sources})
endif()
sdl_source_group(${PROJECT_SOURCE_DIR} ${shared_sources} ${shared_sources} ${both_sources})
set_property(TARGET SDL3-collector APPEND PROPERTY INTERFACE_SOURCES ${shared_sources} ${static_sources} ${both_sources})
endfunction()

Expand All @@ -25,6 +41,7 @@ function(sdl_sources)
if(TARGET SDL3-static)
target_sources(SDL3-static PRIVATE ${ARGS_STATIC} ${ARGS_UNPARSED_ARGUMENTS})
endif()
sdl_source_group(${PROJECT_SOURCE_DIR} ${ARGS_SHARED} ${ARGS_STATIC} ${ARGS_UNPARSED_ARGUMENTS})
set_property(TARGET SDL3-collector APPEND PROPERTY INTERFACE_SOURCES ${ARGS_SHARED} ${ARGS_STATIC} ${ARGS_UNPARSED_ARGUMENTS})
endfunction()

Expand Down
Loading