-
Notifications
You must be signed in to change notification settings - Fork 17
/
Makefile.PL
161 lines (133 loc) · 4.24 KB
/
Makefile.PL
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use 5.020;
use strict;
use warnings;
use Config;
use ExtUtils::MakeMaker;
use ExtUtils::CppGuess;
# TODO: Optionally use system libre2, via ExtUtils::Liblist?
my @objects = qw(RE2.o re2_xs.o re2/obj/libre2.a);
my $guess = ExtUtils::CppGuess->new;
my %opt = (
NAME => 're::engine::RE2',
AUTHOR => 'David Leadbeater <dgl@dgl.cx>',
VERSION_FROM => 'lib/re/engine/RE2.pm',
ABSTRACT_FROM => 'lib/re/engine/RE2.pm',
LICENSE => 'perl',
INC => '-Ire2',
PMLIBDIRS => ["lib"],
OBJECT => join(" ", @objects),
test => {TESTS => 't/*.t t/ree-pcre/*.t'},
CONFIGURE_REQUIRES => {
"ExtUtils::CppGuess" => 0,
"Test::More" => 0.88,
},
$guess->makemaker_options
);
if(eval { ExtUtils::MakeMaker->VERSION(6.46) }) {
$opt{META_MERGE} = {
'meta-spec' => { version => 2 },
resources => {
repository => {
type => 'git',
web => 'https://github.com/dgl/re-engine-RE2',
},
bugtracker => {
web => 'https://github.com/dgl/re-engine-RE2/issues',
}
}
}
}
my $cc = (map +(/^CC=(.*)/i), @ARGV)[0] || $Config{cc};
if (my $gcc_version = gcc_version($cc)) {
say "Compiling on gcc $gcc_version";
# If the user didn't explicitly provide optimisation settings, we'll try to do
# it ourselves, but only for gcc.
if(!grep(/^OPTIMIZE=/i, @ARGV)) {
my $optimize = $Config{optimize};
if($gcc_version) {
$optimize =~ s/-O[s0-2]/-O3/ and say "Optimize level set to -O3";
}
# Attempt to work out if we have a gcc that is likely to support -flto.
# This is probably a lot of work for a minimal gain, but it's worth a try.
if($gcc_version >= 4.5) {
my $try_optimize = "$optimize -flto";
# Try to use this flag
if(gcc_try(cc => $cc, %opt, OPTIMIZE => $try_optimize)) {
$optimize = $try_optimize;
}
# gcc 4.9 needs this otherwise it gets rid of nearly everything in libre2.a.
$try_optimize = "$optimize -ffat-lto-objects";
if(gcc_try(cc => $cc, %opt, OPTIMIZE => $try_optimize)) {
$optimize = $try_optimize;
}
}
say "OPTIMIZE is now: $optimize";
$opt{OPTIMIZE} = $optimize;
}
if ($gcc_version >= 4.8) {
$opt{DEFINE} .= " -std=c++11";
} else {
say "Need gcc version new enough to support C++11, exiting.";
exit 1;
}
}
if (my $clang_version = clang_version($cc)) {
say "Compiling on clang $clang_version";
if ($clang_version >= 3) {
$opt{DEFINE} .= " -std=c++11";
} else {
say "Need clang version new enough to support C++11, this will probably fail.";
}
}
if(defined $Config{usethreads} && $Config{usethreads} eq 'define') {
if(defined $Config{i_pthread} && $Config{i_pthread} eq 'define') {
$opt{DEFINE} .= " -DHAVE_PTHREAD -pthread";
} else {
# For now this allows compilation under Win32/Strawberry, but might cause weird crashes on thread
# destruction...
$opt{DEFINE} .= " -DNO_THREADS";
}
} else {
$opt{DEFINE} .= " -DNO_THREADS";
}
# This is a bit hacky, RE2 makefile needs GNU make, for now we'll try to find
# it, ideally should rewrite the RE2 makefile to not need GNU make.
our $MAKE;
for my $make(qw(make gmake)) {
if(qx{$make --version 2>&1} =~ /GNU Make/i) {
$MAKE = $make;
last;
}
}
if(!$MAKE) {
die "RE2 currently needs GNU Make, please install gmake.\n";
}
WriteMakefile(%opt);
sub gcc_version {
my($cc) = @_;
my $gcc_out = qx{$cc -v 2>&1};
# Just the first two digits
$gcc_out =~ /gcc version (\d+\.\d+)/ ? $1 : 0;
}
# This is highly gcc and unix specific, but that's where I care about
# optimising this anyway.
sub gcc_try {
my(%opts) = @_;
system "$opts{cc} $opts{CCFLAGS} $opts{OPTIMIZE} -c -o /dev/null /dev/null >/dev/null 2>&1";
not $?;
}
sub clang_version {
my ($cc) = @_;
my $out = qx{$cc -v 2>&1};
# Just the major
$out =~ /clang version (\d+)/ ? $1 : 0;
}
sub MY::postamble {
return <<MAKE_FRAG;
RE2_FLAGS = CC="\$(CC)" CXXFLAGS="\$(CCFLAGS) \$(CCCDLFLAGS) \$(OPTIMIZE) \$(DEFINE) -DUSE_CXX0X" LDFLAGS="\$(OTHERLDFLAGS) \$(LDLOADLIBS)"
re2/obj/libre2.a: re2/Makefile
$MAKE -C re2 obj/libre2.a \$(RE2_FLAGS)
re2-tests:
$MAKE -C re2 static-test \$(RE2_FLAGS) LDFLAGS="\$(OTHERLDFLAGS) \$(LDLOADLIBS) -lm -lpthread"
MAKE_FRAG
}