Skip to content

Commit 6e148e2

Browse files
author
undef-in-ed
committed
CMake: mimic folder hierarchy with source_group for MSVC
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@10973 178a84e3-b1eb-0310-8ba1-8eac791a3b58
1 parent 8108045 commit 6e148e2

File tree

4 files changed

+493
-196
lines changed

4 files changed

+493
-196
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,14 @@ else()
103103
endif()
104104

105105

106-
# Provides list of source files in STK_SOURCES
106+
# Provides list of source and header files (STK_SOURCES and STK_HEADERS)
107107
include(sources.cmake)
108108

109+
# Generate source groups useful for MSVC project explorer
110+
include(cmake/SourceGroupFunctions.cmake)
111+
source_group_hierarchy(STK_SOURCES STK_HEADERS)
112+
113+
109114
if(APPLE)
110115
# icon files to copy in the bundle
111116
set(OSX_ICON_FILES ${PROJECT_SOURCE_DIR}/src/ide/Xcode/stk.icns)
@@ -138,7 +143,7 @@ else()
138143
add_definitions(-DSUPERTUXKART_DATADIR=\"${CMAKE_INSTALL_PREFIX}/share/games/supertuxkart\")
139144

140145
# Build the final executable
141-
add_executable(supertuxkart ${STK_SOURCES})
146+
add_executable(supertuxkart ${STK_SOURCES} ${STK_HEADERS})
142147
if(CYGWIN)
143148
target_link_libraries(supertuxkart pthread)
144149
elseif(UNIX)

cmake/SourceGroupFunctions.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generate source groups which mimic the original folder hierarchy.
2+
# This is mainly useful for MSVC's project explorer
3+
# - SRCS list of source files
4+
# - HDRS list of header files
5+
function(source_group_hierarchy SRCS HDRS)
6+
foreach(source_file ${${SRCS}})
7+
source_group_file(${source_file} "Source Files\\")
8+
endforeach()
9+
10+
foreach(header_file ${${HDRS}})
11+
source_group_file(${header_file} "Header Files\\")
12+
endforeach()
13+
endfunction()
14+
15+
# Determine source_group depending on file path
16+
# - FILE path to a file (header or source)
17+
# - GROUP_PREFIX prefix for group name
18+
function(source_group_file file group_prefix)
19+
get_filename_component(file_path ${file} PATH)
20+
if(${file_path} STREQUAL "src")
21+
source_group("${group_prefix}" FILES ${file})
22+
else()
23+
string(REGEX REPLACE "^src/(.*)$" "\\1" group_name ${file_path})
24+
string(REPLACE "/" "\\\\" group_name ${group_name})
25+
source_group("${group_prefix}${group_name}" FILES ${file})
26+
endif()
27+
endfunction()

0 commit comments

Comments
 (0)