<commit_before>#############################################################################
##
## Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
## Contact: http://www.qt-project.org/
##
## This file is part of the Quality Assurance module of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:LGPL$
## GNU Lesser General Public License Usage
## This file may be used under the terms of the GNU Lesser General Public
## License version 2.1 as published by the Free Software Foundation and
## appearing in the file LICENSE.LGPL included in the packaging of this
## file. Please review the following information to ensure the GNU Lesser
## General Public License version 2.1 requirements will be met:
## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
##
## In addition, as a special exception, Nokia gives you certain additional
## rights. These rights are described in the Nokia Qt LGPL Exception
## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU General
## Public License version 3.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of this
## file. Please review the following information to ensure the GNU General
## Public License version 3.0 requirements will be met:
## http://www.gnu.org/copyleft/gpl.html.
##
## Other Usage
## Alternatively, this file may be used in accordance with the terms and
## conditions contained in a signed written agreement between you and Nokia.
##
##
##
##
##
##
## $QT_END_LICENSE$
##
#############################################################################

package QtQA::QMake::Project;
use strict;
use warnings;

use Carp;
use English qw(-no_match_vars);
use File::chdir;
use File::Basename;
use File::Temp;
use Getopt::Long qw(GetOptions);
use IO::File;
use Readonly;
use ReleaseAction qw(on_release);
use List::MoreUtils qw(apply);
use Text::ParseWords;

use QtQA::QMake::Project::LazyValue;

Readonly my $WINDOWS => ($OSNAME =~ m{win32}i);

# Magic string denoting we've deliberately exited qmake early
Readonly my $MAGIC_QMAKE_EXIT_STRING => __PACKAGE__.':EXITING';

sub new
{
    my ($class, $makefile) = @_;

    my $self = bless {
        _die_on_error => 1, # whether to die when an error occurs
        _qmake_count => 0,  # number of times qmake has been run (for testing)
    }, $class;

    if ($makefile) {
        $self->set_makefile( $makefile );
    }

    $self->set_make( $self->_default_make( ) );

    return $self;
}

sub set_makefile
{
    my ($self, $makefile) = @_;

    $self->{ _makefile } = $makefile;

    # Makefile changed, so everything needs to be resolved again.
    $self->{ _resolved } = {};

    return;
}

sub makefile
{
    my ($self) = @_;
    return $self->{ _makefile };
}

sub set_make
{
    my ($self, $make) = @_;

    $self->{ _make } = $make;

    return;
}

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

    return $self->{ _make };
}

# Returns a reasonable default make command based on the platform.
sub _default_make
{
    my ($self) = @_;

    if ($WINDOWS) {
        return 'nmake';
    }

    return 'make';
}

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

    return $self->{ _die_on_error };
}

sub set_die_on_error
{
    my ($self, $value) = @_;

    $self->{ _die_on_error } = $value;
    return;
}

sub _prepare_variable
{
    my ($self, @variable) = @_;

    foreach my $variable (@variable) {
        $self->{ _to_resolve }{ variable }{ $variable } = 1;
    }

    return;
}

sub _prepare_test
{
    my ($self, @test) = @_;

    foreach my $test (@test) {
        $self->{ _to_resolve }{ test }{ $test } = 1;
    }

    return;
}

sub _qx_or_croak
{
    my ($self, $cmd) = @_;

    my $output = qx($cmd);
    if ($? != 0) {
        # If $output contains this magic, we deliberately exited, and the status can
        # be ignored.
        if ($output !~ m/\Q$MAGIC_QMAKE_EXIT_STRING\E/) {
            croak __PACKAGE__.": command `$cmd' exited with status $?, output follows:\n$output\n";
        }
        return $output;
    }

    return $output;
}

# Returns a copy of %ENV with any make-related environment variables removed.
sub _make_cleaned_env
{
    my ($self) = @_;

    my %out = %ENV;

    delete @out{qw(
        MAKEFLAGS
        MAKELEVEL
        MFLAGS
    )};

    return %out;
}

# Returns the qmake command (as a single string) used to generate the given makefile.<commit_msg>QtQA::QMake::Project: output $CWD when a command fails.<commit_after>#############################################################################
##
## Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
## Contact: http://www.qt-project.org/
##
## This file is part of the Quality Assurance module of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:LGPL$
## GNU Lesser General Public License Usage
## This file may be used under the terms of the GNU Lesser General Public
## License version 2.1 as published by the Free Software Foundation and
## appearing in the file LICENSE.LGPL included in the packaging of this
## file. Please review the following information to ensure the GNU Lesser
## General Public License version 2.1 requirements will be met:
## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
##
## In addition, as a special exception, Nokia gives you certain additional
## rights. These rights are described in the Nokia Qt LGPL Exception
## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU General
## Public License version 3.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of this
## file. Please review the following information to ensure the GNU General
## Public License version 3.0 requirements will be met:
## http://www.gnu.org/copyleft/gpl.html.
##
## Other Usage
## Alternatively, this file may be used in accordance with the terms and
## conditions contained in a signed written agreement between you and Nokia.
##
##
##
##
##
##
## $QT_END_LICENSE$
##
#############################################################################

package QtQA::QMake::Project;
use strict;
use warnings;

use Carp;
use English qw(-no_match_vars);
use File::chdir;
use File::Basename;
use File::Temp;
use Getopt::Long qw(GetOptions);
use IO::File;
use Readonly;
use ReleaseAction qw(on_release);
use List::MoreUtils qw(apply);
use Text::ParseWords;

use QtQA::QMake::Project::LazyValue;

Readonly my $WINDOWS => ($OSNAME =~ m{win32}i);

# Magic string denoting we've deliberately exited qmake early
Readonly my $MAGIC_QMAKE_EXIT_STRING => __PACKAGE__.':EXITING';

sub new
{
    my ($class, $makefile) = @_;

    my $self = bless {
        _die_on_error => 1, # whether to die when an error occurs
        _qmake_count => 0,  # number of times qmake has been run (for testing)
    }, $class;

    if ($makefile) {
        $self->set_makefile( $makefile );
    }

    $self->set_make( $self->_default_make( ) );

    return $self;
}

sub set_makefile
{
    my ($self, $makefile) = @_;

    $self->{ _makefile } = $makefile;

    # Makefile changed, so everything needs to be resolved again.
    $self->{ _resolved } = {};

    return;
}

sub makefile
{
    my ($self) = @_;
    return $self->{ _makefile };
}

sub set_make
{
    my ($self, $make) = @_;

    $self->{ _make } = $make;

    return;
}

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

    return $self->{ _make };
}

# Returns a reasonable default make command based on the platform.
sub _default_make
{
    my ($self) = @_;

    if ($WINDOWS) {
        return 'nmake';
    }

    return 'make';
}

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

    return $self->{ _die_on_error };
}

sub set_die_on_error
{
    my ($self, $value) = @_;

    $self->{ _die_on_error } = $value;
    return;
}

sub _prepare_variable
{
    my ($self, @variable) = @_;

    foreach my $variable (@variable) {
        $self->{ _to_resolve }{ variable }{ $variable } = 1;
    }

    return;
}

sub _prepare_test
{
    my ($self, @test) = @_;

    foreach my $test (@test) {
        $self->{ _to_resolve }{ test }{ $test } = 1;
    }

    return;
}

sub _qx_or_croak
{
    my ($self, $cmd) = @_;

    my $output = qx($cmd);
    if ($? != 0) {
        # If $output contains this magic, we deliberately exited, and the status can
        # be ignored.
        if ($output !~ m/\Q$MAGIC_QMAKE_EXIT_STRING\E/) {
            croak __PACKAGE__.": command `$cmd', in directory $CWD, exited with status $?, output follows:\n$output\n";
        }
        return $output;
    }

    return $output;
}

# Returns a copy of %ENV with any make-related environment variables removed.
sub _make_cleaned_env
{
    my ($self) = @_;

    my %out = %ENV;

    delete @out{qw(
        MAKEFLAGS
        MAKELEVEL
        MFLAGS
    )};

    return %out;
}

# Returns the qmake command (as a single string) used to generate the given makefile.
{'fraction_non_alphanumeric': 0.15495137046861185, 'fraction_numerical': 0.005083996463306808, 'mean_word_length': 3.294731846226863, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '1996879', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2784, 'n_words': 974}