+#
+# Basic build targets:
+# all all main tools and the shared library
+# static build static bnaries, requires static version of the libraries
+# test run the full testsuite
+# install install to default location (/usr/local)
+# clean clean built binaries (not the documentation)
+# clean-all clean as above, clean docs and generated files
+#
+# Tuning by variables (environment or make arguments):
+# V=1 verbose, print command lines (default: quiet)
+# C=1 run checker before compilation (default checker: sparse)
+# D=1 debugging build, turn off optimizations
+# D=dflags dtto, turn on additional debugging features:
+# verbose - print file:line along with error/warning messages
+# trace - print trace before the error/warning messages
+# abort - call abort() on first error (dumps core)
+# all - shortcut for all of the above
+# asan - enable address sanitizer compiler feature
+# tsan - enable thread sanitizer compiler feature
+# ubsan - undefined behaviour sanitizer compiler feature
+# bcheck - extended build checks
+# W=123 build with warnings (default: off)
+# DEBUG_CFLAGS additional compiler flags for debugging build
+# EXTRA_CFLAGS additional compiler flags
+# EXTRA_LDFLAGS additional linker flags
+#
+# Testing-specific options (see also tests/README.md):
+# TEST=GLOB run test(s) from directories matching GLOB
+# TEST_LOG=tty print name of a command run via the execution helpers
+# TEST_LOG=dump dump testing log file when a test fails
+#
+# Static checkers:
+# CHECKER static checker binary to be called (default: sparse)
+# CHECKER_FLAGS flags to pass to CHECKER, can override CFLAGS
+#
+
+# Export all variables to sub-makes by default
+export
+
+-include Makefile.inc
+ifneq ($(MAKEFILE_INC_INCLUDED),yes)
+$(error Makefile.inc not generated, please configure first)
+endif
+
+TAGS_CMD := ctags
+CSCOPE_CMD := cscope -u -b -c -q
+
+include Makefile.extrawarn
+
+EXTRA_CFLAGS :=
+EXTRA_LDFLAGS :=
+
+DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
+DEBUG_CFLAGS_INTERNAL =
+DEBUG_CFLAGS :=
+
+DEBUG_LDFLAGS_DEFAULT =
+DEBUG_LDFLAGS_INTERNAL =
+DEBUG_LDFLAGS :=
+
+ABSTOPDIR = $(shell pwd)
+TOPDIR := .
+
+# Common build flags
+CSTD = -std=gnu90
+CFLAGS = $(SUBST_CFLAGS) \
+ $(CSTD) \
+ -include config.h \
+ -DBTRFS_FLAT_INCLUDES \
+ -D_XOPEN_SOURCE=700 \
+ -fno-strict-aliasing \
+ -fPIC \
+ -I$(TOPDIR) \
+ -I$(TOPDIR)/kernel-lib \
+ -I$(TOPDIR)/libbtrfsutil \
+ $(EXTRAWARN_CFLAGS) \
+ $(DEBUG_CFLAGS_INTERNAL) \
+ $(EXTRA_CFLAGS)
+
+LIBBTRFSUTIL_CFLAGS = $(SUBST_CFLAGS) \
+ $(CSTD) \
+ -D_GNU_SOURCE \
+ -fPIC \
+ -fvisibility=hidden \
+ -I$(TOPDIR)/libbtrfsutil \
+ $(EXTRAWARN_CFLAGS) \
+ $(EXTRA_CFLAGS)
+
+LDFLAGS = $(SUBST_LDFLAGS) \
+ -rdynamic -L$(TOPDIR) \
+ $(DEBUG_LDFLAGS_INTERNAL) \
+ $(EXTRA_LDFLAGS)
+
+LIBS = $(LIBS_BASE)
+LIBBTRFS_LIBS = $(LIBS_BASE)
+
+# Static compilation flags
+STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
+STATIC_LDFLAGS = -static -Wl,--gc-sections
+STATIC_LIBS = $(STATIC_LIBS_BASE)
+
+# don't use FORTIFY with sparse because glibc with FORTIFY can
+# generate so many sparse errors that sparse stops parsing,
+# which masks real errors that we want to see.
+# Note: additional flags might get added per-target later
+CHECKER := sparse
+check_defs := .cc-defines.h
+CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
+ -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
+ -U_FORTIFY_SOURCE -Wdeclaration-after-statement -Wdefault-bitfield-sign
+
+objects = ctree.o disk-io.o kernel-lib/radix-tree.o extent-tree.o print-tree.o \
+ root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
+ extent-cache.o extent_io.o volumes.o utils.o repair.o \
+ qgroup.o free-space-cache.o kernel-lib/list_sort.o props.o \
+ kernel-shared/ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
+ inode.o file.o find-root.o free-space-tree.o help.o send-dump.o \
+ fsfeatures.o kernel-lib/tables.o kernel-lib/raid56.o transaction.o
+cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
+ cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
+ cmds-quota.o cmds-qgroup.o cmds-replace.o check/main.o \
+ cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
+ cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
+ cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o \
+ mkfs/common.o check/mode-common.o check/mode-lowmem.o
+libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
+ kernel-lib/crc32c.o messages.o \
+ uuid-tree.o utils-lib.o rbtree-utils.o
+libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
+ kernel-lib/crc32c.h kernel-lib/list.h kerncompat.h \
+ kernel-lib/radix-tree.h kernel-lib/sizes.h kernel-lib/raid56.h \
+ extent-cache.h extent_io.h ioctl.h ctree.h btrfsck.h version.h
+libbtrfsutil_major := $(shell sed -rn 's/^\#define BTRFS_UTIL_VERSION_MAJOR ([0-9])+$$/\1/p' libbtrfsutil/btrfsutil.h)
+libbtrfsutil_minor := $(shell sed -rn 's/^\#define BTRFS_UTIL_VERSION_MINOR ([0-9])+$$/\1/p' libbtrfsutil/btrfsutil.h)
+libbtrfsutil_patch := $(shell sed -rn 's/^\#define BTRFS_UTIL_VERSION_PATCH ([0-9])+$$/\1/p' libbtrfsutil/btrfsutil.h)
+libbtrfsutil_version := $(libbtrfsutil_major).$(libbtrfsutil_minor).$(libbtrfsutil_patch)
+libbtrfsutil_objects = libbtrfsutil/errors.o libbtrfsutil/filesystem.o \
+ libbtrfsutil/subvolume.o libbtrfsutil/qgroup.o \
+ libbtrfsutil/stubs.o
+convert_objects = convert/main.o convert/common.o convert/source-fs.o \
+ convert/source-ext2.o convert/source-reiserfs.o
+mkfs_objects = mkfs/main.o mkfs/common.o mkfs/rootdir.o
+image_objects = image/main.o image/sanitize.o
+all_objects = $(objects) $(cmds_objects) $(libbtrfs_objects) $(convert_objects) \
+ $(mkfs_objects) $(image_objects) $(libbtrfsutil_objects)
+
+udev_rules = 64-btrfs-dm.rules
+
+ifeq ("$(origin V)", "command line")
+ BUILD_VERBOSE = $(V)
+endif
+ifndef BUILD_VERBOSE
+ BUILD_VERBOSE = 0
+endif
+
+ifeq ($(BUILD_VERBOSE),1)
+ Q =
+ SETUP_PY_Q =
+else
+ Q = @
+ SETUP_PY_Q = -q
+endif
+
+ifeq ("$(origin D)", "command line")
+ DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
+ DEBUG_LDFLAGS_INTERNAL = $(DEBUG_LDFLAGS_DEFAULT) $(DEBUG_LDFLAGS)
+endif
+
+ifneq (,$(findstring verbose,$(D)))
+ DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
+endif
+
+ifneq (,$(findstring trace,$(D)))
+ DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
+endif
+
+ifneq (,$(findstring abort,$(D)))
+ DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
+endif
+
+ifneq (,$(findstring all,$(D)))
+ DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
+ DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
+ DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
+endif
+
+ifneq (,$(findstring asan,$(D)))
+ DEBUG_CFLAGS_INTERNAL += -fsanitize=address
+ DEBUG_LDFLAGS_INTERNAL += -fsanitize=address -lasan
+endif
+
+ifneq (,$(findstring tsan,$(D)))
+ DEBUG_CFLAGS_INTERNAL += -fsanitize=thread -fPIC
+ DEBUG_LDFLAGS_INTERNAL += -fsanitize=thread -ltsan -pie
+endif
+
+ifneq (,$(findstring ubsan,$(D)))
+ DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
+ DEBUG_LDFLAGS_INTERNAL += -fsanitize=undefined -lubsan
+endif
+
+ifneq (,$(findstring bcheck,$(D)))
+ DEBUG_CFLAGS_INTERNAL += -DDEBUG_BUILD_CHECKS
+endif
+
+MAKEOPTS = --no-print-directory Q=$(Q)
+
+# build all by default
+progs = $(progs_install) btrfsck btrfs-corrupt-block
+
+# install only selected
+progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
+ btrfs-map-logical btrfs-image btrfs-zero-log \
+ btrfs-find-root btrfstune \
+ btrfs-select-super
+
+# other tools, not built by default
+progs_extra = btrfs-fragments btrfs-calc-size btrfs-show-super
+
+progs_static = $(foreach p,$(progs),$(p).static)
+
+ifneq ($(DISABLE_BTRFSCONVERT),1)
+progs_install += btrfs-convert
+endif
+
+# external libs required by various binaries; for btrfs-foo,
+# specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
+btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2)
+btrfs_convert_cflags += -DBTRFSCONVERT_REISERFS=$(BTRFSCONVERT_REISERFS)
+btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
+btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
+btrfs_show_super_objects = cmds-inspect-dump-super.o
+btrfs_calc_size_objects = cmds-inspect-tree-stats.o
+cmds_restore_cflags = -DBTRFSRESTORE_ZSTD=$(BTRFSRESTORE_ZSTD)
+
+CHECKER_FLAGS += $(btrfs_convert_cflags)
+
+# collect values of the variables above
+standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs) $(progs_extra)))),$($(dep)))
+
+SUBDIRS =
+BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
+INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
+CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
+
+ifneq ($(DISABLE_DOCUMENTATION),1)
+BUILDDIRS += build-Documentation
+INSTALLDIRS += install-Documentation
+endif
+
+.PHONY: $(SUBDIRS)
+.PHONY: $(BUILDDIRS)
+.PHONY: $(INSTALLDIRS)
+.PHONY: $(TESTDIRS)
+.PHONY: $(CLEANDIRS)
+.PHONY: all install clean
+.PHONY: FORCE
+
+# Create all the static targets
+static_objects = $(patsubst %.o, %.static.o, $(objects))
+static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
+static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
+static_convert_objects = $(patsubst %.o, %.static.o, $(convert_objects))
+static_mkfs_objects = $(patsubst %.o, %.static.o, $(mkfs_objects))
+static_image_objects = $(patsubst %.o, %.static.o, $(image_objects))
+
+libs_shared = libbtrfs.so.0.1 libbtrfsutil.so.$(libbtrfsutil_version)
+libs_static = libbtrfs.a libbtrfsutil.a
+libs = $(libs_shared) $(libs_static)
+lib_links = libbtrfs.so.0 libbtrfs.so libbtrfsutil.so.$(libbtrfsutil_major) libbtrfsutil.so