Skip to content

Commit 0dba282

Browse files
committed
Replace current Makefile with smk based one
1 parent e4f0722 commit 0dba282

File tree

11 files changed

+459
-146
lines changed

11 files changed

+459
-146
lines changed

Makefile

+35-146
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
1-
# Basic build configuration
2-
verbose ?= n
3-
config ?= .config
4-
5-
ifeq (,$(filter $(MAKECMDGOALS),configure clean distclean))
6-
-include $(config)
7-
endif
8-
9-
10-
# Applications
11-
CHAOS_BIN := chaos
12-
CHAOS_OBJ :=
13-
CHAOS_OBJ += bin/chaos.src/config.o
14-
CHAOS_OBJ += bin/chaos.src/cmdline.o
15-
CHAOS_OBJ += bin/chaos.src/chaos.o
16-
17-
# Tests
18-
19-
# Libraries
20-
LIBH2_A := libh2.a
21-
LIBH2_SO := libh2.so
22-
LIBH2_SO_M := $(LIBH2_SO).$(LIBH2_V_MAJOR)
23-
LIBH2_SO_Mm := $(LIBH2_SO).$(LIBH2_V_MAJOR).$(LIBH2_V_MINOR)
24-
LIBH2_SO_MmB := $(LIBH2_SO).$(LIBH2_V_MAJOR).$(LIBH2_V_MINOR).$(LIBH2_V_BUGFIX)
25-
26-
LIBH2_OBJ :=
27-
LIBH2_OBJ += lib/h2/xen/xc.o
28-
LIBH2_OBJ += lib/h2/xen/dev.o
29-
LIBH2_OBJ += lib/h2/xen/vif.o
30-
LIBH2_OBJ += lib/h2/xen/xs.o
31-
LIBH2_OBJ += lib/h2/xen/console.o
32-
LIBH2_OBJ += lib/h2/h2.o
33-
LIBH2_OBJ += lib/h2/xen.o
34-
ifeq ($(CONFIG_H2_XEN_NOXS),y)
35-
LIBH2_OBJ += lib/h2/xen/noxs.o
36-
endif
37-
38-
# cscope
39-
CSCOPE_FILES := cscope.files cscope.out cscope.in.out cscope.po.out
1+
include smk/s.mk
402

413
# Default build flags
42-
CFLAGS += -Iinc
43-
ifneq ($(LINUX_HEADERS),)
44-
CFLAGS += -I$(LINUX_HEADERS)
45-
endif
464
CFLAGS += -std=gnu11
47-
CFLAGS += -Wall -MD -MP -g
5+
CFLAGS += -Wall -g
486
ifeq ($(CONFIG_H2_XEN_NOXS),y)
497
CFLAGS += -DCONFIG_H2_XEN_NOXS
508
endif
519

52-
LDFLAGS += -Llib
10+
ifneq ($(LINUX_HEADERS),)
11+
CFLAGS += -I$(LINUX_HEADERS)
12+
endif
5313

5414
XEN_CFLAGS :=
5515
XEN_CFLAGS += -I$(XEN_ROOT)/tools/include
@@ -58,107 +18,36 @@ XEN_CFLAGS += -I$(XEN_ROOT)/dist/install/$(XEN_PREFIX)/include
5818

5919
XEN_LDFLAGS :=
6020
XEN_LDFLAGS += -L$(XEN_ROOT)/dist/install/$(XEN_PREFIX)/lib
61-
XEN_LDFLAGS += -lxenctrl -lxenstore -lxenguest -lxentoollog
62-
63-
LIBH2_DEP_CFLAGS :=
64-
LIBH2_DEP_CFLAGS += $(XEN_CFLAGS)
65-
66-
LIBH2_DEP_LDFLAGS :=
67-
LIBH2_DEP_LDFLAGS += -lh2
68-
LIBH2_DEP_LDFLAGS += -Wl,-rpath-link,$(XEN_ROOT)/dist/install/$(XEN_PREFIX)/lib
69-
70-
71-
# Targets
72-
all: libh2 chaos
73-
74-
tests:
75-
76-
install: libh2 chaos
77-
$(call cmd, "INSTALL", "include/h2" , cp -r , inc/h2 $(PREFIX)/include/)
78-
$(call cmd, "INSTALL", $(LIBH2_SO_MmB), cp -f , lib/$(LIBH2_SO) $(PREFIX)/lib/$(LIBH2_SO_MmB))
79-
$(call cmd, "INSTALL", $(LIBH2_SO_M) , ln -sf, $(LIBH2_SO_MmB) $(PREFIX)/lib/$(LIBH2_SO_M))
80-
$(call cmd, "INSTALL", $(LIBH2_SO) , ln -sf, $(LIBH2_SO_MmB) $(PREFIX)/lib/$(LIBH2_SO))
81-
$(call cmd, "INSTALL", $(LIBH2_A) , cp -f , lib/$(LIBH2_A) $(PREFIX)/lib/$(LIBH2_A))
82-
$(call cmd, "LDCONFIG", "", ldconfig)
83-
$(call cmd, "INSTALL", $(CHAOS_BIN) , cp -f , bin/$(CHAOS_BIN) $(PREFIX)/bin/$(CHAOS_BIN))
84-
85-
uninstall:
86-
$(call cmd, "UNINSTALL", "include/h2" , rm -rf, $(PREFIX)/include/h2)
87-
$(call cmd, "UNINSTALL", $(LIBH2_SO_MmB), rm -f , $(PREFIX)/lib/$(LIBH2_SO_MmB))
88-
$(call cmd, "UNINSTALL", $(LIBH2_SO_M) , rm -f , $(PREFIX)/lib/$(LIBH2_SO_M))
89-
$(call cmd, "UNINSTALL", $(LIBH2_SO) , rm -f , $(PREFIX)/lib/$(LIBH2_SO))
90-
$(call cmd, "UNINSTALL", $(LIBH2_A) , rm -f , $(PREFIX)/lib/$(LIBH2_A))
91-
$(call cmd, "LDCONFIG", "", ldconfig)
92-
$(call cmd, "UNINSTALL", $(CHAOS_BIN) , rm -f , $(PREFIX)/bin/$(CHAOS_BIN))
93-
94-
configure: $(config)
95-
96-
cscope:
97-
$(call cmd, "CSCOPE", "", find . -name '*.c' -or -name '*.h' > cscope.files; cscope -b -q -k)
98-
99-
clean:
100-
$(call cmd, "CLEAN", "*.o", rm -rf, $(shell find -name "*.o"))
101-
$(call cmd, "CLEAN", "*.d", rm -rf, $(shell find -name "*.d"))
102-
103-
distclean: clean
104-
$(call cmd, "CLEAN", $(LIBH2_A) , rm -f, lib/$(LIBH2_A))
105-
$(call cmd, "CLEAN", $(LIBH2_SO) , rm -f, lib/$(LIBH2_SO))
106-
$(call cmd, "CLEAN", $(CHAOS_BIN), rm -f, bin/$(CHAOS_BIN))
107-
$(call cmd, "CLEAN", $(config) , rm -f, $(config))
108-
$(call cmd, "CLEAN", "cscope files", rm -f, $(CSCOPE_FILES))
109-
110-
.PHONY: all tests install uninstall configure cscope clean distclean
111-
112-
113-
libh2: lib/$(LIBH2_SO) lib/$(LIBH2_A)
114-
115-
chaos: bin/$(CHAOS_BIN)
116-
117-
.PHONY: libh2 chaos
118-
119-
120-
# Build rules
121-
$(config): config.in
122-
$(call cmd, "CONFIG", $@, cp -n, $^ $@)
123-
124-
bin/$(CHAOS_BIN): LDFLAGS += -ljansson
125-
bin/$(CHAOS_BIN): LDFLAGS += $(LIBH2_DEP_LDFLAGS)
126-
bin/$(CHAOS_BIN): $(CHAOS_OBJ) lib/$(LIBH2_SO)
127-
$(call clink, $(CHAOS_OBJ), $@)
128-
129-
$(CHAOS_OBJ): CFLAGS += $(LIBH2_DEP_CFLAGS)
130-
131-
lib/$(LIBH2_SO): LDFLAGS += -shared
132-
lib/$(LIBH2_SO): LDFLAGS += -Wl,-soname,$(LIBH2_SO_M)
133-
# Because lib/$(LIBH2_SO) may be built as a dependency of bin/$(CHAOS_BIN) $(LDFLAGS) might contain
134-
# $(LIBH2_DEP_LDFLAGS). We must ensure that is removed before linking lib/$(LIBH2_SO).
135-
lib/$(LIBH2_SO): LDFLAGS += $(XEN_LDFLAGS)
136-
lib/$(LIBH2_SO): LDFLAGS := $(filter-out $(LIBH2_DEP_LDFLAGS), $(LDFLAGS))
137-
lib/$(LIBH2_SO): $(LIBH2_OBJ)
138-
$(call clink, $^, $@)
139-
140-
lib/$(LIBH2_A): $(LIBH2_OBJ)
141-
$(call cmd, "AR", $@, ar rcs, $@ $^)
142-
143-
$(LIBH2_OBJ): CFLAGS += -fPIC
144-
$(LIBH2_OBJ): CFLAGS += $(XEN_CFLAGS)
145-
146-
%.o: %.c $(config)
147-
$(call ccompile, $<, $@)
148-
149-
150-
# Define verbose command
151-
ifneq ($(verbose),y)
152-
cmd = @printf " %-10s %s\n" $(1) $(2) && $(3) $(4)
153-
else
154-
cmd = $(3) $(4)
21+
XEN_LDFLAGS += -Wl,-rpath-link,$(XEN_ROOT)/dist/install/$(XEN_PREFIX)/lib
22+
23+
# Chaos
24+
chaos_obj :=
25+
chaos_obj += bin/chaos.src/config.o
26+
chaos_obj += bin/chaos.src/cmdline.o
27+
chaos_obj += bin/chaos.src/chaos.o
28+
29+
$(eval $(call smk_binary,chaos,$(chaos_obj)))
30+
$(eval $(call smk_depend,chaos,h2))
31+
32+
$(chaos_bin): LDFLAGS += -ljansson -lh2
33+
$(chaos_bin): LDFLAGS += $(XEN_LDFLAGS)
34+
$(chaos_obj): CFLAGS += $(XEN_CFLAGS)
35+
36+
# LibH2
37+
libh2_obj :=
38+
libh2_obj += lib/h2/xen/xc.o
39+
libh2_obj += lib/h2/xen/dev.o
40+
libh2_obj += lib/h2/xen/vif.o
41+
libh2_obj += lib/h2/xen/xs.o
42+
libh2_obj += lib/h2/xen/console.o
43+
libh2_obj += lib/h2/h2.o
44+
libh2_obj += lib/h2/xen.o
45+
ifeq ($(CONFIG_H2_XEN_NOXS),y)
46+
libh2_obj += lib/h2/xen/noxs.o
15547
endif
15648

157-
# Compile and link commands
158-
ccompile = $(call cmd, "CC", $2, $(CC), $(CFLAGS) -c $(1) -o $(2))
159-
clink = $(call cmd, "LD", $2, $(CC), $(CFLAGS) $(1) $(LDFLAGS) -o $(2))
160-
49+
$(eval $(call smk_library,h2,$(LIBH2_V_MAJOR),$(LIBH2_V_MINOR),$(LIBH2_V_BUGFIX),$(libh2_obj)))
16150

162-
# Include auto-generated prerequisites
163-
-include $(CHAOS_OBJ:%.o=%.d)
164-
-include $(LIBH2_OBJ:%.o=%.d)
51+
$(libh2_so): LDFLAGS += -lxenctrl -lxenstore -lxenguest -lxentoollog -lxenforeignmemory
52+
$(libh2_so): LDFLAGS += $(XEN_LDFLAGS)
53+
$(libh2_obj): CFLAGS += $(XEN_CFLAGS)

configure

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
cp config.in .config

smk/LICENSE

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2016, Filipe Manco All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions
5+
are met:
6+
7+
1. Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
3. Neither the name of the copyright holder nor the names of its
13+
contributors may be used to endorse or promote products derived from
14+
this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26+
SUCH DAMAGE.

smk/README.md

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# SimpleMaKe: simple build system based on GNU make
2+
3+
SimpleMaKe intends to provide a simple and straightforward build system for C
4+
and C++ projects. It provides all the boilerplate for the most common targets
5+
leaving the user with only the simple task of defining the binaries and/or
6+
libraries to be built, and the objects they depend on.
7+
8+
This is not intended to build complex projects with complex structures or
9+
complex configuration or build needs. Rather it targets simple projects that
10+
use simple and standard build mechanisms, and a project structure defined in
11+
the next section.
12+
13+
## Project structure
14+
15+
SimpleMaKe based projects have always the same base directory structure:
16+
17+
```
18+
.
19+
├── bin : sources for binaries
20+
├── inc : headers for libraries
21+
├── lib : sources for libraries
22+
├── smk : SimpleMaKe sources
23+
└── Makefile
24+
```
25+
26+
The idea behind this scheme is to always provide most of the functionality as
27+
a library that is then used by one or more very simple applications.
28+
Applications have only the simple tasks of read command line arguments and/or
29+
configurations files, and implement some sort of main loop that mostly calls
30+
libraries, being therefore relatively simple. Application specific headers
31+
(e.g. headers relative to configuration parsing), should be placed under `bin/`
32+
and imported by the application with `""` as opposed to `<>`.
33+
34+
Although the idea is to always write most of the functionality as a library you
35+
don't have to build it as a library, instead you can build everything as a
36+
static binary. The reference text bellow explains the multiple ways you can use
37+
smk.
38+
39+
## Basic targets
40+
41+
SimpleMaKe provides the following targets:
42+
43+
* **all**: build the project
44+
* **install**: install project's binaries and libraries on the system
45+
* **uninstall**: uninstall project from the system
46+
* **clean**: remove generated files except final binaries and libraries
47+
* **distclean**: remove all generated files
48+
* **configure**: Run a configuration script
49+
50+
The system also defines targets for each of the binaries and libraries defined.
51+
52+
53+
# Usage
54+
55+
## Basic usage
56+
57+
There are two main steps to use SimpleMaKe:
58+
59+
1. Include `s.mk`
60+
2. Define targets using `smk_*` functions
61+
62+
The most trivial Makefile looks like:
63+
64+
```
65+
include smk/s.mk
66+
67+
$(eval $(call smk_binary,app,bin/app.o))
68+
```
69+
70+
This example Makefile would be able to build a binary called `app` by
71+
compiling and linking `app.c` located under `bin/` directory.
72+
73+
74+
## SimpleMaKe reference
75+
76+
Simple make defines a set of functions that let you define what to build and a
77+
set of configuration variables to configure the behaviour of the system.
78+
79+
Please define the configuration variables before `include smk/s.mk`. To call
80+
smk functions please use the following construct
81+
`$(eval $(call smk_<fn>,<args>))` appropriately replacing `<fn>` and `<args>`.
82+
83+
### SimpleMaKe configuration
84+
85+
`verbose` : set to `y` to print verbose commands.
86+
87+
`smk_lang` : project language, one of `c` or `c++`. Default: `c`.
88+
89+
`smk_dir` : smk directory. Default: `smk`.
90+
91+
`smk_conf_file` : smk configuration file. Default: `.config`.
92+
93+
`smk_conf_script` : configuration script. Default: `./configure`.
94+
95+
### SimpleMaKe functions
96+
97+
`smk_binary (binary, objects)`
98+
99+
* `binary` : the binary name
100+
* `objects` : space separated list of object paths
101+
102+
Define a binary to be built.
103+
104+
Example usage: `$(eval $(call smk_binary,app,bin/app.o))`
105+
106+
`smk_library (library, v_major, v_minor, v_bug, objects)`
107+
108+
* `library` : the library name
109+
* `v_major` : library version major number
110+
* `v_minor` : library version minor number
111+
* `v_bug` : library version bug revision
112+
* `objects` : space separated list of object paths
113+
114+
Define a library to be built.
115+
116+
Example usage: `$(eval $(call smk_library,apl,0,1,0,lib/apl.o))`
117+
118+
`smk_depends (binary,library)`
119+
120+
* `binary` : the binary name
121+
* `library` : the library name
122+
123+
Define a dependency of a project binary on a project library. Notice that this
124+
function won't add compilation flags for linking the binary with the library,
125+
you need to add those manually.
126+
127+
Example usage: `$(eval $(call smk_depend,app,apl))`
128+
129+
130+
# SimpleMaKe based Makefile example
131+
132+
Let's now define a more complex Makfile to build an application called app,
133+
comprised of two source files and a library called apl, comprised of two more
134+
source files. Define the dependency between them and a set of build flags.
135+
136+
```
137+
include smk/s.mk
138+
139+
app_obj :=
140+
app_obj += bin/app.o
141+
app_obj += bin/cmdline.o
142+
143+
apl_obj :=
144+
apl_obj += lib/structs.o
145+
apl_obj += lib/business.o
146+
147+
148+
$(eval $(call smk_binary,app,$(app_obj)))
149+
$(eval $(call smk_depend,app,apl))
150+
151+
$(apl_obj): CFLAGS += -DAPP_CONFIG_1
152+
$(apl_bin): LDFLAGS += -lapl
153+
154+
$(eval $(call smk_library,apl,$(apl_obj)))
155+
```
156+
157+
And that's all you need to have your build system ready.

0 commit comments

Comments
 (0)