btrfs-progs: copy functionality of btrfs-debug-tree to inspect-internal subcommand
[platform/upstream/btrfs-progs.git] / Makefile.in
1 # btrfs-progs
2 #
3 # Basic build targets:
4 #   all         all main tools
5 #   static      build static bnaries, requires static version of the libraries
6 #   test        run the full testsuite
7 #   install     install to default location (/usr/local)
8 #   clean       clean built binaries (not the documentation)
9 #
10 # Tuning by variables (environment or make arguments):
11 #   V=1            verbose, print command lines (default: quiet)
12 #   C=1            run checker before compilation (default checker: sparse)
13 #   W=123          build with warnings (default: off)
14 #   EXTRA_CFLAGS   additional compiler flags
15 #   EXTRA_LDFLAGS  additional linker flags
16 #
17 # Static checkers:
18 #   CHECKER        static checker binary to be called (default: sparse)
19 #   CHECKER_FLAGS  flags to pass to CHECKER, can override CFLAGS
20 #
21
22 # Export all variables to sub-makes by default
23 export
24
25 include Makefile.extrawarn
26
27 CC = @CC@
28 LN_S = @LN_S@
29 AR = @AR@
30 RM = @RM@
31 RMDIR = @RMDIR@
32 INSTALL = @INSTALL@
33 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
34 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
35
36 EXTRA_CFLAGS :=
37 EXTRA_LDFLAGS :=
38
39 # Common build flags
40 CFLAGS = @CFLAGS@ \
41          -include config.h \
42          -DBTRFS_FLAT_INCLUDES \
43          -D_XOPEN_SOURCE=700  \
44          -fno-strict-aliasing \
45          -fPIC $(EXTRAWARN_CFLAGS) $(EXTRA_CFLAGS)
46
47 LDFLAGS = @LDFLAGS@ \
48           -rdynamic $(EXTRA_LDFLAGS)
49
50 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
51 LIBBTRFS_LIBS = $(LIBS)
52
53 # Static compilation flags
54 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
55 STATIC_LDFLAGS = -static -Wl,--gc-sections
56 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
57               @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
58
59 # don't use FORTIFY with sparse because glibc with FORTIFY can
60 # generate so many sparse errors that sparse stops parsing,
61 # which masks real errors that we want to see.
62 CHECKER := sparse
63 check_defs := .cc-defines.h
64 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
65         -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
66         -U_FORTIFY_SOURCE
67
68 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
69           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
70           extent-cache.o extent_io.o volumes.o utils.o repair.o \
71           qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
72           ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
73           inode.o file.o find-root.o free-space-tree.o help.o cmds-inspect-dump-tree.o
74 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
75                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
76                cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
77                cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
78                cmds-property.o cmds-fi-usage.o
79 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
80                    uuid-tree.o utils-lib.o rbtree-utils.o
81 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
82                crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
83                extent_io.h ioctl.h ctree.h btrfsck.h version.h
84 TESTS = fsck-tests.sh convert-tests.sh
85
86 prefix ?= @prefix@
87 exec_prefix = @exec_prefix@
88 bindir = @bindir@
89 libdir ?= @libdir@
90 incdir = @includedir@/btrfs
91
92 ifeq ("$(origin V)", "command line")
93   BUILD_VERBOSE = $(V)
94 endif
95 ifndef BUILD_VERBOSE
96   BUILD_VERBOSE = 0
97 endif
98
99 ifeq ($(BUILD_VERBOSE),1)
100   Q =
101 else
102   Q = @
103 endif
104
105 MAKEOPTS = --no-print-directory Q=$(Q)
106
107 # build all by default
108 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
109
110 # install only selected
111 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
112         btrfs-map-logical btrfs-image btrfs-zero-log \
113         btrfs-find-root btrfstune btrfs-show-super \
114         btrfs-select-super
115
116 progs_extra = btrfs-fragments
117
118 progs_static = $(foreach p,$(progs),$(p).static)
119
120 ifneq ($(DISABLE_BTRFSCONVERT),1)
121 progs_install += btrfs-convert
122 endif
123
124 # external libs required by various binaries; for btrfs-foo,
125 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
126 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
127 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
128
129 SUBDIRS =
130 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
131 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
132 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
133
134 ifneq ($(DISABLE_DOCUMENTATION),1)
135 BUILDDIRS += build-Documentation
136 INSTALLDIRS += install-Documentation
137 endif
138
139 .PHONY: $(SUBDIRS)
140 .PHONY: $(BUILDDIRS)
141 .PHONY: $(INSTALLDIRS)
142 .PHONY: $(TESTDIRS)
143 .PHONY: $(CLEANDIRS)
144 .PHONY: all install clean
145
146 # Create all the static targets
147 static_objects = $(patsubst %.o, %.static.o, $(objects))
148 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
149 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
150
151 libs_shared = libbtrfs.so.0.1
152 libs_static = libbtrfs.a
153 libs = $(libs_shared) $(libs_static)
154 lib_links = libbtrfs.so.0 libbtrfs.so
155 headers = $(libbtrfs_headers)
156
157 # make C=1 to enable sparse
158 ifdef C
159         # We're trying to use sparse against glibc headers which go wild
160         # trying to use internal compiler macros to test features.  We
161         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
162         # 'cause sparse defines that one.
163         #
164         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
165                         grep -v __SIZE_TYPE__ > $(check_defs))
166         check = $(CHECKER)
167         check_echo = echo
168 else
169         check = true
170         check_echo = true
171 endif
172
173 %.o.d: %.c
174         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
175
176 .c.o:
177         @$(check_echo) "    [SP]     $<"
178         $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
179         @echo "    [CC]     $@"
180         $(Q)$(CC) $(CFLAGS) -c $<
181
182 %.static.o: %.c
183         @echo "    [CC]     $@"
184         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
185
186 all: $(progs) $(BUILDDIRS)
187 $(SUBDIRS): $(BUILDDIRS)
188 $(BUILDDIRS):
189         @echo "Making all in $(patsubst build-%,%,$@)"
190         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
191
192 test-convert: btrfs btrfs-convert
193         @echo "    [TEST]   convert-tests.sh"
194         $(Q)bash tests/convert-tests.sh
195
196 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
197         @echo "    [TEST]   fsck-tests.sh"
198         $(Q)bash tests/fsck-tests.sh
199
200 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
201         @echo "    [TEST]   misc-tests.sh"
202         $(Q)bash tests/misc-tests.sh
203
204 test-mkfs: btrfs mkfs.btrfs
205         @echo "    [TEST]   mkfs-tests.sh"
206         $(Q)bash tests/mkfs-tests.sh
207
208 test-fuzz: btrfs
209         @echo "    [TEST]   fuzz-tests.sh"
210         $(Q)bash tests/fuzz-tests.sh
211
212 test-clean:
213         @echo "Cleaning tests"
214         $(Q)bash tests/clean-tests.sh
215
216 test: test-fsck test-mkfs test-convert test-misc test-fuzz
217
218 #
219 # NOTE: For static compiles, you need to have all the required libs
220 #       static equivalent available
221 #
222 static: $(progs_static)
223
224 version.h: version.sh version.h.in configure.ac
225         @echo "    [SH]     $@"
226         $(Q)bash ./config.status --silent $@
227
228 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
229         @echo "    [LD]     $@"
230         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
231                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
232
233 $(libs_static): $(libbtrfs_objects)
234         @echo "    [AR]     $@"
235         $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
236
237 $(lib_links):
238         @echo "    [LN]     $@"
239         $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
240
241 # keep intermediate files from the below implicit rules around
242 .PRECIOUS: $(addsuffix .o,$(progs))
243
244 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
245 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
246 # turns them into a list of libraries to link against if they exist
247 #
248 # For static variants, use an extra $(subst) to get rid of the ".static"
249 # from the target name before translating to list of libs
250
251 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
252         @echo "    [LD]     $@"
253         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
254                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
255                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
256
257 btrfs-%: $(objects) $(libs_static) btrfs-%.o
258         @echo "    [LD]     $@"
259         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs_static) \
260                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
261
262 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
263         @echo "    [LD]     $@"
264         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
265                 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
266
267 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
268         @echo "    [LD]     $@"
269         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
270                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
271
272 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
273 btrfsck: btrfs
274         @echo "    [LN]     $@"
275         $(Q)$(LN_S) -f btrfs btrfsck
276
277 btrfsck.static: btrfs.static
278         @echo "    [LN]     $@"
279         $(Q)$(LN_S) -f $^ $@
280
281 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
282         @echo "    [LD]     $@"
283         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
284
285 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
286         @echo "    [LD]     $@"
287         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
288                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
289
290 btrfstune: $(objects) $(libs_static) btrfstune.o
291         @echo "    [LD]     $@"
292         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
293
294 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
295         @echo "    [LD]     $@"
296         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
297                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
298
299 dir-test: $(objects) $(libs) dir-test.o
300         @echo "    [LD]     $@"
301         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
302
303 quick-test: $(objects) $(libs) quick-test.o
304         @echo "    [LD]     $@"
305         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
306
307 ioctl-test: $(objects) $(libs) ioctl-test.o
308         @echo "    [LD]     $@"
309         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
310
311 send-test: $(objects) $(libs) send-test.o
312         @echo "    [LD]     $@"
313         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
314
315 library-test: $(libs_shared) library-test.o
316         @echo "    [LD]     $@"
317         $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
318
319 library-test.static: $(libs_static) library-test.o
320         @echo "    [LD]     $@"
321         $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
322
323 test-build: test-build-pre test-build-real
324
325 test-build-pre:
326         $(MAKE) clean-all
327         ./autogen.sh
328         ./configure
329
330 test-build-real:
331         $(MAKE) library-test
332         -$(MAKE) library-test.static
333         $(MAKE) -j 8 all
334         -$(MAKE) -j 8 static
335         $(MAKE) -j 8 $(progs_extra)
336
337 manpages:
338         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
339
340
341 clean-all: clean clean-doc clean-gen
342
343 clean: $(CLEANDIRS)
344         @echo "Cleaning"
345         $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
346               dir-test ioctl-test quick-test send-test library-test library-test-static \
347               btrfs.static mkfs.btrfs.static \
348               $(check_defs) \
349               $(libs) $(lib_links) \
350               $(progs_static) $(progs_extra)
351
352 clean-doc:
353         @echo "Cleaning Documentation"
354         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
355
356 clean-gen:
357         @echo "Cleaning Generated Files"
358         $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
359                 configure.lineno config.status.lineno Makefile \
360                 Documentation/Makefile \
361                 config.log config.h config.h.in~ aclocal.m4 \
362                 configure autom4te.cache/ config/
363
364 $(CLEANDIRS):
365         @echo "Cleaning $(patsubst clean-%,%,$@)"
366         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
367
368 install: $(libs) $(progs_install) $(INSTALLDIRS)
369         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
370         $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
371         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
372         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
373         $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
374         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
375         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
376         cp -a $(lib_links) $(DESTDIR)$(libdir)
377         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
378         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
379
380 install-static: $(progs_static) $(INSTALLDIRS)
381         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
382         $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
383         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
384         $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
385
386 $(INSTALLDIRS):
387         @echo "Making install in $(patsubst install-%,%,$@)"
388         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
389
390 uninstall:
391         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
392         cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
393         $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
394         cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
395         cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
396
397 ifneq ($(MAKECMDGOALS),clean)
398 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
399 endif