Sparse v0.5.1
[smatch.git] / Makefile
blob84bb133da550e484ddf1489905582e490cd33728
1 VERSION=0.5.1
3 # Generating file version.h if current version has changed
4 SPARSE_VERSION:=$(shell git describe 2>/dev/null || echo '$(VERSION)')
5 VERSION_H := $(shell cat version.h 2>/dev/null)
6 ifneq ($(lastword $(VERSION_H)),"$(SPARSE_VERSION)")
7 $(info $(shell echo ' GEN 'version.h))
8 $(shell echo '#define SPARSE_VERSION "$(SPARSE_VERSION)"' > version.h)
9 endif
11 OS = linux
14 CC = gcc
15 CFLAGS = -O2 -finline-functions -fno-strict-aliasing -g
16 CFLAGS += -Wall -Wwrite-strings
17 LDFLAGS += -g
18 LD = gcc
19 AR = ar
20 PKG_CONFIG = pkg-config
21 CHECKER = ./cgcc -no-compile
22 CHECKER_FLAGS =
24 ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS)
26 # For debugging, put this in local.mk:
28 # CFLAGS += -O0 -DDEBUG -g3 -gdwarf-2
31 HAVE_LIBXML:=$(shell $(PKG_CONFIG) --exists libxml-2.0 2>/dev/null && echo 'yes')
32 HAVE_GCC_DEP:=$(shell touch .gcc-test.c && \
33 $(CC) -c -Wp,-MD,.gcc-test.d .gcc-test.c 2>/dev/null && \
34 echo 'yes'; rm -f .gcc-test.d .gcc-test.o .gcc-test.c)
36 GTK_VERSION:=3.0
37 HAVE_GTK:=$(shell $(PKG_CONFIG) --exists gtk+-$(GTK_VERSION) 2>/dev/null && echo 'yes')
38 ifneq ($(HAVE_GTK),yes)
39 GTK_VERSION:=2.0
40 HAVE_GTK:=$(shell $(PKG_CONFIG) --exists gtk+-$(GTK_VERSION) 2>/dev/null && echo 'yes')
41 endif
43 LLVM_CONFIG:=llvm-config
44 HAVE_LLVM:=$(shell $(LLVM_CONFIG) --version >/dev/null 2>&1 && echo 'yes')
46 GCC_BASE := $(shell $(CC) --print-file-name=)
47 BASIC_CFLAGS = -DGCC_BASE=\"$(GCC_BASE)\"
49 MULTIARCH_TRIPLET := $(shell $(CC) -print-multiarch 2>/dev/null)
50 BASIC_CFLAGS += -DMULTIARCH_TRIPLET=\"$(MULTIARCH_TRIPLET)\"
52 ifeq ($(HAVE_GCC_DEP),yes)
53 BASIC_CFLAGS += -Wp,-MD,$(@D)/.$(@F).d
54 endif
56 DESTDIR=
57 PREFIX=$(HOME)
58 BINDIR=$(PREFIX)/bin
59 LIBDIR=$(PREFIX)/lib
60 MANDIR=$(PREFIX)/share/man
61 MAN1DIR=$(MANDIR)/man1
62 INCLUDEDIR=$(PREFIX)/include
63 PKGCONFIGDIR=$(LIBDIR)/pkgconfig
65 PROGRAMS=test-lexing test-parsing obfuscate compile graph sparse \
66 test-linearize example test-unssa test-dissect ctags
67 INST_PROGRAMS=sparse cgcc
68 INST_MAN1=sparse.1 cgcc.1
70 ifeq ($(HAVE_LIBXML),yes)
71 PROGRAMS+=c2xml
72 INST_PROGRAMS+=c2xml
73 c2xml_EXTRA_OBJS = `$(PKG_CONFIG) --libs libxml-2.0`
74 LIBXML_CFLAGS := $(shell $(PKG_CONFIG) --cflags libxml-2.0)
75 else
76 $(warning Your system does not have libxml, disabling c2xml)
77 endif
79 ifeq ($(HAVE_GTK),yes)
80 GTK_CFLAGS := $(shell $(PKG_CONFIG) --cflags gtk+-$(GTK_VERSION))
81 GTK_LIBS := $(shell $(PKG_CONFIG) --libs gtk+-$(GTK_VERSION))
82 PROGRAMS += test-inspect
83 INST_PROGRAMS += test-inspect
84 test-inspect_EXTRA_DEPS := ast-model.o ast-view.o ast-inspect.o
85 test-inspect_OBJS := test-inspect.o $(test-inspect_EXTRA_DEPS)
86 $(test-inspect_OBJS) $(test-inspect_OBJS:.o=.sc): CFLAGS += $(GTK_CFLAGS)
87 test-inspect_EXTRA_OBJS := $(GTK_LIBS)
88 else
89 $(warning Your system does not have gtk3/gtk2, disabling test-inspect)
90 endif
92 ifeq ($(HAVE_LLVM),yes)
93 LLVM_VERSION:=$(shell $(LLVM_CONFIG) --version)
94 ifeq ($(shell expr "$(LLVM_VERSION)" : '[3-9]\.'),2)
95 LLVM_PROGS := sparse-llvm
96 $(LLVM_PROGS): LD := g++
97 LLVM_LDFLAGS := $(shell $(LLVM_CONFIG) --ldflags)
98 LLVM_CFLAGS := $(shell $(LLVM_CONFIG) --cflags | sed -e "s/-DNDEBUG//g" | sed -e "s/-pedantic//g")
99 LLVM_LIBS := $(shell $(LLVM_CONFIG) --libs)
100 LLVM_LIBS += $(shell $(LLVM_CONFIG) --system-libs 2>/dev/null)
101 PROGRAMS += $(LLVM_PROGS)
102 INST_PROGRAMS += sparse-llvm sparsec
103 sparse-llvm.o: BASIC_CFLAGS += $(LLVM_CFLAGS)
104 sparse-llvm_EXTRA_OBJS := $(LLVM_LIBS) $(LLVM_LDFLAGS)
105 else
106 $(warning LLVM 3.0 or later required. Your system has version $(LLVM_VERSION) installed.)
107 endif
108 else
109 $(warning Your system does not have llvm, disabling sparse-llvm)
110 endif
112 LIB_H= token.h parse.h lib.h symbol.h scope.h expression.h target.h \
113 linearize.h bitmap.h ident-list.h compat.h flow.h allocate.h \
114 storage.h ptrlist.h dissect.h
116 LIB_OBJS= target.o parse.o tokenize.o pre-process.o symbol.o lib.o scope.o \
117 expression.o show-parse.o evaluate.o expand.o inline.o linearize.o \
118 char.o sort.o allocate.o compat-$(OS).o ptrlist.o \
119 builtin.o \
120 stats.o \
121 flow.o cse.o simplify.o memops.o liveness.o storage.o unssa.o dissect.o
123 LIB_FILE= libsparse.a
124 SLIB_FILE= libsparse.so
126 # If you add $(SLIB_FILE) to this, you also need to add -fpic to BASIC_CFLAGS above.
127 # Doing so incurs a noticeable performance hit, and Sparse does not have a
128 # stable shared library interface, so this does not occur by default. If you
129 # really want a shared library, you may want to build Sparse twice: once
130 # without -fpic to get all the Sparse tools, and again with -fpic to get the
131 # shared library.
132 LIBS=$(LIB_FILE)
135 # Pretty print
137 V = @
138 Q = $(V:1=)
139 QUIET_CC = $(Q:@=@echo ' CC '$@;)
140 QUIET_CHECK = $(Q:@=@echo ' CHECK '$<;)
141 QUIET_AR = $(Q:@=@echo ' AR '$@;)
142 QUIET_GEN = $(Q:@=@echo ' GEN '$@;)
143 QUIET_LINK = $(Q:@=@echo ' LINK '$@;)
144 # We rely on the -v switch of install to print 'file -> $install_dir/file'
145 QUIET_INST_SH = $(Q:@=echo -n ' INSTALL ';)
146 QUIET_INST = $(Q:@=@echo -n ' INSTALL ';)
148 define INSTALL_EXEC
149 $(QUIET_INST)install -v $1 $(DESTDIR)$2/$1 || exit 1;
151 endef
153 define INSTALL_FILE
154 $(QUIET_INST)install -v -m 644 $1 $(DESTDIR)$2/$1 || exit 1;
156 endef
158 SED_PC_CMD = 's|@version@|$(VERSION)|g; \
159 s|@prefix@|$(PREFIX)|g; \
160 s|@libdir@|$(LIBDIR)|g; \
161 s|@includedir@|$(INCLUDEDIR)|g'
165 # Allow users to override build settings without dirtying their trees
166 -include local.mk
169 all: $(PROGRAMS) sparse.pc
171 all-installable: $(INST_PROGRAMS) $(LIBS) $(LIB_H) sparse.pc
173 install: all-installable
174 $(Q)install -d $(DESTDIR)$(BINDIR)
175 $(Q)install -d $(DESTDIR)$(LIBDIR)
176 $(Q)install -d $(DESTDIR)$(MAN1DIR)
177 $(Q)install -d $(DESTDIR)$(INCLUDEDIR)/sparse
178 $(Q)install -d $(DESTDIR)$(PKGCONFIGDIR)
179 $(foreach f,$(INST_PROGRAMS),$(call INSTALL_EXEC,$f,$(BINDIR)))
180 $(foreach f,$(INST_MAN1),$(call INSTALL_FILE,$f,$(MAN1DIR)))
181 $(foreach f,$(LIBS),$(call INSTALL_FILE,$f,$(LIBDIR)))
182 $(foreach f,$(LIB_H),$(call INSTALL_FILE,$f,$(INCLUDEDIR)/sparse))
183 $(call INSTALL_FILE,sparse.pc,$(PKGCONFIGDIR))
185 sparse.pc: sparse.pc.in
186 $(QUIET_GEN)sed $(SED_PC_CMD) sparse.pc.in > sparse.pc
189 compile_EXTRA_DEPS = compile-i386.o
191 $(foreach p,$(PROGRAMS),$(eval $(p): $($(p)_EXTRA_DEPS) $(LIBS)))
192 $(PROGRAMS): % : %.o
193 $(QUIET_LINK)$(LD) $(LDFLAGS) -o $@ $^ $($@_EXTRA_OBJS)
195 $(LIB_FILE): $(LIB_OBJS)
196 $(QUIET_AR)$(AR) rcs $@ $(LIB_OBJS)
198 $(SLIB_FILE): $(LIB_OBJS)
199 $(QUIET_LINK)$(CC) $(LDFLAGS) -Wl,-soname,$@ -shared -o $@ $(LIB_OBJS)
201 DEP_FILES := $(wildcard .*.o.d)
203 ifneq ($(DEP_FILES),)
204 include $(DEP_FILES)
205 endif
207 c2xml.o c2xml.sc: CFLAGS += $(LIBXML_CFLAGS)
209 pre-process.sc: CHECKER_FLAGS += -Wno-vla
211 %.o: %.c $(LIB_H)
212 $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $<
214 %.sc: %.c sparse
215 $(QUIET_CHECK) $(CHECKER) $(CHECKER_FLAGS) -c $(ALL_CFLAGS) $<
217 ALL_OBJS := $(LIB_OBJS) $(foreach p,$(PROGRAMS),$(p).o $($(p)_EXTRA_DEPS))
218 selfcheck: $(ALL_OBJS:.o=.sc)
221 clean: clean-check
222 rm -f *.[oa] .*.d *.so $(PROGRAMS) $(SLIB_FILE) pre-process.h sparse.pc
224 dist:
225 @if test "$(SPARSE_VERSION)" != "v$(VERSION)" ; then \
226 echo 'Update VERSION in the Makefile before running "make dist".' ; \
227 exit 1 ; \
229 git archive --format=tar --prefix=sparse-$(VERSION)/ HEAD^{tree} | gzip -9 > sparse-$(VERSION).tar.gz
231 check: all
232 $(Q)cd validation && ./test-suite
234 clean-check:
235 find validation/ \( -name "*.c.output.expected" \
236 -o -name "*.c.output.got" \
237 -o -name "*.c.output.diff" \
238 -o -name "*.c.error.expected" \
239 -o -name "*.c.error.got" \
240 -o -name "*.c.error.diff" \
241 \) -exec rm {} \;