1 # Export all variables to sub-makes by default
7 AM_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES -fno-strict-aliasing -fPIC
8 CFLAGS = -g -O1 -fno-strict-aliasing
10 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
11 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
12 extent-cache.o extent_io.o volumes.o utils.o repair.o \
13 qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
14 ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
16 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
17 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
18 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
19 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
20 cmds-property.o cmds-fi-disk_usage.o
21 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
22 uuid-tree.o utils-lib.o rbtree-utils.o
23 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
24 crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
25 extent_io.h ioctl.h ctree.h btrfsck.h version.h
26 TESTS = fsck-tests.sh convert-tests.sh
30 bindir = $(prefix)/bin
31 lib_LIBS = -luuid -lblkid -lz -llzo2 -L. -pthread
32 libdir ?= $(prefix)/lib
33 incdir = $(prefix)/include/btrfs
34 LIBS = $(lib_LIBS) $(libs_static)
36 ifeq ("$(origin V)", "command line")
43 ifeq ($(BUILD_VERBOSE),1)
49 MAKEOPTS = --no-print-directory Q=$(Q)
51 progs = mkfs.btrfs btrfs-debug-tree btrfsck \
52 btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
53 btrfs-find-root btrfstune btrfs-show-super
55 progs_extra = btrfs-corrupt-block btrfs-fragments btrfs-calc-size \
58 progs_static = $(foreach p,$(progs),$(p).static)
60 # external libs required by various binaries; for btrfs-foo,
61 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
62 btrfs_convert_libs = -lext2fs -lcom_err
63 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
66 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
67 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
68 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
70 ifeq ($(DISABLE_BACKTRACE),1)
71 AM_CFLAGS += -DBTRFS_DISABLE_BACKTRACE
74 ifneq ($(DISABLE_DOCUMENTATION),1)
75 BUILDDIRS += build-Documentation
76 INSTALLDIRS += install-Documentation
81 .PHONY: $(INSTALLDIRS)
84 .PHONY: all install clean
86 # Create all the static targets
87 static_objects = $(patsubst %.o, %.static.o, $(objects))
88 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
89 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
91 # Define static compilation flags
92 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
93 STATIC_LDFLAGS = -static -Wl,--gc-sections
94 STATIC_LIBS = $(lib_LIBS)
96 libs_shared = libbtrfs.so.0.1
97 libs_static = libbtrfs.a
98 libs = $(libs_shared) $(libs_static)
99 lib_links = libbtrfs.so.0 libbtrfs.so
100 headers = $(libbtrfs_headers)
102 # make C=1 to enable sparse
103 check_defs := .cc-defines.h
106 # We're trying to use sparse against glibc headers which go wild
107 # trying to use internal compiler macros to test features. We
108 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
109 # 'cause sparse defines that one.
111 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
112 grep -v __SIZE_TYPE__ > $(check_defs))
113 check = sparse -include $(check_defs) -D__CHECKER__ \
114 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
116 # don't use FORTIFY with sparse because glibc with FORTIFY can
117 # generate so many sparse errors that sparse stops parsing,
118 # which masks real errors that we want to see.
122 AM_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
126 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(AM_CFLAGS) $(CFLAGS) $<
129 @$(check_echo) " [SP] $<"
130 $(Q)$(check) $(AM_CFLAGS) $(CFLAGS) $<
132 $(Q)$(CC) $(AM_CFLAGS) $(CFLAGS) -c $<
136 $(Q)$(CC) $(AM_CFLAGS) $(STATIC_CFLAGS) -c $< -o $@
138 all: $(progs) $(BUILDDIRS)
139 $(SUBDIRS): $(BUILDDIRS)
141 @echo "Making all in $(patsubst build-%,%,$@)"
142 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
144 test: btrfs btrfs-convert btrfs-image btrfs-corrupt-block
145 $(Q)for t in $(TESTS); do \
146 echo " [TEST] $$t"; \
147 bash tests/$$t || exit 1; \
151 # NOTE: For static compiles, you need to have all the required libs
152 # static equivalent available
154 static: $(progs_static)
160 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
162 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(lib_LIBS) \
163 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
165 $(libs_static): $(libbtrfs_objects)
167 $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
171 $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so.0
172 $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so
174 # keep intermediate files from the below implicit rules around
175 .PRECIOUS: $(addsuffix .o,$(progs))
177 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
178 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
179 # turns them into a list of libraries to link against if they exist
181 # For static variants, use an extra $(subst) to get rid of the ".static"
182 # from the target name before translating to list of libs
184 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
186 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
187 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
188 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
190 btrfs-%: $(objects) $(libs) btrfs-%.o
192 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
194 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
196 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
197 $(objects) $(LDFLAGS) $(LIBS)
199 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
201 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
202 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
204 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
207 $(Q)$(LN) -f btrfs btrfsck
209 btrfsck.static: btrfs.static
213 mkfs.btrfs: $(objects) $(libs) mkfs.o
215 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS)
217 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
219 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
220 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
222 btrfstune: $(objects) $(libs) btrfstune.o
224 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS)
226 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
228 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
229 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
231 dir-test: $(objects) $(libs) dir-test.o
233 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) dir-test.o $(LDFLAGS) $(LIBS)
235 quick-test: $(objects) $(libs) quick-test.o
237 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) quick-test.o $(LDFLAGS) $(LIBS)
239 ioctl-test: $(objects) $(libs) ioctl-test.o
241 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS)
243 send-test: $(objects) $(libs) send-test.o
245 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) send-test.o $(LDFLAGS) $(LIBS)
247 library-test: $(libs_shared) library-test.o
249 $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
251 library-test.static: $(libs_static) library-test.o
253 $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
258 -$(MAKE) library-test.static
261 $(MAKE) -j 8 $(progs_extra)
264 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
266 clean-all: clean-doc clean
270 $(Q)rm -f $(progs) cscope.out *.o *.o.d \
271 dir-test ioctl-test quick-test send-test library-test library-test-static \
272 btrfs.static mkfs.btrfs.static \
273 version.h $(check_defs) \
274 $(libs) $(lib_links) \
275 $(progs_static) $(progs_extra)
278 @echo "Cleaning Documentation"
279 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
282 @echo "Cleaning $(patsubst clean-%,%,$@)"
283 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
285 install: $(libs) $(progs) $(INSTALLDIRS)
286 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
287 $(INSTALL) $(progs) $(DESTDIR)$(bindir)
288 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
289 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
290 $(LN) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
291 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
292 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
293 cp -a $(lib_links) $(DESTDIR)$(libdir)
294 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
295 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
297 install-static: $(progs_static) $(INSTALLDIRS)
298 for p in $(progs_static) ; do \
299 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/`basename $$p .static` ; \
303 @echo "Making install in $(patsubst install-%,%,$@)"
304 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
307 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
308 cd $(DESTDIR)$(incdir); rm -f $(headers)
309 rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
310 cd $(DESTDIR)$(libdir); rm -f $(lib_links) $(libs)
311 cd $(DESTDIR)$(bindir); rm -f btrfsck fsck.btrfs $(progs)
313 ifneq ($(MAKECMDGOALS),clean)
314 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))