-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile.PL
498 lines (416 loc) · 14.5 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/perl
#*******************************************************************************
# Copyright (c) 2001-2008, Perforce Software, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#*******************************************************************************
use strict;
use warnings;
use lib '.'; # Make sure we find Build::Version
use Build::Version;
use Config;
use Getopt::Long;
use ExtUtils::MakeMaker;
use Cwd;
use Cwd 'abs_path';
use Data::Dumper;
use File::Copy;
use English;
# --- Supported API version ---------------------------------------------------
our $TARGET_API_VERSION = "2024.2";
# --- GLOBALS -----------------------------------------------------------------
our $ssl_path = undef; ## SSL path string
our $p4api_path = undef; ## P4API path string
our $p4api = undef; ## P4API version object
our $p4perl = undef; ## P4PERL version object
# =============================================================================
# MAIN Function
# Arguments: apidir = <path> (required)
# ssl = <lib path> (optional)
# =============================================================================
## Debug options
# $ExtUtils::MakeMaker::Verbose = 1;
## Fetch arguments
my $apidir = undef;
GetOptions(
"apidir=s" => \$apidir,
"ssl:s" => \$ssl_path
);
## Attempt to pull 'Version' and doc (for Build)
my $path = "../p4/Version";
copy( $path, "Version" ) if ( -f $path );
my $src = "../p4-doc/user/p4perlnotes.txt";
my $dst = "RELNOTES.txt";
copy( $src, $dst ) if ( -f $src );
## Set globals for api's version and path
print( "Verifying external libraries...\n");
$p4api_path = p4api_find_path($apidir);
$p4api = p4api_version($p4api_path);
$p4perl = p4perl_version();
## Makefile Flags
my %make_flags = (
'NAME' => 'P4',
'VERSION' => $p4perl->toString(),
'PREREQ_PM' => {}, # e.g., Module::Name => 1.1
'MYEXTLIB' => 'lib/libp4$(LIB_EXT)',
'XSOPT' => '-C++ -prototypes',
'CONFIGURE' => \&config_sub,
'DISTVNAME' => $p4perl->toTarget(),
'PL_FILES' => {},
'clean' => { 'FILES' => "p4perl.*" },
);
WriteMakefile(%make_flags);
exit 0;
# ===================================================================== END ===
# -----------------------------------------------------------------------------
# Function: p4perl_version
# Description: Returns P4PERL version object
# -----------------------------------------------------------------------------
sub p4perl_version {
# Read API version file
my $ver = new Version("Version");
die("Aborting: Cannot read local p4-perl Version file!") unless $ver;
# logging...
print( " building p4perl version: \t" . $ver->toPatch() . "\n" );
return $ver;
}
# -----------------------------------------------------------------------------
# Function: p4api_version
# Description: Returns P4API version object
# -----------------------------------------------------------------------------
sub p4api_version {
my $path = shift;
# Read API version file
my $ver = new Version("$path/Version");
# try alternative location or die
$ver = new Version("$path/sample/Version") if ( !$ver );
# if no Version string then ask user
my $tries = 3;
while ( !$ver && --$tries ) {
print("Unable to determine API version string\n");
print("Enter API version (REL.NAME/PATCH): ");
my $string = <STDIN>;
$ver = new Version();
$ver->parsePatch($string);
}
die("Aborting: API version not provided") unless $ver;
# logging...
print( " using p4api path: \t\t$path\n");
print( " using p4api version: \t\t" . $ver->toPatch() . "\n" );
return $ver;
}
# -----------------------------------------------------------------------------
# Function: p4api_find_path
# Description: Returns full path to P4API.
# -----------------------------------------------------------------------------
sub p4api_find_path {
my $path = shift;
# return full path if it exists
if ( defined($path) ) {
$path = abs_path($path);
if ( is_api_dir($path) ) {
return $path;
}
}
# Ask user for P4API path
$path = undef;
my $tries = 3;
while ( !defined($path) && --$tries ) {
print("Enter the path to the Perforce API: ");
$path = <STDIN>;
chomp ($path);
$path = undef if($path eq "");
# Filthy support for ~/ type paths ( NOT ~user/ though! )
if($path) {
$path =~ s#\~/#$ENV{HOME}/#;
$path = abs_path($path);
$path = undef unless ( is_api_dir($path) );
}
}
die("Aborting - no API directory provided") unless $path;
return $path;
}
# -----------------------------------------------------------------------------
# Function: is_api_dir
# Description: Test to see if 'Version' file is in a known location
# -----------------------------------------------------------------------------
sub is_api_dir {
my $dir = shift;
return 0 unless ( -d $dir );
return 1 if ( -e "$dir/Version" );
return 1 if ( -e "$dir/sample/Version" );
return 0;
}
# -----------------------------------------------------------------------------
# Function: check_api_version
# Description: checks Version object; returns 1 if ok, 0 if not compattable
# -----------------------------------------------------------------------------
sub check_api_version {
my $ver = shift;
my $target = new Version();
$target->parsePatch($TARGET_API_VERSION);
my $v = $ver->getRelease();
my $t = $target->getRelease();
return 1 if ( $v == $t );
my $string = $ver->toString();
print <<EOF;
This version of P4Perl was designed for use with the $TARGET_API_VERSION
release of the Perforce C++ API. Using it with other releases may not work,
and is not supported. ($string)
EOF
print("Do you wish to continue anyway? (y/n): ");
my $answer = <STDIN>;
chomp($answer);
return 0 unless $answer eq "y";
return 1;
}
# -----------------------------------------------------------------------------
# Function: define
# Description: Define Make Macos (qq strings)
# -----------------------------------------------------------------------------
sub define( $$;$$ ) {
my $href = shift;
my $var = shift;
my $val = shift;
my $string = shift;
if ( !$val ) {
$href->{'DEFINE'} .= qq{ -D$var};
}
elsif ($string) {
$href->{'DEFINE'} .= qq{ -D$var="\\"$val\\""};
}
else {
$href->{'DEFINE'} .= qq{ -D$var="$val"};
}
}
# -----------------------------------------------------------------------------
# Function: add_p4_libs
# Description: Add the Perforce libraries to the linker configuration
# -----------------------------------------------------------------------------
sub add_p4_libs( $$$$ ) {
my $cfg = shift; # Perl's pre-set config
my $flags = shift; # Flags we want to add
my $apidir = shift; # Our API directory
my $ssldir = shift; # Our SSL directory
my $os = uc( $Config{osname} ); # LINUX, DARWIN, MSWIN32
my $arch = $Config{ptrsize} * 8; # 32 or 64
# Generate path list and string
my @paths;
push( @paths, "$apidir/lib" ) if ( -d "$apidir/lib" );
if ( defined $ssldir && -d $ssldir ) {
push( @paths, $ssldir );
print(" using ssl path: \t\t$ssldir\n");
}
elsif ( defined $ssldir ) {
print(" ssl defined:\t\t\t(using system path)\n");
}
# Pull in appropriate paths for MinGW on Windows
if ( $os eq "MSWIN32" && $Config{cc} =~ /gcc/i ) {
my $mingw_libpath = $Config{libpth};
my $sitelib = $Config{sitelibexp};
$sitelib =~ s#\\#/#g;
$mingw_libpath =~ s#\\#/#g;
$mingw_libpath =~ s#.*:##g;
$mingw_libpath = $sitelib . "/auto" . $mingw_libpath;
push( @paths, $mingw_libpath );
}
my $libpath = join( " -L", "", @paths );
# Build library list
my @libs = ( "p4api" );
# [MSWIN32]
if ( $os eq "MSWIN32" && $Config{cc} !~ /gcc/i ) {
push( @libs, "libeay32" );
push( @libs, "ssleay32" );
}
# [MSWIN32] and NOT MinGW
elsif ( $os eq "MSWIN32" ) {
push( @libs, "ssl" );
push( @libs, "crypto" );
push( @libs, "crypt32" );
}
# [LINUX]
elsif ( $os eq "LINUX" ) {
push( @libs, "rt" );
push( @libs, "ssl" );
push( @libs, "crypto" );
}
# [DEFAULT]
else {
push( @libs, "ssl" );
push( @libs, "crypto" );
}
# Generate library string
# note: the empty field "" adds -l to the first element
my $liblist = join( " -l", "", @libs );
$flags->{'LIBS'} = [];
if ( defined( $cfg->{LIBS} ) ) {
my $libs = $cfg->{LIBS};
foreach my $libset (@$libs) {
push( @{ $flags->{LIBS} }, "$libpath $liblist $libset" );
}
}
else {
push( @{ $flags->{LIBS} }, "$libpath $liblist" );
}
}
# -----------------------------------------------------------------------------
# Function: add_p4_hdrs
# Description: Add the Perforce headers to the includes
# -----------------------------------------------------------------------------
sub add_p4_hdrs( $$ ) {
my $flags = shift;
my $apipath = shift;
$apipath = "$apipath/include/p4" if ( -d "$apipath/include/p4" );
$flags->{'INC'} = "-I$apipath -Ilib";
}
# -----------------------------------------------------------------------------
# Function: get_platform
# Description: return a string for the platform we are on. Anything specific
# to one platform should go in its hints file.
# -----------------------------------------------------------------------------
sub get_platform {
my $href = shift;
# Read $Config{ 'archname' ), which looks like this:
#
# i86pc-solaris-64int (solaris10x86_64 )
# x86_64-linux-gnu-thread-multi (linux26x86_64)
# MSWin32-x86-multi-thread (ntx86) - note os/plat reversal!!!
#
# So, we get the osname from Config, then we use whichever of the
# first two fields in archname is NOT the os as the platform.
#
my $os = $Config{'osname'};
my @fields = split( /-/, $Config{'archname'} );
print( " with archname: \t\t" . $Config{'archname'} . "\n");
my $plat;
$plat = $fields[0] if ( $os eq $fields[1] );
$plat = $fields[1] if ( $os eq $fields[0] );
# Now convert the platform identifier to Perforce style for known
# differences in terminology
$plat = "x86" if ( $plat =~ /[xi]86pc/ );
$plat = "x86" if ( $plat =~ /[xi]\d?86$/ );
$plat = "sparc" if ( $plat =~ /sun/ );
$plat = uc($plat);
# Override derived value with hints from the hints file
if ( defined( $href->{'P4PERL_PLAT_HINT'} ) ) {
$plat = $href->{'P4PERL_PLAT_HINT'};
delete( $href->{'P4PERL_PLAT_HINT'} );
}
print(" using os: \t\t\t$os\n");
print(" using platform: \t\t$plat\n");
return $plat;
}
# -----------------------------------------------------------------------------
# Function: get_os
# Description: return a string for the os we are on
# -----------------------------------------------------------------------------
sub get_os {
my $href = shift;
my $os = $Config{'osname'};
# Now convert the OS to Perforce style for known differences.
$os = "NT" if ( $os eq "MSWin32" );
# Default is our guestimate.
$os = uc($os);
# Override derived value with hints from the hints file
if ( defined( $href->{'P4PERL_OS_HINT'} ) ) {
$os = $href->{'P4PERL_OS_HINT'};
delete( $href->{'P4PERL_OS_HINT'} );
}
return $os;
}
# -----------------------------------------------------------------------------
# Function: get_osver
# Description: return a string for the os version we are on
# -----------------------------------------------------------------------------
sub get_osver {
my $href = shift;
my $os = get_os();
my $osver;
# Identify the OS version. We take the first two numbers in the
# dotted string. On NT, we don't bother.
if ( $os eq "NT" ) {
$osver = "";
}
else {
$osver = $Config{'osvers'};
$osver =~ s/^(\d+)\.(\d+).*/$1$2/;
}
# Override derived value with hints from the hints file
if ( defined( $href->{'P4PERL_OSVER_HINT'} ) ) {
$osver = $href->{'P4PERL_OSVER_HINT'};
delete( $href->{'P4PERL_OSVER_HINT'} );
}
return $osver;
}
# -----------------------------------------------------------------------------
# Function: config_sub
# Description: Called by WriteMakefile - adds the Perforce API path to the
# header includes and libs
# -----------------------------------------------------------------------------
sub config_sub {
my $class = shift;
my $href = shift;
# Work out what platform we're running on
my $plat = get_platform($href);
my $os = get_os($href);
my $osver = get_osver($href);
# Define Ident macros
define( $href, "ID_OS", $os . $osver . $plat, 1 );
define( $href, "ID_REL", $p4perl->toString(), 1 );
define( $href, "ID_PATCH", $p4perl->getPatch(), 1 );
define( $href, "ID_Y", $p4perl->getDate()->[0], 1 );
define( $href, "ID_M", $p4perl->getDate()->[1], 1 );
define( $href, "ID_D", $p4perl->getDate()->[2], 1 );
# Define OS_* macros
define( $href, "OS_$os" );
define( $href, "OS_$os$osver" );
define( $href, "OS_$os$osver$plat" );
define( $href, "OS_$os$plat" );
# Now find the API!
my $flags = {};
# Abort if the user's decided not to press ahead with a newer
# version of the API than this version was written for
exit(1) unless ( check_api_version($p4api) );
define( $href, "P4API_VERSION", $p4api->getRelease() );
define( $href, "ID_API", $p4api->toPatch(), 1 );
add_p4_libs( $href, $flags, $p4api_path, $ssl_path );
add_p4_hdrs( $flags, $p4api_path );
# Post distribution move
my $source = $p4perl->toTarget() . ".tar.gz";
my $target = "p4perl.tar.gz";
if ( $os eq 'NT' ) {
$flags->{'dist'}{'POSTOP'} = "ren $source $target";
$flags->{'DISTVNAME'} = $p4perl->toTarget();
}
else {
$flags->{'dist'}{'POSTOP'} = "mv -f $source $target";
$flags->{'DISTVNAME'} = $p4perl->toTarget();
}
return $flags;
}
# Ensure that the clientuserperl interface gets built.
sub MY::postamble {
'
$(MYEXTLIB): lib/Makefile
cd lib && $(MAKE) $(PASSTHRU)
';
}