eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' & eval 'exec perl -w -S $0 $argv:q' if 0; ### # # pkg-configToMPC - convert pkg-config files in current directory to MPC mpb files # and write depends.dot as dependency graph. # # 2006, Urs Stotz # ### ### # # Modules # ### use strict; use warnings; use Getopt::Long qw(:config no_ignore_case posix_default ); use POSIX qw(strftime); ### # # Declarations # ### my $version='0.9.0'; my %opt; sub convert($); sub help($); ### # # Main # ### GetOptions("I=s"=>\$opt{includedir}, "L=s"=>\$opt{libdir}, "help|h!"=>\$opt{help}) or help(1); help(0) if (defined($opt{help}) || @ARGV==0); print("includedir: $opt{includedir}\n") if (defined($opt{includedir})); print("libdir: $opt{libdir}\n") if (defined($opt{libdir})); print("pkgfile: @ARGV\n"); open(DOT, '>'. 'depends.dot') or die("Can't write file depends.dot: $!"); print(DOT "digraph pkgconfig_depends {\n"); print(DOT " size=\"8.6\";\n\n"); print(DOT "/*\n"); print(DOT " pkg-configToMPC.pl"); print(DOT " $_") for (@ARGV, "\n"); print(DOT " write png file with: dot -Tpng -odepends.png depends.dot\n"); print(DOT "*/\n\n"); while (my $file=shift) { convert($file); } print(DOT "}\n"); close(DOT); ### # # Defination of subroutines # ### sub convert($) { my $pkgfile=shift or die("usage: convert("); my $mpcfile=$pkgfile; $mpcfile=~s/(.*)\.pc/$1.mpb/; if (! -f $mpcfile ) { print("convert: $pkgfile\n"); my ( @requires, @includes, @libpaths, @libs, %local_var ); open(IN, '<', $pkgfile) or die("Can't read file $pkgfile: $!"); @requires=(); @includes=(); @libpaths=(); @libs=(); my $rgxPath=qr([A-Za-z]{0,1}:{0,1}[\/\\]{1}[\w+\\\/\+\-\.]+); my $rgxVar=qr#\$\([\w\-]+\)[\w+\\\/\+\-\.]*#; foreach my $line () { $line=~s|\$\{includedir\}|$opt{includedir}|g if (defined($opt{includedir})); $line=~s|\$\{libdir\}|$opt{libdir}|g if (defined($opt{libdir})); foreach my $key (keys %local_var) { $line=~s/\$\{$key}/$local_var{$key}/g; } $local_var{$1}=$2 if ($line=~/^([\w\.\+\-]+)=([\/\w\.\+\-]+)/); if ( $line=~m/^Requires[\.\w\-]*:\s+\w+/ ) { $line=~s/Requires[\.\w\-]*:\s*(.*)/$1/; #cleanup $line=~s/,/ /g; $line=~s/>=\s*[\d\.]*//g; while ( $line =~m/([\w\d\.\+\-]+)/g ) { push(@requires, $1); } } if ( $line=~m/^Libs:/ ) { while ( $line =~m/\s+-l([\w\.\+\-]+)/g ) { push(@libs, $1); } } if ( $line=~m/^Libs:/ ) { while ( $line =~m/\s+-L($rgxVar|$rgxPath)/g ) { push(@libpaths, $1); } } if ( $line=~m/^Cflags:/ ) { while ( $line =~m/\s+-I($rgxPath|$rgxVar)/gx ) { push(@includes, $1); } } } close(IN); open(OUT, '>', $mpcfile) or die("Can't write file $mpcfile: $!"); print(OUT "// -*- MPC -*-\n"); print(OUT "// \$Id: $mpcfile,v 1.0 " . strftime("%Y/%m/%d %H:%M:%S ", localtime) . "pkg-configToMPC.pl Urs Stotz \$\n\n"); if ($#requires<0) { print(OUT "project {\n"); } elsif ($#requires>=0) { print(OUT "project: "); for (my $i=0; $i<$#requires; ) { print(DOT ' "' . ($pkgfile=~m/(.*)\.pc/)[0] . '" -> "' . $requires[$i] . "\";\n"); print(OUT "$requires[$i], "); $i++; } } if ($#requires>=0) { print(DOT ' "' . ($pkgfile=~m/(.*)\.pc/)[0] . '" -> "' . $requires[-1] . "\";\n"); print(OUT "$requires[-1] {\n"); } print(OUT " includes += $_\n") for @includes; print(OUT " libs += $_\n") for @libs; print(OUT " libpaths += $_\n") for @libpaths; print(OUT "}\n"); close(OUT); convert(($_ . '.pc')) for (@requires); } } sub help($) { my $exit_code=shift; print <] [-L] [pkgfiles.pc, ...] examples: library and include path as fix value: pkg-configToMPC.pl -I/usr/include -L/usr/lib pgkfile.pc will write library and include path as environment variable in mpc files: pkg-configToMPC.pl -I'\$(INCVAR)\\include' -L'\$(LIBVAR)\lib' pgkfile.pc remeber to quote \$(INCVAR)\\include and \$(LIBVAR)\\lib on Unix with '': pkg-configToMPC.pl -I '\$(GTK_HOME)/include' -L '\$(GTK_HOME)/lib' libglademm-2.4.pc EOF_HELP exit($exit_code); }