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

I'm a relative newbie to PERL and I am trying to figure out how to handle this simple task.

I have a text file with a capture of commands associated with a software build.

e:\green\prelink.exe -X4 -c "e:\green\gnm.exe -c++" -Le:\green\ppc403 + \ obj\hello.o obj\shop.o obj\catalog.o obj\scan.o obj\strings.o +\ obj\itemlist.o -- -lscnoe e:\green\decode.exe < C:\DOCUME~1\BUILD~1.ELE\LOCALS~1\Temp\ghjs1.ler rm -f C:\DOCUME~1\BUILD~1.ELE\LOCALS~1\Temp\ghjs1.ler e:\green\gnm.exe -s catalog > C:\DOCUME~1\BUILD~1.ELE\LOCALS~1\Temp\gh +js2.nm e:\green\dblink.exe -dbopath=obj -nm=gnm \ C:\DOCUME~1\BUILD~1.ELE\LOCALS~1\Temp\ghjs2.nm -o catalog.dnm +\ e:\green\ppc403\crt0.dbo e:\green\ppc403\libmulti.dba \ e:\green\ppc403\libscnoe.dba e:\green\ppc403\libsedgnoe.dba \ e:\green\ppc403\libdbmem.dba e:\green\ppc403\libansi.dba \ e:\green\ppc403\libind.dba e:\green\ppc403\libsys.dba \ e:\green\ppc403\libarch.dba rm -f C:\DOCUME~1\BUILD~1.ELE\LOCALS~1\Temp\ghjs2.nm

These are all command lines for a build. I need to separate each command line (which extends over multiple lines with the " \" at the end until there is one without that), count them, and then put them into a makefile so they can be executed with gmake.

Any ideas how I should approach this?

Replies are listed 'Best First'.
Re: Breaking up a text file to make a Makefile
by bitingduck (Deacon) on Mar 13, 2015 at 07:35 UTC

    Try this for a start?

    #!/usr/bin/perl use strict; use warnings; use v5.10; open my $FILE ,"makefile.txt"; my $accum=""; while(<$FILE>){ chomp; if(s%(.*?)\\$% %){ $accum.=$1; } else { $accum.=$_; $accum=~s/\s+/ /gs; say $accum; $accum=""; } }
      Ok, this sets me in the right direction. I need to store these in an array, and then I need to count how many array entries I made in order to produce the definition of the "all" target in the makefile, then dump the array one at a time for each target into the makefile. Thanks!
Re: Breaking up a text file to make a Makefile
by Anonymous Monk on Mar 13, 2015 at 07:22 UTC
      #!/usr/bin/perl -- use strict; use warnings; use v5.10.0; use Data::Dump qw/ dd /; my $blah = qq{anything \\\nblah\nanything \\\nblah}; dd( $blah ); print "$blah\n\n"; #~ use re 'debug'; my $reblah = qr{ (?<multiline> (?: ^ [^\r\n]* \\ [\r\n]* )+ ## multiline MANY times (?: ^ [^\r\n]* [\r\n]+ ) ## but always ENDS with oneline ) | (?<oneline> ^ [^\r\n]* [\r\n]+ ) }smx; while( $blah =~ m{$reblah}g ){ dd( { %+ } ); } __END__ "anything \\\nblah\nanything \\\nblah" anything \ blah anything \ blah { multiline => "anything \\\nblah\n" } { oneline => "anything \\\n" }
      I have a "makefile" for a completely different build tool that comes with a 15-yr-old product, no compatibility to gmake. and I've devised a way to easily convert it to a gmake-compatible makefile. This is not something makefile tools do. I'm just not proficient enough in Perl to do it intelligently. I could use brute force, but I want to learn how to do things elegantly in Perl.
Re: Breaking up a text file to make a Makefile
by oiskuu (Hermit) on Mar 13, 2015 at 14:06 UTC

    You want automatic translation from another build system to gmake? I don't think that's practicable.

    Or did you lose the makefile and now must recreate it from a build log?

    The answer to your question: by hand. General programming principles apply here such as DRY.
    Your makefile might contain definitions such as

    DBDIR = /green/ppc403
    DBANAMES = dbmem scnoe ...
    DBAS = $(addprefix $(DBDIR)/,$(DBANAMES:%=lib%.dba))
    
    And the recipes would use those definitions, e.g.
    catalog.dnm: $(DBAS)
            dblink ... -o $@ $+
    
    It takes some crafting. But perhaps you only need a simple batchfile?