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)
47 include Makefile.extrawarn
52 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
53 DEBUG_CFLAGS_INTERNAL =
56 TOPDIR := $(shell pwd)
59 CFLAGS = $(SUBST_CFLAGS) \
61 -DBTRFS_FLAT_INCLUDES \
63 -fno-strict-aliasing \
66 -I$(TOPDIR)/kernel-lib \
68 $(DEBUG_CFLAGS_INTERNAL) \
71 LDFLAGS = $(SUBST_LDFLAGS) \
72 -rdynamic -L$(TOPDIR) $(EXTRA_LDFLAGS)
75 LIBBTRFS_LIBS = $(LIBS_BASE)
77 # Static compilation flags
78 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
79 STATIC_LDFLAGS = -static -Wl,--gc-sections
80 STATIC_LIBS = $(STATIC_LIBS_BASE)
82 # don't use FORTIFY with sparse because glibc with FORTIFY can
83 # generate so many sparse errors that sparse stops parsing,
84 # which masks real errors that we want to see.
86 check_defs := .cc-defines.h
87 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
88 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
91 objects = ctree.o disk-io.o kernel-lib/radix-tree.o extent-tree.o print-tree.o \
92 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
93 extent-cache.o extent_io.o volumes.o utils.o repair.o \
94 qgroup.o raid56.o free-space-cache.o kernel-lib/list_sort.o props.o \
95 ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
96 inode.o file.o find-root.o free-space-tree.o help.o send-dump.o \
98 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
99 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
100 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
101 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
102 cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
103 cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o
104 libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
105 kernel-lib/crc32c.o \
106 uuid-tree.o utils-lib.o rbtree-utils.o
107 libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
108 kernel-lib/crc32c.h kernel-lib/list.h kerncompat.h \
109 kernel-lib/radix-tree.h extent-cache.h \
110 extent_io.h ioctl.h ctree.h btrfsck.h version.h
111 convert_objects = convert/main.o convert/common.o convert/source-fs.o \
112 convert/source-ext2.o
113 mkfs_objects = mkfs/main.o mkfs/common.o
115 TESTS = fsck-tests.sh convert-tests.sh
117 udev_rules = 64-btrfs-dm.rules
119 ifeq ("$(origin V)", "command line")
126 ifeq ($(BUILD_VERBOSE),1)
132 ifeq ("$(origin D)", "command line")
133 DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
136 ifneq (,$(findstring verbose,$(D)))
137 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
140 ifneq (,$(findstring trace,$(D)))
141 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
144 ifneq (,$(findstring abort,$(D)))
145 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
148 ifneq (,$(findstring all,$(D)))
149 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
150 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
151 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
154 ifneq (,$(findstring asan,$(D)))
155 DEBUG_CFLAGS_INTERNAL += -fsanitize=address
158 ifneq (,$(findstring ubsan,$(D)))
159 DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
162 ifneq (,$(findstring bcheck,$(D)))
163 DEBUG_CFLAGS_INTERNAL += -DDEBUG_BUILD_CHECKS
166 MAKEOPTS = --no-print-directory Q=$(Q)
168 # build all by default
169 progs = $(progs_install) btrfsck btrfs-corrupt-block
171 # install only selected
172 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
173 btrfs-map-logical btrfs-image btrfs-zero-log \
174 btrfs-find-root btrfstune \
177 # other tools, not built by default
178 progs_extra = btrfs-fragments btrfs-calc-size btrfs-show-super
180 progs_static = $(foreach p,$(progs),$(p).static)
182 ifneq ($(DISABLE_BTRFSCONVERT),1)
183 progs_install += btrfs-convert
186 # external libs required by various binaries; for btrfs-foo,
187 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
188 btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2)
189 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
190 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
191 btrfs_show_super_objects = cmds-inspect-dump-super.o
192 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
194 # collect values of the variables above
195 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
198 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
199 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
200 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
202 ifneq ($(DISABLE_DOCUMENTATION),1)
203 BUILDDIRS += build-Documentation
204 INSTALLDIRS += install-Documentation
209 .PHONY: $(INSTALLDIRS)
212 .PHONY: all install clean
215 # Create all the static targets
216 static_objects = $(patsubst %.o, %.static.o, $(objects))
217 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
218 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
219 static_convert_objects = $(patsubst %.o, %.static.o, $(convert_objects))
220 static_mkfs_objects = $(patsubst %.o, %.static.o, $(mkfs_objects))
222 libs_shared = libbtrfs.so.0.1
223 libs_static = libbtrfs.a
224 libs = $(libs_shared) $(libs_static)
225 lib_links = libbtrfs.so.0 libbtrfs.so
226 headers = $(libbtrfs_headers)
228 # make C=1 to enable sparse
230 # We're trying to use sparse against glibc headers which go wild
231 # trying to use internal compiler macros to test features. We
232 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
233 # 'cause sparse defines that one.
235 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
236 grep -v __SIZE_TYPE__ > $(check_defs))
245 $(Q)$(CC) -MD -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
248 # Pick from per-file variables, btrfs_*_cflags
251 @$(check_echo) " [SP] $<"
252 $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
254 $(Q)$(CC) $(CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.o=%)-cflags)) \
255 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
259 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags)) \
260 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
262 all: $(progs) $(BUILDDIRS)
263 $(SUBDIRS): $(BUILDDIRS)
265 @echo "Making all in $(patsubst build-%,%,$@)"
266 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
268 test-convert: btrfs btrfs-convert
269 @echo " [TEST] convert-tests.sh"
270 $(Q)bash tests/convert-tests.sh
272 test-check: test-fsck
273 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
274 @echo " [TEST] fsck-tests.sh"
275 $(Q)bash tests/fsck-tests.sh
277 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
278 @echo " [TEST] misc-tests.sh"
279 $(Q)bash tests/misc-tests.sh
281 test-mkfs: btrfs mkfs.btrfs
282 @echo " [TEST] mkfs-tests.sh"
283 $(Q)bash tests/mkfs-tests.sh
286 @echo " [TEST] fuzz-tests.sh"
287 $(Q)bash tests/fuzz-tests.sh
290 @echo " [TEST] cli-tests.sh"
291 $(Q)bash tests/cli-tests.sh
294 @echo "Cleaning tests"
295 $(Q)bash tests/clean-tests.sh
298 @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
299 echo "Test installation to $$tmpdest" && \
300 $(MAKE) DESTDIR=$$tmpdest install && \
301 $(RM) -rf -- $$tmpdest
303 test: test-fsck test-mkfs test-convert test-misc test-fuzz test-cli
306 # NOTE: For static compiles, you need to have all the required libs
307 # static equivalent available
309 static: $(progs_static)
311 version.h: version.sh version.h.in configure.ac
313 $(Q)bash ./config.status --silent $@
315 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
317 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
318 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
320 $(libs_static): $(libbtrfs_objects)
322 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
326 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
328 # keep intermediate files from the below implicit rules around
329 .PRECIOUS: $(addsuffix .o,$(progs))
331 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
332 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
333 # turns them into a list of libraries to link against if they exist
335 # For static variants, use an extra $(subst) to get rid of the ".static"
336 # from the target name before translating to list of libs
338 btrfs-%.static: btrfs-%.static.o $(static_objects) $(patsubst %.o,%.static.o,$(standalone_deps)) $(static_libbtrfs_objects)
340 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
341 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
342 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
343 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
345 btrfs-%: btrfs-%.o $(objects) $(standalone_deps) $(libs_static)
347 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
348 $($(subst -,_,$@-objects)) \
350 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
352 btrfs: btrfs.o $(objects) $(cmds_objects) $(libs_static)
354 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
356 btrfs.static: btrfs.static.o $(static_objects) $(static_cmds_objects) $(static_libbtrfs_objects)
358 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
360 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
363 $(Q)$(LN_S) -f btrfs btrfsck
365 btrfsck.static: btrfs.static
369 mkfs.btrfs: $(mkfs_objects) $(objects) $(libs_static)
371 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
373 mkfs.btrfs.static: $(static_mkfs_objects) $(static_objects) $(static_libbtrfs_objects)
375 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
377 btrfstune: btrfstune.o $(objects) $(libs_static)
379 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
381 btrfstune.static: btrfstune.static.o $(static_objects) $(static_libbtrfs_objects)
383 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
385 btrfs-image: image/main.o $(objects) $(libs_static)
387 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
389 btrfs-image.static: image/main.static.o $(static_objects) $(static_libbtrfs_objects)
391 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
393 btrfs-convert: $(convert_objects) $(objects) $(libs_static)
395 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(btrfs_convert_libs) $(LIBS)
397 btrfs-convert.static: $(static_convert_objects) $(static_objects) $(static_libbtrfs_objects)
399 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $^ $(STATIC_LDFLAGS) $(btrfs_convert_libs) $(STATIC_LIBS)
401 dir-test: dir-test.o $(objects) $(libs)
403 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
405 quick-test: quick-test.o $(objects) $(libs)
407 $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
409 ioctl-test.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
411 $(Q)$(CC) $(CFLAGS) -c $< -o $@
413 ioctl-test-32.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
415 $(Q)$(CC) $(CFLAGS) -m32 -c $< -o $@
417 ioctl-test-64.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
419 $(Q)$(CC) $(CFLAGS) -m64 -c $< -o $@
421 ioctl-test: ioctl-test.o
423 $(Q)$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
424 @echo " ?[PAHOLE] $@.pahole"
425 -$(Q)pahole $@ > $@.pahole
427 ioctl-test-32: ioctl-test-32.o
429 $(Q)$(CC) $(CFLAGS) -m32 -o $@ $< $(LDFLAGS)
430 @echo " ?[PAHOLE] $@.pahole"
431 -$(Q)pahole $@ > $@.pahole
433 ioctl-test-64: ioctl-test-64.o
435 $(Q)$(CC) $(CFLAGS) -m64 -o $@ $< $(LDFLAGS)
436 @echo " ?[PAHOLE] $@.pahole"
437 -$(Q)pahole $@ > $@.pahole
439 test-ioctl: ioctl-test ioctl-test-32 ioctl-test-64
440 @echo " [TEST/ioctl]"
441 $(Q)./ioctl-test > ioctl-test.log
442 $(Q)./ioctl-test-32 > ioctl-test-32.log
443 $(Q)./ioctl-test-64 > ioctl-test-64.log
445 library-test: library-test.o $(libs_shared)
447 $(Q)$(CC) $(CFLAGS) -o $@ library-test.o $(LDFLAGS) -Wl,-rpath=$(TOPDIR) -lbtrfs
451 library-test.static: library-test.static.o $(libs_static)
453 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ library-test.static.o $(STATIC_LDFLAGS) $(libs_static) $(STATIC_LIBS)
457 test-build: test-build-pre test-build-real
466 -$(MAKE) library-test.static
469 $(MAKE) -j 8 $(progs_extra)
472 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
475 @echo " [TAGS] $(TAGS_CMD)"
476 $(Q)$(TAGS_CMD) *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch]
478 clean-all: clean clean-doc clean-gen
482 $(Q)$(RM) -f -- $(progs) cscope.out *.o *.o.d \
483 kernel-lib/*.o kernel-lib/*.o.d \
484 image/*.o image/*.o.d \
485 convert/*.o convert/*.o.d \
486 mkfs/*.o mkfs/*.o.d \
487 dir-test ioctl-test quick-test library-test library-test-static \
488 btrfs.static mkfs.btrfs.static \
490 $(libs) $(lib_links) \
491 $(progs_static) $(progs_extra)
494 @echo "Cleaning Documentation"
495 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
498 @echo "Cleaning Generated Files"
499 $(Q)$(RM) -rf -- version.h config.status config.cache connfig.log \
500 configure.lineno config.status.lineno Makefile.inc \
501 Documentation/Makefile tags \
502 config.log config.h config.h.in~ aclocal.m4 \
503 configure autom4te.cache/ config/
506 @echo "Cleaning $(patsubst clean-%,%,$@)"
507 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
509 install: $(libs) $(progs_install) $(INSTALLDIRS)
510 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
511 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
512 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
513 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
514 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
515 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
516 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
517 cp -a $(lib_links) $(DESTDIR)$(libdir)
518 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
519 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
521 $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
522 $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
525 install-static: $(progs_static) $(INSTALLDIRS)
526 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
527 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
528 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
529 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
532 @echo "Making install in $(patsubst install-%,%,$@)"
533 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
536 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
537 cd $(DESTDIR)$(incdir); $(RM) -f -- $(headers)
538 $(RMDIR) -p --ignore-fail-on-non-empty -- $(DESTDIR)$(incdir)
539 cd $(DESTDIR)$(libdir); $(RM) -f -- $(lib_links) $(libs)
540 cd $(DESTDIR)$(bindir); $(RM) -f -- btrfsck fsck.btrfs $(progs_install)
542 ifneq ($(MAKECMDGOALS),clean)
543 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))