Skip to content

Commit

Permalink
Refactor DLL import and improve cmake for cross-platform compatibility
Browse files Browse the repository at this point in the history
Refactored all instances of __declspec(dllimport) to use a new DLL_IMPORT macro, which improves code readability and supports non-Windows platforms. Also overhauled the cmake configuration to better handle linking with different libraries depending on the platform and build type. This prepares our codebase for cross-platform development and deployment.
  • Loading branch information
xorza committed Jun 9, 2024
1 parent e564df8 commit b1501c9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
42 changes: 26 additions & 16 deletions ScenariumEditor.QML/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,43 @@ target_compile_options(scenarium_editor PRIVATE
-Wno-unused-const-variable
)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
target_link_libraries(scenarium_editor
PUBLIC
${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/debug/libcs_interop.dll.a
)
else ()
target_link_libraries(scenarium_editor
PUBLIC
${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/release/libcs_interop.dll.a
)
endif ()

target_include_directories(scenarium_editor PUBLIC lib)

if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_options(scenarium_editor
PUBLIC
-static -static-libgcc -static-libstdc++
)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(LIBB_CS_INTEROP_FILE "${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/debug/libcs_interop.dll.a")
set(LIBB_CS_INTEROP_DYLIB_FILE "${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/debug/cs_interop.dll")
else ()
set(LIBB_CS_INTEROP_FILE "${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/release/libcs_interop.dll.a")
set(LIBB_CS_INTEROP_DYLIB_FILE "${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/release/cs_interop.dll")
endif ()
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(LIBB_CS_INTEROP_FILE "${CMAKE_SOURCE_DIR}/../target/debug/libcs_interop.dylib")
set(LIBB_CS_INTEROP_DYLIB_FILE "${CMAKE_SOURCE_DIR}/../target/debug/libcs_interop.dylib")
else ()
set(LIBB_CS_INTEROP_FILE "${CMAKE_SOURCE_DIR}/../target/release/libcs_interop.dylib")
set(LIBB_CS_INTEROP_DYLIB_FILE "${CMAKE_SOURCE_DIR}/../target/release/libcs_interop.dylib")
endif ()
endif ()


target_link_libraries(scenarium_editor
PUBLIC
${LIBB_CS_INTEROP_FILE}
)

target_include_directories(scenarium_editor PUBLIC lib)


add_custom_command(
TARGET scenarium_editor POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/debug/cs_interop.dll
${LIBB_CS_INTEROP_DYLIB_FILE}
$<TARGET_FILE_DIR:scenarium_editor>
COMMENT "Copying DLL file to build directory"
)
Expand All @@ -107,7 +118,6 @@ install(TARGETS scenarium_editor
)



# Tests
include(FetchContent)
FetchContent_Declare(
Expand All @@ -121,7 +131,7 @@ file(GLOB_RECURSE TEST_SRC_FILES "tests/*.cpp")
add_executable(tests ${SRC_FILES} ${TEST_SRC_FILES})
target_link_libraries(tests PRIVATE
Catch2::Catch2WithMain
${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/debug/libcs_interop.dll.a
${LIBB_CS_INTEROP_FILE}
)
#list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
Expand Down
10 changes: 5 additions & 5 deletions ScenariumEditor.QML/src/CoreContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ struct FfiNode {
FfiBuf outputs;
};

__declspec(dllimport) void *create_context();
__declspec(dllimport) void destroy_context(void *ctx);
DLL_IMPORT void *create_context();
DLL_IMPORT void destroy_context(void *ctx);

__declspec(dllimport) FfiBuf get_funcs(void *ctx);
__declspec(dllimport) FfiBuf get_nodes(void *ctx);
__declspec(dllimport) FfiNode new_node(void *ctx, FfiUuid func_id);
DLL_IMPORT FfiBuf get_funcs(void *ctx);
DLL_IMPORT FfiBuf get_nodes(void *ctx);
DLL_IMPORT FfiNode new_node(void *ctx, FfiUuid func_id);

}

Expand Down
2 changes: 1 addition & 1 deletion ScenariumEditor.QML/src/utils/interop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


extern "C" {
__declspec(dllimport) void destroy_ffi_buf(FfiBuf buf);
DLL_IMPORT void destroy_ffi_buf(FfiBuf buf);
}


Expand Down
6 changes: 6 additions & 0 deletions ScenariumEditor.QML/src/utils/interop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <vector>
#include <cassert>

#ifdef _WIN32
#define DLL_IMPORT __declspec(dllimport)
#else
#define DLL_IMPORT
#endif

extern "C" {
struct FfiBuf {
void *data;
Expand Down
6 changes: 3 additions & 3 deletions ScenariumEditor.QML/src/utils/uuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "interop.hpp"

extern "C" {
__declspec(dllimport) FfiUuid uuid_new_v4_extern();
__declspec(dllimport) FfiUuid uuid_from_string_extern(FfiBuf str);
__declspec(dllimport) FfiBuf uuid_to_string_extern(FfiUuid uuid);
DLL_IMPORT FfiUuid uuid_new_v4_extern();
DLL_IMPORT FfiUuid uuid_from_string_extern(FfiBuf str);
DLL_IMPORT FfiBuf uuid_to_string_extern(FfiUuid uuid);
}

uuid uuid::new_v4() {
Expand Down

0 comments on commit b1501c9

Please sign in to comment.