blob: b1cdd4c7ea052ff004ecf50f17b04cf4f58baebf [file] [log] [blame]
Jim Flynn0dcef532022-06-14 10:58:23 +01001#
2# Copyright © 2018 Arm Ltd and Contributors. All rights reserved.
3# SPDX-License-Identifier: MIT
4#
5
telsoa014fcda012018-03-09 14:13:49 +00006# Function which creates appropriate "source groups" (filter folders in Visual Studio) for the given list of source files
7function(createSourceGroups source1)
8 set(sources ${source1} ${ARGN})
9 foreach(source ${sources})
10 get_filename_component(source_path ${source} PATH)
11 string(REPLACE "/" "\\" source_path_backslashes "${source_path}")
12 source_group(${source_path_backslashes} FILES ${source})
13 endforeach()
14endfunction()
15
16# Further processes a target and its list of source files adding extra touches useful for some generators
17# (filter folders, group targets in folders, etc.).
18# All optional arguments are treated as additional source files.
19function(setup_target targetName source1)
20 set(sources ${source1} ${ARGN})
21
22 createSourceGroups(${sources})
23
24 # Enable USE_FOLDERS. This is required by the set_target_properties(... FOLDER ...) call below.
25 # We prefer to set it here rather than globally at the top of the file so that we only modify
26 # the Cmake environment if/when the functionality is actually required.
27 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
28 file(RELATIVE_PATH projectFolder ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
29 set_target_properties(${targetName} PROPERTIES FOLDER "${projectFolder}")
30endfunction()
31
32# Convenience replacement of add_executable(), which besides adding an executable to the project
33# further configures the target via setup_target().
34# All optional arguments are treated as additional source files.
35function(add_executable_ex targetName source1)
36 set(sources ${source1} ${ARGN})
37 add_executable(${targetName} ${sources})
38 setup_target(${targetName} ${sources})
39endfunction()
40
41# Convenience replacement of add_library(), which besides adding a library to the project
42# further configures the target via setup_target().
43# All optional arguments are treated as additional source files.
44function(add_library_ex targetName libraryType source1)
45 set(sources ${source1} ${ARGN})
46 add_library(${targetName} ${libraryType} ${sources})
47 setup_target(${targetName} ${sources})
48endfunction()