1 # Export all variables to sub-makes by default
8 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
10 # Non-static compilation flags
12 -include config.h -Wall \
13 -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES \
15 -fno-strict-aliasing -fPIC \
20 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
21 LIBBTRFS_LIBS = $(LIBS)
23 # Static compilation flags
24 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
25 STATIC_LDFLAGS = -static -Wl,--gc-sections
26 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
27 @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
29 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
30 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
31 extent-cache.o extent_io.o volumes.o utils.o repair.o \
32 qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
33 ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
35 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
36 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
37 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
38 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
39 cmds-property.o cmds-fi-disk_usage.o
40 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
41 uuid-tree.o utils-lib.o rbtree-utils.o
42 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
43 crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
44 extent_io.h ioctl.h ctree.h btrfsck.h version.h
45 TESTS = fsck-tests.sh convert-tests.sh
50 incdir = @includedir@/btrfs
52 ifeq ("$(origin V)", "command line")
59 ifeq ($(BUILD_VERBOSE),1)
65 MAKEOPTS = --no-print-directory Q=$(Q)
67 progs = mkfs.btrfs btrfs-debug-tree btrfsck \
68 btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
69 btrfs-find-root btrfstune btrfs-show-super
71 progs_extra = btrfs-corrupt-block btrfs-fragments btrfs-calc-size \
74 progs_static = $(foreach p,$(progs),$(p).static)
76 # external libs required by various binaries; for btrfs-foo,
77 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
78 btrfs_convert_libs = -lext2fs -lcom_err
79 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
82 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
83 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
84 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
86 ifneq ($(DISABLE_DOCUMENTATION),1)
87 BUILDDIRS += build-Documentation
88 INSTALLDIRS += install-Documentation
93 .PHONY: $(INSTALLDIRS)
96 .PHONY: all install clean
98 # Create all the static targets
99 static_objects = $(patsubst %.o, %.static.o, $(objects))
100 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
101 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
103 libs_shared = libbtrfs.so.0.1
104 libs_static = libbtrfs.a
105 libs = $(libs_shared) $(libs_static)
106 lib_links = libbtrfs.so.0 libbtrfs.so
107 headers = $(libbtrfs_headers)
109 # make C=1 to enable sparse
110 check_defs := .cc-defines.h
113 # We're trying to use sparse against glibc headers which go wild
114 # trying to use internal compiler macros to test features. We
115 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
116 # 'cause sparse defines that one.
118 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
119 grep -v __SIZE_TYPE__ > $(check_defs))
120 check = sparse -include $(check_defs) -D__CHECKER__ \
121 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
123 # don't use FORTIFY with sparse because glibc with FORTIFY can
124 # generate so many sparse errors that sparse stops parsing,
125 # which masks real errors that we want to see.
129 CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
133 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
136 @$(check_echo) " [SP] $<"
137 $(Q)$(check) $(CFLAGS) $<
139 $(Q)$(CC) $(CFLAGS) -c $<
143 $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
145 all: $(progs) $(BUILDDIRS)
146 $(SUBDIRS): $(BUILDDIRS)
148 @echo "Making all in $(patsubst build-%,%,$@)"
149 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
151 test: btrfs btrfs-convert btrfs-image btrfs-corrupt-block
152 $(Q)for t in $(TESTS); do \
153 echo " [TEST] $$t"; \
154 bash tests/$$t || exit 1; \
158 # NOTE: For static compiles, you need to have all the required libs
159 # static equivalent available
161 static: $(progs_static)
163 version.h: version.sh version.h.in configure.ac
165 $(Q)bash ./config.status --silent $@
167 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
169 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
170 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
172 $(libs_static): $(libbtrfs_objects)
174 $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
178 $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so.0
179 $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so
181 # keep intermediate files from the below implicit rules around
182 .PRECIOUS: $(addsuffix .o,$(progs))
184 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
185 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
186 # turns them into a list of libraries to link against if they exist
188 # For static variants, use an extra $(subst) to get rid of the ".static"
189 # from the target name before translating to list of libs
191 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
193 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
194 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
195 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
197 btrfs-%: $(objects) $(libs) btrfs-%.o
199 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs) \
200 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
202 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
204 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
205 $(objects) $(libs) $(LDFLAGS) $(LIBS)
207 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
209 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
210 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
212 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
215 $(Q)$(LN_S) -f btrfs btrfsck
217 btrfsck.static: btrfs.static
221 mkfs.btrfs: $(objects) $(libs) mkfs.o
223 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs) mkfs.o $(LDFLAGS) $(LIBS)
225 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
227 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
228 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
230 btrfstune: $(objects) $(libs) btrfstune.o
232 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) $(libs) btrfstune.o $(LDFLAGS) $(LIBS)
234 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
236 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
237 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
239 dir-test: $(objects) $(libs) dir-test.o
241 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
243 quick-test: $(objects) $(libs) quick-test.o
245 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
247 ioctl-test: $(objects) $(libs) ioctl-test.o
249 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
251 send-test: $(objects) $(libs) send-test.o
253 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
255 library-test: $(libs_shared) library-test.o
257 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
259 library-test.static: $(libs_static) library-test.o
261 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
263 test-build: test-build-pre test-build-real
272 -$(MAKE) library-test.static
275 $(MAKE) -j 8 $(progs_extra)
278 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
281 clean-all: clean clean-doc clean-gen
285 $(Q)rm -f $(progs) cscope.out *.o *.o.d \
286 dir-test ioctl-test quick-test send-test library-test library-test-static \
287 btrfs.static mkfs.btrfs.static \
289 $(libs) $(lib_links) \
290 $(progs_static) $(progs_extra)
293 @echo "Cleaning Documentation"
294 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
297 @echo "Cleaning Generated Files"
298 $(Q)rm -rf version.h config.status config.cache connfig.log \
299 configure.lineno config.status.lineno Makefile \
300 config.log config.h config.h.in~ aclocal.m4 \
301 configure autom4te.cache/ config/
304 @echo "Cleaning $(patsubst clean-%,%,$@)"
305 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
307 install: $(libs) $(progs) $(INSTALLDIRS)
308 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
309 $(INSTALL) $(progs) $(DESTDIR)$(bindir)
310 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
311 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
312 $(LN_S) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
313 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
314 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
315 cp -a $(lib_links) $(DESTDIR)$(libdir)
316 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
317 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
319 install-static: $(progs_static) $(INSTALLDIRS)
320 for p in $(progs_static) ; do \
321 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/`basename $$p .static` ; \
325 @echo "Making install in $(patsubst install-%,%,$@)"
326 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
329 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
330 cd $(DESTDIR)$(incdir); rm -f $(headers)
331 rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
332 cd $(DESTDIR)$(libdir); rm -f $(lib_links) $(libs)
333 cd $(DESTDIR)$(bindir); rm -f btrfsck fsck.btrfs $(progs)
335 ifneq ($(MAKECMDGOALS),clean)
336 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))