forked from wilhelmtell/inspext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
168 lines (148 loc) · 5.69 KB
/
Makefile
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
###############################################################################
# Copyright (C) 2010 Matan Nassau
#
# This file is part of INSPext.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
# Specify the project's name
PROJECT_NAME = "INSPext"
# Specify the main target
TARGET = inspc
# Default build type
TYPE = debug
# Which directories contain source files
DIRS = .
# Which libraries are linked
LIBS =
# Dynamic libraries
DLIBS =
# Add directories to the include and library paths
INCPATH = $(HOME)/Development/include
LIBPATH =
# Which files to add to backups, apart from the source code
EXTRA_FILES = Makefile README grammar
# The compiler
CC = gcc
# filename-friendly project name
PROJECT_FILENAME = $(shell echo $(PROJECT_NAME) | tr -d -C 'a-zA-Z0-9 _-' |tr 'A-Z ' 'a-z_')
# Where to store object and dependancy files.
STORE = .make-$(TYPE)
# Makes a list of the source (.c) files.
SOURCE := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.c))
# List of header files.
HEADERS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.h))
# Makes a list of the object files that will have to be created.
OBJECTS := $(addprefix $(STORE)/, $(SOURCE:.c=.o))
# Same for the .d (dependancy) files.
DFILES := $(addprefix $(STORE)/,$(SOURCE:.c=.d))
# function for reversing a list. this should be a standard function ...
reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
# The next blocks change some variables depending on the build type
ifeq ($(TYPE),debug)
LDPARAM =
CCPARAM = -Wall -pedantic -ansi -g3
MACROS =
endif
ifeq ($(TYPE),profile)
LDPARAM = -pg /lib/libc.so.5
CCPARAM = -Wall -pedantic -ansi -pg
MACROS = NDEBUG
endif
ifeq ($(TYPE),release)
LDPARAM = -s
CCPARAM = -O2
MACROS = NDEBUG
endif
ifeq ($(TYPE),check)
LDPARAM =
CCPARAM = -Wall -pedantic -ansi -g3
MACROS =
DIRS = . test
TARGET = check
# Unit-test source includes test/main.c instead of ./main.c. We also have to
# specify test/main.c explicitly, not with a wildcard, because it initially
# doesn't exist; it will be auto-generated during the build.
SOURCE := $(filter-out ./main.c, $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.c)) test/main.c)
OBJECTS := $(addprefix $(STORE)/, $(SOURCE:.c=.o))
TEST_SOURCE := $(wildcard test/test_*.c)
TEST_HEADERS := $(TEST_SOURCE:.c=.h)
CHECK=./$(TARGET)
endif
# Specify phony rules. These are rules that are not real files.
.PHONY: all clean_check clean distclean backup
# Main target. The @ in front of a command prevents make from displaying
# it to the standard output.
all: $(TARGET)
$(TARGET): $(OBJECTS)
@echo " LD $(TARGET)"
@$(CC) -o $(TARGET) $(OBJECTS) $(LDPARAM) $(foreach LIBRARY, \
$(LIBS),-l$(LIBRARY)) $(foreach LIB,$(LIBPATH),-L$(LIB))
$(CHECK)
test/test_%.h: test/test_%.c
@-echo " GEN $@"
@-cd test && ./gen_test_h.sh $(notdir $<) >$(notdir $@)
# FIXME: If test/main.c recompiles then ld fails with dup symbol for _main.
# To reproduce: 1. make TYPE=check distclean && make TYPE=check
# 2. touch test/main.c
# 3. make TYPE=check
test/main.c: $(TEST_HEADERS)
@-echo " GEN $@"
@-cd test && ./gen_test_main.sh $(foreach FILE,$^,$(notdir $(FILE))) >$(notdir $@) && cd ..
# Rule for creating object file and .d file, the sed magic is to add
# the object path at the start of the file because the files gcc
# outputs assume it will be in the same dir as the source file.
$(STORE)/%.o: %.c
@-[ -d $(dir $@) ] || mkdir -p $(dir $@);
@echo " CC $?"
@$(CC) -Wp,-MMD,$(STORE)/$*.dd $(CCPARAM) $(foreach INC,$(INCPATH),-I$(INC)) \
$(foreach MACRO,$(MACROS),-D$(MACRO)) -c $< -o $@
@sed -e '1s/^\(.*\)$$/$(subst /,\/,$(dir $@))\1/' $(STORE)/$*.dd > $(STORE)/$*.d
@rm -f $(STORE)/$*.dd
# Empty rule to prevent problems when a header is deleted.
%.h: ;
clean_check:
@-echo " RM test/tmp.*.out"
@-rm -f test/tmp.*.out
@-echo " RM test/tmp.*.err"
@-rm -f test/tmp.*.err
@-echo " RM test/test_*.h"
@-rm -f test/test_*.h
@-echo " RM test/main.c"
@-rm -f test/main.c
# Cleans up the objects, .d files and executables.
# FIXME: doesn't consider make TYPE=check (we can have multiple stores)
clean: clean_check
@-$(foreach DIR,$(DIRS),echo " RM $(STORE)/$(DIR)/*.d"; \
rm -f $(STORE)/$(DIR)/*.d; \
echo " RM $(STORE)/$(DIR)/*.o"; \
rm -f $(STORE)/$(DIR)/*.o)
@-$(foreach DIR,$(call reverse, $(sort $(patsubst .,"",$(DIRS)))),if [ -d $(STORE)/$(DIR) ]; \
then echo " RM $(STORE)/$(DIR)"; rmdir $(STORE)/$(DIR); fi; )
# FIXME: doesn't consider make TYPE=check (we can have multiple targets)
distclean: clean
@echo " RM $(TARGET)"
@-rm -f $(TARGET)
# Backup the source files.
# FIXME: when TYPE=check backup is broken: SOURCE doesn't include test code
backup:
@-if [ ! -e .backup ]; then mkdir .backup; fi;
@if [ -e $(PROJECT_FILENAME) ]; then echo "Directory $(PROJECT_FILENAME) already exists." >&2; false; fi;
@mkdir $(PROJECT_FILENAME)
@cp --archive --parents $(SOURCE) $(HEADERS) $(EXTRA_FILES) $(PROJECT_FILENAME)
@tar c $(PROJECT_FILENAME) |gzip -9 >.backup/backup_`date +%Y%m%d%H%M`.tar.gz
@rm -rf $(PROJECT_FILENAME)
# Includes the .d files so it knows the exact dependencies for every
# source.
-include $(DFILES)