Makefile scripts to build your projects.
scripts.mk is a makefile to include in the main "Makefile" of your project. You just have to declare your binaries and their source files, after Makemore does the rest and more.
Makemore support C, C++, Qt applications, libraries and dynamic modules.
- bin-y: application binary installed into bindir.
- lib-y: shared library installed into libdir.
- slib-y: static library.
- modules-y: dynamic module installed into pkglibdir.
- hostbin-y: application binary for the build machine, not installed.
- subdir-y: sub directory or sub makefile.
- include-y: header file to install with library into includedir.
- pkgconfig-y: generic pkgconfig to install into libdir/pkgconfig.
- data-y: static file to install into datadir.
- sysconfdir-y: configuration file to install into sysconfdir.
- download-y: download file.
- gitclone-y: git repository.
- hook-*-y: Makefile target to call after build, hostbuild, install, clean.
- *_SOURCES: source files for bin-y, lib-y, slib-y or modules-y.
- *_LIBRARY: libraries to link to the binary.
- *_LIBS: libraries to link to the binary.
- *_CFLAGS: compiler options for the binary.
- *_LDFLAGS: linker options for the binary.
- *_PKGCONFIG: library pkgconfig file.
- DEBUG: force the debug flags on the compiler.
- CROSS_COMPILE: toolchain prefix for cross-compilation.
- SYSROOT: sysroot directory to use with the binaries' compiler.
- DESTDIR: prefix directory for installation.
- DEVINSTALL: force the header files, static libraries installation.
The simplest way is to have a C or C++ file with all code. The application will have the same name of the file.
Makefile:
include scripts.mk
bin-y+=main
The command lines to call are:
$: make
CC main
LD main
$: make install
INSTALL main
$: make distclean
CLEAN main.o
CLEAN main
To use more than one file for your source code, you must define the list
of files for your binary. That list is a variable build with the name of
your binary and the suffix _SOURCES
.
Makefile:
include scripts.mk
bin-y+=main
main_SOURCES:=main.c test.c
$: make
CC main
CC test
LD main
$: make install
INSTALL main
$: make distclean
CLEAN main.o
CLEAN test.o
CLEAN main
Each kind of library uses the same syntax from the binary. Only the entry point change and the target installation.
- shared libraries use lib-y and are installed into libdir directory
- static libraries use slib-y and not installed
- dynamic modules use modules-y and are installed into pkglibdir directory
Makefile:
include scripts.mk
lib-y+=myshared
myshared_SOURCES=test.c
Makefile:
include scripts.mk
slib-y+=mystatic
mystatic_SOURCES=test.c
Makemore allows to link your binaries to the system libraries or your own libraries. Like the *_SOURCES, you can define several variables to manage the target build:
- *_LIBRARY to link your binary to another library (1)(2).
- *_LIBS to link your binary to another library.
- *_CFLAGS to change the arguments of the compiler.
- *_LDFLAGS to change the arguments of the linker.
(1) the *_CFLAGS and the *_LDFLAGS is updated if the pkg-config file (.pc) exists. (2) the entries of *_LIBRARY may contain a version for the checking:
Example to link the application with libm the mathematic library and glib-2 with the version 0.6400.6:
include scripts.mk
bin-y+=main
main_LIBS+=m
main_LIBRARY+=glib-2.0{0.6400.6}
Your Makefile may contain more than one target. Each target may define its own rules as previously see.
This example define a generic CFLAGS for all binaries, after 2 binaries are built:
- the libtest.so library with the file test/test.c
- the main application with the file main.c. This application defines the macro TEST and it is linked to the libtest.so library.
include scripts.mk
CFLAGS+=-g
lib-y:=foo
test_SOURCES=lib/foo.c
bin-y+=bar
bar_CFLAGS+=-DTEST
bar_CFLAGS+=-Ilib
bar_LDFLAGS:=-Llib.c
bar_LIBS+=foo
bin-$(TESTS)+=test
test_SOURCES+=testrun.c
test_SOURCES+=test1.c
test_SOURCES+=test2.c
test_LIBS+=foo
Makemore can manage your project into several directories. Each directory can contain its own "Makefile" and "script.mk" may be included only in the main one.
Makefile:
include scripts.mk
subdir-y+=src
subdir-y+=lib
src/Makefile:
bin-y+=bar
bar_SOURCES+=main.c bar.c
bar_LIBS+=foo
bar_CFLAGS+=-DTEST
bar_CFLAGS+=-I../lib
bar_LDFLAGS:=-L../lib
lib/Makefile:
lib-y+=foo
foo_SOURCES:=foo.c
The subdir-y entries may contain a list of directories or files. Only *.mk or Makefile are allowed.
This example merge the project files into the same directory but dispatch the build rules in different Makefile:
Makefile:
include scripts.mk
subdir-y+=src/main.mk
subdir-y+=src/test.mk
src/main.mk:
bin-y+=bar
bar_SOURCES+=main.c bar.c
bar_LIBS+=foo
bar_CFLAGS+=-DTEST
src/test.mk:
lib-y+=foo
foo_SOURCES:=foo.c
It is possible to define some variables to manage the build. The variables will allow to build or not some tools or append files to the binary, either add some build flags.
The variables take the values:
- a boolean value
y
orn
- a numerical value
- a string.
The variables may be set inside a Makefile to be use in the same Makefile or theirs sub-Makefiles:
CONFIG_TEST=y
bin-y+=main
main_SOURCES:=main.c
main_SOURCES-$(CONFIG_TEST)+=test.c
main_CFLAGS-$(CONFIG_TEST)+=-DTEST
This variables may be included into a configuration file of your project.
To do that you can modify the defconfig
file into the same directory
of your main Makefile with:
defconfig:
FOO=y
TEST=n
MYSTRING="helloworld"
Makefile:
include scripts.mk
bin-y+=bar
lib-$(FOO)+=foo
bar_LIBS-$(FOO)+=foo
bar_CFLAGS-$(TEST)+=-DTEST -DMYSTRING2=$(MYSTRING)
All variables must be defined inside the defconfig file. This one will be used by Makmore to generate or to update the .config file.
The project may contain several configuration files, and all have to stored into the configs folder of your project. Each one must be named *_defconfig.
$ make mytest_defconfig
$ make
$ make DESTDIR=$PWD/tempo install
Makemore searchs the file into the configs folder, merges with the defconfig to check the new entries, and create the .config file.
It is possible to change the name of the configuration file, instead to
use to use the .config file. The CONFIG
variable may be modified
inside the main Makefile:
CONFIG:=myconfig
include scripts.mk
bin-y+=main
lib-$(CONFIG_TEST)+=test
main_LIBS-$(CONFIG_TEST)+=test
or on the command line:
$ make CONFIG=myconfig
include scripts.mk
bin-y+=main
main_SOURCES:=main.c
main_CFLAGS-$(CONFIG_TEST)+=-DTEST
Add a source file to the project *:
include scripts.mk
bin-y+=main
main_SOURCES:=main.c
main_CFLAGS-$(CONFIG_TEST)+=-DTEST
main_SOURCES-$(CONFIG_TEST)+=test.c
bin-y+=main
main_SOURCES:=main.c
main_CFLAGS-$(CONFIG_X264)+=-DX264
main_LIBRARY-$(CONFIG_X264)+=x264
main_LDFLAGS-$(CONFIG_X264)+=-L/usr/local/lib
include scripts.mk
Add flag for a specific file *:
bin-y+=main
main_SOURCES:=main.c
main_SOURCES-$(CONFIG_TEST)+=test.c
test_CFLAGS+=-DTEST
include scripts.mk
{#Note})Note the difference between the both solutions:
- first the CFLAGS is associated to the binary : main_CFLAGS.
- second the CFLAGS is associated to the source file : test_CFLAGS.
During the build step, makemore
generates a config.h
file
which will contains the definition of your configuration. This file
is automaticly included in your source code.
CONFIG_TEST=n
CONFIG_X264=y
CONFIG_X265=n
gererate:
#define CONFIG_TEST n
#define CONFIG_X264 y
#define CONFIG_X265 n
Makemore may check the availability and version of and external libarry
and retrieves the compiler flags with pkg-config
.
include scripts.mk
bin-y+=main
main_SOURCES:=main.c
main_LIBRARY-$(CONFIG_X264)+=x264
cf.:dependencies
The builddir
variable may define the results' directory of the build.
The variable may be defined during the call:
$ make builddir=build
...
or into the main Makefile
builddir=.libs
include scripts.mk
lib-y+=mylib
Makemore uses the standard variables for build:
- CC
- LD
- CFLAGS
- LDFLAGS
- CROSS_COMPILE
- SYSROOT
A simple way may be to set the variables 1), 2), 3), 4) with the compilator version of the target. Or to set the variable 5) with the target compilor prefix.
$ make CROSS_COMPILE=aarch64-linux-gnu-
The variable 6) is useful to change the root directory to search the headers and the libraries during the build.
Some projects may need to build a tool to continue the build. During a cross compilation is necessary to separate.
The host binaries may be identified by the hostbin-y entry.
DEFAULT_ADDRESS=192.168.1.254
DEFAULT_PORT=8080
bin-y+=server
server_SOURCES+=main.c
server_SOURCES+=server.c
hostbin-y+=clienttest
clienttest_SOURCES+=test.c
#Install your project
The default installation uses the path prefix=/usr/local
and modify the other paths with :
- exec_prefix is $(prefix) by default
- binary into
$(exec_prefix)/bin
- library into
$(exec_prefix)/lib
- modules into
$(exec_prefix)/lib/$(package_name)
- data into
$(prefix)/share/$(package_name)
To change the installation you can modify some conventional variables into your configuration file
config:
package_name=myproject
prefix=/usr
libdir=$(prefix)/lib
datadir=/etc
To package the binary, it is possible to modify the installation directory with a prefixing destination directory
$ make DESTDIR=/my/path/to/package install