| 1 | # Build::Hopen::App: hopen(1) program | ||
| 2 | package Build::Hopen::App; | ||
| 3 | our $VERSION = '0.000008'; # TRIAL | ||
| 4 | |||
| 5 | # Imports {{{1 | ||
| 6 | use Build::Hopen::Base; | ||
| 7 | |||
| 8 | use Build::Hopen qw(:default loadfrom isMYH MYH $VERBOSE $QUIET); | ||
| 9 | use Build::Hopen::AppUtil ':all'; | ||
| 10 | use Build::Hopen::BuildSystemGlobals; | ||
| 11 | use Build::Hopen::Phases qw(:default phase_idx next_phase); | ||
| 12 | use Build::Hopen::Scope::Hash; | ||
| 13 | use Build::Hopen::Scope::Environment; | ||
| 14 | use Build::Hopen::Util::Data qw(dedent forward_opts); | ||
| 15 | use Data::Dumper; | ||
| 16 | use File::Path::Tiny; | ||
| 17 | use File::stat (); | ||
| 18 | use Getopt::Long qw(GetOptionsFromArray :config gnu_getopt); | ||
| 19 | use Hash::Merge; | ||
| 20 | use Path::Class; | ||
| 21 | use Scalar::Util qw(looks_like_number); | ||
| 22 | |||
| 23 | # }}}1 | ||
| 24 | # Constants {{{1 | ||
| 25 | |||
| 26 | use constant DEBUG => false; | ||
| 27 | |||
| 28 | # Shell exit codes | ||
| 29 | use constant EXIT_OK => 0; # success | ||
| 30 | use constant EXIT_PROC_ERR => 1; # error during processing | ||
| 31 | use constant EXIT_PARAM_ERR => 2; # couldn't understand the command line | ||
| 32 | |||
| 33 | # }}}1 | ||
| 34 | # Documentation {{{1 | ||
| 35 | |||
| 36 | =pod | ||
| 37 | |||
| 38 | =encoding UTF-8 | ||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | Build::Hopen::App - hopen build system command-line interface | ||
| 43 | |||
| 44 | =head1 USAGE | ||
| 45 | |||
| 46 | hopen [options] [--] [destination dir [project dir]] | ||
| 47 | |||
| 48 | If no project directory is specified, the current directory is used. | ||
| 49 | |||
| 50 | If no destination directory is specified, C<< <project dir>/built >> is used. | ||
| 51 | |||
| 52 | =head1 INTERNALS | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | # }}}1 | ||
| 57 | # === Command line parsing ============================================== {{{1 | ||
| 58 | |||
| 59 | =head2 %CMDLINE_OPTS | ||
| 60 | |||
| 61 | A hash from internal name to array reference of | ||
| 62 | [getopt-name, getopt-options, optional default-value]. | ||
| 63 | |||
| 64 | If default-value is a reference, it will be the destination for that value. | ||
| 65 | =cut | ||
| 66 | |||
| 67 | my %CMDLINE_OPTS = ( | ||
| 68 | # They are listed in alphabetical order by option name, | ||
| 69 | # lowercase before upper, although the code does not require that order. | ||
| 70 | |||
| 71 | ARCHITECTURE => ['a','|A|architecture|platform=s'], | ||
| 72 | # -A and --platform are for the comfort of folks migrating from CMake | ||
| 73 | |||
| 74 | #BUILD => ['build'], # TODO implement this --- if specified, do not | ||
| 75 | # run any phases. Instead, run the | ||
| 76 | # build tool indicated by the generator. | ||
| 77 | |||
| 78 | #DUMP_VARS => ['d', '|dump-variables', false], | ||
| 79 | #DEBUG => ['debug','', false], | ||
| 80 | DEFINE => ['D',':s%'], | ||
| 81 | EVAL => ['e','|eval=s@'], # Perl source to run as a hopen file | ||
| 82 | #RESTRICTED_EVAL => ['E','|exec=s@'], | ||
| 83 | # TODO add -f to specify additional hopen files | ||
| 84 | FRESH => ['fresh'], # Don't run MY.hopen.pl | ||
| 85 | PROJ_DIR => ['from','=s'], | ||
| 86 | |||
| 87 | GENERATOR => ['g', '|G|generator=s', 'Make'], # -G is from CMake | ||
| 88 | # *** This is where the default generator is set *** | ||
| 89 | |||
| 90 | #GO => ['go'], # TODO implement this --- if specified, run all phases | ||
| 91 | # and invoke the build tool without requiring the user to | ||
| 92 | # re-run hopen. | ||
| 93 | |||
| 94 | # -h and --help reserved | ||
| 95 | #INCLUDE => ['i','|include=s@'], | ||
| 96 | #LIB => ['l','|load=s@'], # TODO implement this. A separate option | ||
| 97 | # for libs only used for hopen files? | ||
| 98 | #LANGUAGE => ['L','|language:s'], | ||
| 99 | # --man reserved | ||
| 100 | # OUTPUT_FILENAME => ['o','|output=s', ""], | ||
| 101 | # OPTIMIZE => ['O','|optimize'], | ||
| 102 | |||
| 103 | PHASE => ['phase','=s'], # NO DEFAULT so we can tell if --phase was used | ||
| 104 | |||
| 105 | QUIET => ['q'], | ||
| 106 | #SANDBOX => ['S','|sandbox',false], | ||
| 107 | #SOURCES reserved | ||
| 108 | TOOLSET => ['t','|T|toolset=s'], # -T is from CMake | ||
| 109 | DEST_DIR => ['to','=s'], | ||
| 110 | # --usage reserved | ||
| 111 | PRINT_VERSION => ['version','', false], | ||
| 112 | VERBOSE => ['v','+', 0], | ||
| 113 | VERBOSE2 => ['verbose',':s'], # --verbose=<n> | ||
| 114 | # -? reserved | ||
| 115 | |||
| 116 | ); | ||
| 117 | |||
| 118 | sub _parse_command_line { # {{{2 | ||
| 119 | |||
| 120 | =head2 _parse_command_line | ||
| 121 | |||
| 122 | Takes {into=>hash ref, from=>array ref}. Fills in the hash with the | ||
| 123 | values from the command line, keyed by the keys in L</%CMDLINE_OPTS>. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | my %params = @_; | ||
| 128 | #local @_Sources; | ||
| 129 | |||
| 130 | my $hrOptsOut = $params{into}; | ||
| 131 | |||
| 132 | # Easier syntax for checking whether optional args were provided. | ||
| 133 | # Syntax thanks to http://www.perlmonks.org/?node_id=696592 | ||
| 134 | local *have = sub { return exists($hrOptsOut->{ $_[0] }); }; | ||
| 135 | |||
| 136 | # Set defaults so we don't have to test them with exists(). | ||
| 137 | %$hrOptsOut = ( # map getopt option name to default value | ||
| 138 | map { $CMDLINE_OPTS{ $_ }->[0] => $CMDLINE_OPTS{ $_ }[2] } | ||
| 139 | grep { (scalar @{$CMDLINE_OPTS{ $_ }})==3 } | ||
| 140 | keys %CMDLINE_OPTS | ||
| 141 | ); | ||
| 142 | |||
| 143 | # Get options | ||
| 144 | my $opts_ok = GetOptionsFromArray( | ||
| 145 | $params{from}, # source array | ||
| 146 | $hrOptsOut, # destination hash | ||
| 147 | 'usage|?', 'h|help', 'man', # options we handle here | ||
| 148 | map { $_->[0] . ($_->[1] // '') } values %CMDLINE_OPTS, # options strs | ||
| 149 | ); | ||
| 150 | |||
| 151 | # Help, if requested | ||
| 152 | if(!$opts_ok || have('usage') || have('h') || have('man')) { | ||
| 153 | |||
| 154 | # Only pull in the Pod routines if we actually need them. | ||
| 155 | |||
| 156 | # Terminal formatting, if present. | ||
| 157 | { | ||
| 158 | no warnings 'once'; | ||
| 159 | eval "require Pod::Text::Termcap"; | ||
| 160 | $Pod::Usage::Formatter = 'Pod::Text::Termcap' unless $@; | ||
| 161 | } | ||
| 162 | |||
| 163 | require Pod::Usage; | ||
| 164 | |||
| 165 | my @in = (-input => __FILE__); | ||
| 166 | Pod::Usage::pod2usage(-verbose => 0, -exitval => EXIT_PARAM_ERR, @in) | ||
| 167 | unless $opts_ok; # unknown opt | ||
| 168 | Pod::Usage::pod2usage(-verbose => 0, -exitval => EXIT_OK, @in) | ||
| 169 | if have('usage'); | ||
| 170 | Pod::Usage::pod2usage(-verbose => 1, -exitval => EXIT_OK, @in) | ||
| 171 | if have('h'); | ||
| 172 | |||
| 173 | # --man: suppress "INTERNALS" section. Note that this does | ||
| 174 | # get rid of the automatic pager we would otherwise get | ||
| 175 | # by virtue of pod2usage's invoking perldoc(1). Oh well. | ||
| 176 | |||
| 177 | Pod::Usage::pod2usage( | ||
| 178 | -exitval => EXIT_OK, @in, | ||
| 179 | -verbose => 99, -sections => '!INTERNALS' # suppress | ||
| 180 | ) if have('man'); | ||
| 181 | } | ||
| 182 | |||
| 183 | # Map the option names from GetOptions back to the internal names we use, | ||
| 184 | # e.g., $hrOptsOut->{EVAL} from $hrOptsOut->{e}. | ||
| 185 | my %revmap = map { $CMDLINE_OPTS{$_}->[0] => $_ } keys %CMDLINE_OPTS; | ||
| 186 | for my $optname (keys %$hrOptsOut) { | ||
| 187 | $hrOptsOut->{ $revmap{$optname} } = $hrOptsOut->{ $optname }; | ||
| 188 | } | ||
| 189 | |||
| 190 | # Process other arguments. The first two non-option arguments are dest | ||
| 191 | # dir and project dir, if --from and --to were not given. | ||
| 192 | $hrOptsOut->{DEST_DIR} //= $params{from}->[0] if @{$params{from}}; | ||
| 193 | $hrOptsOut->{PROJ_DIR} //= $params{from}->[1] if @{$params{from}}>1; | ||
| 194 | |||
| 195 | # Sanity check VERBOSE2, and give it a default of 0 | ||
| 196 | my $v2 = $hrOptsOut->{VERBOSE2} // 0; | ||
| 197 | $v2 = 1 if $v2 eq ''; # --verbose without value === --verbose=1 | ||
| 198 | die "--verbose requires a positive numeric argument" | ||
| 199 | if (defined $v2) && ( !looks_like_number($v2) || (int($v2) < 0) ); | ||
| 200 | $hrOptsOut->{VERBOSE2} = int($v2 // 0); | ||
| 201 | |||
| 202 | } #_parse_command_line() }}}2 | ||
| 203 | |||
| 204 | # }}}1 | ||
| 205 | # === Main worker code ================================================== {{{1 | ||
| 206 | |||
| 207 | =head2 $_hrData | ||
| 208 | |||
| 209 | The hashref of the current data we have built up by processing hopen files. | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | our $_hrData; # the hashref of current data | ||
| 214 | |||
| 215 | =head2 $_did_set_phase | ||
| 216 | |||
| 217 | Set to truthy if MY.hopen.pl sets the phase. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | our $_did_set_phase = false; | ||
| 222 | |||
| 223 | sub _execute_hopen_file { # Load and run a single hopen file {{{2 | ||
| 224 | |||
| 225 | =head2 _execute_hopen_file | ||
| 226 | |||
| 227 | Execute a single hopen file, but B<do not> run the DAG. Usage: | ||
| 228 | |||
| 229 | _execute_hopen_file($filename[, options...]) | ||
| 230 | |||
| 231 | This function takes input from L</$_hrData> unless a C<< DATA=>{...} >> option | ||
| 232 | is given. This function updates L</$_hrData> based on the results. | ||
| 233 | |||
| 234 | Options are: | ||
| 235 | |||
| 236 | =over | ||
| 237 | |||
| 238 | =item phase | ||
| 239 | |||
| 240 | If given, force the phase to be the one specified. | ||
| 241 | |||
| 242 | =item quiet | ||
| 243 | |||
| 244 | If truthy, suppress extra output. | ||
| 245 | |||
| 246 | =item libs | ||
| 247 | |||
| 248 | If given, it must be an arrayref of directories. Each of those will be | ||
| 249 | turned into a C<use lib> statement (see L<lib>) in the generated source. | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | my $fn = shift or croak 'Need a file to run'; | ||
| 256 | my %opts = @_; | ||
| 257 | $Phase = $opts{phase} if $opts{phase}; | ||
| 258 | |||
| 259 | my $merger = Hash::Merge->new('RETAINMENT_PRECEDENT'); | ||
| 260 | |||
| 261 | # == Set up code pieces related to phase control == | ||
| 262 | |||
| 263 | my ($set_phase, $cannot_set_phase, $cannot_set_phase_warn); | ||
| 264 | my $setting_phase_allowed = false; | ||
| 265 | |||
| 266 | $set_phase = q( | ||
| 267 | sub can_set_phase { true } | ||
| 268 | sub set_phase { | ||
| 269 | my $new_phase = shift or croak 'Need a phase'; | ||
| 270 | croak "Phase $new_phase is not one of the ones I know about (" . | ||
| 271 | join(', ', @PHASES) . ')' | ||
| 272 | unless defined phase_idx($new_phase); | ||
| 273 | $Build::Hopen::BuildSystemGlobals::Phase = $new_phase; | ||
| 274 | $Build::Hopen::App::_did_set_phase = true; | ||
| 275 | ) . | ||
| 276 | ($opts{quiet} ? '' : 'say "Running $new_phase phase";') . "}\n"; | ||
| 277 | |||
| 278 | $cannot_set_phase = q( | ||
| 279 | sub can_set_phase { false } | ||
| 280 | sub set_phase { | ||
| 281 | croak "I'm sorry, but this file (``$FILENAME'') is not allowed to set the phase" | ||
| 282 | } | ||
| 283 | ); | ||
| 284 | |||
| 285 | $cannot_set_phase_warn = q( | ||
| 286 | sub can_set_phase { false } | ||
| 287 | sub set_phase { | ||
| 288 | ) . | ||
| 289 | ($opts{quiet} ? '' : | ||
| 290 | q( | ||
| 291 | warn "``$FILENAME'': Ignoring attempt to set phase"; | ||
| 292 | ) | ||
| 293 | ) . "}\n"; | ||
| 294 | |||
| 295 | my $lib_dirs = ''; | ||
| 296 | if($opts{libs}) { | ||
| 297 | $lib_dirs .= "use lib '" . (dir($_)->absolute =~ s/'/\\'/gr) . "';\n" | ||
| 298 | foreach @{$opts{libs}}; | ||
| 299 | } | ||
| 300 | |||
| 301 | # == Make the hopen file into a package we can eval == | ||
| 302 | |||
| 303 | my ($friendly_name, $pkg_name, $file_text, $phase_text); | ||
| 304 | |||
| 305 | $phase_text = q( | ||
| 306 | use Build::Hopen::Phases ':all'; | ||
| 307 | ); | ||
| 308 | |||
| 309 | # -- Load the file | ||
| 310 | |||
| 311 | if(ref $fn eq 'HASH') { # it's a -e | ||
| 312 | hlog { 'Processing', $fn->{name} }; | ||
| 313 | $file_text = $fn->{text}; | ||
| 314 | $friendly_name = $fn->{name}; | ||
| 315 | $pkg_name = 'CmdLineE' . $fn->{num}; | ||
| 316 | $phase_text .= defined($opts{phase}) ? $cannot_set_phase : $set_phase; | ||
| 317 | # -e's can set phase unless --phase was specified | ||
| 318 | |||
| 319 | } else { | ||
| 320 | hlog { 'Processing', $fn }; | ||
| 321 | $file_text = file($fn)->slurp; | ||
| 322 | $pkg_name = ($fn =~ s/[^a-zA-Z0-9]/_/gr); | ||
| 323 | $friendly_name = $fn; | ||
| 324 | |||
| 325 | if( isMYH($fn) and !defined($opts{phase}) ) { | ||
| 326 | # MY.hopen.pl files can set $Phase unless --phase was given. | ||
| 327 | $phase_text .= $set_phase; | ||
| 328 | $setting_phase_allowed = true; | ||
| 329 | |||
| 330 | } else { | ||
| 331 | # For MY.hopen.pl, when --phase is set, set_phase doesn't croak. | ||
| 332 | # If this were not the case, every second or subsequent run | ||
| 333 | # of hopen(1) would croak if --phase were specified! | ||
| 334 | $phase_text .= isMYH($fn) ? $cannot_set_phase_warn : $cannot_set_phase; | ||
| 335 | } | ||
| 336 | } #endif -e else | ||
| 337 | |||
| 338 | $friendly_name =~ s{"}{-}g; | ||
| 339 | # as far as I can tell, #line can't handle embedded quotes. | ||
| 340 | |||
| 341 | # -- Build the package | ||
| 342 | |||
| 343 | my $src = <<EOT; | ||
| 344 | { | ||
| 345 | package __Rpkg_$pkg_name; | ||
| 346 | use Build::Hopen::HopenFileKit "$friendly_name"; | ||
| 347 | |||
| 348 | # Other lib dirs | ||
| 349 | $lib_dirs | ||
| 350 | # /Other lib dirs | ||
| 351 | |||
| 352 | # Other phase text | ||
| 353 | $phase_text | ||
| 354 | # /Other phase text | ||
| 355 | EOT | ||
| 356 | |||
| 357 | # Now shadow $Phase so the hopen file can't change it without | ||
| 358 | # really trying! Note that we actually interpolate the current | ||
| 359 | # phase in as a literal so that it's read-only (see perlmod). | ||
| 360 | |||
| 361 | unless($setting_phase_allowed) { | ||
| 362 | $src .= <<EOT; | ||
| 363 | our \$Phase; | ||
| 364 | local *Phase = \\"$Phase"; | ||
| 365 | EOT | ||
| 366 | } | ||
| 367 | |||
| 368 | $src .= <<EOT; | ||
| 369 | |||
| 370 | sub __Rsub_$pkg_name { | ||
| 371 | my \$__R_retval = do { # return statements in here will exit the Rsub | ||
| 372 | __R_DO: { | ||
| 373 | #line 1 "$friendly_name" | ||
| 374 | $file_text | ||
| 375 | } #__R_DO | ||
| 376 | }; # do{} | ||
| 377 | EOT | ||
| 378 | |||
| 379 | # If the file_text did not expressly return(), control will reach the | ||
| 380 | # following block, where we get the correct return value. If the file_text | ||
| 381 | # ran to completion, we have a defined __R_retval. If the file text exited | ||
| 382 | # via Phases::on(), we have a defined __R_on_result. If either of those | ||
| 383 | # is defined, make sure it's not a DAG or GraphBuilder. Those should not | ||
| 384 | # be put into the return data. | ||
| 385 | |||
| 386 | $src .= <<EOT; | ||
| 387 | \$__R_retval //= \$__R_on_result; | ||
| 388 | |||
| 389 | if(defined(\$__R_retval) && ref(\$__R_retval)) { | ||
| 390 | die 'Hopen files may not return graphs' | ||
| 391 | if eval { \$__R_retval->DOES('Build::Hopen::G::DAG') }; | ||
| 392 | die 'Hopen files may not return graph builders (is a ->goal or ->default_goal missing?)' | ||
| 393 | if eval { \$__R_retval->DOES('Build::Hopen::G::GraphBuilder') }; | ||
| 394 | } | ||
| 395 | |||
| 396 | return \$__R_retval; | ||
| 397 | } #__Rsub_$pkg_name | ||
| 398 | |||
| 399 | our \$hrNewData = __Rsub_$pkg_name(\$Build::Hopen::App::_hrData); | ||
| 400 | } #package | ||
| 401 | EOT | ||
| 402 | # Put the result in a package variable because that way I don't have | ||
| 403 | # to remember the rules for the return value of eval(). | ||
| 404 | |||
| 405 | hlog { "Source for $fn\n", $src, "\n" } 3; | ||
| 406 | |||
| 407 | # == Run the package == | ||
| 408 | |||
| 409 | eval($src); | ||
| 410 | die "Error in $friendly_name: $@" if $@; | ||
| 411 | |||
| 412 | # Get the data from the package we just ran | ||
| 413 | my $hrAddlData = eval ("\$__Rpkg_${pkg_name}" . '::hrNewData'); | ||
| 414 | |||
| 415 | hlog { 'old data', Dumper($_hrData) } 3; | ||
| 416 | hlog { 'new data', Dumper($hrAddlData) } 2; | ||
| 417 | |||
| 418 | # TODO? Remove all __R* hash keys from $hrAddlData unless it's a | ||
| 419 | # MY.hopen.pl file? | ||
| 420 | |||
| 421 | # == Merge in the data == | ||
| 422 | |||
| 423 | $_hrData = $merger->merge($_hrData, $hrAddlData) if $hrAddlData; | ||
| 424 | hlog { 'data after merge', Dumper($_hrData) } 2; | ||
| 425 | |||
| 426 | } #_execute_hopen_file() }}}2 | ||
| 427 | |||
| 428 | sub _run_phase { # Run a single phase. {{{2 | ||
| 429 | |||
| 430 | =head2 _run_phase | ||
| 431 | |||
| 432 | Run a phase by executing the hopen files and running the DAG. | ||
| 433 | Reads from and writes to L</$_hrData>, which must be initialized by | ||
| 434 | the caller. Usage: | ||
| 435 | |||
| 436 | my $hrDagOutput = _run_phase(files=>[...][, options...]) | ||
| 437 | |||
| 438 | Options C<phase>, C<quiet>, and C<libs> are as L</_execute_hopen_file>. | ||
| 439 | Other options are: | ||
| 440 | |||
| 441 | =over | ||
| 442 | |||
| 443 | =item files | ||
| 444 | |||
| 445 | (Required) An arrayref of filenames to run | ||
| 446 | |||
| 447 | =item norun | ||
| 448 | |||
| 449 | (Optional) if truthy, do not run the DAG. Note that the DAG will also not | ||
| 450 | be run if it is empty. | ||
| 451 | |||
| 452 | =back | ||
| 453 | |||
| 454 | =cut | ||
| 455 | |||
| 456 | my %opts = @_; | ||
| 457 | $Phase = $opts{phase} if $opts{phase}; | ||
| 458 | my $lrHopenFiles = $opts{files}; | ||
| 459 | croak 'Need files=>[...]' unless ref $lrHopenFiles eq 'ARRAY'; | ||
| 460 | hlog { Phase => $Phase, Running => Dumper($lrHopenFiles) }; | ||
| 461 | |||
| 462 | # = Process the files ====================================== | ||
| 463 | |||
| 464 | foreach my $fn (@$lrHopenFiles) { | ||
| 465 | _execute_hopen_file($fn, | ||
| 466 | forward_opts(\%opts, qw(phase quiet libs)) | ||
| 467 | ); | ||
| 468 | } # foreach hopen file | ||
| 469 | |||
| 470 | hlog { 'Graph is', ($Build->empty ? 'empty.' : 'not empty.'), | ||
| 471 | ' Final data is', Dumper($_hrData) } 2; | ||
| 472 | |||
| 473 | hlog { Data::Dumper->new([$Build], ['$Build'])->Indent(1)->Dump } 5; | ||
| 474 | |||
| 475 | # If there is no build graph, just return the data. This is useful | ||
| 476 | # enough for debugging that I am making it documented behaviour. | ||
| 477 | |||
| 478 | return $_hrData if $Build->empty or $opts{norun}; | ||
| 479 | |||
| 480 | # = Execute the resulting build graph ====================== | ||
| 481 | |||
| 482 | # Wrap the final data in a Scope | ||
| 483 | my $env = Build::Hopen::Scope::Environment->new(name => 'outermost'); | ||
| 484 | my $scope = Build::Hopen::Scope::Hash->new(name => 'from hopen files'); | ||
| 485 | $scope->adopt_hash($_hrData); | ||
| 486 | |||
| 487 | # Run the DAG | ||
| 488 | my $result_data = $Build->run(-context => $scope, -phase => $Phase, | ||
| 489 | -generator => $Generator); | ||
| 490 | hlog { Data::Dumper->new([$result_data], ['Build graph result data'])->Indent(1)->Dump } 2; | ||
| 491 | return $result_data; | ||
| 492 | } #_run_phase() }}}2 | ||
| 493 | |||
| 494 | sub _inner { # Run a single invocation of hopen(1). {{{2 | ||
| 495 | |||
| 496 | =head2 _inner | ||
| 497 | |||
| 498 | Do the work for one invocation of hopen(1). Dies on failure. Main() then | ||
| 499 | translates the die() into a print and error return. | ||
| 500 | |||
| 501 | =cut | ||
| 502 | |||
| 503 | my %opts = @_; | ||
| 504 | local $_hrData = {}; | ||
| 505 | |||
| 506 | if($opts{PRINT_VERSION}) { # print version, raw and dotted | ||
| 507 | if($Build::Hopen::VERSION =~ m<^([^\.]+)\.(\d{3})(\d{3})>) { | ||
| 508 | printf "hopen version %d.%d.%d ($Build::Hopen::VERSION)\n", $1, $2, $3; | ||
| 509 | } else { | ||
| 510 | say "hopen $VERSION"; | ||
| 511 | } | ||
| 512 | if($opts{VERBOSE} >= 1) { | ||
| 513 | say "Build::Hopen: $INC{'Build/Hopen.pm'}"; | ||
| 514 | } | ||
| 515 | return EXIT_OK; | ||
| 516 | } | ||
| 517 | |||
| 518 | # Start with the default phase unless one was specified. | ||
| 519 | $Phase = $opts{PHASE} // $PHASES[0]; | ||
| 520 | die "Phase $Phase is not one of the ones I know about (" . | ||
| 521 | join(', ', @PHASES) . ')' | ||
| 522 | unless defined phase_idx($Phase); | ||
| 523 | |||
| 524 | # Get the project dir | ||
| 525 | my $proj_dir = $opts{PROJ_DIR} ? dir($opts{PROJ_DIR}) : dir; #default=cwd | ||
| 526 | $ProjDir = $proj_dir; | ||
| 527 | |||
| 528 | # Get the destination dir | ||
| 529 | my $dest_dir; | ||
| 530 | if($opts{DEST_DIR}) { | ||
| 531 | $dest_dir = dir($opts{DEST_DIR}); | ||
| 532 | } else { | ||
| 533 | $dest_dir = $proj_dir->subdir('built'); | ||
| 534 | } | ||
| 535 | $DestDir = $dest_dir; | ||
| 536 | |||
| 537 | # Prohibit in-source builds | ||
| 538 | die <<EOT if $proj_dir eq $dest_dir; | ||
| 539 | I'm sorry, but I don't support in-source builds (dir ``$proj_dir''). Please | ||
| 540 | specify a different project directory (--from) or destination directory (--to). | ||
| 541 | EOT | ||
| 542 | |||
| 543 | # Prohibit builds if there's a MY.hopen.pl file in the project directory, | ||
| 544 | # since those are the marker of a destination directory. | ||
| 545 | die <<EOT if -e $proj_dir->file(MYH); | ||
| 546 | I'm sorry, but project directory ``$proj_dir'' appears to actually be a | ||
| 547 | build directory --- it has a @{[MYH]} file. If you really want to build | ||
| 548 | here, remove or rename @{[MYH]} and run me again. | ||
| 549 | EOT | ||
| 550 | |||
| 551 | # See if we have hopen files associated with the project dir | ||
| 552 | my $myhopen = find_myhopen($dest_dir, !!$opts{FRESH}); | ||
| 553 | my $lrHopenFiles = find_hopen_files($proj_dir, $dest_dir, !!$opts{FRESH}); | ||
| 554 | |||
| 555 | # Check the mtimes - we don't use MYH if another hopen file is newer. | ||
| 556 | if($myhopen && -e $myhopen) { | ||
| 557 | my $myhstat = file($myhopen)->stat; | ||
| 558 | |||
| 559 | foreach my $fn (@$lrHopenFiles) { | ||
| 560 | my $stat = file($fn)->stat; | ||
| 561 | |||
| 562 | if( $stat->mtime > $myhstat->mtime || | ||
| 563 | $stat->ctime > $myhstat->ctime) | ||
| 564 | { | ||
| 565 | say "Skipping out-of-date ``$myhopen''" unless $opts{QUIET}; | ||
| 566 | $myhopen = undef; | ||
| 567 | last; | ||
| 568 | } | ||
| 569 | } #foreach hopen file | ||
| 570 | } #if MYH exists | ||
| 571 | |||
| 572 | # Add -e's to the list of hopen files | ||
| 573 | if($opts{EVAL}) { | ||
| 574 | my $which_e = 0; | ||
| 575 | push @$lrHopenFiles, | ||
| 576 | map { | ||
| 577 | ++$which_e; | ||
| 578 | +{text=>$_, num=>$which_e, name=>("-e #" . $which_e)} | ||
| 579 | } @{$opts{EVAL}}; | ||
| 580 | } | ||
| 581 | |||
| 582 | hlog { 'hopen files: ', | ||
| 583 | map { ref eq 'HASH' ? "<<$_->{text}>>" : "``$_''" } | ||
| 584 | ($myhopen // (), @$lrHopenFiles) } 2; | ||
| 585 | |||
| 586 | die <<EOT unless $myhopen || @$lrHopenFiles; | ||
| 587 | I can't find any hopen project files (.hopen.pl or *.hopen.pl) for | ||
| 588 | project directory ``$proj_dir''. | ||
| 589 | EOT | ||
| 590 | |||
| 591 | # = Initialize ========================================================== | ||
| 592 | |||
| 593 | say "From ``$proj_dir'' into ``$dest_dir''" unless $opts{QUIET}; | ||
| 594 | |||
| 595 | # Prepare the destination directory if it doesn't exist | ||
| 596 | File::Path::Tiny::mk($dest_dir) or die "Couldn't create $dest_dir: $!"; | ||
| 597 | |||
| 598 | # Create the initial DAG before loading anything so that the | ||
| 599 | # generator and toolset can add initialization operations. | ||
| 600 | $Build = hnew DAG => '__R_main'; | ||
| 601 | |||
| 602 | # Load MY.hopen.pl first so the results of the Probe phase are | ||
| 603 | # available to the generator and toolset. | ||
| 604 | if($myhopen) { | ||
| 605 | _execute_hopen_file($myhopen, | ||
| 606 | forward_opts(\%opts, {lc=>1}, qw(PHASE QUIET)), | ||
| 607 | ); # TODO support _e_h_f libs option | ||
| 608 | } | ||
| 609 | |||
| 610 | # Tell the user the initial phase if MY.hopen.pl didn't change it | ||
| 611 | say "Running $Phase phase" unless $_did_set_phase or $opts{QUIET}; | ||
| 612 | |||
| 613 | # Load generator | ||
| 614 | my ($gen, $gen_class); | ||
| 615 | $gen_class = loadfrom($opts{GENERATOR}, 'Build::Hopen::Gen::', ''); | ||
| 616 | die "Can't find generator $opts{GENERATOR}" unless $gen_class; | ||
| 617 | hlog { "Generator spec ``$opts{GENERATOR}'' -> using generator $gen_class" }; | ||
| 618 | |||
| 619 | $gen = "$gen_class"->new(proj_dir => $proj_dir, dest_dir => $dest_dir, | ||
| 620 | architecture => $opts{ARCHITECTURE}) | ||
| 621 | or die "Can't initialize generator"; | ||
| 622 | $Generator = $gen; | ||
| 623 | |||
| 624 | # Load toolset | ||
| 625 | my $toolset_class; | ||
| 626 | $opts{TOOLSET} //= $gen->default_toolset; | ||
| 627 | $toolset_class = loadfrom($opts{TOOLSET}, | ||
| 628 | 'Build::Hopen::T::', ''); | ||
| 629 | die "Can't find toolset $opts{TOOLSET}" unless $toolset_class; | ||
| 630 | |||
| 631 | hlog { "Toolset spec ``$opts{TOOLSET}'' -> using toolset $toolset_class" }; | ||
| 632 | $Toolset = $toolset_class; | ||
| 633 | |||
| 634 | # = Run the hopen files ================================================= | ||
| 635 | |||
| 636 | my $new_data; | ||
| 637 | if(@$lrHopenFiles) { | ||
| 638 | $new_data = _run_phase( | ||
| 639 | files => [@$lrHopenFiles], | ||
| 640 | forward_opts(\%opts, {lc=>1}, qw(PHASE QUIET)) | ||
| 641 | ); # TODO support _run_phase libs option | ||
| 642 | |||
| 643 | } else { # No hopen files (other than MYH) => just use the data from MYH | ||
| 644 | $new_data = $_hrData; | ||
| 645 | } | ||
| 646 | |||
| 647 | $Generator->finalize(-phase => $Phase, -dag => $Build, | ||
| 648 | -data => $new_data); | ||
| 649 | # TODO RESUME HERE - figure out how the generator works into this. | ||
| 650 | |||
| 651 | # = Save state in MY.hopen.pl for the next run ========================== | ||
| 652 | |||
| 653 | # If we get here, _run_phase succeeded. Therefore, we can move | ||
| 654 | # on to the next phase. | ||
| 655 | my $new_phase = next_phase($Phase) // $Phase; | ||
| 656 | |||
| 657 | # TODO? give the generators a way to stash information that will be | ||
| 658 | # written at the top of MY.hopen.pl. This way, the user may only | ||
| 659 | # need to edit right at the top of the file, and not also at the | ||
| 660 | |||
| 661 | my $dumper = Data::Dumper->new([$new_data], ['__R_new_data']); | ||
| 662 | $dumper->Quotekeys(0); | ||
| 663 | $dumper->Purity(1); | ||
| 664 | $dumper->Maxrecurse(0); # no limit | ||
| 665 | $dumper->Sortkeys(true); # For consistency between runs | ||
| 666 | $dumper->Sparseseen(true); # We don't use Seen() | ||
| 667 | |||
| 668 | my $new_text = dedent [], qq( | ||
| 669 | # @{[MYH]} generated at @{[scalar gmtime]} GMT | ||
| 670 | # From ``@{[$proj_dir->absolute]}'' into ``@{[$dest_dir->absolute]}'' | ||
| 671 | |||
| 672 | set_phase '$new_phase'; | ||
| 673 | do { | ||
| 674 | my @{[$dumper->Dump]} | ||
| 675 | } | ||
| 676 | ); | ||
| 677 | |||
| 678 | $dest_dir->file(MYH)->spew($new_text); | ||
| 679 | |||
| 680 | } #_inner() }}}2 | ||
| 681 | |||
| 682 | # }}}1 | ||
| 683 | # === Command-line runner =============================================== {{{1 | ||
| 684 | |||
| 685 | sub Main { | ||
| 686 | |||
| 687 | =head2 Main | ||
| 688 | |||
| 689 | Command-line runner. Call as C<< Build::Hopen::App::Main(\@ARGV) >>. | ||
| 690 | |||
| 691 | =cut | ||
| 692 | |||
| 693 | my $lrArgs = shift // []; | ||
| 694 | |||
| 695 | # = Process options ===================================================== | ||
| 696 | |||
| 697 | my %opts; | ||
| 698 | _parse_command_line(from => $lrArgs, into => \%opts); | ||
| 699 | |||
| 700 | # Verbosity is the max of -v and --verbose | ||
| 701 | $opts{VERBOSE} = $opts{VERBOSE2} if $opts{VERBOSE2} > $opts{VERBOSE}; | ||
| 702 | |||
| 703 | # Option overrides: -q beats -v | ||
| 704 | $opts{VERBOSE} = 0 if $opts{QUIET}; | ||
| 705 | $QUIET = $opts{QUIET} // false; | ||
| 706 | |||
| 707 | # Implement verbosity | ||
| 708 | if(!$QUIET && $opts{VERBOSE}) { | ||
| 709 | $VERBOSE += $opts{VERBOSE}; | ||
| 710 | #hlog { Verbosity => $VERBOSE }; | ||
| 711 | |||
| 712 | # Under -v, keep stdout and stderr lines in order. | ||
| 713 | use IO::Handle; | ||
| 714 | STDOUT->autoflush; | ||
| 715 | STDERR->autoflush; | ||
| 716 | } | ||
| 717 | |||
| 718 | # Don't print the source of an eval'ed hopen file unless -vvv or higher. | ||
| 719 | # Need 3 for the "..." that Carp prints when truncating. | ||
| 720 | $Carp::MaxEvalLen = 3 unless $VERBOSE >= 3; | ||
| 721 | |||
| 722 | # = Do it, Rockapella! ================================================== | ||
| 723 | |||
| 724 | eval { _inner(%opts); }; | ||
| 725 | my $msg = $@; | ||
| 726 | if($msg) { | ||
| 727 | print STDERR $msg; | ||
| 728 | return EXIT_PROC_ERR; # eval{} so we can do this (die() exitcode = 2) | ||
| 729 | } | ||
| 730 | |||
| 731 | return EXIT_OK; | ||
| 732 | } #Main() | ||
| 733 | |||
| 734 | # }}}1 | ||
| 735 | |||
| 736 | # no import() --- call Main() directly with its fully-qualified name | ||
| 737 | |||
| 738 | 1; # End of Build::Hopen::App | ||
| 739 | __END__ |