Debian Codegraph @

samples/hello2/README.md

raw
1 # Sample "hello2"
2
3 This sample is essentially the same as [Sample "hello"](../hello/README.md),
4 but it demonstrates how to compile multiple source files into a single binary
5 using the ESSTRA Core.
6
7 Please note that this document assumes you have completed all the steps
8 outlined in [Sample "hello"](../hello/README.md).
9
10 ## Source Code to be Compiled
11
12 In this sample, we use the [ESSTRA Core](/core/README.md) to compile
13 the two source files [`hello_main.c`](./hello_main.c) and [`hello_sub.c`](./hello_sub.c)
14 into a single binary named `hello2`.
15 The binary `hello2` is a program that, similar to [Sample "hello"](../hello/README.md),
16 simply prints `Hello, world!` to the standard output.
17
18 **`hello_main.c`**:
19 ```c
20 #include "hello_sub.h"
21
22 int main(void)
23 {
24 sub_puts("Hello, world!");
25 return 0;
26 }
27 ```
28
29 **`hello_sub.c`**:
30 ```c
31 #include <stdio.h>
32
33 void sub_puts(const char *str)
34 {
35 puts(str);
36 }
37 ```
38
39 In this program, the function `sub_puts()` provided by
40 [`hello_sub.c`](./hello_sub.c) is called within the `main()` function in
41 [`hello_main.c`](./hello_main.c). Therefore, we have prepared a header file
42 [`hello_sub.h`](./hello_sub.h) to use the function defined in
43 [`hello_sub.c`](./hello_sub.c) externally:
44
45 **`hello_sub.h`**:
46 ```c
47 #ifndef _HELLO_SUB_H_
48
49 extern void sub_puts(const char *);
50
51 #endif
52 ```
53
54 Similar to [Sample "hello"](../hello/README.md), the license is specified at
55 the beginning of each file as follows:
56
57 **`hello_main.c`**:
58 ```c
59 // SPDX-License-Identifier: MIT
60 ```
61
62 **`hello_sub.c`**:
63 ```c
64 // SPDX-License-Identifier: BSD-3-Clause
65 ```
66
67 **`hello_sub.h`**:
68 ```c
69 // SPDX-License-Identifier: LGPL-2.1-or-later
70 ```
71
72 Please note that these licenses are only for verifying the operation of the
73 ESSTRA and do not represent the actual licenses of the source code. For the
74 actual source code licenses, please refer to the [LICENSE](../../LICENSE) file
75 in the top directory.
76
77 ## Compiling with ESSTRA Core
78
79 We will compile the source files shown above to generate the binary
80 `hello2`. By using the
81 [ESSTRA Core](/core/README.md) during compilation,
82 metadata is embedded into `hello2`.
83 Since we are using GCC version 11 on an x86\_64 PC, the command line would be as follows:
84
85 ```sh
86 $ gcc -fplugin=/usr/local/lib/gcc/x86_64-linux-gnu/11/plugin/esstracore.so hello_main.c hello_sub.c -o hello2
87 ```
88
89 If you have already installed the [Spec File](/README.md#installing-spec-file), the
90 [ESSTRA Core](/core/README.md) will intervene in the compilation
91 without needing to specify the `-fplugin=` option, yielding the same result as above:
92
93 ```sh
94 $ gcc hello_main.c hello_sub.c -o hello2
95 ```
96
97 The output of the generated binary `hello2` is shown below:
98
99 ```sh
100 $ ./hello2
101 Hello, world!
102 ```
103
104 ## Verifying Metadata in the Binary
105
106 Use the `show` command of the [ESSTRA utility](/util/README.md) to verify
107 the metadata within `hello2`:
108
109 ```sh
110 $ esstra show hello2
111 ```
112
113 And you will get a result as follows:
114
115 ```yaml
116 #
117 # BinaryFileName: hello2
118 # BinaryPath: /home/snagao/esstra/samples/hello2/hello2
119 #
120 Headers:
121 ToolName: ESSTRA Core
122 ToolVersion: 0.2.0
123 DataFormatVersion: 0.1.0
124 InputFileNames:
125 - hello_main.c
126 - hello_sub.c
127 SourceFiles:
128 - Directory: /home/snagao/esstra/samples/hello2
129 Files:
130 - File: hello_main.c
131 SHA1: f7f5c447d68fd9685594a31cb10c8d8b1dd5ebd6
132 - File: hello_sub.c
133 SHA1: cfb72998ae0242237fa42c8bcf61ee5887137392
134 - File: hello_sub.h
135 SHA1: 3e5b3ed1aed966c0e0c183eac8fe6ea02dfa62a0
136 - Directory: /usr/include
137 Files:
138 - File: features-time64.h
139 SHA1: 57c3c8093c3af70e5851f6d498600e2f6e24fdeb
140 - File: features.h
141 SHA1: d8725bb98129d6d70ddcbf010021c2841db783f7
142 - File: stdc-predef.h
143 SHA1: 2fef05d80514ca0be77efec90bda051cf87d771f
144 - File: stdio.h
145 SHA1: c7181b48c4194cd122024971527aab4056baf600
146 - Directory: /usr/include/x86_64-linux-gnu/bits
147 Files:
148 - File: floatn-common.h
149 SHA1: 3f37104123a2e6180621331c1da87125808e47bd
150 - File: floatn.h
151 SHA1: 806b759ab6894a09a3b3a422eec5f2414ba7dab7
152 - File: libc-header-start.h
153 SHA1: e0a400c194cd3962a342a6504a441920163b799c
154 - File: long-double.h
155 SHA1: 4e3f5928e816ad29079d1c7d75f3a510a0939ffb
156 - File: stdio.h
157 SHA1: 521d7cd0f6572f70122386784120cc55d84899bc
158 - File: stdio2.h
159 SHA1: 6c3ee923db9679a79941a688de72e114a794fc54
160 - File: stdio_lim.h
161 SHA1: 6210c8ae410ee0f39a6096b0adb9fa86febd3517
162 - File: time64.h
163 SHA1: ab2017da21608498b58eea37b2aa6a3387ee978c
164 - File: timesize.h
165 SHA1: f1dd8d62a4d75288654626933edfc82ccf2394a7
166 - File: types.h
167 SHA1: e5893a9c4c523615c73a51feb9680279608027c6
168 - File: typesizes.h
169 SHA1: ee94b5a60d007c23bdda9e5c46c8ba40f4eb402c
170 - File: wordsize.h
171 SHA1: 281ddd3c93f1e8653e809a45b606574c9b691092
172 - Directory: /usr/include/x86_64-linux-gnu/bits/types
173 Files:
174 - File: FILE.h
175 SHA1: 497924e329d53517631713ae52acb73e870d7d65
176 - File: __FILE.h
177 SHA1: 274242343e85d1c06e7f5ccc5abf15e120f6e957
178 - File: __fpos64_t.h
179 SHA1: ac38e294b004f6e2bf18f1c55e03dc80f48d6830
180 - File: __fpos_t.h
181 SHA1: 760ef77769ac1921f4b1f908cbf06863e2506775
182 - File: __mbstate_t.h
183 SHA1: e3a4f2ee55e635520db0b4610d2b361e9ce41de7
184 - File: struct_FILE.h
185 SHA1: 1dbf8bac589cb09e09aa4c1d36913e549a57bcf0
186 - Directory: /usr/include/x86_64-linux-gnu/gnu
187 Files:
188 - File: stubs-64.h
189 SHA1: f7603fa3908b56e9d1b33c91590db3252e13a799
190 - File: stubs.h
191 SHA1: be168037b7503a82b1cf694cdbac8c063bb6e476
192 - Directory: /usr/include/x86_64-linux-gnu/sys
193 Files:
194 - File: cdefs.h
195 SHA1: a419a6372029d89ba38ada0811d34f51df8d09b7
196 - Directory: /usr/lib/gcc/x86_64-linux-gnu/11/include
197 Files:
198 - File: stdarg.h
199 SHA1: fa23f49da8a0a5068b781dff7182f1a1c363dc30
200 - File: stddef.h
201 SHA1: 0de70008ffa3f198baf55c7b3f3d03b4ca11c21f
202 ```
203
204 You can see that the metadata includes the source files specified during
205 compilation: [`hello_main.c`](./hello_main.c), [`hello_sub.c`](./hello_sub.c),
206 and [`hello_sub.h`](./hello_sub.h).
207 Additionally, similar to [Sample "hello"](../hello/README.md),
208 it also includes information about all header files that are explicitly or
209 implicitly `#include`'d.
210
211 ## Adding License Information to Metadata
212
213 Here, similar to
214 [Sample "hello"](../hello/README.md#adding-license-information-to-metadata),
215 we will use the license information file
216 [`SPDX2TV_esstra.spdx`](../output-examples/SPDX2TV_esstra.spdx)
217 that has already been prepared.
218 Next, you can add license information to the metadata in the binary
219 `hello2` using the following command:
220
221 ```sh
222 $ esstra update hello2 -i SPDX2TV_esstra.spdx
223 * processing 'hello2'...
224 * done.
225 ```
226
227 To display the metadata content of the binary `hello2`, use the following command:
228
229 ```sh
230 $ esstra show hello2
231 ```
232
233 You will get the result shown below:
234
235 ```yaml
236 #
237 # BinaryFileName: hello2
238 # BinaryPath: /home/snagao/esstra/samples/hello2/hello2
239 #
240 Headers:
241 ToolName: ESSTRA Core
242 ToolVersion: 0.2.0
243 DataFormatVersion: 0.1.0
244 InputFileNames:
245 - hello_main.c
246 - hello_sub.c
247 SourceFiles:
248 - Directory: /home/snagao/esstra/samples/hello2
249 Files:
250 - File: hello_main.c
251 SHA1: f7f5c447d68fd9685594a31cb10c8d8b1dd5ebd6
252 LicenseInfo:
253 - MIT
254 - File: hello_sub.c
255 SHA1: cfb72998ae0242237fa42c8bcf61ee5887137392
256 LicenseInfo:
257 - BSD-3-Clause
258 - File: hello_sub.h
259 SHA1: 3e5b3ed1aed966c0e0c183eac8fe6ea02dfa62a0
260 LicenseInfo:
261 - LGPL-2.1-or-later
262 - Directory: /usr/include
263 Files:
264 - File: features-time64.h
265 SHA1: 57c3c8093c3af70e5851f6d498600e2f6e24fdeb
266
267 :
268
269 ```
270
271 From the above result, you can see that the `LicenseInfo` tags have been added
272 to the metadata for the files
273 [`hello_main.c`](./hello_main.c),
274 [`hello_sub.c`](./hello_sub.c), and
275 [`hello_sub.h`](./hello_sub.h),
276 with the values `MIT`, `BSD-3-Clause`, and `LGPL-2.1-or-later`, respectively.
277
278 ## Summary
279
280 In this sample, we confirmed that by using the
281 [ESSTRA Core](/core/README.md) during the compilation of multiple
282 source files into a single binary, the metadata includes information on
283 all the source files involved in the compilation and
284 all the header files they `#include`.
285
286 Additionally, by using the [ESSTRA Utility](/util/README.md) to add license
287 information to the metadata,
288 we updated the metadata in the binary `hello2` and confirmed that the license
289 information declared in each source file was added to the metadata.