1#!/usr/local/bin/perl -w
2#
3# You may distribute under the terms of either the GNU General Public
4# License or the Artistic License, as specified in the Perl README file.
5#
6# Test dependencies on CPAN:
7# http://cpandeps.cantrell.org.uk/?module=DBD::ODBC;perl=latest
8#
9## no critic (ProhibitMagicNumbers RequireInterpolationOfMetachars)
10## no critic (RequireExtendedFormatting RequireCheckingReturnValueOfEval)
11## no critic (RequireCarping ProhibitParensWithBuiltins RequireBriefOpen)
12## no critic (RequireLocalizedPunctuationVars ProhibitBacktickOperators)
13#  keeps reporting this even though I am not matching dots:
14## no critic (RequireDotMatchAnything)
15# I am not changing all these - would take too long to check each one
16## no critic (RequireLineBoundaryMatching)
17
18use strict;
19BEGIN { require 5.004 }         # 5.004 is required for Win32
20use Config;
21use ExtUtils::MakeMaker 5.16, qw(&WriteMakefile $Verbose prompt);
22use File::Basename;
23use Getopt::Long;
24use File::Spec;
25use English qw( -no_match_vars );
26use warnings;
27
28$OUTPUT_AUTOFLUSH = 1;
29
30print <<"EOT";
31
32**********
33\tRemember to actually *READ* the README file!
34\tAnd re-read it if you have any problems.\n
35**********
36
37EOT
38
39{
40    # some useful info when debugging problems
41    print "OSNAME: $OSNAME\n";
42    my @envs = qw(LANG ODBCHOME LD_LIBRARY_PATH DBROOT WINDIR II_SYSTEM DBD_ODBC_UNICODE);
43    foreach (@envs) {
44        print "$_: ", ($ENV{$_} ? $ENV{$_} : ''), "\n";
45    }
46    print "Perl: $]\n";
47    print "ExtUtils::MakeMaker: $ExtUtils::MakeMaker::VERSION\n";
48}
49
50# the following now redundant since we found constants (see end) which allows
51# us to override the constants.
52######
53###### if INC is set on the command line ExtUtils::MakeMaker will not override it
54###### or allow us to add to it so you can never find the DBI header files.
55###### It is pointless to continue
56###### see http://www.mail-archive.com/makemaker@perl.org/msg02680.html
57###### see http://www.nntp.perl.org/group/perl.dbi.users/2008/09/msg33278.html
58###### http://www.perlmonks.org/?node_id=714150
59#####my $INC_argc;
60#####for (my $n = 0; $n <= $#ARGV; $n++) {
61#####    if ($ARGV[$n] =~ /^INC=/) {
62#####        $INC_argc = $n;
63#####        print "\nCannot generate successful Makefile - " .
64#####            "INC is set on the command line.\n\n";
65#####        prompt("Press return to see possible solutions:");
66#####        print <<"EOT";
67#####
68#####INC has been set on the command line as: $ARGV[$n]
69#####
70#####and there is no way this Makefile.PL can add the path to the DBI
71#####header files to INC. If you manually added INC to the command line
72#####remove it. If you are building from the CPAN shell perhaps your
73#####makepl_arg is set to include INC (as it is with Strawberry Perl). You
74#####need to remove INC from makepl_arg with:
75#####
76#####cpan> o conf
77#####
78#####Look for makepl_arg and set makepl_arg to whatever text it is minus the INC
79#####setting. You set makepl_arg with:
80#####
81#####cpan> o conf makepl_arg 'text of makepl_arg - INC'
82#####
83#####Alternatively, if you are using Strawberry perl you can just install
84#####DBD::ODBC outside of the CPAN shell.
85#####
86#####If you cannot remove INC from the command line then you will need to hand
87#####edit the generated Makefile. Search it for :
88#####
89##### #   MakeMaker 'CONFIGURE' Parameters:
90##### #     INC => q[-I. -I/some_path/DBI]
91#####
92#####then add the -I/some_path/DBI to INC where it is set later in the
93#####Makefile.
94#####
95#####EOT
96#####        #exit 0;
97#####    }
98#####}
99my %opts =
100(
101    ## no critic (RequireInterpolationOfMetachars)
102    NAME         => 'DBD::ODBC',
103    VERSION_FROM => 'ODBC.pm',
104    # See note below on CONFIGURE. This used to work when we could rely on
105    # CONFIGURE being after PREREQ_PM but that is not the case now so the
106    # line below does nothing since 6.33 of MakeMaker.
107    BUILD_REQUIRES => {
108        "Test::Simple" => 0.90, # actually Test::More pkg in T::S dist,
109        "Test::Output" => 1.031 },
110    PREREQ_PM    => { "DBI" => 1.609 },
111    clean        => { FILES => 'ODBC.xsi dbdodbc.h' },
112    dist         => {
113	DIST_DEFAULT => 'clean distcheck tardist',
114	PREOP        => '$(MAKE) -f Makefile.old distdir',
115	COMPRESS     => 'gzip -v9', SUFFIX => 'gz'
116    },
117    OBJECT       => '$(O_FILES)',
118    DEFINE => q{},
119 );
120# just save the open suse guys patching the following:
121if ($ENV{RPM_OPT_FLAGS}) {
122    $opts{OPTIMIZE} = $ENV{RPM_OPT_FLAGS};
123}
124
125my $eumm = $ExtUtils::MakeMaker::VERSION;
126$eumm =~ tr/_//d;
127
128$opts{LICENSE} = 'perl' if $eumm >= 6.3002;
129$opts{NO_META} = 1 if $eumm >= 6.10;
130
131if ($eumm >= 5.43) {
132    $opts{AUTHOR} = 'Tim Bunce, Jeff Urlwin, Martin J. Evans mailto:dbi-users@perl.org';
133    $opts{ABSTRACT} = 'ODBC driver for the DBI module.';
134    $opts{CAPI} = 'TRUE' if $Config{archname} =~ /-object\b/i;
135    # Never set PREREQ_FATAL to true - it is flawed
136    # - see http://wiki.cpantesters.org/wiki/CPANAuthorNotes
137    #$opts{PREREQ_FATAL} = 1;
138    # See
139    # http://www.mail-archive.com/cpan-testers-discuss%40perl.org/msg00076.html
140    # In ExtUtils::MakeMaker 6.32 and earlier CONFIGURE was run after PREREQ_PM
141    # so we could safely require DBI::DBD here and PREREQ_PM would fail first
142    # if DBI was not installed. Since 6.33 CONFIGURE is run before PREREQ_PM
143    # so now the require below fails and if we do not exit 0 without generating
144    # a Makefile cpan-testers will fail us if DBI is not found.
145    $opts{CONFIGURE} = sub {
146        eval {require DBI::DBD;};
147        if ($@) {
148            warn $@;
149            exit 0;
150        } else {
151        my $dbi_arch_dir = DBI::DBD::dbd_dbi_arch_dir();
152        if (exists($opts{INC})) {
153            return {INC => "$opts{INC} -I$dbi_arch_dir"};
154        } else {
155            return {INC => "-I$dbi_arch_dir"};
156        }
157    }
158    };
159}
160if ($eumm >= 5.48) {
161    $opts{PREREQ_PRINT} = 1;
162}
163
164my $opt_g = 0;                  # build debug
165my $opt_o = q{};                # odbc home overrides ODBCHOME
166my $opt_u = undef;              # build unicode version
167my $opt_e = undef;              # easysoft
168my $opt_x = undef;              # prefer unixODBC over iODBC
169my $opt_w = undef;              # enable -Wall (gcc only)
170
171$opt_u = 1 if $ENV{DBD_ODBC_UNICODE};
172
173my @options = ("g!" => \$opt_g,
174               "o=s" => \$opt_o,
175               "u!" => \$opt_u,
176               "e!" => \$opt_e,
177               "x!" => \$opt_x,
178               "w!" => \$opt_w);
179my %options = @options;
180Getopt::Long::GetOptions(@options) or die "Invalid arguments\n";
181
182print "Command line options:\n",
183    (map {"  $_ = " . (defined(${$options{$_}}) ? ${$options{$_}} : 'undef') . "\n"} keys %options), "\n";
184if (($ENV{LANG} || q{}) =~ m/utf-?8/i) {
185    print <<"EOT";
186
187Your LANG environment variable is set to "$ENV{LANG}"\a
188This is known to cause problems in some perl installations - even stopping
189this Makefile.PL from running without errors. If you have problems please
190try re-running with LANG unset or with the utf part of LANG removed.
191
192EOT
193    sleep 4;
194}
195
196if ($Config{useithreads}) {
197    print <<'EOT';
198You are using a Perl configured with threading enabled.
199Please read the warnings in DBI about this.
200
201EOT
202    if ($OSNAME ne 'MSWin32') {
203        print <<'EOT';
204You should also be aware that on non-Windows platforms ODBC drivers come
205in two forms, thread-safe and non-thread-safe drivers and you may need
206to make sure you are using the right one.
207
208EOT
209    }
210    # see rt 46944 for why the following was suggested to be commented
211    # out which I rejected as PERL_MM_USE_DEFAULT can be used
212#    prompt("Press return to continue...");
213}
214print "Overriding ODBC Directory with command line option: $opt_o\n"
215    if $opt_o ;
216if ($opt_g) {
217   print "Setting debug options!\n";
218   if ($OSNAME eq 'MSWin32') {
219      $opts{OPTIMIZE} = '/Zi';
220   } else {
221      $opts{OPTIMIZE} = '-g -O0';
222   }
223}
224if ($opt_w) {
225    $opts{CCFLAGS} = '-Wint-to-pointer-cast -fno-strict-aliasing -Wall -Werror=format-security';
226}
227if (defined($opt_u) && $opt_u) {
228    $opts{DEFINE} .= ' -DWITH_UNICODE';
229    require 5.008001;
230}
231
232my @known_drivers = sort { $a cmp $b } (
233	'Microsoft ODBC',
234	'unixodbc',
235	'iodbc',
236	'empress',
237	'intersolve',
238	'sapdb',
239	'adabas',
240	'udbc',
241	'solid',
242	'informix',
243    'ingrescli', ## clach04 Ingres CLI name?
244	);
245my $odbchome_specified;
246$odbchome_specified = 1 if defined($opt_o) || defined($ENV{ODBCHOME});
247
248#
249# create the dbdodbc.h file with all the required includes per driver or
250# driver manager
251#
252my $sqlhfh;
253open($sqlhfh, q/>/, 'dbdodbc.h') || die "Can't open dbdodbc.h: $!\n";
254print {$sqlhfh}
255    qq{/* Do not edit this file. It is automatically written by Makefile.PL.\n};
256print {$sqlhfh} qq{   Any changes made here will be lost. \n*/\n\n};
257print {$sqlhfh} qq{#undef WORD /* from perly.y */\n};
258
259if ($OSNAME eq 'MSWin32') {
260    my $extrainc = q{};
261    $extrainc = ";$Config{incpath}\\mfc" if $Config{cc} eq 'bcc32';
262    $opts{SKIP} = ['processPL'];
263    if (!defined($opt_u) || $opt_u) {
264	$opts{DEFINE}  .= " -DWITH_UNICODE";
265	require 5.008001;
266    }
267    # stop all those strcpy etc warnings:
268    $opts{DEFINE} .= " -D_CRT_SECURE_NO_DEPRECATE";
269    $opts{INC}  = "$extrainc";
270    $opts{LIBS} = ["ODBC32.LIB"];
271    $opts{macro}->{EXTRALIB} = 'ODBC32.LIB';
272    print {$sqlhfh} "#include <windows.h>\n";
273    print {$sqlhfh} <<'EOT';
274#ifdef WIN32
275# ifndef _WIN64
276#  ifndef SQLLEN
277#   define SQLLEN          SQLINTEGER
278#  endif
279#  ifndef SQLULEN
280#   define SQLULEN         SQLUINTEGER
281#  endif
282#  ifndef SQLSETPOSIROW
283#   define SQLSETPOSIROW   SQLUSMALLINT
284#  endif
285# endif
286#endif
287EOT
288    print {$sqlhfh}
289        "#include <sql.h>\n#include <sqltypes.h>\n#include <sqlext.h>\n";
290} elsif ($opt_e) {
291    my $odbchome = "/usr/local/easysoft/unixODBC";
292    my $odbclibdir = "/usr/local/easysoft/unixODBC/lib";
293    my $odbcincdir = "/usr/local/easysoft/unixODBC/include";
294    $opts{INC} = "-I. -I$odbcincdir";
295    $opts{DEFINE} .= "-DSIZEOF_LONG=8 -DBUILD_REAL_64_BIT_MODE";
296
297    my $soext = $Config{so};      # extension for share objects
298    my $dlext = $Config{dlext};   # extension for dynamically loaded modules
299    my $arext = $Config{lib_ext}; # this is _a now, library extension
300
301    my @libs = glob "$odbclibdir/libodbc.*";
302    my @ilibs = grep { /\.($soext|$dlext|a)$/ } @libs;
303    if (scalar(@ilibs) == 0) {
304        die "Cannot find unixODBC";
305    }
306    my $ilibpath = $ilibs[0]; # if both .so and .a, pick based on LINKTYPE?
307    my $ilibname = basename($ilibpath);
308    if ($ilibname =~ /^odbc/) { # no "lib" prefix
309        $opts{LIBS} = q{};
310        $opts{dynamic_lib} = { OTHERLDFLAGS => "$ilibpath" };
311    }
312    else {
313        if ($ilibname =~ /^lib(odbc[^.]*?)\.\w+$/) {
314            # remove lib prefix and .so suffix so "-l" style link can be used
315            $ilibname = $1;
316            $opts{LIBS} = "-L$odbclibdir -l$ilibname";
317        } else {
318            # cannot use "-l" style link so specify pull path
319            $opts{LIBS} = q{};
320            $opts{dynamic_lib} = { OTHERLDFLAGS => "$ilibpath" };
321        }
322        warn "Warning: LD_LIBRARY_PATH doesn't include $odbclibdir\n"
323            unless (exists($ENV{LD_LIBRARY_PATH}) &&
324                        ($ENV{LD_LIBRARY_PATH} =~ /\Q$odbclibdir/));
325    }
326    print {$sqlhfh} qq{#include <sql.h>\n};
327    print {$sqlhfh} qq{#include <sqlucode.h>\n};
328    print {$sqlhfh} qq{#include <sqltypes.h>\n};
329    print {$sqlhfh} qq{#include <sqlext.h>\n};
330} else {
331
332    my $myodbc = "unixodbc";
333    my $myodbc_version = -1;
334
335    # cannot believe the following still works as $odbchome is checked later
336    # to be a directory - so commented out - there are other ways to do
337    # this anyway
338    # for Adabas
339    #$ENV{ODBCHOME} = $ENV{DBROOT}
340    #    if $ENV{DBROOT} && -f "$ENV{DBROOT}/lib/odbclib.a";
341
342    print "Overriding ODBC Directory with command line option: $opt_o\n"
343        if $opt_o ;
344    my $odbchome= $opt_o || $ENV{ODBCHOME};
345    my ($odbcincdir, $odbclibdir);
346    $odbchome = VMS::Filespec::unixify($odbchome) if $OSNAME eq 'VMS';
347
348
349    # if we haven't got odbchome set try and find a driver, driver manager.
350    if (!$odbchome) {
351        if ($ENV{WINDIR} && $OSNAME eq 'cygwin') {
352            ## per patches from Teun Burgers
353            #my $tmp_odbchome = $ENV{WINDIR};
354            #$tmp_odbchome =~ s/^([A-Za-z]):*$/\/\/$1/;
355            #$tmp_odbchome =~ s/\\/\//g;
356            #$odbchome = $tmp_odbchome if (-e "$tmp_odbchome/odbc.ini")
357            chomp($odbchome = `cygpath \$WINDIR`);
358        } elsif (-f '/opt/sapdb/interfaces/odbc/lib/libsqlod.a') {
359            $odbchome = '/opt/sapdb/interfaces/odbc/';
360        }
361    }
362    if ($OSNAME eq 'cygwin'){   #Trade cygwin as MSWin.
363        my $extrainc = q{};
364        $extrainc = ";$Config{incpath}\\mfc" if $Config{cc} eq 'bcc32';
365        $opts{SKIP} = ['processPL'];
366        if (!defined($opt_u) || $opt_u) {
367            $opts{DEFINE}  .= " -DWITH_UNICODE";
368            require 5.008001;
369        }
370        $opts{INC}  = "$extrainc";
371        $opts{LIBS} = ["ODBC32.LIB"];
372        $opts{macro}->{EXTRALIB} = 'ODBC32.LIB';
373        print {$sqlhfh} "#include <windows.h>\n";
374        print {$sqlhfh}
375            "#include <sql.h>\n#include <sqltypes.h>\n#include <sqlext.h>\n";
376    } elsif ($OSNAME !~ /MSWin/  ) {
377        # look for Ingres' driver manager first on the basis of if you
378        # have ingres you probably want to use it. Also Ingres ships with
379        # libs that look like unixODBC/iODBC
380        if ($ENV{II_SYSTEM}) {
381            my $home = File::Spec->catdir($ENV{II_SYSTEM}, 'ingres');
382            my $inc = File::Spec->catdir($home, 'files');
383            if ((-f File::Spec->catfile($inc, 'sql.h')) &&
384                     (-f File::Spec->catfile($home, 'bin', 'iiodbcadmin'))) {
385                $odbchome = File::Spec->catdir($ENV{II_SYSTEM}, 'ingres');
386                $odbclibdir = File::Spec->catdir($odbchome, 'lib');
387                $odbcincdir = $inc;
388                $myodbc = 'ingrescli';
389            }
390        }
391        # try and find unixODBC's odbc_config binary
392        if (!$myodbc) {
393            ($myodbc, $myodbc_version, $odbchome, $odbcincdir, $odbclibdir) =
394                unixodbc_config($odbchome);
395        }
396        if (!$myodbc) {
397            # try and find iODBC's iodbc_config binary
398            ($myodbc, $myodbc_version, $odbchome,
399             $odbcincdir, $odbclibdir) = iodbc_config($odbchome);
400        }
401
402        if (!$odbchome) {
403            print "odbc_config not found - " .
404                "ok, there are other things I can do\n";
405
406            print "Still trying to guess ODBCHOME - looking for headers now\n";
407            $odbchome = find_dm_hdr_files();
408        }
409    }
410
411    unless ($odbchome) {
412	print <<'EOT';
413
414The DBD::ODBC module needs to link with an ODBC 'Driver Manager'.
415(The Driver Manager, in turn, needs one or more database specific ODBC
416drivers. The DBD::ODBC module does _not_ include any ODBC drivers!)
417
418You need to indicate where your ODBC Driver Manager is installed.
419You can do this by:
420
421o setting the ODBCHOME environment variable
422o running 'perl Makefile.PL -o odbcdir'
423o adding path to odbc_config/iodbc_config to PATH
424
425If you do not have an ODBC Driver Manager you should try to get hold of
426the unixODBC packages for your system or build it from source (see
427http://www.unixodbc.org). If you install driver manager packages you
428need to include the "XXX-dev" package which includes the C header files.
429EOT
430        # stop cpan testers from reporting a failure when a driver manager
431        # library is not installed. Do not know if Devel::CheckLib (when
432        # it is released) is going to help here.
433        # see http://www.mail-archive.com/cpan-testers-discuss%40perl.org/msg00043.html
434        exit 0;
435    }
436    die "odbc home ($odbchome) does not refer to a directory.\n"
437        unless -d $odbchome;
438    warn "Using ODBCHOME $odbchome\n";
439
440    # $odbcincdir and $odbclibdir will only be set at this point if we
441    # found odbc_config/iodbc_config - otherwise we only have a $odbchome
442    # either from -o, ODBCHOME or by finding sql*.h header files somewhere.
443    if (!$odbcincdir) {
444        $odbcincdir = File::Spec->catdir($odbchome, 'include');
445    }
446    if (!$odbclibdir) {
447        $odbclibdir = File::Spec->catdir($odbchome, 'lib');
448    }
449
450    $opts{INC}  = "-I.";
451
452    # cygwin patch
453    $opts{INC}  .= " -I/usr/include/w32api" if $OSNAME eq 'cygwin';
454
455    # TO_DO all this needs to move until later
456#    my $lib_d1 = "$odbchome/lib";
457#    my $lib_d2 = "$odbchome/dlls";
458#    my $libs   = "odbc";
459#    $opts{LIBS} = " -L$lib_d1 -R$lib_d1 -L$lib_d2 -R$lib_d2 -l$libs";
460
461    my $soext = $Config{so};      # extension for share objects
462    my $dlext = $Config{dlext};   # extension for dynamically loaded modules
463    my $arext = $Config{lib_ext}; # this is _a now, library extension
464
465    # fix to avoid foo..ext on many systems.
466    $arext =~ s/^\.//;
467
468    # Try to work out which driver manager is being used.
469    #
470    # NOTE: if $myodbc is already set we found a driver manager config binary
471    # above and have already set $odbchome, $odbcincdir and $odbclibdir.
472    #
473    # NOTE: we look for iodbc first because both it and unixODBC supply a
474    # libodbc.xx but only iodbc supplies a libiodbc.xx. As a result the only
475    # reliable way of telling one from the other is to look for libiodbc.xx
476    # first.
477    # NOTE: -x says we prefer unixODBC over iODBC
478    # This is to cater for those insane situations when someone had libiodbc
479    # and unixODBC binary packages installed but unixODBC dev package
480    # e.g. some people on Debian/Ubuntu
481    #
482    # PERL_DBD_ODBC_PREFER_UNIXODBC  added for Caelum/Rafael for automated
483    # installs/upgrades from cpan clients
484    if (!$myodbc && ($opt_x || $ENV{PERL_DBD_ODBC_PREFER_UNIXODBC})) {
485        ($myodbc, $odbclibdir) = find_unixodbc($odbchome);
486    } else {
487        ($myodbc, $odbclibdir) = find_iodbc($odbchome) if !$myodbc;
488
489        ($myodbc, $odbclibdir) = find_unixodbc($odbchome) if !$myodbc;
490    }
491
492    $myodbc = 'Microsoft ODBC'
493        if (!$myodbc &&
494                (-e "$odbchome/system/odbc32.dll" or
495                     -e "$odbchome/system32/odbc32.dll" or
496                         -e "$odbchome/odbc32.dll"));
497
498    $myodbc = 'empress'
499        if !$myodbc && glob "$odbchome/lib/libempodbc.*";
500
501    $myodbc = 'intersolve'
502        if !$myodbc && -f "$odbchome/include/qeodbc.h";
503
504    $myodbc = 'sapdb'
505        if !$myodbc && -f "$odbchome/lib/libsqlod.$arext";
506
507    $myodbc = 'adabas'
508        if (!$myodbc &&
509                $ENV{DBROOT} &&
510                    ($odbchome eq $ENV{DBROOT}) &&
511                        -f "$odbchome/lib/odbclib.$arext");
512
513    $myodbc = 'udbc'
514        if !$myodbc && -f "$odbchome/lib/libudbc.$arext";
515
516    $myodbc = 'solid'
517        if !$myodbc && -f "$odbchome/lib/libsolcli.$dlext";
518
519    # JL 2002-12-16: This test is accurate on Unix (Solaris 7) with IBM
520    # Informix ClientSDK 2.80.UC1, which includes IBM Informix CLI
521    # v3.81.000, an ODBC 3.x driver.
522	# NB: The correct value for $ODBCHOME is $INFORMIXDIR.
523    $myodbc = 'informix'
524        if !$myodbc && -f "$odbchome/lib/cli/libifcli.$dlext";
525
526    if (!$myodbc) {
527	local($LIST_SEPARATOR) = ", ";
528	my($list) = "@known_drivers";
529	$list =~ s/^(.{30,70})\s/$1\n\t/gmo;
530        die qq{I cannot find an ODBC driver manager that I recognize.\n...And I know about these drivers:\n$list\n};
531    }
532
533    warn "\nThis looks like a $myodbc type of driver manager.\n";
534
535    # some specific checks for incompatibilities
536    if (defined($opt_u) && $opt_u) {
537        if (-e File::Spec->catfile($odbcincdir, 'sql.h')) {
538            my $fh;
539            open($fh, q/</, "$odbchome/include/sql.h") or
540                die "Failed to open $odbchome/include/sql.h - $!";
541            my @lines = <$fh>;
542            my @found = grep {/iODBC driver manager/i} @lines;
543            if (scalar(@found)) {
544                die "\n\nDBD::ODBC does not support unicode with iODBC and this looks like iODBC. The iODBC driver manager expects wide characters to be 4 bytes long and DBD::ODBC wants wide characters to be UTF16.\nEither\no) Rerun without the -u switch\no) complain to the producer of your ODBC driver manager\no) get another ODBC driver manager (like unixODBC).\n\n";
545            }
546            close $fh or warn "Failed to close sql.h - $!";
547        }
548    }
549
550    if ($myodbc eq 'Microsoft ODBC') {
551        print "\nBuilding for Microsoft under Cygwin\n";
552        $opts{LIBS} = "-L/usr/lib/w32api -lodbc32";
553        print {$sqlhfh} "#include <windows.h>\n";
554        print {$sqlhfh} "#include <sql.h>\n";
555        print {$sqlhfh} "#include <sqltypes.h>\n";
556        print {$sqlhfh} "#include <sqlext.h>\n";
557        print {$sqlhfh} "#undef WIN32\n";
558        $opts{dynamic_lib} = {OTHERLDFLAGS => "-lodbc32"};
559    } elsif ($myodbc eq 'ingrescli') {
560        $opts{INC} .= " -I$odbcincdir";
561        $opts{LIBS} = "-L$odbclibdir -liiodbc.1";
562        print {$sqlhfh}
563            qq{typedef void* PTR;\n#include <sql.h>\n#include <sqlext.h>\n};
564    } elsif ($myodbc eq 'iodbc') {
565        my @libs = glob "$odbclibdir/*iodbc*.*";
566        my @ilibs = grep { /\.($dlext|$soext|$arext)/ } @libs;
567        if (scalar(@ilibs) == 0) {
568            die "That's odd, I can't see any iodbc libs in $odbclibdir. " .
569                "This is all I found:\n" . join(q{,}, @libs) . "\n" .
570                "Perhaps you need to install the iODBC development " .
571                "package, often called libiodbc-dev.";
572        }
573        # Note: we use DEFINE not INC for iODBC so we don't get its config.h
574        my $ilibpath = $ilibs[0]; # if both .so and .a, pick based on LINKTYPE?
575        my $ilibname = basename($ilibpath);
576        $opts{DEFINE} .= " -I$odbcincdir";
577        if ($ilibname =~ /^iodbc/) { # no "lib" prefix
578            $opts{LIBS} = q{};
579            $opts{dynamic_lib} = { OTHERLDFLAGS => "$ilibpath" };
580        }
581        else {
582            if ($ilibname =~ /^lib(iodbc[^.]*?)\.\w+$/) {
583                # remove lib prefix and .so suffix so "-l" style link can be used
584                $ilibname = $1;
585                $opts{LIBS} = "-L$odbclibdir -l$ilibname";
586            } else {
587                # cannot use "-l" style link so specify pull path
588                $opts{LIBS} = q{};
589                $opts{dynamic_lib} = { OTHERLDFLAGS => "$ilibpath" };
590            }
591            warn "Warning: LD_LIBRARY_PATH doesn't include $odbchome/lib\n"
592                if (!defined($ENV{LD_LIBRARY_PATH})) ||
593                    ($ENV{LD_LIBRARY_PATH} =~ /\Q$odbclibdir/);
594        }
595        if (-x "$odbchome/bin/iodbc-config") {
596            my $cf = `$odbchome/bin/iodbc-config --cflags 2>&1`;
597            if ($cf =~ /\-I/) {
598                chomp $cf;
599                $cf =~ s/\n//g;
600                print qq/Adding iodbc_config --cflags "$cf" to CC line\n/;
601                $opts{DEFINE} .= " $cf";
602            }
603        }
604        print {$sqlhfh} qq{#include <sqlext.h>\n};
605        print {$sqlhfh} qq{#include <sql.h>\n};
606        print {$sqlhfh} qq{#include <sqltypes.h>\n};
607    }
608    elsif ($myodbc eq 'unixodbc') {
609        # if we find odbcinst, output useful info about this version of unixODBC
610        # and store unixODBC version
611        print "Looking for odbcinst\n";
612        if (-x "$odbchome/bin/odbcinst") {
613            print "  Found odbcinst in $odbchome/bin\n";
614            my $j = `$odbchome/bin/odbcinst -j 2>&1`;
615            print "  odbcinst -j reports:\n\n$j\n" if $j;
616            if ($j =~ /^unixODBC ([\d\.]+).*/ ) {
617                $myodbc_version = $1;
618            }
619            print "Please note these files as they are where you define your ODBC drivers and data sources.\n\n";
620        } else {
621            print "  odbcinst not found - ok, I can deal with that.\n";
622        }
623        # if we find odbc_config add --cflags output to CC line
624        print "Looking for odbc_config to get cflags\n";
625        if (-x "$odbchome/bin/odbc_config") {
626            #my @args = qw(--prefix --include-prefix --lib-prefix --version --odbcversion);
627            #for my $oca (@args) {
628            #    my $c = `$odbchome/bin/odbc_config $oca 2>&1`;
629            #    chomp $c;
630            #    if ($c) {
631            #        print "odbc_config $oca = $c\n";
632            #    }
633            #}
634            #print "\n";
635            my $cf = `$odbchome/bin/odbc_config --cflags 2>&1`;
636            if ($cf =~ /\-D/) {
637                chomp $cf;
638                $cf =~ s/\n//g;
639                print qq/Adding odbc_config --cflags "$cf" to CC line\n/;
640                $opts{DEFINE} .= " $cf";
641            }
642        } else {
643            print "  odbc_config not found - ok\n";
644        }
645        my ($ilibpath, $ilibname, @ilibs);
646        my @libs = glob "$odbclibdir/libodbc.*";
647        # prefer dynamic linking
648        if (@ilibs = grep /\.($soext)/, @libs) {
649            # remove any shared object version off the end e.g. libodbc.so.2
650            $ilibpath = $ilibs[0];
651            $ilibpath =~ s/(.*\.$soext).*$/$1/;
652            $ilibname = basename($ilibpath);
653            if ($ilibname =~ /^lib(odbc[^.]*?)\.\w+$/) {
654                # remove lib prefix and .so suffix so "-l" style link can be used
655                $ilibname = $1;
656                $opts{LIBS} = "-L$odbclibdir -l$ilibname";
657            } else {
658                # cannot use "-l" style link so specify pull path
659                $opts{LIBS} = q{};
660                $opts{dynamic_lib} = { OTHERLDFLAGS => "$ilibpath" };
661            }
662            warn "Warning: LD_LIBRARY_PATH=", ($ENV{LD_LIBRARY_PATH} ? $ENV{LD_LIBRARY_PATH} : ""), " doesn't include $odbclibdir\n"
663                unless (exists($ENV{LD_LIBRARY_PATH}) &&
664                            ($ENV{LD_LIBRARY_PATH} =~ /\Q$odbclibdir/));
665        } elsif (@ilibs = grep /\.($dlext)/, @libs) {
666            $ilibpath = $ilibs[0];
667            $ilibpath =~ s/(.*\.$dlext).*$/$1/;
668            $ilibname = basename($ilibpath);
669            if ($ilibname =~ /^lib(odbc[^.]*?)\.\w+$/) {
670                # remove lib prefix and .so suffix so "-l" style link can be used
671                $ilibname = $1;
672                $opts{LIBS} = "-L$odbclibdir -l$ilibname";
673            } else {
674                # cannot use "-l" style link so specify pull path
675                $opts{LIBS} = q{};
676                $opts{dynamic_lib} = { OTHERLDFLAGS => "$ilibpath" };
677            }
678        } elsif (@ilibs = grep /\.($arext)$/, @libs) {
679            $ilibpath = $ilibs[0];
680            $ilibname = basename($ilibpath);
681            #$opts{LINKTYPE} = 'static';
682            #$opts{linkext} = {LINKTYPE => 'static'};
683            $opts{dynamic_lib} = { OTHERLDFLAGS => "$ilibpath" };
684            # you can build against a static unixODBC lib but it still needs
685            # to dynamically load (dl_open) drivers so we need libltdl:
686            $opts{LIBS} = "-lltdl";
687        } else {
688            die "That's odd, I can't see any unixodbc libs in $odbchome.\n" .
689                "This is all I found:\n" . join(q{,}, @libs) . "\n" .
690                "Perhaps you need to install the unixODBC development " .
691                "package, often called unixodbc-dev.\n";
692        }
693
694        $opts{DEFINE} .= " -I$odbcincdir";
695        print {$sqlhfh} qq{#include <sql.h>\n};
696        print {$sqlhfh} qq{#include <sqlucode.h>\n};
697        print {$sqlhfh} qq{#include <sqltypes.h>\n};
698        print {$sqlhfh} qq{#include <sqlext.h>\n};
699    }
700    elsif ($myodbc eq 'intersolve') {
701  	print {$sqlhfh} qq{#include <qeodbc.h>\n};
702	if (-f "$odbcincdir/sql.h") {
703	    print "You seem to have the official header files.\n";
704	    $opts{INC} .= " -I$odbcincdir";
705	    print {$sqlhfh} qq{#include <sql.h>\n#include <sqltypes.h>\n#include <sqlext.h>\n};
706	}
707	else {
708	    # This is common on Solaris
709	    print "You don't seem to have the official header files,\n";
710	    print "so I'll use the iODBC ones instead.\n";
711	    $opts{INC} .= " -I$odbcincdir -Iiodbcsrc";
712	    print {$sqlhfh} qq{#include <isql.h> \n#include <isqlext.h>\n};
713	}
714    }
715    elsif ($myodbc eq 'empress') {
716	$opts{INC} .= " -I$odbcincdir";
717	print {$sqlhfh} qq{#include <odbcsys.h>\n};
718	print {$sqlhfh} qq{#include <sql.h>\n#include <sqlext.h>\n};
719	$opts{LIBS} = "-L$odbclibdir -R$odbclibdir -lempodbc";
720    }
721    elsif ($myodbc eq 'sapdb') {
722	print {$sqlhfh} "#include <WINDOWS.H>\n";
723	print {$sqlhfh} "#include <sql.h>\n";
724	print {$sqlhfh} "#include <sqlext.h>\n";
725	print {$sqlhfh} "#define HENV SQLHENV\n";
726	print {$sqlhfh} "#define HDBC SQLHDBC\n";
727	print {$sqlhfh} "#define HSTMT SQLHSTMT\n";
728	print {$sqlhfh} "#define DBD_ODBC_NO_SQLDRIVERCONNECT\n";
729	print {$sqlhfh} qq{#define DBD_ODBC_NO_DATASOURCES\n};
730
731	$opts{INC} .= " -I$odbchome/incl";
732	$opts{LDFROM} = "\$(OBJECT) $odbchome/lib/libsqlod.a";
733    }
734    elsif ($myodbc eq 'adabas') {
735	print {$sqlhfh} "#define FAR \n#define EXPORT \n#define CALLBACK \n";
736	print {$sqlhfh} "#include <WINDOWS.H>\n";
737	print {$sqlhfh} "#include <sql.h>\n";
738	print {$sqlhfh} "#include <sqlext.h>\n";
739	$opts{INC} .= " -I$odbchome/incl";
740	$opts{LIBS} = "-L$odbclibdir -lsqlrte -lsqlptc";
741	$opts{LDFROM} = "\$(OBJECT) $odbclibdir/odbclib.a";
742    }
743    elsif ($myodbc eq 'udbc') {
744        print {$sqlhfh} qq{#include <libudbc.h>\n};
745        $opts{INC} .= " -I$odbcincdir";
746        $opts{LIBS} = "-L$odbclibdir -R$odbclibdir -ludbc";
747    }
748    elsif ($myodbc eq 'solid') {
749        $opts{INC} .= " -I$odbcincdir";
750        $opts{LIBS} = "-L$odbclibdir -lsolcli";
751	# Solid does not support DataSources
752	print {$sqlhfh} qq{#define DBD_ODBC_NO_DATASOURCES\n};
753	# Solid does not support DataSources
754	print {$sqlhfh} qq{#define DBD_ODBC_NO_SQLDRIVERCONNECT\n};
755        print {$sqlhfh} qq{#include <cli0cli.h>\n};
756    }
757    elsif ($myodbc eq 'informix') {
758        # JL 2002-12-16: See comments above for environment details.
759        $opts{INC}  = "-I$odbchome/incl/cli $opts{INC}";
760        $opts{LIBS} = "-L$odbchome/lib/cli -lifcli -lifdmr";
761        $opts{DEFINE} .= " -DNO_WIN32"; # Applies to Unix only, of course
762        print {$sqlhfh} qq{#include <stddef.h>\n};
763        print {$sqlhfh} qq{#include <infxcli.h>\n};
764    }
765    else {
766	print <<'EOT';
767*** WARNING ***
768Unknown driver or driver manager. Using default build process.
769This will almost certainly fail at some point.
770In which case you will need to edit/hack the Makefile.PL
771to suit your needs. (Specifically to locate your odbc
772library and header files.)
773EOT
774	print {$sqlhfh} qq{#include <sql.h> \n#include <sqlext.h>\n};
775    }
776}
777print {$sqlhfh} qq{\n};
778print {$sqlhfh} qq{#include "fixup_c.h"\n};
779print {$sqlhfh} qq{\n};
780close($sqlhfh) or die "Failed to close dbdodbc.h - $!";
781
782print "\n";
783
784if ($OSNAME eq 'darwin') {
785    $opts{LD} = $Config{ld} . ' -framework CoreFoundation';
786    # some older versions of darwin had a problem with iODBC which leads to
787    # Symbol not found: _SQLGetPrivateProfileString
788    # SQLGetPrivateProfileString is in libiodbcinst.a
789    my $osver = `uname -r`;
790    if ($osver && ($osver =~ /^8/)) {
791        $opts{LIBS} .= ' -L/usr/lib -liodbcinst';
792    }
793}
794
795
796my $rv = WriteMakefile(%opts);
797
798local($WARNING)=0;
799print "Warning: not all required environment variables are set.\n"
800	unless ($ENV{DBI_DSN} && $ENV{DBI_USER} && $ENV{DBI_PASS});
801print "Warning: DBI_DSN ($ENV{DBI_DSN}) doesn't start with 'dbi:ODBC:'\n"
802	if ($ENV{DBI_DSN} && $ENV{DBI_DSN} !~ m/^dbi:ODBC:/);
803print "\n";
804if (!exists($ENV{DBI_DSN}) ||
805                 !exists($ENV{DBI_USER}) ||
806                     !exists($ENV{DBI_PASS})) {
807    print "Warning: Will not be able to run tests as you have not defined\n",
808        "all of DBI_DSN, DBI_USER and DBI_PASS environment variables.\n";
809} else {
810    print <<"EOT";
811The DBD::ODBC tests will use these values for the database connection:
812    DBI_DSN=$ENV{DBI_DSN}     e.g. dbi:ODBC:demo
813    DBI_USER=$ENV{DBI_USER}
814    DBI_PASS=$ENV{DBI_PASS}
815EOT
816}
817
818#
819# find the files in @files in $path returning 1 if they all exist, 0 otherwise
820#
821sub files_exist
822{
823    my ($path, @files) = @_;
824    my $found = 1;
825
826    foreach my $file (@files) {
827        my $f = File::Spec->catfile($path, $file);
828        if (! -f $f) {
829            $found = 0;
830            last;
831        }
832    }
833    return $found;
834}
835
836#
837# Try and find out from odbc_config where unixODBC is
838#
839sub unixodbc_config
840{
841    my $odbchome = shift;       # may not be set
842
843    print "Looking for odbc_config in : ", ($odbchome ? $odbchome : "nowhere"), "\n";
844    my ($inc, $lib, $home, $configbin, $odbc_config_v);
845
846    # unixODBC - would have liked to use odbc_config but it did not
847    # exist until 2.2.11 and it was broken wrt --cflags in 2.2.11/2.2.12
848    # i.e. --cflags did not include -I/xxx/yyy
849    if ($odbchome) {
850        $configbin = "$odbchome/bin/odbc_config";
851        print "Looking for odbc_config at $configbin\n";
852        $odbc_config_v = `$configbin --version 2>&1`;
853    }
854    if (!defined($odbc_config_v) || ($odbc_config_v !~ /^(\d\.)+/)) {
855        print "Looking for odbc_config in (PATH) $ENV{PATH}\n";
856        $configbin = 'odbc_config';
857        $odbc_config_v = `$configbin --version 2>/dev/null`;
858        if (!defined($odbc_config_v) || ($odbc_config_v !~ /^(\d\.)+/)) {
859            print "  odbc_config not found\n";
860            return;
861        }
862        if ($odbchome) {
863            my $warning = <<"EOT";
864
865***** WARNING *****
866You provided ODBCHOME ($odbchome)
867which has no odbc_config (not unusual for older unixODBCs)
868but we've found an odbc_config on your PATH. It is unlikely the
869odbc_config specifications are going to match your specified ODBCHOME
870so this script is going to ignore your specified ODBCHOME. If you don't
871like this do something  to remove odbc_config from your PATH or ensure
872there is an odbc_config in your provided ODBCHOME.
873
874EOT
875            warn $warning;
876            prompt("Press return to continue...");
877        }
878    }
879    print "  Found odbc_config (via $configbin) version $odbc_config_v\n";
880
881    my @hdrstofind = ('sql.h', 'sqlext.h', 'sqltypes.h');
882    push @hdrstofind, 'sqlucode.h' if $opt_u;
883
884    $home = `$configbin --prefix 2>&1`;
885    chomp $home;
886    if (!defined($home)) {
887        print "  cannot find --prefix from odbc_config\n";
888        return;
889    }
890    print "  odbc_config reports --prefix=$home\n";
891
892    # if we cannot find the --prefix dir someone perhaps someone has built
893    # it in one place and moved it to another. Try path to odbc_config
894    if (! -e "$home") {
895        my $ocp = `which $configbin`;
896        if ($ocp) {
897            chomp $ocp;
898            if ($ocp =~ /$configbin$/) {
899                $ocp =~ s/bin\/$configbin$//;
900                $home = $ocp;
901            }
902        }
903    }
904
905    $inc = `$configbin --include-prefix`;
906    chomp $inc;
907    print "  odbc_config reports --include-prefix=$inc\n";
908
909    $lib = `$configbin --lib-prefix`;
910    chomp $lib;
911    print "  odbc_config reports --lib-prefix=$lib\n";
912
913    my $found_hdrs = 0;
914    # try with --include-prefix
915    if (defined($inc) && (-e $inc) && files_exist($inc, @hdrstofind)) {
916        print "  ODBC INC dir set to $inc via odbc_config\n";
917        $found_hdrs++;
918    }
919    if (!$found_hdrs && (-e "$home")) {
920        # try with --prefix + include
921        $inc = File::Spec->catdir($home, 'include');
922        if ((-e "$inc") && files_exist($inc, @hdrstofind)) {
923            print "  ODBC INC dir set to $inc via odbc_config\n";
924            $found_hdrs++;
925        }
926    }
927#    if (!$found_hdrs) {
928#        my $ocp = `which $configbin`;
929#        if ($ocp) {
930#            chomp $ocp;
931#            if ($ocp =~ /$configbin$/) {
932#                $ocp =~ s/bin\/$configbin$//;
933#                $inc = File::Spec->catdir($ocp, 'include');
934#                if ((-e "$inc") && files_exist($inc, @hdrstofind)) {
935#                    print "  ODBC INC dir set to $inc from path to $configbin\n";
936#                    $found_hdrs++;
937#                }
938#            }
939#        }
940#    }
941    if (!$found_hdrs) {
942        print "  but cannot find header files " . join(',', @hdrstofind) .
943            " in that path so ignoring\n";
944        print "NOTE: Have you installed the unixodbc-dev package\n";
945        return;
946    }
947
948    my $found_libdir = 0;
949
950    if (-e "$lib") { # try with --lib-prefix
951        print "  ODBC LIB dir set to $lib via odbc_config\n";
952        $found_libdir++;
953    }
954    if (!$found_libdir && -e "$home") {
955        # try with --prefix + lib
956        $lib = File::Spec->catdir($home, 'lib');
957        if (-e "$lib") {
958            print "  ODBC LIB dir set to $lib from odbc_config\n";
959            $found_libdir++;
960        }
961    }
962#    if (!$found_libdir) {
963#        my $ocp = `which $configbin`;
964#        if ($ocp) {
965#            chomp $ocp;
966#            if ($ocp =~ /$configbin$/) {
967#                $ocp =~ s/bin\/$configbin$//;
968#                $lib = File::Spec->catdir($ocp, 'lib');
969#                if (-e "$lib") {
970#                    print "Found lib dir $lib from path to $configbin";
971#                    $found_libdir++;
972#                }
973#            }
974#        }
975#    }
976    if (!$lib) {
977        print "  but cannot find lib dir so ignoring\n";
978        return;
979    }
980
981    return ('unixodbc', $odbc_config_v, $home, $inc, $lib);
982}
983
984#
985# Try and find out from iodbc_config where iODBC is
986#
987sub iodbc_config
988{
989    my $odbchome = shift;       # may not be set
990
991    my ($home, $inc, $lib, $configbin, $iodbc_config_v);
992
993    if ($odbchome) {
994        $configbin = "$odbchome/bin/iodbc-config";
995        print "Looking for iodbc-config at $configbin\n";
996        $iodbc_config_v = `$configbin --version 2>&1`;
997    }
998    if (!defined($iodbc_config_v) || ($iodbc_config_v !~ /^(\d\.)+/)) {
999        print "Looking for iodbc-config in PATH $ENV{PATH}\n";
1000        $configbin = 'iodbc-config';
1001        $iodbc_config_v = `$configbin --version 2>/dev/null`;
1002        if (!defined($iodbc_config_v) || ($iodbc_config_v !~ /^(\d\.)+/)) {
1003            print "  iodbc_config not found\n";
1004            return;
1005        }
1006    }
1007    print "  Found iodbc-config (via $configbin) version $iodbc_config_v\n";
1008
1009    my $iodbc_ini = `$configbin --odbcini 2>&1`;
1010    print "  ODBC data sources should be added to $iodbc_ini\n"
1011        if ($iodbc_ini);
1012
1013    my $iodbc_instini = `$configbin --odbcinstini 2>&1`;
1014    print "  ODBC drivers should be added to $iodbc_instini\n"
1015        if ($iodbc_instini);
1016
1017    my @hdrstofind = ('sql.h', 'sqlext.h', 'sqltypes.h');
1018    push @hdrstofind, 'sqlucode.h' if $opt_u;
1019
1020    $home = `$configbin --prefix 2>&1`;
1021    if (!defined($home)) {
1022        print "  cannot find --prefix from iodbc_config\n";
1023        return;
1024    }
1025    chomp $home;
1026    print "  iodbc-config reports --prefix=$home\n";
1027
1028    $inc = File::Spec->catdir($home, 'include');
1029    $lib = File::Spec->catdir($home, 'lib');
1030
1031    my $found_hdrs = 0;
1032    if ((defined($inc)) && (-e $inc) && files_exist($inc, @hdrstofind)) {
1033        print "  ODBC INC dir set to $inc from iodbc-config\n";
1034        $found_hdrs++;
1035    } elsif (-e $home) {
1036        # try with --prefix + include
1037        $inc = File::Spec->catdir($home, 'include');
1038        if (defined($inc) && (-e $inc) && files_exist($inc, @hdrstofind)) {
1039            print "  ODBC INC dir set to $inc from iodbc_config\n";
1040            $found_hdrs++;
1041        }
1042    }
1043    if (!$found_hdrs) {
1044        print "  but cannot find header files " . join(',', @hdrstofind) .
1045            " in that path so ignoring\n";
1046        print "NOTE: Have you installed the libiodbc-dev package.\n";
1047        return;
1048    }
1049
1050    if (-e $lib) {
1051        print "  ODBC LIB dir set to $lib from iodbc_config/lib\n";
1052    } else {
1053        print "  but cannot find liob dir so ignoring\n";
1054        return;
1055    }
1056
1057    return ('iodbc', $iodbc_config_v, $home, $inc, $lib);
1058}
1059
1060#
1061# Try and find ODBC driver manager header files in general areas
1062#
1063sub find_dm_hdr_files
1064{
1065    my ($home, $inc, $lib);
1066
1067    my @hdrstofind = ('sql.h', 'sqlext.h', 'sqltypes.h');
1068    push @hdrstofind, 'sqlucode.h' if $opt_u;
1069
1070    my @paths = ('/usr', '/usr/local', '/usr/pkg',
1071                 '/usr/local/easysoft/unixODBC');
1072    unshift @paths, $opt_o if $opt_o;
1073    unshift @paths, $ENV{ODBCHOME} if $ENV{ODBCHOME};
1074
1075    foreach my $dir(@paths) {
1076        my $path = File::Spec->catdir($dir, 'include');
1077        print "  trying $path\n";
1078        if (files_exist($path, @hdrstofind)) {
1079            print "  Found " . join(', ', @hdrstofind) . " in $path\n";
1080            $home = $dir;
1081            return $home;
1082        }
1083    }
1084    print <<'EOT';
1085
1086I cannot find driver manager header files. Perhaps you need to install the
1087unixodbc-dev package or the iodbc-dev package
1088
1089EOT
1090    return;
1091}
1092
1093sub find_iodbc
1094{
1095    my $home = shift;           # will be specified odbc home or one we've found
1096
1097    my @dirs;
1098    # start with specified dir if there was one
1099    push @dirs, "$home/lib" if defined($opt_o) || defined($ENV{ODBCHOME});
1100
1101    # look in perl's libspath as it is more likely to be compatible
1102    # (e.g., a lib64 dir)
1103    push @dirs, split(' ', $Config{libspath});
1104    # add found odbc home if not added already
1105    if (defined($opt_o) || defined($ENV{ODBCHOME})) {
1106        push @dirs, "$home";
1107        push @dirs, "$home/lib";
1108    }
1109    for my $d(@dirs) {
1110        print "  Looking for iODBC libs in $d\n";
1111        my @found;
1112        if (@found = glob "$d/libiodbc*") {
1113            print "    Found iODBC libs ", join(",", @found), " in $d\n";
1114            return ('iodbc', $d);
1115        }
1116    }
1117    return;
1118}
1119
1120sub find_unixodbc
1121{
1122    my $home = shift;           # will be specified odbc home or one we've found
1123
1124    my @dirs;
1125    # start with specified dir if there was one
1126    push @dirs, "$home/lib" if defined($opt_o) || defined($ENV{ODBCHOME});
1127
1128    # look in perl's libspath as it is more likely to be compatible
1129    # (e.g., a lib64 dir)
1130    push @dirs, split(' ', $Config{libspath});
1131    # add found odbc home if not added already
1132    push @dirs, "$home/lib" if !defined($opt_o) && !defined($ENV{ODBCHOME});
1133    # for debian/ubuntu 12 thanks to Maestro(Geoff Darling, mitsi)  for finding:
1134    push @dirs, "/usr/lib/i386-linux-gnu/";
1135
1136    for my $d(@dirs) {
1137        my @found;
1138        print "  Looking for unixODBC libs in $d\n";
1139        if (@found = glob "$d/libodbc*") {
1140            print "    Found unixODBC libs ", join(",", @found), " in $d\n";
1141            return ('unixodbc', $d);
1142        }
1143    }
1144    return;
1145}
1146
1147# Following generates
1148# Useless use of private variable in void context at Makefile.PL
1149# but is required
1150no warnings 'void';
1151$rv;
1152
1153# ====================================================================
1154
1155package MY;
1156
1157use strict;
1158use Config;
1159use English;
1160
1161sub postamble {
1162    return DBI::DBD::dbd_postamble(@_);
1163}
1164
1165sub const_cccmd {
1166    my $self = shift;
1167    local($_) = $self->SUPER::const_cccmd(@_);
1168
1169    # inject the defined local ODBC before default include to ensure
1170    # the ODBC driver we want is first
1171    if ($OSNAME ne 'MSWin32') {
1172        s/-c/-c \$\(DEFINE\)/;
1173    }
1174    $_;
1175}
1176sub MY::post_constants {
1177    my ($self) = shift;
1178
1179    '
1180# make Changes file available as installed pod docs "perldoc DBD::ODBC::Changes"
1181inst_libdbdodbc = ' . File::Spec->catdir($self->{INST_LIB}, 'DBD/ODBC') . '
1182changes_pm = ' . File::Spec->catfile($self->{INST_LIB}, 'DBD/ODBC', 'Changes.pm') . '
1183
1184# make FAQ file available as installed pod docs "perldoc DBD::ODBC::FAQ"
1185inst_libdbdodbc = ' . File::Spec->catdir($self->{INST_LIB}, 'DBD/ODBC') . '
1186faq_pm = ' . File::Spec->catfile($self->{INST_LIB}, 'DBD/ODBC', 'FAQ.pm') . '
1187
1188# make TO_DO file available as installed pod docs "perldoc DBD::ODBC::TO_DO"
1189inst_libdbdodbc = ' . File::Spec->catdir($self->{INST_LIB}, 'DBD/ODBC') . '
1190todo_pm = ' . File::Spec->catfile($self->{INST_LIB}, 'DBD/ODBC', 'TO_DO.pm') . '
1191
1192config :: $(changes_pm) $(faq_pm) $(todo_pm)
1193	@$(NOOP)
1194
1195$(changes_pm): Changes
1196	$(NOECHO) $(MKPATH) $(inst_libdbdodbc)
1197	$(NOECHO) $(RM_F) $(changes_pm)
1198	$(CP) Changes $(changes_pm)
1199
1200$(faq_pm): FAQ
1201	$(NOECHO) $(MKPATH) $(inst_libdbdodbc)
1202	$(NOECHO) $(RM_F) $(faq_pm)
1203	$(CP) FAQ $(faq_pm)
1204
1205$(todo_pm): TO_DO
1206	$(NOECHO) $(MKPATH) $(inst_libdbdodbc)
1207	$(NOECHO) $(RM_F) $(todo_pm)
1208	$(CP) TO_DO $(todo_pm)
1209';
1210
1211}
1212
1213sub constants {
1214    my $self = shift;
1215    require DBI::DBD;
1216
1217    # The problem with stawberry perl is it sets INC on the command line
1218    # and that overrides INC in this Makefile unless we set it here.
1219    my $old_constants = $self->SUPER::constants();
1220    my $new_constants;
1221    foreach my $line ( split(/\n/, $old_constants) ) {
1222        if ( $line =~ /^INC = .*strawberry.*/ ) {
1223            print qq(Strawberry Perl found; adjusting the INC variable;\n);
1224            $line = $line . q( -I) .  DBI::DBD::dbd_dbi_arch_dir();
1225            print qq(INC is now $line\n);
1226        }
1227        $new_constants .= $line . qq(\n);
1228    }
1229    return $new_constants;
1230}
1231
1232# end.
1233