Files
anope/CMakeLists.txt
2023-05-07 13:44:21 -04:00

25 KiB

# This usage of CMake requires at least version 2.4 (checks are made to determine what to use when certain versions lack functions) cmake_minimum_required(VERSION 2.4 FATAL_ERROR) if(COMMAND cmake_policy) cmake_policy(SET CMP0003 NEW) if(POLICY CMP0026) cmake_policy(SET CMP0026 OLD) endif(POLICY CMP0026) if(POLICY CMP0007) cmake_policy(SET CMP0007 OLD) endif(POLICY CMP0007) endif(COMMAND cmake_policy) # Set the project as C++ primarily, but have C enabled for the checks required later project(Anope CXX) enable_language(C) # Detect the version of CMake for the later conditional checks execute_process(COMMAND ${CMAKE_COMMAND} --version OUTPUT_VARIABLE VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) string(REGEX REPLACE "cmake version 2\\.(.*)" "\\1" ONLY_VERSION "${VERSION}") string(REGEX MATCH "-patch .*$" HAS_PATCH "${ONLY_VERSION}") if(HAS_PATCH) string(REGEX REPLACE "(.*)-patch .*" "\\1" MINOR_VERSION "${ONLY_VERSION}") string(REGEX REPLACE ".*-patch (.*)" "\\1" PATCH_VERSION "${ONLY_VERSION}") else(HAS_PATCH) string(REGEX MATCH "\\." HAS_DOT "${ONLY_VERSION}") if(HAS_DOT) string(REGEX REPLACE "(.*)\\..*" "\\1" MINOR_VERSION "${ONLY_VERSION}") string(REGEX REPLACE ".*\\.(.*)" "\\1" PATCH_VERSION "${ONLY_VERSION}") else(HAS_DOT) string(REGEX REPLACE "(.*)-beta" "\\1" MINOR_VERSION "${ONLY_VERSION}") if(MINOR_VERSION STREQUAL "4-1\n") set(PATCH_VERSION 1) else(MINOR_VERSION STREQUAL "4-1\n") set(PATCH_VERSION 0) endif(MINOR_VERSION STREQUAL "4-1\n") set(MINOR_VERSION 4) endif(HAS_DOT) endif(HAS_PATCH) # Detect is we are using CMake 2.6 or better, these versions include functions that require less work than CMake 2.4 does if(MINOR_VERSION GREATER 5) set(CMAKE26_OR_BETTER TRUE) set(CMAKE248_OR_BETTER TRUE) set(CMAKE244_OR_BETTER TRUE) set(CMAKE242_OR_BETTER TRUE) else(MINOR_VERSION GREATER 5) set(CMAKE26_OR_BETTER FALSE) # Also detect if we are using CMake 2.4.8 or better, the FIND sub-command of list() is nonexistent in earlier versions if(PATCH_VERSION GREATER 7) set(CMAKE248_OR_BETTER TRUE) set(CMAKE244_OR_BETTER TRUE) set(CMAKE242_OR_BETTER TRUE) else(PATCH_VERSION GREATER 7) set(CMAKE248_OR_BETTER FALSE) # Also detect if we are using CMake 2.4.4 or better, the CheckCXXCompilerFlag module and SORT sub-command of list() are nonexistent in earlier versions if(PATCH_VERSION GREATER 3) set(CMAKE244_OR_BETTER TRUE) set(CMAKE242_OR_BETTER TRUE) else(PATCH_VERSION GREATER 3) set(CMAKE244_OR_BETTER FALSE) # ALSO detect if we are using CMake 2.4.2 or better, the APPEND sub-command of list() is nonexistent in earlier versions if(PATCH_VERSION GREATER 1) set(CMAKE242_OR_BETTER TRUE) else(PATCH_VERSION GREATER 1) set(CMAKE242_OR_BETTER FALSE) endif(PATCH_VERSION GREATER 1) endif(PATCH_VERSION GREATER 3) endif(PATCH_VERSION GREATER 7) endif(MINOR_VERSION GREATER 5) # Override the module include path to include our directory, for our Anope.cmake, as well as we are using our own version of the NSIS template set(CMAKE_MODULE_PATH ${Anope_SOURCE_DIR}/cmake) include(Anope) # Force the locale to C for later uses of things like gcc so the messages come up in English, not the user's default language set(ENV{LC_ALL} C) # Start with empty defaults for library and include directories, to be used by GNU compilers only set(DEFAULT_LIBRARY_DIRS) set(DEFAULT_INCLUDE_DIRS) # Check that we aren't running on an ancient broken GCC if(CMAKE_COMPILER_IS_GNUCXX) execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_FULL_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) string(REGEX REPLACE "^(\\d+\\.\\d+)" "\\1" GCC_VERSION ${GCC_FULL_VERSION}) if(GCC_VERSION LESS 4.2) message(FATAL_ERROR "Your compiler is too old to build Anope. Upgrade to GCC 4.2 or newer!") endif(GCC_VERSION LESS 4.2) if(GCC_VERSION GREATER 6.0 OR GCC_VERSION EQUAL 6.0) set(CXXFLAGS "${CXXFLAGS} -fno-delete-null-pointer-checks") endif(GCC_VERSION GREATER 6.0 OR GCC_VERSION EQUAL 6.0) endif(CMAKE_COMPILER_IS_GNUCXX) # If we are using a GNU compiler (have to use CXX because it seems to fail on C), we will be able to determine it's default paths for libraries and includes if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang$") # First look for the compiler's default library directories execute_process(COMMAND ${CMAKE_C_COMPILER} -print-search-dirs OUTPUT_VARIABLE LINES OUTPUT_STRIP_TRAILING_WHITESPACE) # Find only the part after "libraries: " string(REGEX REPLACE ".*\nlibraries: (.*)$" "\\1" LINE "${LINES}") # Replace the colons in the list with semicolons (only when not on MinGW, which uses semicolons already), and if on MinGW, just copy the line if(NOT MINGW) string(REGEX REPLACE ":" ";" LIBRARIES ${LINE}) else(NOT MINGW) set(LIBRARIES "${LINE}") endif(NOT MINGW) # Iterate through the libraries foreach(LIBRARY ${LIBRARIES}) # Check if the first character is an equal sign, and skip that library directory as it is (I believe) the primary default and shows up later in the list anyways string(SUBSTRING ${LIBRARY} 0 1 FIRST_CHAR) if(NOT FIRST_CHAR STREQUAL "=") # If the directory had no = in front of it, make sure it's absolute and add it to the list of default library directories get_filename_component(LIBRARY ${LIBRARY} ABSOLUTE) append_to_list(DEFAULT_LIBRARY_DIRS ${LIBRARY}) endif(NOT FIRST_CHAR STREQUAL "=") endforeach(LIBRARY) # Remove duplicate entries from the list if(DEFAULT_LIBRARY_DIRS) remove_list_duplicates(DEFAULT_LIBRARY_DIRS) endif(DEFAULT_LIBRARY_DIRS) # Create a temporary file to test for the default include directories FILE(WRITE empty.cpp "") # Next, we look for the compiler's default include directories # Run the command to find the default include directories execute_process(COMMAND ${CMAKE_C_COMPILER} -v -x c++ -E ${CMAKE_CURRENT_SOURCE_DIR}/empty.cpp ERROR_VARIABLE LINES OUTPUT_QUIET ERROR_STRIP_TRAILING_WHITESPACE) # Remove the empty file, it is no longer needed FILE(REMOVE empty.cpp) # Convert the new lines to semicolons string(REGEX REPLACE "\n" ";" LINES ${LINES}) # Temporary variable saying if we are in the search list or not set(IN_SEARCH_LIST FALSE) # Iterate through the lines foreach(LINE ${LINES}) # If the line has the following on it, the next lines will contain directory names if(LINE STREQUAL "#include <...> search starts here:") set(IN_SEARCH TRUE) else(LINE STREQUAL "#include <...> search starts here:") # If the line has the following on it, we hit the end of the list if(LINE STREQUAL "End of search list.") set(IN_SEARCH FALSE) else(LINE STREQUAL "End of search list.") # If we are within the block between the above two lines... if(IN_SEARCH) # Get everything but the first character of the line string(LENGTH ${LINE} LINE_LENGTH) math(EXPR LINE_LENGTH "${LINE_LENGTH} - 1") string(SUBSTRING ${LINE} 1 ${LINE_LENGTH} INCLUDE) # For systems like Mac OS X, look for include paths that say " (framework directory)" at the end of them and strip that off string(REGEX REPLACE " \\(framework directory\\)$" "" INCLUDE ${INCLUDE}) # Convert the path to an absolute one, just in case it wasn't get_filename_component(INCLUDE ${INCLUDE} ABSOLUTE) # Add that directory to the list of default include directories append_to_list(DEFAULT_INCLUDE_DIRS ${INCLUDE}) endif(IN_SEARCH) endif(LINE STREQUAL "End of search list.") endif(LINE STREQUAL "#include <...> search starts here:") endforeach(LINE) # Remove duplicate entries from the list if(DEFAULT_INCLUDE_DIRS) remove_list_duplicates(DEFAULT_INCLUDE_DIRS) endif(DEFAULT_INCLUDE_DIRS) endif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang$") # If we are using Visual Studio, locate the path of the Windows Server 2008 SDK or Windows Server 2003 Platform SDK, depending on which is installed if(MSVC) # If the path comes up as "/registry" from any of these, the path wasn't found, otherwise, we'll set WSDK_PATH to the corresponding path # Look for the 2008 SDK under HKLM first get_filename_component(WSDK2008_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows;CurrentInstallFolder]" ABSOLUTE CACHE) if(WSDK2008_PATH STREQUAL "/registry") # If not found, look for the 2003 SDK under HKLM get_filename_component(WSDK2003_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MicrosoftSDK\\InstalledSDKs\\D2FF9F89-8AA2-4373-8A31-C838BF4DBBE1;Install Dir]" ABSOLUTE CACHE) if(WSDK2003_PATH STREQUAL "/registry") # If not found, look for the 2008 SDK under HKCU get_filename_component(WSDK2008_PATH "[HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows;CurrentInstallFolder]" ABSOLUTE CACHE) if(WSDK2008_PATH STREQUAL "/registry") # If not found, look for the 2003 SDK under HKCU get_filename_component(WSDK2003_PATH "[HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\MicrosoftSDK\\InstalledSDKs\\D2FF9F89-8AA2-4373-8A31-C838BF4DBBE1;Install Dir]" ABSOLUTE CACHE) if(WSDK2003_PATH STREQUAL "/registry") # The SDK was never found, set the path to nothing set(WSDK_PATH "") else(WSDK2003_PATH STREQUAL "/registry") set(WSDK_PATH "${WSDK2003_PATH}") endif(WSDK2003_PATH STREQUAL "/registry") else(WSDK2008_PATH STREQUAL "/registry") set(WSDK_PATH "${WSDK2008_PATH}") endif(WSDK2008_PATH STREQUAL "/registry") else(WSDK2003_PATH STREQUAL "/registry") set(WSDK_PATH "${WSDK2003_PATH}") endif(WSDK2003_PATH STREQUAL "/registry") else(WSDK2008_PATH STREQUAL "/registry") set(WSDK_PATH "${WSDK2008_PATH}") endif(WSDK2008_PATH STREQUAL "/registry") endif(MSVC) # If the user specifies -DCMAKE_BUILD_TYPE on the command line, take their definition # and dump it in the cache along with proper documentation, otherwise set CMAKE_BUILD_TYPE # to Debug # Only do this if not using Visual Studio if(NOT MSVC) if(CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.") else(CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE DEBUG CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.") endif(CMAKE_BUILD_TYPE) endif(NOT MSVC) # If running under MinGW, we have to force the resource compiler settings (hopefully this will be fixed in a later version of CMake) if(MINGW) set(CMAKE_RC_COMPILER_INIT windres) enable_language(RC) set(CMAKE_RC_COMPILE_OBJECT " -o