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
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 # bcheck - extended build checks
23 # W=123 build with warnings (default: off)
24 # DEBUG_CFLAGS additional compiler flags for debugging build
25 # EXTRA_CFLAGS additional compiler flags
26 # EXTRA_LDFLAGS additional linker flags
29 # CHECKER static checker binary to be called (default: sparse)
30 # CHECKER_FLAGS flags to pass to CHECKER, can override CFLAGS
33 # Export all variables to sub-makes by default
36 include Makefile.extrawarn
44 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
45 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
46 BTRFSCONVERT_EXT2 = @BTRFSCONVERT_EXT2@
51 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
52 DEBUG_CFLAGS_INTERNAL =
55 TOPDIR := $(shell pwd)
60 -DBTRFS_FLAT_INCLUDES \
62 -fno-strict-aliasing \
65 -I$(TOPDIR)/kernel-lib \
67 $(DEBUG_CFLAGS_INTERNAL) \
71 -rdynamic $(EXTRA_LDFLAGS)
73 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
74 LIBBTRFS_LIBS = $(LIBS)
76 # Static compilation flags
77 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
78 STATIC_LDFLAGS = -static -Wl,--gc-sections
79 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
80 @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
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
97 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
98 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
99 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
100 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
101 cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
102 cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o
103 libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
104 kernel-lib/crc32c.o \
105 uuid-tree.o utils-lib.o rbtree-utils.o
106 libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
107 kernel-lib/crc32c.h kernel-lib/list.h kerncompat.h \
108 kernel-lib/radix-tree.h extent-cache.h \
109 extent_io.h ioctl.h ctree.h btrfsck.h version.h
110 TESTS = fsck-tests.sh convert-tests.sh
112 udev_rules = 64-btrfs-dm.rules
115 exec_prefix = @exec_prefix@
118 incdir = @includedir@/btrfs
120 udevruledir = ${udevdir}/rules.d
122 ifeq ("$(origin V)", "command line")
129 ifeq ($(BUILD_VERBOSE),1)
135 ifeq ("$(origin D)", "command line")
136 DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
139 ifneq (,$(findstring verbose,$(D)))
140 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
143 ifneq (,$(findstring trace,$(D)))
144 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
147 ifneq (,$(findstring abort,$(D)))
148 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
151 ifneq (,$(findstring all,$(D)))
152 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
153 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
154 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
157 ifneq (,$(findstring asan,$(D)))
158 DEBUG_CFLAGS_INTERNAL += -fsanitize=address
161 ifneq (,$(findstring ubsan,$(D)))
162 DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
165 ifneq (,$(findstring bcheck,$(D)))
166 DEBUG_CFLAGS_INTERNAL += -DDEBUG_BUILD_CHECKS
169 MAKEOPTS = --no-print-directory Q=$(Q)
171 # build all by default
172 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
174 # install only selected
175 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
176 btrfs-map-logical btrfs-image btrfs-zero-log \
177 btrfs-find-root btrfstune btrfs-show-super \
180 progs_extra = btrfs-fragments
182 progs_static = $(foreach p,$(progs),$(p).static)
184 ifneq ($(DISABLE_BTRFSCONVERT),1)
185 progs_install += btrfs-convert
188 # external libs required by various binaries; for btrfs-foo,
189 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
190 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
191 btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2)
192 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
193 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
194 btrfs_show_super_objects = cmds-inspect-dump-super.o
195 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
197 # collect values of the variables above
198 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
201 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
202 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
203 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
205 ifneq ($(DISABLE_DOCUMENTATION),1)
206 BUILDDIRS += build-Documentation
207 INSTALLDIRS += install-Documentation
212 .PHONY: $(INSTALLDIRS)
215 .PHONY: all install clean
217 # Create all the static targets
218 static_objects = $(patsubst %.o, %.static.o, $(objects))
219 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
220 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_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))
258 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags))
260 all: $(progs) $(BUILDDIRS)
261 $(SUBDIRS): $(BUILDDIRS)
263 @echo "Making all in $(patsubst build-%,%,$@)"
264 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
266 test-convert: btrfs btrfs-convert
267 @echo " [TEST] convert-tests.sh"
268 $(Q)bash tests/convert-tests.sh
270 test-check: test-fsck
271 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
272 @echo " [TEST] fsck-tests.sh"
273 $(Q)bash tests/fsck-tests.sh
275 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
276 @echo " [TEST] misc-tests.sh"
277 $(Q)bash tests/misc-tests.sh
279 test-mkfs: btrfs mkfs.btrfs
280 @echo " [TEST] mkfs-tests.sh"
281 $(Q)bash tests/mkfs-tests.sh
284 @echo " [TEST] fuzz-tests.sh"
285 $(Q)bash tests/fuzz-tests.sh
288 @echo " [TEST] cli-tests.sh"
289 $(Q)bash tests/cli-tests.sh
292 @echo "Cleaning tests"
293 $(Q)bash tests/clean-tests.sh
296 @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
297 echo "Test installation to $$tmpdest" && \
298 $(MAKE) DESTDIR=$$tmpdest install && \
299 $(RM) -rf -- $$tmpdest
301 test: test-fsck test-mkfs test-convert test-misc test-fuzz
304 # NOTE: For static compiles, you need to have all the required libs
305 # static equivalent available
307 static: $(progs_static)
309 version.h: version.sh version.h.in configure.ac
311 $(Q)bash ./config.status --silent $@
313 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
315 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
316 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
318 $(libs_static): $(libbtrfs_objects)
320 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
324 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
326 # keep intermediate files from the below implicit rules around
327 .PRECIOUS: $(addsuffix .o,$(progs))
329 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
330 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
331 # turns them into a list of libraries to link against if they exist
333 # For static variants, use an extra $(subst) to get rid of the ".static"
334 # from the target name before translating to list of libs
336 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects) $(patsubst %.o,%.static.o,$(standalone_deps))
338 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
339 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
340 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
341 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
343 btrfs-%: $(objects) $(libs_static) btrfs-%.o $(standalone_deps)
345 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
346 $($(subst -,_,$@-objects)) \
348 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
350 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
352 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
353 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
355 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
357 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
358 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
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: $(objects) $(libs_static) mkfs.o
371 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
373 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
375 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
376 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
378 btrfstune: $(objects) $(libs_static) btrfstune.o
380 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
382 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
384 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
385 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
387 dir-test: $(objects) $(libs) dir-test.o
389 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
391 quick-test: $(objects) $(libs) quick-test.o
393 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
395 ioctl-test.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
397 $(Q)$(CC) $(CFLAGS) -c $< -o $@
399 ioctl-test-32.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
401 $(Q)$(CC) $(CFLAGS) -m32 -c $< -o $@
403 ioctl-test-64.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
405 $(Q)$(CC) $(CFLAGS) -m64 -c $< -o $@
407 ioctl-test: ioctl-test.o
409 $(Q)$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
410 @echo " ?[PAHOLE] $@.pahole"
411 -$(Q)pahole $@ > $@.pahole
413 ioctl-test-32: ioctl-test-32.o
415 $(Q)$(CC) $(CFLAGS) -m32 -o $@ $< $(LDFLAGS)
416 @echo " ?[PAHOLE] $@.pahole"
417 -$(Q)pahole $@ > $@.pahole
419 ioctl-test-64: ioctl-test-64.o
421 $(Q)$(CC) $(CFLAGS) -m64 -o $@ $< $(LDFLAGS)
422 @echo " ?[PAHOLE] $@.pahole"
423 -$(Q)pahole $@ > $@.pahole
425 test-ioctl: ioctl-test ioctl-test-32 ioctl-test-64
426 @echo " [TEST/ioctl]"
427 $(Q)./ioctl-test > ioctl-test.log
428 $(Q)./ioctl-test-32 > ioctl-test-32.log
429 $(Q)./ioctl-test-64 > ioctl-test-64.log
431 library-test: $(libs_shared) library-test.o
433 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
435 library-test.static: $(libs_static) library-test.o
437 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
439 test-build: test-build-pre test-build-real
448 -$(MAKE) library-test.static
451 $(MAKE) -j 8 $(progs_extra)
454 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
457 clean-all: clean clean-doc clean-gen
461 $(Q)$(RM) -f -- $(progs) cscope.out *.o *.o.d \
462 kernel-lib/*.o kernel-lib/*.o.d \
463 dir-test ioctl-test quick-test library-test library-test-static \
464 btrfs.static mkfs.btrfs.static \
466 $(libs) $(lib_links) \
467 $(progs_static) $(progs_extra)
470 @echo "Cleaning Documentation"
471 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
474 @echo "Cleaning Generated Files"
475 $(Q)$(RM) -rf -- version.h config.status config.cache connfig.log \
476 configure.lineno config.status.lineno Makefile \
477 Documentation/Makefile \
478 config.log config.h config.h.in~ aclocal.m4 \
479 configure autom4te.cache/ config/
482 @echo "Cleaning $(patsubst clean-%,%,$@)"
483 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
485 install: $(libs) $(progs_install) $(INSTALLDIRS)
486 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
487 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
488 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
489 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
490 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
491 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
492 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
493 cp -a $(lib_links) $(DESTDIR)$(libdir)
494 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
495 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
497 $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
498 $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
501 install-static: $(progs_static) $(INSTALLDIRS)
502 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
503 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
504 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
505 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
508 @echo "Making install in $(patsubst install-%,%,$@)"
509 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
512 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
513 cd $(DESTDIR)$(incdir); $(RM) -f -- $(headers)
514 $(RMDIR) -p --ignore-fail-on-non-empty -- $(DESTDIR)$(incdir)
515 cd $(DESTDIR)$(libdir); $(RM) -f -- $(lib_links) $(libs)
516 cd $(DESTDIR)$(bindir); $(RM) -f -- btrfsck fsck.btrfs $(progs_install)
518 ifneq ($(MAKECMDGOALS),clean)
519 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))