How To Create a Project Using the Makefile Engine
Haiku helps developers with the build process of their projects by providing the so
called makefile-engine. It's made of two files, that reside in
/boot/system/develop/etc directory and are named 'Makefile' and 'makefile-engine'.
Together, these two files provide you with a simple ready-to-be used build
engine for your projects.
This How-To describes the makefile-engine v2.6 and the
Makefile template v2.6. Regardless of mentioning the 'makefiles' in this
How-To, the same technique can be used for creating Jamfile-driven
projects. Corresponding Jamfile and Jamfile-engine template files are provided
with Haiku. We made both, the Makefile and Jamfile engines completely
target-compatible for the user's convenience.
Contents
Getting Started
To start a project, just copy Makefile from /boot/system/develop/etc directory, into
your project directory. Write a few files that you want to add to your project. Add
either relative or full paths to them into the SRCS variable definition in the
Makefile and run make. Example files for a "Hello World" project:
hello.cpp:
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
return 0;
}
Makefile:
NAME = hello
TYPE = APP
SRCS = hello.cpp
## Include the Makefile-Engine
DEVEL_DIRECTORY := \
$(shell findpaths -r "makefile_engine" B_FIND_PATH_DEVELOP_DIRECTORY)
include $(DEVEL_DIRECTORY)/etc/makefile-engine
After creating both these files in same directory, just go there in Terminal,
using the 'cd' command and run 'make'. This will create a new directory,
named in the format: 'objects.x86-cc2-release' (the name depends on current compiler,
that may be either "cc2" or "cc4", and defining DEBUG will force using
"debug" instead of "release"), which will contain .o files (one
for each source file), .d files with dependencies, generated automatically by
the engine and a binary file, named 'hello' for the example case above.
Configuring a Project
In Makefile, there are many variables to configure builder helpers for your
needs. Let's take a look at them:
- NAME specifies the name of the project and the output binary filename
- TYPE specifies the type of binary, can be one of the following:
- APP - Application
- SHARED - Shared library or add-on
- STATIC - Static library archive
- DRIVER - Kernel driver
- APP_MIME_SIG specifies the application's mime signature for
localization features. Note that it should correspond to MIME type
provided to the BApplication's constructor and the application MIME type
defined in resource file. In case this parameter is not set, the
default value 'x-vnd.Haiku-$(NAME)' will be used.
- SRCS specifies the source files to use. You may specify both, full
paths and paths relative to the location of the Makefile. All objects,
regardless of the location of their sources will be created in the
common object directory. Please note, that this means, that the Makefile
won't work correctly, if two source files with the same name
(e.g. source.c and source.cpp) are included from different
directories. Also note, that spaces in folder names do not work well
with the engine.
- RDEFS specifies the resource definition files to be used. You may
specify both, relative and full paths to the files.
- RSRCS specifies the binary file compiled from RDEF, or created
separately by resource editors. Both RDEFS and RSRCS can be
defined in the same Makefile.
- LIBS specifies additional libraries, that the binary file should be
linked against. There are two acceptable forms of library specifications:
- if your library follows the naming pattern of libXXX.so or libXXX.a,
you can simply specify XXX, e.g. for the library libbe.so, that would be:
be
- for version-independent linking of standard C++ libraries, please
add $(STDCPPLIBS instead of raw "stdc++[.r4] [supc++]" library names
- for localization support add the following libraries: locale localestub
- if your library doesn't follow the standard library naming
scheme, you need to specify the path to the library and its name, e.g.
for the library: my_lib.a, the entry would be either: my_lib.a or
path/my_lib.a
- LIBPATHS specifies additional paths to directories following the
standard libXXX.so or libXXX.a naming scheme. You can specify both,
full paths or paths relative to the Makefile. The paths included may
not be recursive, so include all the paths where libraries can be found.
Directories where source files are found are automatically included.
- SYSTEM_INCLUDE_PATHS specifies additional paths to look for system
headers. These use the form: #include <header>. Source file
directories are NOT automatically included here.
- LOCAL_INCLUDE_PATHS specifies additional paths to look for local
headers. There use the form: #include "header". Source file
directories are automatically included.
- OPTIMIZE specifies the level of optimization desired, can be one of
following: NONE, SOME, FULL.
- LOCALES specifies language codes, that are going to be supported
by application. The default "en" one must be provided too. For more
information about localization, see the corresponding section of this
how-to.
- DEFINES specifies any preprocessor symbols to be defined. The symbols
will not have their values set automatically, you have to provide
these values (if any). For example, setting DEFINES to "DEBUG=1" will
cause the compiler option "-DDEBUG=1" to be used. However, setting
DEFINES to "DEBUG" would pass "-DDEBUG" option.
- WARNINGS specifies the level of warnings reported by compiler. If this
option is unspecified, the default warnings will be used. It can be
set to one of the following:
- NONE - supress all warnings
- ALL - enable all warnings
- SYMBOLS specifies, whether image symbols should be created, so the
stack crawls in the debugger are meaningful. Setting it to TRUE
enables the creation of symbols.
- DEBUGGER specifies debugging settings. If set to TRUE, it allows
the application to be run from a source-level debugger. Please note,
that this will disable all optimization.
- COMPILER_FLAGS specifies additional compiler flags for all files.
- LINKER_FLAGS specifies additional linker flags.
- APP_VERSION specifies the version of the particular item (e.g. -app
3 4 0 d 0 -short 340 -long "340 "
echo -n -e '\302\251'
).
"1999 GNU GPL"). This may also be specified in a resource.
- DRIVER_PATH works only for TYPE == DRIVER. It specifies desired
location of driver in the /dev hierarchy. It's user by the
driverinstall rule. E.g. DRIVER_PATH = video/usb will instruct
the driverinstall rule to place a symlink to your driver's binary into
~/config/non-packaged/add-ons/kernel/drivers/dev/video/usb, so that your driver will
appear in /dev/video/usb when loaded. Default is "misc".
- INSTALL_DIR specifies the installation directory of application.
Please also note, that if you're building your own Makefile, that will use this
engine, last lines must contain:
DEVEL_DIRECTORY := \
$(shell findpaths -r "makefile_engine" B_FIND_PATH_DEVELOP_DIRECTORY)
include $(DEVEL_DIRECTORY)/etc/makefile-engine
Using Localization
Localization in Haiku programs is achieved simply, as following example shows.
localized_hello.cpp:
#include <stdio.h>
#include <Catalog.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "hello"
int main(void)
{
printf(B_TRANSLATE("Hello, world!\n"));
return 0;
}
This file uses header file Catalog.h, that belongs to locale library. So to
actually be able to use localization in your programs, you have to adjust few
settings in your Makefile.
Adjust a value to your project's APP_MIME_SIG variable.
Application's mime signature should also be set in the following
format: x.vnd-<author>-<project_name>
Add following two libraries into your LIBS variable: locale
localestub
Add every language, that you want to support, into LOCALES variable,
e.g. 'LOCALES = en de fr' for English, German and French locale
support.
Add the resource definition script (also specified in the RDEF
variable) containing the following entries to project:
resource app_signature "application/x-vnd.<author>-<project_name>";
resource app_name_catalog_entry "<author>-<project_name>:System name:Terminal";
Run 'make' to build the binary file.
Run 'make catkeys' to get the locales/en.catkeys file.
Copy this file to locales/<language_code>.catkeys and translate it,
as needed.
When you've prepared all needed .catkeys files, run 'make catalogs' to create
catalog files from them.
Run either 'make catalogsinstall' or 'make bindcatalogs' to make the catalogs
available for the application. For more information about differences
between these two commands, please see the next section.
Here is an example Makefile for the localized_hello.cpp above:
Makefile:
NAME = hello
TYPE = APP
APP_MIME_SIG = x-vnd.example-hello
SRCS = localized_hello.cpp
LIBS = locale localestub
LOCALES = en de fr
## Include the Makefile-Engine
DEVEL_DIRECTORY := \
$(shell findpaths -r "makefile_engine" B_FIND_PATH_DEVELOP_DIRECTORY)
include $(DEVEL_DIRECTORY)/etc/makefile-engine
Target Reference
This is supposed to be the list of all non-file related targets.
- default is the same as running make without arguments, it builds theoutput
file
- catkeys creates the locales/en.catkeys file, containing all strings from
the sources, ready to be localized.
- catalogs compiles all .catkeys files into corresponding .catalog files
- clean cleans the project directory of building leftovers, removes
everything in the objects folder.
- rmapp removes only the executable application file from the objects folder
- driverinstall installs the driver in the system.
- install installs the program into the directory specified by the INSTALL_DIR
variable.
- catalogsinstall installs localization resource catalogs into
/boot/home/config/non-packaged/data/locale/catalogs/<APP_MIME_SIG>
for testing purposes. Note that for the distribution of a release version, catalogs should be stored in
/boot/system/non-packaged/data/locale/catalogs/<APP_MIME_SIG> instead of
home. Even better, create a proper HPKG and don't install in any non-packaged folder at all.
- bindcatalogs binds localization resource catalogs into the executable
file's resources (it's an alternative way of storing localization
catalogs that doesn't require to distribute separate catalog files).
This How-To was originally created on November 28, 2011 by Peter
Poláčik |
Copyright © 2017 Haiku Inc. |