From d776cda6f4468ba0642a92123e51e1ef961f8523 Mon Sep 17 00:00:00 2001 From: Rudolf Jaksa Date: Sun, 30 Jun 2024 00:14:57 +0200 Subject: [PATCH] 0.5 --- .gitignore | 1 + Makefile | 19 +++++++++------ NEWS | 24 +++++++++++++++++++ README.md | 27 +++++++++++++++++++-- doc/pcpp.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ doc/uninclude.md | 20 ++++++++++++++++ include.pl | 3 +++ pcpp | 34 ++++++++++++++++++++++++--- pcpp.pl | 27 ++++++++++++++++++++- uninclude | 4 ++-- usr/bin/pcpp | 33 ++++++++++++++++++++++++-- 11 files changed, 236 insertions(+), 17 deletions(-) create mode 100644 NEWS create mode 100644 doc/pcpp.md create mode 100644 doc/uninclude.md diff --git a/.gitignore b/.gitignore index 1333ed7..6708e28 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ TODO +.*.d diff --git a/Makefile b/Makefile index 6fc7de9..d674725 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PACKAGE := pcpp -VERSION := 0.4 +VERSION := 0.5 AUTHOR := R.Jaksa 2008,2024 GPLv3 -SUBVERSION := b +SUBVERSION := SHELL := /bin/bash PATH := usr/bin:$(PATH) @@ -11,12 +11,13 @@ PRJNAME := $(shell getversion -prj) DATE := $(shell date '+%Y-%m-%d') BIN := pcpp uninclude +DEP := $(BIN:%=.%.d) DOC := $(BIN:%=doc/%.md) -BDEP := $(shell pcpp -lp $(BIN:%=%.pl)) +#BDEP := $(shell usr/bin/pcpp -lp $(BIN:%=%.pl)) all: $(BIN) $(DOC) -%: %.pl $(BDEP) Makefile +$(BIN): %: %.pl .%.d Makefile echo -e '#!/usr/bin/perl' > $@ echo -e "# $@ generated from $(PKGNAME)/$< $(DATE)\n" >> $@ echo -e '$$SIGN = $(SIGN);\n' >> $@ @@ -25,7 +26,10 @@ all: $(BIN) $(DOC) @sync # to ensure pcpp is saved before used in the next rule @echo -$(DOC): doc/%.md: %.pl Makefile | doc +$(DEP): .%.d: %.pl + pcpp -d $(<:%.pl=%) $< > $@ + +$(DOC): doc/%.md: % | doc ./$* -h | man2md > $@ doc: mkdir -p doc @@ -42,9 +46,10 @@ install: $(BIN) endif clean: - rm -rf doc + rm -rf $(DEP) mrproper: clean - rm -f pcpp uninclude + rm -f doc $(BIN) +-include $(DEP) -include ~/.github/Makefile.git diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..7b8faf7 --- /dev/null +++ b/NEWS @@ -0,0 +1,24 @@ +0.8 - raised version number to be above perlpp + - added -d to generate Makefile dependencies lists +0.1 - rewritten simplified preprocessor + +perlpp + +0.7 - dosomething-if now working also inside {} + - syntax change: in {} act.comments they should be last, not first, but before next,exit,etc. +0.6 - don't include commented out active comments + - added block syntax for .pl.debug code + - added debug line style "dosomething if condition;" + - another duplicate includes bug fix +0.5 - fixed duplicate includes bug + - added debug labels support +0.4 - added #eval directive + - added -x overwrite switch +0.3 - simplified and renamed to perlpp + +perlkit + +0.2 - added make list + - added more advanced perlpp (instead of plpp.pl) + - improved make install +0.1 - the start diff --git a/README.md b/README.md index 3d8dfef..0addad9 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,8 @@ the extra step `pcpp abc.in.pl`. But the following step `perl abc.pl` becomes actually simpler: * now the abc\.pl doesn't require the dependent .pl files to be installed, - * the abc\.pl stays a interpreted language code which can be fixed if needed (comments stay in as well). + * the abc\.pl stays a interpreted language code which can be fixed if needed + (comments stay in as well). The same holds for Python. Pcpp-ing is some kind of simple code amalgamation, or a "static linker" for Perl/Python. In C/C++ the pcpp is useful if you want @@ -66,7 +67,8 @@ cannot be skewed at the use-time, run-time. ### In-comment directives Hidding the pcpp directives in comments allows to avoid conflicts with the main -language interpreter/compiler/preprocessor, with IDEs or editing modes. Further, this increases the number of comments ;-) +language interpreter/compiler/preprocessor, with IDEs or editing modes. +Further, this increases the number of comments ;-) Multiple filenames in the single include directive line are allowed, quoting is optional, whitespace between the hash and the "include" word is optional: @@ -252,6 +254,27 @@ BIN := xyz DEP := $(shell pcpp -lp $(BIN:%=%.pl)) ``` +### Dependencies + +The `pcpp -d target_name` can be used to generate a dependency file for +Makefile. Compared to the `-lp` option, the `-d` and `-dd` options also +include the input file into the list. The `pcpp -lp input | xargs` will list +only included files. The usage in the Makefile is: + +``` makefile +# require rebuild of the dependencies file .abc.d when processing abc.pl +%: %.pl .%.d + echo -e '#!/usr/bin/perl' > $@ + pcpp -v $< >> $@ + +# save dependencies into .abc.d for the abc.pl source of the abc target +.%.d: %.pl + pcpp -d $(<:%.pl=%) $< > $@ + +# include generated dependencies but don't fail if they are missing +-include .abc.d +``` + ### See also    [pcpp -h](doc/pcpp.md) diff --git a/doc/pcpp.md b/doc/pcpp.md new file mode 100644 index 0000000..1611578 --- /dev/null +++ b/doc/pcpp.md @@ -0,0 +1,61 @@ +### NAME +pcpp - simple Perl/Python/C/C++ preprocessor + +### USAGE + pcpp [OPTIONS] file file ... + +### DESCRIPTION +Simple Perl/Python/C/C++ preprocessor for in-comment directives. +* process include directives, +* removes triple-comments. + +### OPTIONS + -h This help. + -v Verbose, -vv for more verbose. + -l Just list files to include, -l1/lp for level1 or paths. + -dd Print a list of dependencies (input file plus included ones). + -d TGT Generate dependencies list for Makefile for the TGT target. + -e DIR Exclude directory from a search, multiple -e possible. + -nt No triple comments removal. + -nw No watermarking of included parts (by #included). + -ni No indentation propagation. + +### INCLUDE DIRECTIVE + Only lines with the "include" directive are recognized. + Whitespace after the hash is optional, quotes optional. + Whitespace before the hash is used to indent the included content. + + Include files can be defined by the filename, by the path, or by + a partial incomplete path. The path resolving procedure is: + + 1. look for direct path from CWD, + 2. look for relative path from file to which we include, + 3. find filename recursively in the depth order from CWD, + 4. strip directory part from include and serch by filename. + + Double includes are avoided. Missing includes are ignored. + Any text after include files is a comment. + + # include "abc.pl" # Perl, Python + // include "abc.h" // C, C++ + #include abc.pl xyz.pl # multiple files in one include possible + ## include abc.pl # not an include due to two hashes + # include abc.pl # indented include of abc.pl + # include "abc.pl" # comment + # include abc.pl xyz.pl comment + +### TRIPLE COMMENTS + Triple comments are removed, together with preceding empty lines. + All other comments are propagated to the output. + + ### this line will be removed from the Perl/Python code by pcpp + /// this line will be removed from the C/C++ code by pcpp + #### but this will be kept + +### EXAMPLES + pcpp -v pcpp.pl > pcpp + pcpp -d pcpp pcpp.pl > .pcpp.d + +### VERSION +pcpp-0.5 R.Jaksa 2008,2024 GPLv3 + diff --git a/doc/uninclude.md b/doc/uninclude.md new file mode 100644 index 0000000..0ad0981 --- /dev/null +++ b/doc/uninclude.md @@ -0,0 +1,20 @@ +### NAME +uninclude - remove included parts from pcpp generated files + +### USAGE + uninclude [OPTIONS] file + +### DESCRIPTION +Removes all included parts from a pcpp generated file. Depends +on pcpp watermarking. Can return back the #include statements, +but they are flattened to a single level direct includes. + +### OPTIONS + -h This help. + -v Verbose. + -l Just list all includes, indentation by the include level. + -ni Don't return back #include statements. + +### VERSION +pcpp-0.5 R.Jaksa 2008,2024 GPLv3 + diff --git a/include.pl b/include.pl index 638f5cc..a719529 100644 --- a/include.pl +++ b/include.pl @@ -79,6 +79,9 @@ elsif($ok==3) { report $CC_,beautify($path) } # OK else { report $CR_,$file } # not found + # deps + $DEPLIST.=beautify($path)." " if $DEPS and $ok; + return if $ok==0; # file not found return if $ok==1; # file already included (TODO: accept if requested, but avoid recursion) push @INCLUDED,$path; # register file diff --git a/pcpp b/pcpp index ba1a13e..aae9bdc 100755 --- a/pcpp +++ b/pcpp @@ -1,7 +1,7 @@ #!/usr/bin/perl -# pcpp generated from pcpp-0.4b/pcpp.pl 2024-05-10 +# pcpp generated from pcpp-0.5/pcpp.pl 2024-06-30 -$SIGN = "pcpp-0.4b R.Jaksa 2008,2024 GPLv3"; +$SIGN = "pcpp-0.5 R.Jaksa 2008,2024 GPLv3"; $HELP=< pcpp) + CW(pcpp -d pcpp pcpp.pl > .pcpp.d) + VERSION $SIGN @@ -202,6 +208,13 @@ for(@ARGV) { if($_ eq "-ni") { $NOIND=1; $_=""; last }} for(@ARGV) { if($_ eq "-l") { $LIST=1; $_=""; last }} for(@ARGV) { if($_ eq "-l1") { $LIST=2; $_=""; last }} for(@ARGV) { if($_ eq "-lp") { $LIST=3; $_=""; last }} +for(@ARGV) { if($_ eq "-dd") { $DEPS=1; $_=""; last }} + +# dependencies-list target +our $DEPS; +for(my $i=0;$i<$#ARGV;$i++) { + next if $ARGV[$i] ne "-d"; + $DEPS=$ARGV[$i+1]; $ARGV[$i]=$ARGV[$i+1]="" } # list of dirs to be excluded our @EXCL; @@ -409,6 +422,9 @@ our sub addfile { elsif($ok==3) { report $CC_,beautify($path) } # OK else { report $CR_,$file } # not found + # deps + $DEPLIST.=beautify($path)." " if $DEPS and $ok; + return if $ok==0; # file not found return if $ok==1; # file already included (TODO: accept if requested, but avoid recursion) push @INCLUDED,$path; # register file @@ -451,18 +467,30 @@ our sub addfile { # end "include.pl" # auxiliary output buffer, as the include recursion would break simple print to stdout, -# we print to the @output buffer instead and only at the end to the stdout +# we print to the @output buffer instead, then at the end print it to the stdout my @output; # TODO: header with timestamp and list of inputs # TODO: #! interpreter identifier +# for dependencies list +our $DEPLIST; + # add each argv file to the output push @output,addfile($_) for @FILES; # skip the rest in the list mode exit if $LIST; +# print deps +if($DEPS==1) { + print "$DEPLIST\n"; + exit } + +if($DEPS) { + print "$DEPS: $DEPLIST\n"; + exit } + if(1) { # remove tripled comments if(not $NOTRIPLE) { diff --git a/pcpp.pl b/pcpp.pl index d92587b..a8a10da 100644 --- a/pcpp.pl +++ b/pcpp.pl @@ -15,6 +15,8 @@ -h This help. -v Verbose, CC(-vv) for more verbose. -l Just list files to include, CC(-l1/lp) for level1 or paths. + -dd Print a list of dependencies (input file plus included ones). + -d TGT Generate dependencies list for Makefile for the TGT target. -e DIR Exclude directory from a search, multiple -e possible. -nt No triple comments removal. -nw No watermarking of included parts (by #included). @@ -52,6 +54,10 @@ CW(/// this line will be removed from the C/C++ code by pcpp) CW(#### but this will be kept) +EXAMPLES + CW(pcpp -v pcpp.pl > pcpp) + CW(pcpp -d pcpp pcpp.pl > .pcpp.d) + VERSION $SIGN @@ -69,6 +75,13 @@ for(@ARGV) { if($_ eq "-l") { $LIST=1; $_=""; last }} for(@ARGV) { if($_ eq "-l1") { $LIST=2; $_=""; last }} for(@ARGV) { if($_ eq "-lp") { $LIST=3; $_=""; last }} +for(@ARGV) { if($_ eq "-dd") { $DEPS=1; $_=""; last }} + +# dependencies-list target +our $DEPS; +for(my $i=0;$i<$#ARGV;$i++) { + next if $ARGV[$i] ne "-d"; + $DEPS=$ARGV[$i+1]; $ARGV[$i]=$ARGV[$i+1]="" } # list of dirs to be excluded our @EXCL; @@ -112,18 +125,30 @@ # include include.pl # auxiliary output buffer, as the include recursion would break simple print to stdout, -# we print to the @output buffer instead and only at the end to the stdout +# we print to the @output buffer instead, then at the end print it to the stdout my @output; # TODO: header with timestamp and list of inputs # TODO: #! interpreter identifier +# for dependencies list +our $DEPLIST; + # add each argv file to the output push @output,addfile($_) for @FILES; # skip the rest in the list mode exit if $LIST; +# print deps +if($DEPS==1) { + print "$DEPLIST\n"; + exit } + +if($DEPS) { + print "$DEPS: $DEPLIST\n"; + exit } + if(1) { # remove tripled comments if(not $NOTRIPLE) { diff --git a/uninclude b/uninclude index 6d84bf2..d18afaa 100755 --- a/uninclude +++ b/uninclude @@ -1,7 +1,7 @@ #!/usr/bin/perl -# uninclude generated from pcpp-0.4b/uninclude.pl 2024-05-10 +# uninclude generated from pcpp-0.5/uninclude.pl 2024-06-30 -$SIGN = "pcpp-0.4b R.Jaksa 2008,2024 GPLv3"; +$SIGN = "pcpp-0.5 R.Jaksa 2008,2024 GPLv3"; $HELP=< pcpp) + CW(pcpp -d pcpp pcpp.pl > .pcpp.d) + VERSION $SIGN @@ -201,6 +208,13 @@ for(@ARGV) { if($_ eq "-ni") { $NOIND=1; $_=""; last }} for(@ARGV) { if($_ eq "-l") { $LIST=1; $_=""; last }} for(@ARGV) { if($_ eq "-l1") { $LIST=2; $_=""; last }} for(@ARGV) { if($_ eq "-lp") { $LIST=3; $_=""; last }} +for(@ARGV) { if($_ eq "-dd") { $DEPS=1; $_=""; last }} + +# dependencies-list target +our $DEPS; +for(my $i=0;$i<$#ARGV;$i++) { + next if $ARGV[$i] ne "-d"; + $DEPS=$ARGV[$i+1]; $ARGV[$i]=$ARGV[$i+1]="" } # list of dirs to be excluded our @EXCL; @@ -408,6 +422,9 @@ our sub addfile { elsif($ok==3) { report $CC_,beautify($path) } # OK else { report $CR_,$file } # not found + # deps + $DEPLIST.=beautify($path)." " if $DEPS and $ok; + return if $ok==0; # file not found return if $ok==1; # file already included (TODO: accept if requested, but avoid recursion) push @INCLUDED,$path; # register file @@ -450,18 +467,30 @@ our sub addfile { # end "include.pl" # auxiliary output buffer, as the include recursion would break simple print to stdout, -# we print to the @output buffer instead and only at the end to the stdout +# we print to the @output buffer instead, then at the end print it to the stdout my @output; # TODO: header with timestamp and list of inputs # TODO: #! interpreter identifier +# for dependencies list +our $DEPLIST; + # add each argv file to the output push @output,addfile($_) for @FILES; # skip the rest in the list mode exit if $LIST; +# print deps +if($DEPS==1) { + print "$DEPLIST\n"; + exit } + +if($DEPS) { + print "$DEPS: $DEPLIST\n"; + exit } + if(1) { # remove tripled comments if(not $NOTRIPLE) {