aboutsummaryrefslogtreecommitdiff
path: root/ninja.m4
blob: 004acd94431b7f521b97938c2eef944144679a01 (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
divert(-1)
changequote([,])

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

getenv([PROD])
divert(0)

# building

cflags  = ifdef([PROD], [-O3],  [-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])

define([install_file], [
build $3/$1: install $2/$1
  dir = $3
])
divert(0)

rule install
  command = install -v -D -t $dir $in

install_file(libmsgpack.h,  $bin, INCLUDEDIR)
install_file(libmsgpack.so, $bin, LIBDIR)
install_file(libmsgpack.a,  $bin, LIBDIR)
install_file(msgpack.h,     $bin, INCLUDEDIR)

# testing

build $bin/test: cc msgpack.c test.c
  cflags = $cflags -std=gnu99

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

# named targets

divert(-1)
getenv([TARGET], [header])

define([target_table], [ifelse(
  TARGET, [header], [$2/libmsgpack.h],
  TARGET, [shared], [$1/libmsgpack.so $2/msgpack.h],
  TARGET, [static], [$1/libmsgpack.a  $2/msgpack.h]
)])
divert(0)

rule ninja_clean
  command = ninja -t clean
build clean: ninja_clean

build test: valgrind $bin/test

build install: phony target_table(LIBDIR, INCLUDEDIR)
default ifdef([PROD], [target_table($bin, $bin)], [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