1 function(auto_sources RETURN_VALUE PATTERN SOURCE_SUBDIRS)
2 if ("${SOURCE_SUBDIRS}" STREQUAL "RECURSE")
9 if ("${SOURCE_SUBDIRS}" STREQUAL "RECURSE")
10 unset(${RETURN_VALUE})
11 file(GLOB SUBDIR_FILES "${PATH}/${PATTERN}")
12 list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})
14 file(GLOB subdirs RELATIVE ${PATH} ${PATH}/*)
16 foreach(DIR ${subdirs})
17 if (IS_DIRECTORY ${PATH}/${DIR})
18 if (NOT "${DIR}" STREQUAL "CMakeFiles")
19 file(GLOB_RECURSE SUBDIR_FILES "${PATH}/${DIR}/${PATTERN}")
20 list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})
25 file(GLOB ${RETURN_VALUE} "${PATTERN}")
27 foreach (PATH ${SOURCE_SUBDIRS})
28 file(GLOB SUBDIR_FILES "${PATH}/${PATTERN}")
29 list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})
33 set(${RETURN_VALUE} ${${RETURN_VALUE}} PARENT_SCOPE)
34 endfunction(auto_sources)
36 # Remove all files matching a set of patterns, and,
37 # optionally, not matching a second set of patterns,
38 # from a set of lists.
41 # This will remove all files in the CPP_SOURCES list
42 # matching "/test/" or "Test.cpp$", but not matching
44 # REMOVE_MATCHES_FROM_LISTS(CPP_SOURCES MATCHES "/test/" "Test.cpp$" IGNORE_MATCHES "BobTest.cpp$")
49 # The names of the lists to remove matches from.
52 # The matches to remove from the lists.
54 # [IGNORE_MATCHES ...]:
55 # The matches not to remove, even if they match
56 # the main set of matches to remove.
57 function(REMOVE_MATCHES_FROM_LISTS)
59 set(MATCHES_TO_REMOVE)
60 set(MATCHES_TO_IGNORE)
63 if ("x${arg}" STREQUAL "xMATCHES")
65 elseif ("x${arg}" STREQUAL "xIGNORE_MATCHES")
67 elseif (argumentState EQUAL 0)
68 list(APPEND LISTS_TO_SEARCH ${arg})
69 elseif (argumentState EQUAL 1)
70 list(APPEND MATCHES_TO_REMOVE ${arg})
71 elseif (argumentState EQUAL 2)
72 list(APPEND MATCHES_TO_IGNORE ${arg})
74 message(FATAL_ERROR "Unknown argument state!")
78 foreach (theList ${LISTS_TO_SEARCH})
79 foreach (entry ${${theList}})
80 foreach (match ${MATCHES_TO_REMOVE})
81 if (${entry} MATCHES ${match})
82 set(SHOULD_IGNORE OFF)
83 foreach (ign ${MATCHES_TO_IGNORE})
84 if (${entry} MATCHES ${ign})
90 if (NOT SHOULD_IGNORE)
91 list(REMOVE_ITEM ${theList} ${entry})
96 set(${theList} ${${theList}} PARENT_SCOPE)
100 # Automatically create source_group directives for the sources passed in.
101 function(auto_source_group rootName rootDir)
102 file(TO_CMAKE_PATH "${rootDir}" rootDir)
103 string(LENGTH "${rootDir}" rootDirLength)
105 foreach (fil ${ARGN})
106 file(TO_CMAKE_PATH "${fil}" filePath)
107 string(FIND "${filePath}" "/" rIdx REVERSE)
109 message(FATAL_ERROR "Unable to locate the final forward slash in '${filePath}'!")
111 string(SUBSTRING "${filePath}" 0 ${rIdx} filePath)
113 string(LENGTH "${filePath}" filePathLength)
114 string(FIND "${filePath}" "${rootDir}" rIdx)
116 math(EXPR filePathLength "${filePathLength} - ${rootDirLength}")
117 string(SUBSTRING "${filePath}" ${rootDirLength} ${filePathLength} fileGroup)
119 string(REPLACE "/" "\\" fileGroup "${fileGroup}")
120 set(fileGroup "\\${rootName}${fileGroup}")
122 list(FIND sourceGroups "${fileGroup}" rIdx)
124 list(APPEND sourceGroups "${fileGroup}")
125 source_group("${fileGroup}" REGULAR_EXPRESSION "${filePath}/[^/.]+.(cpp|h)$")
131 # CMake is a pain and doesn't have an easy way to install only the files
132 # we actually included in our build :(
133 function(auto_install_files rootName rootDir)
134 file(TO_CMAKE_PATH "${rootDir}" rootDir)
135 string(LENGTH "${rootDir}" rootDirLength)
137 foreach (fil ${ARGN})
138 file(TO_CMAKE_PATH "${fil}" filePath)
139 string(FIND "${filePath}" "/" rIdx REVERSE)
141 message(FATAL_ERROR "Unable to locate the final forward slash in '${filePath}'!")
143 string(SUBSTRING "${filePath}" 0 ${rIdx} filePath)
145 string(LENGTH "${filePath}" filePathLength)
146 string(FIND "${filePath}" "${rootDir}" rIdx)
148 math(EXPR filePathLength "${filePathLength} - ${rootDirLength}")
149 string(SUBSTRING "${filePath}" ${rootDirLength} ${filePathLength} fileGroup)
150 install(FILES ${fil} DESTINATION include/${rootName}${fileGroup})
155 function(folly_define_tests)
156 set(directory_count 0)
159 while (currentArg LESS ${ARGC})
160 if ("x${ARGV${currentArg}}" STREQUAL "xDIRECTORY")
161 math(EXPR currentArg "${currentArg} + 1")
162 if (NOT currentArg LESS ${ARGC})
163 message(FATAL_ERROR "Expected base directory!")
166 set(cur_dir ${directory_count})
167 math(EXPR directory_count "${directory_count} + 1")
168 set(directory_${cur_dir}_name "${ARGV${currentArg}}")
169 # We need a single list of sources to get source_group to work nicely.
170 set(directory_${cur_dir}_source_list)
172 math(EXPR currentArg "${currentArg} + 1")
173 while (currentArg LESS ${ARGC})
174 if ("x${ARGV${currentArg}}" STREQUAL "xDIRECTORY")
176 elseif ("x${ARGV${currentArg}}" STREQUAL "xTEST")
177 math(EXPR currentArg "${currentArg} + 1")
178 if (NOT currentArg LESS ${ARGC})
179 message(FATAL_ERROR "Expected test name!")
182 set(cur_test ${test_count})
183 math(EXPR test_count "${test_count} + 1")
184 set(test_${cur_test}_name "${ARGV${currentArg}}")
185 math(EXPR currentArg "${currentArg} + 1")
186 set(test_${cur_test}_directory ${cur_dir})
187 set(test_${cur_test}_content_dir)
188 set(test_${cur_test}_headers)
189 set(test_${cur_test}_sources)
190 set(test_${cur_test}_tag "NONE")
193 while (currentArg LESS ${ARGC})
194 if ("x${ARGV${currentArg}}" STREQUAL "xHEADERS")
196 elseif ("x${ARGV${currentArg}}" STREQUAL "xSOURCES")
198 elseif ("x${ARGV${currentArg}}" STREQUAL "xCONTENT_DIR")
199 math(EXPR currentArg "${currentArg} + 1")
200 if (NOT currentArg LESS ${ARGC})
201 message(FATAL_ERROR "Expected content directory name!")
203 set(test_${cur_test}_content_dir "${ARGV${currentArg}}")
204 elseif ("x${ARGV${currentArg}}" STREQUAL "xTEST" OR
205 "x${ARGV${currentArg}}" STREQUAL "xDIRECTORY")
207 elseif (argumentState EQUAL 0)
208 if ("x${ARGV${currentArg}}" STREQUAL "xBROKEN")
209 set(test_${cur_test}_tag "BROKEN")
210 elseif ("x${ARGV${currentArg}}" STREQUAL "xHANGING")
211 set(test_${cur_test}_tag "HANGING")
212 elseif ("x${ARGV${currentArg}}" STREQUAL "xSLOW")
213 set(test_${cur_test}_tag "SLOW")
215 message(FATAL_ERROR "Unknown test tag '${ARGV${currentArg}}'!")
217 elseif (argumentState EQUAL 1)
218 list(APPEND test_${cur_test}_headers
219 "${FOLLY_DIR}/${directory_${cur_dir}_name}${ARGV${currentArg}}"
221 elseif (argumentState EQUAL 2)
222 list(APPEND test_${cur_test}_sources
223 "${FOLLY_DIR}/${directory_${cur_dir}_name}${ARGV${currentArg}}"
226 message(FATAL_ERROR "Unknown argument state!")
228 math(EXPR currentArg "${currentArg} + 1")
231 list(APPEND directory_${cur_dir}_source_list
232 ${test_${cur_test}_sources} ${test_${cur_test}_headers})
234 message(FATAL_ERROR "Unknown argument inside directory '${ARGV${currentArg}}'!")
238 message(FATAL_ERROR "Unknown argument '${ARGV${currentArg}}'!")
243 while (cur_dir LESS directory_count)
244 source_group("" FILES ${directory_${cur_dir}_source_list})
245 math(EXPR cur_dir "${cur_dir} + 1")
249 while (cur_test LESS test_count)
250 if ("x${test_${cur_test}_tag}" STREQUAL "xNONE" OR
251 ("x${test_${cur_test}_tag}" STREQUAL "xBROKEN" AND BUILD_BROKEN_TESTS) OR
252 ("x${test_${cur_test}_tag}" STREQUAL "xSLOW" AND BUILD_SLOW_TESTS) OR
253 ("x${test_${cur_test}_tag}" STREQUAL "xHANGING" AND BUILD_HANGING_TESTS)
255 set(cur_test_name ${test_${cur_test}_name})
256 set(cur_dir_name ${directory_${test_${cur_test}_directory}_name})
257 add_executable(${cur_test_name}
258 ${test_${cur_test}_headers}
259 ${test_${cur_test}_sources}
261 if (NOT "x${test_${cur_test}_content_dir}" STREQUAL "x")
262 # Copy the content directory to the output directory tree so that
263 # tests can be run easily from Visual Studio without having to change
264 # the working directory for each test individually.
266 COPY "${FOLLY_DIR}/${cur_dir_name}${test_${cur_test}_content_dir}"
267 DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/folly/${cur_dir_name}${test_${cur_test}_content_dir}"
269 add_custom_command(TARGET ${cur_test_name} POST_BUILD COMMAND
270 ${CMAKE_COMMAND} ARGS -E copy_directory
271 "${FOLLY_DIR}/${cur_dir_name}${test_${cur_test}_content_dir}"
272 "$<TARGET_FILE_DIR:${cur_test_name}>/folly/${cur_dir_name}${test_${cur_test}_content_dir}"
273 COMMENT "Copying test content for ${cur_test_name}" VERBATIM
276 # Strip the tailing test directory name for the folder name.
277 string(REPLACE "test/" "" test_dir_name "${cur_dir_name}")
278 set_property(TARGET ${cur_test_name} PROPERTY FOLDER "Tests/${test_dir_name}")
279 target_link_libraries(${cur_test_name} PRIVATE folly_test_support)
280 apply_folly_compile_options_to_target(${cur_test_name})
282 math(EXPR cur_test "${cur_test} + 1")