Skip to content

Commit

Permalink
Add NodeJS support for load_from_package and execution_path, add test…
Browse files Browse the repository at this point in the history
… with libgit2, solve issues c_loader.
  • Loading branch information
viferga committed Jan 14, 2025
1 parent 8c9d615 commit 5b592ac
Show file tree
Hide file tree
Showing 11 changed files with 711 additions and 8 deletions.
78 changes: 78 additions & 0 deletions cmake/FindLibGit2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# CMake Find LibGit2 Library by Parra Studios
# Copyright (C) 2016 - 2025 Vicente Eduardo Ferrer Garcia <[email protected]>
#

# Find libgit2 library and include paths
#
# LibGit2_FOUND - True if LibGit2 was found
# LibGit2_INCLUDE_DIR - LibGit2 headers path
# LibGit2_VERSION - LibGit2 version
# LibGit2_VERSION_MAJOR - LibGit2 major version
# LibGit2_VERSION_MINOR - LibGit2 minor version
# LibGit2_VERSION_REVISION - LibGit2 patch version
# LibGit2_LIBRARY - LibGit2 shared library
# LibGit2_LIBRARY_DIR - LibGit2 shared library folder
#

# Prevent vervosity if already included
if(LibGit2_LIBRARY)
set(LibGit2_FIND_QUIETLY TRUE)
endif()

# Include package manager
include(FindPackageHandleStandardArgs)

# Find via PkgConfig
find_package(PkgConfig QUIET)
pkg_check_modules(PKG_GIT2 QUIET libgit2)

if(NOT LibGit2_DEFINITIONS)
set(LibGit2_DEFINITIONS ${PKG_GIT2_CFLAGS_OTHER})
endif()

if(NOT LibGit2_INCLUDE_DIR)
find_path(LibGit2_INCLUDE_DIR
NAMES git2.h
HINTS ${PKG_GIT2_INCLUDE_DIRS}
)
endif()

if(NOT LibGit2_VERSION AND LibGit2_INCLUDE_DIR)
file(STRINGS "${LibGit2_INCLUDE_DIR}/git2/version.h" LibGit2_VERSION_MAJOR REGEX "^#define LIBGIT2_VER_MAJOR +([0-9]+)")
string(REGEX MATCH "([0-9]+)$" LibGit2_VERSION_MAJOR ${LibGit2_VERSION_MAJOR})

file(STRINGS "${LibGit2_INCLUDE_DIR}/git2/version.h" LibGit2_VERSION_MINOR REGEX "^#define LIBGIT2_VER_MINOR +([0-9]+)")
string(REGEX MATCH "([0-9]+)$" LibGit2_VERSION_MINOR ${LibGit2_VERSION_MINOR})

file(STRINGS "${LibGit2_INCLUDE_DIR}/git2/version.h" LibGit2_VERSION_REVISION REGEX "^#define LIBGIT2_VER_REVISION +([0-9]+)")
string(REGEX MATCH "([0-9]+)$" LibGit2_VERSION_REVISION ${LibGit2_VERSION_REVISION})

set(LibGit2_VERSION "${LibGit2_VERSION_MAJOR}.${LibGit2_VERSION_MINOR}.${LibGit2_VERSION_REVISION}")
endif()

if(NOT LibGit2_LIBRARY)
find_library(LibGit2_LIBRARY
NAMES git2
HINTS ${PKG_GIT2_LIBRARY_DIRS}
)
endif()

set(LibGit2_LIBRARIES ${LibGit2_LIBRARY})
set(LibGit2_INCLUDE_DIRS ${LibGit2_INCLUDE_DIR})
get_filename_component(LibGit2_LIBRARY_DIR ${LibGit2_LIBRARY} DIRECTORY)

# Define package
find_package_handle_standard_args(LibGit2
FOUND_VAR
LibGit2_FOUND
REQUIRED_VARS
LibGit2_LIBRARY
LibGit2_LIBRARY_DIR
LibGit2_INCLUDE_DIR
LibGit2_INCLUDE_DIRS
VERSION_VAR
LibGit2_VERSION
)

mark_as_advanced(LibGit2_LIBRARY LibGit2_LIBRARY_DIR LibGit2_INCLUDE_DIR)
47 changes: 41 additions & 6 deletions source/loaders/c_loader/source/c_loader_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ typedef struct loader_impl_c_handle_base_type

virtual ~loader_impl_c_handle_base_type() {}

virtual bool recursive_includes() = 0;

virtual int discover(loader_impl impl, context ctx) = 0;

virtual const void *symbol(std::string &name) = 0;
Expand Down Expand Up @@ -136,6 +138,11 @@ typedef struct loader_impl_c_handle_tcc_type : loader_impl_c_handle_base_type
}
}

bool recursive_includes()
{
return false;
}

bool initialize(loader_impl_c c_impl)
{
this->state = tcc_new();
Expand Down Expand Up @@ -232,6 +239,11 @@ typedef struct loader_impl_c_handle_dynlink_type : loader_impl_c_handle_base_typ
}
}

bool recursive_includes()
{
return true;
}

bool initialize(loader_impl_c c_impl, const loader_path path)
{
std::string lib_path_str(path);
Expand Down Expand Up @@ -1022,12 +1034,18 @@ static int c_loader_impl_discover_signature(loader_impl impl, loader_impl_c_hand
symbol_name.insert(0, 1, '_');
#endif

if (scope_get(sp, symbol_name.c_str()) != NULL)
{
log_write("metacall", LOG_LEVEL_WARNING, "Symbol '%s' redefined, skipping the function", func_name.c_str());
return 0;
}

const void *address = c_handle->symbol(symbol_name);

if (address == NULL)
{
log_write("metacall", LOG_LEVEL_ERROR, "Symbol '%s' not found, skipping the function", func_name.c_str());
return 1;
log_write("metacall", LOG_LEVEL_WARNING, "Symbol '%s' not found, skipping the function", func_name.c_str());
return 0;
}

loader_impl_c_function c_function = new loader_impl_c_function_type(address);
Expand Down Expand Up @@ -1080,9 +1098,13 @@ static CXChildVisitResult c_loader_impl_discover_visitor(CXCursor cursor, CXCurs
{
c_loader_impl_discover_visitor_data visitor_data = static_cast<c_loader_impl_discover_visitor_data>(data);

if (clang_Location_isFromMainFile(clang_getCursorLocation(cursor)) == 0)
/* Include recursively when disabled, include only the header inlcuded is populated when enabled */
if (visitor_data->c_handle->recursive_includes() == false)
{
return CXChildVisit_Continue;
if (clang_Location_isFromMainFile(clang_getCursorLocation(cursor)) == 0)
{
return CXChildVisit_Continue;
}
}

CXCursorKind kind = clang_getCursorKind(cursor);
Expand All @@ -1102,19 +1124,32 @@ static CXChildVisitResult c_loader_impl_discover_visitor(CXCursor cursor, CXCurs

static int c_loader_impl_discover_ast(loader_impl impl, loader_impl_c_handle_base c_handle, context ctx)
{
loader_impl_c c_impl = static_cast<loader_impl_c>(loader_impl_get(impl));
c_loader_impl_discover_visitor_data_type data = {
impl,
c_handle,
context_scope(ctx),
0
};

std::vector<std::string> includes;
std::vector<const char *> command_line_args;

/* Otherwise, check the execution paths */
for (auto exec_path : c_impl->execution_paths)
{
includes.push_back("-I" + exec_path);
command_line_args.push_back(includes.back().c_str());
}

for (std::string file : c_handle->files)
{
CXIndex index = clang_createIndex(0, 0);
/* Define the command line arguments (simulating compiler flags) */
CXIndex index = clang_createIndex(0, 1);
CXTranslationUnit unit = clang_parseTranslationUnit(
index,
file.c_str(), nullptr, 0,
file.c_str(),
command_line_args.data(), command_line_args.size(),
nullptr, 0,
CXTranslationUnit_None);

Expand Down
Loading

0 comments on commit 5b592ac

Please sign in to comment.