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 # W=123 build with warnings (default: off)
14 # EXTRA_CFLAGS additional compiler flags
15 # EXTRA_LDFLAGS additional linker flags
18 # CHECKER static checker binary to be called (default: sparse)
19 # CHECKER_FLAGS flags to pass to CHECKER, can override CFLAGS
22 # Export all variables to sub-makes by default
25 include Makefile.extrawarn
33 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
34 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
42 -DBTRFS_FLAT_INCLUDES \
44 -fno-strict-aliasing \
45 -fPIC $(EXTRAWARN_CFLAGS) $(EXTRA_CFLAGS)
48 -rdynamic $(EXTRA_LDFLAGS)
50 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
51 LIBBTRFS_LIBS = $(LIBS)
53 # Static compilation flags
54 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
55 STATIC_LDFLAGS = -static -Wl,--gc-sections
56 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
57 @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
59 # don't use FORTIFY with sparse because glibc with FORTIFY can
60 # generate so many sparse errors that sparse stops parsing,
61 # which masks real errors that we want to see.
63 check_defs := .cc-defines.h
64 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
65 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
68 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
69 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
70 extent-cache.o extent_io.o volumes.o utils.o repair.o \
71 qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
72 ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
73 inode.o file.o find-root.o free-space-tree.o help.o
74 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
75 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
76 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
77 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
78 cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
79 cmds-inspect-dump-super.o cmds-fi-du.o
80 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
81 uuid-tree.o utils-lib.o rbtree-utils.o
82 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
83 crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
84 extent_io.h ioctl.h ctree.h btrfsck.h version.h
85 TESTS = fsck-tests.sh convert-tests.sh
88 exec_prefix = @exec_prefix@
91 incdir = @includedir@/btrfs
93 ifeq ("$(origin V)", "command line")
100 ifeq ($(BUILD_VERBOSE),1)
106 MAKEOPTS = --no-print-directory Q=$(Q)
108 # build all by default
109 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
111 # install only selected
112 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
113 btrfs-map-logical btrfs-image btrfs-zero-log \
114 btrfs-find-root btrfstune btrfs-show-super \
117 progs_extra = btrfs-fragments
119 progs_static = $(foreach p,$(progs),$(p).static)
121 ifneq ($(DISABLE_BTRFSCONVERT),1)
122 progs_install += btrfs-convert
125 # external libs required by various binaries; for btrfs-foo,
126 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
127 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
128 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
129 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
130 btrfs_show_super_objects = cmds-inspect-dump-super.o
133 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
134 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
135 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
137 ifneq ($(DISABLE_DOCUMENTATION),1)
138 BUILDDIRS += build-Documentation
139 INSTALLDIRS += install-Documentation
144 .PHONY: $(INSTALLDIRS)
147 .PHONY: all install clean
149 # Create all the static targets
150 static_objects = $(patsubst %.o, %.static.o, $(objects))
151 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
152 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
154 libs_shared = libbtrfs.so.0.1
155 libs_static = libbtrfs.a
156 libs = $(libs_shared) $(libs_static)
157 lib_links = libbtrfs.so.0 libbtrfs.so
158 headers = $(libbtrfs_headers)
160 # make C=1 to enable sparse
162 # We're trying to use sparse against glibc headers which go wild
163 # trying to use internal compiler macros to test features. We
164 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
165 # 'cause sparse defines that one.
167 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
168 grep -v __SIZE_TYPE__ > $(check_defs))
177 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
180 @$(check_echo) " [SP] $<"
181 $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
183 $(Q)$(CC) $(CFLAGS) -c $<
187 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
189 all: $(progs) $(BUILDDIRS)
190 $(SUBDIRS): $(BUILDDIRS)
192 @echo "Making all in $(patsubst build-%,%,$@)"
193 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
195 test-convert: btrfs btrfs-convert
196 @echo " [TEST] convert-tests.sh"
197 $(Q)bash tests/convert-tests.sh
199 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
200 @echo " [TEST] fsck-tests.sh"
201 $(Q)bash tests/fsck-tests.sh
203 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
204 @echo " [TEST] misc-tests.sh"
205 $(Q)bash tests/misc-tests.sh
207 test-mkfs: btrfs mkfs.btrfs
208 @echo " [TEST] mkfs-tests.sh"
209 $(Q)bash tests/mkfs-tests.sh
212 @echo " [TEST] fuzz-tests.sh"
213 $(Q)bash tests/fuzz-tests.sh
216 @echo "Cleaning tests"
217 $(Q)bash tests/clean-tests.sh
219 test: test-fsck test-mkfs test-convert test-misc test-fuzz
222 # NOTE: For static compiles, you need to have all the required libs
223 # static equivalent available
225 static: $(progs_static)
227 version.h: version.sh version.h.in configure.ac
229 $(Q)bash ./config.status --silent $@
231 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
233 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
234 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
236 $(libs_static): $(libbtrfs_objects)
238 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
242 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
244 # keep intermediate files from the below implicit rules around
245 .PRECIOUS: $(addsuffix .o,$(progs))
247 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
248 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
249 # turns them into a list of libraries to link against if they exist
251 # For static variants, use an extra $(subst) to get rid of the ".static"
252 # from the target name before translating to list of libs
254 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
256 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
257 $($(subst -,_,$(subst .static,,$@)-objects)) \
258 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
259 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
261 btrfs-%: $(objects) $(libs_static) btrfs-%.o
263 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
264 $($(subst -,_,$@-objects)) \
266 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
268 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
270 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
271 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
273 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
275 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
276 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
278 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
281 $(Q)$(LN_S) -f btrfs btrfsck
283 btrfsck.static: btrfs.static
287 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
289 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
291 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
293 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
294 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
296 btrfstune: $(objects) $(libs_static) btrfstune.o
298 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
300 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
302 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
303 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
305 dir-test: $(objects) $(libs) dir-test.o
307 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
309 quick-test: $(objects) $(libs) quick-test.o
311 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
313 ioctl-test: $(objects) $(libs) ioctl-test.o
315 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
317 send-test: $(objects) $(libs) send-test.o
319 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
321 library-test: $(libs_shared) library-test.o
323 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
325 library-test.static: $(libs_static) library-test.o
327 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
329 test-build: test-build-pre test-build-real
338 -$(MAKE) library-test.static
341 $(MAKE) -j 8 $(progs_extra)
344 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
347 clean-all: clean clean-doc clean-gen
351 $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
352 dir-test ioctl-test quick-test send-test library-test library-test-static \
353 btrfs.static mkfs.btrfs.static \
355 $(libs) $(lib_links) \
356 $(progs_static) $(progs_extra)
359 @echo "Cleaning Documentation"
360 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
363 @echo "Cleaning Generated Files"
364 $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
365 configure.lineno config.status.lineno Makefile \
366 Documentation/Makefile \
367 config.log config.h config.h.in~ aclocal.m4 \
368 configure autom4te.cache/ config/
371 @echo "Cleaning $(patsubst clean-%,%,$@)"
372 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
374 install: $(libs) $(progs_install) $(INSTALLDIRS)
375 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
376 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
377 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
378 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
379 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
380 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
381 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
382 cp -a $(lib_links) $(DESTDIR)$(libdir)
383 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
384 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
386 install-static: $(progs_static) $(INSTALLDIRS)
387 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
388 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
389 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
390 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
393 @echo "Making install in $(patsubst install-%,%,$@)"
394 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
397 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
398 cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
399 $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
400 cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
401 cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
403 ifneq ($(MAKECMDGOALS),clean)
404 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))