Skip to content

Commit 85190a5

Browse files
committed
Added cmake tooling
This CMake file was tested under linux, macOS and windows. Also added backend glfw_gl3 for samples.
1 parent 99bef62 commit 85190a5

File tree

3 files changed

+573
-3
lines changed

3 files changed

+573
-3
lines changed

CMakeLists.txt

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(netImgui)
3+
set(CMAKE_CXX_STANDARD 17)
4+
5+
6+
#
7+
# Options for the project (edit or set those)
8+
#
9+
option(NETIMGUI_BUILD_CLIENT "Build client" ON)
10+
option(NETIMGUI_BUILD_IMGUI "Build imgui from sources in netImgui" ${PROJECT_IS_TOP_LEVEL})
11+
option(NETIMGUI_BUILD_SAMPLES "Build samples" ${PROJECT_IS_TOP_LEVEL})
12+
option(NETIMGUI_BUILD_SERVER_LIB "Build server lib" ${PROJECT_IS_TOP_LEVEL})
13+
option(NETIMGUI_BUILD_SERVER_APP "Build server app" ${PROJECT_IS_TOP_LEVEL})
14+
15+
if(NETIMGUI_BUILD_SAMPLES)
16+
# Set which backend to use for the samples between WIN32_DX11 and GLFW_GL3
17+
# (select only one backend!)
18+
# (this backend is used only for the local display, the server app can use a different backend)
19+
if (NOT WIN32)
20+
set(not_win32 ON)
21+
endif()
22+
option(NETIMGUI_SAMPLES_BACKEND_WIN32_DX11 "Use Win32/DX11 backend for samples" ${WIN32})
23+
option(NETIMGUI_SAMPLES_BACKEND_GLFW_GL3 "Use GLFW/GL3 backend for samples" ${not_win32})
24+
endif()
25+
if(NETIMGUI_BUILD_SERVER_APP)
26+
# NETIMGUI_SERVER_APP_BACKEND:
27+
# Set which backend to use, between SOKOL, WIN32_DX11, and GLFW_GL3
28+
# By default, we use Sokol
29+
# (select only one backend!)
30+
option(NETIMGUI_SERVER_APP_BACKEND_SOKOL "Use Sokol backend for server app" ON)
31+
32+
# implementation to be completed below
33+
# option(NETIMGUI_SERVER_APP_BACKEND_WIN32_DX11 "Use Win32/DX11 backend for server app" OFF)
34+
# option(NETIMGUI_SERVER_APP_BACKEND_GLFW_GL3 "Use GLFW/GL3 backend for server app" OFF)
35+
endif()
36+
37+
#
38+
# Code directories
39+
#
40+
set(CODE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Code)
41+
set(CLIENT_DIR ${CODE_DIR}/Client)
42+
set(SERVER_DIR ${CODE_DIR}/ServerApp/Source)
43+
set(SAMPLES_DIR ${CODE_DIR}/Sample)
44+
set(IMGUI_DIR ${CODE_DIR}/ThirdParty/DearImgui)
45+
set(COMMON_DIR ${SAMPLES_DIR}/Common)
46+
47+
#
48+
# Add imgui
49+
#
50+
function(add_imgui)
51+
file(GLOB imgui_files ${IMGUI_DIR}/*.cpp ${IMGUI_DIR}/*.h)
52+
add_library(imgui ${imgui_files})
53+
target_include_directories(imgui PUBLIC ${IMGUI_DIR} $<BUILD_INTERFACE:${IMGUI_DIR}/backends>)
54+
# target_compile_definitions(imgui PUBLIC ImTextureID=uint64_t)
55+
endfunction()
56+
57+
#
58+
# Add netImgui Client: this is the library that will be linked to the application logic.
59+
# The client will sent display commands to the server
60+
#
61+
function(add_net_imgui_client)
62+
file(GLOB_RECURSE client_files ${CLIENT_DIR}/*.cpp ${CLIENT_DIR}/*.h)
63+
add_library(net_imgui_client ${client_files})
64+
target_include_directories(net_imgui_client PUBLIC $<BUILD_INTERFACE:${CLIENT_DIR}>)
65+
target_link_libraries(net_imgui_client PUBLIC imgui)
66+
endfunction()
67+
68+
#
69+
# Add netImgui Server lib (because several server apps can be created, using different backends)
70+
#
71+
function(add_net_imgui_server_lib)
72+
file(GLOB server_files ${SERVER_DIR}/*.cpp ${SERVER_DIR}/*.h)
73+
add_library(net_imgui_server_lib ${server_files})
74+
target_link_libraries(net_imgui_server_lib PUBLIC net_imgui_client)
75+
target_include_directories(net_imgui_server_lib PUBLIC $<BUILD_INTERFACE:${SERVER_DIR}> $<BUILD_INTERFACE:${CODE_DIR}>)
76+
77+
# Add NetImguiServer_App_Custom
78+
target_sources(net_imgui_server_lib PRIVATE ${SERVER_DIR}/Custom/NetImguiServer_App_Custom.cpp)
79+
endfunction()
80+
81+
#
82+
# Add Server app (depending on the backend)
83+
#
84+
function(_add_net_imgui_server_app_sokol)
85+
set(server_app_sokol_dir ${SERVER_DIR}/Sokol)
86+
file(GLOB server_app_sokol_files ${server_app_sokol_dir}/*.cpp)
87+
add_executable(net_imgui_server_app_sokol ${server_app_sokol_files})
88+
target_link_libraries(net_imgui_server_app_sokol net_imgui_server_lib)
89+
# Per platform settings
90+
if(APPLE)
91+
target_compile_options(net_imgui_server_app_sokol PRIVATE -x objective-c++)
92+
target_link_libraries(net_imgui_server_app_sokol "-framework Cocoa" "-framework QuartzCore" "-framework Metal" "-framework MetalKit -framework OpenGL")
93+
elseif(UNIX)
94+
target_link_libraries(net_imgui_server_app_sokol X11 GL Xcursor Xi)
95+
endif()
96+
endfunction()
97+
98+
function(_add_net_imgui_server_app_win32_dx11)
99+
message(FATAL_ERROR "Not implemented")
100+
endfunction()
101+
102+
function(_add_net_imgui_server_app_glfw_gl3)
103+
message(FATAL_ERROR "Not implemented")
104+
endfunction()
105+
106+
function(add_net_imgui_server_app)
107+
# Ensure only one backend is selected
108+
set(backend_count 0)
109+
if(NETIMGUI_SERVER_APP_BACKEND_SOKOL)
110+
math(EXPR backend_count "${backend_count}+1")
111+
endif()
112+
if(NETIMGUI_SERVER_APP_BACKEND_WIN32_DX11)
113+
math(EXPR backend_count "${backend_count}+1")
114+
endif()
115+
if(NETIMGUI_SERVER_APP_BACKEND_GLFW_GL3)
116+
math(EXPR backend_count "${backend_count}+1")
117+
endif()
118+
if(NOT backend_count EQUAL 1)
119+
message(FATAL_ERROR "Select one and only one backend for the server app")
120+
endif()
121+
122+
if(NETIMGUI_SERVER_APP_BACKEND_SOKOL)
123+
target_compile_definitions(net_imgui_server_lib PUBLIC NETIMGUI_SERVER_APP_BACKEND_SOKOL)
124+
_add_net_imgui_server_app_sokol()
125+
# elseif(NETIMGUI_SERVER_APP_BACKEND_WIN32_DX11)
126+
# target_compile_definitions(net_imgui_server_lib PUBLIC NETIMGUI_SERVER_APP_BACKEND_WIN32_DX11)
127+
# _add_net_imgui_server_app_win32_dx11()
128+
# elseif(NETIMGUI_SERVER_APP_BACKEND_GLFW_GL3)
129+
# target_compile_definitions(net_imgui_server_lib PUBLIC NETIMGUI_SERVER_APP_BACKEND_GLFW_GL3)
130+
# _add_net_imgui_server_app_glfw_gl3()
131+
endif()
132+
endfunction()
133+
134+
#
135+
# Build all Samples (in a dir whose name starts with "Sample")
136+
#
137+
function(_add_backend_to_sample sample_name)
138+
# Depending on the backend, we need to
139+
# - use different main files (fill main_file)
140+
# - link to different libraries (fill link_libraries)
141+
# - add different backend impl files (fill backend_files)
142+
if(NETIMGUI_SAMPLES_BACKEND_WIN32_DX11)
143+
add_compile_definitions(NETIMGUI_SAMPLES_BACKEND_WIN32_DX11)
144+
set(main_file ${COMMON_DIR}/main.cpp)
145+
set(backend_files ${IMGUI_DIR}/backends/imgui_impl_dx11.cpp ${IMGUI_DIR}/backends/imgui_impl_win32.cpp)
146+
set(link_libraries d3d11)
147+
148+
elseif(NETIMGUI_SAMPLES_BACKEND_GLFW_GL3)
149+
find_package(glfw3 REQUIRED)
150+
find_package(OpenGL REQUIRED)
151+
add_compile_definitions(NETIMGUI_SAMPLES_BACKEND_GLFW_GL3)
152+
set(main_file ${COMMON_DIR}/main_glfw_gl3.cpp)
153+
set(backend_files ${IMGUI_DIR}/backends/imgui_impl_glfw.cpp ${IMGUI_DIR}/backends/imgui_impl_opengl3.cpp)
154+
set(link_libraries glfw OpenGL::GL)
155+
endif()
156+
157+
target_sources(${sample_name} PRIVATE ${main_file} ${backend_files})
158+
target_link_libraries(${sample_name} PRIVATE ${link_libraries})
159+
endfunction()
160+
161+
function(_add_common_to_sample sample_name)
162+
target_sources(${sample_name} PRIVATE ${COMMON_DIR}/Sample.h ${COMMON_DIR}/Sample.cpp)
163+
target_include_directories(${sample_name} PUBLIC $<BUILD_INTERFACE:${CODE_DIR}>)
164+
endfunction()
165+
166+
function(_add_net_imgui_sample sample_name)
167+
message("Building sample: ${sample_name}")
168+
set(sample_dir ${SAMPLES_DIR}/${sample_name})
169+
file(GLOB sample_files ${sample_dir}/*.cpp ${sample_dir}/*.h)
170+
add_executable(${sample_name} ${sample_files})
171+
target_link_libraries(${sample_name} PRIVATE net_imgui_client)
172+
endfunction()
173+
174+
function(add_net_imgui_samples)
175+
file(GLOB samples_list ${SAMPLES_DIR}/Sample*)
176+
foreach (sample_dir ${samples_list})
177+
get_filename_component(sample_name ${sample_dir} NAME)
178+
179+
# SampleCompression is not supported, because
180+
# SampleNetworkWin32.cpp is problematic:
181+
# - it does not compile outside of windows (need to add #ifdef _WIN32)
182+
# - under windows, its presence will lead to link error (doubly defined functions): NetImgui::Internal::Network::Startup(void) already defined, etc.
183+
if (${sample_name} STREQUAL "SampleCompression")
184+
continue()
185+
endif()
186+
187+
_add_net_imgui_sample(${sample_name})
188+
189+
# Link backend to sample (if not SampleNoBackend)
190+
if(NOT ${sample_name} STREQUAL "SampleNoBackend")
191+
_add_common_to_sample(${sample_name})
192+
_add_backend_to_sample(${sample_name})
193+
endif()
194+
endforeach()
195+
endfunction()
196+
197+
198+
#
199+
# Main
200+
#
201+
if(NETIMGUI_BUILD_IMGUI)
202+
add_imgui()
203+
endif()
204+
if (NETIMGUI_BUILD_CLIENT)
205+
add_net_imgui_client()
206+
endif()
207+
if(NETIMGUI_BUILD_SERVER_LIB)
208+
add_net_imgui_server_lib()
209+
endif()
210+
if(NETIMGUI_BUILD_SERVER_APP)
211+
add_net_imgui_server_app()
212+
endif()
213+
if(NETIMGUI_BUILD_SAMPLES)
214+
add_net_imgui_samples()
215+
endif()

0 commit comments

Comments
 (0)