从Perl脚本调用configure和make

问题描述 投票:0回答:2

我需要帮助。我正在编写一个 Perl 脚本来配置、编译和安装 MPICH。最初的两个任务需要调用

configure
make
命令。我正在努力将选项传递给
configure
命令。请在下面找到我的最小示例:

#!/usr/local/bin/perl

use v5.38;
use autodie;
use strict;
use warnings;
use IPC::System::Simple qw(capturex);

# Writes text into given file
sub writef($fname, $txt) {
    open  my $fh, ">:encoding(UTF-8)", $fname;
    print $fh $txt;
    close $fh;
}

my $gnubin ='/opt/local/bin';
my $prefix ='/opt/mpich-4.1.2';
my $np     = 8;
my $txt;

print "Setting environment variables...\n";

$ENV{'FC'}  = $gnubin . '/gfortran';
$ENV{'F77'} = $ENV{'FC'};
$ENV{'CC'}  = $gnubin . '/gcc';
$ENV{'CXX'} = $gnubin . '/g++';

$ENV{'FFLAGS'} = '-m64 -fallow-argument-mismatch';
$ENV{'CFLAGS'} = '-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -m64';

mkdir 'build';
chdir 'build';

print "Configuring MPICH...\n";
my $conf_options = "--prefix=$prefix --enable-shared=no --enable-fast=O3,ndebug --disable-error-checking --enable-fortran=all --enable-cxx --disable-opencl --enable-romio --without-timing --without-mpit-pvars";
$txt = capturex('../configure', $conf_options);
&writef('c.txt', $txt);

print "Compiling MPICH...\n";
$txt = capturex('make', '-j', $np);
&writef('m.txt', $txt);

配置正确完成,但编译失败,并出现以下错误消息:

gcc: error: unrecognized command-line option '--without-timing'
gcc: error: unrecognized command-line option '--without-mpit-pvars/etc"'
gcc: error: unrecognized command-line option '--without-mpit-pvars/lib/libfabric"'

看来我在

capturex
通话中遇到了问题。我怀疑当它们传递给
$conf_options
命令时,
capturex
之间没有空格。我尝试将配置选项声明为数组
my @conf_options
并在
qq()
运算符中传递字符串。但这失败了,并出现了一系列类似的错误。任何帮助将不胜感激。

perl ipc gnu-make configure
2个回答
2
投票

capturex
获取要传递给命令的选项列表。如果将所有选项放入一个字符串中并仅传递该字符串,则该字符串是该命令的单个选项。任何带有多个参数的
capturex
调用都会绕过 shell,因此 shell 没有机会将单个字符串分解为单独的参数。您可以在 IPC::System::Simple 的文档示例中看到这一点。


2
投票

您不是传递参数,而是将 shell 命令的一部分传递给不调用 shell 的子程序。

更换

my $conf_options = "--prefix=$prefix --enable-shared=no ...";
my $txt = capturex( "../configure", $conf_options );

my @conf_options = ( "--prefix=$prefix", "--enable-shared=no", "..." );
my $txt = capturex( "../configure", @conf_options );

use String::ShellQuote qw( shell_quote );

my $prefix_lit = shell_quote( $prefix );

my $conf_options = "--prefix=$prefix_lit --enable-shared=no ...";
my $txt = capture( "../configure $conf_options" );

前者不使用shell,而后者使用shell。

© www.soinside.com 2019 - 2024. All rights reserved.