1
- # cpphash.sh
1
+ This project enables the C pre-processor to be run on files containing hash-style comments. It provides shell scripts that can be used to integrate with any build system and it also provides Makefiles to enable make. It has optional support for esphome projects, for which it was originally written.
2
+
3
+ cpphash.sh
4
+ ==========
2
5
3
6
run the C preprocessor (cpp) on files with hash-style comments
4
7
5
- ## Usage
8
+ Usage
9
+ -----
6
10
```
7
11
./cpphash.sh [-f] [-C] [-v] [-h] [-t <dir>] [-D define|define=<x>] [-I includedir] cppFile [extraFiles]...
8
- -t|--tempdir argument is a directory for temporary files. Defaults is .cpphash
12
+ -t|--tempdir argument is a directory for temporary files. Default is .cpphash
9
13
-D|--define> add the argument as a define to pass to cpp
10
14
-I|--include add the argument as an include file directory to pass to cpp
11
15
-o|--outfile argument is a filename to write to. Default is <cppFile>.cpp
@@ -18,13 +22,17 @@ run the C preprocessor (cpp) on files with hash-style comments
18
22
cppFile the main file to run cpp on which optionally includes <extraFiles>.
19
23
[extraFiles] extra files to process that are #included by <cppFile>
20
24
21
- ./cpphash.sh overwrites the file specified by -o (default is <cppFile>.cpp)
25
+ cpphash.sh processes <cppFile> and <extraFiles> using dehash.sh. The dehashed <extraFiles> are stored in <tempDir>. The dehashed <cppFile> is stored in <cppFile>.cpp. Then <cppFile> is run through the C pre-processor with -I <tempDir> as the first include directory path.
26
+
27
+ Note: ./cpphash.sh overwrites the file specified by -o (default is <cppFile>.cpp)
22
28
```
23
29
24
- # dehash.sh
25
- dehash.sh removes '#'-style comments
30
+ dehash.sh
31
+ =========
32
+ dehash.sh removes '#'-style comments
26
33
27
- ## Usage
34
+ Usage
35
+ -----
28
36
```
29
37
dehash.sh [-c] [-b] [-h] filename
30
38
-c|--cpp keep CPP directives
@@ -34,10 +42,9 @@ dehash.sh [-c] [-b] [-h] filename
34
42
filename file to dehash to stout or - for stdin
35
43
```
36
44
37
- dehash was originally written to remove hash-style comments in text files
38
- such as yaml that require the C-preprocessor for macro features. cpphash.sh
39
- was added as a front-end for dehash.sh to do exactly that for any text files,
40
- including yaml.
45
+ dehash.sh was removes hash style comments from one file. Use cpphash.sh
46
+ to run dehash.sh on multiple files and to run teh C-preprocessor on one of
47
+ them that includes one or more of the others.
41
48
42
49
Some possible dehash command variants are:
43
50
```
@@ -46,136 +53,97 @@ Some possible dehash command variants are:
46
53
./dehash.sh -b example.txt
47
54
./dehash.sh -c -b example.txt
48
55
```
49
- Note: cpphash uses GNU sed, md5sum and yq.
50
56
51
- If missing, yq can be installed on Linux with:
57
+ espmerge.sh
58
+ ===========
59
+ espmerge.sh: reads esphome yaml and outputs merged esphome yaml.
52
60
53
- sudo apt install yq -y
54
- or
55
- snap install yq --channel=v3/stable
56
- or
57
- apk add yq
58
-
59
- On MacOS, usually all three are missing and can be installed as follows:
60
- ```
61
- brew install gsed md5sha1sum yq
61
+ Usage
62
+ -----
62
63
```
63
- ## How to use this repo
64
-
65
- "makefile" and "Makefile" are functional makefile templates that will clone
66
- this repo into a project.
67
- To use them, just copy them both into a project directory that does not
68
- already use makefiles. If your project already does, then you likely
69
- already know how to integrate them to your project.
70
-
71
- There is an example in example/make that shows how to use make and cpphash
72
- to assist with managing multiple project variants that share text files.
73
- That example shows how to share constant #defines and configuration
74
- #defines between text files and C / C++ files. See the files main.yaml,
75
- config.h and pins.h in example/make for more details.
76
-
77
- ## User variables
78
-
79
- These variables can be changed from their defaults by editting
80
- "Makefile" or overriding them with a CLI argument to make such as
81
- ``` bash
82
- make MAIN=init.yaml
64
+ espmerge.sh <input.yaml >output.yaml
83
65
```
84
66
85
- ### OUTDIR
86
-
87
- Where to write generated files, including the cpphash repo itself.
88
- Unlike the other user variables, it is set in "makefile".
89
-
90
- ### MAIN
91
-
92
- The initial/main text file that "#include"s the others. It defaults
93
- to "main.yaml".
94
-
95
- ### SUFFIX
96
-
97
- The suffix of source files that will be dehash-ed so that they can be
98
- included by <MAIN >. <MAIN > may not end up including them all however they
99
- will be dehash-ed regardless. See user variables DIRS and SRCS. SUFFIX
100
- defaults to the file suffix of <MAIN >, including the '.'.
101
-
102
- ### DIRS
103
-
104
- The directories that are searched for files ending in <SUFFIX > to dehash.
105
- See user variable SRCS. It defaults to ".".
106
-
107
- ### SRCS
108
-
109
- The list of files to dehash. It defaults * .<SUFFIX > wildcards in
110
- the directories dpecified by DIRS
111
-
112
- ### PREFIX
113
-
114
- cpphash.mk generates a single filename named <PREFIX ><PROJTAG >.
115
- PREFIX defaults to "./myProj_ "
116
-
117
- ### PROJTAG
118
-
119
- PROJTAG is used to uniquely declar the project name. It defaults to "0"
120
- but it can be any character string of any length. To build different
121
- project variants from the same project directory, specify a different
122
- PROJTAG for each by overriding it on the make command line using, for
123
- example, make PROJTAG=1.
67
+ espmerge.sh parses yaml into common blocks and merges sections that
68
+ are repeated * and* named.
69
+
70
+ Common blocks are merged backwards in the output by referencing tags
71
+ specified in "id: <tag >" yaml lines. The first common block to
72
+ specify a unique <tag > is the destination for subsequent references.
73
+ lines in subsequent blocks are merged at the end of the block
74
+ that declared "id: <tag >" first. Array elements are each treated
75
+ as common blocks so it is possible to merge into an array element
76
+ as long as it declares itself using "id: <tag >".
77
+
78
+ After running espmerge.sh, it is advised to merge remaining repeated
79
+ sections that are not named with "id: <tag >" by piping the espmerge.sh
80
+ output through this pipe which sets up each section as a separate yaml
81
+ document and then removes yaml comments;
82
+ ```
83
+ awk '/^[[:alnum:]_]/{print "---"} | yq '... comments=""' esphome.yaml
84
+ ```
85
+ and then piping its out into yq to merge the multiple documents using:
124
86
125
- Argument -D_PROJTAG_ $(PROJTAG) is passed to the C-preprocessor so that
126
- text sources can vary the generated file using #if directives such as:
127
- ``` code
128
- #if _PROJTAG_foo
129
- # yaml code only for project foo goes here
130
- #endif
131
- Some other C preprocessor defines are passed as well. They can be
132
- found by reviewing the CPPDEFS definition in cpphash/cpphash.mk.
87
+ ```
88
+ yq eval-all '. as $item ireduce ({}; . *+ $item)'
89
+ ```
90
+ For an example of how this is done, refer to the yamlmerge.sh implementation.
91
+
92
+ yamlmerge.sh
93
+ ============
94
+ yamlmerge.sh: merge duplicate map keys in non-compliant yaml
95
+
96
+ Usage
97
+ -----
98
+ yamlmerge.sh: [ -okseEqh] [ -o outfile] <file.yaml>\n
99
+ -o|--outfile File to write to, else stdout.
100
+ -k|--keep Keep yaml comments.
101
+ -s|--sort Sort the map keys.
102
+ -e|--esphoist Hoist the esphome: and esp32: map keys to output first.
103
+ -E|--espmerge Enable esphome item merging using \" id\" : references.
104
+ -q|--quiet Do not output the number of merged components.
105
+ -h|--help Output this help.
106
+
107
+ <file.yaml> The yaml file to merge, else stdin.
108
+
109
+ yamlmerge.sh processes a single yaml files using these steps:
110
+ ```
111
+ First, yaml comments are optionally removed using yq.
112
+ Then espmerge.sh is run if requested.
113
+ Then map keys are each put in their own yaml document, using awk.
114
+ Then map keys are merged using yq.
115
+ Then map keys are optionally sorted using yq.
116
+ Then esphome map keys are optionally hoisted using yq.
133
117
```
134
118
135
- ## Generated files
136
- cpphash.mk generates output file <OUTDIR >/<PREFIX ><PROJTAG >.<SUFFIX >
137
- Intermediate C-preprocessed files used to generate it are stored in
138
- directory <OUTDIR >/<PREFIX ><PROJTAG >/
119
+ Installation
120
+ ============
139
121
140
- Both can be deleted using 'make clean' .
122
+ Please refer to INSTALL.md for information relating to cpphash installation .
141
123
142
- ## Other
124
+ How to use this repo
125
+ ====================
143
126
144
- There are some additional comments describing cpptest features in
145
- Makefile.
127
+ Use the cpphash.sh, dehash.sh, espmerge.sh or yamlmerge.sh scripts
128
+ individually or optionally use the Makefile.cpphash or Makefile.esphome
129
+ makefiles.
146
130
147
- You will note that cpphash.mk uses dehash.sh to remove the
148
- hash-style comments before running the files through the c-preprocessor.
149
- It leverages a sed script to do that and sets up dehash.sh flags
150
- to leave the C preprocessor directives.
131
+ Makefile.cpphash implements a cpphash project build mechanism using make.
132
+ See examples/stackoverflow for an example of how to use it,.
133
+ Makefile.cpphash implements a cpphash project build mechanism using make
134
+ specifically for esphome yaml files. See examples/esphome for an example
135
+ of how to use it.
151
136
152
- There are some aliases in file Bashrc also which may be helpful for
153
- issuing esphome commands.
137
+ To use either Makefile.cpphash or Makefile.esphome, copy one of them into
138
+ a project directory that does not already use make. If your project already
139
+ does, then you likely already know how to integrate them into your project.
154
140
155
- There is an optional esphome.mk that may be useful to those
156
- using this project with esphome projects, such as the example project.
157
- YMMV.
141
+ For an example of a larger project that uses cpphash and has customized
142
+ Makefile.esphome, see github.com/maartenSXM/GrowOS
158
143
159
144
# Credits
160
145
161
146
Thank you to Landon Rohatensky for the exemplary esphome yaml file
162
147
https://github.com/landonr/lilygo-tdisplays3-esphome used to demonstrate
163
148
configuration & build and also as used in the test subdirectory.
164
149
165
- # Disclaimers
166
-
167
- The author has not attempted to use cpphash with Visual Studio.
168
-
169
- # MacOS Note
170
-
171
- Note (repeated intentionally): on MacOS, you need GNU sed to run dehash.sh,
172
- which dehash.sh invokes. To install GNU sed, please do this:
173
- ```
174
- brew install gsed
175
- ```
176
- and then add this line to your .bashrc:
177
- ```
178
- export PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"
179
- ```
180
- and then 'source .bashrc' or logout and log back in.
181
-
0 commit comments