Skip to content

add extension for loading metacall*.json #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ add_subdirectory(serial)
add_subdirectory(configuration)
add_subdirectory(loader)
add_subdirectory(metacall)
add_subdirectory(extensions)

# Loaders
set(IDE_FOLDER "Loaders")
Expand Down
10 changes: 10 additions & 0 deletions source/extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#Check if extension loader is enabled
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_EXT)
return()
endif()

#
# Sub-projects
#

add_subdirectory(load_extensions)
177 changes: 177 additions & 0 deletions source/extensions/load_extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Target name
set(target load_extensions)

# Exit here if required dependencies are not met
message(STATUS "Script ${target}")

# Set API export file and macro
string(TOUPPER ${target} target_upper)
set(feature_file "include/${target}/${target}_features.h")
set(export_file "include/${target}/${target}_api.h")
set(export_macro "${target_upper}_API")

#
# Compiler warnings
#

include(Warnings)

#
# Compiler security
#

include(SecurityFlags)

#
# Sources
#

set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")

set(headers
${include_path}/load_extensions.h
)

set(sources
${source_path}/load_extensions.cpp
)

# Group source files
set(header_group "Header Files (API)")
set(source_group "Source Files")
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
${header_group} ${headers})
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
${source_group} ${sources})

#
# Create library
#

# Build library
add_library(${target} MODULE
${sources}
${headers}
)

# Create namespaced alias
add_library(${META_PROJECT_NAME}::${target} ALIAS ${target})

# Export library for downstream projects
export(TARGETS ${target} NAMESPACE ${META_PROJECT_NAME}:: FILE ${PROJECT_BINARY_DIR}/cmake/${target}/${target}-export.cmake)

# Create feature detection header
# Compilers: https://cmake.org/cmake/help/v3.1/variable/CMAKE_LANG_COMPILER_ID.html#variable:CMAKE_%3CLANG%3E_COMPILER_ID
# Feature: https://cmake.org/cmake/help/v3.1/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html

# Check for availability of module; use pre-generated version if not found
if (WriterCompilerDetectionHeaderFound)
write_compiler_detection_header(
FILE ${feature_file}
PREFIX ${target_upper}
COMPILERS AppleClang Clang GNU MSVC
FEATURES cxx_alignas cxx_alignof cxx_constexpr cxx_final cxx_noexcept cxx_nullptr cxx_sizeof_member cxx_thread_local
VERSION 3.2
)
else()
file(
COPY ${PROJECT_SOURCE_DIR}/codegeneration/${target}_features.h
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include/${target}
USE_SOURCE_PERMISSIONS
)
endif()

# Create API export header
generate_export_header(${target}
EXPORT_FILE_NAME ${export_file}
EXPORT_MACRO_NAME ${export_macro}
)

#
# Project options
#

set_target_properties(${target}
PROPERTIES
${DEFAULT_PROJECT_OPTIONS}
FOLDER "${IDE_FOLDER}"
BUNDLE $<$<BOOL:${APPLE}>:$<$<VERSION_GREATER:${PROJECT_OS_VERSION},8>>>
)

#
# Include directories
#

target_include_directories(${target}
PRIVATE
${PROJECT_BINARY_DIR}/source/include
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}/include

$<TARGET_PROPERTY:${META_PROJECT_NAME}::metacall,INCLUDE_DIRECTORIES> # MetaCall includes

PUBLIC
${DEFAULT_INCLUDE_DIRECTORIES}

INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>
)

#
# Libraries
#

target_link_libraries(${target}
PRIVATE
${META_PROJECT_NAME}::metacall # MetaCall library

PUBLIC
${DEFAULT_LIBRARIES}

INTERFACE
)

#
# Compile definitions
#

target_compile_definitions(${target}
PRIVATE

PUBLIC
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:${target_upper}_STATIC_DEFINE>
${DEFAULT_COMPILE_DEFINITIONS}

INTERFACE
)

#
# Compile options
#

target_compile_options(${target}
PRIVATE

PUBLIC
${DEFAULT_COMPILE_OPTIONS}

INTERFACE
)

#
# Linker options
#

target_link_libraries(${target}
PRIVATE

PUBLIC
${DEFAULT_LINKER_OPTIONS}

INTERFACE
)

set_property(TARGET ${target} PROPERTY CXX_STANDARD 17)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef LOAD_EXTENSIONS_H
#define LOAD_EXTENSIONS_H 1

#include <dynlink/dynlink.h>
#include <load_extensions/load_extensions_api.h>

#ifdef __cplusplus
extern "C" {
#endif

LOAD_EXTENSIONS_API void load_extensions(void *loader, void *context);

DYNLINK_SYMBOL_EXPORT(load_extensions);

#ifdef __cplusplus
}
#endif

#endif //extension.h
80 changes: 80 additions & 0 deletions source/extensions/load_extensions/source/load_extensions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <load_extensions/load_extensions.h>

#include <environment/environment_variable_path.h>
#include <log/log.h>
#include <metacall/metacall.h>

#include <assert.h>

#include <filesystem>
#include <regex>
#include <string>

#define METACALL_EXTENSIONS_PATH "METACALL_EXTENSIONS_PATH" /*ENV Variable for plugin path*/

namespace fs = std::filesystem;

std::string get_ext_path()
{
/* Initialize the library path */
const char name[] = "metacall"
#if (!defined(NDEBUG) || defined(DEBUG) || defined(_DEBUG) || defined(__DEBUG) || defined(__DEBUG__))
"d"
#endif
;

dynlink_library_path_str path;
size_t length = 0;

/* The order of precedence is:
* 1) Environment variable
* 2) Dynamic link library path of the host library
*/
dynlink_library_path(name, path, &length); //TODO: check return value

char *lib_path = environment_variable_path_create(METACALL_EXTENSIONS_PATH, path, length + 1, NULL);
if (!lib_path)
{
return "";
}

fs::path tmp(lib_path);
environment_variable_path_destroy(lib_path);
tmp /= "extensions";
return tmp.string();
}

void load_extensions(void *loader, void *context)
{
std::regex metacall_json{ R"(metacall(-.+)?\.json$)" };
std::string ext_path = get_ext_path();
if (ext_path.empty())
{
/*TODO: log*/
assert(!"Failed to get metacall lib path");
}

struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };
void *config_allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);

auto i = fs::recursive_directory_iterator(ext_path);
while (i != fs::recursive_directory_iterator())
{
if (i.depth() == 2)
i.disable_recursion_pending();

fs::directory_entry dir(*i);
if (dir.is_regular_file())
{
if (std::regex_match(dir.path().filename().c_str(), metacall_json))
{
metacall_load_from_configuration(dir.path().c_str(), NULL, config_allocator);
i.pop();
continue;
}
}
i++;
}

metacall_allocator_destroy(config_allocator);
}