|
| 1 | +module HarnessCoverageHook |
| 2 | + def run_podfile_post_install_hooks |
| 3 | + super |
| 4 | + |
| 5 | + pods = resolve_coverage_pods |
| 6 | + return if pods.empty? |
| 7 | + |
| 8 | + Pod::UI.puts "[HarnessCoverage] Instrumenting pods for native coverage: #{pods.join(', ')}" |
| 9 | + |
| 10 | + apply_coverage_flags_to_pods(pods) |
| 11 | + enable_harness_coverage_pod |
| 12 | + apply_linker_flags |
| 13 | + end |
| 14 | + |
| 15 | + private |
| 16 | + |
| 17 | + def resolve_coverage_pods |
| 18 | + project_dir = Dir.pwd |
| 19 | + config_json = `node -e " |
| 20 | + import('#{project_dir}/rn-harness.config.mjs') |
| 21 | + .then(m => console.log(JSON.stringify( |
| 22 | + m.default?.coverage?.native?.ios?.pods || [] |
| 23 | + ))) |
| 24 | + .catch(() => console.log('[]')) |
| 25 | + "`.strip |
| 26 | + JSON.parse(config_json) |
| 27 | + rescue => e |
| 28 | + Pod::UI.warn "[HarnessCoverage] Failed to read config: #{e.message}" |
| 29 | + [] |
| 30 | + end |
| 31 | + |
| 32 | + def apply_coverage_flags_to_pods(pods) |
| 33 | + pods_project.targets.each do |target| |
| 34 | + next unless pods.include?(target.name) |
| 35 | + |
| 36 | + target.build_configurations.each do |config| |
| 37 | + swift_flags = config.build_settings['OTHER_SWIFT_FLAGS'] || '$(inherited)' |
| 38 | + unless swift_flags.include?('-profile-generate') |
| 39 | + config.build_settings['OTHER_SWIFT_FLAGS'] = |
| 40 | + "#{swift_flags} -profile-generate -profile-coverage-mapping" |
| 41 | + end |
| 42 | + |
| 43 | + c_flags = config.build_settings['OTHER_CFLAGS'] || '$(inherited)' |
| 44 | + unless c_flags.include?('-fprofile-instr-generate') |
| 45 | + config.build_settings['OTHER_CFLAGS'] = |
| 46 | + "#{c_flags} -fprofile-instr-generate -fcoverage-mapping" |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + Pod::UI.puts "[HarnessCoverage] -> #{target.name}" |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + def enable_harness_coverage_pod |
| 55 | + pods_project.targets.each do |target| |
| 56 | + next unless target.name == 'HarnessCoverage' |
| 57 | + |
| 58 | + target.build_configurations.each do |config| |
| 59 | + swift_conditions = config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] || '$(inherited)' |
| 60 | + unless swift_conditions.include?('HARNESS_COVERAGE') |
| 61 | + config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = |
| 62 | + "#{swift_conditions} HARNESS_COVERAGE" |
| 63 | + end |
| 64 | + |
| 65 | + gcc_defs = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] || '$(inherited)' |
| 66 | + unless gcc_defs.include?('HARNESS_COVERAGE') |
| 67 | + config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = |
| 68 | + "#{gcc_defs} HARNESS_COVERAGE=1" |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def apply_linker_flags |
| 75 | + pods_project.targets.each do |target| |
| 76 | + target.build_configurations.each do |config| |
| 77 | + ldflags = config.build_settings['OTHER_LDFLAGS'] || '$(inherited)' |
| 78 | + unless ldflags.include?('-fprofile-instr-generate') |
| 79 | + config.build_settings['OTHER_LDFLAGS'] = |
| 80 | + "#{ldflags} -fprofile-instr-generate" |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +Pod::Installer.prepend(HarnessCoverageHook) |
0 commit comments