anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

CMakeLists.txt (7958B)

      1 # If using Windows, add the MODULE_COMPILE define
      2 if(WIN32)
      3   add_definitions(-DMODULE_COMPILE)
      4 endif(WIN32)
      5 
      6 macro(build_modules SRC)
      7   if(NOT ${SRC} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR} AND EXISTS "${SRC}/CMakeLists.txt")
      8     add_subdirectory("${SRC}")
      9   else(NOT ${SRC} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR} AND EXISTS "${SRC}/CMakeLists.txt")
     10     file(GLOB MODULES_SRCS "${SRC}/*")
     11     foreach(MODULE_SRC ${MODULES_SRCS})
     12       if(IS_DIRECTORY "${MODULE_SRC}")
     13         build_modules("${MODULE_SRC}")
     14       else(IS_DIRECTORY "${MODULE_SRC}")
     15         string(REGEX MATCH "\\.c$" ANOPE18MODULE ${MODULE_SRC})
     16         if(ANOPE18MODULE)
     17           message(FATAL_ERROR "Anope 1 modules are not compatible with Anope 2!\nOffending module: ${MODULE_SRC}")
     18         endif(ANOPE18MODULE)
     19         string(REGEX MATCH "\\.cpp$" CPP ${MODULE_SRC})
     20         if(CPP)
     21           set_source_files_properties(${MODULE_SRC} PROPERTIES LANGUAGE CXX COMPILE_FLAGS "${CXXFLAGS}")
     22 
     23           file(RELATIVE_PATH FNAME ${SRC} ${MODULE_SRC})
     24           # Convert the real source file extension to have a .so extension
     25           string(REGEX REPLACE "\\.cpp$" ".so" SO ${FNAME})
     26           # Temporary variable for the current source's include directories
     27           set(TEMP_INCLUDES)
     28           # Calculate the header file dependencies for the given source file
     29           calculate_depends(${MODULE_SRC} TEMP_INCLUDES)
     30           # If there were some extra include directories, add them to the list
     31           if(TEMP_INCLUDES)
     32             append_to_list(EXTRA_INCLUDES ${TEMP_INCLUDES})
     33           endif(TEMP_INCLUDES)
     34 
     35           # Reset linker flags
     36           set(TEMP_LDFLAGS)
     37           # Reset extra dependencies
     38           set(TEMP_DEPENDENCIES)
     39           # Calculate the library dependencies for the given source file
     40           calculate_libraries(${MODULE_SRC} TEMP_LDFLAGS TEMP_DEPENDENCIES)
     41           # Reset has_function
     42           set(HAS_FUNCTION)
     43           # Check the function dependencies for the given source file
     44           check_functions(${MODULE_SRC} HAS_FUNCTION)
     45           # Only continue if this module has all of the required functions
     46           if(HAS_FUNCTION)
     47             # For Visual Studio only, include win32_memory static library, required to override Visual Studio's overrides of the new/delete operators
     48             if(MSVC)
     49               set(WIN32_MEMORY win32_memory)
     50             else(MSVC)
     51               set(WIN32_MEMORY)
     52             endif(MSVC)
     53             # Generate the module and set its linker flags, also set it to depend on the main Anope executable to be built beforehand
     54             add_library(${SO} MODULE ${MODULE_SRC})
     55             # Windows requires this because it's weird
     56             if(WIN32)
     57               set(WIN32_NO_LIBS "/nodefaultlib:\"libcmt.lib\" /OPT:NOREF")
     58             else(WIN32)
     59               set(WIN32_NO_LIBS)
     60             endif(WIN32)
     61             set_target_properties(${SO} PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX "" LINK_FLAGS "${TEMP_LDFLAGS} ${WIN32_NO_LIBS}" INSTALL_RPATH_USE_LINK_PATH ON BUILD_WITH_INSTALL_RPATH ON)
     62             add_dependencies(${SO} ${PROGRAM_NAME})
     63             if(GETTEXT_FOUND)
     64               add_dependencies(${SO} module_language)
     65             endif(GETTEXT_FOUND)
     66             target_link_libraries(${SO} ${TEMP_DEPENDENCIES})
     67             # For Windows only, have the module link to the export library of Anope as well as wsock32 and Ws2_32 libraries (most of the modules probably don't need this, but this is to be on the safe side), also set its version
     68             if(WIN32)
     69               target_link_libraries(${SO} ${PROGRAM_NAME} wsock32 Ws2_32 ${WIN32_MEMORY})
     70               set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}")
     71             else(WIN32)
     72               if(APPLE)
     73                 target_link_libraries(${SO} ${PROGRAM_NAME})
     74               endif(APPLE)
     75             endif(WIN32)
     76             # Set the module to be installed to the module directory under the data directory
     77             install(TARGETS ${SO} DESTINATION ${LIB_DIR}/modules)
     78           endif(HAS_FUNCTION)
     79         endif(CPP)
     80       endif(IS_DIRECTORY "${MODULE_SRC}")
     81     endforeach(MODULE_SRC ${MODULES_SRCS})
     82   endif(NOT ${SRC} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR} AND EXISTS "${SRC}/CMakeLists.txt")
     83 endmacro(build_modules)
     84 
     85 macro(build_subdir)
     86   file(GLOB_RECURSE MODULES_SUBDIR_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cpp")
     87   sort_list(MODULES_SUBDIR_SRCS)
     88 
     89   GET_FILENAME_COMPONENT(FOLDER_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
     90   set(SO "${FOLDER_NAME}.so")
     91 
     92   # Set all the files to use C++ as well as set their compile flags (use the module-specific compile flags, though)
     93   set_source_files_properties(${MODULES_SUBDIR_SRCS} PROPERTIES LANGUAGE CXX COMPILE_FLAGS "${CXXFLAGS}")
     94 
     95   set(HAS_FUNCTION TRUE)
     96 
     97   # Iterate through the source files in the subdirectory
     98   foreach(SRC ${MODULES_SUBDIR_SRCS})
     99     if(HAS_FUNCTION)
    100       # Temporary variable for the current source's include directories
    101       set(TEMP_INCLUDES)
    102       # Calculate the header file dependencies for the given source file
    103       calculate_depends(${SRC} TEMP_INCLUDES)
    104       # If there were some extra include directories, add them to the list
    105       if(TEMP_INCLUDES)
    106         include_directories(${TEMP_INCLUDES})
    107       endif(TEMP_INCLUDES)
    108 
    109       # Reset linker flags
    110       set(TEMP_LDFLAGS)
    111       # Reset extra dependencies
    112       set(TEMP_DEPENDENCIES)
    113       # Calculate the library dependencies for the given source file
    114       calculate_libraries(${SRC} SKIP_LIBRARIES MODULE TEMP_LDFLAGS TEMP_DEPENDENCIES)
    115       # Check the function dependencies for the given source file
    116       check_functions(${SRC} HAS_FUNCTION)
    117 
    118       # Append this source file's linker flags to the subdirectoy's linker flags, if there are any to append
    119       if(TEMP_DEPENDENCIES)
    120         append_to_list(SUBDIR_EXTRA_DEPENDS ${TEMP_DEPENDENCIES})
    121       endif(TEMP_DEPENDENCIES)
    122     endif(HAS_FUNCTION)
    123   endforeach(SRC ${MODULES_SUBDIR_SRCS})
    124 
    125   # Continue if library and function requirements are met
    126   if(HAS_FUNCTION)
    127     # Remove duplicates from the linker flags
    128     if(SUBDIR_LDFLAGS)
    129       remove_list_duplicates(SUBDIR_LDFLAGS)
    130     endif(SUBDIR_LDFLAGS)
    131 
    132     # Remove duplicates from the extra dependencies
    133     if(SUBDIR_EXTRA_DEPENDS)
    134       remove_list_duplicates(SUBDIR_EXTRA_DEPENDS)
    135     endif(SUBDIR_EXTRA_DEPENDS)
    136 
    137     # For Visual Studio only, include win32_memory static library, required to override Visual Studio's overrides of the new/delete operators
    138     if(MSVC)
    139       set(WIN32_MEMORY win32_memory)
    140     else(MSVC)
    141       set(WIN32_MEMORY)
    142     endif(MSVC)
    143 
    144     # Generate the module and set it's linker flags, also set it to depend on the main Anope executable to be built beforehand
    145     add_library(${SO} MODULE ${MODULES_SUBDIR_SRCS})
    146     set_target_properties(${SO} PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX "" LINK_FLAGS "${SUBDIR_LDFLAGS}" INSTALL_RPATH_USE_LINK_PATH ON BUILD_WITH_INSTALL_RPATH ON)
    147     add_dependencies(${SO} ${PROGRAM_NAME})
    148     if(GETTEXT_FOUND)
    149       add_dependencies(${SO} module_language)
    150     endif(GETTEXT_FOUND)
    151     target_link_libraries(${SO} ${SUBDIR_EXTRA_DEPENDS})
    152     # For Windows only, have the module link to the export library of Anope as well as wsock32 and Ws2_32 libraries (most of the modules probably don't need this, but this is to be on the safe side), also set it's version
    153     if(WIN32)
    154       target_link_libraries(${SO} ${PROGRAM_NAME} wsock32 Ws2_32 ${WIN32_MEMORY})
    155       set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}")
    156     else(WIN32)
    157       if(APPLE)
    158         target_link_libraries(${SO} ${PROGRAM_NAME})
    159       endif(APPLE)
    160     endif(WIN32)
    161 
    162     # Set the module to be installed to the module directory under the data directory
    163     install(TARGETS ${SO} DESTINATION ${LIB_DIR}/modules)
    164 
    165   endif(HAS_FUNCTION)
    166 endmacro(build_subdir)
    167 
    168 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
    169 build_modules(${CMAKE_CURRENT_SOURCE_DIR})