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
132 # collect values of the variables above
133 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
136 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
137 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
138 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
140 ifneq ($(DISABLE_DOCUMENTATION),1)
141 BUILDDIRS += build-Documentation
142 INSTALLDIRS += install-Documentation
147 .PHONY: $(INSTALLDIRS)
150 .PHONY: all install clean
152 # Create all the static targets
153 static_objects = $(patsubst %.o, %.static.o, $(objects))
154 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
155 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
157 libs_shared = libbtrfs.so.0.1
158 libs_static = libbtrfs.a
159 libs = $(libs_shared) $(libs_static)
160 lib_links = libbtrfs.so.0 libbtrfs.so
161 headers = $(libbtrfs_headers)
163 # make C=1 to enable sparse
165 # We're trying to use sparse against glibc headers which go wild
166 # trying to use internal compiler macros to test features. We
167 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
168 # 'cause sparse defines that one.
170 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
171 grep -v __SIZE_TYPE__ > $(check_defs))
180 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
183 @$(check_echo) " [SP] $<"
184 $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
186 $(Q)$(CC) $(CFLAGS) -c $<
190 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
192 all: $(progs) $(BUILDDIRS)
193 $(SUBDIRS): $(BUILDDIRS)
195 @echo "Making all in $(patsubst build-%,%,$@)"
196 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
198 test-convert: btrfs btrfs-convert
199 @echo " [TEST] convert-tests.sh"
200 $(Q)bash tests/convert-tests.sh
202 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
203 @echo " [TEST] fsck-tests.sh"
204 $(Q)bash tests/fsck-tests.sh
206 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
207 @echo " [TEST] misc-tests.sh"
208 $(Q)bash tests/misc-tests.sh
210 test-mkfs: btrfs mkfs.btrfs
211 @echo " [TEST] mkfs-tests.sh"
212 $(Q)bash tests/mkfs-tests.sh
215 @echo " [TEST] fuzz-tests.sh"
216 $(Q)bash tests/fuzz-tests.sh
219 @echo "Cleaning tests"
220 $(Q)bash tests/clean-tests.sh
222 test: test-fsck test-mkfs test-convert test-misc test-fuzz
225 # NOTE: For static compiles, you need to have all the required libs
226 # static equivalent available
228 static: $(progs_static)
230 version.h: version.sh version.h.in configure.ac
232 $(Q)bash ./config.status --silent $@
234 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
236 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
237 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
239 $(libs_static): $(libbtrfs_objects)
241 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
245 $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
247 # keep intermediate files from the below implicit rules around
248 .PRECIOUS: $(addsuffix .o,$(progs))
250 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
251 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
252 # turns them into a list of libraries to link against if they exist
254 # For static variants, use an extra $(subst) to get rid of the ".static"
255 # from the target name before translating to list of libs
257 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects) $(patsubst %.o,%.static.o,$(standalone_deps))
259 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
260 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
261 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
262 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
264 btrfs-%: $(objects) $(libs_static) btrfs-%.o $(standalone_deps)
266 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
267 $($(subst -,_,$@-objects)) \
269 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
271 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
273 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
274 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
276 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
278 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
279 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
281 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
284 $(Q)$(LN_S) -f btrfs btrfsck
286 btrfsck.static: btrfs.static
290 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
292 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
294 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
296 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
297 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
299 btrfstune: $(objects) $(libs_static) btrfstune.o
301 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
303 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
305 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
306 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
308 dir-test: $(objects) $(libs) dir-test.o
310 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
312 quick-test: $(objects) $(libs) quick-test.o
314 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
316 ioctl-test: $(objects) $(libs) ioctl-test.o
318 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
320 send-test: $(objects) $(libs) send-test.o
322 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
324 library-test: $(libs_shared) library-test.o
326 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
328 library-test.static: $(libs_static) library-test.o
330 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
332 test-build: test-build-pre test-build-real
341 -$(MAKE) library-test.static
344 $(MAKE) -j 8 $(progs_extra)
347 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
350 clean-all: clean clean-doc clean-gen
354 $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
355 dir-test ioctl-test quick-test send-test library-test library-test-static \
356 btrfs.static mkfs.btrfs.static \
358 $(libs) $(lib_links) \
359 $(progs_static) $(progs_extra)
362 @echo "Cleaning Documentation"
363 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
366 @echo "Cleaning Generated Files"
367 $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
368 configure.lineno config.status.lineno Makefile \
369 Documentation/Makefile \
370 config.log config.h config.h.in~ aclocal.m4 \
371 configure autom4te.cache/ config/
374 @echo "Cleaning $(patsubst clean-%,%,$@)"
375 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
377 install: $(libs) $(progs_install) $(INSTALLDIRS)
378 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
379 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
380 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
381 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
382 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
383 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
384 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
385 cp -a $(lib_links) $(DESTDIR)$(libdir)
386 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
387 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
389 install-static: $(progs_static) $(INSTALLDIRS)
390 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
391 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
392 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
393 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
396 @echo "Making install in $(patsubst install-%,%,$@)"
397 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
400 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
401 cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
402 $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
403 cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
404 cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
406 ifneq ($(MAKECMDGOALS),clean)
407 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))