btrfs-progs: get C=1 sparse checking working again
[platform/upstream/btrfs-progs.git] / Makefile
1 CC = gcc
2 LN = ln
3 AR = ar
4 AM_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES -fPIC
5 CFLAGS = -g -O1
6 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
7           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
8           extent-cache.o extent_io.o volumes.o utils.o repair.o \
9           qgroup.o raid6.o free-space-cache.o uuid-tree.o
10 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
11                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
12                cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
13                cmds-restore.o cmds-chunk.o
14 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
15                    uuid-tree.o
16 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
17                crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
18                extent_io.h ioctl.h ctree.h btrfsck.h
19
20 INSTALL = install
21 prefix ?= /usr/local
22 bindir = $(prefix)/bin
23 lib_LIBS = -luuid -lblkid -lm -lz -llzo2 -L.
24 libdir ?= $(prefix)/lib
25 incdir = $(prefix)/include/btrfs
26 LIBS = $(lib_LIBS) $(libs_static)
27
28 ifeq ("$(origin V)", "command line")
29   BUILD_VERBOSE = $(V)
30 endif
31 ifndef BUILD_VERBOSE
32   BUILD_VERBOSE = 0
33 endif
34
35 ifeq ($(BUILD_VERBOSE),1)
36   Q =
37 else
38   Q = @
39 endif
40
41 MAKEOPTS = --no-print-directory Q=$(Q)
42
43 progs = mkfs.btrfs btrfs-debug-tree btrfsck \
44         btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
45         btrfs-find-root btrfstune btrfs-show-super
46
47 # external libs required by various binaries; for btrfs-foo,
48 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
49 btrfs_convert_libs = -lext2fs -lcom_err
50 btrfs_image_libs = -lpthread
51 btrfs_fragment_libs = -lgd -lpng -ljpeg -lfreetype
52
53 # Create all the static targets
54 static_objects = $(patsubst %.o, %.static.o, $(objects))
55 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
56 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
57
58 # Define static compilation flags
59 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
60 STATIC_LDFLAGS = -static -Wl,--gc-sections
61 STATIC_LIBS = $(lib_LIBS) -lpthread
62
63 libs_shared = libbtrfs.so.0.1
64 libs_static = libbtrfs.a
65 libs = $(libs_shared) $(libs_static)
66 lib_links = libbtrfs.so.0 libbtrfs.so
67 headers = $(libbtrfs_headers)
68
69 # make C=1 to enable sparse
70 check_defs := .cc-defines.h 
71 ifdef C
72         #
73         # We're trying to use sparse against glibc headers which go wild
74         # trying to use internal compiler macros to test features.  We
75         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
76         # 'cause sparse defines that one.
77         #
78         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
79                         grep -v __SIZE_TYPE__ > $(check_defs))
80         check = sparse -include $(check_defs) -D__CHECKER__ \
81                 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
82         check_echo = echo
83         # don't use FORTIFY with sparse because glibc with FORTIFY can
84         # generate so many sparse errors that sparse stops parsing,
85         # which masks real errors that we want to see.
86 else
87         check = true
88         check_echo = true
89         AM_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
90 endif
91
92 %.o.d: %.c
93         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(AM_CFLAGS) $(CFLAGS) $<
94
95 .c.o:
96         @$(check_echo) "    [SP]     $<"
97         $(Q)$(check) $(AM_CFLAGS) $(CFLAGS) $<
98         @echo "    [CC]     $@"
99         $(Q)$(CC) $(AM_CFLAGS) $(CFLAGS) -c $<
100
101 %.static.o: %.c
102         @echo "    [CC]     $@"
103         $(Q)$(CC) $(AM_CFLAGS) $(STATIC_CFLAGS) -c $< -o $@
104
105 all: $(progs) manpages
106
107 #
108 # NOTE: For static compiles, you need to have all the required libs
109 #       static equivalent available
110 #
111 static: btrfs.static mkfs.btrfs.static btrfs-find-root.static
112
113 version.h:
114         @echo "    [SH]     $@"
115         $(Q)bash version.sh
116
117 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
118         @echo "    [LD]     $@"
119         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(lib_LIBS) \
120                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
121
122 $(libs_static): $(libbtrfs_objects)
123         @echo "    [AR]     $@"
124         $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
125
126 $(lib_links):
127         @echo "    [LN]     $@"
128         $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so.0
129         $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so
130
131 # keep intermediate files from the below implicit rules around
132 .PRECIOUS: $(addsuffix .o,$(progs))
133
134 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
135 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
136 # turns them into a list of libraries to link against if they exist
137 #
138 # For static variants, use an extra $(subst) to get rid of the ".static"
139 # from the target name before translating to list of libs
140
141 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
142         @echo "    [LD]     $@"
143         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
144                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS) \
145                 $($(subst -,_,$(subst .static,,$@)-libs))
146
147 btrfs-%: $(objects) $(libs) btrfs-%.o
148         @echo "    [LD]     $@"
149         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
150
151 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
152         @echo "    [LD]     $@"
153         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
154                 $(objects) $(LDFLAGS) $(LIBS) -lpthread
155
156 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
157         @echo "    [LD]     $@"
158         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
159                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
160
161 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
162 btrfsck: btrfs
163         @echo "    [LN]     $@"
164         $(Q)$(LN) -f btrfs btrfsck
165
166 mkfs.btrfs: $(objects) $(libs) mkfs.o
167         @echo "    [LD]     $@"
168         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS)
169
170 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
171         @echo "    [LD]     $@"
172         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
173                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
174
175 btrfstune: $(objects) $(libs) btrfstune.o
176         @echo "    [LD]     $@"
177         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS)
178
179 dir-test: $(objects) $(libs) dir-test.o
180         @echo "    [LD]     $@"
181         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) dir-test.o $(LDFLAGS) $(LIBS)
182
183 quick-test: $(objects) $(libs) quick-test.o
184         @echo "    [LD]     $@"
185         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) quick-test.o $(LDFLAGS) $(LIBS)
186
187 ioctl-test: $(objects) $(libs) ioctl-test.o
188         @echo "    [LD]     $@"
189         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS)
190
191 send-test: $(objects) $(libs) send-test.o
192         @echo "    [LD]     $@"
193         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) send-test.o $(LDFLAGS) $(LIBS) -lpthread
194
195 manpages:
196         $(Q)$(MAKE) $(MAKEOPTS) -C man
197
198 install-man:
199         cd man; $(MAKE) install
200
201 clean :
202         @echo "Cleaning"
203         $(Q)rm -f $(progs) cscope.out *.o *.o.d btrfs-convert btrfs-image btrfs-select-super \
204               btrfs-zero-log btrfstune dir-test ioctl-test quick-test send-test btrfsck \
205               btrfs.static mkfs.btrfs.static btrfs-calc-size \
206               version.h $(check_defs) \
207               $(libs) $(lib_links)
208         $(Q)$(MAKE) $(MAKEOPTS) -C man $@
209
210 install: $(libs) $(progs) install-man
211         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
212         $(INSTALL) $(progs) $(DESTDIR)$(bindir)
213         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
214         $(LN) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
215         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
216         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
217         cp -a $(lib_links) $(DESTDIR)$(libdir)
218         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
219         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
220
221 ifneq ($(MAKECMDGOALS),clean)
222 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
223 endif