28243dee6ea99f39d4f23f644f8df5e95c5a5520
[platform/upstream/btrfs-progs.git] / Makefile
1 # Export all variables to sub-makes by default
2 export
3
4 CC = gcc
5 LN = ln
6 AR = ar
7 AM_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES -fno-strict-aliasing -fPIC
8 CFLAGS = -g -O1 -fno-strict-aliasing
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 \
18                cmds-property.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
25
26 INSTALL = install
27 prefix ?= /usr/local
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)
33
34 ifeq ("$(origin V)", "command line")
35   BUILD_VERBOSE = $(V)
36 endif
37 ifndef BUILD_VERBOSE
38   BUILD_VERBOSE = 0
39 endif
40
41 ifeq ($(BUILD_VERBOSE),1)
42   Q =
43 else
44   Q = @
45 endif
46
47 MAKEOPTS = --no-print-directory Q=$(Q)
48
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
52
53 # external libs required by various binaries; for btrfs-foo,
54 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
55 btrfs_convert_libs = -lext2fs -lcom_err
56 btrfs_image_libs = -lpthread
57 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
58
59 SUBDIRS =
60 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
61 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
62 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
63
64 ifneq ($(DISABLE_DOCUMENTATION),1)
65 BUILDDIRS += build-Documentation
66 INSTALLDIRS += install-Documentation
67 endif
68
69 .PHONY: $(SUBDIRS)
70 .PHONY: $(BUILDDIRS)
71 .PHONY: $(INSTALLDIRS)
72 .PHONY: $(TESTDIRS)
73 .PHONY: $(CLEANDIRS)
74 .PHONY: all install clean
75
76 # Create all the static targets
77 static_objects = $(patsubst %.o, %.static.o, $(objects))
78 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
79 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
80
81 # Define static compilation flags
82 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
83 STATIC_LDFLAGS = -static -Wl,--gc-sections
84 STATIC_LIBS = $(lib_LIBS) -lpthread
85
86 libs_shared = libbtrfs.so.0.1
87 libs_static = libbtrfs.a
88 libs = $(libs_shared) $(libs_static)
89 lib_links = libbtrfs.so.0 libbtrfs.so
90 headers = $(libbtrfs_headers)
91
92 # make C=1 to enable sparse
93 check_defs := .cc-defines.h 
94 ifdef C
95         #
96         # We're trying to use sparse against glibc headers which go wild
97         # trying to use internal compiler macros to test features.  We
98         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
99         # 'cause sparse defines that one.
100         #
101         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
102                         grep -v __SIZE_TYPE__ > $(check_defs))
103         check = sparse -include $(check_defs) -D__CHECKER__ \
104                 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
105         check_echo = echo
106         # don't use FORTIFY with sparse because glibc with FORTIFY can
107         # generate so many sparse errors that sparse stops parsing,
108         # which masks real errors that we want to see.
109 else
110         check = true
111         check_echo = true
112         AM_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
113 endif
114
115 %.o.d: %.c
116         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(AM_CFLAGS) $(CFLAGS) $<
117
118 .c.o:
119         @$(check_echo) "    [SP]     $<"
120         $(Q)$(check) $(AM_CFLAGS) $(CFLAGS) $<
121         @echo "    [CC]     $@"
122         $(Q)$(CC) $(AM_CFLAGS) $(CFLAGS) -c $<
123
124 %.static.o: %.c
125         @echo "    [CC]     $@"
126         $(Q)$(CC) $(AM_CFLAGS) $(STATIC_CFLAGS) -c $< -o $@
127
128 all: $(progs) $(BUILDDIRS)
129 $(SUBDIRS): $(BUILDDIRS)
130 $(BUILDDIRS):
131         @echo "Making all in $(patsubst build-%,%,$@)"
132         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
133
134 test:
135         $(Q)for t in $(TESTS); do \
136                 echo "     [TEST]    $$t"; \
137                 bash tests/$$t || exit 1; \
138         done
139
140 #
141 # NOTE: For static compiles, you need to have all the required libs
142 #       static equivalent available
143 #
144 static: btrfs.static mkfs.btrfs.static btrfs-find-root.static
145
146 version.h:
147         @echo "    [SH]     $@"
148         $(Q)bash version.sh
149
150 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
151         @echo "    [LD]     $@"
152         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(lib_LIBS) \
153                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
154
155 $(libs_static): $(libbtrfs_objects)
156         @echo "    [AR]     $@"
157         $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
158
159 $(lib_links):
160         @echo "    [LN]     $@"
161         $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so.0
162         $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so
163
164 # keep intermediate files from the below implicit rules around
165 .PRECIOUS: $(addsuffix .o,$(progs))
166
167 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
168 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
169 # turns them into a list of libraries to link against if they exist
170 #
171 # For static variants, use an extra $(subst) to get rid of the ".static"
172 # from the target name before translating to list of libs
173
174 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
175         @echo "    [LD]     $@"
176         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
177                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS) \
178                 $($(subst -,_,$(subst .static,,$@)-libs))
179
180 btrfs-%: $(objects) $(libs) btrfs-%.o
181         @echo "    [LD]     $@"
182         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
183
184 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
185         @echo "    [LD]     $@"
186         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
187                 $(objects) $(LDFLAGS) $(LIBS) -lpthread
188
189 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
190         @echo "    [LD]     $@"
191         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
192                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
193
194 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
195 btrfsck: btrfs
196         @echo "    [LN]     $@"
197         $(Q)$(LN) -f btrfs btrfsck
198
199 mkfs.btrfs: $(objects) $(libs) mkfs.o
200         @echo "    [LD]     $@"
201         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS)
202
203 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
204         @echo "    [LD]     $@"
205         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
206                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
207
208 btrfstune: $(objects) $(libs) btrfstune.o
209         @echo "    [LD]     $@"
210         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS)
211
212 dir-test: $(objects) $(libs) dir-test.o
213         @echo "    [LD]     $@"
214         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) dir-test.o $(LDFLAGS) $(LIBS)
215
216 quick-test: $(objects) $(libs) quick-test.o
217         @echo "    [LD]     $@"
218         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) quick-test.o $(LDFLAGS) $(LIBS)
219
220 ioctl-test: $(objects) $(libs) ioctl-test.o
221         @echo "    [LD]     $@"
222         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS)
223
224 send-test: $(objects) $(libs) send-test.o
225         @echo "    [LD]     $@"
226         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) send-test.o $(LDFLAGS) $(LIBS) -lpthread
227
228 manpages:
229         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
230
231 clean-all: clean-doc clean
232
233 clean: $(CLEANDIRS)
234         @echo "Cleaning"
235         $(Q)rm -f $(progs) cscope.out *.o *.o.d btrfs-convert btrfs-image btrfs-select-super \
236               btrfs-zero-log btrfstune dir-test ioctl-test quick-test send-test btrfsck \
237               btrfs.static mkfs.btrfs.static btrfs-calc-size \
238               version.h $(check_defs) \
239               $(libs) $(lib_links)
240
241 clean-doc:
242         @echo "Cleaning Documentation"
243         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
244
245 $(CLEANDIRS):
246         @echo "Cleaning $(patsubst clean-%,%,$@)"
247         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
248
249 install: $(libs) $(progs) $(INSTALLDIRS)
250         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
251         $(INSTALL) $(progs) $(DESTDIR)$(bindir)
252         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
253         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
254         $(LN) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
255         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
256         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
257         cp -a $(lib_links) $(DESTDIR)$(libdir)
258         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
259         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
260
261 $(INSTALLDIRS):
262         @echo "Making install in $(patsubst install-%,%,$@)"
263         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
264
265 uninstall:
266         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
267         cd $(DESTDIR)$(incdir); rm -f $(headers)
268         rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
269         cd $(DESTDIR)$(libdir); rm -f $(lib_links) $(libs)
270         cd $(DESTDIR)$(bindir); rm -f btrfsck fsck.btrfs $(progs)
271
272 ifneq ($(MAKECMDGOALS),clean)
273 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
274 endif