1
0
mirror of https://github.com/anope/anope.git synced 2026-06-25 04:56:39 +02:00

Added ability for CMake to build a module from a subdirectory of src/modules.

Added calculate_libraries() CMake macro to condense library checking in CMakeLists.txt in src/modules.
Fixed slight problem with strip_string() macro call in root CMakeLists.txt.

git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/trunk@2302 5417fbe8-f217-4b02-8779-1006273d7864
This commit is contained in:
cyberbotx
2009-05-25 18:58:51 +00:00
parent 3fa978c958
commit c1bc7cbafc
3 changed files with 161 additions and 64 deletions
+72
View File
@@ -436,6 +436,78 @@ macro(calculate_depends SRC)
endif(HEADERS)
endmacro(calculate_depends)
###############################################################################
# calculate_libraries(<source filename> <output variable for linker flags> <output variable for extra depends>)
#
# This macro is used in most of the src (sub)directories to calculate the
# library dependencies for the given source file.
###############################################################################
macro(calculate_libraries SRC SRC_LDFLAGS EXTRA_DEPENDS)
# Set up a temporary LDFLAGS for this file
set(THIS_LDFLAGS "${LDFLAGS}")
# Reset extra dependencies
set(EXTRA_DEPENDENCIES)
# Reset library paths
set(LIBRARY_PATHS)
# Reset libraries
set(LIBRARIES)
# Check to see if there are any lines matching: /* RequiredLibraries: [something] */
read_from_file(${SRC} "/\\\\*[ \t]*RequiredLibraries:[ \t]*.*[ \t]*\\\\*/" REQUIRED_LIBRARIES)
# Iterate through those lines
foreach(REQUIRED_LIBRARY ${REQUIRED_LIBRARIES})
# Strip off the /* RequiredLibraries: and */ from the line
string(REGEX REPLACE "/\\*[ \t]*RequiredLibraries:[ \t]*([^ \t]*)[ \t]*\\*/" "\\1" REQUIRED_LIBRARY ${REQUIRED_LIBRARY})
# Replace all commas with semicolons
string(REGEX REPLACE "," ";" REQUIRED_LIBRARY ${REQUIRED_LIBRARY})
# Iterate through the libraries given
foreach(LIBRARY ${REQUIRED_LIBRARY})
# Locate the library to see if it exists
if(DEFAULT_LIBRARY_DIRS OR WSDK_PATH OR DEFINED $ENV{VCINSTALLDIR})
find_library(FOUND_${LIBRARY}_LIBRARY NAMES ${LIBRARY} PATHS ${DEFAULT_LIBRARY_DIRS} ${WSDK_PATH}/lib $ENV{VCINSTALLDIR}/lib)
else(DEFAULT_LIBRARY_DIRS OR WSDK_PATH OR DEFINED $ENV{VCINSTALLDIR})
find_library(FOUND_${LIBRARY}_LIBRARY NAMES ${LIBRARY})
endif(DEFAULT_LIBRARY_DIRS OR WSDK_PATH OR DEFINED $ENV{VCINSTALLDIR})
# If the library was found, we will add it to the linker flags
if(FOUND_${LIBRARY}_LIBRARY)
# Get the path only of the library, to add it to linker flags
get_filename_component(LIBRARY_PATH ${FOUND_${LIBRARY}_LIBRARY} PATH)
if(MSVC)
# For Visual Studio, instead of editing the linker flags, we'll add the library to a separate list of extra dependencies
append_to_list(EXTRA_DEPENDENCIES "${FOUND_${LIBRARY}_LIBRARY}")
else(MSVC)
# For all others, add the library paths and libraries
append_to_list(LIBRARY_PATHS "${LIBRARY_PATH}")
append_to_list(LIBRARIES "${LIBRARY}")
endif(MSVC)
else(FOUND_${LIBRARY}_LIBRARY)
# In the case of the library not being found, we fatally error so CMake stops trying to generate
message(FATAL_ERROR "${SRC} needs library ${LIBRARY} but we were unable to locate that library! Check that the library is within the search path of your OS.")
endif(FOUND_${LIBRARY}_LIBRARY)
endforeach(LIBRARY)
endforeach(REQUIRED_LIBRARY)
# Remove duplicates from the library paths
if(LIBRARY_PATHS)
remove_list_duplicates(LIBRARY_PATHS)
endif(LIBRARY_PATHS)
# Remove diplicates from the libraries
if(LIBRARIES)
remove_list_duplicates(LIBRARIES)
endif(LIBRARIES)
# Iterate through library paths and add them to the linker flags
foreach(LIBRARY_PATH ${LIBRARY_PATHS})
find_in_list(DEFAULT_LIBRARY_DIRS "${LIBRARY_PATH}" FOUND_IN_DEFAULTS)
if(FOUND_IN_DEFAULTS EQUAL -1)
set(THIS_LDFLAGS "${THIS_LDFLAGS} -L${LIBRARY_PATH}")
endif(FOUND_IN_DEFAULTS EQUAL -1)
endforeach(LIBRARY_PATH)
# Iterate through libraries and add them to the linker flags
foreach(LIBRARY ${LIBRARIES})
set(THIS_LDFLAGS "${THIS_LDFLAGS} -l${LIBRARY}")
endforeach(LIBRARY)
set(${SRC_LDFLAGS} ${THIS_LDFLAGS})
set(${EXTRA_DEPENDS} ${EXTRA_DEPENDENCIES})
endmacro(calculate_libraries)
###############################################################################
# add_to_cpack_ignored_files(<item> [TRUE])
#
+2 -2
View File
@@ -315,11 +315,11 @@ check_type_size(u_int32_t U_INT32_T)
# Strip the leading and trailing spaces from the compile flags
if(CXXFLAGS)
strip_string(STRIP ${CXXFLAGS} CXXFLAGS)
strip_string(${CXXFLAGS} CXXFLAGS)
endif(CXXFLAGS)
# Strip the leading and trailing spaces from the linker flags
if(LDFLAGS)
strip_string(STRIP ${LDFLAGS} LDFLAGS)
strip_string(${LDFLAGS} LDFLAGS)
endif(LDFLAGS)
# Search for the following programs
+87 -62
View File
@@ -27,79 +27,24 @@ foreach(SRC ${MODULES_SRCS})
if(TEMP_INCLUDES)
append_to_list(EXTRA_INCLUDES ${TEMP_INCLUDES})
endif(TEMP_INCLUDES)
# Set up a temporary LDFLAGS for this file
set(THIS_LDFLAGS "${LDFLAGS}")
# Reset linker flags
set(TEMP_LDFLAGS)
# Reset extra dependencies
set(EXTRA_DEPENDENCIES)
# Reset library paths
set(LIBRARY_PATHS)
# Reset libraries
set(LIBRARIES)
# Check to see if there are any lines matching: /* RequiredLibraries: [something] */
read_from_file(${SRC} "/\\\\*[ \t]*RequiredLibraries:[ \t]*.*[ \t]*\\\\*/" REQUIRED_LIBRARIES)
# Iterate through those lines
foreach(REQUIRED_LIBRARY ${REQUIRED_LIBRARIES})
# Strip off the /* RequiredLibraries: and */ from the line
string(REGEX REPLACE "/\\*[ \t]*RequiredLibraries:[ \t]*([^ \t]*)[ \t]*\\*/" "\\1" REQUIRED_LIBRARY ${REQUIRED_LIBRARY})
# Replace all commas with semicolons
string(REGEX REPLACE "," ";" REQUIRED_LIBRARY ${REQUIRED_LIBRARY})
# Iterate through the libraries given
foreach(LIBRARY ${REQUIRED_LIBRARY})
# Locate the library to see if it exists
if(DEFAULT_LIBRARY_DIRS OR WSDK_PATH OR DEFINED $ENV{VCINSTALLDIR})
find_library(FOUND_${LIBRARY}_LIBRARY NAMES ${LIBRARY} PATHS ${DEFAULT_LIBRARY_DIRS} ${WSDK_PATH}/lib $ENV{VCINSTALLDIR}/lib)
else(DEFAULT_LIBRARY_DIRS OR WSDK_PATH OR DEFINED $ENV{VCINSTALLDIR})
find_library(FOUND_${LIBRARY}_LIBRARY NAMES ${LIBRARY})
endif(DEFAULT_LIBRARY_DIRS OR WSDK_PATH OR DEFINED $ENV{VCINSTALLDIR})
# If the library was found, we will add it to the linker flags
if(FOUND_${LIBRARY}_LIBRARY)
# Get the path only of the library, to add it to linker flags
get_filename_component(LIBRARY_PATH ${FOUND_${LIBRARY}_LIBRARY} PATH)
if(MSVC)
# For Visual Studio, instead of editing the linker flags, we'll add the library to a separate list of extra dependencies
append_to_list(EXTRA_DEPENDENCIES "${FOUND_${LIBRARY}_LIBRARY}")
else(MSVC)
# For all others, add the library paths and libraries
append_to_list(LIBRARY_PATHS "${LIBRARY_PATH}")
append_to_list(LIBRARIES "${LIBRARY}")
endif(MSVC)
else(FOUND_${LIBRARY}_LIBRARY)
# In the case of the library not being found, we fatally error so CMake stops trying to generate
message(FATAL_ERROR "${SRC} needs library ${LIBRARY} but we were unable to locate that library! Check that the library is within the search path of your OS.")
endif(FOUND_${LIBRARY}_LIBRARY)
endforeach(LIBRARY)
endforeach(REQUIRED_LIBRARY)
set(TEMP_DEPENDENCIES)
# Calculate the library dependencies for the given source file
calculate_libraries(${SRC} TEMP_LDFLAGS TEMP_DEPENDENCIES)
# For Visual Studio only, include win32_memory.cpp to the list of sources, required to override Visual Studio's overrides of the new/delete operators
if(MSVC)
append_to_list(SRC ${Anope_SOURCE_DIR}/src/win32_memory.cpp)
set_source_files_properties(${Anope_SOURCE_DIR}/src/win32_memory.cpp LANGUAGE CXX COMPILE_FLAGS "${CXXFLAGS}")
endif(MSVC)
# Remove duplicates from the library paths
if(LIBRARY_PATHS)
remove_list_duplicates(LIBRARY_PATHS)
endif(LIBRARY_PATHS)
# Remove diplicates from the libraries
if(LIBRARIES)
remove_list_duplicates(LIBRARIES)
endif(LIBRARIES)
# Iterate through library paths and add them to the linker flags
foreach(LIBRARY_PATH ${LIBRARY_PATHS})
find_in_list(DEFAULT_LIBRARY_DIRS "${LIBRARY_PATH}" FOUND_IN_DEFAULTS)
if(FOUND_IN_DEFAULTS EQUAL -1)
set(THIS_LDFLAGS "${THIS_LDFLAGS} -L${LIBRARY_PATH}")
endif(FOUND_IN_DEFAULTS EQUAL -1)
endforeach(LIBRARY_PATH)
# Iterate through libraries and add them to the linker flags
foreach(LIBRARY ${LIBRARIES})
set(THIS_LDFLAGS "${THIS_LDFLAGS} -l${LIBRARY}")
endforeach(LIBRARY)
# Generate the module and set it's linker flags, also set it to depend on the main Anope executable to be built beforehand
add_library(${SO} MODULE ${SRC})
set_target_properties(${SO} PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX "" LINK_FLAGS "${THIS_LDFLAGS}")
set_target_properties(${SO} PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX "" LINK_FLAGS "${TEMP_LDFLAGS}")
add_dependencies(${SO} ${PROGRAM_NAME})
# For Windows only, have the module link to the export library of Anope as well as the wsock32 library (most of the modules probably don't need this, but this is to be on the safe side), also set it's version
if(WIN32)
target_link_libraries(${SO} ${PROGRAM_NAME} wsock32 ${EXTRA_DEPENDENCIES})
target_link_libraries(${SO} ${PROGRAM_NAME} wsock32 ${TEMP_DEPENDENCIES})
set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}")
endif(WIN32)
# Set the module to be installed to the module directory under the data directory
@@ -108,6 +53,86 @@ foreach(SRC ${MODULES_SRCS})
)
endforeach(SRC)
# Get a list of ALL files and directories within the current directory
file(GLOB MODULES_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*")
# Iterate through this directory searching for subdirectories, and creating modules for those subdirectories
foreach(FILE ${MODULES_FILES})
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}")
file(GLOB MODULES_SUBDIR_SRCS_C RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${FILE}/*.c")
file(GLOB MODULES_SUBDIR_SRCS_CPP RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${FILE}/*.cpp")
set(MODULES_SUBDIR_SRCS ${MODULES_SUBDIR_SRCS_C} ${MODULES_SUBDIR_SRCS_CPP})
sort_list(MODULES_SUBDIR_SRCS)
# Set all the files to use C++ as well as set their compile flags (use the module-specific compile flags, though)
set_source_files_properties(${MODULES_SUBDIR_SRCS} PROPERTIES LANGUAGE CXX COMPILE_FLAGS "${CXXFLAGS}")
# Add .so to the end of the directory name, this will be the module's name
set(SO "${FILE}.so")
# Temporary linker flags for this subdirectory
set(SUBDIR_LDFLAGS "${LDFLAGS}")
# Temporary extra dependencies for this subdirectory
set(SUBDIR_EXTRA_DEPENDS)
# Iterate through the source files in the subdirectory
foreach(SRC ${MODULES_SUBDIR_SRCS})
# Temporary variable for the current source's include directories
set(TEMP_INCLUDES)
# Calculate the header file dependencies for the given source file
calculate_depends(${SRC} TEMP_INCLUDES)
# If there were some extra include directories, add them to the list
if(TEMP_INCLUDES)
append_to_list(EXTRA_INCLUDES ${TEMP_INCLUDES})
endif(TEMP_INCLUDES)
# Reset linker flags
set(TEMP_LDFLAGS)
# Reset extra dependencies
set(TEMP_DEPENDENCIES)
# Calculate the library dependencies for the given source file
calculate_libraries(${SRC} TEMP_LDFLAGS TEMP_DEPENDENCIES)
# Append this source file's linker flags to the subdirectoy's linker flags, if there are any to append
if(TEMP_LDFLAGS)
append_to_list(SUBDIR_LDFLAGS ${TEMP_LDFLAGS})
endif(TEMP_LDFLAGS)
# Append this source file's extra dependencies to the subdirector's extra dependencies, if there are any to append
if(TEMP_DEPENDENCIES)
append_to_list(SUBDIR_EXTRA_DEPENDS ${TEMP_DEPENDENCIES})
endif(TEMP_DEPENDENCIES)
endforeach(SRC)
# Remove duplicates from the linker flags
if(SUBDIR_LDFLAGS)
remove_list_duplicates(SUBDIR_LDFLAGS)
endif(SUBDIR_LDFLAGS)
# Remove duplicates from the extra dependencies
if(SUBDIR_EXTRA_DEPENDS)
remove_list_duplicates(SUBDIR_EXTRA_DEPENDS)
endif(SUBDIR_EXTRA_DEPENDS)
# For Visual Studio only, include win32_memory.cpp to the list of sources, required to override Visual Studio's overrides of the new/delete operators
if(MSVC)
append_to_list(MODULE_SUBDIR_SRCS ${Anope_SOURCE_DIR}/src/win32_memory.cpp)
set_source_files_properties(${Anope_SOURCE_DIR}/src/win32_memory.cpp LANGUAGE CXX COMPILE_FLAGS "${CXXFLAGS}")
endif(MSVC)
# Generate the module and set it's linker flags, also set it to depend on the main Anope executable to be built beforehand
add_library(${SO} MODULE ${MODULES_SUBDIR_SRCS})
set_target_properties(${SO} PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX "" LINK_FLAGS "${SUBDIR_LDFLAGS}")
add_dependencies(${SO} ${PROGRAM_NAME})
# For Windows only, have the module link to the export library of Anope as well as the wsock32 library (most of the modules probably don't need this, but this is to be on the safe side), also set it's version
if(WIN32)
target_link_libraries(${SO} ${PROGRAM_NAME} wsock32 ${SUBDIR_EXTRA_DEPENDS})
set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}")
endif(WIN32)
# Set the module to be installed to the module directory under the data directory
install(TARGETS ${SO}
DESTINATION data/modules
)
endif(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}")
endforeach(FILE)
# If there were extra include directories, remove the duplicates and add the directories to the include path
if(EXTRA_INCLUDES)
remove_list_duplicates(EXTRA_INCLUDES)