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
79 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
80 uuid-tree.o utils-lib.o rbtree-utils.o
81 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
82 crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
83 extent_io.h ioctl.h ctree.h btrfsck.h version.h
84 TESTS = fsck-tests.sh convert-tests.sh
87 exec_prefix = @exec_prefix@
90 incdir = @includedir@/btrfs
92 ifeq ("$(origin V)", "command line")
99 ifeq ($(BUILD_VERBOSE),1)
105 MAKEOPTS = --no-print-directory Q=$(Q)
107 # build all by default
108 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
110 # install only selected
111 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
112 btrfs-map-logical btrfs-image btrfs-zero-log \
113 btrfs-find-root btrfstune btrfs-show-super \
116 progs_extra = btrfs-fragments
118 progs_static = $(foreach p,$(progs),$(p).static)
120 ifneq ($(DISABLE_BTRFSCONVERT),1)
121 progs_install += btrfs-convert
124 # external libs required by various binaries; for btrfs-foo,
125 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
126 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
127 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
130 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
131 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
132 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
134 ifneq ($(DISABLE_DOCUMENTATION),1)
135 BUILDDIRS += build-Documentation
136 INSTALLDIRS += install-Documentation
141 .PHONY: $(INSTALLDIRS)
144 .PHONY: all install clean
146 # Create all the static targets
147 static_objects = $(patsubst %.o, %.static.o, $(objects))
148 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
149 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
151 libs_shared = libbtrfs.so.0.1
152 libs_static = libbtrfs.a
153 libs = $(libs_shared) $(libs_static)
154 lib_links = libbtrfs.so.0 libbtrfs.so
155 headers = $(libbtrfs_headers)
157 # make C=1 to enable sparse
159 # We're trying to use sparse against glibc headers which go wild
160 # trying to use internal compiler macros to test features. We
161 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
162 # 'cause sparse defines that one.
164 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
165 grep -v __SIZE_TYPE__ > $(check_defs))
174 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
177 @$(check_echo) " [SP] $<"
178 $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
180 $(Q)$(CC) $(CFLAGS) -c $<
184 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
186 all: $(progs) $(BUILDDIRS)
187 $(SUBDIRS): $(BUILDDIRS)
189 @echo "Making all in $(patsubst build-%,%,$@)"
190 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
192 test-convert: btrfs btrfs-convert
193 @echo " [TEST] convert-tests.sh"
194 $(Q)bash tests/convert-tests.sh
196 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
197 @echo " [TEST] fsck-tests.sh"
198 $(Q)bash tests/fsck-tests.sh
200 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
201 @echo " [TEST] misc-tests.sh"
202 $(Q)bash tests/misc-tests.sh
204 test-mkfs: btrfs mkfs.btrfs
205 @echo " [TEST] mkfs-tests.sh"
206 $(Q)bash tests/mkfs-tests.sh
209 @echo " [TEST] fuzz-tests.sh"
210 $(Q)bash tests/fuzz-tests.sh
213 @echo "Cleaning tests"
214 $(Q)bash tests/clean-tests.sh
216 test: test-fsck test-mkfs test-convert test-misc test-fuzz
219 # NOTE: For static compiles, you need to have all the required libs
220 # static equivalent available
222 static: $(progs_static)
224 version.h: version.sh version.h.in configure.ac
226 $(Q)bash ./config.status --silent $@
228 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
230 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
231 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
233 $(libs_static): $(libbtrfs_objects)
235 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
239 $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so.0
240 $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so
242 # keep intermediate files from the below implicit rules around
243 .PRECIOUS: $(addsuffix .o,$(progs))
245 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
246 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
247 # turns them into a list of libraries to link against if they exist
249 # For static variants, use an extra $(subst) to get rid of the ".static"
250 # from the target name before translating to list of libs
252 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
254 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
255 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
256 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
258 btrfs-%: $(objects) $(libs_static) btrfs-%.o
260 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs_static) \
261 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
263 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
265 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
266 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
268 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
270 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
271 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
273 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
276 $(Q)$(LN_S) -f btrfs btrfsck
278 btrfsck.static: btrfs.static
282 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
284 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
286 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
288 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
289 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
291 btrfstune: $(objects) $(libs_static) btrfstune.o
293 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
295 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
297 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
298 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
300 dir-test: $(objects) $(libs) dir-test.o
302 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
304 quick-test: $(objects) $(libs) quick-test.o
306 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
308 ioctl-test: $(objects) $(libs) ioctl-test.o
310 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
312 send-test: $(objects) $(libs) send-test.o
314 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
316 library-test: $(libs_shared) library-test.o
318 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
320 library-test.static: $(libs_static) library-test.o
322 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
324 test-build: test-build-pre test-build-real
333 -$(MAKE) library-test.static
336 $(MAKE) -j 8 $(progs_extra)
339 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
342 clean-all: clean clean-doc clean-gen
346 $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
347 dir-test ioctl-test quick-test send-test library-test library-test-static \
348 btrfs.static mkfs.btrfs.static \
350 $(libs) $(lib_links) \
351 $(progs_static) $(progs_extra)
354 @echo "Cleaning Documentation"
355 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
358 @echo "Cleaning Generated Files"
359 $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
360 configure.lineno config.status.lineno Makefile \
361 Documentation/Makefile \
362 config.log config.h config.h.in~ aclocal.m4 \
363 configure autom4te.cache/ config/
366 @echo "Cleaning $(patsubst clean-%,%,$@)"
367 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
369 install: $(libs) $(progs_install) $(INSTALLDIRS)
370 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
371 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
372 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
373 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
374 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
375 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
376 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
377 cp -a $(lib_links) $(DESTDIR)$(libdir)
378 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
379 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
381 install-static: $(progs_static) $(INSTALLDIRS)
382 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
383 $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
384 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
385 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
388 @echo "Making install in $(patsubst install-%,%,$@)"
389 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
392 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
393 cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
394 $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
395 cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
396 cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
398 ifneq ($(MAKECMDGOALS),clean)
399 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))