Skip to content

Commit 5184c1f

Browse files
add samples
1 parent 9638697 commit 5184c1f

File tree

10 files changed

+164
-151
lines changed

10 files changed

+164
-151
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,8 @@ vkb__enable_testing()
4848
# component libraries
4949
add_subdirectory(components)
5050

51+
# register samples
52+
add_subdirectory(samples)
53+
5154
# executables
5255
add_subdirectory(cmd)

bldsys/cmake/sample_helper.cmake

Lines changed: 42 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -17,159 +17,66 @@
1717
1818
]]
1919

20-
set(SCRIPT_DIR ${CMAKE_CURRENT_LIST_DIR})
20+
set_property(GLOBAL PROPERTY SAMPLE_DESCRIPTORS "")
2121

22-
function(add_sample)
22+
function(vkb__register_sample_descriptor)
2323
set(options)
24-
set(oneValueArgs ID CATEGORY AUTHOR NAME DESCRIPTION)
25-
set(multiValueArgs FILES LIBS SHADER_FILES_GLSL)
24+
set(oneValueArgs NAME DESCRIPTION LIB)
25+
set(multiValueArgs)
2626

2727
cmake_parse_arguments(TARGET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
2828

29-
add_sample_with_tags(
30-
TYPE "Sample"
31-
ID ${TARGET_ID}
32-
CATEGORY ${TARGET_CATEGORY}
33-
AUTHOR ${TARGET_AUTHOR}
34-
NAME ${TARGET_NAME}
35-
DESCRIPTION ${TARGET_DESCRIPTION}
36-
TAGS
37-
"any"
38-
FILES
39-
${SRC_FILES}
40-
LIBS
41-
${TARGET_LIBS}
42-
SHADER_FILES_GLSL
43-
${TARGET_SHADER_FILES_GLSL})
29+
get_property(DESCRIPTORS GLOBAL PROPERTY SAMPLE_DESCRIPTORS)
30+
list(APPEND DESCRIPTORS "{\"name\":\"${TARGET_NAME}\", \"description\": \"${TARGET_DESCRIPTION}\", \"library_name\": \"${TARGET_LIB}\"}")
31+
set_property(GLOBAL PROPERTY SAMPLE_DESCRIPTORS ${DESCRIPTORS})
4432
endfunction()
4533

46-
function(add_sample_with_tags)
47-
set(options)
48-
set(oneValueArgs ID CATEGORY AUTHOR NAME DESCRIPTION)
49-
set(multiValueArgs TAGS FILES LIBS SHADER_FILES_GLSL)
50-
51-
cmake_parse_arguments(TARGET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
52-
53-
list(APPEND TARGET_TAGS "any")
54-
55-
set(SRC_FILES
56-
${TARGET_ID}.h
57-
${TARGET_ID}.cpp
58-
)
59-
60-
# Append extra files if present
61-
if (TARGET_FILES)
62-
list(APPEND SRC_FILES ${TARGET_FILES})
63-
endif()
64-
65-
# Add GLSL shader files for this sample
66-
if (TARGET_SHADER_FILES_GLSL)
67-
list(APPEND SHADER_FILES_GLSL ${TARGET_SHADER_FILES_GLSL})
68-
foreach(SHADER_FILE_GLSL ${SHADER_FILES_GLSL})
69-
list(APPEND SHADERS_GLSL "${PROJECT_SOURCE_DIR}/shaders/${SHADER_FILE_GLSL}")
70-
endforeach()
71-
endif()
72-
73-
add_project(
74-
TYPE "Sample"
75-
ID ${TARGET_ID}
76-
CATEGORY ${TARGET_CATEGORY}
77-
AUTHOR ${TARGET_AUTHOR}
78-
NAME ${TARGET_NAME}
79-
DESCRIPTION ${TARGET_DESCRIPTION}
80-
TAGS
81-
${TARGET_TAGS}
82-
FILES
83-
${SRC_FILES}
84-
LIBS
85-
${TARGET_LIBS}
86-
SHADERS_GLSL
87-
${SHADERS_GLSL})
88-
89-
endfunction()
34+
add_custom_target(vkb__samples)
9035

91-
function(vkb_add_test)
36+
function(vkb__register_sample)
9237
set(options)
93-
set(oneValueArgs ID)
94-
set(multiValueArgs)
95-
96-
cmake_parse_arguments(TARGET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
97-
98-
add_project(
99-
TYPE "Test"
100-
ID ${TARGET_ID}
101-
CATEGORY "Tests"
102-
AUTHOR " "
103-
NAME ${TARGET_ID}
104-
DESCRIPTION " "
105-
VENDOR_TAG " "
106-
LIBS test_framework
107-
FILES
108-
${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_ID}.h
109-
${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_ID}.cpp)
110-
endfunction()
111-
112-
function(add_project)
113-
set(options)
114-
set(oneValueArgs TYPE ID CATEGORY AUTHOR NAME DESCRIPTION)
115-
set(multiValueArgs TAGS FILES LIBS SHADERS_GLSL)
38+
set(oneValueArgs NAME)
39+
set(multiValueArgs SRC LINK_LIBS INCLUDE_DIRS)
11640

11741
cmake_parse_arguments(TARGET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
11842

119-
if(${TARGET_TYPE} STREQUAL "Sample")
120-
set("VKB_${TARGET_ID}" ON CACHE BOOL "Build sample ${TARGET_ID}")
43+
if(TARGET_NAME STREQUAL "")
44+
message(FATAL_ERROR "NAME must be defined in vkb__register_tests")
12145
endif()
12246

123-
if(NOT ${VKB_${TARGET_ID}})
124-
message(STATUS "${TARGET_TYPE} `${TARGET_ID}` - DISABLED")
125-
return()
126-
endif()
47+
if(TARGET_SRC) ## Create static library
48+
message("ADDING SAMPLE: sample__${TARGET_NAME}")
12749

128-
message(STATUS "${TARGET_TYPE} `${TARGET_ID}` - BUILD")
50+
add_library("sample__${TARGET_NAME}" SHARED ${TARGET_SRC})
12951

130-
# create project (object target - reused by app target)
131-
project(${TARGET_ID} LANGUAGES C CXX)
52+
target_link_libraries("sample__${TARGET_NAME}" PUBLIC vkb__platform_headers) # sample_main
13253

133-
source_group("\\" FILES ${TARGET_FILES})
54+
if(TARGET_LINK_LIBS)
55+
target_link_libraries("sample__${TARGET_NAME}" PUBLIC ${TARGET_LINK_LIBS})
56+
endif()
13457

135-
# Add shaders to project group
136-
if (SHADERS_GLSL)
137-
source_group("\\Shaders" FILES ${SHADERS_GLSL})
58+
if(TARGET_INCLUDE_DIRS)
59+
target_include_directories("sample__${TARGET_NAME}" PUBLIC ${TARGET_INCLUDE_DIRS})
60+
endif()
61+
62+
if(MSVC)
63+
target_compile_options("sample__${TARGET_NAME}" PRIVATE /W4 /WX)
64+
else()
65+
target_compile_options("sample__${TARGET_NAME}" PRIVATE -Wall -Wextra -Wpedantic -Werror)
66+
endif()
67+
else()
68+
message(FATAL_ERROR "a sample must contain one or more source files")
13869
endif()
13970

140-
add_library(${PROJECT_NAME} STATIC ${TARGET_FILES} ${SHADERS_GLSL})
141-
142-
# inherit compile definitions from framework target
143-
target_compile_definitions(${PROJECT_NAME} PUBLIC $<TARGET_PROPERTY:framework,COMPILE_DEFINITIONS>)
144-
145-
# # inherit include directories from framework target
146-
target_include_directories(${PROJECT_NAME} PUBLIC $<TARGET_PROPERTY:framework,INCLUDE_DIRECTORIES> $<TARGET_PROPERTY:plugins,INCLUDE_DIRECTORIES> ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
147-
target_link_libraries(${PROJECT_NAME} PRIVATE framework)
148-
149-
# Link against extra project specific libraries
150-
if(TARGET_LIBS)
151-
target_link_libraries(${PROJECT_NAME} PUBLIC ${TARGET_LIBS})
152-
endif()
71+
# copy lib to samples_launcher executable
72+
add_custom_command(
73+
TARGET "sample__${TARGET_NAME}"
74+
POST_BUILD
75+
COMMAND ${CMAKE_COMMAND} -E copy
76+
"$<TARGET_FILE:sample__${TARGET_NAME}>"
77+
"$<TARGET_FILE_DIR:samples_launcher>/$<TARGET_FILE_NAME:sample__${TARGET_NAME}>"
78+
)
15379

154-
# capitalise the first letter of the category (performance -> Performance)
155-
string(SUBSTRING ${TARGET_CATEGORY} 0 1 FIRST_LETTER)
156-
string(TOUPPER ${FIRST_LETTER} FIRST_LETTER)
157-
string(REGEX REPLACE "^.(.*)" "${FIRST_LETTER}\\1" CATEGORY "${TARGET_CATEGORY}")
158-
159-
if(${TARGET_TYPE} STREQUAL "Sample")
160-
# set sample properties
161-
set_target_properties(${PROJECT_NAME}
162-
PROPERTIES
163-
SAMPLE_CATEGORY ${TARGET_CATEGORY}
164-
SAMPLE_AUTHOR ${TARGET_AUTHOR}
165-
SAMPLE_NAME ${TARGET_NAME}
166-
SAMPLE_DESCRIPTION ${TARGET_DESCRIPTION}
167-
SAMPLE_TAGS "${TARGET_TAGS}")
168-
169-
# add sample project to a folder
170-
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER "Samples//${CATEGORY}")
171-
elseif(${TARGET_TYPE} STREQUAL "Test")
172-
# add test project to a folder
173-
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER "Tests")
174-
endif()
175-
endfunction()
80+
add_dependencies("sample__${TARGET_NAME}" samples_launcher) # automatically build the samples_launcher if a sample is set to compile
81+
add_dependencies(vkb__samples "sample__${TARGET_NAME}")
82+
endfunction()

cmd/samples_launcher/CMakeLists.txt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,33 @@
1515
# limitations under the License.
1616
#
1717

18-
set(SRC
19-
src/main.cpp
20-
src/config.cpp
21-
)
22-
23-
add_library(samples_launcher STATIC ${SRC})
24-
target_link_libraries(samples_launcher PUBLIC vkb__platform vkb__encoding)
25-
target_include_directories(samples_launcher PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
18+
add_library(samples_launcher_lib STATIC src/config.cpp)
19+
target_link_libraries(samples_launcher_lib PUBLIC vkb__platform vkb__encoding)
20+
target_include_directories(samples_launcher_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
2621

2722
vkb__register_tests(
2823
NAME samples_launcher_test
2924
SRC
3025
tests/config.test.cpp
3126
tests/config_marshaler.test.cpp
3227
LIBS
33-
samples_launcher
28+
samples_launcher_lib
29+
)
30+
31+
# samples launcher executable
32+
add_executable(samples_launcher src/main.cpp)
33+
target_link_libraries(samples_launcher PUBLIC samples_launcher_lib)
34+
35+
# compile sample information
36+
get_property(DESCRIPTORS GLOBAL PROPERTY SAMPLE_DESCRIPTORS)
37+
list(JOIN DESCRIPTORS "," JSON_ARRAY)
38+
FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/samples.json" "{\"samples\": [${JSON_ARRAY}]}")
39+
40+
# copy samples json to binary output dir
41+
add_custom_command(
42+
TARGET samples_launcher
43+
POST_BUILD
44+
COMMAND ${CMAKE_COMMAND} -E copy
45+
"${CMAKE_CURRENT_BINARY_DIR}/samples.json"
46+
"$<TARGET_FILE_DIR:samples_launcher>/samples.json"
3447
)

components/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@
1919
add_subdirectory(common)
2020
add_subdirectory(platform)
2121
add_subdirectory(encoding)
22-
add_subdirectory(platform)
2322
add_subdirectory(events)
2423
add_subdirectory(vfs)

components/platform/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@
1515
# limitations under the License.
1616
#
1717

18+
vkb__register_component(
19+
NAME platform_headers
20+
INCLUDE_DIRS
21+
${CMAKE_CURRENT_SOURCE_DIR}/include
22+
)
23+
1824
vkb__register_component(
1925
NAME platform
2026
SRC
2127
src/dl.cpp
2228
src/sample.cpp
2329
LINK_LIBS
2430
${CMAKE_DL_LIBS}
25-
INCLUDE_DIRS
26-
${CMAKE_CURRENT_SOURCE_DIR}/include
31+
vkb__platform_headers
2732
)
2833

2934
vkb__register_tests_no_catch2(

samples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
add_subdirectory(hello_sample)
17+
add_subdirectory(hello_sample)
18+
add_subdirectory(hello_triangle)

samples/hello_sample/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2020-2022, Arm Limited and Contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 the "License";
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
vkb__register_sample(
18+
NAME hello_sample
19+
SRC
20+
src/main.cpp
21+
)
22+
23+
vkb__register_sample_descriptor(
24+
NAME "Hello Sample"
25+
DESCRIPTION "This is an example sample"
26+
LIB sample__hello_sample
27+
)

samples/hello_sample/src/main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
* limitations under the License.
1616
*/
1717

18-
#include <components/platform/platform.hpp>
18+
#include <components/platform/sample_main.hpp>
1919

2020
#include <iostream>
2121

22-
CUSTOM_MAIN(context)
22+
using namespace components;
23+
24+
EXPORT_CLIB int sample_main(PlatformContext * /* context */)
2325
{
24-
std::cout << "Hello World!\n";
26+
std::cout << "Hello Sample!\n";
2527

2628
return EXIT_SUCCESS;
2729
}

samples/hello_triangle/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2020-2022, Arm Limited and Contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 the "License";
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
vkb__register_sample(
18+
NAME hello_triangle
19+
SRC
20+
src/main.cpp
21+
)
22+
23+
vkb__register_sample_descriptor(
24+
NAME "Hello Triangle"
25+
DESCRIPTION "Vulkan Hello Triangle"
26+
LIB sample__hello_triangle
27+
)

0 commit comments

Comments
 (0)