+ my $redir = $^O eq 'MSWin32' ? '' : '2>/dev/null';
+
+ !system("pkg-config $pkg --exists $redir");
+}
+
+# probes for freetype1 by scanning @incs for the includes and
+# @libs for the libs. This is done separately because freetype's headers
+# are stored in a freetype or freetype1 directory under PREFIX/include.
+#
+# we could find the files with the existing mechanism, but it won't set
+# -I flags correctly.
+#
+# This could be extended to freetype2 too, but freetype-config catches
+# that
+sub freetype1_probe {
+ my ($frm, $frmkey) = @_;
+
+ my $found_inc;
+ INCS:
+ for my $inc (@incs) {
+ for my $subdir (qw/freetype freetype1/) {
+ my $path = File::Spec->catfile($inc, $subdir, 'freetype.h');
+ -e $path or next;
+ $path = File::Spec->catfile($inc, $subdir, 'fterrors.h');
+ -e $path and next;
+
+ $found_inc = File::Spec->catdir($inc, $subdir);
+ last INCS;
+ }
+ }
+
+ my $found_lib;
+ LIBS:
+ for my $lib (@libs) {
+ my $a_path = File::Spec->catfile($lib, "libttf$aext");
+ my $l_path = File::Spec->catfile($lib, "libttf.$lext");
+ if (-e $a_path || -e $l_path) {
+ $found_lib = $lib;
+ last LIBS;
+ }
+ }
+
+ return unless $found_inc && $found_lib;
+ printf("%10s: includes %s - libraries %s\n", $frmkey,
+ ($found_inc ? 'found' : 'not found'),
+ ($found_lib ? 'found' : 'not found'));
+
+ $frm->{cflags} = "-I$found_inc";
+ $frm->{libfiles} = "-lttf";
+
+ return 1;
+}
+
+# probes for freetype2 by trying to run freetype-config
+sub freetype2_probe_ftconfig {
+ my ($frm, $frmkey) = @_;
+
+ is_exe('freetype-config') or return;
+
+ my $cflags = `freetype-config --cflags`
+ and !$? or return;
+ chomp $cflags;
+
+ my $lflags = `freetype-config --libs`
+ and !$? or return;
+ chomp $lflags;
+
+ # before 2.1.5 freetype-config --cflags could output
+ # the -I options in the wrong order, causing a conflict with
+ # freetype1.x installed with the same --prefix
+ #
+ # can happen iff:
+ # - both -Iprefix/include and -Iprefix/include/freetype2 are in cflags
+ # in that order
+ # - freetype 1.x headers are in prefix/include/freetype
+ my @incdirs = map substr($_, 2), grep /^-I/, split ' ', $cflags;
+ if (@incdirs == 2
+ && $incdirs[1] eq "$incdirs[0]/freetype2"
+ && -e "$incdirs[0]/freetype/freetype.h"
+ && -e "$incdirs[0]/freetype/fterrid.h") {
+ print "** freetype-config provided -I options out of order, correcting\n"
+ if $VERBOSE;
+ $cflags = join(' ', grep(!/-I/, split ' ', $cflags),
+ map "-I$_", reverse @incdirs);
+ }
+ $frm->{cflags} = $cflags;
+ $frm->{lflags} = $lflags;
+
+ printf "%10s: configured via freetype-config\n", $frmkey;
+
+ return 1;
+}
+
+# attempt to probe for freetype2 by scanning directories
+# we can't use the normal scan since we need to find the directory
+# containing most of the includes
+sub freetype2_probe_scan {
+ my ($frm, $frmkey) = @_;
+
+ my $found_inc;
+ my $found_inc2;
+ INCS:
+ for my $inc (@incs) {
+ my $path = File::Spec->catfile($inc, 'ft2build.h');
+ -e $path or next;
+
+ # try to find what it's including
+ my $ftheader;
+ open FT2BUILD, "< $path"
+ or next;
+ while (<FT2BUILD>) {
+ if (m!^\s*\#\s*include\s+<([\w/.]+)>!
+ || m!^\s*\#\s*include\s+"([\w/.]+)"!) {
+ $ftheader = $1;
+ last;
+ }
+ }
+ close FT2BUILD;
+ $ftheader
+ or next;
+ # non-Unix installs put this directly under the same directory in
+ # theory
+ if (-e File::Spec->catfile($inc, $ftheader)) {
+ $found_inc = $inc;
+ last INCS;
+ }
+ for my $subdir (qw/freetype2 freetype/) {
+ $path = File::Spec->catfile($inc, $subdir, 'fterrors.h');
+ -e $path and next;
+
+ $found_inc = $inc;
+ $found_inc2 = File::Spec->catdir($inc, $subdir);
+ last INCS;
+ }