
= Embedding libusual as part of the source. =


Here we build libusual as part of top-level tree.
This gives the advantage of building only the modules
that are actually used in main tree and without the
intermediate `libusual.a` step.

This method is for projects that are developed
in parallel with libusual.  Not recommended for
casual usage.


== Configure libusual ==


Here we configure libusual, but do not build it.

---------------------------------
$ git clone git://github.com/libusual/libusual.git lib
Cloning into 'lib'...
done.
$ cd lib
$ ./autogen.sh
[...]
$ ./configure
[...]
$ cd ..
---------------------------------

== Prepare own code ==


Here is the source that needs to be linked with libusual:

.File: prog.c
[source,c]
-----------------------------------
#include <usual/crc32.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
	const char *data = "CECSFXX";
	uint32_t crc;

	crc = calc_crc32(data, strlen(data), 0);
	printf("crc: %08x\n", crc);
	return 0;
}
-----------------------------------

== Old way, with plain Make ==


Here is corresponding Makefile:

.File: Makefile
[source,makefile]
-----------------------------------
CC = gcc
CFLAGS = -O -g -Wall

# here we describe our program
SRCS = prog.c
OBJS = $(SRCS:.c=.o)

# here we link to libusual
USUAL_DIR = ./lib
USUAL_LOCAL_SRCS = $(SRCS)
CPPFLAGS = $(USUAL_CPPFLAGS)
OBJS += $(USUAL_OBJS)

# this looks what modules are used by files in USUAL_LOCAL_SRCS
# and fills the USUAL_OBJS variable based on that
include $(USUAL_DIR)/Setup.mk

all: prog

prog: $(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@

%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

# special rule to build 
%.o: $(USUAL_DIR)/usual/%.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

clean:
	rm -f *.o prog
-----------------------------------

Build the project

---------------------------------
$ make
gcc -O -g -Wall -I./lib -c -o prog.o prog.c
gcc -O -g -Wall -I./lib -c -o crc32.o lib/usual/crc32.c
gcc -O -g -Wall -I./lib -c -o base.o lib/usual/base.c
gcc -O -g -Wall  prog.o ./crc32.o ./base.o  -o prog
$ ls
Makefile  base.o  crc32.o  lib	prog  prog.c  prog.o
$ ./prog
crc: 12345678
$ make clean
rm -f *.o prog
$ ls
Makefile  lib  prog.c
---------------------------------

It's quite complex and that is even without trying to get
dependencies rigth.  See next section for preferred way.


== New way, with Antimake. ==


Antimake is build framework on plain GNU Make that reads
build instructons with Automake syntax.  It has also hooks
for libusual integration.

Here is Makefile that uses Antimake:

.File: Makefile
[source,makefile]
-----------------------------------
# the automake-style build description for 'prog'
noinst_PROGRAMS = prog
prog_SOURCES = prog.c

# location of configured libusual
USUAL_DIR = lib

# mention that 'prog' wants embedded libusual
prog_EMBED_LIBUSUAL = 1
AM_FEATURES = libusual

# launch Antimake
include $(USUAL_DIR)/mk/antimake.mk
-----------------------------------

Build the project

---------------------------------
$ make
     CC       prog.c
     CC       lib/usual/crc32.c
     CC       lib/usual/base.c
     CCLD     prog
$ ls
Makefile  lib  prog  prog.c
$ ./prog
crc: 12345678
$ make clean
     CLEAN    prog
$ ls
Makefile  lib  prog.c
---------------------------------

Done

