Skip to content

Commit

Permalink
[build] add a single nicegraf target that pulls in the correct backend
Browse files Browse the repository at this point in the history
  • Loading branch information
nicebyte committed Dec 26, 2023
1 parent ad6c01e commit 3b8f3aa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 37 deletions.
22 changes: 13 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,15 @@ if (NGF_BUILD_TESTS STREQUAL "yes")
DEPS nicegraf-vk-for-tests "$<IF:$<NOT:$<BOOL:${WIN32}>>,dl,>")
endif()

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/misc/common)

# Build samples only if explicitly requested.
if (NGF_BUILD_SAMPLES STREQUAL "yes")
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/samples/deps/glfw)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/misc/common)
nmk_header_library(NAME nicegraf
PUB_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/../../include
PUB_DEPS ${NGF_BACKEND})


# Ensure we have the required dependencies are available.
if (TARGET glfw)
Expand Down Expand Up @@ -170,9 +175,6 @@ if (NGF_BUILD_SAMPLES STREQUAL "yes")
SRCS ${shader_files})
set_target_properties(sample-shaders PROPERTIES FOLDER "samples")

string(TOUPPER "NGF_SAMPLES_BACKEND_${NGF_SAMPLES_BACKEND}" NGF_SAMPLES_BACKEND_DEFINE)
string(REPLACE "-" "_" NGF_SAMPLES_BACKEND_DEFINE ${NGF_SAMPLES_BACKEND_DEFINE})

set(NGF_IMGUI_SRCS ${CMAKE_CURRENT_LIST_DIR}/samples/deps/imgui/imgui.cpp
${CMAKE_CURRENT_LIST_DIR}/samples/deps/imgui/imgui_draw.cpp
${CMAKE_CURRENT_LIST_DIR}/samples/deps/imgui/imgui_tables.cpp
Expand All @@ -193,10 +195,11 @@ if (NGF_BUILD_SAMPLES STREQUAL "yes")
nmk_static_library(NAME ngf-imgui
SRCS ${NGF_IMGUI_SRCS}
DEPS glfw
PVT_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/samples/deps/imgui
PUB_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/samples/deps/imgui
${CMAKE_CURRENT_LIST_DIR}/samples/deps/imgui/backends
PVT_DEFINES "GLFW_INCLUDE_NONE"
COPTS ${NGF_IMGUI_COPTS})
PVT_DEFINES "GLFW_INCLUDE_NONE"
COPTS ${NGF_IMGUI_COPTS})
nmk_static_library(NAME ngf-samples-common
SRCS ${CMAKE_CURRENT_LIST_DIR}/samples/common/main.cpp
${CMAKE_CURRENT_LIST_DIR}/samples/common/diagnostic-callback.cpp
Expand All @@ -210,19 +213,20 @@ if (NGF_BUILD_SAMPLES STREQUAL "yes")
${CMAKE_CURRENT_LIST_DIR}/samples/common/camera-controller.cpp
${NGF_PLATFORM_SOURCE}
DEPS ngf-imgui glfw nicegraf-misc-common
PUB_DEFINES ${NGF_SAMPLES_BACKEND_DEFINE}
PVT_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/samples/deps/nicemath
PUB_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/samples/common
${CMAKE_CURRENT_LIST_DIR}/samples/deps/nicemath)
set_target_properties(ngf-samples-common PROPERTIES FOLDER "samples")

function (ngf_sample)
cmake_parse_arguments(SAMPLE "" "NAME" "" ${ARGN})
file(GLOB_RECURSE SAMPLE_SRCS ${CMAKE_CURRENT_LIST_DIR}/samples/${SAMPLE_NAME}/*.cpp)

nmk_binary(NAME ${SAMPLE_NAME}
SRCS ${SAMPLE_SRCS}
DEPS ngf-samples-common ${NGF_SAMPLES_BACKEND} nicegraf-util
DEPS nicegraf ngf-samples-common nicegraf-misc-common nicegraf-util ngf-imgui
PVT_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/samples/${SAMPLE_NAME}
PVT_DEFINES "GLFW_INCLUDE_NONE"
PVT_DEFINES "GLFW_INCLUDE_NONE"
OUTPUT_DIR "${NGF_SAMPLES_OUTPUT_DIR}")
add_dependencies(${SAMPLE_NAME} sample-shaders)
set_target_properties(${SAMPLE_NAME} PROPERTIES FOLDER "samples")
Expand Down
55 changes: 40 additions & 15 deletions build-utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SOFTWARE.
# PUB_DEFINES - a list of preprocessor definitions to add to all targets depending on this target.
# OUTPUT_DIR - the path to the folder where the output for this target shall be stored.
function (nmk_target)
cmake_parse_arguments(TGT "" "NAME;TYPE" "SRCS;DEPS;COPTS;PUB_INCLUDES;PVT_INCLUDES;PUB_DEFINES;PVT_DEFINES;OUTPUT_DIR;VS_DEBUGGER_WORKING_DIR" ${ARGN})
cmake_parse_arguments(TGT "" "NAME;TYPE" "SRCS;DEPS;COPTS;PUB_INCLUDES;PVT_INCLUDES;PUB_DEFINES;PUB_DEPS;PVT_DEFINES;OUTPUT_DIR;VS_DEBUGGER_WORKING_DIR" ${ARGN})
if (TGT_TYPE STREQUAL "lib")
add_library(${TGT_NAME} STATIC ${TGT_SRCS})
elseif(TGT_TYPE STREQUAL "hdr")
Expand All @@ -44,25 +44,50 @@ function (nmk_target)
else()
message(FATAL_ERROR "invalid target type")
endif()

# Add dependencies.
if ( TGT_DEPS )
target_link_libraries(${TGT_NAME} ${TGT_DEPS})
target_link_libraries(${TGT_NAME} PRIVATE ${TGT_DEPS})
endif()
if ( TGT_TYPE STREQUAL "hdr")
target_include_directories(${TGT_NAME}
INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include ${TGT_PUB_INCLUDES})
target_compile_definitions(${TGT_NAME} INTERFACE ${TGT_PUB_DEFINES} ${TGT_PVT_DEFINES})
else()
target_include_directories(${TGT_NAME}
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/source ${TGT_PVT_INCLUDES})
if ( TGT_PUB_DEPS )
target_link_libraries(${TGT_NAME} INTERFACE ${TGT_PUB_DEPS})
endif()

# Add include directories.
if ( TGT_PUB_INCLUDES )
target_include_directories(${TGT_NAME}
INTERFACE ${TGT_PUB_INCLUDES})
endif()
if ( TGT_PVT_INCLUDES )
target_include_directories(${TGT_NAME}
PRIVATE ${TGT_PVT_INCLUDES})
endif()
if ( NOT ( TGT_TYPE STREQUAL "hdr" ) )
target_include_directories(${TGT_NAME}
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include ${TGT_PUB_INCLUDES})
target_compile_options(${TGT_NAME} PRIVATE ${NICEMAKE_COMMON_COMPILE_OPTS})
target_compile_definitions(${TGT_NAME} PUBLIC ${TGT_PUB_DEFINES} PRIVATE ${TGT_PVT_DEFINES})
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/source ${CMAKE_CURRENT_LIST_DIR}/include)
endif()
target_include_directories(${TGT_NAME}
INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)

# Add compile-time definitions.
if ( TGT_PUB_DEFINES )
target_compile_definitions(${TGT_NAME} INTERFACE ${TGT_PUB_DEFINES})
endif()
if ( TGT_PVT_DEFINES )
target_compile_definitions(${TGT_NAME} PRIVATE ${TGT_PVT_DEFINES})
endif()

if ( TGT_COPTS )
target_compile_options(${TGT_NAME} PRIVATE ${TGT_COPTS})

# Add compiler options.
if ( NOT ( TGT_TYPE STREQUAL "hdr" ) )
if ( NICEMAKE_COMMON_COMPILE_OPTS )
target_compile_options(${TGT_NAME} PRIVATE ${NICEMAKE_COMMON_COMPILE_OPTS})
endif()
if ( TGT_COPTS )
target_compile_options(${TGT_NAME} PRIVATE ${TGT_COPTS})
endif()
endif()

# Set output directory.
if( TGT_OUTPUT_DIR )
set_target_properties(${TGT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${TGT_OUTPUT_DIR}")
Expand Down
34 changes: 21 additions & 13 deletions misc/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,27 @@ elseif(UNIX AND NOT APPLE)
else()
message(FATAL_ERROR "Your platform is not currently supported by nicegraf.")
endif()

nmk_header_library(NAME nicegraf
PUB_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/../../include
PUB_DEPS ${NGF_BACKEND})


string(TOUPPER "NGF_BACKEND_${NGF_BACKEND}" NGF_BACKEND_DEFINE)
string(REPLACE "-" "_" NGF_BACKEND_DEFINE ${NGF_BACKEND_DEFINE})

nmk_static_library(NAME nicegraf-misc-common
SRCS ${CMAKE_CURRENT_LIST_DIR}/shader-loader.h
${CMAKE_CURRENT_LIST_DIR}/shader-loader.cpp
${CMAKE_CURRENT_LIST_DIR}/file-utils.h
${CMAKE_CURRENT_LIST_DIR}/file-utils.cpp
${CMAKE_CURRENT_LIST_DIR}/mesh-loader.cpp
${CMAKE_CURRENT_LIST_DIR}/mesh-loader.h
${CMAKE_CURRENT_LIST_DIR}/targa-loader.cpp
${CMAKE_CURRENT_LIST_DIR}/targa-loader.h
${CMAKE_CURRENT_LIST_DIR}/logging.h
DEPS ${NGF_BACKEND}
PUB_DEFINES ${NGF_BACKEND_DEFINE}
PUB_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/)
if (NGF_BUILD_SAMPLES STREQUAL "yes")
nmk_static_library(NAME nicegraf-misc-common
SRCS ${CMAKE_CURRENT_LIST_DIR}/shader-loader.h
${CMAKE_CURRENT_LIST_DIR}/shader-loader.cpp
${CMAKE_CURRENT_LIST_DIR}/file-utils.h
${CMAKE_CURRENT_LIST_DIR}/file-utils.cpp
${CMAKE_CURRENT_LIST_DIR}/mesh-loader.cpp
${CMAKE_CURRENT_LIST_DIR}/mesh-loader.h
${CMAKE_CURRENT_LIST_DIR}/targa-loader.cpp
${CMAKE_CURRENT_LIST_DIR}/targa-loader.h
${CMAKE_CURRENT_LIST_DIR}/logging.h
DEPS nicegraf
PVT_DEFINES ${NGF_BACKEND_DEFINE}
PUB_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/)
endif()

0 comments on commit 3b8f3aa

Please sign in to comment.