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