Skip to content

mchalain/makemore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

makemore

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.

Content:

  1. Create a binary
    1. Simple application with one source file
    2. Application with several source files
    3. Libraries
  2. Complexe project
    1. Library linking
    2. Several binaries
    3. Files dispatching
  3. Project Configuration
    1. Configuration variables

List of entries:

  1. bin-y: application binary installed into bindir.
  2. lib-y: shared library installed into libdir.
  3. slib-y: static library.
  4. modules-y: dynamic module installed into pkglibdir.
  5. hostbin-y: application binary for the build machine, not installed.
  6. subdir-y: sub directory or sub makefile.
  7. include-y: header file to install with library into includedir.
  8. pkgconfig-y: generic pkgconfig to install into libdir/pkgconfig.
  9. data-y: static file to install into datadir.
  10. sysconfdir-y: configuration file to install into sysconfdir.
  11. download-y: download file.
  12. gitclone-y: git repository.
  13. hook-*-y: Makefile target to call after build, hostbuild, install, clean.

List of variables:

  1. *_SOURCES: source files for bin-y, lib-y, slib-y or modules-y.
  2. *_LIBRARY: libraries to link to the binary.
  3. *_LIBS: libraries to link to the binary.
  4. *_CFLAGS: compiler options for the binary.
  5. *_LDFLAGS: linker options for the binary.
  6. *_PKGCONFIG: library pkgconfig file.
  7. DEBUG: force the debug flags on the compiler.
  8. CROSS_COMPILE: toolchain prefix for cross-compilation.
  9. SYSROOT: sysroot directory to use with the binaries' compiler.
  10. DESTDIR: prefix directory for installation.
  11. DEVINSTALL: force the header files, static libraries installation.

Create a binary {#binary}

Simple application with one source file {#application_simple}

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

Application with several source files {#application_files}

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

Libraries {#libraries}

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

Complexe project {#project}

Library linking {#linking}

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}

Several binaries {#several_binaries}

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

Files dispatching {#submakefile}

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

Project Configuration {#configuration}

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 or n
  • a numerical value
  • a string.

Local configuration

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

Default configuration file

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.

Configuration directory

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.

rename the configuration 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

Configuration possibilities are many

Add macro for a specific configuration:

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

Add a library and all elements for this library:

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.

Use the configuration in the source files

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

Dependencies to external libraries:

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

Build your project outside the source tree

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

Cross-compilation

Build variables

Makemore uses the standard variables for build:

  1. CC
  2. LD
  3. CFLAGS
  4. LDFLAGS
  5. CROSS_COMPILE
  6. 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.

Mix host and target binaries

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

About

Makefile script

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published