|
| 1 | +# Replaces boost::unordered_flat_{map,set} with std::unordered_{map,set} |
| 2 | +# in the fetched onpair_cpp source tree. Idempotent. |
| 3 | +# |
| 4 | +# Invoked by FetchContent_Declare(PATCH_COMMAND ...). |
| 5 | +# |
| 6 | +# We rewrite `#include <boost/unordered/...>` to `#include <unordered_{map,set}>` |
| 7 | +# and substitute the qualified types. OnPair only uses the public, std-compatible |
| 8 | +# subset of boost::unordered_flat_map (operator[], find, emplace, size, iterators), |
| 9 | +# so this is a sound substitution. |
| 10 | + |
| 11 | +if(NOT DEFINED SRC_DIR) |
| 12 | + message(FATAL_ERROR "strip_boost.cmake: SRC_DIR not set") |
| 13 | +endif() |
| 14 | + |
| 15 | +file(GLOB_RECURSE ONPAIR_SOURCES |
| 16 | + "${SRC_DIR}/include/onpair/*.h" |
| 17 | + "${SRC_DIR}/include/onpair/*.hpp" |
| 18 | + "${SRC_DIR}/src/onpair/*.cpp" |
| 19 | + "${SRC_DIR}/src/onpair/*.h" |
| 20 | + "${SRC_DIR}/src/onpair/*.hpp" |
| 21 | +) |
| 22 | + |
| 23 | +set(_PAIR_HASH_BLOCK |
| 24 | +"// strip_boost.cmake: std::hash<std::pair<uint64_t, uint8_t>> for unordered_map keys\n#include <cstdint>\n#include <functional>\n#include <utility>\nnamespace std {\ntemplate<> struct hash<std::pair<uint64_t, uint8_t>> {\n size_t operator()(const std::pair<uint64_t, uint8_t>& p) const noexcept {\n return std::hash<uint64_t>{}(p.first) ^ (std::hash<uint8_t>{}(p.second) << 1);\n }\n};\n} // namespace std\n") |
| 25 | + |
| 26 | +foreach(F ${ONPAIR_SOURCES}) |
| 27 | + file(READ "${F}" CONTENT) |
| 28 | + string(REGEX REPLACE |
| 29 | + "#include[ \t]+<boost/unordered/unordered_flat_map\\.hpp>" |
| 30 | + "#include <unordered_map>" CONTENT "${CONTENT}") |
| 31 | + string(REGEX REPLACE |
| 32 | + "#include[ \t]+<boost/unordered/unordered_flat_set\\.hpp>" |
| 33 | + "#include <unordered_set>" CONTENT "${CONTENT}") |
| 34 | + string(REGEX REPLACE |
| 35 | + "#include[ \t]+<boost/unordered\\.hpp>" |
| 36 | + "#include <unordered_map>\n#include <unordered_set>" CONTENT "${CONTENT}") |
| 37 | + string(REPLACE "boost::unordered_flat_map" "std::unordered_map" CONTENT "${CONTENT}") |
| 38 | + string(REPLACE "boost::unordered_flat_set" "std::unordered_set" CONTENT "${CONTENT}") |
| 39 | + string(REPLACE "boost::unordered::unordered_flat_map" "std::unordered_map" CONTENT "${CONTENT}") |
| 40 | + string(REPLACE "boost::unordered::unordered_flat_set" "std::unordered_set" CONTENT "${CONTENT}") |
| 41 | + # Inject the pair-hash specialization once, at the top of any file that |
| 42 | + # keys an unordered_map by std::pair. std::hash<std::pair<...>> does not |
| 43 | + # exist by default; boost::unordered_flat_map shipped its own. |
| 44 | + string(FIND "${CONTENT}" "unordered_map<std::pair" _has_pair_key) |
| 45 | + if(NOT _has_pair_key EQUAL -1) |
| 46 | + string(FIND "${CONTENT}" "strip_boost.cmake: std::hash<std::pair" _has_block) |
| 47 | + if(_has_block EQUAL -1) |
| 48 | + set(CONTENT "${_PAIR_HASH_BLOCK}${CONTENT}") |
| 49 | + endif() |
| 50 | + endif() |
| 51 | + file(WRITE "${F}" "${CONTENT}") |
| 52 | +endforeach() |
| 53 | + |
| 54 | +# Drop find_package(Boost) and Boost link lines from onpair_cpp's CMake files |
| 55 | +# so the build doesn't error out looking for Boost on the host. |
| 56 | +file(GLOB_RECURSE ONPAIR_CMAKE |
| 57 | + "${SRC_DIR}/CMakeLists.txt" |
| 58 | + "${SRC_DIR}/cmake/*.cmake" |
| 59 | +) |
| 60 | +foreach(F ${ONPAIR_CMAKE}) |
| 61 | + file(READ "${F}" CONTENT) |
| 62 | + string(REGEX REPLACE "find_package\\([ \t]*Boost[^)]*\\)" "" CONTENT "${CONTENT}") |
| 63 | + string(REGEX REPLACE "FetchContent_Declare\\([ \t\r\n]*Boost[^)]*\\)" "" CONTENT "${CONTENT}") |
| 64 | + string(REGEX REPLACE "FetchContent_MakeAvailable\\([ \t]*Boost[ \t]*\\)" "" CONTENT "${CONTENT}") |
| 65 | + string(REGEX REPLACE "Boost::[A-Za-z_]+" "" CONTENT "${CONTENT}") |
| 66 | + file(WRITE "${F}" "${CONTENT}") |
| 67 | +endforeach() |
0 commit comments