Skip to content

Commit

Permalink
0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
rudojaksa committed Jun 29, 2024
1 parent 66da1d7 commit d776cda
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
TODO
.*.d
19 changes: 12 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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' >> $@
Expand All @@ -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
Expand All @@ -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
24 changes: 24 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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

&nbsp;&nbsp; [pcpp -h](doc/pcpp.md)
Expand Down
61 changes: 61 additions & 0 deletions doc/pcpp.md
Original file line number Diff line number Diff line change
@@ -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

20 changes: 20 additions & 0 deletions doc/uninclude.md
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions include.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 31 additions & 3 deletions pcpp
Original file line number Diff line number Diff line change
@@ -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=<<EOF;
Expand All @@ -20,6 +20,8 @@ OPTIONS
-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).
Expand Down Expand Up @@ -57,6 +59,10 @@ TRIPLE COMMENTS
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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 26 additions & 1 deletion pcpp.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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

Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions uninclude
Original file line number Diff line number Diff line change
@@ -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=<<EOF;
Expand Down
Loading

0 comments on commit d776cda

Please sign in to comment.