blob: ccc734ed96e1e62d419fbb60fb31c7442184f7a5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
###############################################################################
# calculate_libraries(<source filename> <output variable for linker flags> <output variable for extra depends>)
#
# This macro is used in most of the module (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] */
if(WIN32)
file(STRINGS ${SRC} REQUIRED_LIBRARIES REGEX "/\\*[ \t]*RequiredWindowsLibraries:[ \t]*.*[ \t]*\\*/")
else()
file(STRINGS ${SRC} REQUIRED_LIBRARIES REGEX "/\\*[ \t]*RequiredLibraries:[ \t]*.*[ \t]*\\*/")
endif()
# Iterate through those lines
foreach(REQUIRED_LIBRARY ${REQUIRED_LIBRARIES})
# Strip off the /* RequiredLibraries: and */ from the line
string(REGEX REPLACE "/\\*[ \t]*Required.*Libraries:[ \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
find_library(FOUND_${LIBRARY}_LIBRARY NAMES ${LIBRARY} PATHS $ENV{VCINSTALLDIR}/lib ${EXTRA_INCLUDE} ${EXTRA_LIBS})
# 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
list(APPEND EXTRA_DEPENDENCIES "${FOUND_${LIBRARY}_LIBRARY}")
else()
# For all others, add the library paths and libraries
list(APPEND LIBRARY_PATHS "${LIBRARY_PATH}")
list(APPEND LIBRARIES "${LIBRARY}")
endif()
else()
# 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()
endforeach()
endforeach()
# Remove duplicates from the library paths
if(LIBRARY_PATHS)
list(REMOVE_DUPLICATES LIBRARY_PATHS)
endif()
# Remove diplicates from the libraries
if(LIBRARIES)
list(REMOVE_DUPLICATES LIBRARIES)
endif()
# Iterate through library paths and add them to the linker flags
foreach(LIBRARY_PATH ${LIBRARY_PATHS})
set(THIS_LDFLAGS "${THIS_LDFLAGS} -L${LIBRARY_PATH}")
endforeach()
# Iterate through libraries and add them to the linker flags
foreach(LIBRARY ${LIBRARIES})
list(APPEND EXTRA_DEPENDENCIES "${LIBRARY}")
endforeach()
set(${SRC_LDFLAGS} "${THIS_LDFLAGS}")
set(${EXTRA_DEPENDS} "${EXTRA_DEPENDENCIES}")
endmacro()
###############################################################################
# add_to_cpack_ignored_files(<item> [TRUE])
#
# A macro to update the environment variable CPACK_IGNORED_FILES which
# contains a list of files for CPack to ignore. If the optional 2nd argument
# of TRUE is given, periods will be converted to \\. for CPack.
###############################################################################
macro(add_to_cpack_ignored_files ITEM)
# Temporary copy of the orignal item
set(REAL_ITEM "${ITEM}")
# If we have 2+ arguments, assume that the second one was something like TRUE (doesn't matter really) and convert periods so they will be \\. for CPack
if(${ARGC} GREATER 1)
string(REPLACE "." "\\\\." REAL_ITEM ${REAL_ITEM})
endif()
# If the environment variable is already defined, just tack the item to the end
if(DEFINED ENV{CPACK_IGNORED_FILES})
set(ENV{CPACK_IGNORED_FILES} "$ENV{CPACK_IGNORED_FILES};${REAL_ITEM}")
# Otherwise set the environment variable to the item
else()
set(ENV{CPACK_IGNORED_FILES} "${REAL_ITEM}")
endif()
endmacro()
macro(calculate_dependencies SRC DEPENDENCIES)
file(STRINGS ${SRC} REQUIRED_LIBRARIES REGEX "/\\*[ \t]*Dependencies:[ \t]*.*[ \t]*\\*/")
# Iterate through those lines
foreach(REQUIRED_LIBRARY ${REQUIRED_LIBRARIES})
string(REGEX REPLACE "/\\*[ \t]*Dependencies:[ \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})
list(APPEND ${DEPENDENCIES} "${LIBRARY}")
endforeach()
endforeach()
endmacro()
|