Skip to content

Commit

Permalink
fix: define entrypoint in header
Browse files Browse the repository at this point in the history
Avoids optimization problems
  • Loading branch information
Curve committed Jul 12, 2023
1 parent bc29ff6 commit 3c4471c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 81 deletions.
36 changes: 7 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,8 @@ project(lime LANGUAGES CXX VERSION 2.0)
# Library options
# --------------------------------------------------------------------------------------------------------

option(lime_tests "Compile tests" OFF)
set(lime_entrypoint "OFF" CACHE STRING "Wether or not to enable the cross-platform entry-point")

# --------------------------------------------------------------------------------------------------------
# Ensure valid entrypoint options
# --------------------------------------------------------------------------------------------------------

set(lime_entrypoints OFF ON static native)
set_property(CACHE lime_entrypoint PROPERTY STRINGS ${lime_entrypoints})

if (NOT lime_entrypoint IN_LIST lime_entrypoints)
message(FATAL_ERROR "Bad Entrypoint, expected one of ${lime_entrypoints}")
endif()

if (lime_entrypoint STREQUAL "ON")
set(lime_entrypoint "native")
message(STATUS "[lime] Entrypoint was set to 'ON', using 'native' as default")
endif()
option(lime_tests "Compile tests" OFF)
option(lime_static_entrypoint "Use platform independent entrypoint implementation" OFF)

# --------------------------------------------------------------------------------------------------------
# CMake options
Expand Down Expand Up @@ -104,27 +88,21 @@ target_link_libraries(${PROJECT_NAME} PRIVATE Zydis)

if (UNIX AND NOT MINGW)
file(GLOB src "src/*.linux*.cpp")
list(FILTER src EXCLUDE REGEX "entrypoint")
target_sources(${PROJECT_NAME} PRIVATE ${src})
endif()

if (WIN32)
file(GLOB src "src/*.win*.cpp")
list(FILTER src EXCLUDE REGEX "entrypoint")
target_sources(${PROJECT_NAME} PRIVATE ${src})
target_compile_definitions(${PROJECT_NAME} PRIVATE NOMINMAX)
endif()

if (lime_entrypoint STREQUAL "native")
if (WIN32)
target_sources(${PROJECT_NAME} PRIVATE "src/entrypoint.win.cpp")
else()
target_sources(${PROJECT_NAME} PRIVATE "src/entrypoint.linux.cpp")
endif()
endif()
# --------------------------------------------------------------------------------------------------------
# Setup cross-platform entrypoint
# --------------------------------------------------------------------------------------------------------

if (lime_entrypoint STREQUAL "static")
target_sources(${PROJECT_NAME} PRIVATE "src/entrypoint.static.cpp")
if (lime_static_entrypoint)
target_compile_definitions(${PROJECT_NAME} PUBLIC LIME_STATIC_ENTRYPOINT)
endif()

# --------------------------------------------------------------------------------------------------------
Expand Down
52 changes: 51 additions & 1 deletion include/lime/entrypoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,54 @@ namespace lime
{
extern void load();
extern void unload();
} // namespace lime
} // namespace lime

#if LIME_STATIC_ENTRYPOINT
#include <memory>

// NOLINTNEXTLINE(cert-err58-cpp, *-namespace)
[[maybe_unused]] static auto constructor = []() {
lime::load();
return 1;
}();

// NOLINTNEXTLINE(cert-err58-cpp, *-namespace)
[[maybe_unused]] static auto destructor = []() {
return std::shared_ptr<char>(new char, [](auto *data) {
lime::unload();
delete data;
});
}();
#elif defined(WIN32)
#include <windows.h>

BOOL WINAPI DllMain(HINSTANCE, DWORD reason, LPVOID reserved)
{
if (reason == DLL_PROCESS_ATTACH)
{
lime::load();
}

if (reason == DLL_PROCESS_DETACH)
{
if (reserved)
{
return TRUE;
}

lime::unload();
}

return TRUE;
}
#else
void __attribute__((constructor)) constructor()
{
lime::load();
}

void __attribute__((destructor)) destructor()
{
lime::unload();
}
#endif
11 changes: 0 additions & 11 deletions src/entrypoint.linux.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions src/entrypoint.static.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions src/entrypoint.win.cpp

This file was deleted.

0 comments on commit 3c4471c

Please sign in to comment.