| 1 |
# Makefile.PL for XML::Hash::XS. |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify it |
| 4 |
# under the same terms as Perl itself. |
| 5 |
# Copyright 2012-2021 Yuriy Ustushenko, all rights reserved. |
| 6 |
# |
| 7 |
# Derived from the module XML::LibXML |
| 8 |
# Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas |
| 9 |
|
| 10 |
use 5.008008; |
| 11 |
use warnings; |
| 12 |
use strict; |
| 13 |
use lib qw(inc); |
| 14 |
use Devel::CheckLib; |
| 15 |
use ExtUtils::MakeMaker; |
| 16 |
use Config; |
| 17 |
use File::Spec; |
| 18 |
|
| 19 |
$| = 0; |
| 20 |
|
| 21 |
our $IS_VC = $Config{cc} eq 'cl'; |
| 22 |
our %PARAMS = parse_params(); |
| 23 |
|
| 24 |
my %WriteMakefileArgs = ( |
| 25 |
NAME => 'XML::Hash::XS', |
| 26 |
VERSION_FROM => 'lib/XML/Hash/XS.pm', |
| 27 |
ABSTRACT_FROM => 'lib/XML/Hash/XS.pm', |
| 28 |
AUTHOR => 'Yuriy Ustushenko <[email protected]>', |
| 29 |
test => { |
| 30 |
TESTS => 't/*.t'. ($ENV{AUTHOR_TESTING} ? ' xt/*.t' : '') |
| 31 |
}, |
| 32 |
META_MERGE => { |
| 33 |
'meta-spec' => { version => 2 }, |
| 34 |
resources => { |
| 35 |
license => 'http://dev.perl.org/licenses/', |
| 36 |
homepage => 'https://github.com/yoreek/XML-Hash-XS', |
| 37 |
repository => { |
| 38 |
type => 'git', |
| 39 |
url => 'https://github.com/yoreek/XML-Hash-XS.git', |
| 40 |
web => 'https://github.com/yoreek/XML-Hash-XS', |
| 41 |
}, |
| 42 |
bugtracker => { |
| 43 |
web => 'https://github.com/yoreek/XML-Hash-XS/issues', |
| 44 |
}, |
| 45 |
}, |
| 46 |
}, |
| 47 |
META_ADD => { |
| 48 |
prereqs => { |
| 49 |
configure => { |
| 50 |
requires => { |
| 51 |
'ExtUtils::MakeMaker' => 0, |
| 52 |
'Config' => 0, |
| 53 |
'File::Spec' => 0, |
| 54 |
}, |
| 55 |
}, |
| 56 |
build => { |
| 57 |
requires => { |
| 58 |
'ExtUtils::MakeMaker' => 0, |
| 59 |
'Config' => 0, |
| 60 |
'File::Spec' => 0, |
| 61 |
}, |
| 62 |
}, |
| 63 |
runtime => { |
| 64 |
requires => { |
| 65 |
'Exporter' => 5.57, |
| 66 |
'base' => 0, |
| 67 |
'XSLoader' => 0, |
| 68 |
}, |
| 69 |
}, |
| 70 |
test => { |
| 71 |
# 0.98 for done_testing() and subtest(), used by the |
| 72 |
# incremental parser and callback tests |
| 73 |
requires => { |
| 74 |
'Test::More' => 0.98, |
| 75 |
'Data::Dumper' => 0, |
| 76 |
}, |
| 77 |
}, |
| 78 |
# modules required by the author tests in xt/, |
| 79 |
# install with: cpanm --with-develop --installdeps . |
| 80 |
develop => { |
| 81 |
requires => { |
| 82 |
'Test::Pod' => 1.22, |
| 83 |
'Test::Perl::Critic' => 0, |
| 84 |
'Test::CPAN::Changes' => 0, |
| 85 |
}, |
| 86 |
}, |
| 87 |
}, |
| 88 |
}, |
| 89 |
MIN_PERL_VERSION => 5.008008, |
| 90 |
LICENSE => 'perl_5', |
| 91 |
CCFLAGS => $Config{ccflags} . ( $IS_VC ? ' /Fosrc/' : ' -o $@' ), |
| 92 |
OBJECT => q/$(O_FILES)/, |
| 93 |
XS => { 'src/XS.xs' => 'src/XS.c', }, |
| 94 |
# ExtUtils::ParseXS before perl 5.22 chdirs into the directory of the .xs |
| 95 |
# file, so a relative typemap path is looked up in src/ and never found. |
| 96 |
# The file lives in src/ so that MakeMaker does not also add a bare |
| 97 |
# "typemap" argument of its own, which would hit the same problem. |
| 98 |
TYPEMAPS => [ File::Spec->rel2abs(File::Spec->catfile('src', 'typemap')) ], |
| 99 |
C => [ 'src/XS.c', glob('src/*.c') ], |
| 100 |
H => [ glob('src/*.h') ], |
| 101 |
clean => { FILES => 'src/*.o* *.o* src/*.pdb *.pdb' }, |
| 102 |
extra_config(), |
| 103 |
); |
| 104 |
|
| 105 |
my $eumm_version = eval $ExtUtils::MakeMaker::VERSION; |
| 106 |
|
| 107 |
for (qw(configure build test runtime)) { |
| 108 |
my $key = $_ eq 'runtime' ? 'PREREQ_PM' : uc $_.'_REQUIRES'; |
| 109 |
next unless exists $WriteMakefileArgs{META_ADD}{prereqs}{$_} |
| 110 |
or exists $WriteMakefileArgs{$key}; |
| 111 |
my $r = $WriteMakefileArgs{$key} = { |
| 112 |
%{$WriteMakefileArgs{META_ADD}{prereqs}{$_}{requires} || {}}, |
| 113 |
%{delete $WriteMakefileArgs{$key} || {}}, |
| 114 |
}; |
| 115 |
defined $r->{$_} or delete $r->{$_} for keys %$r; |
| 116 |
} |
| 117 |
|
| 118 |
$WriteMakefileArgs{BUILD_REQUIRES} = { |
| 119 |
%{$WriteMakefileArgs{BUILD_REQUIRES} || {}}, |
| 120 |
%{delete $WriteMakefileArgs{TEST_REQUIRES}} |
| 121 |
} if $eumm_version < 6.63_03; |
| 122 |
|
| 123 |
$WriteMakefileArgs{PREREQ_PM} = { |
| 124 |
%{$WriteMakefileArgs{PREREQ_PM}}, |
| 125 |
%{delete $WriteMakefileArgs{BUILD_REQUIRES}} |
| 126 |
} if $eumm_version < 6.55_01; |
| 127 |
|
| 128 |
delete $WriteMakefileArgs{CONFIGURE_REQUIRES} |
| 129 |
if $eumm_version < 6.51_03; |
| 130 |
|
| 131 |
delete @WriteMakefileArgs{qw(META_ADD META_MERGE)} |
| 132 |
if $eumm_version < 6.46; |
| 133 |
|
| 134 |
delete $WriteMakefileArgs{MIN_PERL_VERSION} |
| 135 |
if $eumm_version < 6.48; |
| 136 |
|
| 137 |
delete $WriteMakefileArgs{LICENSE} |
| 138 |
if $eumm_version < 6.31; |
| 139 |
|
| 140 |
WriteMakefile(%WriteMakefileArgs); |
| 141 |
|
| 142 |
sub extra_config { |
| 143 |
my $available_libs = get_available_libs(); |
| 144 |
|
| 145 |
my (%config, %summary_config); |
| 146 |
my @options = qw(lib libpath incpath); |
| 147 |
|
| 148 |
# remove unneded libxml2 |
| 149 |
delete $available_libs->{xml2} unless exists $available_libs->{'XML::LibXML'}; |
| 150 |
|
| 151 |
foreach my $lib_config (values %$available_libs) { |
| 152 |
foreach my $option (@options) { |
| 153 |
my $value = $lib_config->{$option} or next; |
| 154 |
$value = [ split(/\s+/, $value) ] unless ref $value; |
| 155 |
$config{$option}{$_} = 1 for @$value; |
| 156 |
} |
| 157 |
} |
| 158 |
if ( $PARAMS{LIBS} ) { |
| 159 |
$summary_config{LIBS} = [ $PARAMS{LIBS} ]; |
| 160 |
} |
| 161 |
else { |
| 162 |
my @libs = ( |
| 163 |
# order is important! |
| 164 |
( map { "-L$_" } keys %{ $config{libpath} || {} } ), |
| 165 |
( map { |
| 166 |
$IS_VC && $_ !~ /\.lib$/ ? "-l$_.lib" : "-l$_" |
| 167 |
} keys %{ $config{lib} || {} } |
| 168 |
), |
| 169 |
); |
| 170 |
$summary_config{LIBS} = [$Config::Config{ldflags} || '', @libs] |
| 171 |
if scalar @libs; |
| 172 |
} |
| 173 |
|
| 174 |
if ( $PARAMS{INC} ) { |
| 175 |
$summary_config{INC} = [ $PARAMS{INC} ]; |
| 176 |
} |
| 177 |
else { |
| 178 |
my @inc = map { "-I$_" } keys %{ $config{incpath} || {} }; |
| 179 |
$summary_config{INC} = \@inc if scalar @inc; |
| 180 |
} |
| 181 |
|
| 182 |
my @define = map { s/:/_/g; "-DXH_HAVE_" . uc($_) } keys %$available_libs; |
| 183 |
$summary_config{DEFINE} = \@define if scalar @define; |
| 184 |
|
| 185 |
# convert array to string |
| 186 |
$summary_config{$_} = join(' ', @{ $summary_config{$_} }) for keys %summary_config; |
| 187 |
|
| 188 |
# CCFLAGS |
| 189 |
# The debug build is gnu11 rather than c99 because -pedantic is also |
| 190 |
# applied to the perl headers, and config.h of a modern perl uses |
| 191 |
# _Thread_local, which C99 does not have. |
| 192 |
$summary_config{CCFLAGS} = ( $Config{ccflags} || '' ) . ( $IS_VC ? ' /Fosrc/' : ' -o $@' ); |
| 193 |
$summary_config{CCFLAGS} .= ' -g -Wall -Werror -Wextra -pedantic -Wno-clobbered -std=gnu11 -DPERL_GCC_PEDANTIC -DWITH_DEBUG -O0' |
| 194 |
if $PARAMS{DEBUG}; |
| 195 |
$summary_config{CCFLAGS} .= ' -DWITH_TRACE' |
| 196 |
if $PARAMS{DEBUG} && $PARAMS{TRACE}; |
| 197 |
|
| 198 |
# Fix from Glib module. |
| 199 |
# On OpenBSD, any program that directly or indirectly wants to load |
| 200 |
# libpthread.so must do so from the start. But when perl is built without |
| 201 |
# ithreads, it will also most likely not be compiled with "-pthread". When |
| 202 |
# libglib/libgobject then go and try to load libpthread.so, the loader will |
| 203 |
# error out. |
| 204 |
if ($^O =~ /^openbsd|netbsd$/ |
| 205 |
&& $Config::Config{ldflags} !~ m/-pthread\b/ |
| 206 |
&& $Config::Config{ccflags} !~ m/-pthread\b/) { |
| 207 |
warn " ***\n *** on OpenBSD, we either need perl linked with '-pthread',\n", |
| 208 |
" *** or we need to set LD_PRELOAD=libpthread.so; doing the latter now...\n ***\n"; |
| 209 |
$summary_config{macro} = { FULLPERLRUN => 'LD_PRELOAD=/usr/lib/libpthread.so $(FULLPERL)' }; |
| 210 |
} |
| 211 |
|
| 212 |
return %summary_config; |
| 213 |
} |
| 214 |
|
| 215 |
sub get_available_libs { |
| 216 |
my @COMMON_LIB_PATH = ( |
| 217 |
'/usr/lib', |
| 218 |
'/usr/local/lib', |
| 219 |
'/usr/pkg/lib', |
| 220 |
'/opt/local/lib', |
| 221 |
); |
| 222 |
|
| 223 |
my @COMMON_INC_PATH = ( |
| 224 |
'/usr/include', |
| 225 |
'/usr/local/include', |
| 226 |
'/usr/pkg/include', |
| 227 |
'/opt/local/include', |
| 228 |
); |
| 229 |
|
| 230 |
my @REQUIRED_LIBS = ( |
| 231 |
{ |
| 232 |
perl => 1, |
| 233 |
name => 'XML::LibXML', |
| 234 |
version => 1.62, |
| 235 |
mandatory => 0, |
| 236 |
}, |
| 237 |
{ |
| 238 |
name => 'xml2', |
| 239 |
mandatory => 0, |
| 240 |
configs => [ |
| 241 |
{ |
| 242 |
lib => sub { |
| 243 |
my $results = $PARAMS{WIN32} ? undef : backtick( |
| 244 |
join('/bin/', grep { $_ } ($ENV{XMLPREFIX} || $PARAMS{XMLPREFIX}, 'xml2-config')), |
| 245 |
'--libs', |
| 246 |
); |
| 247 |
$results ? [ map { substr($_, 2) } grep { $_ =~ /^-l/ } split(/\s+/, $results) ] : undef; |
| 248 |
}, |
| 249 |
libpath => sub { |
| 250 |
my $results = $PARAMS{WIN32} ? undef : backtick( |
| 251 |
join('/bin/', grep { $_ } ($ENV{XMLPREFIX} || $PARAMS{XMLPREFIX}, 'xml2-config')), |
| 252 |
'--libs', |
| 253 |
); |
| 254 |
$results ? [ map { substr($_, 2) } grep { $_ =~ /^-L/ } split(/\s+/, $results) ] : undef; |
| 255 |
}, |
| 256 |
incpath => sub { |
| 257 |
my $results = $PARAMS{WIN32} ? undef : backtick( |
| 258 |
join('/bin/', grep { $_ } ($ENV{XMLPREFIX} || $PARAMS{XMLPREFIX}, 'xml2-config')), |
| 259 |
'--cflags', |
| 260 |
); |
| 261 |
$results ? [ map { substr($_, 2) } grep { $_ =~ /^-I/ } split(/\s+/, $results) ] : undef; |
| 262 |
}, |
| 263 |
header => 'libxml/parser.h', |
| 264 |
}, |
| 265 |
{ |
| 266 |
lib => 'xml2 m z', |
| 267 |
libpath => \@COMMON_LIB_PATH, |
| 268 |
incpath => [ |
| 269 |
@COMMON_INC_PATH, |
| 270 |
'/usr/include/libxml2', |
| 271 |
'/usr/local/include/libxml2', |
| 272 |
'/usr/pkg/include/libxml2', |
| 273 |
'/opt/local/include/libxml2', |
| 274 |
], |
| 275 |
header => 'libxml/parser.h', |
| 276 |
}, |
| 277 |
], |
| 278 |
}, |
| 279 |
{ |
| 280 |
name => 'iconv', |
| 281 |
configs => [ |
| 282 |
{ |
| 283 |
lib => 'c', |
| 284 |
header => 'iconv.h', |
| 285 |
function => 'iconv_t iconv = iconv_open("UTF-8", "UTF-8");(void) iconv_close(iconv);return 0;', |
| 286 |
}, |
| 287 |
{ |
| 288 |
lib => 'iconv', |
| 289 |
header => 'iconv.h', |
| 290 |
function => 'iconv_t iconv = iconv_open("UTF-8", "UTF-8");(void) iconv_close(iconv);return 0;', |
| 291 |
}, |
| 292 |
], |
| 293 |
}, |
| 294 |
{ |
| 295 |
name => 'icu', |
| 296 |
configs => [ |
| 297 |
{ |
| 298 |
lib => sub { |
| 299 |
my $results = $PARAMS{WIN32} ? undef : backtick('icu-config', '--ldflags-libsonly'); |
| 300 |
$results ? [ map { substr($_, 2) } grep { $_ =~ /^-l/ } split(/\s+/, $results) ] : undef; |
| 301 |
}, |
| 302 |
libpath => sub { |
| 303 |
my $results = $PARAMS{WIN32} ? undef : backtick('icu-config', '--ldflags-searchpath'); |
| 304 |
$results ? [ map { substr($_, 2) } grep { $_ =~ /^-L/ } split(/\s+/, $results) ] : undef; |
| 305 |
}, |
| 306 |
incpath => sub { |
| 307 |
my $results = $PARAMS{WIN32} ? undef : backtick('icu-config', '--cppflags-searchpath'); |
| 308 |
$results ? [ map { substr($_, 2) } grep { $_ =~ /^-I/ } split(/\s+/, $results) ] : undef; |
| 309 |
}, |
| 310 |
header => [ |
| 311 |
'unicode/utypes.h', |
| 312 |
'unicode/ucnv.h', |
| 313 |
], |
| 314 |
function => 'UErrorCode status = U_ZERO_ERROR;UConverter *uconv = ucnv_open("UTF-8", &status);ucnv_close(uconv);return 0;', |
| 315 |
}, |
| 316 |
{ |
| 317 |
lib => 'icui18n icuuc icudata', |
| 318 |
libpath => \@COMMON_LIB_PATH, |
| 319 |
incpath => \@COMMON_INC_PATH, |
| 320 |
header => [ |
| 321 |
'unicode/utypes.h', |
| 322 |
'unicode/ucnv.h', |
| 323 |
], |
| 324 |
function => 'UErrorCode status = U_ZERO_ERROR;UConverter *uconv = ucnv_open("UTF-8", &status);ucnv_close(uconv);return 0;', |
| 325 |
}, |
| 326 |
], |
| 327 |
}, |
| 328 |
); |
| 329 |
|
| 330 |
my %libs; |
| 331 |
my @options = qw(lib libpath incpath header function); |
| 332 |
foreach my $info (@REQUIRED_LIBS) { |
| 333 |
my $lib = $info->{name}; |
| 334 |
|
| 335 |
if ( $info->{perl} ) { |
| 336 |
my $ver = $info->{version} || ''; |
| 337 |
print "Checking for '$lib'... "; |
| 338 |
eval "use $lib $ver"; |
| 339 |
if (my $err = $@) { |
| 340 |
print "no\n"; |
| 341 |
print $err if $PARAMS{DEBUG} |
| 342 |
} |
| 343 |
else { |
| 344 |
print "yes\n"; |
| 345 |
$libs{$lib} = {}; |
| 346 |
} |
| 347 |
} |
| 348 |
else { |
| 349 |
foreach my $lib_config (@{ $info->{configs} }) { |
| 350 |
my %config; |
| 351 |
|
| 352 |
@config{@options} = map { |
| 353 |
my $results = $lib_config->{$_}; |
| 354 |
$results = ($results and ref($results) eq 'CODE') ? &$results() : $results; |
| 355 |
($results and $_ ne 'function' and !ref($results)) |
| 356 |
? [ split(/\s+/, $results) ] |
| 357 |
: $results |
| 358 |
; |
| 359 |
} @options; |
| 360 |
|
| 361 |
next unless $config{lib} || $config{perl_lib}; |
| 362 |
|
| 363 |
print "Checking for '$lib'... "; |
| 364 |
|
| 365 |
delete $config{$_} for grep { !$config{$_} } keys %config; |
| 366 |
if ( $PARAMS{LIBS} ) { |
| 367 |
delete $config{lib}; |
| 368 |
delete $config{libpath}; |
| 369 |
$config{LIBS} = $PARAMS{LIBS}; |
| 370 |
} |
| 371 |
if ( $PARAMS{INC} ) { |
| 372 |
delete $config{incpath}; |
| 373 |
$config{INC} = $PARAMS{INC}; |
| 374 |
} |
| 375 |
|
| 376 |
if ( check_lib(debug => $PARAMS{DEBUG}, %config) ) { |
| 377 |
print "yes\n"; |
| 378 |
$libs{$lib} = \%config; |
| 379 |
last; |
| 380 |
} |
| 381 |
|
| 382 |
print "no\n"; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
if ($info->{mandatory} && !$libs{$lib}) { |
| 387 |
print STDERR <<"DEATH"; |
| 388 |
'$lib' library not found |
| 389 |
Try setting LIBS and INC values on the command line |
| 390 |
|
| 391 |
Also, you may try to run perl Makefile.PL with the DEBUG=1 parameter |
| 392 |
to see the exact reason why the detection of '$lib' installation |
| 393 |
failed or why Makefile.PL was not able to compile a test program. |
| 394 |
DEATH |
| 395 |
exit 1; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
return \%libs; |
| 400 |
} |
| 401 |
|
| 402 |
sub parse_params { |
| 403 |
my %params; |
| 404 |
@params{qw(DEBUG TRACE INC LIBS XMLPREFIX)} = (); |
| 405 |
|
| 406 |
@ARGV = grep { |
| 407 |
my ($key, $val) = split(/=/, $_, 2); |
| 408 |
if (exists $params{$key}) { |
| 409 |
$params{$key} = $val; 0 |
| 410 |
} else { 1 } |
| 411 |
} @ARGV; |
| 412 |
|
| 413 |
$params{WIN32} = ($^O =~ /Win32/); |
| 414 |
if ( $params{WIN32} ) { |
| 415 |
$params{DEVNULL} = 'DEVNULL'; |
| 416 |
} |
| 417 |
else { |
| 418 |
$params{DEVNULL} = eval { File::Spec->devnull }; |
| 419 |
if ($@) { $params{DEVNULL} = '/dev/null' } |
| 420 |
} |
| 421 |
|
| 422 |
return %params; |
| 423 |
} |
| 424 |
|
| 425 |
sub backtick { |
| 426 |
my $command = join(' ', @_); |
| 427 |
if ($PARAMS{DEBUG}) { |
| 428 |
print "Cmd: $command \n"; |
| 429 |
my $results = `$command`; |
| 430 |
if ($? != 0) { |
| 431 |
warn "Backticks call to '$command' failed"; |
| 432 |
return; |
| 433 |
} |
| 434 |
chomp $results; |
| 435 |
return $results; |
| 436 |
} |
| 437 |
|
| 438 |
local *OLDOUT; |
| 439 |
local *OLDERR; |
| 440 |
|
| 441 |
open(OLDOUT, ">&STDOUT"); |
| 442 |
open(OLDERR, ">&STDERR"); |
| 443 |
open(STDOUT, ">$PARAMS{DEVNULL}"); |
| 444 |
open(STDERR, ">$PARAMS{DEVNULL}"); |
| 445 |
my $results = `$command`; |
| 446 |
my $retval = $?; |
| 447 |
open(STDOUT, ">&OLDOUT"); |
| 448 |
open(STDERR, ">&OLDERR"); |
| 449 |
if ($retval != 0) { |
| 450 |
if ($PARAMS{DEBUG}) { |
| 451 |
warn "backticks call to '$command' failed"; |
| 452 |
} |
| 453 |
return; |
| 454 |
} |
| 455 |
chomp $results; |
| 456 |
return $results; |
| 457 |
} |
| 458 |
|
| 459 |
package MY; |
| 460 |
|
| 461 |
# Fix bug occurs while using multiple job compile: |
| 462 |
# gcc: error: xh_buffer.c: No such file or directory |
| 463 |
# Replace $* to $< |
| 464 |
# $*: dir/file.o -> file.c |
| 465 |
# $<: dir/file.o -> dir/file.c |
| 466 |
sub c_o { |
| 467 |
my $inherited = shift->SUPER::c_o(@_); |
| 468 |
$inherited =~ s/\$\*.*/\$</g; |
| 469 |
$inherited; |
| 470 |
} |