From c68c039c70730e1ee1eaa7dfdbbe9df1757f951a Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Thu, 6 Jun 2024 18:15:51 +0100 Subject: [PATCH] Fixed + enabled all compiler warnings --- .gitignore | 1 + HarmonyLinkTest/CMakeLists.txt | 7 +++++ HarmonyLinkTest/src/main.c | 2 +- HarmonyLinkTest_CPP/CMakeLists.txt | 7 +++++ HarmonyLinkTest_CPP/src/main.cpp | 2 +- LibHarmonyLink/CMakeLists.txt | 21 +++++++++++-- LibHarmonyLink/include/Core.h | 2 +- LibHarmonyLink/include/HarmonyLinkLib.h | 2 +- LibHarmonyLink/include/Structs/CPUInfo.h | 6 ++-- LibHarmonyLink/include/Structs/StringArray.h | 2 +- LibHarmonyLink/include/Utilities.h | 2 +- LibHarmonyLink/src/HarmonyLinkLib.c | 2 +- LibHarmonyLink/src/Structs/Battery.c | 3 +- LibHarmonyLink/src/Structs/OSInfo.c | 31 +++++++++++++++----- LibHarmonyLink/src/Utilities.c | 2 +- LibHarmonyLink/src/Version.c | 2 ++ 16 files changed, 72 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 81e8788..390d006 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ build/** linuxbuild/ build/ .idea/ +winbuild/ diff --git a/HarmonyLinkTest/CMakeLists.txt b/HarmonyLinkTest/CMakeLists.txt index a821500..a7c0d12 100644 --- a/HarmonyLinkTest/CMakeLists.txt +++ b/HarmonyLinkTest/CMakeLists.txt @@ -22,6 +22,13 @@ set(CMAKE_C_STANDARD_REQUIRED True) file(GLOB_RECURSE TEST_SOURCES "src/*.c") file(GLOB_RECURSE TEST_HEADERS "src/*.h" "src/*.hpp") +# Enable all compiler warnings and errors +if(MSVC) + add_compile_options(/W4 /WX) +else() + add_compile_options(-Wall -Wextra -pedantic -Werror) +endif() + # Add executable for static library add_executable(HarmonyLinkTestStatic ${TEST_SOURCES} ${TEST_HEADERS}) target_link_libraries(HarmonyLinkTestStatic PRIVATE LibHarmonyLinkStatic) diff --git a/HarmonyLinkTest/src/main.c b/HarmonyLinkTest/src/main.c index 245d90e..4752dd4 100644 --- a/HarmonyLinkTest/src/main.c +++ b/HarmonyLinkTest/src/main.c @@ -19,7 +19,7 @@ int main(void) { wprintf(L"Hello from C!\n"); - if (!HarmonyLink_Init()) + if (!HL_Init()) { wprintf(L"Error: Failed to initialise HarmonyLink!\n"); return 1; diff --git a/HarmonyLinkTest_CPP/CMakeLists.txt b/HarmonyLinkTest_CPP/CMakeLists.txt index 9c18edd..65efc21 100644 --- a/HarmonyLinkTest_CPP/CMakeLists.txt +++ b/HarmonyLinkTest_CPP/CMakeLists.txt @@ -22,6 +22,13 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) file(GLOB_RECURSE TEST_SOURCES "src/*.cpp") file(GLOB_RECURSE TEST_HEADERS "src/*.h" "src/*.hpp") +# Enable all compiler warnings and errors +if(MSVC) + add_compile_options(/W4 /WX) +else() + add_compile_options(-Wall -Wextra -pedantic -Werror) +endif() + # Add executable for static library add_executable(HarmonyLinkTestStatic_CPP ${TEST_SOURCES} ${TEST_HEADERS}) target_link_libraries(HarmonyLinkTestStatic_CPP PRIVATE LibHarmonyLinkStatic) diff --git a/HarmonyLinkTest_CPP/src/main.cpp b/HarmonyLinkTest_CPP/src/main.cpp index 7d9a93f..2128219 100644 --- a/HarmonyLinkTest_CPP/src/main.cpp +++ b/HarmonyLinkTest_CPP/src/main.cpp @@ -89,7 +89,7 @@ int main() std::thread inputThread(checkForQuit); - if (!LibHarmonyLink::HarmonyLink_Init()) + if (!LibHarmonyLink::HL_Init()) { printf("Failed to Initialize HarmonyLink!"); } diff --git a/LibHarmonyLink/CMakeLists.txt b/LibHarmonyLink/CMakeLists.txt index b250121..217c38e 100644 --- a/LibHarmonyLink/CMakeLists.txt +++ b/LibHarmonyLink/CMakeLists.txt @@ -12,7 +12,7 @@ # limitations under the License. cmake_minimum_required(VERSION 3.10) -project(LibHarmonyLink VERSION 2.1.1) +project(LibHarmonyLink VERSION 2.1.1 LANGUAGES C) # Specify the C++ standard set(CMAKE_C_STANDARD 11) @@ -60,7 +60,7 @@ set(COMMON_SOURCES src/Structs/CPUInfo.c src/Structs/StringArray.c src/Structs/Device.c - src/Structs/OSInfo.C + src/Structs/OSInfo.c ) # Explicitly list include files @@ -119,6 +119,21 @@ elseif(UNIX) endif() endif() +# Detect the compiler name +get_filename_component(COMPILER_NAME ${CMAKE_CXX_COMPILER} NAME) + +# Replace forbidden characters in file names (optional, if needed) +string(REPLACE "." "_" COMPILER_NAME ${COMPILER_NAME}) +string(REPLACE "/" "_" COMPILER_NAME ${COMPILER_NAME}) +string(REPLACE "\\" "_" COMPILER_NAME ${COMPILER_NAME}) + +# Enable all compiler warnings and errors +if(MSVC) + add_compile_options(/W4 /WX) +else() + add_compile_options(-Wall -Wextra -pedantic -Werror) +endif() + # Create the shared library add_library(LibHarmonyLinkShared SHARED ${LIB_SOURCES} ${LIB_INCLUDES} ${SHARED_SOURCES}) target_include_directories(LibHarmonyLinkShared @@ -129,6 +144,7 @@ target_include_directories(LibHarmonyLinkShared "${PROJECT_SOURCE_DIR}/include" ) target_compile_definitions(LibHarmonyLinkShared PRIVATE HARMONYLINK_SHARED) +set_target_properties(LibHarmonyLinkShared PROPERTIES OUTPUT_NAME "LibHarmonyLink_${COMPILER_NAME}") # Create the static library add_library(LibHarmonyLinkStatic STATIC ${LIB_SOURCES} ${LIB_INCLUDES}) @@ -140,6 +156,7 @@ target_include_directories(LibHarmonyLinkStatic "${PROJECT_SOURCE_DIR}/include" ) target_compile_definitions(LibHarmonyLinkStatic PRIVATE HARMONYLINK_STATIC) +set_target_properties(LibHarmonyLinkStatic PROPERTIES OUTPUT_NAME "LibHarmonyLink_${COMPILER_NAME}") # Set output directories for all build types foreach(TYPE IN ITEMS DEBUG RELEASE) diff --git a/LibHarmonyLink/include/Core.h b/LibHarmonyLink/include/Core.h index 49ea871..f561b77 100644 --- a/LibHarmonyLink/include/Core.h +++ b/LibHarmonyLink/include/Core.h @@ -26,5 +26,5 @@ #endif #endif #else - #define HARMONYLINKLIB_API + #define HARMONYLINK_API #endif diff --git a/LibHarmonyLink/include/HarmonyLinkLib.h b/LibHarmonyLink/include/HarmonyLinkLib.h index a6a45ed..a62e00c 100644 --- a/LibHarmonyLink/include/HarmonyLinkLib.h +++ b/LibHarmonyLink/include/HarmonyLinkLib.h @@ -22,7 +22,7 @@ namespace LibHarmonyLink { extern "C" { #endif -HARMONYLINK_API bool HarmonyLink_Init(void); +HARMONYLINK_API bool HL_Init(void); #ifdef __cplusplus } diff --git a/LibHarmonyLink/include/Structs/CPUInfo.h b/LibHarmonyLink/include/Structs/CPUInfo.h index 044adb9..36d9d13 100644 --- a/LibHarmonyLink/include/Structs/CPUInfo.h +++ b/LibHarmonyLink/include/Structs/CPUInfo.h @@ -27,9 +27,9 @@ namespace LibHarmonyLink { extern "C" { #endif -typedef struct { - char *VendorID; - char *Model_Name; +typedef struct FCPUInfo { + char* VendorID; + char* Model_Name; unsigned int Physical_Cores; unsigned int Logical_Cores; StringArray flagsInfo; diff --git a/LibHarmonyLink/include/Structs/StringArray.h b/LibHarmonyLink/include/Structs/StringArray.h index 1b87569..af9beea 100644 --- a/LibHarmonyLink/include/Structs/StringArray.h +++ b/LibHarmonyLink/include/Structs/StringArray.h @@ -28,7 +28,7 @@ namespace LibHarmonyLink { extern "C" { #endif -typedef struct { +typedef struct StringArray { char **data; // Array of strings (flags) size_t FlagsCount; // Number of flags size_t AllocatedSize; // Number of allocated slots diff --git a/LibHarmonyLink/include/Utilities.h b/LibHarmonyLink/include/Utilities.h index 4aa6856..cffd96e 100644 --- a/LibHarmonyLink/include/Utilities.h +++ b/LibHarmonyLink/include/Utilities.h @@ -25,4 +25,4 @@ wchar_t *convertToWideChar(const char *str); #ifdef __cplusplus } } -#endif \ No newline at end of file +#endif diff --git a/LibHarmonyLink/src/HarmonyLinkLib.c b/LibHarmonyLink/src/HarmonyLinkLib.c index ab45b0a..ae7ae47 100644 --- a/LibHarmonyLink/src/HarmonyLinkLib.c +++ b/LibHarmonyLink/src/HarmonyLinkLib.c @@ -17,7 +17,7 @@ #include "Version.h" -bool HarmonyLink_Init(void) +bool HL_Init(void) { HL_version_print(); return 1; diff --git a/LibHarmonyLink/src/Structs/Battery.c b/LibHarmonyLink/src/Structs/Battery.c index dbd0bba..56d69a2 100644 --- a/LibHarmonyLink/src/Structs/Battery.c +++ b/LibHarmonyLink/src/Structs/Battery.c @@ -23,7 +23,8 @@ FBattery* HL_FBattery_Init(bool has_battery, bool is_connected_to_ac, unsigned c if (battery == NULL) { fprintf(stderr, "Memory allocation failed for FCPUInfo.\n"); - exit(EXIT_FAILURE); + return NULL; + //exit(EXIT_FAILURE); } battery->has_battery = has_battery; diff --git a/LibHarmonyLink/src/Structs/OSInfo.c b/LibHarmonyLink/src/Structs/OSInfo.c index 5116ba4..d3493ac 100644 --- a/LibHarmonyLink/src/Structs/OSInfo.c +++ b/LibHarmonyLink/src/Structs/OSInfo.c @@ -13,28 +13,43 @@ // limitations under the License. #include +#include +#include #include "Structs/OSInfo.h" +#ifdef _WIN32 +#define strdup _strdup +#endif + FOSVerInfo* FOSVerInfo_Init(char* name, char* version, unsigned int id, char* version_id, char* version_codename, char* pretty_name, char* variant_id) { FOSVerInfo* OSVerInfo = (FOSVerInfo*)malloc(sizeof(FOSVerInfo)); if (OSVerInfo == NULL) { fprintf(stderr, "Memory allocation failed for FOSVerInfo.\n"); - exit(EXIT_FAILURE); + return NULL; + //exit(EXIT_FAILURE); } - OSVerInfo->name = name; - OSVerInfo->version = version; + OSVerInfo->name = strdup(name); + OSVerInfo->version = strdup(version); OSVerInfo->id = id; - OSVerInfo->variant_id = version_id; - OSVerInfo->version_codename = version_codename; - OSVerInfo->pretty_name = pretty_name; - OSVerInfo->version_id = variant_id; + OSVerInfo->version_id = strdup(version_id); + OSVerInfo->version_codename = strdup(version_codename); + OSVerInfo->pretty_name = strdup(pretty_name); + OSVerInfo->variant_id = strdup(variant_id); return OSVerInfo; } -void HL_FOSVerInfo_Free(FOSVerInfo *OSVerInfo) { +void HL_FOSVerInfo_Free(FOSVerInfo* osVerInfo) { + if (!osVerInfo) return; + free(osVerInfo->name); + free(osVerInfo->version); + free(osVerInfo->version_id); + free(osVerInfo->version_codename); + free(osVerInfo->pretty_name); + free(osVerInfo->variant_id); + free(osVerInfo); } diff --git a/LibHarmonyLink/src/Utilities.c b/LibHarmonyLink/src/Utilities.c index 6aa8b51..5434fee 100644 --- a/LibHarmonyLink/src/Utilities.c +++ b/LibHarmonyLink/src/Utilities.c @@ -41,4 +41,4 @@ wchar_t* convertToWideChar(const char* str) { #endif return wstr; -} \ No newline at end of file +} diff --git a/LibHarmonyLink/src/Version.c b/LibHarmonyLink/src/Version.c index fd34e33..01e0a4b 100644 --- a/LibHarmonyLink/src/Version.c +++ b/LibHarmonyLink/src/Version.c @@ -16,6 +16,8 @@ #include #include +#include +#include char* HL_version_get_string(void) {