| 1 | package Test::Smoke::App::ConfigSmoke::SmokeEnv; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | our $VERSION = '0.002'; | ||
| 6 | |||
| 7 | use Exporter 'import'; | ||
| 8 | our @EXPORT = qw/ | ||
| 9 | config_smoke_env | ||
| 10 | check_build_configs_file | ||
| 11 | report_build_configs | ||
| 12 | /; | ||
| 13 | |||
| 14 | use Test::Smoke::App::AppOption; | ||
| 15 | use Test::Smoke::App::Options; | ||
| 16 | use Test::Smoke::BuildCFG; | ||
| 17 | use Test::Smoke::Util qw/ skip_config /; | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Test::Smoke::App::ConfigSmoke::SmokeEnv - Mixin for Test::Smoke::App::ConfigSmoke. | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | These methods will be added to the L<Test::Smoke::App::ConfigSmoke> class. | ||
| 26 | |||
| 27 | =head2 config_smoke_env | ||
| 28 | |||
| 29 | Configure options: C<v>, C<smartsmoke>, C<killtime>, C<renice>, C<umask> and C<cfg> | ||
| 30 | |||
| 31 | Also C<perl5lib> and C<perl5opt> | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub config_smoke_env { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | print "\n-- Environment section --\n"; | ||
| 39 | my @options = qw/ v smartsmoke killtime /; | ||
| 40 | for my $opt (@options) { | ||
| 41 | my $option = Test::Smoke::App::Options->$opt; | ||
| 42 | $self->handle_option($option); | ||
| 43 | } | ||
| 44 | |||
| 45 | unless ($^O =~ m/^(?: mswin32 | vms )$/xi) { | ||
| 46 | $self->handle_option(renice_option()); | ||
| 47 | $self->handle_option(umask_option()); | ||
| 48 | } | ||
| 49 | for my $env (qw/ PERL5LIB PERL5OPT /) { | ||
| 50 | if (my $p5env = $ENV{$env}) { | ||
| 51 | $self->handle_option(option_from_env($env)); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | $self->handle_option(Test::Smoke::App::Options->cfg); | ||
| 56 | $self->check_build_configs_file(); | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 check_build_configs_file | ||
| 60 | |||
| 61 | This check, creates and checks a build-configs-file. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub check_build_configs_file { | ||
| 66 | my $self = shift; | ||
| 67 | |||
| 68 | my $cfg; | ||
| 69 | my $buildcfg_file = $self->current_values->{cfg}; | ||
| 70 | if (-f $buildcfg_file) { | ||
| 71 | $cfg = Test::Smoke::BuildCFG->new($buildcfg_file); | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | my $dft_config = Test::Smoke::BuildCFG->os_default_buildcfg($^O); | ||
| 75 | $cfg = Test::Smoke::BuildCFG->new(\$dft_config); | ||
| 76 | } | ||
| 77 | |||
| 78 | my @current_config = split(m{\n}, $cfg->source); | ||
| 79 | my $old_config = -f $buildcfg_file | ||
| 80 | ? join("", grep { ! m/^#/ } @current_config) | ||
| 81 | : ""; | ||
| 82 | |||
| 83 | my @must_option = ( ); | ||
| 84 | my @no_option = ( '-Uuseperlio' ); | ||
| 85 | GIVEN: { | ||
| 86 | local $_ = lc($self->sysinfo->_osname); | ||
| 87 | |||
| 88 | my $os_major = $self->sysinfo->_osvers =~ m/(\d+)/ ? $1 : 0; | ||
| 89 | |||
| 90 | /darwin/ && $os_major >= 8 and do { | ||
| 91 | push @no_option, qw( -Duselongdouble -Dusemorebits ); | ||
| 92 | last GIVEN; | ||
| 93 | }; | ||
| 94 | |||
| 95 | /darwin|bsd/i && do { | ||
| 96 | push @no_option, qw( -Duselongdouble -Dusemorebits -Duse64bitall ); | ||
| 97 | }; | ||
| 98 | |||
| 99 | /linux/i && do { | ||
| 100 | push @no_option, qw( -Duse64bitall ); | ||
| 101 | }; | ||
| 102 | |||
| 103 | /mswin32/i && do { | ||
| 104 | my %compilers = get_avail_w32compilers(); | ||
| 105 | my $cc_info = $compilers{ $self->current_values->{w32cc} } or last GIVEN; | ||
| 106 | # Add -DCCHOME=C:\Strawberry\c for strawberryperl-gcc | ||
| 107 | if ($cc_info->{ccversarg} =~ /strawberryperl/i) { | ||
| 108 | (my $cchome = $cc_info->{ccbin}) =~ s{.bin.gcc\.exe$}{}i; | ||
| 109 | push @must_option, "-DCCHOME=$cchome"; | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | /cygwin/i && do { | ||
| 114 | push @no_option, qw( -Duse64bitall -Duselongdouble -Dusemorebits ); | ||
| 115 | }; | ||
| 116 | } | ||
| 117 | |||
| 118 | for my $option ( @no_option ) { | ||
| 119 | !m/^#/ && m/\Q$option\E/ && s/^/#/ for @current_config; | ||
| 120 | } | ||
| 121 | for my $option ( @must_option ) { | ||
| 122 | unshift @current_config, ("==\n", "$option\n", "==\n") | ||
| 123 | unless grep { /^\Q$option\E/ } @current_config; | ||
| 124 | } | ||
| 125 | |||
| 126 | my $new_config = join("", grep { ! m/^#/ } @current_config); | ||
| 127 | return if $old_config eq $new_config and -f $buildcfg_file; | ||
| 128 | |||
| 129 | my $display = join("", map { chomp; "\t$_\n" } grep { ! m/^#/ } @current_config); | ||
| 130 | |||
| 131 | print "We removed/added some Configure options.\n"; | ||
| 132 | print <<"EOT"; | ||
| 133 | Some options that do not apply to your platform were found. | ||
| 134 | (Comment-lines left out below, but will be written to disk.) | ||
| 135 | $display | ||
| 136 | EOT | ||
| 137 | printf "\n [%s]\n", $buildcfg_file; | ||
| 138 | my $save_file = $self->handle_option(save_buildcfg_file_option()); | ||
| 139 | delete($self->current_values->{save_buildcfg}); | ||
| 140 | |||
| 141 | if (! $save_file) { | ||
| 142 | print "!!!!!\nContents not saved.\n!!!!!\n"; | ||
| 143 | print "Please, fix yourself.\n"; | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | BACKUP_FILE: if (-f $buildcfg_file) { | ||
| 147 | my $backup_file = "${buildcfg_file}.bak"; | ||
| 148 | if (-f $backup_file) { | ||
| 149 | 1 while unlink $backup_file; | ||
| 150 | } | ||
| 151 | rename($buildcfg_file, $backup_file) | ||
| 152 | or do { | ||
| 153 | print "!!!!!\nProblem: cannot move to '$backup_file': $!\n!!!!!\n"; | ||
| 154 | print "Original contents will be lost.\n"; | ||
| 155 | last BACKUP_FILE; | ||
| 156 | }; | ||
| 157 | print " >> Created '$backup_file'\n"; | ||
| 158 | } | ||
| 159 | SAVE_FILE: if ( open(my $fh, '>', $buildcfg_file) ) { | ||
| 160 | print {$fh} $_ for map { chomp; "$_\n" } @current_config; | ||
| 161 | close($fh) or do{ | ||
| 162 | print "!!!!!\nProblem: cannot close($buildcfg_file): $!\n!!!!!\n"; | ||
| 163 | print "Content might be lost.\n"; | ||
| 164 | last SAVE_FILE; | ||
| 165 | }; | ||
| 166 | print " >> Created '$buildcfg_file'\n"; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | print "!!!!!\nProblem: cannot create($buildcfg_file): $!\n!!!!!\n"; | ||
| 170 | print "Please, fix this yourself.\n"; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | =head2 report_build_configs | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub report_build_configs { | ||
| 179 | my $self = shift; | ||
| 180 | my $bcfg = Test::Smoke::BuildCFG->new($self->current_values->{cfg}); | ||
| 181 | |||
| 182 | my $report = ''; | ||
| 183 | my( $skips, $smokes ) = ( 0, 0 ); | ||
| 184 | for my $cfg ( $bcfg->configurations ) { | ||
| 185 | if ( skip_config( $cfg ) ) { | ||
| 186 | $report .= " skip: '$cfg'\n"; | ||
| 187 | $skips++; | ||
| 188 | } else { | ||
| 189 | $report .= "smoke: '$cfg'\n"; | ||
| 190 | $smokes++; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | my $total = $skips + $smokes; | ||
| 194 | $report .= "Smoke $smokes; skip $skips (total $total)\n"; | ||
| 195 | return $report; | ||
| 196 | } | ||
| 197 | |||
| 198 | =head2 renice_option | ||
| 199 | |||
| 200 | This option C<renice> is in the config-file, but only needed to write the | ||
| 201 | shell-script that runs the smoke. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub renice_option { | ||
| 206 | return Test::Smoke::App::AppOption->new( | ||
| 207 | name => 'renice', | ||
| 208 | allow => [0 .. 20], | ||
| 209 | default => 0, | ||
| 210 | helptext => "Unix way to set priority on processes", | ||
| 211 | configtext => "With which value should 'renice' be run?\n" | ||
| 212 | . "(leave '0' for no renice)", | ||
| 213 | configalt => sub { [0 .. 20] }, | ||
| 214 | configdft => sub { 0 }, | ||
| 215 | ); | ||
| 216 | } | ||
| 217 | |||
| 218 | =head2 umask_option | ||
| 219 | |||
| 220 | This option C<umask> is in the config-file, but is only needed to write the | ||
| 221 | shell-script that runs the smoke. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub umask_option { | ||
| 226 | return Test::Smoke::App::AppOption->new( | ||
| 227 | name => 'umask', | ||
| 228 | allow => undef, | ||
| 229 | default => 0, | ||
| 230 | helptext => "Unix way to set authorisation on files", | ||
| 231 | configtext => "What 'umask' can be used ('0' preferred)?\n", | ||
| 232 | configalt => sub { [] }, | ||
| 233 | configdft => sub { 0 }, | ||
| 234 | ); | ||
| 235 | } | ||
| 236 | |||
| 237 | =head2 save_buildcfg_file_option | ||
| 238 | |||
| 239 | This option is not in the config-file, it's for local use only. | ||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | sub save_buildcfg_file_option { | ||
| 244 | return Test::Smoke::App::AppOption->new( | ||
| 245 | name => 'save_buildcfg_file', | ||
| 246 | default => 1, | ||
| 247 | helptext => "Buildconfigurations have changed", | ||
| 248 | configtext => "Would you like to save the file?", | ||
| 249 | configtype => 'prompt_yn', | ||
| 250 | configalt => sub { [qw/ Y n /] }, | ||
| 251 | configdft => sub {'y'}, | ||
| 252 | ); | ||
| 253 | } | ||
| 254 | |||
| 255 | sub option_from_env { | ||
| 256 | my $env = shift; | ||
| 257 | my $opt = lc($env); | ||
| 258 | return Test::Smoke::App::AppOption->new( | ||
| 259 | name => $opt, | ||
| 260 | option => '=s', | ||
| 261 | default => '', | ||
| 262 | helptext => "Sets $env in the smoke script.", | ||
| 263 | configtext => "I see you have $env set. Use this value in the smoke script? | ||
| 264 | \t(Make empty, to not have $env)", | ||
| 265 | configdft => sub { $ENV{$env} }, | ||
| 266 | ); | ||
| 267 | } | ||
| 268 | |||
| 269 | 1; | ||
| 270 | |||
| 271 | =head1 COPYRIGHT | ||
| 272 | |||
| 273 | (c) 2020, All rights reserved. | ||
| 274 | |||
| 275 | * Abe Timmerman <abeltje@cpan.org> | ||
| 276 | |||
| 277 | This library is free software; you can redistribute it and/or modify | ||
| 278 | it under the same terms as Perl itself. | ||
| 279 | |||
| 280 | See: | ||
| 281 | |||
| 282 | =over 4 | ||
| 283 | |||
| 284 | =item * L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 285 | |||
| 286 | =item * L<http://www.gnu.org/copyleft/gpl.html> | ||
| 287 | |||
| 288 | =back | ||
| 289 | |||
| 290 | This program is distributed in the hope that it will be useful, | ||
| 291 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 292 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 293 | |||
| 294 | =cut |