btrfs-progs: drop feature defines from C files, in favour of CFLAGS defines
[platform/upstream/btrfs-progs.git] / Makefile
1 # Export all variables to sub-makes by default
2 export
3
4 CC = gcc
5 LN = ln
6 AR = ar
7 AM_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES -D_XOPEN_SOURCE=700 -D_GNU_SOURCE -fno-strict-aliasing -fPIC
8 CFLAGS = -g -O1 -fno-strict-aliasing
9 LDFLAGS = -rdynamic
10 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
11           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
12           extent-cache.o extent_io.o volumes.o utils.o repair.o \
13           qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
14           ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
15           inode.o
16 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
17                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
18                cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
19                cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
20                cmds-property.o cmds-fi-disk_usage.o
21 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
22                    uuid-tree.o utils-lib.o rbtree-utils.o
23 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
24                crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
25                extent_io.h ioctl.h ctree.h btrfsck.h version.h
26 TESTS = fsck-tests.sh convert-tests.sh
27
28 INSTALL = install
29 prefix ?= /usr/local
30 bindir = $(prefix)/bin
31 lib_LIBS = -luuid -lblkid -lz -llzo2 -L. -pthread
32 libdir ?= $(prefix)/lib
33 incdir = $(prefix)/include/btrfs
34 LIBS = $(lib_LIBS) $(libs_static)
35
36 ifeq ("$(origin V)", "command line")
37   BUILD_VERBOSE = $(V)
38 endif
39 ifndef BUILD_VERBOSE
40   BUILD_VERBOSE = 0
41 endif
42
43 ifeq ($(BUILD_VERBOSE),1)
44   Q =
45 else
46   Q = @
47 endif
48
49 MAKEOPTS = --no-print-directory Q=$(Q)
50
51 progs = mkfs.btrfs btrfs-debug-tree btrfsck \
52         btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
53         btrfs-find-root btrfstune btrfs-show-super
54
55 progs_extra = btrfs-corrupt-block btrfs-fragments btrfs-calc-size \
56               btrfs-select-super
57
58 progs_static = $(foreach p,$(progs),$(p).static)
59
60 # external libs required by various binaries; for btrfs-foo,
61 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
62 btrfs_convert_libs = -lext2fs -lcom_err
63 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
64
65 SUBDIRS =
66 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
67 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
68 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
69
70 ifeq ($(DISABLE_BACKTRACE),1)
71 AM_CFLAGS += -DBTRFS_DISABLE_BACKTRACE
72 endif
73
74 ifneq ($(DISABLE_DOCUMENTATION),1)
75 BUILDDIRS += build-Documentation
76 INSTALLDIRS += install-Documentation
77 endif
78
79 .PHONY: $(SUBDIRS)
80 .PHONY: $(BUILDDIRS)
81 .PHONY: $(INSTALLDIRS)
82 .PHONY: $(TESTDIRS)
83 .PHONY: $(CLEANDIRS)
84 .PHONY: all install clean
85
86 # Create all the static targets
87 static_objects = $(patsubst %.o, %.static.o, $(objects))
88 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
89 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
90
91 # Define static compilation flags
92 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
93 STATIC_LDFLAGS = -static -Wl,--gc-sections
94 STATIC_LIBS = $(lib_LIBS)
95
96 libs_shared = libbtrfs.so.0.1
97 libs_static = libbtrfs.a
98 libs = $(libs_shared) $(libs_static)
99 lib_links = libbtrfs.so.0 libbtrfs.so
100 headers = $(libbtrfs_headers)
101
102 # make C=1 to enable sparse
103 check_defs := .cc-defines.h 
104 ifdef C
105         #
106         # We're trying to use sparse against glibc headers which go wild
107         # trying to use internal compiler macros to test features.  We
108         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
109         # 'cause sparse defines that one.
110         #
111         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
112                         grep -v __SIZE_TYPE__ > $(check_defs))
113         check = sparse -include $(check_defs) -D__CHECKER__ \
114                 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
115         check_echo = echo
116         # don't use FORTIFY with sparse because glibc with FORTIFY can
117         # generate so many sparse errors that sparse stops parsing,
118         # which masks real errors that we want to see.
119 else
120         check = true
121         check_echo = true
122         AM_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
123 endif
124
125 %.o.d: %.c
126         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(AM_CFLAGS) $(CFLAGS) $<
127
128 .c.o:
129         @$(check_echo) "    [SP]     $<"
130         $(Q)$(check) $(AM_CFLAGS) $(CFLAGS) $<
131         @echo "    [CC]     $@"
132         $(Q)$(CC) $(AM_CFLAGS) $(CFLAGS) -c $<
133
134 %.static.o: %.c
135         @echo "    [CC]     $@"
136         $(Q)$(CC) $(AM_CFLAGS) $(STATIC_CFLAGS) -c $< -o $@
137
138 all: $(progs) $(BUILDDIRS)
139 $(SUBDIRS): $(BUILDDIRS)
140 $(BUILDDIRS):
141         @echo "Making all in $(patsubst build-%,%,$@)"
142         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
143
144 test: btrfs btrfs-convert btrfs-image btrfs-corrupt-block
145         $(Q)for t in $(TESTS); do \
146                 echo "    [TEST]   $$t"; \
147                 bash tests/$$t || exit 1; \
148         done
149
150 #
151 # NOTE: For static compiles, you need to have all the required libs
152 #       static equivalent available
153 #
154 static: $(progs_static)
155
156 version.h:
157         @echo "    [SH]     $@"
158         $(Q)bash version.sh
159
160 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
161         @echo "    [LD]     $@"
162         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(lib_LIBS) \
163                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
164
165 $(libs_static): $(libbtrfs_objects)
166         @echo "    [AR]     $@"
167         $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
168
169 $(lib_links):
170         @echo "    [LN]     $@"
171         $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so.0
172         $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so
173
174 # keep intermediate files from the below implicit rules around
175 .PRECIOUS: $(addsuffix .o,$(progs))
176
177 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
178 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
179 # turns them into a list of libraries to link against if they exist
180 #
181 # For static variants, use an extra $(subst) to get rid of the ".static"
182 # from the target name before translating to list of libs
183
184 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
185         @echo "    [LD]     $@"
186         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
187                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
188                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
189
190 btrfs-%: $(objects) $(libs) btrfs-%.o
191         @echo "    [LD]     $@"
192         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
193
194 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
195         @echo "    [LD]     $@"
196         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
197                 $(objects) $(LDFLAGS) $(LIBS)
198
199 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
200         @echo "    [LD]     $@"
201         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
202                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
203
204 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
205 btrfsck: btrfs
206         @echo "    [LN]     $@"
207         $(Q)$(LN) -f btrfs btrfsck
208
209 btrfsck.static: btrfs.static
210         @echo "    [LN]     $@"
211         $(Q)$(LN) -f $^ $@
212
213 mkfs.btrfs: $(objects) $(libs) mkfs.o
214         @echo "    [LD]     $@"
215         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS)
216
217 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
218         @echo "    [LD]     $@"
219         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
220                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
221
222 btrfstune: $(objects) $(libs) btrfstune.o
223         @echo "    [LD]     $@"
224         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS)
225
226 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
227         @echo "    [LD]     $@"
228         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
229                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
230
231 dir-test: $(objects) $(libs) dir-test.o
232         @echo "    [LD]     $@"
233         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) dir-test.o $(LDFLAGS) $(LIBS)
234
235 quick-test: $(objects) $(libs) quick-test.o
236         @echo "    [LD]     $@"
237         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) quick-test.o $(LDFLAGS) $(LIBS)
238
239 ioctl-test: $(objects) $(libs) ioctl-test.o
240         @echo "    [LD]     $@"
241         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS)
242
243 send-test: $(objects) $(libs) send-test.o
244         @echo "    [LD]     $@"
245         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) send-test.o $(LDFLAGS) $(LIBS)
246
247 library-test: $(libs_shared) library-test.o
248         @echo "    [LD]     $@"
249         $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
250
251 library-test.static: $(libs_static) library-test.o
252         @echo "    [LD]     $@"
253         $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
254
255 test-build:
256         $(MAKE) clean-all
257         $(MAKE) library-test
258         -$(MAKE) library-test.static
259         $(MAKE) -j 8 all
260         -$(MAKE) -j 8 static
261         $(MAKE) -j 8 $(progs_extra)
262
263 manpages:
264         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
265
266 clean-all: clean-doc clean
267
268 clean: $(CLEANDIRS)
269         @echo "Cleaning"
270         $(Q)rm -f $(progs) cscope.out *.o *.o.d \
271               dir-test ioctl-test quick-test send-test library-test library-test-static \
272               btrfs.static mkfs.btrfs.static \
273               version.h $(check_defs) \
274               $(libs) $(lib_links) \
275               $(progs_static) $(progs_extra)
276
277 clean-doc:
278         @echo "Cleaning Documentation"
279         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
280
281 $(CLEANDIRS):
282         @echo "Cleaning $(patsubst clean-%,%,$@)"
283         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
284
285 install: $(libs) $(progs) $(INSTALLDIRS)
286         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
287         $(INSTALL) $(progs) $(DESTDIR)$(bindir)
288         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
289         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
290         $(LN) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
291         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
292         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
293         cp -a $(lib_links) $(DESTDIR)$(libdir)
294         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
295         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
296
297 install-static: $(progs_static) $(INSTALLDIRS)
298         for p in $(progs_static) ; do \
299                 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/`basename $$p .static` ; \
300         done
301
302 $(INSTALLDIRS):
303         @echo "Making install in $(patsubst install-%,%,$@)"
304         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
305
306 uninstall:
307         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
308         cd $(DESTDIR)$(incdir); rm -f $(headers)
309         rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
310         cd $(DESTDIR)$(libdir); rm -f $(lib_links) $(libs)
311         cd $(DESTDIR)$(bindir); rm -f btrfsck fsck.btrfs $(progs)
312
313 ifneq ($(MAKECMDGOALS),clean)
314 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
315 endif