1 # Export all variables to sub-makes by default
4 include Makefile.extrawarn
12 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
13 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
21 -DBTRFS_FLAT_INCLUDES \
23 -fno-strict-aliasing \
24 -fPIC $(KBUILD_CFLAGS) $(EXTRA_CFLAGS)
27 -rdynamic $(EXTRA_LDFLAGS)
29 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
30 LIBBTRFS_LIBS = $(LIBS)
32 # Static compilation flags
33 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
34 STATIC_LDFLAGS = -static -Wl,--gc-sections
35 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
36 @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
38 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
39 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
40 extent-cache.o extent_io.o volumes.o utils.o repair.o \
41 qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
42 ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
43 inode.o file.o find-root.o
44 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
45 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
46 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
47 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
48 cmds-property.o cmds-fi-usage.o
49 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
50 uuid-tree.o utils-lib.o rbtree-utils.o
51 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
52 crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
53 extent_io.h ioctl.h ctree.h btrfsck.h version.h
54 TESTS = fsck-tests.sh convert-tests.sh
57 exec_prefix = @exec_prefix@
60 incdir = @includedir@/btrfs
62 ifeq ("$(origin V)", "command line")
69 ifeq ($(BUILD_VERBOSE),1)
75 MAKEOPTS = --no-print-directory Q=$(Q)
77 # build all by default
78 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
80 # install only selected
81 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
82 btrfs-map-logical btrfs-image btrfs-zero-log \
83 btrfs-find-root btrfstune btrfs-show-super \
86 progs_extra = btrfs-fragments
88 progs_static = $(foreach p,$(progs),$(p).static)
90 ifneq ($(DISABLE_BTRFSCONVERT),1)
91 progs_install += btrfs-convert
94 # external libs required by various binaries; for btrfs-foo,
95 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
96 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
97 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
100 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
101 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
102 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
104 ifneq ($(DISABLE_DOCUMENTATION),1)
105 BUILDDIRS += build-Documentation
106 INSTALLDIRS += install-Documentation
111 .PHONY: $(INSTALLDIRS)
114 .PHONY: all install clean
116 # Create all the static targets
117 static_objects = $(patsubst %.o, %.static.o, $(objects))
118 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
119 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
121 libs_shared = libbtrfs.so.0.1
122 libs_static = libbtrfs.a
123 libs = $(libs_shared) $(libs_static)
124 lib_links = libbtrfs.so.0 libbtrfs.so
125 headers = $(libbtrfs_headers)
127 # make C=1 to enable sparse
128 check_defs := .cc-defines.h
131 # We're trying to use sparse against glibc headers which go wild
132 # trying to use internal compiler macros to test features. We
133 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
134 # 'cause sparse defines that one.
136 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
137 grep -v __SIZE_TYPE__ > $(check_defs))
138 check = sparse -include $(check_defs) -D__CHECKER__ \
139 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
141 # don't use FORTIFY with sparse because glibc with FORTIFY can
142 # generate so many sparse errors that sparse stops parsing,
143 # which masks real errors that we want to see.
144 CFLAGS += -U_FORTIFY_SOURCE
151 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
154 @$(check_echo) " [SP] $<"
155 $(Q)$(check) $(CFLAGS) $<
157 $(Q)$(CC) $(CFLAGS) -c $<
161 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
163 all: $(progs) $(BUILDDIRS)
164 $(SUBDIRS): $(BUILDDIRS)
166 @echo "Making all in $(patsubst build-%,%,$@)"
167 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
169 test-convert: btrfs btrfs-convert
170 @echo " [TEST] convert-tests.sh"
171 $(Q)bash tests/convert-tests.sh
173 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
174 @echo " [TEST] fsck-tests.sh"
175 $(Q)bash tests/fsck-tests.sh
177 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
178 @echo " [TEST] misc-tests.sh"
179 $(Q)bash tests/misc-tests.sh
182 @echo "Cleaning tests"
183 $(Q)bash tests/clean-tests.sh
185 test: test-fsck test-convert test-misc
188 # NOTE: For static compiles, you need to have all the required libs
189 # static equivalent available
191 static: $(progs_static)
193 version.h: version.sh version.h.in configure.ac
195 $(Q)bash ./config.status --silent $@
197 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
199 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
200 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
202 $(libs_static): $(libbtrfs_objects)
204 $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
208 $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so.0
209 $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so
211 # keep intermediate files from the below implicit rules around
212 .PRECIOUS: $(addsuffix .o,$(progs))
214 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
215 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
216 # turns them into a list of libraries to link against if they exist
218 # For static variants, use an extra $(subst) to get rid of the ".static"
219 # from the target name before translating to list of libs
221 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
223 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
224 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
225 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
227 btrfs-%: $(objects) $(libs_static) btrfs-%.o
229 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs_static) \
230 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
232 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs_static)
234 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
235 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
237 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
239 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
240 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
242 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
245 $(Q)$(LN_S) -f btrfs btrfsck
247 btrfsck.static: btrfs.static
251 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
253 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
255 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
257 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
258 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
260 btrfstune: $(objects) $(libs_static) btrfstune.o
262 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
264 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
266 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
267 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
269 dir-test: $(objects) $(libs) dir-test.o
271 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
273 quick-test: $(objects) $(libs) quick-test.o
275 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
277 ioctl-test: $(objects) $(libs) ioctl-test.o
279 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
281 send-test: $(objects) $(libs) send-test.o
283 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
285 library-test: $(libs_shared) library-test.o
287 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
289 library-test.static: $(libs_static) library-test.o
291 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
293 test-build: test-build-pre test-build-real
302 -$(MAKE) library-test.static
305 $(MAKE) -j 8 $(progs_extra)
308 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
311 clean-all: clean clean-doc clean-gen
315 $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
316 dir-test ioctl-test quick-test send-test library-test library-test-static \
317 btrfs.static mkfs.btrfs.static \
319 $(libs) $(lib_links) \
320 $(progs_static) $(progs_extra)
323 @echo "Cleaning Documentation"
324 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
327 @echo "Cleaning Generated Files"
328 $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
329 configure.lineno config.status.lineno Makefile \
330 Documentation/Makefile \
331 config.log config.h config.h.in~ aclocal.m4 \
332 configure autom4te.cache/ config/
335 @echo "Cleaning $(patsubst clean-%,%,$@)"
336 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
338 install: $(libs) $(progs_install) $(INSTALLDIRS)
339 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
340 $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
341 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
342 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
343 $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
344 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
345 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
346 cp -a $(lib_links) $(DESTDIR)$(libdir)
347 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
348 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
350 install-static: $(progs_static) $(INSTALLDIRS)
351 for p in $(progs_static) ; do \
352 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/ ; \
354 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
355 $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
358 @echo "Making install in $(patsubst install-%,%,$@)"
359 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
362 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
363 cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
364 $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
365 cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
366 cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
368 ifneq ($(MAKECMDGOALS),clean)
369 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))