source: rtems-libbsd/CONTRIBUTING.md @ 0cb4075

5-freebsd-12
Last change on this file since 0cb4075 was cd7d52d, checked in by Sebastian Huber <sebastian.huber@…>, on Oct 10, 2017 at 12:35:40 PM

Update CONTRIBUTING.md

  • Property mode set to 100644
File size: 12.7 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](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  "\tstatic"). 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 [pfctl.c](freebsd/sbin/pfctl/pfctl.c).
170* You probably have to use getopt_r() instead of getopt(). Have a look at
171  [pfctl.c](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_.*) should already be filtered.
184* Put the generated header files into the same folder like the source files.
185* At the top of each source file place the following right after the user-space header:
186  ```c
187  #ifdef __rtems__
188  #include <machine/rtems-bsd-program.h>
189  #include "rtems-bsd-PROGNAME-namespace.h"
190  #endif /* __rtems__ */
191  ```
192  The following command may be useful:
193  ```
194  sed -i 's%#include <machine/rtems-bsd-user-space.h>%#include <machine/rtems-bsd-user-space.h>\n#ifdef __rtems__\n#include <machine/rtems-bsd-program.h>\n#include "rtems-bsd-PROGNAME-namespace.h"\n#endif /* __rtems__ */%' *.c
195  ```
196* At the bottom of each source file place the follwing:
197  ```c
198  #ifdef __rtems__
199  #include "rtems-bsd-PROGNAME-FILE-data.h"
200  #endif /* __rtems__ */
201  ```
202  The following command may be useful:
203  ```
204  for i in *.c ; do n=$(basename $i .c) ; echo -e "#ifdef __rtems__\n#include \"rtems-bsd-PROGNAME-$n-data.h\"\n#endif /* __rtems__ */" >> $i ; done
205  ```
206* Create one compilable commit.
207
208Rules for Modifying FreeBSD Source
209----------------------------------
210
211Changes in FreeBSD files must be done using `__rtems__` C pre-processor guards.
212This makes synchronization with the FreeBSD upstream easier and is very
213important.  Patches which do not follow these rules will be rejected.  Only add
214lines.  If your patch contains lines starting with a `-`, then this is wrong.
215Subtract code by added `#ifndef __rtems__`.  For example:
216
217```c
218/* Global variables for the kernel. */
219
220#ifndef __rtems__
221/* 1.1 */
222extern char kernelname[MAXPATHLEN];
223#endif /* __rtems__ */
224
225extern int tick;                        /* usec per tick (1000000 / hz) */
226```
227
228```c
229#if defined(_KERNEL) || defined(_WANT_FILE)
230#ifdef __rtems__
231#include <rtems/libio_.h>
232#include <sys/fcntl.h>
233#endif /* __rtems__ */
234/*
235 * Kernel descriptor table.
236 * One entry for each open kernel vnode and socket.
237 *
238 * Below is the list of locks that protects members in struct file.
239 *
240 * (f) protected with mtx_lock(mtx_pool_find(fp))
241 * (d) cdevpriv_mtx
242 * none not locked
243 */
244```
245
246```c
247extern int profprocs;                   /* number of process's profiling */
248#ifndef __rtems__
249extern volatile int ticks;
250#else /* __rtems__ */
251#include <rtems/score/watchdogimpl.h>
252#define ticks _Watchdog_Ticks_since_boot
253#endif /* __rtems__ */
254
255#endif /* _KERNEL */
256```
257
258Add nothing (even blank lines) before or after the `__rtems__` guards.  Always
259include a `__rtems__` in the guards to make searches easy, so use
260
261* `#ifndef __rtems__`,
262* `#ifdef __rtems__`,
263* `#else /* __rtems__ */`, and
264* `#endif /* __rtems__ */`.
265
266The guards must start at the begin of the line.  Examples for wrong guards:
267
268```c
269static void
270guards_must_start_at_the_begin_of_the_line(int j)
271{
272
273        /* WRONG */
274        #ifdef __rtems__
275        return (j + 1);
276        #else /* __rtems__ */
277        return (j + 2);
278        #endif /* __rtems__ */
279}
280
281static void
282missing_rtems_comments_in_the_guards(int j)
283{
284
285#ifdef __rtems__
286        return (j + 3);
287/* WRONG */
288#else
289        return (j + 4);
290#endif
291}
292```
293
294The FreeBSD build and configuration system uses option header files, e.g.
295`#include "opt_xyz.h"` in an unmodified FreeBSD file.  This include is
296transformed by the import script into `#include <rtems/bsd/local/opt_xyz.h>`.  Do
297not disable option header includes via guards.  Instead, add an empty option
298header, e.g. `touch rtemsbsd/include/rtems/bsd/local/opt_xyz.h`.
299```c
300/* WRONG */
301#ifndef __rtems__
302#include <rtems/bsd/local/opt_xyz.h>
303#endif /* __rtems__ */
304```
305
306In general, provide empty header files and do not guard includes.
307
308For new code use
309[STYLE(9)](http://www.freebsd.org/cgi/man.cgi?query=style&apropos=0&sektion=9).
310
311Do not format original FreeBSD code.  Do not perform white space changes even
312if you get git commit warnings.
313
314Automatically Generated FreeBSD Files
315-------------------------------------
316
317Some source and header files are automatically generated during the FreeBSD
318build process.  The `Makefile.todo` file performs this manually.  The should be
319included in `freebsd-to-rtems.py` script some time in the future.  For details,
320see also
321[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.