Conditionally compile different drivers

This commit is contained in:
2025-03-28 18:13:19 +10:00
parent 211eb9a1d6
commit 40cfa9901e
7 changed files with 117 additions and 37 deletions

View File

@@ -41,9 +41,15 @@ endif ()
if (WIN32) if (WIN32)
add_compile_definitions( add_compile_definitions(
_CRT_SECURE_NO_WARNINGS NOVA_WINDOWS
NOMINMAX NOMINMAX
WIN32_LEAN_AND_MEAN WIN32_LEAN_AND_MEAN
_CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_WARNINGS
)
else ()
add_compile_definitions(
NOVA_LINUX
) )
endif () endif ()

View File

@@ -1,9 +1,55 @@
# Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com> # Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
find_package(Vulkan REQUIRED)
include(FetchContent) include(FetchContent)
set(NOVA_VULKAN ON CACHE BOOL "Enable Vulkan support")
set(NOVA_WAYLAND ON CACHE BOOL "Enable Wayland support")
set(NOVA_X11 ON CACHE BOOL "Enable X11 support")
set(ENGINE_SRC
core/debug.cpp
platform/system.cpp
render/renderer.cpp
render/render_device.cpp
)
if (WIN32)
set(ENGINE_SRC
${ENGINE_SRC}
drivers/windows/system_driver.cpp
)
set(NOVA_WAYLAND OFF)
set(NOVA_X11 OFF)
else ()
set(ENGINE_SRC
${ENGINE_SRC}
drivers/linux/system_driver.cpp
)
endif ()
if (NOVA_VULKAN)
find_package(Vulkan REQUIRED)
set(ENGINE_SRC
${ENGINE_SRC}
drivers/vulkan/render_driver.cpp
)
endif ()
if (NOVA_WAYLAND)
set(ENGINE_SRC
${ENGINE_SRC}
drivers/linux/wayland/system_driver.cpp
)
endif ()
if (NOVA_X11)
set(ENGINE_SRC
${ENGINE_SRC}
drivers/linux/x11/system_driver.cpp
)
endif ()
list(TRANSFORM ENGINE_SRC PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/src/)
FetchContent_Declare( FetchContent_Declare(
spdlog spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git GIT_REPOSITORY https://github.com/gabime/spdlog.git
@@ -20,23 +66,9 @@ FetchContent_Declare(
) )
FetchContent_MakeAvailable(SDL3) FetchContent_MakeAvailable(SDL3)
set(ENGINE_SRC
core/debug.cpp
drivers/vulkan/render_driver.cpp
drivers/windows/system_driver.cpp
drivers/linux/system_driver.cpp
drivers/linux/wayland/system_driver.cpp
drivers/linux/x11/system_driver.cpp
platform/system.cpp
render/renderer.cpp
render/render_device.cpp
)
list(TRANSFORM ENGINE_SRC PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/src/)
set(ENGINE_INCLUDES_PRIVATE set(ENGINE_INCLUDES_PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/src
${Vulkan_INCLUDE_DIRS} $<$<BOOL:${NOVA_VULKAN}>:${Vulkan_INCLUDE_DIRS}>
${SDL3_INCLUDE_DIRS} ${SDL3_INCLUDE_DIRS}
) )
set(ENGINE_INCLUDES_PUBLIC set(ENGINE_INCLUDES_PUBLIC
@@ -44,12 +76,17 @@ set(ENGINE_INCLUDES_PUBLIC
${spdlog_INCLUDE_DIRS} ${spdlog_INCLUDE_DIRS}
) )
set(ENGINE_LIBS_PRIVATE set(ENGINE_LIBS_PRIVATE
Vulkan::Vulkan $<$<BOOL:${NOVA_VULKAN}>:Vulkan::Vulkan>
SDL3::SDL3 SDL3::SDL3
) )
set(ENGINE_LIBS_PUBLIC set(ENGINE_LIBS_PUBLIC
spdlog::spdlog spdlog::spdlog
) )
set(ENGINE_DEFS
$<$<BOOL:${NOVA_VULKAN}>:NOVA_VULKAN>
$<$<BOOL:${NOVA_WAYLAND}>:NOVA_WAYLAND>
$<$<BOOL:${NOVA_X11}>:NOVA_X11>
)
if (NOVA_ENGINE_SHARED) if (NOVA_ENGINE_SHARED)
add_library(nova SHARED ${ENGINE_SRC}) add_library(nova SHARED ${ENGINE_SRC})
@@ -63,6 +100,7 @@ if (NOVA_ENGINE_SHARED)
) )
target_compile_definitions(nova PRIVATE target_compile_definitions(nova PRIVATE
NOVA_DLL_EXPORT NOVA_DLL_EXPORT
${ENGINE_DEFS}
) )
endif () endif ()
if (NOVA_ENGINE_STATIC) if (NOVA_ENGINE_STATIC)
@@ -75,4 +113,7 @@ if (NOVA_ENGINE_STATIC)
PUBLIC ${ENGINE_LIBS_PUBLIC} PUBLIC ${ENGINE_LIBS_PUBLIC}
PRIVATE ${ENGINE_LIBS_PRIVATE} PRIVATE ${ENGINE_LIBS_PRIVATE}
) )
target_compile_definitions(nova_static PRIVATE
${ENGINE_DEFS}
)
endif () endif ()

View File

@@ -7,7 +7,7 @@
#pragma once #pragma once
#ifndef NOVA_API #ifndef NOVA_API
#ifdef WIN32 #ifdef NOVA_WINDOWS
#ifdef NOVA_DLL_EXPORT #ifdef NOVA_DLL_EXPORT
#define NOVA_API __declspec(dllexport) #define NOVA_API __declspec(dllexport)
#elif NOVA_DLL_IMPORT #elif NOVA_DLL_IMPORT
@@ -15,7 +15,7 @@
#else #else
#define NOVA_API #define NOVA_API
#endif #endif
#else #elif NOVA_LINUX
#ifdef NOVA_DLL_EXPORT #ifdef NOVA_DLL_EXPORT
#define NOVA_API __attribute__((visibility("default"))) #define NOVA_API __attribute__((visibility("default")))
#else #else

View File

@@ -4,10 +4,17 @@
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
*/ */
#include "drivers/linux/system_driver.h" #ifdef NOVA_WAYLAND
#include "drivers/linux/wayland/system_driver.h"
#endif
#ifdef NOVA_X11
#include "drivers/linux/x11/system_driver.h"
#endif
#include <nova/core/debug.h> #include <nova/core/debug.h>
#include "drivers/linux/system_driver.h"
using namespace Nova; using namespace Nova;
LinuxSystemDriver::LinuxSystemDriver() { LinuxSystemDriver::LinuxSystemDriver() {
@@ -17,3 +24,21 @@ LinuxSystemDriver::LinuxSystemDriver() {
LinuxSystemDriver::~LinuxSystemDriver() { LinuxSystemDriver::~LinuxSystemDriver() {
NOVA_AUTO_TRACE(); NOVA_AUTO_TRACE();
} }
std::unique_ptr<SystemDriver> LinuxSystemDriver::get_default_driver() {
NOVA_AUTO_TRACE();
#ifdef NOVA_WAYLAND
if (std::getenv("WAYLAND_DISPLAY")) {
return std::make_unique<WaylandSystemDriver>();
}
#endif
#ifdef NOVA_X11
if (std::getenv("DISPLAY")) {
return std::make_unique<X11SystemDriver>();
}
#endif
NOVA_WARN("Unsupported display server");
return std::make_unique<LinuxSystemDriver>();
}

View File

@@ -8,10 +8,14 @@
#include <nova/platform/system_driver.h> #include <nova/platform/system_driver.h>
#include <memory>
namespace Nova { namespace Nova {
class LinuxSystemDriver : public SystemDriver { class LinuxSystemDriver : public SystemDriver {
public: public:
LinuxSystemDriver(); LinuxSystemDriver();
~LinuxSystemDriver() override; ~LinuxSystemDriver() override;
static std::unique_ptr<SystemDriver> get_default_driver();
}; };
} // namespace Nova } // namespace Nova

View File

@@ -4,12 +4,17 @@
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
*/ */
#ifdef NOVA_WINDOWS
#include "drivers/windows/system_driver.h"
#endif
#ifdef NOVA_LINUX
#include "drivers/linux/system_driver.h"
#endif
#include <nova/core/debug.h> #include <nova/core/debug.h>
#include <nova/platform/system.h> #include <nova/platform/system.h>
#include "drivers/linux/wayland/system_driver.h" #include <memory>
#include "drivers/linux/x11/system_driver.h"
#include "drivers/windows/system_driver.h"
using namespace Nova; using namespace Nova;
@@ -19,17 +24,12 @@ void System::init() {
NOVA_AUTO_TRACE(); NOVA_AUTO_TRACE();
NOVA_ASSERT(!s_driver); NOVA_ASSERT(!s_driver);
#ifdef WIN32 #ifdef NOVA_WINDOWS
s_driver = std::make_unique<WindowsSystemDriver>(); s_driver = std::make_unique<WindowsSystemDriver>();
#elif NOVA_LINUX
s_driver = LinuxSystemDriver::get_default_driver();
#else #else
if (std::getenv("WAYLAND_DISPLAY")) { throw std::runtime_error("Unsupported platform");
s_driver = std::make_unique<WaylandSystemDriver>();
} else if (std::getenv("DISPLAY")) {
s_driver = std::make_unique<X11SystemDriver>();
} else {
NOVA_ERROR("Unsupported windowing system");
s_driver = std::make_unique<LinuxSystemDriver>();
}
#endif #endif
} }

View File

@@ -4,13 +4,15 @@
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
*/ */
#ifdef NOVA_VULKAN
#include "drivers/vulkan/render_driver.h"
#endif
#include <nova/core/debug.h> #include <nova/core/debug.h>
#include <nova/render/renderer.h> #include <nova/render/renderer.h>
#include <memory> #include <memory>
#include "drivers/vulkan/render_driver.h"
using namespace Nova; using namespace Nova;
static std::unique_ptr<RenderDriver> s_driver; static std::unique_ptr<RenderDriver> s_driver;
@@ -18,13 +20,15 @@ static std::unique_ptr<RenderDriver> s_driver;
void Renderer::init(const RenderAPI api) { void Renderer::init(const RenderAPI api) {
NOVA_AUTO_TRACE(); NOVA_AUTO_TRACE();
NOVA_ASSERT(!s_driver); NOVA_ASSERT(!s_driver);
switch (api) { switch (api) {
#ifdef NOVA_VULKAN
case RenderAPI::VULKAN: case RenderAPI::VULKAN:
s_driver = std::make_unique<VulkanRenderDriver>(); s_driver = std::make_unique<VulkanRenderDriver>();
break; break;
#endif
default: default:
NOVA_ERROR("Unsupported render API"); throw std::runtime_error("Unsupported render API");
break;
} }
} }