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