5525dd0172c75e8bc452312901d3a7c663cbf9ea
[liboping.git] / bindings / perl / Makefile.PL
1 use 5.006;
2 use ExtUtils::MakeMaker;
3 use Config (%Config);
4
5 my @OPING_PREFIX = (qw(/opt/oping /usr /usr/local));
6 my $OPING_PREFIX;
7
8 my $OPING_CPPFLAGS;
9 my $OPING_LDDLFLAGS;
10 my $OPING_LIBS;
11 my $OPING_DEPEND;
12
13 # TOP_BUILDDIR is set by liboping's build system, so Net::Oping can link with
14 # the yet uninstalled library.
15 my $TOP_BUILDDIR;
16 my $TARGET_LIBDIR;
17
18 # Parse custom command line arguments.
19 for (my $i = 0; $i < @ARGV; $i++)
20 {
21         if ($ARGV[$i] =~ m#^OPING_PREFIX=(.*[^/])#)
22         {
23                 unshift (@OPING_PREFIX, $1);
24                 splice (@ARGV, $i, 1);
25                 $i--;
26         }
27         elsif ($ARGV[$i] =~ m#^TOP_BUILDDIR=(.*[^/])#)
28         {
29                 $TOP_BUILDDIR = $1;
30                 # TOP_BUILDDIR is passed from bindings/, but we're currently in
31                 # bindings/perl/. If it is a relative path, we need to add an
32                 # extra `../' in order to compensate for this.
33                 if ($TOP_BUILDDIR !~ m#^/#)
34                 {
35                         $TOP_BUILDDIR = "../$TOP_BUILDDIR";
36                 }
37                 splice (@ARGV, $i, 1);
38                 $i--;
39         }
40         elsif ($ARGV[$i] =~ m#^TARGET_LIBDIR=(.*[^/])#)
41         {
42                 # Only save TARGET_LIBDIR if it's not a standard system library
43                 # directory, such as /usr/lib.
44                 if (!is_system_libdir ($1))
45                 {
46                         $TARGET_LIBDIR = $1;
47                 }
48                 splice (@ARGV, $i, 1);
49                 $i--;
50         }
51 }
52
53 if (!$TOP_BUILDDIR)
54 {
55         for (my $i = 0; $i < @OPING_PREFIX; $i++)
56         {
57                 if (!-e $OPING_PREFIX[$i] . '/include/oping.h')
58                 {
59                         next;
60                 }
61
62                 $OPING_PREFIX = $OPING_PREFIX[$i];
63                 print "Found <oping.h> in $OPING_PREFIX/include\n";
64                 last;
65         }
66 }
67
68 if ($TOP_BUILDDIR)
69 {
70         # Use LDDLFLAGS here instead of LIBS, because:
71         #  1) We need to make sure our library path comes first (and no locally
72         #     installed version is used).
73         #  2) Prevent MakeMaker from stipping the -rpath option when the
74         #     library is to be installed in a non-standard path. Standard-paths
75         #     are read from $Config{'libsdirs'} above.
76         $OPING_CPPFLAGS = "-I$TOP_BUILDDIR/src";
77         $OPING_LDDLFLAGS = "-L$TOP_BUILDDIR/src/.libs " . $Config{'lddlflags'};
78         $OPING_LIBS = "-L$TOP_BUILDDIR/src/.libs -loping";
79
80         if ($TARGET_LIBDIR)
81         {
82                 $OPING_LDDLFLAGS .= qq( -Wl,-rpath -Wl,"$TARGET_LIBDIR");
83         }
84
85         $OPING_DEPEND = { 'Oping.o' => "$TOP_BUILDDIR/src/liboping.la" };
86 }
87 elsif ($OPING_PREFIX)
88 {
89         # -rpath is automagically set in this case.
90         $OPING_CPPFLAGS = "-I$OPING_PREFIX/include";
91         $OPING_LIBS = "-L$OPING_PREFIX/lib -loping";
92 }
93
94 if (!$OPING_CPPFLAGS)
95 {
96         my $search_path = join (', ', @OPING_PREFIX);
97         print STDERR <<EOF;
98 ******************************************************************************
99 * ERROR: COULD NOT FIND THE NEEDED HEADER FILE <oping.h>!                    *
100 ******************************************************************************
101 The <oping.h> header file could not be found in the usual places. The prefix
102 paths searched right now are:
103   $search_path
104
105 Please rerun Makefile.PL giving the prefix to the oping library using the
106 `OPING_PREFIX' argument. If you, for example, had installed the oping library
107 to /tmp/oping, the header file would be at /tmp/oping/include/oping.h; you'd
108 then need to run the Makefile.PL as follows:
109   perl Makefile.PL OPING_PREFIX=/tmp/oping
110
111 As you can see, the argument passed via `OPING_PREFIX' must be the same
112 directory you passed to the liboping configure script using the `--prefix'
113 argument.
114
115 No Makefile has been created.
116 EOF
117         exit (0);
118 }
119
120 WriteMakefile(
121     NAME              => 'Net::Oping',
122     VERSION_FROM      => 'lib/Net/Oping.pm',
123     PREREQ_PM         => {},
124     ($] >= 5.005
125      ? (ABSTRACT_FROM => 'lib/Net/Oping.pm',
126         AUTHOR        => 'Florian Forster <octo@verplant.org>')
127      : ()),
128     ($OPING_DEPEND ? (depend => $OPING_DEPEND) : ()),
129     LIBS              => [$OPING_LIBS],
130     ($OPING_LDDLFLAGS ? (LDDLFLAGS => "$OPING_LDDLFLAGS") : ()),
131     DEFINE            => '',
132     INC               => "$OPING_CPPFLAGS"
133 );
134
135 sub is_system_libdir
136 {
137         my $path = shift;
138         for (split (' ', $Config{'libsdirs'}))
139         {
140                 if ($path eq $_)
141                 {
142                         return (1);
143                 }
144         }
145         return;
146 }