Statistics
| Branch: | Tag: | Revision:

root / README.hacking @ 9db640f5

History | View | Annotate | Download (10.8 kB)

1 5d69a524 jcorgan
# -*- Outline -*-
2 5d69a524 jcorgan
#
3 78c121b8 jcorgan
# Copyright 2004,2007,2008,2009 Free Software Foundation, Inc.
4 5d69a524 jcorgan
# 
5 5d69a524 jcorgan
# This file is part of GNU Radio
6 5d69a524 jcorgan
# 
7 5d69a524 jcorgan
# GNU Radio is free software; you can redistribute it and/or modify
8 5d69a524 jcorgan
# it under the terms of the GNU General Public License as published by
9 937b719d eb
# the Free Software Foundation; either version 3, or (at your option)
10 5d69a524 jcorgan
# any later version.
11 5d69a524 jcorgan
# 
12 5d69a524 jcorgan
# GNU Radio is distributed in the hope that it will be useful,
13 5d69a524 jcorgan
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14 5d69a524 jcorgan
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 5d69a524 jcorgan
# GNU General Public License for more details.
16 5d69a524 jcorgan
# 
17 5d69a524 jcorgan
# You should have received a copy of the GNU General Public License
18 5d69a524 jcorgan
# along with GNU Radio; see the file COPYING.  If not, write to
19 86f5c924 eb
# the Free Software Foundation, Inc., 51 Franklin Street,
20 86f5c924 eb
# Boston, MA 02110-1301, USA.
21 5d69a524 jcorgan
# 
22 5d69a524 jcorgan
23 5d69a524 jcorgan
Random notes on coding conventions, some explanations about why things
24 5d69a524 jcorgan
aren't done differently, etc, etc,
25 5d69a524 jcorgan
26 5d69a524 jcorgan
27 eb5c6240 eb
* Boost 1.35
28 eb5c6240 eb
29 eb5c6240 eb
Until boost 1.35 or later is common in distributions, you'll need to
30 eb5c6240 eb
build boost from source yourself.  See README.building-boost.
31 eb5c6240 eb
32 eb5c6240 eb
Also, when running make distcheck you'll need to provide the
33 eb5c6240 eb
DISTCHECK_CONFIGURE_FLAGS.  E.g.,
34 eb5c6240 eb
35 eb5c6240 eb
  $ make distcheck DISTCHECK_CONFIGURE_FLAGS=--with-boost=/opt/boost_1_36_0
36 eb5c6240 eb
37 eb5c6240 eb
38 5d69a524 jcorgan
* C++ and Python
39 5d69a524 jcorgan
40 5d69a524 jcorgan
GNU Radio is now a hybrid system.  Some parts of the system are built
41 5d69a524 jcorgan
in C++ and some of it in Python.  In general, prefer Python to C++.
42 5d69a524 jcorgan
Signal processing primitives are still built in C++ for performance.
43 5d69a524 jcorgan
44 5d69a524 jcorgan
45 5d69a524 jcorgan
* C++ namespaces
46 5d69a524 jcorgan
47 5d69a524 jcorgan
In the cleanup process, I considered putting everything in the
48 5d69a524 jcorgan
gnuradio namespace and dropping the Gr|gr prefix.  In fact, I think
49 5d69a524 jcorgan
it's probably the right idea, but when I tested it out, I ran into
50 5d69a524 jcorgan
problems with SWIG's handling of namespaces.  Bottom line, SWIG
51 5d69a524 jcorgan
(1.3.21) got confused and generated bad code when I started playing
52 5d69a524 jcorgan
around with namespaces in a not particularly convoluted way.  I saw
53 5d69a524 jcorgan
problems using the boost::shared_ptr template in combination with
54 5d69a524 jcorgan
classes defined in the gnuradio namespace.  It wasn't pretty...
55 5d69a524 jcorgan
56 5d69a524 jcorgan
57 5d69a524 jcorgan
* Naming conventions
58 5d69a524 jcorgan
59 5d69a524 jcorgan
Death to CamelCaseNames!  We've returned to a kinder, gentler era.
60 5d69a524 jcorgan
We're now using the "STL style" naming convention with a couple of
61 5d69a524 jcorgan
modifications since we're not using namespaces.
62 5d69a524 jcorgan
63 5d69a524 jcorgan
With the exception of macros and other constant values, all
64 5d69a524 jcorgan
identifiers shall be lower case with words_separated_like_this.
65 5d69a524 jcorgan
66 5d69a524 jcorgan
Macros and constant values (e.g., enumerated values,
67 5d69a524 jcorgan
static const int FOO = 23) shall be in UPPER_CASE.
68 5d69a524 jcorgan
69 5d69a524 jcorgan
70 5d69a524 jcorgan
** Global names
71 5d69a524 jcorgan
72 5d69a524 jcorgan
All globally visible names (types, functions, variables, consts, etc)
73 5d69a524 jcorgan
shall begin with a "package prefix", followed by an '_'.  The bulk of
74 5d69a524 jcorgan
the code in GNU Radio logically belongs to the "gr" package, hence
75 5d69a524 jcorgan
names look like gr_open_file (...).
76 5d69a524 jcorgan
77 5d69a524 jcorgan
Large coherent bodies of code may use other package prefixes, but
78 5d69a524 jcorgan
let's try to keep them to a well thought out list.  See the list
79 5d69a524 jcorgan
below.
80 5d69a524 jcorgan
81 5d69a524 jcorgan
*** Package prefixes
82 5d69a524 jcorgan
83 5d69a524 jcorgan
These are the current package prefixes:
84 5d69a524 jcorgan
85 5d69a524 jcorgan
    gr_		Almost everything
86 5d69a524 jcorgan
87 5d69a524 jcorgan
    gri_	Implementation primitives.  Sometimes we
88 5d69a524 jcorgan
		have both a gr_<foo> and a gri_<foo>.  In that case,
89 5d69a524 jcorgan
		gr_<foo> would be derived from gr_block and gri_<foo>
90 5d69a524 jcorgan
		would be the low level guts of the function.
91 5d69a524 jcorgan
92 5d69a524 jcorgan
    atsc_	Code related to the Advanced Television
93 5d69a524 jcorgan
	       	Standards Committee HDTV implementation
94 5d69a524 jcorgan
95 5d69a524 jcorgan
    usrp_	Universal Software Radio Peripheral
96 5d69a524 jcorgan
97 5d69a524 jcorgan
    qa_		Quality Assurance.  Test code.
98 5d69a524 jcorgan
99 5d69a524 jcorgan
100 5d69a524 jcorgan
** Class data members (instance variables)
101 5d69a524 jcorgan
102 5d69a524 jcorgan
All class data members shall begin with d_<foo>.
103 5d69a524 jcorgan
104 5d69a524 jcorgan
The big win is when you're staring at a block of code it's obvious
105 5d69a524 jcorgan
which of the things being assigned to persist outside of the block.
106 5d69a524 jcorgan
This also keeps you from having to be creative with parameter names
107 5d69a524 jcorgan
for methods and constructors.  You just use the same name as the
108 5d69a524 jcorgan
instance variable, without the d_. 
109 5d69a524 jcorgan
110 5d69a524 jcorgan
class gr_wonderfulness {
111 5d69a524 jcorgan
  std::string	d_name;
112 5d69a524 jcorgan
  double	d_wonderfulness_factor;
113 5d69a524 jcorgan
114 5d69a524 jcorgan
public:
115 5d69a524 jcorgan
  gr_wonderfulness (std::string name, double wonderfulness_factor)
116 5d69a524 jcorgan
    : d_name (name), d_wonderfulness_factor (wonderfulness_factor)
117 5d69a524 jcorgan
  {
118 5d69a524 jcorgan
    ...
119 5d69a524 jcorgan
  }
120 5d69a524 jcorgan
  ...
121 5d69a524 jcorgan
};
122 5d69a524 jcorgan
123 5d69a524 jcorgan
124 5d69a524 jcorgan
** Class static data members (class variables)
125 5d69a524 jcorgan
126 5d69a524 jcorgan
All class static data members shall begin with s_<foo>.
127 5d69a524 jcorgan
128 5d69a524 jcorgan
129 5d69a524 jcorgan
** File names
130 5d69a524 jcorgan
131 5d69a524 jcorgan
Each significant class shall be contained in it's own file.  The
132 5d69a524 jcorgan
declaration of class gr_foo shall be in gr_foo.h, the definition in
133 5d69a524 jcorgan
gr_foo.cc.
134 5d69a524 jcorgan
135 5d69a524 jcorgan
136 5d69a524 jcorgan
137 5d69a524 jcorgan
* Storage management
138 5d69a524 jcorgan
139 5d69a524 jcorgan
Strongly consider using the boost smart pointer templates, scoped_ptr
140 5d69a524 jcorgan
and shared_ptr.  scoped_ptr should be used for locals that contain
141 5d69a524 jcorgan
pointers to objects that we need to delete when we exit the current
142 5d69a524 jcorgan
scope.  shared_ptr implements transparent reference counting and is a
143 5d69a524 jcorgan
major win.  You never have to worry about calling delete.  The right
144 5d69a524 jcorgan
thing happens.
145 5d69a524 jcorgan
146 5d69a524 jcorgan
See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
147 5d69a524 jcorgan
148 5d69a524 jcorgan
149 5d69a524 jcorgan
* Unit tests
150 5d69a524 jcorgan
151 5d69a524 jcorgan
Build unit tests for everything non-trivial and run them after every
152 5d69a524 jcorgan
change.  Check out Extreme Programming: 
153 5d69a524 jcorgan
http://c2.com/cgi/wiki?ExtremeProgrammingRoadmap
154 5d69a524 jcorgan
155 5d69a524 jcorgan
Unit tests should also be written for all examples.  This should kill
156 5d69a524 jcorgan
off the bit rot we've been plagued with.
157 5d69a524 jcorgan
158 5d69a524 jcorgan
** C++ unit tests
159 5d69a524 jcorgan
160 5d69a524 jcorgan
For C++ we're using the cppunit framework.  cppunit has its bad
161 5d69a524 jcorgan
smells, but it's mostly workable.  http://cppunit.sf.net
162 5d69a524 jcorgan
163 5d69a524 jcorgan
Currently each directory <dirname> contains files qa_<dirname>.{h,cc}
164 5d69a524 jcorgan
that bring together all the qa_<foo> test suites in the directory.
165 5d69a524 jcorgan
We ought to be able to automate this without too much trouble.
166 5d69a524 jcorgan
167 5d69a524 jcorgan
The directory gnuradio-core/src/tests contains programs that run
168 5d69a524 jcorgan
the tests.  test_all runs all of the registered C++ unit tests.
169 5d69a524 jcorgan
170 5d69a524 jcorgan
As far as I can tell, the cppunit TestFactoryRegistry maybe able to be
171 5d69a524 jcorgan
tricked into doing what we want.  As is, I don't think it's enough by
172 5d69a524 jcorgan
itself, since there's nothing dragging the qa* files out of the
173 5d69a524 jcorgan
library and into the program.   I haven't tested out this idea.
174 5d69a524 jcorgan
175 5d69a524 jcorgan
** Python unit tests
176 5d69a524 jcorgan
177 5d69a524 jcorgan
We use the standard unittest package for unit testing of Python code.
178 5d69a524 jcorgan
179 5d69a524 jcorgan
180 5a286457 jcorgan
* Subversion line ending styles
181 5a286457 jcorgan
182 5a286457 jcorgan
All text files in the tree should have the subversion property 
183 5a286457 jcorgan
'svn:eol-style' set to 'native', with the following exceptions:
184 5a286457 jcorgan
185 5a286457 jcorgan
config/*.m4
186 5a286457 jcorgan
configure.ac
187 5a286457 jcorgan
gr-howto-write-a-block/config/*.m4
188 5a286457 jcorgan
gr-howto-write-a-block/configure.ac
189 5a286457 jcorgan
190 5a286457 jcorgan
The easiest way to ensure this is to add or edit the following lines in
191 5a286457 jcorgan
your svn client configuration file (~/.subversion/config):
192 5a286457 jcorgan
193 5a286457 jcorgan
enable-auto-props=yes
194 5a286457 jcorgan
195 5a286457 jcorgan
[auto-props]
196 5a286457 jcorgan
*.c = svn:eol-style=native
197 5a286457 jcorgan
*.cc = svn:eol-style=native  
198 5a286457 jcorgan
*.i = svn:eol-style=native
199 5a286457 jcorgan
*.h = svn:eol-style=native
200 5a286457 jcorgan
*.am = svn:eol-style=native
201 5a286457 jcorgan
*.py = svn:eol-style=native
202 5a286457 jcorgan
*.ac = svn:eol-style=LF
203 5a286457 jcorgan
*.m4 = svn:eol-style=LF
204 5a286457 jcorgan
205 5d69a524 jcorgan
* Misc tips
206 5d69a524 jcorgan
207 5d69a524 jcorgan
ccache, a compiler cache, can really speed up your builds.
208 5d69a524 jcorgan
See http://ccache.samba.org/
209 5d69a524 jcorgan
210 5d69a524 jcorgan
Be sure to create links for gcc and g++
211 d00c31fe eb
212 d00c31fe eb
213 d00c31fe eb
* Standard command line options
214 d00c31fe eb
215 d00c31fe eb
When writing programs that are executable from the command line,
216 d00c31fe eb
please follow these guidelines for command line argument names (short
217 d00c31fe eb
and long) and types of the arguments.  We list them below using the
218 d00c31fe eb
Python optparse syntax.  In general, the default value should be coded
219 d00c31fe eb
into the help string using the "... [default=%default]" syntax.
220 d00c31fe eb
221 d00c31fe eb
** Mandatory options by gr_block
222 d00c31fe eb
223 d00c31fe eb
*** USRP source
224 d00c31fe eb
225 d00c31fe eb
Any program using a USRP source (usrp.source_*) shall include:
226 d00c31fe eb
227 d00c31fe eb
  add_option("", "--which-usrp", type="intx", default=0,
228 d00c31fe eb
	     help="select which USRP to use [default=%default]")
229 d00c31fe eb
230 d00c31fe eb
  add_option("-R", "--rx-subdev-spec", type="subdev", default=(0, 0),
231 d00c31fe eb
             help="select USRP Rx side A or B [default=A]")
232 d00c31fe eb
233 d00c31fe eb
You are free to change the default if it makes sense in your application.
234 d00c31fe eb
235 d00c31fe eb
236 d00c31fe eb
*** USRP sink
237 d00c31fe eb
238 d00c31fe eb
Any program using a USRP sink (usrp.sink_*) shall include:
239 d00c31fe eb
240 d00c31fe eb
  add_option("", "--which-usrp", type="intx", default=0,
241 d00c31fe eb
	     help="select which USRP to use [default=%default]")
242 d00c31fe eb
243 d00c31fe eb
  add_option("-T", "--tx-subdev-spec", type="subdev", default=(0, 0),
244 d00c31fe eb
             help="select USRP Tx side A or B [default=A]")
245 d00c31fe eb
246 d00c31fe eb
You are free to change the default if it makes sense in your application.
247 d00c31fe eb
248 d00c31fe eb
249 d00c31fe eb
*** Audio source
250 d00c31fe eb
251 d00c31fe eb
Any program using an audio source shall include:
252 d00c31fe eb
253 d00c31fe eb
  add_option("-I", "--audio-input", type="string", default="",
254 d00c31fe eb
             help="pcm input device name.  E.g., hw:0,0 or /dev/dsp")
255 d00c31fe eb
256 d00c31fe eb
The default must be "".  This allows an audio module-dependent default
257 d00c31fe eb
to be specified in the user preferences file.
258 d00c31fe eb
259 d00c31fe eb
260 d00c31fe eb
*** Audio sink
261 d00c31fe eb
262 d00c31fe eb
  add_option("-O", "--audio-output", type="string", default="",
263 d00c31fe eb
             help="pcm output device name.  E.g., hw:0,0 or /dev/dsp")
264 d00c31fe eb
265 d00c31fe eb
The default must be "".  This allows an audio module-dependent default
266 d00c31fe eb
to be specified in the user preferences file.
267 d00c31fe eb
268 d00c31fe eb
269 d00c31fe eb
** Standard options names by parameter
270 d00c31fe eb
271 d00c31fe eb
Whenever you want an integer, use the "intx" type.  This allows the
272 d00c31fe eb
user to input decimal, hex or octal numbers.  E.g., 10, 012, 0xa.
273 d00c31fe eb
274 d00c31fe eb
Whenever you want a float, use the "eng_float" type.  This allows the
275 d00c31fe eb
user to input numbers with SI suffixes.  E.g, 10000, 10k, 10M, 10m, 92.1M
276 d00c31fe eb
277 d00c31fe eb
If your program allows the user to specify values for any of the
278 d00c31fe eb
following parameters, please use these options to specify them:
279 d00c31fe eb
280 d00c31fe eb
281 d00c31fe eb
To specify a frequency (typically an RF center frequency) use:
282 d00c31fe eb
283 d00c31fe eb
  add_option("-f", "--freq", type="eng_float", default=<your-default-here>,
284 d00c31fe eb
             help="set frequency to FREQ [default=%default]")
285 d00c31fe eb
286 d00c31fe eb
287 d00c31fe eb
To specify a decimation factor use:
288 d00c31fe eb
289 d00c31fe eb
  add_option("-d", "--decim", type="intx", default=<your-default-here>,
290 d00c31fe eb
             help="set decimation rate to DECIM [default=%default]")
291 d00c31fe eb
292 d00c31fe eb
293 d00c31fe eb
To specify an interpolation factor use:
294 d00c31fe eb
295 d00c31fe eb
  add_option("-i", "--interp", type="intx", default=<your-default-here>,
296 d00c31fe eb
             help="set interpolation rate to INTERP [default=%default]")
297 d00c31fe eb
298 d00c31fe eb
299 d00c31fe eb
To specify a gain setting use:
300 d00c31fe eb
301 d00c31fe eb
  add_option("-g", "--gain", type="eng_float", default=<your-default-here>,
302 d00c31fe eb
	     help="set gain in dB [default=%default]")
303 d00c31fe eb
304 d00c31fe eb
305 d00c31fe eb
If your application specifies both a tx and an rx gain, use:
306 d00c31fe eb
307 d00c31fe eb
  add_option("", "--rx-gain", type="eng_float", default=<your-default-here>,
308 d00c31fe eb
	     help="set receive gain in dB [default=%default]")
309 d00c31fe eb
310 d00c31fe eb
  add_option("", "--tx-gain", type="eng_float", default=<your-default-here>,
311 d00c31fe eb
	     help="set transmit gain in dB [default=%default]")
312 d00c31fe eb
313 d00c31fe eb
314 d00c31fe eb
To specify the number of channels of something use:
315 d00c31fe eb
316 d00c31fe eb
  add_option("-n", "--nchannels", type="intx", default=1,
317 d00c31fe eb
             help="specify number of channels [default=%default]")
318 d00c31fe eb
319 d00c31fe eb
320 d00c31fe eb
To specify an output filename use:
321 d00c31fe eb
322 d00c31fe eb
  add_option("-o", "--output-filename", type="string", default=<your-default-here>,
323 d00c31fe eb
             help="specify output-filename [default=%default]")
324 d00c31fe eb
325 d00c31fe eb
326 d00c31fe eb
To specify a rate use:
327 d00c31fe eb
328 d00c31fe eb
  add_option("-r", "--bit-rate", type="eng_float", default=<your-default-here>,
329 d00c31fe eb
             help="specify bit-rate [default=%default]")
330 d00c31fe eb
     or
331 d00c31fe eb
332 d00c31fe eb
  add_option("-r", "--sample-rate", type="eng_float", default=<your-default-here>,
333 d00c31fe eb
             help="specify sample-rate [default=%default]")
334 d00c31fe eb
  
335 d00c31fe eb
336 d00c31fe eb
If your application has a verbose option, use:
337 d00c31fe eb
338 d00c31fe eb
  add_option('-v', '--verbose', action="store_true", default=False,
339 d00c31fe eb
	     help="verbose output")
340 d00c31fe eb
341 d00c31fe eb
342 d00c31fe eb
If your application allows the user to specify the "fast USB" options, use: 
343 d00c31fe eb
344 d00c31fe eb
  add_option("", "--fusb-block-size", type="intx", default=0,
345 d00c31fe eb
             help="specify fast usb block size [default=%default]")
346 d00c31fe eb
347 d00c31fe eb
  add_option("", "--fusb-nblocks", type="intx", default=0,
348 d00c31fe eb
             help="specify number of fast usb blocks [default=%default]")