3 # w32locatedb.pl -- Build apr-util with Berkeley DB on Win32
5 # Usage: perl w32locatedb.pl <type> <incdir> <libdir>
6 # type: Library type to link with ('lib' or 'dll')
7 # incdir: BDB includes directory (for db.h)
8 # libdir: Library directory (for libdbXY[s][d].lib)
10 # This script falls under the Apache License.
11 # See http://www.apache.org/docs/LICENSE
15 use File::Spec::Functions qw(canonpath rel2abs
16 splitpath catpath splitdir catdir);
19 # Subroutine prototypes
27 # Parse program arguments and set globals
28 die usage() unless scalar @ARGV >= 3;
30 my $type = lc($ARGV[0]);
31 die "Invalid library type '$type'\n"
32 unless $type eq 'lib' or $type eq 'dll';
34 my $incdir = $ARGV[1];
35 die "No 'db.h' in $incdir\n" unless -f "$incdir/db.h";
37 my $libdir = $ARGV[2];
38 die "$libdir: $!" unless -d $libdir;
40 my $libname = get_lib_name($type, $incdir);
41 die "No '$libname.lib' in $libdir" unless -f "$libdir/$libname.lib";
42 die "No '${libname}d.lib' in $libdir" unless -f "$libdir/${libname}d.lib";
44 my $srcdir = find_srcdir();
45 my $apu_hw = canonpath("$srcdir/include/apu.hw");
46 my $apu_want_hw = canonpath("$srcdir/include/apu_want.hw");
47 my $apu_select_dbm_hw = canonpath("$srcdir/include/private/apu_select_dbm.hw");
48 my $aprutil_dsp = canonpath("$srcdir/aprutil.dsp");
49 my $libaprutil_dsp = canonpath("$srcdir/libaprutil.dsp");
50 die "Can't find $apu_hw" unless -f $apu_hw;
51 die "Can't find $apu_want_hw" unless -f $apu_want_hw;
52 die "Can't find $apu_select_dbm_hw" unless -f $apu_select_dbm_hw;
53 die "Can't find $aprutil_dsp" unless -f $aprutil_dsp;
54 die "Can't find $libaprutil_dsp" unless -f $libaprutil_dsp;
58 # Edit the header file templates
59 my $db_h = rel2abs(canonpath("$incdir/db.h"));
62 [['^\s*\#\s*define\s+APU_HAVE_DB\s+0\s*$',
63 '#define APU_HAVE_DB 1']]);
64 edit_header($apu_want_hw,
65 [['^\s*\#\s*include\s+\<db\.h\>\s*$',
66 "#include \"$db_h\""]]);
67 edit_header($apu_select_dbm_hw,
68 [['^\s*\#\s*define\s+APU_USE_DB\s+0\s*$',
69 '#define APU_USE_DB 1'],
70 ['^\s*\#\s*include\s+\<db\.h\>\s*$',
71 "#include \"$db_h\""]]);
75 my $libpath = rel2abs(canonpath("$libdir/$libname"));
76 edit_project($aprutil_dsp, $libpath);
77 edit_project($libaprutil_dsp, $libpath);
84 return ("Usage: perl w32locatedb.pl <type> <incdir> <libdir>\n"
85 . " type: Library type to link with ('lib' or 'dll')\n"
86 . " incdir: BDB includes directory (for db.h)\n"
87 . " libdir: Library directory (for libdbXY[s][d].lib)\n");
91 # Calculate the (possibly relative) path to the top of the apr-util
95 my $srcdir = rel2abs(canonpath($0));
96 my ($vol, $dir, $file) = splitpath($srcdir);
97 my @dirs = splitdir($dir);
98 die if scalar @dirs < 1;
99 do { $_ = pop @dirs } while ($_ eq '');
100 return catpath($vol, catdir(@dirs), '');
104 # Construct the name of the BDB library, based on the type and
105 # version information in db.h
108 my ($type, $incdir) = @_;
113 open(DBH, "< $incdir/db.h")
114 or die "Can't open $incdir/db.h: $!";
117 m/^\s*\#\s*define\s+DB_VERSION_(MAJOR|MINOR|PATCH)\s+(\d+)\s*$/;
118 next unless defined $1 and defined $2;
119 if ($1 eq 'MAJOR') { $major = $2; }
120 elsif ($1 eq 'MINOR') { $minor = $2; }
121 elsif ($1 eq 'PATCH') { $patch = $2; }
122 last if defined $major and defined $minor and defined $patch;
125 die "Can't determine BDB version\n"
126 unless defined $major and defined $minor and defined $patch;
128 print "Using BDB version $major.$minor.$patch\n";
130 my $libname = "libdb$major$minor";
131 $libname .= 's' if $type eq 'lib';
136 # Replace a file, keeping a backup copy
137 sub maybe_rename_with_backup($$$)
139 my ($tmpfile, $file, $maybe) = @_;
141 # Make the file writable by the owner. On Windows, this removes
142 # any read-only bits.
143 chmod((stat($file))[2] | 0600, $file);
144 rename($file, "${file}~");
145 rename($tmpfile, $file);
147 print "No changes in $file\n";
153 # Edit a header template in-place.
156 my ($file, $pairs) = @_;
157 my $tmpfile = "$file.tmp";
160 open(IN, "< $file") or die "Can't open $file: $!";
161 open(TMP, "> $tmpfile") or die "Can't open $tmpfile: $!";
164 foreach my $pair (@$pairs) {
165 $substs += s/${$pair}[0]/${$pair}[1]/;
172 maybe_rename_with_backup($tmpfile, $file, $substs > 0);
176 # Edit a project file in-place
179 my ($file, $libpath) = @_;
180 my $tmpfile = "$file.tmp";
182 my ($prog, $debug) = (undef, undef);
184 my $libsearch = $libpath;
185 $libsearch =~ s/\\/\\\\/g;
187 open(IN, "< $file") or die "Can't open $file: $!";
188 open(TMP, "> $tmpfile") or die "Can't open $tmpfile: $!";
192 if (m/^\# TARGTYPE \"[^\"]+\" 0x([0-9A-Za-z]+)/
194 $prog = 'LINK32' if $1 eq '0102';
195 $prog = 'LIB32' if $1 eq '0104';
196 die "Unknown project type 0x$1" unless defined $prog;
197 } elsif (defined $prog
198 and m/^\# PROP Use_Debug_Libraries ([01])/
201 } elsif (defined $prog and defined $debug
202 and m/^\# ADD $prog (\"$libsearch)?/
203 and not defined $1) {
205 ($debug eq '1' ? "${libpath}d.lib" : "$libpath.lib");
206 $substs += s/^\# ADD $prog /\# ADD $prog \"$fullpath\" /;
207 } elsif (m/^\# ADD CPP/) {
208 $substs += s/APU_USE_SDBM/APU_USE_DB/g;
216 maybe_rename_with_backup($tmpfile, $file, $substs > 0);