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-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
266 @echo " [TEST] fsck-tests.sh"
267 $(Q)bash tests/fsck-tests.sh
269 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
270 @echo " [TEST] misc-tests.sh"
271 $(Q)bash tests/misc-tests.sh
273 test-mkfs: btrfs mkfs.btrfs
274 @echo " [TEST] mkfs-tests.sh"
275 $(Q)bash tests/mkfs-tests.sh
278 @echo " [TEST] fuzz-tests.sh"
279 $(Q)bash tests/fuzz-tests.sh
282 @echo " [TEST] cli-tests.sh"
283 $(Q)bash tests/cli-tests.sh
286 @echo "Cleaning tests"
287 $(Q)bash tests/clean-tests.sh
290 @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
291 echo "Test installation to $$tmpdest" && \
292 $(MAKE) DESTDIR=$$tmpdest install && \
293 $(RM) -rf -- $$tmpdest
295 test: test-fsck test-mkfs test-convert test-misc test-fuzz
298 # NOTE: For static compiles, you need to have all the required libs
299 # static equivalent available
301 static: $(progs_static)
303 version.h: version.sh version.h.in configure.ac
305 $(Q)bash ./config.status --silent $@
307 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
309 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
310 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
312 $(libs_static): $(libbtrfs_objects)
314 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
318 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
320 # keep intermediate files from the below implicit rules around
321 .PRECIOUS: $(addsuffix .o,$(progs))
323 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
324 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
325 # turns them into a list of libraries to link against if they exist
327 # For static variants, use an extra $(subst) to get rid of the ".static"
328 # from the target name before translating to list of libs
330 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects) $(patsubst %.o,%.static.o,$(standalone_deps))
332 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
333 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
334 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
335 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
337 btrfs-%: $(objects) $(libs_static) btrfs-%.o $(standalone_deps)
339 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
340 $($(subst -,_,$@-objects)) \
342 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
344 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
346 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
347 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
349 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
351 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
352 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
354 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
357 $(Q)$(LN_S) -f btrfs btrfsck
359 btrfsck.static: btrfs.static
363 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
365 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
367 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
369 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
370 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
372 btrfstune: $(objects) $(libs_static) btrfstune.o
374 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
376 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
378 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
379 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
381 dir-test: $(objects) $(libs) dir-test.o
383 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
385 quick-test: $(objects) $(libs) quick-test.o
387 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
389 ioctl-test: $(objects) $(libs) ioctl-test.o
391 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
393 send-test: $(objects) $(libs) send-test.o
395 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
397 library-test: $(libs_shared) library-test.o
399 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
401 library-test.static: $(libs_static) library-test.o
403 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
405 test-build: test-build-pre test-build-real
414 -$(MAKE) library-test.static
417 $(MAKE) -j 8 $(progs_extra)
420 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
423 clean-all: clean clean-doc clean-gen
427 $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
428 kernel-lib/*.o kernel-lib/*.o.d \
429 dir-test ioctl-test quick-test send-test library-test library-test-static \
430 btrfs.static mkfs.btrfs.static \
432 $(libs) $(lib_links) \
433 $(progs_static) $(progs_extra)
436 @echo "Cleaning Documentation"
437 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
440 @echo "Cleaning Generated Files"
441 $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
442 configure.lineno config.status.lineno Makefile \
443 Documentation/Makefile \
444 config.log config.h config.h.in~ aclocal.m4 \
445 configure autom4te.cache/ config/
448 @echo "Cleaning $(patsubst clean-%,%,$@)"
449 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
451 install: $(libs) $(progs_install) $(INSTALLDIRS)
452 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
453 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
454 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
455 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
456 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
457 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
458 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
459 cp -a $(lib_links) $(DESTDIR)$(libdir)
460 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
461 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
463 $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
464 $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
467 install-static: $(progs_static) $(INSTALLDIRS)
468 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
469 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
470 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
471 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
474 @echo "Making install in $(patsubst install-%,%,$@)"
475 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
478 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
479 cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
480 $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
481 cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
482 cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
484 ifneq ($(MAKECMDGOALS),clean)
485 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))