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 # W=123 build with warnings (default: off)
21 # DEBUG_CFLAGS additional compiler flags for debugging build
22 # EXTRA_CFLAGS additional compiler flags
23 # EXTRA_LDFLAGS additional linker flags
26 # CHECKER static checker binary to be called (default: sparse)
27 # CHECKER_FLAGS flags to pass to CHECKER, can override CFLAGS
30 # Export all variables to sub-makes by default
33 include Makefile.extrawarn
41 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
42 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
47 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
48 DEBUG_CFLAGS_INTERNAL =
54 -DBTRFS_FLAT_INCLUDES \
56 -fno-strict-aliasing \
59 $(DEBUG_CFLAGS_INTERNAL) \
63 -rdynamic $(EXTRA_LDFLAGS)
65 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
66 LIBBTRFS_LIBS = $(LIBS)
68 # Static compilation flags
69 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
70 STATIC_LDFLAGS = -static -Wl,--gc-sections
71 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
72 @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
74 # don't use FORTIFY with sparse because glibc with FORTIFY can
75 # generate so many sparse errors that sparse stops parsing,
76 # which masks real errors that we want to see.
78 check_defs := .cc-defines.h
79 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
80 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
83 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
84 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
85 extent-cache.o extent_io.o volumes.o utils.o repair.o \
86 qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
87 ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
88 inode.o file.o find-root.o free-space-tree.o help.o
89 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
90 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
91 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
92 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
93 cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
94 cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o
95 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
96 uuid-tree.o utils-lib.o rbtree-utils.o
97 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
98 crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
99 extent_io.h ioctl.h ctree.h btrfsck.h version.h
100 TESTS = fsck-tests.sh convert-tests.sh
102 udev_rules = 64-btrfs-dm.rules
105 exec_prefix = @exec_prefix@
108 incdir = @includedir@/btrfs
110 udevruledir = ${udevdir}/rules.d
112 ifeq ("$(origin V)", "command line")
119 ifeq ($(BUILD_VERBOSE),1)
125 ifeq ("$(origin D)", "command line")
126 DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
129 ifneq (,$(findstring verbose,$(D)))
130 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
133 ifneq (,$(findstring trace,$(D)))
134 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
137 ifneq (,$(findstring abort,$(D)))
138 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
141 ifneq (,$(findstring all,$(D)))
142 DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
143 DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
144 DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
147 MAKEOPTS = --no-print-directory Q=$(Q)
149 # build all by default
150 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
152 # install only selected
153 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
154 btrfs-map-logical btrfs-image btrfs-zero-log \
155 btrfs-find-root btrfstune btrfs-show-super \
158 progs_extra = btrfs-fragments
160 progs_static = $(foreach p,$(progs),$(p).static)
162 ifneq ($(DISABLE_BTRFSCONVERT),1)
163 progs_install += btrfs-convert
166 # external libs required by various binaries; for btrfs-foo,
167 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
168 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
169 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
170 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
171 btrfs_show_super_objects = cmds-inspect-dump-super.o
172 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
174 # collect values of the variables above
175 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
178 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
179 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
180 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
182 ifneq ($(DISABLE_DOCUMENTATION),1)
183 BUILDDIRS += build-Documentation
184 INSTALLDIRS += install-Documentation
189 .PHONY: $(INSTALLDIRS)
192 .PHONY: all install clean
194 # Create all the static targets
195 static_objects = $(patsubst %.o, %.static.o, $(objects))
196 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
197 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
199 libs_shared = libbtrfs.so.0.1
200 libs_static = libbtrfs.a
201 libs = $(libs_shared) $(libs_static)
202 lib_links = libbtrfs.so.0 libbtrfs.so
203 headers = $(libbtrfs_headers)
205 # make C=1 to enable sparse
207 # We're trying to use sparse against glibc headers which go wild
208 # trying to use internal compiler macros to test features. We
209 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
210 # 'cause sparse defines that one.
212 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
213 grep -v __SIZE_TYPE__ > $(check_defs))
222 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
225 @$(check_echo) " [SP] $<"
226 $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
228 $(Q)$(CC) $(CFLAGS) -c $<
232 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
234 all: $(progs) $(BUILDDIRS)
235 $(SUBDIRS): $(BUILDDIRS)
237 @echo "Making all in $(patsubst build-%,%,$@)"
238 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
240 test-convert: btrfs btrfs-convert
241 @echo " [TEST] convert-tests.sh"
242 $(Q)bash tests/convert-tests.sh
244 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
245 @echo " [TEST] fsck-tests.sh"
246 $(Q)bash tests/fsck-tests.sh
248 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
249 @echo " [TEST] misc-tests.sh"
250 $(Q)bash tests/misc-tests.sh
252 test-mkfs: btrfs mkfs.btrfs
253 @echo " [TEST] mkfs-tests.sh"
254 $(Q)bash tests/mkfs-tests.sh
257 @echo " [TEST] fuzz-tests.sh"
258 $(Q)bash tests/fuzz-tests.sh
261 @echo " [TEST] cli-tests.sh"
262 $(Q)bash tests/cli-tests.sh
265 @echo "Cleaning tests"
266 $(Q)bash tests/clean-tests.sh
269 @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
270 echo "Test installation to $$tmpdest" && \
271 $(MAKE) DESTDIR=$$tmpdest install && \
272 $(RM) -rf -- $$tmpdest
274 test: test-fsck test-mkfs test-convert test-misc test-fuzz
277 # NOTE: For static compiles, you need to have all the required libs
278 # static equivalent available
280 static: $(progs_static)
282 version.h: version.sh version.h.in configure.ac
284 $(Q)bash ./config.status --silent $@
286 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
288 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
289 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
291 $(libs_static): $(libbtrfs_objects)
293 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
297 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
299 # keep intermediate files from the below implicit rules around
300 .PRECIOUS: $(addsuffix .o,$(progs))
302 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
303 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
304 # turns them into a list of libraries to link against if they exist
306 # For static variants, use an extra $(subst) to get rid of the ".static"
307 # from the target name before translating to list of libs
309 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects) $(patsubst %.o,%.static.o,$(standalone_deps))
311 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
312 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
313 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
314 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
316 btrfs-%: $(objects) $(libs_static) btrfs-%.o $(standalone_deps)
318 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
319 $($(subst -,_,$@-objects)) \
321 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
323 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
325 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
326 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
328 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
330 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
331 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
333 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
336 $(Q)$(LN_S) -f btrfs btrfsck
338 btrfsck.static: btrfs.static
342 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
344 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
346 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
348 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
349 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
351 btrfstune: $(objects) $(libs_static) btrfstune.o
353 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
355 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
357 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
358 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
360 dir-test: $(objects) $(libs) dir-test.o
362 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
364 quick-test: $(objects) $(libs) quick-test.o
366 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
368 ioctl-test: $(objects) $(libs) ioctl-test.o
370 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
372 send-test: $(objects) $(libs) send-test.o
374 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
376 library-test: $(libs_shared) library-test.o
378 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
380 library-test.static: $(libs_static) library-test.o
382 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
384 test-build: test-build-pre test-build-real
393 -$(MAKE) library-test.static
396 $(MAKE) -j 8 $(progs_extra)
399 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
402 clean-all: clean clean-doc clean-gen
406 $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
407 dir-test ioctl-test quick-test send-test library-test library-test-static \
408 btrfs.static mkfs.btrfs.static \
410 $(libs) $(lib_links) \
411 $(progs_static) $(progs_extra)
414 @echo "Cleaning Documentation"
415 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
418 @echo "Cleaning Generated Files"
419 $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
420 configure.lineno config.status.lineno Makefile \
421 Documentation/Makefile \
422 config.log config.h config.h.in~ aclocal.m4 \
423 configure autom4te.cache/ config/
426 @echo "Cleaning $(patsubst clean-%,%,$@)"
427 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
429 install: $(libs) $(progs_install) $(INSTALLDIRS)
430 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
431 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
432 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
433 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
434 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
435 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
436 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
437 cp -a $(lib_links) $(DESTDIR)$(libdir)
438 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
439 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
441 $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
442 $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
445 install-static: $(progs_static) $(INSTALLDIRS)
446 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
447 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
448 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
449 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
452 @echo "Making install in $(patsubst install-%,%,$@)"
453 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
456 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
457 cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
458 $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
459 cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
460 cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
462 ifneq ($(MAKECMDGOALS),clean)
463 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))