3 # all all main tools and the shared library
4 # static build static bnaries, requires static version of the libraries
5 # test run the full testsuite
6 # install install to default location (/usr/local)
7 # clean clean built binaries (not the documentation)
8 # clean-all clean as above, clean docs and generated files
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 # D=1 debugging build, turn off optimizations
14 # D=dflags dtto, turn on additional debugging features:
15 # verbose - print file:line along with error/warning messages
16 # trace - print trace before the error/warning messages
17 # abort - call abort() on first error (dumps core)
18 # all - shortcut for all of the above
19 # asan - enable address sanitizer compiler feature
20 # ubsan - undefined behaviour sanitizer compiler feature
21 # bcheck - extended build checks
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
27 # Testing-specific options (see also tests/README.md):
28 # TEST=GLOB run test(s) from directories matching GLOB
29 # TEST_LOG=tty print name of a command run via the execution helpers
30 # TEST_LOG=dump dump testing log file when a test fails
33 # CHECKER static checker binary to be called (default: sparse)
34 # CHECKER_FLAGS flags to pass to CHECKER, can override CFLAGS
37 # Export all variables to sub-makes by default
41 ifneq ($(MAKEFILE_INC_INCLUDED),yes)
42 $(error Makefile.inc not generated, please configure first)
46 CSCOPE_CMD := cscope -u -b -c -q
48 include Makefile.extrawarn
53 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
54 DEBUG_CFLAGS_INTERNAL =
57 TOPDIR := $(shell pwd)
60 CFLAGS = $(SUBST_CFLAGS) \
62 -DBTRFS_FLAT_INCLUDES \
64 -fno-strict-aliasing \
67 -I$(TOPDIR)/kernel-lib \
69 $(DEBUG_CFLAGS_INTERNAL) \
72 LDFLAGS = $(SUBST_LDFLAGS) \
73 -rdynamic -L$(TOPDIR) $(EXTRA_LDFLAGS)
76 LIBBTRFS_LIBS = $(LIBS_BASE)
78 # Static compilation flags
79 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
80 STATIC_LDFLAGS = -static -Wl,--gc-sections
81 STATIC_LIBS = $(STATIC_LIBS_BASE)
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.
87 check_defs := .cc-defines.h
88 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
89 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
92 objects = ctree.o disk-io.o kernel-lib/radix-tree.o extent-tree.o print-tree.o \
93 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
94 extent-cache.o extent_io.o volumes.o utils.o repair.o \
95 qgroup.o raid56.o free-space-cache.o kernel-lib/list_sort.o props.o \
96 ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
97 inode.o file.o find-root.o free-space-tree.o help.o send-dump.o \
99 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
100 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
101 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
102 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
103 cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
104 cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o
105 libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
106 kernel-lib/crc32c.o \
107 uuid-tree.o utils-lib.o rbtree-utils.o
108 libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
109 kernel-lib/crc32c.h kernel-lib/list.h kerncompat.h \
110 kernel-lib/radix-tree.h extent-cache.h \
111 extent_io.h ioctl.h ctree.h btrfsck.h version.h
112 convert_objects = convert/main.o convert/common.o convert/source-fs.o \
113 convert/source-ext2.o
114 mkfs_objects = mkfs/main.o mkfs/common.o
116 TESTS = fsck-tests.sh convert-tests.sh
118 udev_rules = 64-btrfs-dm.rules
120 ifeq ("$(origin V)", "command line")
127 ifeq ($(BUILD_VERBOSE),1)
133 ifeq ("$(origin D)", "command line")
134 DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
137 ifneq (,$(findstring verbose,$(D)))
138 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
141 ifneq (,$(findstring trace,$(D)))
142 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
145 ifneq (,$(findstring abort,$(D)))
146 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
149 ifneq (,$(findstring all,$(D)))
150 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
151 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
152 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
155 ifneq (,$(findstring asan,$(D)))
156 DEBUG_CFLAGS_INTERNAL += -fsanitize=address
159 ifneq (,$(findstring ubsan,$(D)))
160 DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
163 ifneq (,$(findstring bcheck,$(D)))
164 DEBUG_CFLAGS_INTERNAL += -DDEBUG_BUILD_CHECKS
167 MAKEOPTS = --no-print-directory Q=$(Q)
169 # build all by default
170 progs = $(progs_install) btrfsck btrfs-corrupt-block
172 # install only selected
173 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
174 btrfs-map-logical btrfs-image btrfs-zero-log \
175 btrfs-find-root btrfstune \
178 # other tools, not built by default
179 progs_extra = btrfs-fragments btrfs-calc-size btrfs-show-super
181 progs_static = $(foreach p,$(progs),$(p).static)
183 ifneq ($(DISABLE_BTRFSCONVERT),1)
184 progs_install += btrfs-convert
187 # external libs required by various binaries; for btrfs-foo,
188 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
189 btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2)
190 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
191 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
192 btrfs_show_super_objects = cmds-inspect-dump-super.o
193 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
195 # collect values of the variables above
196 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
199 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
200 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
201 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
203 ifneq ($(DISABLE_DOCUMENTATION),1)
204 BUILDDIRS += build-Documentation
205 INSTALLDIRS += install-Documentation
210 .PHONY: $(INSTALLDIRS)
213 .PHONY: all install clean
216 # Create all the static targets
217 static_objects = $(patsubst %.o, %.static.o, $(objects))
218 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
219 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
220 static_convert_objects = $(patsubst %.o, %.static.o, $(convert_objects))
221 static_mkfs_objects = $(patsubst %.o, %.static.o, $(mkfs_objects))
223 libs_shared = libbtrfs.so.0.1
224 libs_static = libbtrfs.a
225 libs = $(libs_shared) $(libs_static)
226 lib_links = libbtrfs.so.0 libbtrfs.so
227 headers = $(libbtrfs_headers)
229 # make C=1 to enable sparse
231 # We're trying to use sparse against glibc headers which go wild
232 # trying to use internal compiler macros to test features. We
233 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
234 # 'cause sparse defines that one.
236 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
237 grep -v __SIZE_TYPE__ > $(check_defs))
246 $(Q)$(CC) -MD -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
249 # Pick from per-file variables, btrfs_*_cflags
252 @$(check_echo) " [SP] $<"
253 $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
255 $(Q)$(CC) $(CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.o=%)-cflags)) \
256 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
260 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags)) \
261 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
263 all: $(progs) libbtrfs $(BUILDDIRS)
264 $(SUBDIRS): $(BUILDDIRS)
266 @echo "Making all in $(patsubst build-%,%,$@)"
267 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
269 test-convert: btrfs btrfs-convert
270 @echo " [TEST] convert-tests.sh"
271 $(Q)bash tests/convert-tests.sh
273 test-check: test-fsck
274 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
275 @echo " [TEST] fsck-tests.sh"
276 $(Q)bash tests/fsck-tests.sh
278 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
279 @echo " [TEST] misc-tests.sh"
280 $(Q)bash tests/misc-tests.sh
282 test-mkfs: btrfs mkfs.btrfs
283 @echo " [TEST] mkfs-tests.sh"
284 $(Q)bash tests/mkfs-tests.sh
287 @echo " [TEST] fuzz-tests.sh"
288 $(Q)bash tests/fuzz-tests.sh
291 @echo " [TEST] cli-tests.sh"
292 $(Q)bash tests/cli-tests.sh
295 @echo "Cleaning tests"
296 $(Q)bash tests/clean-tests.sh
299 @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
300 echo "Test installation to $$tmpdest" && \
301 $(MAKE) DESTDIR=$$tmpdest install && \
302 $(RM) -rf -- $$tmpdest
304 test: test-fsck test-mkfs test-convert test-misc test-fuzz test-cli
307 # NOTE: For static compiles, you need to have all the required libs
308 # static equivalent available
310 static: $(progs_static)
312 version.h: version.sh version.h.in configure.ac
314 $(Q)bash ./config.status --silent $@
316 libbtrfs: $(libs_shared) $(lib_links)
318 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
320 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
321 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
323 $(libs_static): $(libbtrfs_objects)
325 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
329 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
331 # keep intermediate files from the below implicit rules around
332 .PRECIOUS: $(addsuffix .o,$(progs))
334 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
335 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
336 # turns them into a list of libraries to link against if they exist
338 # For static variants, use an extra $(subst) to get rid of the ".static"
339 # from the target name before translating to list of libs
341 btrfs-%.static: btrfs-%.static.o $(static_objects) $(patsubst %.o,%.static.o,$(standalone_deps)) $(static_libbtrfs_objects)
343 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
344 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
345 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
346 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
348 btrfs-%: btrfs-%.o $(objects) $(standalone_deps) $(libs_static)
350 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
351 $($(subst -,_,$@-objects)) \
353 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
355 btrfs: btrfs.o $(objects) $(cmds_objects) $(libs_static)
357 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
359 btrfs.static: btrfs.static.o $(static_objects) $(static_cmds_objects) $(static_libbtrfs_objects)
361 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
363 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
366 $(Q)$(LN_S) -f btrfs btrfsck
368 btrfsck.static: btrfs.static
372 mkfs.btrfs: $(mkfs_objects) $(objects) $(libs_static)
374 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
376 mkfs.btrfs.static: $(static_mkfs_objects) $(static_objects) $(static_libbtrfs_objects)
378 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
380 btrfstune: btrfstune.o $(objects) $(libs_static)
382 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
384 btrfstune.static: btrfstune.static.o $(static_objects) $(static_libbtrfs_objects)
386 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
388 btrfs-image: image/main.o $(objects) $(libs_static)
390 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
392 btrfs-image.static: image/main.static.o $(static_objects) $(static_libbtrfs_objects)
394 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
396 btrfs-convert: $(convert_objects) $(objects) $(libs_static)
398 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(btrfs_convert_libs) $(LIBS)
400 btrfs-convert.static: $(static_convert_objects) $(static_objects) $(static_libbtrfs_objects)
402 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(btrfs_convert_libs) $(STATIC_LIBS)
404 dir-test: dir-test.o $(objects) $(libs)
406 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
408 quick-test: quick-test.o $(objects) $(libs)
410 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
412 ioctl-test.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
414 $(Q)$(CC) $(CFLAGS) -c $< -o $@
416 ioctl-test-32.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
418 $(Q)$(CC) $(CFLAGS) -m32 -c $< -o $@
420 ioctl-test-64.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
422 $(Q)$(CC) $(CFLAGS) -m64 -c $< -o $@
424 ioctl-test: ioctl-test.o
426 $(Q)$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
427 @echo " ?[PAHOLE] $@.pahole"
428 -$(Q)pahole $@ > $@.pahole
430 ioctl-test-32: ioctl-test-32.o
432 $(Q)$(CC) $(CFLAGS) -m32 -o $@ $< $(LDFLAGS)
433 @echo " ?[PAHOLE] $@.pahole"
434 -$(Q)pahole $@ > $@.pahole
436 ioctl-test-64: ioctl-test-64.o
438 $(Q)$(CC) $(CFLAGS) -m64 -o $@ $< $(LDFLAGS)
439 @echo " ?[PAHOLE] $@.pahole"
440 -$(Q)pahole $@ > $@.pahole
442 test-ioctl: ioctl-test ioctl-test-32 ioctl-test-64
443 @echo " [TEST/ioctl]"
444 $(Q)./ioctl-test > ioctl-test.log
445 $(Q)./ioctl-test-32 > ioctl-test-32.log
446 $(Q)./ioctl-test-64 > ioctl-test-64.log
448 library-test: library-test.o $(libs_shared)
450 $(Q)$(CC) $(CFLAGS) -o $@ library-test.o $(LDFLAGS) -Wl,-rpath=$(TOPDIR) -lbtrfs
454 library-test.static: library-test.static.o $(libs_static)
456 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ library-test.static.o $(STATIC_LDFLAGS) $(libs_static) $(STATIC_LIBS)
460 test-build: test-build-pre test-build-real
469 -$(MAKE) library-test.static
472 $(MAKE) -j 8 $(progs_extra)
475 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
478 @echo " [TAGS] $(TAGS_CMD)"
479 $(Q)$(TAGS_CMD) *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch]
482 @echo " [CSCOPE] $(CSCOPE_CMD)"
483 $(Q)ls -1 *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch] > cscope.files
486 clean-all: clean clean-doc clean-gen
490 $(Q)$(RM) -f -- $(progs) *.o *.o.d \
491 kernel-lib/*.o kernel-lib/*.o.d \
492 image/*.o image/*.o.d \
493 convert/*.o convert/*.o.d \
494 mkfs/*.o mkfs/*.o.d \
495 dir-test ioctl-test quick-test library-test library-test-static \
496 btrfs.static mkfs.btrfs.static \
498 $(libs) $(lib_links) \
499 $(progs_static) $(progs_extra)
502 @echo "Cleaning Documentation"
503 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
506 @echo "Cleaning Generated Files"
507 $(Q)$(RM) -rf -- version.h config.status config.cache connfig.log \
508 configure.lineno config.status.lineno Makefile.inc \
509 Documentation/Makefile tags \
510 cscope.files cscope.out cscope.in.out cscope.po.out \
511 config.log config.h config.h.in~ aclocal.m4 \
512 configure autom4te.cache/ config/
515 @echo "Cleaning $(patsubst clean-%,%,$@)"
516 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
518 install: $(libs) $(progs_install) $(INSTALLDIRS)
519 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
520 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
521 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
522 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
523 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
524 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
525 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
526 cp -a $(lib_links) $(DESTDIR)$(libdir)
527 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
528 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
530 $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
531 $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
534 install-static: $(progs_static) $(INSTALLDIRS)
535 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
536 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
537 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
538 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
541 @echo "Making install in $(patsubst install-%,%,$@)"
542 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
545 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
546 cd $(DESTDIR)$(incdir); $(RM) -f -- $(headers)
547 $(RMDIR) -p --ignore-fail-on-non-empty -- $(DESTDIR)$(incdir)
548 cd $(DESTDIR)$(libdir); $(RM) -f -- $(lib_links) $(libs)
549 cd $(DESTDIR)$(bindir); $(RM) -f -- btrfsck fsck.btrfs $(progs_install)
551 ifneq ($(MAKECMDGOALS),clean)
552 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))