|
| 1 | +const { withPodfile, createRunOncePlugin } = require("expo/config-plugins") |
| 2 | + |
| 3 | +const HELPER_NAME = "patch_fmt_xcode_26_compatibility" |
| 4 | + |
| 5 | +const FMT_SOURCE_BLOCK = `// Detect consteval, C++20 constexpr extensions and std::is_constant_evaluated. |
| 6 | +#if !defined(__cpp_lib_is_constant_evaluated) |
| 7 | +# define FMT_USE_CONSTEVAL 0 |
| 8 | +#elif FMT_CPLUSPLUS < 201709L |
| 9 | +# define FMT_USE_CONSTEVAL 0 |
| 10 | +#elif FMT_GLIBCXX_RELEASE && FMT_GLIBCXX_RELEASE < 10 |
| 11 | +# define FMT_USE_CONSTEVAL 0 |
| 12 | +#elif FMT_LIBCPP_VERSION && FMT_LIBCPP_VERSION < 10000 |
| 13 | +# define FMT_USE_CONSTEVAL 0 |
| 14 | +#elif defined(__apple_build_version__) && __apple_build_version__ < 14000029L |
| 15 | +# define FMT_USE_CONSTEVAL 0 // consteval is broken in Apple clang < 14. |
| 16 | +#elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1929 |
| 17 | +# define FMT_USE_CONSTEVAL 0 // consteval is broken in MSVC VS2019 < 16.10. |
| 18 | +#elif defined(__cpp_consteval) |
| 19 | +# define FMT_USE_CONSTEVAL 1 |
| 20 | +#elif FMT_GCC_VERSION >= 1002 || FMT_CLANG_VERSION >= 1101 |
| 21 | +# define FMT_USE_CONSTEVAL 1 |
| 22 | +#else |
| 23 | +# define FMT_USE_CONSTEVAL 0 |
| 24 | +#endif` |
| 25 | + |
| 26 | +const FMT_PATCHED_BLOCK = `// Detect consteval, C++20 constexpr extensions and std::is_constant_evaluated. |
| 27 | +#if !defined(FMT_USE_CONSTEVAL) |
| 28 | +# if !defined(__cpp_lib_is_constant_evaluated) |
| 29 | +# define FMT_USE_CONSTEVAL 0 |
| 30 | +# elif FMT_CPLUSPLUS < 201709L |
| 31 | +# define FMT_USE_CONSTEVAL 0 |
| 32 | +# elif FMT_GLIBCXX_RELEASE && FMT_GLIBCXX_RELEASE < 10 |
| 33 | +# define FMT_USE_CONSTEVAL 0 |
| 34 | +# elif FMT_LIBCPP_VERSION && FMT_LIBCPP_VERSION < 10000 |
| 35 | +# define FMT_USE_CONSTEVAL 0 |
| 36 | +# elif defined(__apple_build_version__) && __apple_build_version__ < 14000029L |
| 37 | +# define FMT_USE_CONSTEVAL 0 // consteval is broken in Apple clang < 14. |
| 38 | +# elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1929 |
| 39 | +# define FMT_USE_CONSTEVAL 0 // consteval is broken in MSVC VS2019 < 16.10. |
| 40 | +# elif defined(__cpp_consteval) |
| 41 | +# define FMT_USE_CONSTEVAL 1 |
| 42 | +# elif FMT_GCC_VERSION >= 1002 || FMT_CLANG_VERSION >= 1101 |
| 43 | +# define FMT_USE_CONSTEVAL 1 |
| 44 | +# else |
| 45 | +# define FMT_USE_CONSTEVAL 0 |
| 46 | +# endif |
| 47 | +#endif` |
| 48 | + |
| 49 | +const RUBY_HELPER = ` |
| 50 | +def ${HELPER_NAME}(installer) |
| 51 | + installer.pods_project.targets.each do |target| |
| 52 | + next unless target.name == 'fmt' |
| 53 | +
|
| 54 | + target.build_configurations.each do |build_config| |
| 55 | + definitions = build_config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] || ['$(inherited)'] |
| 56 | + definitions = [definitions] unless definitions.is_a?(Array) |
| 57 | + definitions << 'FMT_USE_CONSTEVAL=0' unless definitions.include?('FMT_USE_CONSTEVAL=0') |
| 58 | + build_config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = definitions |
| 59 | + end |
| 60 | + end |
| 61 | +
|
| 62 | + base_header = File.join(installer.sandbox.root.to_s, 'fmt', 'include', 'fmt', 'base.h') |
| 63 | + return unless File.exist?(base_header) |
| 64 | +
|
| 65 | + source = File.read(base_header) |
| 66 | + current = <<~'FMT' |
| 67 | +${FMT_SOURCE_BLOCK} |
| 68 | + FMT |
| 69 | + replacement = <<~'FMT' |
| 70 | +${FMT_PATCHED_BLOCK} |
| 71 | + FMT |
| 72 | +
|
| 73 | + if source.include?(current) && !source.include?('#if !defined(FMT_USE_CONSTEVAL)') |
| 74 | + File.write(base_header, source.sub(current, replacement)) |
| 75 | + end |
| 76 | +end |
| 77 | +`.trim() |
| 78 | + |
| 79 | +function addHelper(contents) { |
| 80 | + if (contents.includes(`def ${HELPER_NAME}(installer)`)) { |
| 81 | + return contents |
| 82 | + } |
| 83 | + |
| 84 | + const anchor = "target 'NitroStorage' do" |
| 85 | + if (!contents.includes(anchor)) { |
| 86 | + throw new Error(`Could not find Podfile anchor: ${anchor}`) |
| 87 | + } |
| 88 | + |
| 89 | + return contents.replace(anchor, `${RUBY_HELPER}\n\n${anchor}`) |
| 90 | +} |
| 91 | + |
| 92 | +function addPostInstallCall(contents) { |
| 93 | + const call = ` ${HELPER_NAME}(installer)\n` |
| 94 | + if (contents.includes(call)) { |
| 95 | + return contents |
| 96 | + } |
| 97 | + |
| 98 | + return contents.replace( |
| 99 | + /( post_install do \|installer\|\n[\s\S]*?)( end\nend)/, |
| 100 | + `$1\n${call}$2`, |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +const withFmtIosCompat = (config) => |
| 105 | + withPodfile(config, (config) => { |
| 106 | + let contents = config.modResults.contents |
| 107 | + contents = addHelper(contents) |
| 108 | + contents = addPostInstallCall(contents) |
| 109 | + config.modResults.contents = contents |
| 110 | + return config |
| 111 | + }) |
| 112 | + |
| 113 | +module.exports = createRunOncePlugin( |
| 114 | + withFmtIosCompat, |
| 115 | + "with-fmt-ios-compat", |
| 116 | + "1.0.0", |
| 117 | +) |
0 commit comments