<commit_before># Copyright 2000-2004 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package Apache::Build;

use 5.006;
use strict;
use warnings;

use Config;
use Cwd ();
use File::Spec::Functions qw(catfile catdir canonpath rel2abs devnull);
use File::Basename;
use ExtUtils::Embed ();

use constant IS_MOD_PERL_BUILD => grep { -e "$_/lib/mod_perl.pm" } qw(. ..);

use constant AIX    => $^O eq 'aix';
use constant DARWIN => $^O eq 'darwin';
use constant HPUX   => $^O eq 'hpux';
use constant OPENBSD => $^O eq 'openbsd';
use constant WIN32  => $^O eq 'MSWin32';

use constant MSVC => WIN32() && ($Config{cc} eq 'cl');

use constant REQUIRE_ITHREADS => grep { $^O eq $_ } qw(MSWin32);
use constant PERL_HAS_ITHREADS =>
    $Config{useithreads} && ($Config{useithreads} eq 'define');

use ModPerl::Code ();
use ModPerl::BuildOptions ();
use Apache::TestTrace;
use Apache::TestConfig ();

our $VERSION = '0.01';
our $AUTOLOAD;

sub AUTOLOAD {
    my $self = shift;
    my $name = uc ((split '::', $AUTOLOAD)[-1]);
    unless ($name =~ /^MP_/) {
        die "no such method: $AUTOLOAD";
    }
    unless ($self->{$name}) {
        return wantarray ? () : undef;
    }
    return wantarray ? (split /\s+/, $self->{$name}) : $self->{$name};
}

#--- apxs stuff ---

our $APXS;

my %apxs_query = (
    INCLUDEDIR => 'include',
    LIBEXECDIR => 'modules',
    CFLAGS     => undef,
    PREFIX     => '',
);

sub ap_prefix_invalid {
    my $self = shift;

    my $prefix = $self->{MP_AP_PREFIX};

    unless (-d $prefix) {
        return "$prefix: No such file or directory";
    }

    my $include_dir = $self->apxs(-q => 'INCLUDEDIR');

    unless (-e $include_dir) {
        return "include/ directory not found in $prefix";
    }

    return '';
}

sub httpd_is_source_tree {
    my $self = shift;

    return $self->{httpd_is_source_tree}
        if exists $self->{httpd_is_source_tree};

    my $prefix = $self->dir;
    $self->{httpd_is_source_tree} = 
        defined $prefix && -d $prefix && -e "$prefix/CHANGES";
}

sub apxs {
    my $self = shift;

    my $is_query = (@_ == 2) && ($_[0] eq '-q');

    $self = $self->build_config unless ref $self;

    my $query_key;
    if ($is_query) {
        $query_key = 'APXS_' . uc $_[1];
        if ($self->{$query_key}) {
            return $self->{$query_key};
        }
    }

    my $apxs;
    my @trys = ($Apache::Build::APXS,
                $self->{MP_APXS},
                $ENV{MP_APXS},
                catfile $self->{MP_AP_PREFIX}, 'bin', 'apxs');

    if (WIN32) {
        my $ext = '.bat';
        for (@trys) {
            $_ .= $ext if ($_ and $_ !~ /$ext$/);
        }
    }

    unless (IS_MOD_PERL_BUILD) {
        #if we are building mod_perl via apxs, apxs should already be known
        #these extra tries are for things built outside of mod_perl
        #e.g. libapreq
        # XXX: this may pick a wrong apxs version!
        push @trys,
        Apache::TestConfig::which('apxs'),
        '/usr/local/apache/bin/apxs';
    }

    for (@trys) {
        next unless ($apxs = $_);
        chomp $apxs;
        last if -x $apxs;
    }

    unless ($apxs and -x $apxs) {
        my $prefix = $self->{MP_AP_PREFIX} || "";
        return '' unless -d $prefix and $is_query;
        my $val = $apxs_query{$_[1]};
        return defined $val ? ($val ? "$prefix/$val" : $prefix) : "";
    }

    my $devnull = devnull();
    my $val = qx($apxs @_ 2>$devnull);
    chomp $val if defined $val; # apxs post-2.0.40 adds a new line

    unless ($val) {
        error "'$apxs @_' failed:";

        if (my $error = qx($apxs @_ 2>&1)) {
            error $error;
        }
        else {<commit_msg>abstract the code looking for apxs to its own function and call it only once, and not on every apxs query<commit_after># Copyright 2000-2004 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package Apache::Build;

use 5.006;
use strict;
use warnings;

use Config;
use Cwd ();
use File::Spec::Functions qw(catfile catdir canonpath rel2abs devnull);
use File::Basename;
use ExtUtils::Embed ();

use constant IS_MOD_PERL_BUILD => grep { -e "$_/lib/mod_perl.pm" } qw(. ..);

use constant AIX    => $^O eq 'aix';
use constant DARWIN => $^O eq 'darwin';
use constant HPUX   => $^O eq 'hpux';
use constant OPENBSD => $^O eq 'openbsd';
use constant WIN32  => $^O eq 'MSWin32';

use constant MSVC => WIN32() && ($Config{cc} eq 'cl');

use constant REQUIRE_ITHREADS => grep { $^O eq $_ } qw(MSWin32);
use constant PERL_HAS_ITHREADS =>
    $Config{useithreads} && ($Config{useithreads} eq 'define');

use ModPerl::Code ();
use ModPerl::BuildOptions ();
use Apache::TestTrace;
use Apache::TestConfig ();

our $VERSION = '0.01';
our $AUTOLOAD;

sub AUTOLOAD {
    my $self = shift;
    my $name = uc ((split '::', $AUTOLOAD)[-1]);
    unless ($name =~ /^MP_/) {
        die "no such method: $AUTOLOAD";
    }
    unless ($self->{$name}) {
        return wantarray ? () : undef;
    }
    return wantarray ? (split /\s+/, $self->{$name}) : $self->{$name};
}

#--- apxs stuff ---

our $APXS;

my %apxs_query = (
    INCLUDEDIR => 'include',
    LIBEXECDIR => 'modules',
    CFLAGS     => undef,
    PREFIX     => '',
);

sub ap_prefix_invalid {
    my $self = shift;

    my $prefix = $self->{MP_AP_PREFIX};

    unless (-d $prefix) {
        return "$prefix: No such file or directory";
    }

    my $include_dir = $self->apxs(-q => 'INCLUDEDIR');

    unless (-e $include_dir) {
        return "include/ directory not found in $prefix";
    }

    return '';
}

sub httpd_is_source_tree {
    my $self = shift;

    return $self->{httpd_is_source_tree}
        if exists $self->{httpd_is_source_tree};

    my $prefix = $self->dir;
    $self->{httpd_is_source_tree} = 
        defined $prefix && -d $prefix && -e "$prefix/CHANGES";
}

# try to find the apxs utility, set $apxs to the path if found,
# otherwise to ''
my $apxs; # undef so we know we haven't tried to set it yet
sub find_apxs_util {
    my $self = shift;

    $apxs = ''; # not found

    my @trys = ($Apache::Build::APXS,
                $self->{MP_APXS},
                $ENV{MP_APXS},
                catfile $self->{MP_AP_PREFIX}, 'bin', 'apxs');

    if (WIN32) {
        my $ext = '.bat';
        for (@trys) {
            $_ .= $ext if ($_ and $_ !~ /$ext$/);
        }
    }

    unless (IS_MOD_PERL_BUILD) {
        #if we are building mod_perl via apxs, apxs should already be known
        #these extra tries are for things built outside of mod_perl
        #e.g. libapreq
        # XXX: this may pick a wrong apxs version!
        push @trys,
        Apache::TestConfig::which('apxs'),
        '/usr/local/apache/bin/apxs';
    }

    my $apxs_try;
    for (@trys) {
        next unless ($apxs_try = $_);
        chomp $apxs_try;
        if (-x $apxs_try) {
            $apxs = $apxs_try;
            last;
        }
    }
}

sub apxs {
    my $self = shift;

    $self->find_apxs_util() unless defined $apxs;

    my $is_query = (@_ == 2) && ($_[0] eq '-q');

    $self = $self->build_config unless ref $self;

    my $query_key;
    if ($is_query) {
        $query_key = 'APXS_' . uc $_[1];
        if ($self->{$query_key}) {
            return $self->{$query_key};
        }
    }

    unless ($apxs) {
        my $prefix = $self->{MP_AP_PREFIX} || "";
        return '' unless -d $prefix and $is_query;
        my $val = $apxs_query{$_[1]};
        return defined $val ? ($val ? "$prefix/$val" : $prefix) : "";
    }

    my $devnull = devnull();
    my $val = qx($apxs @_ 2>$devnull);
    chomp $val if defined $val; # apxs post-2.0.40 adds a new line

    unless ($val) {
        error "'$apxs @_' failed:";

        if (my $error = qx($apxs @_ 2>&1)) {
            error $error;
        }
        else {
{'fraction_non_alphanumeric': 0.15312855517633675, 'fraction_numerical': 0.00955631399317406, 'mean_word_length': 2.52485966319166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '614722', 'n_tokens_mistral': 3115, 'n_tokens_neox': 2972, 'n_words': 913}