http://www.perlmonks.org?node_id=1050946

baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE: solved , the problem was so obvious system("cd ... ; make this; cd ..."); should be spitted as Corion stated. Thank you all for comments

Hi

Recently I have been developing some stuff in c and to compile the code i am using classic makefile approach. But my code kept getting bigger and bigger and more complicated. So i realized that i need to make a shell script to orchestrate the entire make procedure. Since all linux platforms (and unix) support basic perl I decided to build my sources using a simple perl script (How smart is that ?? - not a rhetorical question). and every time i need to execute make i use regular system command

system("cd ... ; make this; cd ...");
UPDATE: cd ... are used to move through directories since each build directory contains different make file and is build according to different make rules

END UPDATE

now if i run into errors i would like to know when they happened at make runtime, that is why system is a good option for me. But on the other hand, if any errors do occur, then my perl script since it has no info about that err, just continues further on s, which is bad.

My question is how to bypass this problem and capture the error or just the status that the error occurred? (Limits: if i use qx then i do not get the runtime compilation reports which is bad) I tried redirecting the error to a file and the reading it after each make but then my errors are clustered together in one place, which, againi is bad.

any suggestions ?

Thank you

baxy

Replies are listed 'Best First'.
Re: Capture the make status
by RichardK (Parson) on Aug 26, 2013 at 12:39 UTC

    Why not get make to do all the work? It can call itself recursively, just write a rule for each subsystem.

    subsystem: cd subdir && $(MAKE) or, equivalently, subsystem: $(MAKE) -C subdir

    It is explained in the make docs, try:

    info make
Re: Capture the make status
by Corion (Patriarch) on Aug 26, 2013 at 11:56 UTC

    make should pass through the last exit status to system, or should exit itself with some status that indicates error.

    Have you looked at system and how one can catch the exit status of a subprocess? I would look at autodie, which is available with recent versions of Perl, or at the following idiom:

    system( $cmd ) == 0 or die "Couldn't launch/run [$cmd]: $? / $! / $^E";
      well then i am doing something else wrong because when i induce an error my system returns 0 as if everything is ok...

        Aaaah - sorry! Now I realize your command line, and where that behaviour stems from.

        You are not passing a single command to system() but a string containing ";" which will be interpreted by the shell instead of directly started. You get back the exit status of the shell, which (likely) is the exit status of the last command run, which is cd ...

        I recommend you do the chdir in Perl and run only make ... directly via system().

Re: Capture the make status
by daxim (Curate) on Aug 26, 2013 at 12:02 UTC
    Yes, the vexing problem of chdir being global. Separate your commands, thus:
    use File::chdir qw($CWD); use IPC::System::Simple qw(runx); { local $CWD = '...'; runx [0], 'make', 'this'; }
    At the end of the block, the working directory will snap back to what it was before. If the directory cannot be changed, it throws an exception. runx as shown will throw an exception if the return code is not 0.
Re: Capture the make status
by choroba (Cardinal) on Aug 26, 2013 at 11:59 UTC
    It is not clear to me what exactly you are trying to achieve. Just some observations:
    • The final cd server no purpose. After system finishes the command, its working directory is forgotten. Therefore, you can safely remove it.
    • You do not test the exit status of the system command. Use
      my $status = system('cd ... ; make this'); die $? if not 0 == $status;
      See the documentation linked above for details.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: [SOLVED] Capture the make status
by Laurent_R (Canon) on Aug 26, 2013 at 17:50 UTC

    Hi,

    just as a side note, have you considered to use cons instead of make. It is written in Perl and it seems to be vastly superior to make (but I haven't tried it myself). You can check its documentation here: http://www.gnu.org/software/cons/stable/cons.html.