从Perl脚本调用configure和make

wqlqzqxt  于 2023-10-24  发布在  Perl
关注(0)|答案(2)|浏览(149)

我需要帮助。我正在编写一个Perl脚本来配置,编译和安装MPICH。最初的两个任务需要调用configuremake命令。我正在努力将选项传递给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调用中遇到了一个问题。我怀疑当capturex命令传递给capturex命令时,$conf_options之间没有空格。我试图将配置选项声明为数组my @conf_options并在qq()操作符中传递字符串。但这失败了,出现了类似的错误。任何帮助都将不胜感激。

4dc9hkyq

4dc9hkyq1#

不是传递参数,而是将shell命令的一部分传递给不调用shell的sub。
取代

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,而后者使用。

mrfwxfqh

mrfwxfqh2#

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

相关问题