1<h1>How To Create a Project Using Makefile Engine</h1>
2
3<p>Haiku helps developers in build process of their projects by providing so
4called Makefile engine. It's made of two files, that reside in 
5/boot/develop/etc directory and are named 'makefile' and 'makefile-engine'.
6Together, these two files provide you with simple ready-to-be used build
7engine for your projects. This How To describes makefile-engine v2.5.1 and
8makefile template v2.5. Regardless of mentioning the 'makefiles' in this
9How To, the same technique can be used for creating Jamfile-driven
10projects. Corresponding Jamfile and Jamfile-engine template files are provided
11with Haiku. We made both, the makefile and Jamfile engines completely 
12target-compatible for user's convenience.</p>
13
14<h2>Contents</h2>
15<p>
16<ul>
17	<li><a href="#getting_started">Getting Started</a></li>
18	<li><a href="#config">Configuring a Project</a></li>
19	<li><a href="#localization">Using Localization</a></li>
20	<li><a href="#targets">Target Reference</a></li>
21</ul>
22</p>
23
24<div id="getting_started"><h2>Getting Started</h2></div>
25
26<p>To start a project, just copy makefile from /boot/develop/etc directory, into
27your project directory. Write few files, you want to add into project. Add
28either relative or full paths to them, into SRCS variable definition in 
29makefile and run make. Example files for Hello World project:</p>
30
31<p><em>hello.cpp</em>:</p>
32
33<pre><code>#include &lt;stdio.h&gt;
34
35int main(void)
36{
37    printf("Hello world!\n");
38    return 0;
39}
40</code></pre>
41
42<p><em>makefile</em>:</p>
43
44<pre><code>NAME = hello
45TYPE = APP
46SRCS = hello.cpp
47include $(BUILDHOME)/etc/makefile-engine
48</code></pre>
49
50<p>After adding both these files into same directory, just go there in Terminal,
51using 'cd' command and run 'make'. This will create a new directory, named in
52similar format: 'objects.x86-gcc2-release' (name depends on current compiler,
53that may be either "gcc2" or "gcc4", and defining DEBUG will force using
54"debug" instead of "release"), which will contain .o files (one
55for each source file), .d files with dependencies, generated automatically by
56the engine and a binary file, named 'hello' for the example case above.</p>
57
58<div id="config"><h2>Configuring a Project</h2></div>
59
60<p>In makefile, there are many variables, to configure builder helpers for your
61needs. Let's take a look at them:</p>
62
63<ul>
64<li><strong>NAME</strong> specifies the name of the project and the output binary filename</li>
65<li><strong>TYPE</strong> specifies the type of binary, can be one of the following:
66<ul>
67<li><strong>APP</strong> - Application</li>
68<li><strong>SHARED</strong> - Shared library or add-on</li>
69<li><strong>STATIC</strong> - Static library archive</li>
70<li><strong>DRIVER</strong> - Kernel Driver</li>
71</ul></li>
72<li><strong>APP_MIME_SIG</strong> specifies application's mime signature for 
73localization features. Note that it should correspond to MIME type
74provided to BApplication's constructor and the application MIME type
75defined in resource file. In case this parameter is not set, the
76default value x-vnd.Haiku-$(NAME) will be used.</li>
77<li><strong>SRCS</strong> specifies the source files to use. You may specify both, full 
78paths and paths relative to the position of makefile, all objects,
79regardless of the position of their sources will be created in the
80common object directory. Please note, that this means, that makefile
81won't work correctly, if two source files with the same name
82(e.g. source.c and source.cpp) are included from different
83directories. Also note, that spaces in folder names do not work well
84with the engine.</li>
85<li><strong>RDEFS</strong> specifies the resource definition files to be used. You may
86specify both, relative and full paths to the files.</li>
87<li><strong>RSRCS</strong> specifies the binary file compiled from <em>RDEF</em>, or created
88separately by Resource Editors, both <em>RDEFS</em> and <em>RSRCS</em> can be
89defined in the same makefile.</li>
90<li><strong>LIBS</strong> specifies additional libraries, that binary file should be
91linked against. There are two acceptable forms of library 
92specifications:
93<ul>
94<li>if your library follows the naming pattern of libXXX.so or libXXX.a,
95you can simply specify XXX, e.g. for library libbe.so, that would be:
96be</li>
97<li>for version-independent linking of standard C++ libraries, please
98add $(STDCPPLIBS instead of raw "stdc++[.r4] [supc++]" library names</li>
99<li>for localization support add following libraries: locale localestub</li>
100<li>if your library doesn't follow the standard library naming
101scheme, you need to specify the path to the library and its name, e.g.
102for library: my_lib.a, the entry would be either: my_lib.a or 
103path/my_lib.a</li>
104</ul></li>
105<li><strong>LIBPATHS</strong> specifies additional paths to directories following the
106standard libXXX.so or libXXX.a naming scheme. You can specify both,
107full paths or paths relative to the makefile. The paths included may
108not be recursive, so include all paths, where libraries can be found.
109Directories where source files are found are automatically included.</li>
110<li><strong>SYSTEM_INCLUDE_PATHS</strong> specifies additional paths to look for system
111headers. These use the form: #include &lt;header&gt;. Source file
112directories are <em>NOT</em> automatically included here.</li>
113<li><strong>LOCAL_INCLUDE_PATHS</strong> specifies additional paths to look for local
114headers. There use the form: #include "header". Source file
115directories are automatically included.</li>
116<li><strong>OPTIMIZE</strong> specifies the level of optimization desired, can be one of
117following: <em>NONE</em>, <em>SOME</em>, <em>FULL</em>.</li>
118<li><strong>LOCALES</strong> specifies language codes, that are going to be supported
119by application. The default "en" one must be provided too. For more
120information about localization, see the corresponding section of this
121how-to.</li>
122<li><strong>DEFINES</strong> specifies any preprocessor symbols to be defined. The symbols
123will not have their values set automatically, you have to provide
124these values (if any). For example, setting <em>DEFINES</em> to "DEBUG=1" will
125cause the compiler option "-DDEBUG=1" to be used. However, setting
126<em>DEFINES</em> to "DEBUG" would pass "-DDEBUG" option.</li>
127<li><strong>WARNINGS</strong> specifies the level of warnings reported by compiler. If this
128option is unspecified, the default warnings will be used. It can be
129set to one of the following:
130<ul>
131<li>NONE - supress all warnings</li>
132<li>ALL - enable all warnings</li>
133</ul></li>
134<li><strong>SYMBOLS</strong> specifies, whether image symbols should be created, so the
135stack crawls in the debugger are meaningful. Setting it to <em>TRUE</em>
136enables the creation of symbols.</li>
137<li><strong>DEBUGGER</strong> specifies debugging settings. If set to <em>TRUE</em>, it allows
138the application to be run from a source-level debugger. Please note,
139that this will disable all optimization.</li>
140<li><strong>COMPILER_FLAGS</strong> specifies additional compiler flags for all files.</li>
141<li><strong>LINKER_FLAGS</strong> specifies additional linker flags.</li>
142<li><strong>APP_VERSION</strong> specifies the version of the particular item (e.g. -app
1433 4 0 d 0 -short 340 -long "340 "<code>echo -n -e '\302\251'</code>).
144"1999 GNU GPL"). This may also be specified in a resource.</li>
145<li><strong>DRIVER_PATH</strong> works only for <em>TYPE</em> == <em>DRIVER</em>. It specifies desired
146location of driver in the /dev hierarchy. It's user by the
147driverinstall rule. E.g. <em>DRIVER_PATH</em> = video/usb will instruct
148the driverinstall rule to place a symlink to your driver's binary into
149~/add-ons/kernel/drivers/dev/video/usb, so that your driver will
150appear in /dev/video/usb when loaded. Default is "misc".</li>
151<li><strong>INSTALL_DIR</strong> specifies the installation directory of application.</li>
152</ul>
153
154<p>Please also note, that if you're building your own makefile, that will use this
155engine, last line must contain:</p>
156
157<pre><code>include $(BUILDHOME)/etc/makefile-engine
158</code></pre>
159
160<div id="localization"><h2>Using Localization</h2></div>
161
162<p>Localization in Haiku programs is achieved simply, as following example shows.</p>
163
164<p><em>localized_hello.cpp</em>:</p>
165
166<pre><code>#include &lt;stdio.h&gt;
167#include &lt;Catalog.h&gt;
168
169#undef B_TRANSLATION_CONTEXT
170#define B_TRANSLATION_CONTEXT "hello"
171
172int main(void)
173{
174    printf(B_TRANSLATE("Hello, world!\n"));
175    return 0;
176}
177</code></pre>
178
179<p>This file uses header file Catalog.h, that belongs to locale library. So to
180actually be able to use localization in your programs, you have to adjust few
181settings in your makefile.</p>
182
183<ol>
184<li>Adjust a value to your project's <strong>APP_MIME_SIG</strong> variable. 
185Application's mime signature should also be set in the following
186format: x.vnd-&lt;author&gt;-&lt;project_name&gt;</li>
187<li>Add following two libraries into your <strong>LIBS</strong> variable: locale localestub</li>
188<li>Add every language, that you want to support, into <strong>LOCALES</strong> variable, 
189e.g. 'LOCALES = en de fr' for English, German and French locale 
190support.</li>
191<li><p>Add the Resource Definition script (also please specify it in <em>RDEF</em> 
192variable) containing the following entries into project:</p>
193
194<p>resource app_signature "application/x-vnd.<author>-<project_name>";</p>
195
196<p>resource app<em>name</em>catalog_entry "<author>-<project_name>:System name:Terminal";</p></li>
197<li><p>Run 'make' to build binary file.</p></li>
198<li>Run either: 'make catkeys' to get locales/en.catkeys file.</li>
199<li>Copy this file to locales/&lt;language_code&gt;.catkeys and translate it,
200as needed.</li>
201<li>When you prepared all needed .catkeys files, run 'make catalogs' to create
202catalogs files from them.</li>
203<li>Run either 'make catalogsinstall' or 'make bindcatalogs' to make catalogs
204available for application. For more information about differences
205between these two commands, please see the next section.</li>
206</ol>
207
208<p>Here is also example makefile for the localized_hello.cpp above:</p>
209
210<p><em>makefile</em>:</p>
211
212<pre><code>NAME = hello
213TYPE = APP
214APP_MIME_SIG = x.vnd-example-hello
215SRCS = localized_hello.cpp
216LIBS = locale localestub
217LOCALES = en de fr
218include $(BUILDHOME)/etc/makefile-engine
219</code></pre>
220
221<div id="targets"><h2>Target Reference</h2></div>
222
223<p>This is supposed to be the list of all non-file related targets.</p>
224
225<ul>
226<li><strong>default</strong> is the same as running make without arguments, it builds output
227file</li>
228<li><strong>catkeys</strong> creates locales/en.catkeys file, containing all strings from
229sources, ready to be localized.</li>
230<li><strong>catalogs</strong> compiles all .catkeys files into corresponding .catalog files</li>
231<li><strong>clean</strong> cleans project directory of building leftovers, removes
232everything in the objects folder.</li>
233<li><strong>rmapp</strong> removes only the executable application file from objects folder</li>
234<li><strong>driverinstall</strong> installs driver into system.</li>
235<li><strong>install</strong> installs program into directory, specified by <em>INSTALL_DIR</em>
236variable.</li>
237<li><strong>catalogsinstall</strong> installs localization resources catalogs into 
238/boot/home/config/data/locale/catalogs/&lt;APP_MIME_SIG&gt; 
239directory for testing purposes. Note that for the distribution of
240release version catalogs should be stored in
241/boot/common/data/locale/catalogs/&lt;APP_MIME_SIG&gt; instead of
242home.</li>
243<li><strong>bindcatalogs</strong> binds localization resources catalogs into executable 
244file's resources (it's alternative way of storing localization 
245catalogs that doesn't require to distribute separate catalog files).</li>
246</ul>
247
248<table border="0" width="100%">
249<tr>
250<td align="left">This How To was created on November 28, 2011 by Peter
251Poláčik</td>
252<td align="right">Copyright &copy; 2011 Haiku Inc.</td>
253</tr>
254</table>
255