|
| 1 | +#! /usr/bin/env perl |
| 2 | +# ============================================================================ # |
| 3 | +# |
| 4 | +# This script processes a C Family module map file to adjust header paths. |
| 5 | +# |
| 6 | +# USAGE: mixed_language_module_map_extender.pl <output> \ |
| 7 | +# <modulemap> \ |
| 8 | +# <module_name> \ |
| 9 | +# <swift_generated_header> |
| 10 | +# |
| 11 | +# |
| 12 | +# ============================================================================ # |
| 13 | + |
| 14 | +use strict; |
| 15 | +use warnings; |
| 16 | +use File::Spec; |
| 17 | + |
| 18 | +# Check arguments |
| 19 | +if ( @ARGV != 4 || |
| 20 | + ( @ARGV = 1 && ( $ARGV[0] == '-h' || $ARGV[0] == "--help" ) ) |
| 21 | + ) |
| 22 | + { |
| 23 | + my $msg = "USAGE: $0 <output> <modulemap> <module_name> " . |
| 24 | + "<swift_generated_header>\n"; |
| 25 | + if ( @ARGV == 1 ) |
| 26 | + { |
| 27 | + print STDERR $msg; |
| 28 | + exit 0; |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + die $msg; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | +my ( $output, $modulemap, $module_name, $swift_generated_header ) = @ARGV; |
| 37 | + |
| 38 | +# Get absolute paths for directories |
| 39 | +my $modulemap_dir = File::Spec->rel2abs( |
| 40 | + ( File::Spec->splitpath( $modulemap ) )[1] |
| 41 | + ); |
| 42 | +my $output_dir = File::Spec->rel2abs( ( File::Spec->splitpath( $output ) )[1] ); |
| 43 | + |
| 44 | +# Open input and output files |
| 45 | +open( my $in_fh, '<', $modulemap ) or |
| 46 | + die "Cannot open input file '$modulemap': $!\n"; |
| 47 | + |
| 48 | +open( my $out_fh, '>', $output ) or |
| 49 | + die "Cannot open output file '$output': $!\n"; |
| 50 | + |
| 51 | + |
| 52 | +# Process each line of the module map |
| 53 | +while ( my $line = <$in_fh> ) |
| 54 | + { |
| 55 | + # Match header declarations with optional modifiers |
| 56 | + # ( private, textual, umbrella ) |
| 57 | + if ( $line =~ /^(\s*(?:private\s+|textual\s+|umbrella\s+)*) |
| 58 | + header\s+"([^"]+)"(.*)$/x |
| 59 | + ) |
| 60 | + { |
| 61 | + my ( $prefix, $header_path, $suffix ) = ( $1, $2, $3 ); |
| 62 | + |
| 63 | + # Convert header path to absolute, then to relative from output directory |
| 64 | + my $original_header_abs = File::Spec->rel2abs( |
| 65 | + $header_path |
| 66 | + , $modulemap_dir |
| 67 | + ); |
| 68 | + my $new_header_rel = File::Spec->abs2rel( |
| 69 | + $original_header_abs |
| 70 | + , $output_dir |
| 71 | + ); |
| 72 | + |
| 73 | + print $out_fh "${prefix}header \"$new_header_rel\"$suffix\n"; |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + print $out_fh $line; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +# Add Swift submodule |
| 82 | +print $out_fh "\n"; |
| 83 | +print $out_fh "module \"$module_name\".Swift {\n"; |
| 84 | +print $out_fh " header \"$swift_generated_header\"\n"; |
| 85 | +print $out_fh "\n"; |
| 86 | +print $out_fh " requires objc\n"; |
| 87 | +print $out_fh "}\n"; |
| 88 | + |
| 89 | +close( $in_fh ); |
| 90 | +close( $out_fh ); |
0 commit comments