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 -rdynamic
9 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
10 root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
11 extent-cache.o extent_io.o volumes.o utils.o repair.o \
12 qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
13 ulist.o qgroup-verify.o
14 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
15 cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
16 cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
17 cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
19 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
20 uuid-tree.o utils-lib.o
21 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
22 crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
23 extent_io.h ioctl.h ctree.h btrfsck.h version.h
24 TESTS = fsck-tests.sh convert-tests.sh
28 bindir = $(prefix)/bin
29 lib_LIBS = -luuid -lblkid -lm -lz -llzo2 -L.
30 libdir ?= $(prefix)/lib
31 incdir = $(prefix)/include/btrfs
32 LIBS = $(lib_LIBS) $(libs_static)
34 ifeq ("$(origin V)", "command line")
41 ifeq ($(BUILD_VERBOSE),1)
47 MAKEOPTS = --no-print-directory Q=$(Q)
49 progs = mkfs.btrfs btrfs-debug-tree btrfsck \
50 btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
51 btrfs-find-root btrfstune btrfs-show-super
53 progs_static = $(foreach p,$(progs),$(p).static)
55 # external libs required by various binaries; for btrfs-foo,
56 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
57 btrfs_convert_libs = -lext2fs -lcom_err
58 btrfs_image_libs = -lpthread
59 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
62 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
63 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
64 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
66 ifeq ($(DISABLE_BACKTRACE),1)
67 AM_CFLAGS += -DBTRFS_DISABLE_BACKTRACE
70 ifneq ($(DISABLE_DOCUMENTATION),1)
71 BUILDDIRS += build-Documentation
72 INSTALLDIRS += install-Documentation
77 .PHONY: $(INSTALLDIRS)
80 .PHONY: all install clean
82 # Create all the static targets
83 static_objects = $(patsubst %.o, %.static.o, $(objects))
84 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
85 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
87 # Define static compilation flags
88 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
89 STATIC_LDFLAGS = -static -Wl,--gc-sections
90 STATIC_LIBS = $(lib_LIBS) -lpthread
92 libs_shared = libbtrfs.so.0.1
93 libs_static = libbtrfs.a
94 libs = $(libs_shared) $(libs_static)
95 lib_links = libbtrfs.so.0 libbtrfs.so
96 headers = $(libbtrfs_headers)
98 # make C=1 to enable sparse
99 check_defs := .cc-defines.h
102 # We're trying to use sparse against glibc headers which go wild
103 # trying to use internal compiler macros to test features. We
104 # copy gcc's and give them to sparse. But not __SIZE_TYPE__
105 # 'cause sparse defines that one.
107 dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
108 grep -v __SIZE_TYPE__ > $(check_defs))
109 check = sparse -include $(check_defs) -D__CHECKER__ \
110 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
112 # don't use FORTIFY with sparse because glibc with FORTIFY can
113 # generate so many sparse errors that sparse stops parsing,
114 # which masks real errors that we want to see.
118 AM_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
122 $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(AM_CFLAGS) $(CFLAGS) $<
125 @$(check_echo) " [SP] $<"
126 $(Q)$(check) $(AM_CFLAGS) $(CFLAGS) $<
128 $(Q)$(CC) $(AM_CFLAGS) $(CFLAGS) -c $<
132 $(Q)$(CC) $(AM_CFLAGS) $(STATIC_CFLAGS) -c $< -o $@
134 all: $(progs) $(BUILDDIRS)
135 $(SUBDIRS): $(BUILDDIRS)
137 @echo "Making all in $(patsubst build-%,%,$@)"
138 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
141 $(Q)for t in $(TESTS); do \
142 echo " [TEST] $$t"; \
143 bash tests/$$t || exit 1; \
147 # NOTE: For static compiles, you need to have all the required libs
148 # static equivalent available
150 static: $(progs_static)
156 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
158 $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(lib_LIBS) \
159 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
161 $(libs_static): $(libbtrfs_objects)
163 $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
167 $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so.0
168 $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so
170 # keep intermediate files from the below implicit rules around
171 .PRECIOUS: $(addsuffix .o,$(progs))
173 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
174 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
175 # turns them into a list of libraries to link against if they exist
177 # For static variants, use an extra $(subst) to get rid of the ".static"
178 # from the target name before translating to list of libs
180 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
182 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
183 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
184 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
186 btrfs-%: $(objects) $(libs) btrfs-%.o
188 $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
190 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
192 $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
193 $(objects) $(LDFLAGS) $(LIBS) -lpthread
195 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
197 $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
198 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
200 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
203 $(Q)$(LN) -f btrfs btrfsck
205 btrfsck.static: btrfs.static
209 mkfs.btrfs: $(objects) $(libs) mkfs.o
211 $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS)
213 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
215 $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
216 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
218 btrfstune: $(objects) $(libs) btrfstune.o
220 $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS)
222 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
224 $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
225 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
227 dir-test: $(objects) $(libs) dir-test.o
229 $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) dir-test.o $(LDFLAGS) $(LIBS)
231 quick-test: $(objects) $(libs) quick-test.o
233 $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) quick-test.o $(LDFLAGS) $(LIBS)
235 ioctl-test: $(objects) $(libs) ioctl-test.o
237 $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS)
239 send-test: $(objects) $(libs) send-test.o
241 $(Q)$(CC) $(CFLAGS) -o send-test $(objects) send-test.o $(LDFLAGS) $(LIBS) -lpthread
244 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
246 clean-all: clean-doc clean
250 $(Q)rm -f $(progs) cscope.out *.o *.o.d btrfs-convert btrfs-image btrfs-select-super \
251 btrfs-zero-log btrfstune dir-test ioctl-test quick-test send-test btrfsck \
252 btrfs.static mkfs.btrfs.static btrfs-calc-size \
253 version.h $(check_defs) \
257 @echo "Cleaning Documentation"
258 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
261 @echo "Cleaning $(patsubst clean-%,%,$@)"
262 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
264 install: $(libs) $(progs) $(INSTALLDIRS)
265 $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
266 $(INSTALL) $(progs) $(DESTDIR)$(bindir)
267 $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
268 # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
269 $(LN) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
270 $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
271 $(INSTALL) $(libs) $(DESTDIR)$(libdir)
272 cp -a $(lib_links) $(DESTDIR)$(libdir)
273 $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
274 $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
276 install-static: $(progs_static) $(INSTALLDIRS)
277 for p in $(progs_static) ; do \
278 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/`basename $$p .static` ; \
282 @echo "Making install in $(patsubst install-%,%,$@)"
283 $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
286 $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
287 cd $(DESTDIR)$(incdir); rm -f $(headers)
288 rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
289 cd $(DESTDIR)$(libdir); rm -f $(lib_links) $(libs)
290 cd $(DESTDIR)$(bindir); rm -f btrfsck fsck.btrfs $(progs)
292 ifneq ($(MAKECMDGOALS),clean)
293 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))