|
1 | 1 | find_path(Avro_INCLUDE_DIR NAMES avro/Encoder.hh) |
2 | 2 | find_library(Avro_LIBRARY NAMES avrocpp libavrocpp) |
3 | 3 |
|
| 4 | +# ============================================================================= |
| 5 | +# Workaround for conda-forge avro-cpp fmt::formatter incompatibility on Windows |
| 6 | +# ============================================================================= |
| 7 | +# conda-forge's avro-cpp has fmt::formatter specializations with non-const |
| 8 | +# format() methods, but fmt v12+ requires const. This causes MSVC error C2766. |
| 9 | +# |
| 10 | +# Solution: On Windows, we check if the avro headers have this bug, and if so, |
| 11 | +# we create patched versions in the build directory and prepend them to the |
| 12 | +# include path so they shadow the broken originals. |
| 13 | +# |
| 14 | +# This workaround can be removed once conda-forge updates avro-cpp. |
| 15 | +# ============================================================================= |
| 16 | + |
| 17 | +set(CSP_AVRO_PATCHED_INCLUDE_DIR "") |
| 18 | + |
| 19 | +if(WIN32 AND Avro_INCLUDE_DIR AND NOT CSP_USE_VCPKG) |
| 20 | + # Check if avro/Node.hh has the non-const format() bug |
| 21 | + # The buggy pattern is: "auto format(...) {" without "const" before the brace |
| 22 | + set(_avro_node_hh "${Avro_INCLUDE_DIR}/avro/Node.hh") |
| 23 | + set(_avro_types_hh "${Avro_INCLUDE_DIR}/avro/Types.hh") |
| 24 | + set(_needs_patching FALSE) |
| 25 | + |
| 26 | + if(EXISTS "${_avro_node_hh}") |
| 27 | + file(READ "${_avro_node_hh}" _node_hh_content) |
| 28 | + |
| 29 | + # Check if the file contains fmt::formatter and non-const format() |
| 30 | + # We look for "auto format" followed by ")" then whitespace then "{" |
| 31 | + # without "const" in between |
| 32 | + string(FIND "${_node_hh_content}" "fmt::formatter<avro::Name>" _has_formatter) |
| 33 | + if(NOT _has_formatter EQUAL -1) |
| 34 | + # Check specifically for the non-const pattern |
| 35 | + # Buggy: auto format(const avro::Name &n, FormatContext &ctx) { |
| 36 | + # Fixed: auto format(const avro::Name &n, FormatContext &ctx) const { |
| 37 | + string(REGEX MATCH "auto format\\(const avro::Name[^)]+\\)[^c]*\\{" _buggy_pattern "${_node_hh_content}") |
| 38 | + if(_buggy_pattern) |
| 39 | + set(_needs_patching TRUE) |
| 40 | + endif() |
| 41 | + endif() |
| 42 | + endif() |
| 43 | + |
| 44 | + if(_needs_patching) |
| 45 | + message(STATUS "Detected avro-cpp with non-const fmt::formatter bug - applying build-time patch") |
| 46 | + |
| 47 | + # Create patched headers directory structure |
| 48 | + set(CSP_AVRO_PATCHED_INCLUDE_DIR "${CMAKE_BINARY_DIR}/_patched_avro_headers") |
| 49 | + set(_patched_avro_dir "${CSP_AVRO_PATCHED_INCLUDE_DIR}/avro") |
| 50 | + file(MAKE_DIRECTORY "${_patched_avro_dir}") |
| 51 | + |
| 52 | + # Patch Node.hh |
| 53 | + # Replace: auto format(const avro::Name &n, FormatContext &ctx) { |
| 54 | + # With: auto format(const avro::Name &n, FormatContext &ctx) const { |
| 55 | + string(REGEX REPLACE |
| 56 | + "(auto format\\(const avro::Name[^)]+\\))[ \t\r\n]*(\\{)" |
| 57 | + "\\1 const \\2" |
| 58 | + _patched_node_content |
| 59 | + "${_node_hh_content}" |
| 60 | + ) |
| 61 | + file(WRITE "${_patched_avro_dir}/Node.hh" "${_patched_node_content}") |
| 62 | + message(STATUS " Patched: avro/Node.hh -> ${_patched_avro_dir}/Node.hh") |
| 63 | + |
| 64 | + # Patch Types.hh if it exists and has the same bug |
| 65 | + if(EXISTS "${_avro_types_hh}") |
| 66 | + file(READ "${_avro_types_hh}" _types_hh_content) |
| 67 | + string(FIND "${_types_hh_content}" "fmt::formatter<avro::Type>" _has_type_formatter) |
| 68 | + if(NOT _has_type_formatter EQUAL -1) |
| 69 | + string(REGEX MATCH "auto format\\(avro::Type[^)]+\\)[^c]*\\{" _types_buggy "${_types_hh_content}") |
| 70 | + if(_types_buggy) |
| 71 | + string(REGEX REPLACE |
| 72 | + "(auto format\\(avro::Type[^)]+\\))[ \t\r\n]*(\\{)" |
| 73 | + "\\1 const \\2" |
| 74 | + _patched_types_content |
| 75 | + "${_types_hh_content}" |
| 76 | + ) |
| 77 | + file(WRITE "${_patched_avro_dir}/Types.hh" "${_patched_types_content}") |
| 78 | + message(STATUS " Patched: avro/Types.hh -> ${_patched_avro_dir}/Types.hh") |
| 79 | + endif() |
| 80 | + endif() |
| 81 | + endif() |
| 82 | + endif() |
| 83 | +endif() |
| 84 | + |
4 | 85 | if (NOT TARGET Avro::avrocpp) |
5 | 86 | add_library(Avro::avrocpp SHARED IMPORTED) |
6 | 87 | set_property(TARGET Avro::avrocpp PROPERTY |
7 | 88 | IMPORTED_LOCATION "${Avro_LIBRARY}") |
8 | | - target_include_directories(Avro::avrocpp INTERFACE ${Avro_INCLUDE_DIR}) |
| 89 | + |
| 90 | + # If we have patched headers, prepend them to include path so they shadow originals |
| 91 | + if(CSP_AVRO_PATCHED_INCLUDE_DIR) |
| 92 | + target_include_directories(Avro::avrocpp INTERFACE |
| 93 | + ${CSP_AVRO_PATCHED_INCLUDE_DIR} # Patched headers first (shadows originals) |
| 94 | + ${Avro_INCLUDE_DIR} # Original headers for everything else |
| 95 | + ) |
| 96 | + else() |
| 97 | + target_include_directories(Avro::avrocpp INTERFACE ${Avro_INCLUDE_DIR}) |
| 98 | + endif() |
9 | 99 | endif() |
10 | 100 |
|
11 | 101 | include(FindPackageHandleStandardArgs) |
|
0 commit comments