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 # 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
28 # CHECKER static checker binary to be called (default: sparse)
29 # CHECKER_FLAGS flags to pass to CHECKER, can override CFLAGS
32 # Export all variables to sub-makes by default
35 include Makefile.extrawarn
43 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
44 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
45 BTRFSCONVERT_EXT2 = @BTRFSCONVERT_EXT2@
50 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
51 DEBUG_CFLAGS_INTERNAL =
54 TOPDIR := $(shell pwd)
59 -DBTRFS_FLAT_INCLUDES \
61 -fno-strict-aliasing \
64 -I$(TOPDIR)/kernel-lib \
66 $(DEBUG_CFLAGS_INTERNAL) \
70 -rdynamic $(EXTRA_LDFLAGS)
72 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
73 LIBBTRFS_LIBS = $(LIBS)
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
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.
85 check_defs := .cc-defines.h
86 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
87 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
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
111 udev_rules = 64-btrfs-dm.rules
114 exec_prefix = @exec_prefix@
117 incdir = @includedir@/btrfs
119 udevruledir = ${udevdir}/rules.d
121 ifeq ("$(origin V)", "command line")
128 ifeq ($(BUILD_VERBOSE),1)
134 ifeq ("$(origin D)", "command line")
135 DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
138 ifneq (,$(findstring verbose,$(D)))
139 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
142 ifneq (,$(findstring trace,$(D)))
143 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
146 ifneq (,$(findstring abort,$(D)))
147 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
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
156 ifneq (,$(findstring asan,$(D)))
157 DEBUG_CFLAGS_INTERNAL += -fsanitize=address
160 ifneq (,$(findstring ubsan,$(D)))
161 DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
164 MAKEOPTS = --no-print-directory Q=$(Q)
166 # build all by default
167 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
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 \
175 progs_extra = btrfs-fragments
177 progs_static = $(foreach p,$(progs),$(p).static)
179 ifneq ($(DISABLE_BTRFSCONVERT),1)
180 progs_install += btrfs-convert
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
192 # collect values of the variables above
193 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
196 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
197 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
198 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
200 ifneq ($(DISABLE_DOCUMENTATION),1)
201 BUILDDIRS += build-Documentation
202 INSTALLDIRS += install-Documentation
207 .PHONY: $(INSTALLDIRS)
210 .PHONY: all install clean
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))
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)
223 # make C=1 to enable sparse
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.
230 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
231 grep -v __SIZE_TYPE__ > $(check_defs))
240 $(Q)$(CC) -MD -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
243 # Pick from per-file variables, btrfs_*_cflags
246 @$(check_echo) " [SP] $<"
247 $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
249 $(Q)$(CC) $(CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.o=%)-cflags))
253 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags))
255 all: $(progs) $(BUILDDIRS)
256 $(SUBDIRS): $(BUILDDIRS)
258 @echo "Making all in $(patsubst build-%,%,$@)"
259 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
261 test-convert: btrfs btrfs-convert
262 @echo " [TEST] convert-tests.sh"
263 $(Q)bash tests/convert-tests.sh
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
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
274 test-mkfs: btrfs mkfs.btrfs
275 @echo " [TEST] mkfs-tests.sh"
276 $(Q)bash tests/mkfs-tests.sh
279 @echo " [TEST] fuzz-tests.sh"
280 $(Q)bash tests/fuzz-tests.sh
283 @echo " [TEST] cli-tests.sh"
284 $(Q)bash tests/cli-tests.sh
287 @echo "Cleaning tests"
288 $(Q)bash tests/clean-tests.sh
291 @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
292 echo "Test installation to $$tmpdest" && \
293 $(MAKE) DESTDIR=$$tmpdest install && \
294 $(RM) -rf -- $$tmpdest
296 test: test-fsck test-mkfs test-convert test-misc test-fuzz
299 # NOTE: For static compiles, you need to have all the required libs
300 # static equivalent available
302 static: $(progs_static)
304 version.h: version.sh version.h.in configure.ac
306 $(Q)bash ./config.status --silent $@
308 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
310 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
311 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
313 $(libs_static): $(libbtrfs_objects)
315 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
319 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
321 # keep intermediate files from the below implicit rules around
322 .PRECIOUS: $(addsuffix .o,$(progs))
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
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
331 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects) $(patsubst %.o,%.static.o,$(standalone_deps))
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)
338 btrfs-%: $(objects) $(libs_static) btrfs-%.o $(standalone_deps)
340 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
341 $($(subst -,_,$@-objects)) \
343 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
345 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
347 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
348 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
350 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
352 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
353 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
355 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
358 $(Q)$(LN_S) -f btrfs btrfsck
360 btrfsck.static: btrfs.static
364 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
366 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
368 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
370 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
371 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
373 btrfstune: $(objects) $(libs_static) btrfstune.o
375 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
377 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
379 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
380 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
382 dir-test: $(objects) $(libs) dir-test.o
384 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
386 quick-test: $(objects) $(libs) quick-test.o
388 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
390 ioctl-test: $(objects) $(libs) ioctl-test.o
392 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
394 send-test: $(objects) $(libs) send-test.o
396 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
398 library-test: $(libs_shared) library-test.o
400 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
402 library-test.static: $(libs_static) library-test.o
404 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
406 test-build: test-build-pre test-build-real
415 -$(MAKE) library-test.static
418 $(MAKE) -j 8 $(progs_extra)
421 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
424 clean-all: clean clean-doc clean-gen
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 \
433 $(libs) $(lib_links) \
434 $(progs_static) $(progs_extra)
437 @echo "Cleaning Documentation"
438 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
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/
449 @echo "Cleaning $(patsubst clean-%,%,$@)"
450 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
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)
464 $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
465 $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
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
475 @echo "Making install in $(patsubst install-%,%,$@)"
476 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
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)
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)))