diff options
author | Kargatum <dowlandtop@yandex.com> | 2021-04-22 03:16:12 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-21 22:16:12 +0200 |
commit | 325dcfc9a6c9a83f9e107a06e11b8280feda2a5d (patch) | |
tree | 70c246b197294f920a632057c250cb77645ab14a /src/cmake | |
parent | 2c5cb29ad49063b7b0e4085f8624ab32786d382d (diff) |
feat(Core/Build): add the possibility to link libraries dynamically (#5348)
Diffstat (limited to 'src/cmake')
-rw-r--r-- | src/cmake/compiler/clang/settings.cmake | 28 | ||||
-rw-r--r-- | src/cmake/compiler/gcc/settings.cmake | 22 | ||||
-rw-r--r-- | src/cmake/compiler/msvc/settings.cmake | 42 | ||||
-rw-r--r-- | src/cmake/macros/ConfigureScripts.cmake | 108 | ||||
-rw-r--r-- | src/cmake/showoptions.cmake | 16 |
5 files changed, 208 insertions, 8 deletions
diff --git a/src/cmake/compiler/clang/settings.cmake b/src/cmake/compiler/clang/settings.cmake index 32234085cf..fecfb2edc4 100644 --- a/src/cmake/compiler/clang/settings.cmake +++ b/src/cmake/compiler/clang/settings.cmake @@ -8,6 +8,14 @@ target_compile_definitions(acore-compile-option-interface INTERFACE -D_BUILD_DIRECTIVE="${CMAKE_BUILD_TYPE}") +set(CLANG_EXPECTED_VERSION 6.0.0) + +if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS CLANG_EXPECTED_VERSION) + message(FATAL_ERROR "Clang: AzerothCore requires version ${CLANG_EXPECTED_VERSION} to build but found ${CMAKE_CXX_COMPILER_VERSION}") +else() + message(STATUS "Clang: Minimum version required is ${CLANG_EXPECTED_VERSION}, found ${CMAKE_CXX_COMPILER_VERSION} - ok!") +endif() + # This tests for a bug in clang-7 that causes linkage to fail for 64-bit from_chars (in some configurations) # If the clang requirement is bumped to >= clang-8, you can remove this check, as well as # the associated ifdef block in src/common/Utilities/StringConvert.h @@ -59,6 +67,20 @@ target_compile_options(acore-compile-option-interface -Wno-narrowing -Wno-deprecated-register) -target_compile_definitions(acore-compile-option-interface - INTERFACE - -DDEBUG=1) +if(BUILD_SHARED_LIBS) + # -fPIC is needed to allow static linking in shared libs. + # -fvisibility=hidden sets the default visibility to hidden to prevent exporting of all symbols. + target_compile_options(acore-compile-option-interface + INTERFACE + -fPIC) + + target_compile_options(acore-hidden-symbols-interface + INTERFACE + -fvisibility=hidden) + + # --no-undefined to throw errors when there are undefined symbols + # (caused through missing WARHEAD_*_API macros). + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --no-undefined") + + message(STATUS "Clang: Disallow undefined symbols") +endif() diff --git a/src/cmake/compiler/gcc/settings.cmake b/src/cmake/compiler/gcc/settings.cmake index 4e6b126e59..7519278053 100644 --- a/src/cmake/compiler/gcc/settings.cmake +++ b/src/cmake/compiler/gcc/settings.cmake @@ -8,10 +8,12 @@ target_compile_definitions(acore-compile-option-interface INTERFACE -D_BUILD_DIRECTIVE="${CMAKE_BUILD_TYPE}") -set(GCC_EXPECTED_VERSION 4.8.2) +set(GCC_EXPECTED_VERSION 7.0.0) if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS GCC_EXPECTED_VERSION) message(FATAL_ERROR "GCC: This project requires version ${GCC_EXPECTED_VERSION} to build but found ${CMAKE_CXX_COMPILER_VERSION}") +else() + message(STATUS "GCC: Minimum version required is ${GCC_EXPECTED_VERSION}, found ${CMAKE_CXX_COMPILER_VERSION} - ok!") endif() if(PLATFORM EQUAL 32) @@ -47,3 +49,21 @@ if( WITH_COREDEBUG ) -g3) message(STATUS "GCC: Debug-flags set (-g3)") endif() + +if(BUILD_SHARED_LIBS) + target_compile_options(acore-compile-option-interface + INTERFACE + -fPIC + -Wno-attributes) + + target_compile_options(acore-hidden-symbols-interface + INTERFACE + -fvisibility=hidden) + + # Should break the build when there are WARHEAD_*_API macros missing + # but it complains about missing references in precompiled headers. + # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--no-undefined") + # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--no-undefined") + + message(STATUS "GCC: Enabled shared linking") +endif() diff --git a/src/cmake/compiler/msvc/settings.cmake b/src/cmake/compiler/msvc/settings.cmake index 0ca173c0e0..aa4b389052 100644 --- a/src/cmake/compiler/msvc/settings.cmake +++ b/src/cmake/compiler/msvc/settings.cmake @@ -6,9 +6,14 @@ # set up output paths for executable binaries (.exe-files, and .dll-files on DLL-capable platforms) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -# set up output paths ofr static libraries etc (commented out - shown here as an example only) -#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(MSVC_EXPECTED_VERSION 19.24) +set(MSVC_EXPECTED_VERSION_STRING "Microsoft Visual Studio 2019 16.4") + +if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS MSVC_EXPECTED_VERSION) + message(FATAL_ERROR "MSVC: AzerothCore requires version ${MSVC_EXPECTED_VERSION} (${MSVC_EXPECTED_VERSION_STRING}) to build but found ${CMAKE_CXX_COMPILER_VERSION}") +else() + message(STATUS "MSVC: Minimum version required is ${MSVC_EXPECTED_VERSION}, found ${CMAKE_CXX_COMPILER_VERSION} - ok!") +endif() # CMake sets warning flags by default, however we manage it manually # for different core and dependency targets @@ -106,6 +111,14 @@ if(NOT WITH_WARNINGS) endif() endif() +# Ignore specific warnings +# C4351: new behavior: elements of array 'x' will be default initialized +# C4091: 'typedef ': ignored on left of '' when no variable is declared +target_compile_options(acore-compile-option-interface + INTERFACE + /wd4351 + /wd4091) + # Specify the maximum PreCompiled Header memory allocation limit # Fixes a compiler-problem when using PCH - the /Ym flag is adjusted by the compiler in MSVC2012, hence we need to set an upper limit with /Zm to avoid discrepancies) # (And yes, this is a verified , unresolved bug with MSVC... *sigh*) @@ -119,3 +132,26 @@ target_compile_options(acore-warning-interface INTERFACE /we4263 /we4264) + +if(BUILD_SHARED_LIBS) + # C4251: needs to have dll-interface to be used by clients of class '...' + # C4275: non dll-interface class ...' used as base for dll-interface class '...' + target_compile_options(acore-compile-option-interface + INTERFACE + /wd4251 + /wd4275) + + message(STATUS "MSVC: Enabled shared linking") +endif() + +# Disable incremental linking in debug builds. +# To prevent linking getting stuck (which might be fixed in a later VS version). +macro(DisableIncrementalLinking variable) +string(REGEX REPLACE "/INCREMENTAL *" "" ${variable} "${${variable}}") +set(${variable} "${${variable}} /INCREMENTAL:NO") +endmacro() + +DisableIncrementalLinking(CMAKE_EXE_LINKER_FLAGS_DEBUG) +DisableIncrementalLinking(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO) +DisableIncrementalLinking(CMAKE_SHARED_LINKER_FLAGS_DEBUG) +DisableIncrementalLinking(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO) diff --git a/src/cmake/macros/ConfigureScripts.cmake b/src/cmake/macros/ConfigureScripts.cmake new file mode 100644 index 0000000000..cefaa88377 --- /dev/null +++ b/src/cmake/macros/ConfigureScripts.cmake @@ -0,0 +1,108 @@ +# +# Copyright (C) 2008-2019 TrinityCore <https://www.trinitycore.org/> +# Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3 +# +# This file is free software; as a special exception the author gives +# unlimited permission to copy and/or distribute it, with or without +# modifications, as long as this notice is preserved. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +# Returns the base path to the script directory in the source directory +function(WarnAboutSpacesInBuildPath) + # Only check win32 since unix doesn't allow spaces in paths + if(WIN32) + string(FIND "${CMAKE_BINARY_DIR}" " " SPACE_INDEX_POS) + + if(SPACE_INDEX_POS GREATER -1) + message("") + message(WARNING " *** WARNING!\n" + " *** Your selected build directory contains spaces!\n" + " *** Please note that this will cause issues!") + endif() + endif() +endfunction() + +# Returns the base path to the script directory in the source directory +function(GetScriptsBasePath variable) + set(${variable} "${CMAKE_SOURCE_DIR}/src/server/scripts" PARENT_SCOPE) +endfunction() + +# Stores the absolut path of the given module in the variable +function(GetPathToScriptModule module variable) + GetScriptsBasePath(SCRIPTS_BASE_PATH) + set(${variable} "${SCRIPTS_BASE_PATH}/${module}" PARENT_SCOPE) +endfunction() + +# Stores the project name of the given module in the variable +function(GetProjectNameOfScriptModule module variable) + string(TOLOWER "scripts_${SCRIPT_MODULE}" GENERATED_NAME) + set(${variable} "${GENERATED_NAME}" PARENT_SCOPE) +endfunction() + +# Creates a list of all script modules +# and stores it in the given variable. +function(GetScriptModuleList variable) + GetScriptsBasePath(BASE_PATH) + file(GLOB LOCALE_SCRIPT_MODULE_LIST RELATIVE + ${BASE_PATH} + ${BASE_PATH}/*) + + set(${variable}) + foreach(SCRIPT_MODULE ${LOCALE_SCRIPT_MODULE_LIST}) + GetPathToScriptModule(${SCRIPT_MODULE} SCRIPT_MODULE_PATH) + if(IS_DIRECTORY ${SCRIPT_MODULE_PATH}) + list(APPEND ${variable} ${SCRIPT_MODULE}) + endif() + endforeach() + set(${variable} ${${variable}} PARENT_SCOPE) +endfunction() + +# Converts the given script module name into it's +# variable name which holds the linkage type. +function(ScriptModuleNameToVariable module variable) + string(TOUPPER ${module} ${variable}) + set(${variable} "SCRIPTS_${${variable}}") + set(${variable} ${${variable}} PARENT_SCOPE) +endfunction() + +# Stores in the given variable whether dynamic linking is required +function(IsDynamicLinkingRequired variable) + if(SCRIPTS MATCHES "dynamic") + set(IS_DEFAULT_VALUE_DYNAMIC ON) + endif() + + GetScriptModuleList(SCRIPT_MODULE_LIST) + set(IS_REQUIRED OFF) + foreach(SCRIPT_MODULE ${SCRIPT_MODULE_LIST}) + ScriptModuleNameToVariable(${SCRIPT_MODULE} SCRIPT_MODULE_VARIABLE) + if((${SCRIPT_MODULE_VARIABLE} STREQUAL "dynamic") OR + (${SCRIPT_MODULE_VARIABLE} STREQUAL "default" AND IS_DEFAULT_VALUE_DYNAMIC)) + set(IS_REQUIRED ON) + break() + endif() + endforeach() + set(${variable} ${IS_REQUIRED} PARENT_SCOPE) +endfunction() + +# Stores the native variable name +function(GetNativeSharedLibraryName module variable) + if(WIN32) + set(${variable} "${module}.dll" PARENT_SCOPE) + elseif(APPLE) + set(${variable} "lib${module}.dylib" PARENT_SCOPE) + else() + set(${variable} "lib${module}.so" PARENT_SCOPE) + endif() +endfunction() + +# Stores the native install path in the variable +function(GetInstallOffset variable) + if(WIN32) + set(${variable} "${CMAKE_INSTALL_PREFIX}/scripts" PARENT_SCOPE) + else() + set(${variable} "${CMAKE_INSTALL_PREFIX}/bin/scripts" PARENT_SCOPE) + endif() +endfunction() diff --git a/src/cmake/showoptions.cmake b/src/cmake/showoptions.cmake index 5822cd82cd..3ee69ae695 100644 --- a/src/cmake/showoptions.cmake +++ b/src/cmake/showoptions.cmake @@ -134,7 +134,7 @@ else() message("* Enable extra logging functions : No (default)") endif() -if(WIN32 AND NOT CMAKE_VERSION VERSION_LESS 2.8.12) +if(WIN32) if(NOT WITH_SOURCE_TREE STREQUAL "no") message("* Show source tree : Yes - \"${WITH_SOURCE_TREE}\"") else() @@ -144,4 +144,18 @@ else() message("* Show source tree : No (For UNIX default)") endif() +if(BUILD_SHARED_LIBS) + message("") + message(" *** WITH_DYNAMIC_LINKING - INFO!") + message(" *** Will link against shared libraries!") + message(" *** Please note that this is an experimental feature!") + if(WITH_DYNAMIC_LINKING_FORCED) + message("") + message(" *** Dynamic linking was enforced through a dynamic script module!") + endif() + add_definitions(-DACORE_API_USE_DYNAMIC_LINKING) + + WarnAboutSpacesInBuildPath() +endif() + message("") |