This file is indexed.

/usr/share/perl5/FCM1/Extract.pm is in fcm 2017.10.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
# ------------------------------------------------------------------------------
# (C) British Crown Copyright 2006-17 Met Office.
#
# This file is part of FCM, tools for managing and building source code.
#
# FCM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FCM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FCM. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
# NAME
#   FCM1::Extract
#
# DESCRIPTION
#   This is the top level class for the FCM extract system.
#
# ------------------------------------------------------------------------------

package FCM1::Extract;
@ISA = qw(FCM1::ConfigSystem);

# Standard pragma
use warnings;
use strict;

# Standard modules
use File::Path;
use File::Spec;

# FCM component modules
use FCM1::CfgFile;
use FCM1::CfgLine;
use FCM1::Config;
use FCM1::ConfigSystem;
use FCM1::Dest;
use FCM1::ExtractFile;
use FCM1::ExtractSrc;
use FCM1::Keyword;
use FCM1::ReposBranch;
use FCM1::SrcDirLayer;
use FCM1::Util;

# List of scalar property methods for this class
my @scalar_properties = (
 'bdeclare', # list of build declarations
 'branches', # list of repository branches
 'conflict', # conflict mode
 'rdest'   , # remote destination information
);

# List of hash property methods for this class
my @hash_properties = (
 'srcdirs' , # list of source directory extract info
 'files',    # list of files processed key=pkgname, value=FCM1::ExtractFile
);

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $obj = FCM1::Extract->new;
#
# DESCRIPTION
#   This method constructs a new instance of the FCM1::Extract class.
# ------------------------------------------------------------------------------

sub new {
  my $this  = shift;
  my %args  = @_;
  my $class = ref $this || $this;

  my $self = FCM1::ConfigSystem->new (%args);

  $self->{$_} = undef for (@scalar_properties);

  $self->{$_} = {} for (@hash_properties);

  bless $self, $class;

  # List of sub-methods for parse_cfg
  push @{ $self->cfg_methods }, (qw/rdest bld conflict project/);

  # System type
  $self->type ('ext');

  return $self;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $value = $obj->X;
#   $obj->X ($value);
#
# DESCRIPTION
#   Details of these properties are explained in @scalar_properties.
# ------------------------------------------------------------------------------

for my $name (@scalar_properties) {
  no strict 'refs';

  *$name = sub {
    my $self = shift;

    # Argument specified, set property to specified argument
    if (@_) {
      $self->{$name} = $_[0];
    }

    # Default value for property
    if (not defined $self->{$name}) {
      if ($name eq 'bdeclare' or $name eq 'branches') {
        # Reference to an array
        $self->{$name} = [];

      } elsif ($name eq 'rdest') {
        # New extract destination local/remote
        $self->{$name} = FCM1::Dest->new (DEST0 => $self->dest(), TYPE => 'ext');

      } elsif ($name eq 'conflict') {
        # Conflict mode, default to "merge"
        $self->{$name} = 'merge';
      }
    }

    return $self->{$name};
  }
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   %hash = %{ $obj->X () };
#   $obj->X (\%hash);
#
#   $value = $obj->X ($index);
#   $obj->X ($index, $value);
#
# DESCRIPTION
#   Details of these properties are explained in @hash_properties.
#
#   If no argument is set, this method returns a hash containing a list of
#   objects. If an argument is set and it is a reference to a hash, the objects
#   are replaced by the specified hash.
#
#   If a scalar argument is specified, this method returns a reference to an
#   object, if the indexed object exists or undef if the indexed object does
#   not exist. If a second argument is set, the $index element of the hash will
#   be set to the value of the argument.
# ------------------------------------------------------------------------------

for my $name (@hash_properties) {
  no strict 'refs';

  *$name = sub {
    my ($self, $arg1, $arg2) = @_;

    # Ensure property is defined as a reference to a hash
    $self->{$name} = {} if not defined ($self->{$name});

    # Argument 1 can be a reference to a hash or a scalar index
    my ($index, %hash);

    if (defined $arg1) {
      if (ref ($arg1) eq 'HASH') {
        %hash = %$arg1;

      } else {
        $index = $arg1;
      }
    }

    if (defined $index) {
      # A scalar index is defined, set and/or return the value of an element
      $self->{$name}{$index} = $arg2 if defined $arg2;

      return (
        exists $self->{$name}{$index} ? $self->{$name}{$index} : undef
      );

    } else {
      # A scalar index is not defined, set and/or return the hash
      $self->{$name} = \%hash if defined $arg1;
      return $self->{$name};
    }
  }
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->check_lock_is_allowed ($lock);
#
# DESCRIPTION
#   This method returns true if it is OK for $lock to exist in the destination.
# ------------------------------------------------------------------------------

sub check_lock_is_allowed {
  my ($self, $lock) = @_;

  # Allow existence of build lock in inherited extract
  return ($lock eq $self->dest->bldlock and @{ $self->inherited });
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->invoke_extract ();
#
# DESCRIPTION
#   This method invokes the extract stage of the extract system. It returns
#   true on success.
# ------------------------------------------------------------------------------

sub invoke_extract {
  my $self = shift;

  my $rc = 1;

  my @methods = (
    'expand_cfg',       # expand URL, revision keywords, relative path, etc
    'create_dir_stack', # analyse the branches to create an extract sequence
    'extract_src',      # use the sequence to extract source to destination
    'write_cfg',        # generate final configuration file
    'write_cfg_bld',    # generate build configuration file
  );

  for my $method (@methods) {
    $rc = $self->$method if $rc;
  }

  return $rc;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->invoke_mirror ();
#
# DESCRIPTION
#   This method invokes the mirror stage of the extract system. It returns
#   true on success.
# ------------------------------------------------------------------------------

sub invoke_mirror {
  my $self = shift;
  return $self->rdest->mirror ([qw/bldcfg extcfg srcdir/]);
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->invoke_system ();
#
# DESCRIPTION
#   This method invokes the extract system. It returns true on success.
# ------------------------------------------------------------------------------

sub invoke_system {
  my $self = shift;

  my $rc = 1;
  
  $rc = $self->invoke_stage ('Extract', 'invoke_extract');
  $rc = $self->invoke_stage ('Mirror', 'invoke_mirror')
    if $rc and $self->rdest->rootdir;

  return $rc;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->parse_cfg_rdest(\@cfg_lines);
#
# DESCRIPTION
#   This method parses the remote destination settings in the @cfg_lines.
# ------------------------------------------------------------------------------

sub parse_cfg_rdest {
  my ($self, $cfg_lines_ref) = @_;

  # RDEST declarations
  # ----------------------------------------------------------------------------
  for my $line (grep {$_->slabel_starts_with_cfg('RDEST')} @{$cfg_lines_ref}) {
    my ($d, $method) = map {lc($_)} $line->slabel_fields();
    $method ||= 'rootdir';
    if ($self->rdest()->can($method)) {
      $self->rdest()->$method(expand_tilde($line->value()));
      $line->parsed(1);
    }
  }

  # MIRROR declaration, deprecated = RDEST::MIRROR_CMD
  # ----------------------------------------------------------------------------
  for my $line (grep {$_->slabel_starts_with_cfg('MIRROR')} @{$cfg_lines_ref}) {
    $self->rdest()->mirror_cmd($line->value());
    $line->parsed(1);
  }

  return 1;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->parse_cfg_bld (\@cfg_lines);
#
# DESCRIPTION
#   This method parses the build configurations in the @cfg_lines.
# ------------------------------------------------------------------------------

sub parse_cfg_bld {
  my ($self, $cfg_lines) = @_;

  # BLD declarations
  # ----------------------------------------------------------------------------
  for my $line (grep {$_->slabel_starts_with_cfg ('BDECLARE')} @$cfg_lines) {
    # Remove BLD from label
    my @words = $line->slabel_fields;

    # Check that a declaration follows BLD
    next if @words <= 1;

    push @{ $self->bdeclare }, FCM1::CfgLine->new (
      LABEL  => join ($FCM1::Config::DELIMITER, @words),
      PREFIX => $self->cfglabel ('BDECLARE'),
      VALUE  => $line->value,
    );
    $line->parsed (1);
  }

  return 1;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->parse_cfg_conflict (\@cfg_lines);
#
# DESCRIPTION
#   This method parses the conflict settings in the @cfg_lines.
# ------------------------------------------------------------------------------

sub parse_cfg_conflict {
  my ($self, $cfg_lines) = @_;

  # Deprecated: Override mode setting
  # ----------------------------------------------------------------------------
  for my $line (grep {$_->slabel_starts_with_cfg ('OVERRIDE')} @$cfg_lines) {
    next if ($line->slabel_fields) > 1;
    $self->conflict ($line->bvalue ? 'override' : 'fail');
    $line->parsed (1);
    $line->warning($line->slabel . ' is deprecated. Use ' .
                   $line->cfglabel('CONFLICT') . ' override|merge|fail.');
  }

  # Conflict mode setting
  # ----------------------------------------------------------------------------
  my @conflict_modes = qw/fail merge override/;
  my $conflict_modes_pattern = join ('|', @conflict_modes);
  for my $line (grep {$_->slabel_starts_with_cfg ('CONFLICT')} @$cfg_lines) {
    if ($line->value =~ /$conflict_modes_pattern/i) {
      $self->conflict (lc ($line->value));
      $line->parsed (1);

    } elsif ($line->value =~ /^[012]$/) {
      $self->conflict ($conflict_modes[$line->value]);
      $line->parsed (1);

    } else {
      $line->error ($line->value, ': invalid value');
    }
  }

  return 1;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->parse_cfg_project (\@cfg_lines);
#
# DESCRIPTION
#   This method parses the project settings in the @cfg_lines.
# ------------------------------------------------------------------------------

sub parse_cfg_project {
  my ($self, $cfg_lines) = @_;

  # Flag to indicate that a declared branch revision must match with its changed
  # revision
  # ----------------------------------------------------------------------------
  for my $line (grep {$_->slabel_starts_with_cfg ('REVMATCH')} @$cfg_lines) {
    next if ($line->slabel_fields) > 1;
    $self->setting ([qw/EXT_REVMATCH/], $line->bvalue);
    $line->parsed (1);
  }

  # Repository, revision and source directories
  # ----------------------------------------------------------------------------
  for my $name (qw/repos revision dirs expdirs/) {
    my @lines = grep {
      $_->slabel_starts_with_cfg (uc ($name)) or
      $name eq 'revision' and $_->slabel_starts_with_cfg ('VERSION');
    } @$cfg_lines;
    for my $line (@lines) {
      my @names = $line->slabel_fields;
      shift @names;

      # Detemine package and tag
      my $tag     = pop @names;
      my $pckroot = $names[0];
      my $pck     = join ($FCM1::Config::DELIMITER, @names);

      # Check that $tag and $pckroot are defined
      next unless $tag and $pckroot;

      # Check if branch already exists.
      # If so, set $branch to point to existing branch
      my $branch = undef;
      for (@{ $self->branches }) {
        next unless $_->package eq $pckroot and $_->tag eq $tag;

        $branch = $_;
        last;
      }

      # Otherwise, create a new branch
      if (not $branch) {
        $branch = FCM1::ReposBranch->new (PACKAGE => $pckroot, TAG => $tag,);

        push @{ $self->branches }, $branch;
      }

      if ($name eq 'repos' or $name eq 'revision') {
        # Branch location or revision
        $branch->$name ($line->value);

      } else { # $name eq 'dirs' or $name eq 'expdirs'
        # Source directory or expandable source directory
        if ($pck eq $pckroot and $line->value !~ m#^/#) {
          # Sub-package name not set and source directory quoted as a relative
          # path, determine package name from path name
          $pck = join (
            $FCM1::Config::DELIMITER,
            ($pckroot, File::Spec->splitdir ($line->value)),
          );
        }

        # A "/" is equivalent to the top (empty) package
        my $value = ($line->value =~ m#^/+$#) ? '' : $line->value;
        $branch->$name ($pck, $value);
      }

      $line->parsed (1);
    }
  }

  return 1;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $obj->expand_cfg ();
#
# DESCRIPTION
#   This method expands the settings of the extract configuration.
# ------------------------------------------------------------------------------

sub expand_cfg {
  my $self = shift;

  my $rc = 1;
  for my $use (@{ $self->inherit }) {
    $rc = $use->expand_cfg if $rc;
  }

  return $rc unless $rc;

  # Establish a set of source directories from the "base repository"
  my %base_branches = ();

  # Inherit "base" set of source directories from re-used extracts
  for my $use (@{ $self->inherit }) {
    my @branches = @{ $use->branches };

    for my $branch (@branches) {
      my $package              = $branch->package;
      $base_branches{$package} = $branch unless exists $base_branches{$package};
    }
  }

  for my $branch (@{ $self->branches }) {
    # Expand URL keywords if necessary
    if ($branch->repos) {
      my $repos = FCM1::Util::tidy_url(FCM1::Keyword::expand($branch->repos()));
      $branch->repos ($repos) if $repos ne $branch->repos;
    }

    # Check that repository type and revision are set
    if ($branch->repos and &is_url ($branch->repos)) {
      $branch->type ('svn') unless $branch->type;
      $branch->revision ('head') unless $branch->revision;

    } else {
      $branch->type ('user') unless $branch->type;
      $branch->revision ('user') unless $branch->revision;
    }

    $rc = $branch->expand_revision if $rc; # Get revision number from keywords
    $rc = $branch->expand_path     if $rc; # Expand relative path to full path
    $rc = $branch->expand_all      if $rc; # Search sub-directories
    last unless $rc;

    my $package = $branch->package;

    if (exists $base_branches{$package}) {
      # A base branch for this package exists

      # If current branch has no source directory, use the set provided by the
      # base branch
      my %dirs = %{ $branch->dirs };
      $branch->add_base_dirs ($base_branches{$package}) unless keys %dirs;

    } else {
      # This package does not yet have a base branch, set this branch as base
      $base_branches{$package} = $branch;
    }
  }

  return $rc;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $obj->create_dir_stack ();
#
# DESCRIPTION
#   This method creates a hash of source directories to be processed. If the
#   flag INHERITED is set to true, the source directories are assumed processed
#   and extracted.
# ------------------------------------------------------------------------------

sub create_dir_stack {
  my $self = shift;
  my %args = @_;

  # Inherit from USE ext cfg
  for my $use (@{ $self->inherit }) {
    $use->create_dir_stack () or return 0;
    my %use_srcdirs = %{ $use->srcdirs };

    while (my ($key, $value) = each %use_srcdirs) {
      $self->srcdirs ($key, $value);

      # Re-set destination to current destination
      my @path = split (/$FCM1::Config::DELIMITER/, $key);
      $self->srcdirs ($key)->{DEST} = File::Spec->catfile (
        $self->dest->srcdir, @path,
      );
    }
  }

  # Build stack from current ext cfg
  for my $branch (@{ $self->branches }) {
    my %branch_dirs = %{ $branch->dirs };

    for my $dir (keys %branch_dirs) {
      # Check whether source directory is already in the list
      if (not $self->srcdirs ($dir)) { # if not, create it
        $self->srcdirs ($dir, {
          DEST  => File::Spec->catfile (
            $self->dest->srcdir, split (/$FCM1::Config::DELIMITER/, $dir)
          ),
          STACK => [],
          FILES => {},
        });
      }

      my $stack = $self->srcdirs ($dir)->{STACK}; # copy reference

      # Create a new layer in the input stack
      my $layer = FCM1::SrcDirLayer->new (
        NAME      => $dir,
        PACKAGE   => $branch->package,
        TAG       => $branch->tag,
        LOCATION  => $branch->dirs ($dir),
        REPOSROOT => $branch->repos,
        REVISION  => $branch->revision,
        TYPE      => $branch->type,
        EXTRACTED => @{ $self->inherited }
                     ? $self->srcdirs ($dir)->{DEST} : undef,
      );

      # Check whether layer is already in the stack
      my $exist = grep {
        $_->location eq $layer->location and $_->revision eq $layer->revision;
      } @{ $stack };

      if (not $exist) {
        # If not already exist, put layer into stack

        # Note: user stack always comes last
        if (! $layer->user and exists $stack->[-1] and $stack->[-1]->user) {
          my $lastlayer = pop @{ $stack };
          push @{ $stack }, $layer;
          $layer = $lastlayer;
        }

        push @{ $stack }, $layer;

      } elsif ($layer->user) {

        # User layer already exists, overwrite it
        $stack->[-1] = $layer;

      }
    }
  }

  # Use the cache to sort the source directory layer hash
  return $self->compare_setting (METHOD_LIST => ['sort_dir_stack']);
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   ($rc, \@new_lines) = $self->sort_dir_stack ($old_lines);
#
# DESCRIPTION
#   This method sorts thesource directories hash to be processed.
# ------------------------------------------------------------------------------

sub sort_dir_stack {
  my ($self, $old_lines) = @_;

  my $rc = 0;

  my %old = ();
  if ($old_lines) {
    for my $line (@$old_lines) {
      $old{$line->label} = $line->value;
    }
  }

  my %new;

  # Compare each layer to base layer, discard unnecessary layers
  DIR: for my $srcdir (keys %{ $self->srcdirs }) {
    my @stack = ();

    while (my $layer = shift @{ $self->srcdirs ($srcdir)->{STACK} }) {
      if ($layer->user) {
        # Local file system branch, check that the declared location exists
        if (-d $layer->location) {
          # Local file system branch always takes precedence
          push @stack, $layer;

        } else {
          w_report 'ERROR: ', $layer->location, ': declared source directory ',
                   'does not exists ';
          $rc = undef;
          last DIR;
        }

      } else {
        my $key = join ($FCM1::Config::DELIMITER, (
          $srcdir, $layer->location, $layer->revision
        ));

        unless ($layer->extracted and $layer->commit) {
          # See if commit revision information is cached
          if (keys %old and exists $old{$key}) {
            $layer->commit ($old{$key});

          } else {
            $layer->get_commit;
            $rc = 1;
          }

          # Check source directory for commit revision, if necessary
          if (not $layer->commit) {
            w_report 'Error: cannot determine the last changed revision of ',
                     $layer->location;
            $rc = undef;
            last DIR;
          }

          # Set cache directory for layer
          my $tag_ver = $layer->tag . '__' . $layer->commit;
          $layer->cachedir (File::Spec->catfile (
            $self->dest->cachedir,
            split (/$FCM1::Config::DELIMITER/, $srcdir),
            $tag_ver,
          ));
        }

        # New line in cache config file
        $new{$key} = $layer->commit;

        # Push this layer in the stack:
        # 1. it has a different revision compared to the top layer
        # 2. it is the top layer (base line code)
        if (@stack > 0) {
          push @stack, $layer if $layer->commit != $stack[0]->commit;

        } else {
          push @stack, $layer;
        }

      }
    }

    $self->srcdirs ($srcdir)->{STACK} = \@stack;
  }

  # Write "commit cache" file
  my @new_lines;
  if (defined ($rc)) {
    for my $key (sort keys %new) {
      push @new_lines, FCM1::CfgLine->new (label => $key, value => $new{$key});
    }
  }

  return ($rc, \@new_lines);
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->extract_src ();
#
# DESCRIPTION
#   This internal method performs the extract of the source directories and
#   files if necessary.
# ------------------------------------------------------------------------------

sub extract_src {
  my $self = shift;
  my $rc = 1;

  # Ensure destinations exist and are directories
  for my $srcdir (values %{ $self->srcdirs }) {
    last if not $rc;
    if (-f $srcdir->{DEST}) {
      w_report $srcdir->{DEST},
               ': destination exists and is not a directory, abort.';
      $rc = 0;
    }
  }

  # Retrieve previous/record current extract configuration for each file.
  $rc = $self->compare_setting (
    CACHEBASE => $self->setting ('CACHE_FILE_SRC'),
    METHOD_LIST => ['compare_setting_srcfiles'],
  ) if $rc;

  return $rc;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   ($rc, \@new_lines) = $self->compare_setting_srcfiles ($old_lines);
#
# DESCRIPTION
#   For each file to be extracted, this method creates an instance of an
#   FCM1::ExtractFile object. It then compares its file's sources to determine
#   if they have changed. If so, it will allow the FCM1::ExtractFile to
#   "re-extract" the file to the destination. Otherwise, it will set
#   FCM1::ExtractFile->dest_status to a null string to denote an "unchanged"
#   dest_status.
#
# SEE ALSO
#   FCM1::ConfigSystem->compare_setting.
# ------------------------------------------------------------------------------

sub compare_setting_srcfiles {
  my ($self, $old_lines) = @_;
  my $rc = 1;

  # Retrieve previous extract configuration for each file
  # ----------------------------------------------------------------------------
  my %old = ();
  if ($old_lines) {
    for my $line (@$old_lines) {
      $old{$line->label} = $line->value;
    }
  }

  # Build up a sequence using a FCM1::ExtractFile object for each file
  # ----------------------------------------------------------------------------
  for my $srcdir (values %{ $self->srcdirs }) {
    my %pkgnames0; # (to be) list of package names in the base layer
    for my $i (0 .. @{ $srcdir->{STACK} } - 1) {
      my $layer = $srcdir->{STACK}->[$i];
      # Update the cache for each layer of the stack if necessary
      $layer->update_cache unless $layer->extracted or -d $layer->localdir;

      # Get list of files in the cache or local directory
      my %pkgnames;
      for my $file (($layer->get_files)) {
        my $pkgname = join (
          '/', split (/$FCM1::Config::DELIMITER/, $layer->name), $file
        );
        $pkgnames0{$pkgname} = 1 if $i == 0; # store package name in base layer
        $pkgnames{$pkgname} = 1; # store package name in the current layer
        if (not $self->files ($pkgname)) {
          $self->files ($pkgname, FCM1::ExtractFile->new (
            conflict => $self->conflict,
            dest     => $self->dest->srcpath,
            pkgname  => $pkgname,
          ));

          # Base is empty
          $self->files ($pkgname)->src->[0] = FCM1::ExtractSrc->new (
            id      => $layer->tag,
            pkgname => $pkgname,
          ) if $i > 0;
        }
        my $cache = File::Spec->catfile ($layer->localdir, $file);
        push @{ $self->files ($pkgname)->src }, FCM1::ExtractSrc->new (
          cache   => $cache,
          id      => $layer->tag,
          pkgname => $pkgname,
          rev     => ($layer->user ? (stat ($cache))[9] : $layer->commit),
          uri     => join ('/', $layer->location, $file),
        );
      }

      # List of removed files in this layer (relative to base layer)
      if ($i > 0) {
        for my $pkgname (keys %pkgnames0) {
          push @{ $self->files ($pkgname)->src }, FCM1::ExtractSrc->new (
            id      => $layer->tag,
            pkgname => $pkgname,
          ) if not exists $pkgnames{$pkgname}
        }
      }
    }
  }

  # Compare with old settings
  # ----------------------------------------------------------------------------
  my %new = ();
  for my $key (sort keys %{ $self->files }) {
    # Set up value for cache
    my @sources = ();
    for my $src (@{ $self->files ($key)->src }) {
      push @sources, (defined ($src->uri) ? ($src->uri . '@' . $src->rev) : '');
    }

    my $value = join ($FCM1::Config::DELIMITER, @sources);

    # Set FCM1::ExtractFile->dest_status to "unchanged" if value is unchanged
    if (exists($old{$key}) && $old{$key} eq $value && !grep {!$_} @sources) {
      $self->files($key)->dest_status('');
    }

    # Write current settings
    $new{$key} = $value;
  }

  # Delete those that exist in previous extract but not in current
  # ----------------------------------------------------------------------------
  for my $key (sort keys %old) {
    next if exists $new{$key};
    $self->files ($key, FCM1::ExtractFile->new (
      dest    => $self->dest->srcpath,
      pkgname => $key,
    ));
  }

  # Extract each file, if necessary
  # ----------------------------------------------------------------------------
  for my $key (sort keys %{ $self->files }) {
    $rc = $self->files ($key)->run if defined ($rc);
    last if not defined ($rc);
  }

  # Report status
  # ----------------------------------------------------------------------------
  if (defined ($rc) and $self->verbose) {
    my %src_status_count = ();
    my %dest_status_count = ();
    for my $key (sort keys %{ $self->files }) {
      # Report changes in destination in verbose 1 or above
      my $dest_status = $self->files ($key)->dest_status;
      my $src_status = $self->files ($key)->src_status;
      next unless $self->verbose and $dest_status;

      if ($dest_status and $dest_status ne '?') {
        if (exists $dest_status_count{$dest_status}) {
          $dest_status_count{$dest_status}++;

        } else {
          $dest_status_count{$dest_status} = 1;
        }
      }

      if ($src_status and $src_status ne '?') {
        if (exists $src_status_count{$src_status}) {
          $src_status_count{$src_status}++;

        } else {
          $src_status_count{$src_status} = 1;
        }
      }

      # Destination status in column 1, source status in column 2
      if ($self->verbose > 1) {
        my @srcs = @{$self->files ($key)->src_actual};
        print ($dest_status ? $dest_status : ' ');
        print ($src_status ? $src_status : ' ');
        print ' ' x 5, $key;
        print ' (', join (', ', map {$_->id} @srcs), ')' if @srcs;
        print "\n";
      }
    }

    # Report number of files in each dest_status category
    if (%dest_status_count) {
      print 'Column 1: ' if $self->verbose > 1;
      print 'Destination status summary:', "\n";
      for my $key (sort keys %FCM1::ExtractFile::DEST_STATUS_CODE) {
        next unless $key;
        next if not exists ($dest_status_count{$key});
        print '  No of files ';
        print '[', $key, '] ' if $self->verbose > 1;
        print $FCM1::ExtractFile::DEST_STATUS_CODE{$key}, ': ',
              $dest_status_count{$key}, "\n";
      }
    }

    # Report number of files in each dest_status category
    if (%src_status_count) {
      print 'Column 2: ' if $self->verbose > 1;
      print 'Source status summary:', "\n";
      for my $key (sort keys %FCM1::ExtractFile::SRC_STATUS_CODE) {
        next unless $key;
        next if not exists ($src_status_count{$key});
        print '  No of files ';
        print '[', $key, '] ' if $self->verbose > 1;
        print $FCM1::ExtractFile::SRC_STATUS_CODE{$key}, ': ',
              $src_status_count{$key}, "\n";
      }
    }
  }

  # Record configuration of current extract for each file
  # ----------------------------------------------------------------------------
  my @new_lines;
  if (defined ($rc)) {
    for my $key (sort keys %new) {
      push @new_lines, FCM1::CfgLine->new (label => $key, value => $new{$key});
    }
  }

  return ($rc, \@new_lines);
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   @array = $self->sort_bdeclare ();
#
# DESCRIPTION
#   This method returns sorted build declarations, filtering out repeated
#   entries, where possible.
# ------------------------------------------------------------------------------

sub sort_bdeclare {
  my $self = shift;

  # Get list of build configuration labels that can be declared multiple times
  my %cfg_keyword = map {
    ($self->cfglabel ($_), 1)
  } split (/$FCM1::Config::DELIMITER_LIST/, $self->setting ('CFG_KEYWORD'));

  my @bdeclares = ();
  for my $d (reverse @{ $self->bdeclare }) {
    # Reconstruct array from bottom up
    # * always add declarations that can be declared multiple times
    # * otherwise add only if it is declared below
    unshift @bdeclares, $d
      if exists $cfg_keyword{uc (($d->slabel_fields)[0])} or
         not grep {$_->slabel eq $d->slabel} @bdeclares;
  }

  return (sort {$a->slabel cmp $b->slabel} @bdeclares);
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   @cfglines = $obj->to_cfglines ();
#
# DESCRIPTION
#   See description of FCM1::ConfigSystem->to_cfglines for further information.
# ------------------------------------------------------------------------------

sub to_cfglines {
  my ($self) = @_;

  return (
    FCM1::ConfigSystem::to_cfglines($self),

    $self->rdest->to_cfglines (),
    FCM1::CfgLine->new (),

    @{ $self->bdeclare } ? (
      FCM1::CfgLine::comment_block ('Build declarations'),
      map {
        FCM1::CfgLine->new (label => $_->label, value => $_->value)
      } ($self->sort_bdeclare),
      FCM1::CfgLine->new (),
    ) : (),

    FCM1::CfgLine::comment_block ('Project and branches'),
    (map {($_->to_cfglines ())} @{ $self->branches }),

    ($self->conflict ne 'merge') ? (
      FCM1::CfgLine->new (
        label => $self->cfglabel ('CONFLICT'), value => $self->conflict,
      ),
      FCM1::CfgLine->new (),
    ) : (),
  );
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   @cfglines = $obj->to_cfglines_bld ();
#
# DESCRIPTION
#   Returns a list of configuration lines of the current extract suitable for
#   feeding into the build system.
# ------------------------------------------------------------------------------

sub to_cfglines_bld {
  my ($self) = @_;

  my $dest = $self->rdest->rootdir ? 'rdest' : 'dest';
  my $root = File::Spec->catfile ('$HERE', '..');

  my @inherits;
  my @no_inherits;
  if (@{ $self->inherit }) {
    # List of inherited builds
    for (@{ $self->inherit }) {
      push @inherits, FCM1::CfgLine->new (
        label => $self->cfglabel ('USE'), value => $_->$dest->rootdir
      );
    }

    # List of files that should not be inherited
    for my $key (sort keys %{ $self->files }) {
      next unless $self->files ($key)->dest_status eq 'd';
      my $label = join ('::', (
        $self->cfglabel ('INHERIT'),
        $self->cfglabel ('FILE'),
        split (m#/#, $self->files ($key)->pkgname),
      ));
      push @no_inherits, FCM1::CfgLine->new (label => $label, value => 'false');
    }
  }

  return (
    FCM1::CfgLine::comment_block ('File header'),
    (map
      {my ($lbl, $val) = @{$_}; FCM1::CfgLine->new(label => $lbl, value => $val)}
      (
        [$self->cfglabel('CFGFILE') . $FCM1::Config::DELIMITER . 'TYPE'   , 'bld'],
        [$self->cfglabel('CFGFILE') . $FCM1::Config::DELIMITER . 'VERSION', '1.0'],
        [],
      )
    ),

    @{ $self->inherit } ? (
      @inherits,
      @no_inherits,
      FCM1::CfgLine->new (),
    ) : (),

    FCM1::CfgLine::comment_block ('Destination'),
    FCM1::CfgLine->new (label => $self->cfglabel ('DEST'), value => $root),
    FCM1::CfgLine->new (),

    @{ $self->bdeclare } ? (
      FCM1::CfgLine::comment_block ('Build declarations'),
      map {
        FCM1::CfgLine->new (label => $_->slabel, value => $_->value)
      } ($self->sort_bdeclare),
      FCM1::CfgLine->new (),
    ) : (),
  );
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->write_cfg ();
#
# DESCRIPTION
#   This method writes the configuration file at the end of the run. It calls
#   $self->write_cfg_system ($cfg) to write any system specific settings.
# ------------------------------------------------------------------------------

sub write_cfg {
  my $self = shift;

  my $cfg = FCM1::CfgFile->new (TYPE => $self->type);
  $cfg->lines ([$self->to_cfglines()]);
  $cfg->print_cfg ($self->dest->extcfg);

  return 1;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $self->write_cfg_bld ();
#
# DESCRIPTION
#   This internal method writes the build configuration file.
# ------------------------------------------------------------------------------

sub write_cfg_bld {
  my $self = shift;

  my $cfg = FCM1::CfgFile->new (TYPE => 'bld');
  $cfg->lines ([$self->to_cfglines_bld()]);
  $cfg->print_cfg ($self->dest->bldcfg);

  return 1;
}

# ------------------------------------------------------------------------------

1;

__END__