aboutsummaryrefslogtreecommitdiff
path: root/ninja.m4
blob: 7181a00cdce6cb4d71cabb11eab89550c356d874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
divert(-1)
changequote([,])

define([getenv], [
  define([$1], [esyscmd([echo -n $$1])])
  ifelse($1, , [
    ifelse($2, , [undefine([$1])], [define([$1], [$2])])
  ])])

# global vars
getenv([PROD])

divert(0)

# building

cflags  = ifdef([PROD], [],  [-Wall -Wextra -g]) -std=c99 -D_DEFAULT_SOURCE
bin     = build
m4flags =

rule cc
  command = gcc $cflags $in -o $out
rule obj
  command = gcc -MD -MF $out.d $cflags -c $in -o $out
  depfile = $out.d
rule ar
  command = ar rcs $out $in
rule m4
  command = m4 $m4flags $in > $out
rule cpy
  command = cp $in $out
  
build $bin/msgpack.o: obj msgpack.c
build $bin/msgpack.h: cpy msgpack.h

build $bin/libmsgpack.a:  ar $bin/msgpack.o
build $bin/libmsgpack.so: cc $bin/msgpack.o
  cflags = $cflags -fPIC -shared
build $bin/libmsgpack.h:  m4 headeronly.h.m4 | msgpack.c msgpack.h
  m4flags = -P

# installing

divert(-1)
getenv([PREFIX],     [installdir])
getenv([LIBDIR],     [PREFIX/lib])
getenv([INCLUDEDIR], [PREFIX/include])
divert(0)

rule cpy_pattern
  command = cp $$(command ls $pattern 2> /dev/null) $dest

build force: phony
build $bin/files.dd: m4 builtfiles.dd.m4 | force
  m4flags = -D BIN=$bin -D [LIBDIR]=LIBDIR -D [INCLUDEDIR]=INCLUDEDIR
  description = Generate dynamic dependency from the built files

build libs: cpy_pattern || $bin/files.dd
  description = Install library files to 'LIBDIR'
  dyndep = $bin/files.dd
  dest = LIBDIR
  pattern = $bin/*.a $bin/*.so
build headers: cpy_pattern || $bin/files.dd
  description = Install header files to 'INCLUDEDIR'
  dyndep = $bin/files.dd
  dest = INCLUDEDIR
  pattern = $bin/*.h

# testing

build $bin/test: cc msgpack.c test.c

rule valgrind
  command = valgrind -s --leak-check=full --show-leak-kinds=all $in

# named targets

rule ninja_clean
  command = ninja -t clean

build static: phony $bin/libmsgpack.a  $bin/msgpack.h
build shared: phony $bin/libmsgpack.so $bin/msgpack.h
build header: phony $bin/libmsgpack.h

build install: phony libs headers
build clean:   ninja_clean

build test:  valgrind $bin/test

default ifdef([PROD], [static], [test])

# regeneration

rule regen
  command = m4 ninja.m4 > build.ninja
  generator = 1
  description = Regen build.ninja

build build.ninja: regen ninja.m4
build configure:   regen