diff --git a/CMakeLists.txt b/CMakeLists.txt index d6fdc75..59382f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,21 @@ project(dynalo) #set(CMAKE_CXX_STANDARD_REQUIRED ON ) #set(CMAKE_CXX_EXTENSIONS OFF) +option(CLEAN_BUILD "Clean build with no warnings allowed" OFF) +message(STATUS "Clean build, warnings are not allowed: " ${CLEAN_BUILD}) +option(BUILD_TESTS "Build tests for ${PROJECT_NAME}: " ON) + +if (${CLEAN_BUILD}) + if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + add_compile_options(/W4 /WX) + elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + add_compile_options(-Wall -Wextra -pedantic -Werror) + else() + # TODO: implement flags for other compilers + message(STATUS "CLEAN_BUILD is not supported for ${CMAKE_CXX_COMPILER_ID}") + endif() +endif() + add_library(${PROJECT_NAME} INTERFACE) target_include_directories(${PROJECT_NAME} INTERFACE "$" @@ -58,4 +73,10 @@ install( DESTINATION "${CONFIG_PACKAGE_INSTALL_LOCATION}" ) -add_subdirectory(test) +get_directory_property(IS_EXCLUDED EXCLUDE_FROM_ALL) + +if (NOT IS_EXCLUDED AND BUILD_TESTS) + message(STATUS "Building tests for ${PROJECT_NAME}") + enable_testing() + add_subdirectory(test) +endif() diff --git a/include/dynalo/detail/linux/dynalo.hpp b/include/dynalo/detail/linux/dynalo.hpp index 77c1c76..441ae1d 100644 --- a/include/dynalo/detail/linux/dynalo.hpp +++ b/include/dynalo/detail/linux/dynalo.hpp @@ -64,7 +64,9 @@ FunctionSignature* get_function(native::handle lib_handle, const std::string& fu throw std::runtime_error(std::string("Failed to get [func_name:") + func_name + "]: " + last_error()); } - return reinterpret_cast(func_ptr); +// TODO: disable optimization for these lines since it is UB + void **intermediate_ptr = reinterpret_cast(&func_ptr); + return reinterpret_cast(*intermediate_ptr); } }} diff --git a/include/dynalo/detail/windows/dynalo.hpp b/include/dynalo/detail/windows/dynalo.hpp index c4eea6d..1d4aa0e 100644 --- a/include/dynalo/detail/windows/dynalo.hpp +++ b/include/dynalo/detail/windows/dynalo.hpp @@ -92,7 +92,9 @@ FunctionSignature* get_function(native::handle lib_handle, const std::string& fu throw std::runtime_error(std::string("Failed to get [func_name:") + func_name + "]: " + last_error()); } - return reinterpret_cast(func_ptr); +// TODO: disable optimization for these lines since it is UB + void **intermediate_ptr = reinterpret_cast(&func_ptr); + return reinterpret_cast(*intermediate_ptr); } }} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index be4aeaa..10251d1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ -cmake_minimum_required(VERSION 3.0.0) +#cmake_minimum_required(VERSION 3.0.0) -project(dynalo-test) +# project(dynalo-test) set(CMAKE_CXX_STANDARD 11 ) set(CMAKE_CXX_STANDARD_REQUIRED ON ) @@ -13,7 +13,6 @@ add_library(shared SHARED shared.cpp) add_dependencies(loader shared) -enable_testing() -add_test(NAME all_tests COMMAND - "$" "$" "shared" +add_test(NAME loader COMMAND + "$" "$" ) diff --git a/test/loader.cpp b/test/loader.cpp index 6b490aa..dae97a6 100644 --- a/test/loader.cpp +++ b/test/loader.cpp @@ -1,17 +1,33 @@ #include #include +#include #include // usage: loader "path/to/shared/lib/dir" int main(int argc, char* argv[]) { - dynalo::library lib(std::string(argv[1]) + "/" + dynalo::to_native_name("shared")); + if (argc < 2) + { + std::cerr << "No path for a dynamic library was provided.\nUsage: ./loader path_to_dyn_lib" << std::endl; + } + try + { + dynalo::library lib(argv[1]); - auto add_integers = lib.get_function("add_integers"); - auto print_message = lib.get_function("print_message"); + auto add_integers = lib.get_function("add_integers"); + auto print_message = lib.get_function("print_message"); - std::ostringstream oss; - oss << "it works: " << add_integers(1, 2); - print_message(oss.str().c_str()); + std::ostringstream oss; + oss << "it works: " << add_integers(1, 2); + print_message(oss.str().c_str()); + + return 0; + } + catch (const std::exception& error) + { + std::cerr << "Test failed: " << error.what() << std::endl; + } + + return -1; }