| 1 |
#!env perl |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# Permission is hereby granted, free of charge, to any person obtaining a |
| 4 |
# copy of this software and associated documentation files (the "Software"), |
| 5 |
# to deal in the Software without restriction, including without limitation |
| 6 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 |
# and/or sell copies of the Software, and to permit persons to whom the |
| 8 |
# Software is furnished to do so, subject to the following conditions: |
| 9 |
# |
| 10 |
# The above copyright notice and this permission notice shall be included |
| 11 |
# in all copies or substantial portions of the Software. |
| 12 |
# |
| 13 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 17 |
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 18 |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 19 |
# OTHER DEALINGS IN THE SOFTWARE. |
| 20 |
|
| 21 |
# This script will generate config.h |
| 22 |
# |
| 23 |
# It is assumed that current working directory is a build directory, i.e. |
| 24 |
# the target directory specified on the command-line when you |
| 25 |
# execute: cp_libmarpa.sh target_directory |
| 26 |
# |
| 27 |
use feature 'say'; |
| 28 |
use English qw/-no_match_vars/; |
| 29 |
use Config; |
| 30 |
use File::Slurp qw/read_file/; |
| 31 |
use IPC::Cmd qw/run/; |
| 32 |
use Module::Load qw/load/; |
| 33 |
use POSIX qw/EXIT_SUCCESS EXIT_FAILURE/; |
| 34 |
use Getopt::Long; |
| 35 |
|
| 36 |
our %PERL_AUTOCONF_OS = map { $_ => 1 } qw( MSWin32 openbsd solaris sunos midnightbsd ); |
| 37 |
|
| 38 |
my $MARPA_DEBUG = $ENV{MARPA_DEBUG} || 0; |
| 39 |
my $USE_PERL_AUTOCONF = $ENV{MARPA_USE_PERL_AUTOCONF} || ( $PERL_AUTOCONF_OS{$^O} // 0 ); |
| 40 |
my $CC = $ENV{CC} || $Config{cc} || 'cc'; |
| 41 |
my $CCFLAGS = $ENV{CCFLAGS} || $Config{ccflags} || ''; |
| 42 |
my $SH = $ENV{SH} || $Config{sh} || ''; |
| 43 |
my $OBJ_EXT = $ENV{OBJ_EXT} || $Config{obj_ext} || '.o'; |
| 44 |
|
| 45 |
GetOptions ('marpa_debug!' => \$MARPA_DEBUG, |
| 46 |
'use_perl_autoconf!' => \$USE_PERL_AUTOCONF, |
| 47 |
'cc=s' => \$CC, |
| 48 |
'ccflags=s' => \$CCFLAGS, |
| 49 |
'sh=s' => \$SH, |
| 50 |
'obj_ext=s' => \$OBJ_EXT, |
| 51 |
'help!' => \$help) || |
| 52 |
die "Error in command-line arguments"; |
| 53 |
|
| 54 |
if ($help) { |
| 55 |
usage(); |
| 56 |
exit(EXIT_SUCCESS); |
| 57 |
} |
| 58 |
|
| 59 |
if ($USE_PERL_AUTOCONF) { |
| 60 |
load Config::AutoConf || die "Please install Config::AutoConf module"; |
| 61 |
} |
| 62 |
|
| 63 |
my $rc = do_config_h(); |
| 64 |
|
| 65 |
exit($rc ? EXIT_SUCCESS : EXIT_FAILURE); |
| 66 |
|
| 67 |
sub do_config_h { |
| 68 |
|
| 69 |
$ENV{CC} = $CC; |
| 70 |
$ENV{CCFLAGS} = $CCFLAGS; |
| 71 |
$ENV{OBJ_EXT} = $OBJ_EXT; |
| 72 |
|
| 73 |
# If current directory exists and contains a stamp file more recent than an eventual config.h |
| 74 |
# we are done. |
| 75 |
if (-e 'config.h' && -e 'stamp-h1' && up_to_date( 'config.h', 'stamp-h1' )) { |
| 76 |
printf "%s is up-to-date v.s. %s. Remove one of them to force a new %s generation\n", 'config.h', 'stamp-h1', 'config.h'; |
| 77 |
return 1; |
| 78 |
} |
| 79 |
|
| 80 |
unlink('config.h'); |
| 81 |
unlink('stamp-h1'); |
| 82 |
|
| 83 |
# Otherwise, redo config.h |
| 84 |
if (! $USE_PERL_AUTOCONF) { |
| 85 |
|
| 86 |
# This is only necessary for GNU autoconf, which is aggressive |
| 87 |
# about looking for things to update |
| 88 |
|
| 89 |
# Some files should NEVER be updated in this directory, by |
| 90 |
# make or anything else. If for some reason they are |
| 91 |
# out of date, stamp them up to date |
| 92 |
|
| 93 |
my @m4_files = glob('m4/*.m4'); |
| 94 |
my $configure_script = 'configure'; |
| 95 |
|
| 96 |
if (not up_to_date( [ 'configure.ac', @m4_files ], 'aclocal.m4' ) ) |
| 97 |
{ |
| 98 |
utime time(), time(), 'aclocal.m4'; |
| 99 |
} |
| 100 |
if (not up_to_date( |
| 101 |
[ 'configure.ac', 'Makefile.am', 'aclocal.m4' ], |
| 102 |
'Makefile.in' |
| 103 |
) |
| 104 |
) |
| 105 |
{ |
| 106 |
utime time(), time(), 'Makefile.in'; |
| 107 |
} ## end if ( not up_to_date( [ 'configure.ac', 'Makefile.am'...])) |
| 108 |
if (not up_to_date( |
| 109 |
[ 'configure.ac', 'aclocal.m4' ], |
| 110 |
[ $configure_script, 'config.h.in' ] |
| 111 |
) |
| 112 |
) |
| 113 |
{ |
| 114 |
utime time(), time(), $configure_script; |
| 115 |
utime time(), time(), 'config.h.in'; |
| 116 |
} ## end if ( not up_to_date( [ 'configure.ac', 'aclocal.m4'...])) |
| 117 |
|
| 118 |
say join q{ }, "Doing config.h with configure" |
| 119 |
or die "print failed: $ERRNO"; |
| 120 |
my $shell = $SH; |
| 121 |
|
| 122 |
##no critic(ValuesAndExpressions::RequireInterpolationOfMetachars) |
| 123 |
$shell or die q{No Bourne shell available says $SH}; |
| 124 |
##use critic |
| 125 |
} |
| 126 |
|
| 127 |
my $original_cflags = $ENV{CFLAGS}; |
| 128 |
local $ENV{CFLAGS}; |
| 129 |
$ENV{CFLAGS} = $original_cflags if defined $original_cflags; |
| 130 |
|
| 131 |
# We need PIC, but do not want the overhead of building the shared library |
| 132 |
my @configure_command_args = qw(--with-pic --disable-shared); |
| 133 |
|
| 134 |
my @debug_flags = (); |
| 135 |
if ( $MARPA_DEBUG ) { |
| 136 |
if ( defined $ENV{LIBMARPA_CFLAGS} ) { |
| 137 |
$ENV{CFLAGS} = $ENV{LIBMARPA_CFLAGS}; |
| 138 |
} |
| 139 |
push @debug_flags, '-DMARPA_DEBUG=1'; |
| 140 |
push @debug_flags, '-fno-inline', '-Wno-inline' if ($CC eq 'gcc'); |
| 141 |
push @configure_command_args, |
| 142 |
'MARPA_DEBUG_FLAG=' . ( join q{ }, @debug_flags ); |
| 143 |
} ## end if ( $MARPA_DEBUG ) |
| 144 |
|
| 145 |
if ($USE_PERL_AUTOCONF) { |
| 146 |
|
| 147 |
my $libmarpa_version = read_file('LIB_VERSION'); |
| 148 |
chomp $libmarpa_version; |
| 149 |
my @libmarpa_version = split /[.]/xms, $libmarpa_version; |
| 150 |
|
| 151 |
# |
| 152 |
## C.f. http://fr.slideshare.net/hashashin/building-c-and-c-libraries-with-perl |
| 153 |
# |
| 154 |
my @c = qw/marpa_ami.c marpa_avl.c marpa.c |
| 155 |
marpa_codes.c marpa_obs.c marpa_tavl.c/; |
| 156 |
if (! -r 'config.h') { |
| 157 |
# |
| 158 |
## Because Config::AutoConf can only generate #define/#undef |
| 159 |
## stubs, we write our config.h with these stubs, our config.h |
| 160 |
## will then include a generated config_from_autoconf.h |
| 161 |
# |
| 162 |
say join q{ }, "Doing config.h with Config::AutoConf" |
| 163 |
or die "print failed: $ERRNO"; |
| 164 |
open my $config_fh, '>>', 'config.h' || die "Cannot open config.h, $!\n"; |
| 165 |
my $ac = Config::AutoConf->new(); |
| 166 |
my $inline_ok = 0; |
| 167 |
{ |
| 168 |
$ac->msg_checking('inline'); |
| 169 |
my $program = $ac->lang_build_program("static inline int testinline() {return 1;}\n", 'testinline'); |
| 170 |
$inline_ok = $ac->compile_if_else($program); |
| 171 |
$ac->msg_result($inline_ok ? 'yes' : 'no'); |
| 172 |
} |
| 173 |
my $inline = ''; |
| 174 |
if (! $inline_ok) { |
| 175 |
foreach (qw/__inline__ __inline/) { |
| 176 |
my $candidate = $_; |
| 177 |
$ac->msg_checking($candidate); |
| 178 |
my $program = $ac->lang_build_program("static $candidate int testinline() {return 1;}\n", 'testinline'); |
| 179 |
my $rc = $ac->compile_if_else($program); |
| 180 |
$ac->msg_result($rc ? 'yes' : 'no'); |
| 181 |
if ($rc) { |
| 182 |
$inline = $candidate; |
| 183 |
last; |
| 184 |
} |
| 185 |
} |
| 186 |
} else { |
| 187 |
$inline = 'inline'; |
| 188 |
} |
| 189 |
if ($inline) { |
| 190 |
print {$config_fh} <<INLINEHOOK; |
| 191 |
#ifndef __CONFIG_WITH_STUBS_H |
| 192 |
#ifndef __cplusplus |
| 193 |
#define inline $inline |
| 194 |
#endif |
| 195 |
#include "config_from_autoconf.h" |
| 196 |
#endif /* __CONFIG_WITH_STUBS_H */ |
| 197 |
INLINEHOOK |
| 198 |
} else { |
| 199 |
print {$config_fh} <<INLINEHOOK; |
| 200 |
#ifndef __CONFIG_WITH_STUBS_H |
| 201 |
#ifndef __cplusplus |
| 202 |
/* #undef inline */ |
| 203 |
#endif |
| 204 |
#include "config_from_autoconf.h" |
| 205 |
#endif /* __CONFIG_WITH_STUBS_H */ |
| 206 |
INLINEHOOK |
| 207 |
} |
| 208 |
|
| 209 |
# Config::Autoconf mistakes 0 for undef, so these must be done explicitly |
| 210 |
say {$config_fh} join q{ }, '#define MARPA_LIB_MAJOR_VERSION', $libmarpa_version[0]; |
| 211 |
say {$config_fh} join q{ }, '#define MARPA_LIB_MINOR_VERSION', $libmarpa_version[1]; |
| 212 |
say {$config_fh} join q{ }, '#define MARPA_LIB_MICRO_VERSION', $libmarpa_version[2]; |
| 213 |
|
| 214 |
close($config_fh); |
| 215 |
$ac = Config::AutoConf->new(); |
| 216 |
my $sizeof_int = $ac->check_sizeof_type('int'); |
| 217 |
if ($sizeof_int < 4) { |
| 218 |
die "Marpa requires that int be at least 32 bits -- on this system that is not the case"; |
| 219 |
} |
| 220 |
|
| 221 |
$ac->check_stdc_headers; |
| 222 |
$ac->check_default_headers(); |
| 223 |
|
| 224 |
# This check was to prepare for Perl's 64-bit pseudo-UTF8 characters inside the SLIF. |
| 225 |
# It needs to be rethought. |
| 226 |
# |
| 227 |
# if (!$ac->check_type('unsigned long long int')) { |
| 228 |
# die "Marpa requires that unsigned long long int is supported by your compiler"; |
| 229 |
# } |
| 230 |
|
| 231 |
$ac->define_var('PACKAGE', "\"libmarpa\""); |
| 232 |
$ac->define_var('PACKAGE_BUGREPORT', "\"http://rt.cpan.org/NoAuth/Bugs.html?Dist=Marpa\""); |
| 233 |
$ac->define_var('PACKAGE_NAME', "\"libmarpa\""); |
| 234 |
$ac->define_var('PACKAGE_STRING', "\"libmarpa $libmarpa_version[0].$libmarpa_version[1].$libmarpa_version[2]\""); |
| 235 |
$ac->define_var('PACKAGE_TARNAME', "\"libmarpa\""); |
| 236 |
$ac->define_var('PACKAGE_URL', "\"\""); |
| 237 |
$ac->define_var('PACKAGE_VERSION', "\"$libmarpa_version\""); |
| 238 |
$ac->define_var('PACKAGE_STRING', "\"$libmarpa_version\""); |
| 239 |
$ac->write_config_h('config_from_autoconf.h'); |
| 240 |
# |
| 241 |
# Touch $dist_stamp_file |
| 242 |
# |
| 243 |
if (! open(my $stamp, '>', 'stamp-h1')) { |
| 244 |
warn "Cannot update $dist_stamp_file"; |
| 245 |
} else { |
| 246 |
close($stamp); |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
my @o = map {s/\.c$/$OBJ_EXT/; $_} @c; |
| 252 |
if (! -r 'Makefile.PL') { |
| 253 |
open my $makefile_pl_fh, '>', 'Makefile.PL'; |
| 254 |
my $CCFLAGS = @debug_flags ? "$CCFLAGS} @debug_flags" : ''; |
| 255 |
print {$makefile_pl_fh} " |
| 256 |
use ExtUtils::MakeMaker; |
| 257 |
WriteMakefile(VERSION => \"$libmarpa_version\", |
| 258 |
XS_VERSION => \"$libmarpa_version\", |
| 259 |
NAME => 'libmarpa', |
| 260 |
OBJECT => '@o', |
| 261 |
CCFLAGS => '$CCFLAGS', |
| 262 |
LINKTYPE => 'static'); |
| 263 |
"; |
| 264 |
close $makefile_pl_fh; |
| 265 |
die 'Making Makefile: perl Failure' |
| 266 |
if not run( command => [$^X, 'Makefile.PL'], verbose => 1 ); |
| 267 |
} |
| 268 |
} else { |
| 269 |
my $shell = $SH; |
| 270 |
my $configure_script = 'configure'; |
| 271 |
say join q{ }, "Running command:", $shell, $configure_script, |
| 272 |
@configure_command_args |
| 273 |
or die "print failed: $ERRNO"; |
| 274 |
if (not run( |
| 275 |
command => [ $shell, $configure_script, @configure_command_args ], |
| 276 |
verbose => 1 |
| 277 |
) |
| 278 |
) |
| 279 |
{ |
| 280 |
say {*STDERR} "Failed: $configure_script" |
| 281 |
or die "say failed: $ERRNO"; |
| 282 |
say {*STDERR} "Current directory: $build_dir" |
| 283 |
or die "say failed: $ERRNO"; |
| 284 |
die 'Cannot run libmarpa configure'; |
| 285 |
} ## end if ( not run( command => [ $shell, $configure_script...])) |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
return 1; |
| 290 |
|
| 291 |
} ## end sub do_libmarpa_prepare_build |
| 292 |
|
| 293 |
# |
| 294 |
# Copy/little adaptation of /usr/share/perl5/Module/Build/Base.pm:up_to_date() |
| 295 |
# |
| 296 |
sub up_to_date { |
| 297 |
my ($source, $derived) = @_; |
| 298 |
$source = [$source] unless ref $source; |
| 299 |
$derived = [$derived] unless ref $derived; |
| 300 |
|
| 301 |
# empty $derived means $source should always run |
| 302 |
return 0 if @$source && !@$derived || grep {not -e} @$derived; |
| 303 |
|
| 304 |
my $most_recent_source = time / (24*60*60); |
| 305 |
foreach my $file (@$source) { |
| 306 |
unless (-e $file) { |
| 307 |
warn "Can't find source file $file for up-to-date check"; |
| 308 |
next; |
| 309 |
} |
| 310 |
$most_recent_source = -M _ if -M _ < $most_recent_source; |
| 311 |
} |
| 312 |
|
| 313 |
foreach my $derived (@$derived) { |
| 314 |
return 0 if -M $derived > $most_recent_source; |
| 315 |
} |
| 316 |
return 1; |
| 317 |
} |
| 318 |
|
| 319 |
sub usage { |
| 320 |
print STDERR <<USAGE; |
| 321 |
|
| 322 |
$^X $0 [options] where options can be: |
| 323 |
|
| 324 |
--marpa_debug Boolean. Default value: $MARPA_DEBUG. |
| 325 |
--use_perl_autoconf Boolean. Default value: $USE_PERL_AUTOCONF |
| 326 |
--cc=... String. Default value: $CC |
| 327 |
--ccflags=... String. Default value: $CCFLAGS |
| 328 |
--sh=...s String. Default value: $SH |
| 329 |
--obj_ext=... String. Default value: $OBJ_EXT |
| 330 |
--help Boolean. This help -; |
| 331 |
|
| 332 |
USAGE |
| 333 |
} |