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)
10 # Tuning by variables (environment or make arguments):
11 # V=1 verbose, print command lines (default: quiet)
12 # C=1 run checker before compilation (default checker: sparse)
13 # D=1 debugging build, turn off optimizations
14 # W=123 build with warnings (default: off)
15 # DEBUG_CFLAGS additional compiler flags for debugging build
16 # EXTRA_CFLAGS additional compiler flags
17 # EXTRA_LDFLAGS additional linker flags
20 # CHECKER static checker binary to be called (default: sparse)
21 # CHECKER_FLAGS flags to pass to CHECKER, can override CFLAGS
24 # Export all variables to sub-makes by default
27 include Makefile.extrawarn
35 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
36 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
41 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
42 DEBUG_CFLAGS_INTERNAL =
48 -DBTRFS_FLAT_INCLUDES \
50 -fno-strict-aliasing \
53 $(DEBUG_CFLAGS_INTERNAL) \
57 -rdynamic $(EXTRA_LDFLAGS)
59 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
60 LIBBTRFS_LIBS = $(LIBS)
62 # Static compilation flags
63 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
64 STATIC_LDFLAGS = -static -Wl,--gc-sections
65 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
66 @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
68 # don't use FORTIFY with sparse because glibc with FORTIFY can
69 # generate so many sparse errors that sparse stops parsing,
70 # which masks real errors that we want to see.
72 check_defs := .cc-defines.h
73 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
74 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
77 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
78 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
79 extent-cache.o extent_io.o volumes.o utils.o repair.o \
80 qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
81 ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
82 inode.o file.o find-root.o free-space-tree.o help.o
83 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
84 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
85 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
86 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
87 cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
88 cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o
89 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
90 uuid-tree.o utils-lib.o rbtree-utils.o
91 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
92 crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
93 extent_io.h ioctl.h ctree.h btrfsck.h version.h
94 TESTS = fsck-tests.sh convert-tests.sh
96 udev_rules = 64-btrfs-dm.rules
99 exec_prefix = @exec_prefix@
102 incdir = @includedir@/btrfs
104 udevruledir = ${udevdir}/rules.d
106 ifeq ("$(origin V)", "command line")
113 ifeq ($(BUILD_VERBOSE),1)
119 ifeq ("$(origin D)", "command line")
120 DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
123 MAKEOPTS = --no-print-directory Q=$(Q)
125 # build all by default
126 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
128 # install only selected
129 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
130 btrfs-map-logical btrfs-image btrfs-zero-log \
131 btrfs-find-root btrfstune btrfs-show-super \
134 progs_extra = btrfs-fragments
136 progs_static = $(foreach p,$(progs),$(p).static)
138 ifneq ($(DISABLE_BTRFSCONVERT),1)
139 progs_install += btrfs-convert
142 # external libs required by various binaries; for btrfs-foo,
143 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
144 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
145 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
146 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
147 btrfs_show_super_objects = cmds-inspect-dump-super.o
148 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
150 # collect values of the variables above
151 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
154 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
155 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
156 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
158 ifneq ($(DISABLE_DOCUMENTATION),1)
159 BUILDDIRS += build-Documentation
160 INSTALLDIRS += install-Documentation
165 .PHONY: $(INSTALLDIRS)
168 .PHONY: all install clean
170 # Create all the static targets
171 static_objects = $(patsubst %.o, %.static.o, $(objects))
172 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
173 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
175 libs_shared = libbtrfs.so.0.1
176 libs_static = libbtrfs.a
177 libs = $(libs_shared) $(libs_static)
178 lib_links = libbtrfs.so.0 libbtrfs.so
179 headers = $(libbtrfs_headers)
181 # make C=1 to enable sparse
183 # We're trying to use sparse against glibc headers which go wild
184 # trying to use internal compiler macros to test features. We
185 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
186 # 'cause sparse defines that one.
188 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
189 grep -v __SIZE_TYPE__ > $(check_defs))
198 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
201 @$(check_echo) " [SP] $<"
202 $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
204 $(Q)$(CC) $(CFLAGS) -c $<
208 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
210 all: $(progs) $(BUILDDIRS)
211 $(SUBDIRS): $(BUILDDIRS)
213 @echo "Making all in $(patsubst build-%,%,$@)"
214 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
216 test-convert: btrfs btrfs-convert
217 @echo " [TEST] convert-tests.sh"
218 $(Q)bash tests/convert-tests.sh
220 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
221 @echo " [TEST] fsck-tests.sh"
222 $(Q)bash tests/fsck-tests.sh
224 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
225 @echo " [TEST] misc-tests.sh"
226 $(Q)bash tests/misc-tests.sh
228 test-mkfs: btrfs mkfs.btrfs
229 @echo " [TEST] mkfs-tests.sh"
230 $(Q)bash tests/mkfs-tests.sh
233 @echo " [TEST] fuzz-tests.sh"
234 $(Q)bash tests/fuzz-tests.sh
237 @echo " [TEST] cli-tests.sh"
238 $(Q)bash tests/cli-tests.sh
241 @echo "Cleaning tests"
242 $(Q)bash tests/clean-tests.sh
244 test: test-fsck test-mkfs test-convert test-misc test-fuzz
247 # NOTE: For static compiles, you need to have all the required libs
248 # static equivalent available
250 static: $(progs_static)
252 version.h: version.sh version.h.in configure.ac
254 $(Q)bash ./config.status --silent $@
256 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
258 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
259 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
261 $(libs_static): $(libbtrfs_objects)
263 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
267 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
269 # keep intermediate files from the below implicit rules around
270 .PRECIOUS: $(addsuffix .o,$(progs))
272 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
273 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
274 # turns them into a list of libraries to link against if they exist
276 # For static variants, use an extra $(subst) to get rid of the ".static"
277 # from the target name before translating to list of libs
279 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects) $(patsubst %.o,%.static.o,$(standalone_deps))
281 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
282 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
283 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
284 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
286 btrfs-%: $(objects) $(libs_static) btrfs-%.o $(standalone_deps)
288 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
289 $($(subst -,_,$@-objects)) \
291 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
293 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
295 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
296 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
298 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
300 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
301 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
303 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
306 $(Q)$(LN_S) -f btrfs btrfsck
308 btrfsck.static: btrfs.static
312 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
314 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
316 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
318 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
319 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
321 btrfstune: $(objects) $(libs_static) btrfstune.o
323 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
325 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
327 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
328 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
330 dir-test: $(objects) $(libs) dir-test.o
332 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
334 quick-test: $(objects) $(libs) quick-test.o
336 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
338 ioctl-test: $(objects) $(libs) ioctl-test.o
340 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
342 send-test: $(objects) $(libs) send-test.o
344 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
346 library-test: $(libs_shared) library-test.o
348 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
350 library-test.static: $(libs_static) library-test.o
352 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
354 test-build: test-build-pre test-build-real
363 -$(MAKE) library-test.static
366 $(MAKE) -j 8 $(progs_extra)
369 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
372 clean-all: clean clean-doc clean-gen
376 $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
377 dir-test ioctl-test quick-test send-test library-test library-test-static \
378 btrfs.static mkfs.btrfs.static \
380 $(libs) $(lib_links) \
381 $(progs_static) $(progs_extra)
384 @echo "Cleaning Documentation"
385 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
388 @echo "Cleaning Generated Files"
389 $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
390 configure.lineno config.status.lineno Makefile \
391 Documentation/Makefile \
392 config.log config.h config.h.in~ aclocal.m4 \
393 configure autom4te.cache/ config/
396 @echo "Cleaning $(patsubst clean-%,%,$@)"
397 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
399 install: $(libs) $(progs_install) $(INSTALLDIRS)
400 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
401 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
402 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
403 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
404 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
405 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
406 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
407 cp -a $(lib_links) $(DESTDIR)$(libdir)
408 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
409 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
411 $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
412 $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
415 install-static: $(progs_static) $(INSTALLDIRS)
416 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
417 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
418 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
419 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
422 @echo "Making install in $(patsubst install-%,%,$@)"
423 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
426 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
427 cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
428 $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
429 cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
430 cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
432 ifneq ($(MAKECMDGOALS),clean)
433 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))