source: rtems-libbsd/CONTRIBUTING.md @ 301ee6e

55-freebsd-126-freebsd-12
Last change on this file since 301ee6e was e3e88e1, checked in by Sebastian Huber <sebastian.huber@…>, on 06/26/17 at 07:36:56

Add CONTRIBUTING.md

  • Property mode set to 100644
File size: 12.1 KB
Line 
1Guidelines for Developing and Contributing Code
2===============================================
3
4Introduction
5------------
6
7This guide aims to help developing and contributing code to the libbsd.  One
8goal of the libbsd is to stay in synchronization with FreeBSD.  This is only
9feasible if certain rules are in place.  Otherwise, managing more than a
10thousand imported source files will become too labour intensive eventually.
11
12What is in the Git Repository
13-----------------------------
14
15The libbsd a self-contained kit with FreeBSD and RTEMS components pre-merged.
16The Waf wscript in libbsd is automatically generated.
17
18Any changes to source in the `freebsd` directories will need to be merged
19upstream into our master FreeBSD checkout, the `freebsd-org` submodule.
20
21The repository contains two FreeBSD source trees.  In the `freebsd` directory
22are the so called *managed* FreeBSD sources used to build the BSD library.  The
23FreeBSD source in `freebsd-org` is the *master* version.  The
24`freebsd-to-rtems.py` script is used to transfer files between the two trees.
25In general terms, if you have modified managed FreeBSD sources, you will need
26to run the script in *revert* or *reverse* mode using the `-R` switch.  This
27will copy the source back to your local copy of the master FreeBSD source so
28you can run `git diff` against the upstream FreeBSD source.  If you want to
29transfer source files from the master FreeBSD source to the manged FreeBSD
30sources, then you must run the script in *forward* mode (the default).
31
32Organization
33------------
34
35The top level directory contains a few directories and files. The following
36are important to understand
37
38* `freebsd-to-rtems.py` - script to convert to and free FreeBSD and RTEMS trees,
39* `create-kernel-namespace.sh` - script to create the kernel namespace header <machine/rtems-bsd-kernel-namespace.h,
40* `wscript` - automatically generated,
41* `freebsd/` - from FreeBSD by script,
42* `rtemsbsd/` - RTEMS specific implementations of FreeBSD kernel support routines,
43* `testsuite/` - RTEMS specific tests, and
44* `libbsd.txt` - documentation in Asciidoc.
45
46Moving Code Between Managed and Master FreeBSD Source
47-----------------------------------------------------
48
49The script `freebsd-to-rtems.py` is used to copy code from FreeBSD to the
50rtems-libbsd tree and to reverse this process. This script attempts to
51automate this process as much as possible and performs some transformations
52on the FreeBSD code. Its command line arguments are shown below:
53
54```
55freebsd-to-rtems.py [args]
56  -?|-h|--help      print this and exit
57  -d|--dry-run      run program but no modifications
58  -D|--diff         provide diff of files between trees
59  -e|--early-exit   evaluate arguments, print results, and exit
60  -m|--makefile     Warning: depreciated and will be removed
61  -b|--buildscripts just generate the build scripts
62  -S|--stats        Print a statistics report
63  -R|--reverse      default FreeBSD -> RTEMS, reverse that
64  -r|--rtems        RTEMS Libbsd directory (default: '.')
65  -f|--freebsd      FreeBSD SVN directory (default: 'freebsd-org')
66  -v|--verbose      enable verbose output mode
67```
68
69In its default mode of operation, freebsd-to-rtems.py is used to copy code
70from FreeBSD to the rtems-libbsd tree and perform transformations.  In forward
71mode, the script may be requested to just generate the Waf script.
72
73In *reverse mode*, this script undoes those transformations and copies
74the source code back to the *master* FreeBSD tree. This allows us to do
75'git diff', evaluate changes made by the RTEMS Project, and report changes
76back to FreeBSD upstream.
77
78In either mode, the script may be asked to perform a dry-run or be verbose.
79Also, in either mode, the script is also smart enough to avoid copying over
80files which have not changed. This means that the timestamps of files are
81not changed unless the contents change. The script will also report the
82number of files which changed. In verbose mode, the script will print
83the name of the files which are changed.
84
85To add or update files in the RTEMS FreeBSD tree first run the *reverse mode*
86and move the current set of patches FreeBSD. The script may warn you if a file
87is not present at the destination for the direction. This can happen as files
88not avaliable at the FreeBSD snapshot point have been specially added to the
89RTEMS FreeBSD tree. Warnings can also appear if you have changed the list of
90files in libbsd.py. The reverse mode will result in the FreeBSD having
91uncommitted changes. You can ignore these. Once the reverse process has
92finished edit libbsd.py and add any new files then run the forwad mode to bring
93those files into the RTEMS FreeBSD tree.
94
95The following is an example forward run with no changes.
96
97```
98$ ./freebsd-to-rtems.py -v
99Verbose:                     yes (1)
100Dry Run:                     no
101Diff Mode Enabled:           no
102Only Generate Build Scripts: no
103RTEMS Libbsd Directory:      .
104FreeBSD SVN Directory:       freebsd-org
105Direction:                   forward
106Forward from FreeBSD GIT into  .
1070 file(s) were changed:
108```
109
110The script may also be used to generate a diff in either forward or reverse
111direction.
112
113You can add more than one verbose option (-v) to the command line and get more
114detail and debug level information from the command.
115
116FreeBSD Version of Imported Files and Directories
117-------------------------------------------------
118
119* *, trunk, 2017-04-04, 642b174daddbd0efd9bb5f242c43f4ab4db6869f.
120
121How to import code from FreeBSD
122-------------------------------
123
124* In case you import files from a special FreeBSD version, then update the list above.
125* Run `git status` and make sure your working directory is clean.
126* Run `./freebsd-to-rtems.py -R`
127* Run `./freebsd-to-rtems.py`
128* Run `git status` and make sure your working directory is clean.  If you see modified files, then the `freebsd-to-rtems.py` script needs to be fixed first.
129* Add the files to import to `libbsd.py`.
130* Run `./freebsd-to-rtems.py`
131* Immediately check in the imported files without the changes to `libbsd_waf.py`.  Do not touch the imported files yourself at this point.
132* Port the imported files to RTEMS.  See 'Rules for Modifying FreeBSD Source'.
133* Add a test to the testsuite if possible.
134* Run `./create-kernel-namespace.sh` if you imported kernel space headers.  Add only your new defines via `git add -p rtemsbsd/include/machine/rtems-bsd-kernel-namespace.h`.
135* Create one commit from this.
136
137The -S or --stats option generates reports the changes we have made to
138FreeBSD. If the code has been reserved into the original FreeBSD tree it will
139show nothing has changed. To see what we have change:
140
141```
142$ cd freebsd-org
143$ git checkout -- .
144$ cd ..
145$ ./freebsd-to-rtems.py -R -S -d
146 ```
147
148The report lists the files change based on the opacity level. The opacity is a
149measure on how much of a file differs from the original FreeBSD source. The
150lower the value the more transparent the source file it.
151
152Porting of User-Space Utilities
153------------------------------
154
155The theory behind the described method is to put all BSS and initialized data
156objects into a named section. This section then will be saved before the code is
157executed and restored after it has finished. This method limits to a single
158threaded execution of the application but minimizes the necessary changes to the
159original FreeBSD code.
160
161* Import and commit the unchanged source files like described above.
162* Add the files to the libbsd.py and build them.
163* Check the sources for everything that can be made const. This type of patches
164  should go back to the upstream FreeBSD sources.
165* Move static variables out of functions if necessary (search for
166  "<TAB>static"). These patches most likely will not be accepted into FreeBSD.
167* Add a rtems_bsd_command_PROGNAME() wrapper function to the source file
168  containing the main function (e.g. PROGNAME = pfctl). For an example look at
169  `rtems_bsd_command_pfctl()` in `freebsd/sbin/pfctl/pfctl.c`.
170* You probably have to use getopt_r() instead of getopt(). Have a look at
171  `freebsd/sbin/pfctl/pfctl.c`.
172* Build the libbsd without optimization.
173* Use the `userspace-header-gen.py` to generate some necessary header
174  files. It will generate one `rtems-bsd-PROGNAME-MODULE-data.h` per object file, one
175  `rtems-bsd-PROGNAME-namespace.h` and one `rtems-bsd-PROGNAME-data.h`. To call
176  the script, you have to compile the objects and afterwards run the helper
177  script with a call similar to this one:
178  `python ./userspace-header-gen.py build/arm-rtems4.12-xilinx_zynq_a9_qemu/freebsd/sbin/pfctl/*.o -p pfctl`
179  Replace the name (given via -p option) by the name of the userspace tool. It
180  has to match the name that is used in the RTEMS linker set further below.
181* If you regenerated files that have already been generated, you may have to
182  remove RTEMS-specific names from the namespace. The defaults (linker set names
183  and rtems_bsd_program_xxx) should already be filtered.
184* Put the generated header files into the same folder like the source files.
185* Include `PROGNAME-rtems-bsd-namespace.h` at the top of each source file and
186  the `PROGNAME-rtems-bsd-MODULE-data.h` after the include section of the
187  corresponding source files.
188* Include `machine/rtems-bsd-program.h` at the top of the include block in each
189  source file.
190* Create one compilable commit.
191
192Rules for Modifying FreeBSD Source
193----------------------------------
194
195Changes in FreeBSD files must be done using `__rtems__` C pre-processor guards.
196This makes synchronization with the FreeBSD upstream easier and is very
197important.  Patches which do not follow these rules will be rejected.  Only add
198lines.  If your patch contains lines starting with a `-`, then this is wrong.
199Subtract code by added `#ifndef __rtems__`.  For example:
200
201```c
202/* Global variables for the kernel. */
203
204#ifndef __rtems__
205/* 1.1 */
206extern char kernelname[MAXPATHLEN];
207#endif /* __rtems__ */
208
209extern int tick;                        /* usec per tick (1000000 / hz) */
210```
211
212```c
213#if defined(_KERNEL) || defined(_WANT_FILE)
214#ifdef __rtems__
215#include <rtems/libio_.h>
216#include <sys/fcntl.h>
217#endif /* __rtems__ */
218/*
219 * Kernel descriptor table.
220 * One entry for each open kernel vnode and socket.
221 *
222 * Below is the list of locks that protects members in struct file.
223 *
224 * (f) protected with mtx_lock(mtx_pool_find(fp))
225 * (d) cdevpriv_mtx
226 * none not locked
227 */
228```
229
230```c
231extern int profprocs;                   /* number of process's profiling */
232#ifndef __rtems__
233extern volatile int ticks;
234#else /* __rtems__ */
235#include <rtems/score/watchdogimpl.h>
236#define ticks _Watchdog_Ticks_since_boot
237#endif /* __rtems__ */
238
239#endif /* _KERNEL */
240```
241
242Add nothing (even blank lines) before or after the `__rtems__` guards.  Always
243include a `__rtems__` in the guards to make searches easy, so use
244
245* `#ifndef __rtems__`,
246* `#ifdef __rtems__`,
247* `#else /* __rtems__ */`, and
248* `#endif /* __rtems__ */`.
249
250The guards must start at the begin of the line.  Examples for wrong guards:
251
252```c
253static void
254guards_must_start_at_the_begin_of_the_line(int j)
255{
256
257        /* WRONG */
258        #ifdef __rtems__
259        return (j + 1);
260        #else /* __rtems__ */
261        return (j + 2);
262        #endif /* __rtems__ */
263}
264
265static void
266missing_rtems_comments_in_the_guards(int j)
267{
268
269#ifdef __rtems__
270        return (j + 3);
271/* WRONG */
272#else
273        return (j + 4);
274#endif
275}
276```
277
278The FreeBSD build and configuration system uses option header files, e.g.
279`#include "opt_xyz.h"` in an unmodified FreeBSD file.  This include is
280transformed by the import script into `#include <rtems/bsd/local/opt_xyz.h>`.  Do
281not disable option header includes via guards.  Instead, add an empty option
282header, e.g. `touch rtemsbsd/include/rtems/bsd/local/opt_xyz.h`.
283```c
284/* WRONG */
285#ifndef __rtems__
286#include <rtems/bsd/local/opt_xyz.h>
287#endif /* __rtems__ */
288```
289
290In general, provide empty header files and do not guard includes.
291
292For new code use
293[STYLE(9)](http://www.freebsd.org/cgi/man.cgi?query=style&apropos=0&sektion=9).
294
295Do not format original FreeBSD code.  Do not perform white space changes even
296if you get git commit warnings.
297
298Automatically Generated FreeBSD Files
299-------------------------------------
300
301Some source and header files are automatically generated during the FreeBSD
302build process.  The `Makefile.todo` file performs this manually.  The should be
303included in `freebsd-to-rtems.py` script some time in the future.  For details,
304see also
305[KOBJ(9)](http://www.freebsd.org/cgi/man.cgi?query=kobj&sektion=9&apropos=0).
Note: See TracBrowser for help on using the repository browser.