|
| 1 | +# Check all files in thrust to make sure that they use |
| 2 | +# THRUST_NAMESPACE_BEGIN/END instead of bare `namespace thrust {}` declarations. |
| 3 | +# |
| 4 | +# This is run as a ctest test named `thrust.test.cmake.check_namespace`, or |
| 5 | +# manually with: |
| 6 | +# cmake -D "Thrust_SOURCE_DIR=<thrust project root>" -P check_namespace.cmake |
| 7 | + |
| 8 | +cmake_minimum_required(VERSION 3.15) |
| 9 | + |
| 10 | +set(exclusions |
| 11 | + # This defines the macros and must have bare namespace declarations: |
| 12 | + thrust/detail/config/namespace.h |
| 13 | +) |
| 14 | + |
| 15 | +function(count_substrings input search_regex output_var) |
| 16 | + string(REGEX MATCHALL "${search_regex}" matches "${input}") |
| 17 | + list(LENGTH matches num_matches) |
| 18 | + set(${output_var} ${num_matches} PARENT_SCOPE) |
| 19 | +endfunction() |
| 20 | + |
| 21 | +set(bare_ns_regex "namespace[ \n\r\t]+thrust[ \n\r\t]*\\{") |
| 22 | + |
| 23 | +# Validation check for the above regex: |
| 24 | +count_substrings([=[ |
| 25 | +namespace thrust{ |
| 26 | +namespace thrust { |
| 27 | +namespace thrust { |
| 28 | + namespace thrust { |
| 29 | +namespace thrust |
| 30 | +{ |
| 31 | +namespace |
| 32 | +thrust |
| 33 | +{ |
| 34 | +]=] |
| 35 | + ${bare_ns_regex} valid_count) |
| 36 | +if (NOT valid_count EQUAL 6) |
| 37 | + message(FATAL_ERROR "Validation of bare namespace regex failed: " |
| 38 | + "Matched ${valid_count} times, expected 6.") |
| 39 | +endif() |
| 40 | + |
| 41 | +set(found_errors 0) |
| 42 | +file(GLOB_RECURSE thrust_srcs |
| 43 | + RELATIVE "${Thrust_SOURCE_DIR}" |
| 44 | + "${Thrust_SOURCE_DIR}/*.h" |
| 45 | + "${Thrust_SOURCE_DIR}/*.inl" |
| 46 | + "${Thrust_SOURCE_DIR}/*.cu" |
| 47 | +) |
| 48 | + |
| 49 | +foreach(src ${thrust_srcs}) |
| 50 | + if (${src} IN_LIST exclusions) |
| 51 | + continue() |
| 52 | + endif() |
| 53 | + |
| 54 | + file(READ "${Thrust_SOURCE_DIR}/${src}" src_contents) |
| 55 | + |
| 56 | + count_substrings("${src_contents}" "${bare_ns_regex}" bare_ns_count) |
| 57 | + count_substrings("${src_contents}" THRUST_NS_PREFIX prefix_count) |
| 58 | + count_substrings("${src_contents}" THRUST_NS_POSTFIX postfix_count) |
| 59 | + count_substrings("${src_contents}" THRUST_NAMESPACE_BEGIN begin_count) |
| 60 | + count_substrings("${src_contents}" THRUST_NAMESPACE_END end_count) |
| 61 | + count_substrings("${src_contents}" "#include <thrust/detail/config.h>" header_count) |
| 62 | + |
| 63 | + if (NOT bare_ns_count EQUAL 0) |
| 64 | + message("'${src}' contains 'namespace thrust {...}'. Replace with THRUST_NAMESPACE macros.") |
| 65 | + set(found_errors 1) |
| 66 | + endif() |
| 67 | + |
| 68 | + if (NOT prefix_count EQUAL 0) |
| 69 | + message("'${src}' contains 'THRUST_NS_PREFIX'. Replace with THRUST_NAMESPACE macros.") |
| 70 | + set(found_errors 1) |
| 71 | + endif() |
| 72 | + |
| 73 | + if (NOT postfix_count EQUAL 0) |
| 74 | + message("'${src}' contains 'THRUST_NS_POSTFIX'. Replace with THRUST_NAMESPACE macros.") |
| 75 | + set(found_errors 1) |
| 76 | + endif() |
| 77 | + |
| 78 | + if (NOT begin_count EQUAL end_count) |
| 79 | + message("'${src}' namespace macros are unbalanced:") |
| 80 | + message(" - THRUST_NAMESPACE_BEGIN occurs ${begin_count} times.") |
| 81 | + message(" - THRUST_NAMESPACE_END occurs ${end_count} times.") |
| 82 | + set(found_errors 1) |
| 83 | + endif() |
| 84 | + |
| 85 | + if (begin_count GREATER 0 AND header_count EQUAL 0) |
| 86 | + message("'${src}' uses Thrust namespace macros, but does not (directly) `#include <thrust/detail/config.h>`.") |
| 87 | + set(found_errors 1) |
| 88 | + endif() |
| 89 | +endforeach() |
| 90 | + |
| 91 | +if (NOT found_errors EQUAL 0) |
| 92 | + message(FATAL_ERROR "Errors detected.") |
| 93 | +endif() |
0 commit comments