Skip to content
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

Toolchain separation #461

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand Down Expand Up @@ -86,4 +86,4 @@ jobs:
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
20 changes: 15 additions & 5 deletions AppCUI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ find_package(libjpeg-turbo CONFIG REQUIRED)
find_package(PNG REQUIRED)
find_package(Freetype REQUIRED)
find_package(SDL2 CONFIG REQUIRED)
find_package(SDL2TTF REQUIRED)
find_package(SDL2_ttf CONFIG REQUIRED)

if (UNIX)
if (APPLE)
Expand Down Expand Up @@ -113,8 +113,8 @@ target_link_libraries(${PROJECT_NAME} PRIVATE $<IF:$<TARGET_EXISTS:libjpeg-turbo
message("FREETYPE_LIBRARIES => ${FREETYPE_LIBRARIES}")
target_link_libraries(${PROJECT_NAME} PRIVATE ${FREETYPE_LIBRARIES})

# Add font used by SDL
include(CMakeRC)
# Add font used by SDL
find_package(CMakeRC CONFIG REQUIRED)

set(FONT_NAME JuliaMono-Regular.ttf)
set(FONT_PATH resources/${FONT_NAME})
Expand All @@ -125,8 +125,18 @@ message("FONT_NAME => ${FONT_NAME}")
message("FONT_PATH => ${FONT_PATH}")
message("FONT_LIB_NAME => ${FONT_LIB_NAME}")

target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIR} ${SDL2TTF_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE ${SDL2_LIBRARIES} ${SDL2TTF_LIBRARY})
target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME}
PRIVATE
$<TARGET_NAME_IF_EXISTS:SDL2::SDL2main>
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
)

target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2TTF_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME}
PRIVATE
$<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static>
)
target_link_libraries(${PROJECT_NAME} PRIVATE ${FONT_LIB_NAME})

if (APPLE AND NOT APPCUI_ENABLE_OUTPUT_LOCATION)
Expand Down
5 changes: 2 additions & 3 deletions AppCUI/include/AppCUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2351,11 +2351,10 @@ namespace OS

class EXPORT Library
{
void* libraryHandle;
void* libraryHandle{ nullptr };

public:
Library();
bool Load(const std::filesystem::path& path);
bool Load(const std::filesystem::path& path, std::string& errorMessage);
void* GetFunction(const char* functionName) const;
template <typename T>
inline T GetFunction(const char* functionName) const
Expand Down
48 changes: 23 additions & 25 deletions AppCUI/src/OS/Unix/Library.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
#include <dlfcn.h>
#include "Internal.hpp"
#include <dlfcn.h>

namespace AppCUI::OS
{
Library::Library()
bool Library::Load(const std::filesystem::path& path, std::string& errorMessage)
{
this->libraryHandle = nullptr;
}
if (libraryHandle)
{
errorMessage = "Library already opened!";
RETURNERROR(false, errorMessage.c_str());
}
libraryHandle = dlopen(path.native().c_str(), RTLD_LAZY);
if (!libraryHandle)
{
const auto error = dlerror();
errorMessage = "Fail to load library: " + path.generic_string() + " " + "[dlerror=" + error + "]!";
RETURNERROR(false, errorMessage.c_str());
}

bool Library::Load(const std::filesystem::path& path)
{
CHECK(this->libraryHandle == nullptr, false, "Library already opened !");
this->libraryHandle = dlopen(path.native().c_str(), RTLD_LAZY);
CHECK(this->libraryHandle,
false,
"Fail to load library: %s [dlerror=%s]",
path.generic_string().c_str(),
dlerror());
return true;
}

void* Library::GetFunction(const char* functionName) const
void* Library::GetFunction(const char* name) const
{
CHECK(this->libraryHandle, nullptr, "Library was not loaded --> have you call Load(...) first ?");
CHECK(functionName, nullptr, "Expecting a valid (non-null) function name !");
CHECK(*functionName, nullptr, "Expecting a valid (non-empty) function name !");
CHECK(libraryHandle, nullptr, "Library was not loaded --> have you call Load(...) first ?");
CHECK(name, nullptr, "Expecting a valid (non-null) function name !");
CHECK(*name, nullptr, "Expecting a valid (non-empty) function name !");

// all good
void* fnPtr = dlsym(libraryHandle, functionName);
const char* dlsym_error = dlerror();
CHECK((dlsym_error == nullptr) && (fnPtr),
nullptr,
"Unable to find address of function: %s [dlerror=%s]",
functionName,
dlsym_error);
void* fnPtr = dlsym(libraryHandle, name);
const char* error = dlerror();
CHECK(!error && fnPtr, nullptr, "Unable to find address of function: %s [dlerror=%s]", name, error);
return fnPtr;
}
}
} // namespace AppCUI::OS
44 changes: 25 additions & 19 deletions AppCUI/src/OS/Windows/Library.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
#include "Internal.hpp"
#include <system_error>

namespace AppCUI::OS
{
Library::Library()
bool Library::Load(const std::filesystem::path& path, std::string& errorMessage)
{
this->libraryHandle = nullptr;
}
bool Library::Load(const std::filesystem::path& path)
{
CHECK(this->libraryHandle == nullptr, false, "Library already opened !");
this->libraryHandle = LoadLibraryW(path.native().c_str());
CHECK(this->libraryHandle,
false,
"Error: %u! Fail to load library: %s",
GetLastError(),
path.generic_string().c_str());
if (libraryHandle)
{
errorMessage = "Library already opened!";
RETURNERROR(false, errorMessage.c_str());
}
libraryHandle = LoadLibraryW(path.native().c_str());
if (!libraryHandle)
{
const auto code = GetLastError();
std::string sCode = std::to_string(code);
errorMessage = "(" + sCode + ") " + std::system_category().message(code);
RETURNERROR(false, errorMessage.c_str());
}

return true;
}
void* Library::GetFunction(const char* functionName) const

void* Library::GetFunction(const char* name) const
{
CHECK(this->libraryHandle, nullptr, "Library was not loaded --> have you call Load(...) first ?");
CHECK(functionName, nullptr, "Expecting a valid (non-null) function name !");
CHECK(*functionName, nullptr, "Expecting a valid (non-empty) function name !");
CHECK(libraryHandle, nullptr, "Library was not loaded --> have you call Load(...) first ?");
CHECK(name, nullptr, "Expecting a valid (non-null) function name !");
CHECK(*name, nullptr, "Expecting a valid (non-empty) function name !");

// all good
void* fnPtr = GetProcAddress((HMODULE) this->libraryHandle, functionName);
CHECK(fnPtr, nullptr, "Unable to find address of function: %s", functionName);
void* fnPtr = GetProcAddress((HMODULE) libraryHandle, name);
CHECK(fnPtr, nullptr, "Unable to find address of function: %s", name);
return fnPtr;
}
} // namespace AppCUI::OS
} // namespace AppCUI::OS
Loading
Loading