Skip to content

Commit

Permalink
0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
rudojaksa committed Jul 5, 2024
1 parent 20fe0ef commit 406033a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 64 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PACKAGE := pcpp
VERSION := 0.8
VERSION := 0.9
AUTHOR := R.Jaksa 2008,2024 GPLv3
SUBVERSION := c
SUBVERSION :=

SHELL := /bin/bash
PATH := usr/bin:$(PATH)
Expand All @@ -16,10 +16,10 @@ DOC := $(BIN:%=%.md)
all: $(BIN) $(DOC)

$(BIN): %: %.pl .%.d .version.pl .%.built.pl Makefile
echo -e '#!/usr/bin/perl' > $@
echo -e "# $@ generated from $(PKGNAME)/$< $(DATE)" >> $@
@echo -e '#!/usr/bin/perl' > $@
@echo -e "# $@ generated from $(PKGNAME)/$< $(DATE)" >> $@
usr/bin/pcpp $< >> $@
chmod 755 $@
@chmod 755 $@
@sync # to ensure pcpp is saved before used in the next rule
@echo

Expand All @@ -34,12 +34,12 @@ $(DOC): %.md: %
@echo 'our $$VERSION = "$(VERSION)";' >> $@
@echo 'our $$AUTHOR = "$(AUTHOR)";' >> $@
@echo 'our $$SUBVERSION = "$(SUBVERSION)";' >> $@
@echo "update $@"
@echo "make $@"

.PRECIOUS: .%.built.pl
.%.built.pl: %.pl .version.pl Makefile
@echo 'our $$BUILT = "$(DATE)";' > $@
@echo "update $@"
@echo "make $@"

# /map install, requires /map directory and getversion and mapinstall tools
ifneq ($(wildcard /map),)
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
0.9 - -d shows nonexistent files too
- better -l output
0.8 - raised version number to be above perlpp
- added -d to generate Makefile dependencies lists
Expand Down
90 changes: 47 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Simple Perl (Python/C/C++) preprocessor

This simple preprocessor handles in-comments include directive. Such include
statements that are contained inside comments provide additional level of code
organization, above the Perl's `require` and `use`, or Python's `import`, or
C/C++ `#include` native mechanisms. However, these both mechanisms (native and
This simple preprocessor handles in-comments include directives. Such include
statements that are contained inside comments provide an additional level of
code organization, above Perl's `require` and `use`, Python's `import`, or
C/C++ `#include` native mechanisms. However, both these mechanisms (native and
pcpp) can coexist together. Or, the pcpp can be used instead of the native
mechanism.

In following Perl and C examples the "abc" code will be copy-pasted into source
code by the pcpp, while the "xyz" code will be handled be native include
mechanisms:
In the following Perl and C examples the "abc" code will be copy-pasted into
the source code by the pcpp, while the "xyz" code will be handled by native
include mechanisms:

``` perl
# Perl usage
Expand Down Expand Up @@ -47,26 +47,26 @@ perl abc.pl
```

This looks as if we first "compile" the bunch of Perl source files into the
"executable" Perl file and only then run it. Similarly to a c code, which has
to be compiled before it can be executed. It is a disadvantage as it requires
the extra step `pcpp abc.in.pl`. But the following step `perl abc.pl` becomes
actually simpler:
"executable" Perl file and only then run it. Similarly, a C code has to be
compiled before it can be executed. It is a disadvantage as it requires the
extra step `pcpp abc.in.pl`. But the following step `perl abc.pl` becomes
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
* the abc\.pl stays an interpreted language code that can be fixed if needed
(comments stay in as well).

The same holds for Python. Pcpp-ing is some kind of simple code amalgamation,
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
to hide something from the cpp preprocessor.

Other reason to use pcpp instead of native require/use/import is that the pcpp
processing logic is simpler. And one more reason is that such include process
cannot be skewed at the use-time, run-time.
Another reason to use pcpp instead of native require/use/import is that the
pcpp processing logic is simpler. One more reason is that such include process
cannot be skewed at the use-time and run-time.

### In-comment directives

Hidding the pcpp directives in comments allows to avoid conflicts with the main
Hiding 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 ;-)

Expand All @@ -79,15 +79,15 @@ optional, whitespace between the hash and the "include" word is optional:

The additional text after the filename(s) is a comment and is ignored,
filenames have to be identified by the .pl (or .py/.h) suffix or have to be
quoted for the parser to find the begin of comment:
quoted for the parser to find the beginning of the comment:

```
# include abc.pl xyz.pl comment text which is ignored
# include abc.py xyz.py # visualy more noticeable comment
// include abc.h comment in C/C++
```

Multiple hashes (slashes) are not accepted, thus following code are just
Multiple hashes (slashes) are not accepted, the following code are just
comments without any effect:

```
Expand All @@ -107,15 +107,16 @@ Included content in the pcpp output is "watermarked" by the `# included` and
# end "abc.pl"
```

Indentation of the include statement is propagated into output. For instance
a two-spaces indentation of the hash of include statement in perl code:
Indentation of the include statement is propagated into output. For instance a
two-space indentation of the hash of the include statement in perl code:

``` perl
some(code);
# include abc.pl
```

will lead to added two spaces to the original indentation in included file:
will lead to adding two spaces to the original indentation in the included
file:

``` perl
some(code);
Expand All @@ -126,26 +127,26 @@ some(code);
```

The language is autodetected according to the suffix of the input file: `.pl`,
`.py`, `.c` or `.c++`.
`.py`, `.c`, or `.c++`.

### Paths resolving

Include paths can be spcified as a filename only `# include abc.pl` or
Include paths can be specified as a filename only `# include abc.pl` or
specifying also a part of the path `# include xy/abc.pl` or `# include
yz/abc.pl` to distinguish between equal filenames in different directories.
The resolving algorithm is:

1. trying direct path from current working directory,
2. trying path from directory of file from which the include is called,
3. try to find file recursively in any subdirectory of current working directory (in the depth order),
4. strip the directory part from include file name, and try to find it just by the filename.
1. trying direct path from the current working directory,
2. trying the path from the directory of the file from which the include is called,
3. try to find files recursively in any subdirectory of the current working directory (in the depth order),
4. strip the directory part from the included file name, and try to find it just by the filename.

In the case of conflict, i.e. `# include "abc.pl"` where two `abc.pl` are
available, the first one is chosen: `./abc.pl` is direct path so has higher
priority than the `xy/abc.pl`.
available, the first one is chosen: `./abc.pl` is the direct path so it has a
higher priority than the `xy/abc.pl`.

Double includes are avoided, so the file from given path is copy-pasted to the
output only once, on the place of the firste appearance of the include
Double includes are avoided, so the file from the given path is copy-pasted to
the output only once, on the place of the first appearance of the include
statement.

Missing include files are by default silently ignored, or reported in the
Expand All @@ -154,9 +155,9 @@ verbose mode (-v switch).
### Triple comments

Pcpp preserves comments to allow the Perl or Python output code to be readable
as best as possible in order to allow the output to be hacked/fixed.
as best as possible to allow the output to be hacked/fixed.

However, to allow programmer to request the removal of comments from the
However, to allow the programmer to request the removal of comments from the
output, we introduce the "triple comments":

```
Expand All @@ -176,7 +177,7 @@ the pcpp processed code, not the code you wrote.

### Uninclude

Watermarked pcpp output allows to remove included parts and return to the
Watermarked pcpp output allows the removal of included parts and return to the
original source code using the `uninclude` tool. This can be useful when
building "libraries" which can recursively pack all dependencies, which can be
stripped off when not needed (when already provided by another "library").
Expand All @@ -194,7 +195,7 @@ instance the following included content:
# end abc.pl
```

will be flattend by uninclude to:
will be flattened by uninclude to:

```
# include abc.pl
Expand All @@ -215,20 +216,20 @@ which when included back will become:

### Pcpp in Makefile

Example to make `xyz` from from its source `xyz.pl` and two included files:
Example to make `xyz` from its source `xyz.pl` and two included files:

``` makefile
xyz: xyz.pl inc1.pl inc2.pl
echo '#!/usr/bin/perl' > $@
pcpp $< >> $@
@chmod 755 $@
@sync # to ensure the result is saved before used in the next rule
@sync # to ensure the result is saved before being used in the next rule
```

1. generate #! interpreter identifier
2. build `xyz` from `xyz.pl`
3. make it executable
4. sync the result before it is used by other makefile rule (otherwise it can be incomplete)
4. sync the result before it is used by another makefile rule (otherwise it can be incomplete)

More complex example:

Expand All @@ -248,13 +249,16 @@ $(OUTPUT): %: %.pl $(DEPENDENCIES) Makefile
```

* `DEPENDENCIES` are a list of files to be included obtained by `pcpp -lp`
* `SIGN` is a variable made available from Makefile into script
* `SIGN` is a variable made available from Makefile into the script

### Dependency files

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 add
the input file into the list. Example Makefile:
the input file into the list and nonexistent files too. Nonexistent include
files are files that will be generated by the Makefile. A full path is
required for them to work properly (relative path is ok if it is complete).
Example Makefile:

``` makefile
# require rebuild of the dependencies file .abc.d when processing abc.pl
Expand Down Expand Up @@ -282,7 +286,7 @@ to any `/bin` directory for a system-wide installation.

### Under the hood

The pcpp itself is processed by the pcpp, so its source code is example how to
use the pcpp.
The pcpp itself is processed by the pcpp, so its source code is an example of
how to use the pcpp.

<br><div align=right><i>R.Jaksa 2008,2024</i></div>
4 changes: 2 additions & 2 deletions include.pl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
elsif($ok==3) { report $CC_,beautify($path) } # OK
else { report $CR_,$file } # not found

# deps
$DEPLIST.=beautify($path)." " if $DEPS and $ok;
# deps (show also nonexistent files, to allow to be generated)
$DEPLIST.=beautify($path)." " if $DEPS;

return if $ok==0; # file not found
return if $ok==1; # file already included (TODO: accept if requested, but avoid recursion)
Expand Down
12 changes: 6 additions & 6 deletions pcpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/perl
# pcpp generated from pcpp-0.8c/pcpp.pl 2024-06-30
# pcpp generated from pcpp-0.9/pcpp.pl 2024-07-06

# included ".version.pl"
our $PACKAGE = "pcpp";
our $VERSION = "0.8";
our $VERSION = "0.9";
our $AUTHOR = "R.Jaksa 2008,2024 GPLv3";
our $SUBVERSION = "c";
our $SUBVERSION = "";
# end ".version.pl"

# included ".pcpp.built.pl"
our $BUILT = "2024-06-30";
our $BUILT = "2024-07-06";
# end ".pcpp.built.pl"

$HELP=<<EOF;
Expand Down Expand Up @@ -433,8 +433,8 @@ 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;
# deps (show also nonexistent files, to allow to be generated)
$DEPLIST.=beautify($path)." " if $DEPS;

return if $ok==0; # file not found
return if $ok==1; # file already included (TODO: accept if requested, but avoid recursion)
Expand Down
2 changes: 1 addition & 1 deletion pcpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ Simple Perl/Python/C/C++ preprocessor for in-comment directives.
pcpp -d pcpp pcpp.pl > .pcpp.d

### VERSION
pcpp-0.8c R.Jaksa 2008,2024 GPLv3 built 2024-06-30
pcpp-0.9 R.Jaksa 2008,2024 GPLv3 built 2024-07-06

8 changes: 4 additions & 4 deletions uninclude
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/perl
# uninclude generated from pcpp-0.8c/uninclude.pl 2024-06-30
# uninclude generated from pcpp-0.9/uninclude.pl 2024-07-06

# included ".version.pl"
our $PACKAGE = "pcpp";
our $VERSION = "0.8";
our $VERSION = "0.9";
our $AUTHOR = "R.Jaksa 2008,2024 GPLv3";
our $SUBVERSION = "c";
our $SUBVERSION = "";
# end ".version.pl"

# included ".uninclude.built.pl"
our $BUILT = "2024-06-30";
our $BUILT = "2024-07-06";
# end ".uninclude.built.pl"

$HELP=<<EOF;
Expand Down
2 changes: 1 addition & 1 deletion uninclude.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ but they are flattened to a single level direct includes.
-ni Don't return back #include statements.

### VERSION
pcpp-0.8c R.Jaksa 2008,2024 GPLv3 built 2024-06-30
pcpp-0.9 R.Jaksa 2008,2024 GPLv3 built 2024-07-06

0 comments on commit 406033a

Please sign in to comment.