From d6fcb24863e122f83b96ee686ceb34e85dd84d66 Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Thu, 16 May 2024 14:22:33 +0100 Subject: [PATCH] Added static library linking --- CMakeLists.txt | 15 ++++- HarmonyLinkLib/CMakeLists.txt | 86 ++++++++++++++++++-------- HarmonyLinkLib/Resources/Version.rc.in | 49 +++++++++++++++ HarmonyLinkLib/include/Core.h | 10 ++- HarmonyLinkLib/include/Version.h.in | 4 ++ HarmonyLinkTest/CMakeLists.txt | 49 +++++++++++---- 6 files changed, 171 insertions(+), 42 deletions(-) create mode 100644 HarmonyLinkLib/Resources/Version.rc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 16e76c5..02296e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,16 @@ +# Copyright (c) 2024 Jordon Brooks +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + cmake_minimum_required(VERSION 3.10) project(HarmonyLinkProject) @@ -57,7 +70,7 @@ add_subdirectory(HarmonyLinkTest) #add_executable(Testing ${TEST_SOURCES}) # Set HarmonyLinkTest as the default startup project -set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT HarmonyLinkTest) +set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT HarmonyLinkTestShared) # Link Google Test and HarmonyLink library to the test executable #target_link_libraries(Testing gtest gtest_main gmock HarmonyLinkLib) diff --git a/HarmonyLinkLib/CMakeLists.txt b/HarmonyLinkLib/CMakeLists.txt index a0dbaad..97ec61a 100644 --- a/HarmonyLinkLib/CMakeLists.txt +++ b/HarmonyLinkLib/CMakeLists.txt @@ -1,3 +1,16 @@ +# Copyright (c) 2024 Jordon Brooks +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + cmake_minimum_required(VERSION 3.10) project(HarmonyLinkLib VERSION 2.0.0) @@ -45,6 +58,16 @@ configure_file(include/Version.h.in Version.generated.h) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) +# Define metadata variables +set(FILE_DESCRIPTION "Enhances handheld gaming with intelligent hardware recognition, dynamic adaptability, and robust API access for Windows and Linux, including Steam Deck and Wine support.") +set(INTERNAL_NAME "HarmonyLinkLib") +set(ORIGINAL_FILENAME "HarmonyLinkLib.dll") +set(PRODUCT_NAME "HarmonyLinkLib") +set(COMMENTS "") + +# Configure version.rc file for shared library +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Resources/Version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY) + # Explicitly list source files set(COMMON_SOURCES "src/Platform/IPlatformUtilities.cpp" @@ -58,24 +81,19 @@ set(COMMON_SOURCES # Explicitly list include files set(COMMON_INCLUDES "include/Core.h" - "include/Structs/FBattery.h" "include/Structs/FOSVerInfo.h" "include/Structs/FDevice.h" "include/Structs/FCPUInfo.h" - "include/Enums/EDevice.h" "include/Enums/EPlatform.h" "include/Enums/ESteamDeck.h" - "include/FString.h" "include/HarmonyLinkLib.h" "include/Version.h" - "src/Platform/IPlatformUtilities.h" "src/Platform/WineUtilities.h" - - "src//Utilities.h" + "src/Utilities.h" ) set(WINDOWS_SOURCES @@ -111,41 +129,55 @@ if(WIN32) message(STATUS "Compiling for Windows...") list(APPEND LIB_SOURCES ${COMMON_SOURCES} ${WINDOWS_SOURCES}) list(APPEND LIB_INCLUDES ${COMMON_INCLUDES} ${WINDOWS_INCLUDES}) + list(APPEND SHARED_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/version.rc) elseif(UNIX) - message(STATUS "Compiling for Unix-based systems...") - if(APPLE) - message(STATUS "Compiling for Mac...") - list(APPEND LIB_SOURCES ${COMMON_SOURCES} ${MAC_SOURCES}) - list(APPEND LIB_INCLUDES ${COMMON_INCLUDES} ${MAC_INCLUDES}) - else() - message(STATUS "Compiling for Linux...") - list(APPEND LIB_SOURCES ${COMMON_SOURCES} ${LINUX_SOURCES}) - list(APPEND LIB_INCLUDES ${COMMON_INCLUDES} ${LINUX_INCLUDES}) - endif() + message(STATUS "Compiling for Unix-based systems...") + if(APPLE) + message(STATUS "Compiling for Mac...") + list(APPEND LIB_SOURCES ${COMMON_SOURCES} ${MAC_SOURCES}) + list(APPEND LIB_INCLUDES ${COMMON_INCLUDES} ${MAC_INCLUDES}) + else() + message(STATUS "Compiling for Linux...") + list(APPEND LIB_SOURCES ${COMMON_SOURCES} ${LINUX_SOURCES}) + list(APPEND LIB_INCLUDES ${COMMON_INCLUDES} ${LINUX_INCLUDES}) + endif() endif() -# Add library -add_library(HarmonyLinkLib SHARED ${LIB_SOURCES} ${LIB_INCLUDES}) - -target_include_directories(HarmonyLinkLib +# Create the shared library +add_library(HarmonyLinkLibShared SHARED ${LIB_SOURCES} ${SHARED_SOURCES}) +target_include_directories(HarmonyLinkLibShared PRIVATE "${PROJECT_SOURCE_DIR}/src" PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/include" ) +target_compile_definitions(HarmonyLinkLibShared PRIVATE HARMONYLINKLIB_SHARED) -target_compile_definitions(HarmonyLinkLib PRIVATE HARMONYLINKLIB_EXPORTS) +# Create the static library +add_library(HarmonyLinkLibStatic STATIC ${LIB_SOURCES}) +target_include_directories(HarmonyLinkLibStatic + PRIVATE + "${PROJECT_SOURCE_DIR}/src" + PUBLIC + "${PROJECT_BINARY_DIR}" + "${PROJECT_SOURCE_DIR}/include" +) +target_compile_definitions(HarmonyLinkLibStatic PRIVATE HARMONYLINKLIB_STATIC) # Set output directories for all build types foreach(TYPE IN ITEMS DEBUG RELEASE) string(TOUPPER ${TYPE} TYPE_UPPER) - set_target_properties(${PROJECT_NAME} PROPERTIES - RUNTIME_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/bin/${TYPE}/${PROJECT_NAME}" - LIBRARY_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/lib/${TYPE}/${PROJECT_NAME}" - ARCHIVE_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/archive/${TYPE}/${PROJECT_NAME}" + set_target_properties(HarmonyLinkLibShared PROPERTIES + RUNTIME_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/bin/${TYPE}/HarmonyLinkLib" + LIBRARY_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/lib/${TYPE}/HarmonyLinkLib" + ARCHIVE_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/archive/${TYPE}/HarmonyLinkLib" + ) + set_target_properties(HarmonyLinkLibStatic PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/archive/${TYPE}/HarmonyLinkLibStatic" ) endforeach() -# Link fmt library -target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt) +# Link fmt library to both shared and static libraries +target_link_libraries(HarmonyLinkLibShared PRIVATE fmt::fmt) +target_link_libraries(HarmonyLinkLibStatic PRIVATE fmt::fmt) diff --git a/HarmonyLinkLib/Resources/Version.rc.in b/HarmonyLinkLib/Resources/Version.rc.in new file mode 100644 index 0000000..1d443f8 --- /dev/null +++ b/HarmonyLinkLib/Resources/Version.rc.in @@ -0,0 +1,49 @@ +// Copyright (c) 2024 Jordon Brooks +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +VS_VERSION_INFO VERSIONINFO + FILEVERSION @PROJECT_VERSION_MAJOR@,@PROJECT_VERSION_MINOR@,@PROJECT_VERSION_PATCH@,0 + PRODUCTVERSION @PROJECT_VERSION_MAJOR@,@PROJECT_VERSION_MINOR@,@PROJECT_VERSION_PATCH@,0 + FILEFLAGSMASK 0x3fL + FILEFLAGS 0x0L + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE 0x0L +{ + BLOCK "StringFileInfo" + { + BLOCK "040904b0" + { + VALUE "CompanyName", "N/A" + VALUE "FileDescription", "@FILE_DESCRIPTION@" + VALUE "FileVersion", "@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@.0" + VALUE "InternalName", "@INTERNAL_NAME@" + VALUE "OriginalFilename", "@ORIGINAL_FILENAME@" + VALUE "ProductName", "@PRODUCT_NAME@" + VALUE "ProductVersion", "@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@.0" + VALUE "Comments", "@COMMENTS@" + VALUE "LegalCopyright", "N/A" + VALUE "LegalTrademarks", "N/A" + VALUE "PrivateBuild", "N/A" + VALUE "SpecialBuild", "N/A" + } + } + BLOCK "VarFileInfo" + { + VALUE "Translation", 0x409, 1200 + } +} diff --git a/HarmonyLinkLib/include/Core.h b/HarmonyLinkLib/include/Core.h index ec6ef9d..94a32d2 100644 --- a/HarmonyLinkLib/include/Core.h +++ b/HarmonyLinkLib/include/Core.h @@ -16,10 +16,14 @@ // Use a preprocessor definition to switch between export and import declarations #ifdef _WIN32 - #ifdef HARMONYLINKLIB_EXPORTS - #define HARMONYLINKLIB_API __declspec(dllexport) + #ifdef HARMONYLINKLIB_STATIC + #define HARMONYLINKLIB_API #else - #define HARMONYLINKLIB_API __declspec(dllimport) + #ifdef HARMONYLINKLIB_SHARED + #define HARMONYLINKLIB_API __declspec(dllexport) + #else + #define HARMONYLINKLIB_API __declspec(dllimport) + #endif #endif #else #define HARMONYLINKLIB_API diff --git a/HarmonyLinkLib/include/Version.h.in b/HarmonyLinkLib/include/Version.h.in index 08881c5..0802c01 100644 --- a/HarmonyLinkLib/include/Version.h.in +++ b/HarmonyLinkLib/include/Version.h.in @@ -23,3 +23,7 @@ #define GIT_BRANCH_NAME "@GIT_BRANCH_NAME@" #define GIT_COMMIT_TIMESTAMP "@GIT_COMMIT_TIMESTAMP@" + + +#include +#include diff --git a/HarmonyLinkTest/CMakeLists.txt b/HarmonyLinkTest/CMakeLists.txt index 5616a24..949ea4f 100644 --- a/HarmonyLinkTest/CMakeLists.txt +++ b/HarmonyLinkTest/CMakeLists.txt @@ -1,3 +1,16 @@ +# Copyright (c) 2024 Jordon Brooks +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + cmake_minimum_required(VERSION 3.10) project(HarmonyLinkTest) @@ -9,23 +22,37 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) file(GLOB_RECURSE TEST_SOURCES "src/*.cpp") file(GLOB_RECURSE TEST_HEADERS "src/*.h" "src/*.hpp") -# Add executable -add_executable(HarmonyLinkTest ${TEST_SOURCES} ${TEST_HEADERS}) +# Add executable for static library +add_executable(HarmonyLinkTestStatic ${TEST_SOURCES} ${TEST_HEADERS}) +target_link_libraries(HarmonyLinkTestStatic PRIVATE HarmonyLinkLibStatic) +target_compile_definitions(HarmonyLinkTestStatic PRIVATE HARMONYLINKLIB_STATIC) -# Link the HarmonyLinkLib with HarmonyLinkTest -target_link_libraries(HarmonyLinkTest PRIVATE HarmonyLinkLib) +# Add executable for shared library +add_executable(HarmonyLinkTestShared ${TEST_SOURCES} ${TEST_HEADERS}) +target_link_libraries(HarmonyLinkTestShared PRIVATE HarmonyLinkLibShared) +target_compile_definitions(HarmonyLinkTestShared PRIVATE HARMONYLINKLIB_SHARED) # Set output directories for all build types foreach(TYPE IN ITEMS DEBUG RELEASE) string(TOUPPER ${TYPE} TYPE_UPPER) - set_target_properties(${PROJECT_NAME} PROPERTIES - RUNTIME_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/bin/${TYPE}/${PROJECT_NAME}" - LIBRARY_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/bin/${TYPE}/${PROJECT_NAME}" - ARCHIVE_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/bin/${TYPE}/${PROJECT_NAME}" + + # Static test executable properties + set_target_properties(HarmonyLinkTestStatic PROPERTIES + RUNTIME_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/bin/${TYPE}/HarmonyLinkTestStatic" + LIBRARY_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/lib/${TYPE}/HarmonyLinkTestStatic" + ARCHIVE_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/archive/${TYPE}/HarmonyLinkTestStatic" + ) + + # Shared test executable properties + set_target_properties(HarmonyLinkTestShared PROPERTIES + RUNTIME_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/bin/${TYPE}/HarmonyLinkTestShared" + LIBRARY_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/lib/${TYPE}/HarmonyLinkTestShared" + ARCHIVE_OUTPUT_DIRECTORY_${TYPE_UPPER} "${CMAKE_BINARY_DIR}/archive/${TYPE}/HarmonyLinkTestShared" ) endforeach() -add_custom_command(TARGET HarmonyLinkTest POST_BUILD +# Copy the DLL to the executable directory after building the shared test executable +add_custom_command(TARGET HarmonyLinkTestShared POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different - "$" - "$") + "$" + "$")