aboutsummaryrefslogtreecommitdiff
path: root/src/tools/msvc/pgflex.pl
blob: 332d422f6492ce9650d93582942d7eb9f9b0a5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*-perl-*- hey - emacs - this is a perl file

# src/tools/msvc/pgflex.pl

# silence flex bleatings about file path style
$ENV{CYGWIN} = 'nodosfilewarning';

use strict;
use File::Basename;

# assume we are in the postgres source root

require 'src/tools/msvc/buildenv.pl' if -e 'src/tools/msvc/buildenv.pl';

system('flex -V > NUL');
if ($? != 0)
{
    print "WARNING! flex install not found, attempting to build without\n";
    exit 0;
}

my $input = shift;
if ($input !~ /\.l$/)
{
    print "Input must be a .l file\n";
    exit 1;
}
elsif (!-e $input)
{
    print "Input file $input not found\n";
    exit 1;
}

(my $output = $input) =~ s/\.l$/.c/;

# get flex flags from make file
my $makefile = dirname($input) . "/Makefile";
my ($mf, $make);
open($mf,$makefile);
local $/ = undef;
$make=<$mf>;
close($mf);
my $flexflags = ($make =~ /^\s*FLEXFLAGS\s*=\s*(\S.*)/m ? $1 : '');

system("flex $flexflags -o$output $input");
if ($? == 0)
{

    # For non-reentrant scanners we need to fix up the yywrap macro definition
    # to keep the MS compiler happy.
    # For reentrant scanners (like the core scanner) we do not
    # need to (and must not) change the yywrap definition.
    my $lfile;
    open($lfile,$input) || die "opening $input for reading: $!";
    my $lcode = <$lfile>;
    close($lfile);
    if ($lcode !~ /\%option\sreentrant/)
    {
        my $cfile;
        open($cfile,$output) || die "opening $output for reading: $!";
        my $ccode = <$cfile>;
        close($cfile);
        $ccode =~ s/yywrap\(n\)/yywrap()/;
        open($cfile,">$output") || die "opening $output for reading: $!";
        print $cfile $ccode;
        close($cfile);
    }

    exit 0;

}
else
{
    exit $? >> 8;
}