btrfs-progs: build, make build test work again
[platform/upstream/btrfs-progs.git] / Makefile.in
1 # Export all variables to sub-makes by default
2 export
3
4 CC = @CC@
5 LN_S = @LN_S@
6 AR = @AR@
7 INSTALL = @INSTALL@
8 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
9
10 # Non-static compilation flags
11 CFLAGS = @CFLAGS@ \
12          -include config.h -Wall \
13          -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES \
14          -D_XOPEN_SOURCE=700  \
15          -fno-strict-aliasing -fPIC \
16          -rdynamic
17
18 LDFLAGS = @LDFLAGS@
19
20 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
21 LIBBTRFS_LIBS = $(LIBS)
22
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
28
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 \
34           inode.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
46
47 prefix ?= @prefix@
48 bindir = @bindir@
49 libdir ?= @libdir@
50 incdir = @includedir@/btrfs
51
52 ifeq ("$(origin V)", "command line")
53   BUILD_VERBOSE = $(V)
54 endif
55 ifndef BUILD_VERBOSE
56   BUILD_VERBOSE = 0
57 endif
58
59 ifeq ($(BUILD_VERBOSE),1)
60   Q =
61 else
62   Q = @
63 endif
64
65 MAKEOPTS = --no-print-directory Q=$(Q)
66
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
70
71 progs_extra = btrfs-corrupt-block btrfs-fragments btrfs-calc-size \
72               btrfs-select-super
73
74 progs_static = $(foreach p,$(progs),$(p).static)
75
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
80
81 SUBDIRS =
82 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
83 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
84 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
85
86 ifneq ($(DISABLE_DOCUMENTATION),1)
87 BUILDDIRS += build-Documentation
88 INSTALLDIRS += install-Documentation
89 endif
90
91 .PHONY: $(SUBDIRS)
92 .PHONY: $(BUILDDIRS)
93 .PHONY: $(INSTALLDIRS)
94 .PHONY: $(TESTDIRS)
95 .PHONY: $(CLEANDIRS)
96 .PHONY: all install clean
97
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))
102
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)
108
109 # make C=1 to enable sparse
110 check_defs := .cc-defines.h 
111 ifdef C
112         #
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.
117         #
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
122         check_echo = echo
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.
126 else
127         check = true
128         check_echo = true
129         CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
130 endif
131
132 %.o.d: %.c
133         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
134
135 .c.o:
136         @$(check_echo) "    [SP]     $<"
137         $(Q)$(check) $(CFLAGS) $<
138         @echo "    [CC]     $@"
139         $(Q)$(CC) $(CFLAGS) -c $<
140
141 %.static.o: %.c
142         @echo "    [CC]     $@"
143         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
144
145 all: $(progs) $(BUILDDIRS)
146 $(SUBDIRS): $(BUILDDIRS)
147 $(BUILDDIRS):
148         @echo "Making all in $(patsubst build-%,%,$@)"
149         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
150
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; \
155         done
156
157 #
158 # NOTE: For static compiles, you need to have all the required libs
159 #       static equivalent available
160 #
161 static: $(progs_static)
162
163 version.h: version.sh version.h.in configure.ac
164         @echo "    [SH]     $@"
165         $(Q)bash ./config.status --silent $@
166
167 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
168         @echo "    [LD]     $@"
169         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
170                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
171
172 $(libs_static): $(libbtrfs_objects)
173         @echo "    [AR]     $@"
174         $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
175
176 $(lib_links):
177         @echo "    [LN]     $@"
178         $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so.0
179         $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so
180
181 # keep intermediate files from the below implicit rules around
182 .PRECIOUS: $(addsuffix .o,$(progs))
183
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
187 #
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
190
191 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
192         @echo "    [LD]     $@"
193         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
194                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
195                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
196
197 btrfs-%: $(objects) $(libs) btrfs-%.o
198         @echo "    [LD]     $@"
199         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs) \
200                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
201
202 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
203         @echo "    [LD]     $@"
204         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
205                 $(objects) $(libs) $(LDFLAGS) $(LIBS)
206
207 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
208         @echo "    [LD]     $@"
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)
211
212 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
213 btrfsck: btrfs
214         @echo "    [LN]     $@"
215         $(Q)$(LN_S) -f btrfs btrfsck
216
217 btrfsck.static: btrfs.static
218         @echo "    [LN]     $@"
219         $(Q)$(LN_S) -f $^ $@
220
221 mkfs.btrfs: $(objects) $(libs) mkfs.o
222         @echo "    [LD]     $@"
223         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs) mkfs.o $(LDFLAGS) $(LIBS)
224
225 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
226         @echo "    [LD]     $@"
227         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
228                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
229
230 btrfstune: $(objects) $(libs) btrfstune.o
231         @echo "    [LD]     $@"
232         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) $(libs) btrfstune.o $(LDFLAGS) $(LIBS)
233
234 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
235         @echo "    [LD]     $@"
236         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
237                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
238
239 dir-test: $(objects) $(libs) dir-test.o
240         @echo "    [LD]     $@"
241         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
242
243 quick-test: $(objects) $(libs) quick-test.o
244         @echo "    [LD]     $@"
245         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
246
247 ioctl-test: $(objects) $(libs) ioctl-test.o
248         @echo "    [LD]     $@"
249         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
250
251 send-test: $(objects) $(libs) send-test.o
252         @echo "    [LD]     $@"
253         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
254
255 library-test: $(libs_shared) library-test.o
256         @echo "    [LD]     $@"
257         $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
258
259 library-test.static: $(libs_static) library-test.o
260         @echo "    [LD]     $@"
261         $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
262
263 test-build: test-build-pre test-build-real
264
265 test-build-pre:
266         $(MAKE) clean-all
267         ./autogen.sh
268         ./configure
269
270 test-build-real:
271         $(MAKE) library-test
272         -$(MAKE) library-test.static
273         $(MAKE) -j 8 all
274         -$(MAKE) -j 8 static
275         $(MAKE) -j 8 $(progs_extra)
276
277 manpages:
278         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
279
280
281 clean-all: clean clean-doc clean-gen
282
283 clean: $(CLEANDIRS)
284         @echo "Cleaning"
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 \
288               $(check_defs) \
289               $(libs) $(lib_links) \
290               $(progs_static) $(progs_extra)
291
292 clean-doc:
293         @echo "Cleaning Documentation"
294         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
295
296 clean-gen:
297         @echo "Cleaning Generated Files"
298         $(Q)rm -f version.h config.status config.cache connfig.log \
299                 configure.lineno config.status.lineno Makefile
300
301 $(CLEANDIRS):
302         @echo "Cleaning $(patsubst clean-%,%,$@)"
303         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
304
305 install: $(libs) $(progs) $(INSTALLDIRS)
306         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
307         $(INSTALL) $(progs) $(DESTDIR)$(bindir)
308         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
309         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
310         $(LN_S) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
311         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
312         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
313         cp -a $(lib_links) $(DESTDIR)$(libdir)
314         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
315         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
316
317 install-static: $(progs_static) $(INSTALLDIRS)
318         for p in $(progs_static) ; do \
319                 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/`basename $$p .static` ; \
320         done
321
322 $(INSTALLDIRS):
323         @echo "Making install in $(patsubst install-%,%,$@)"
324         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
325
326 uninstall:
327         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
328         cd $(DESTDIR)$(incdir); rm -f $(headers)
329         rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
330         cd $(DESTDIR)$(libdir); rm -f $(lib_links) $(libs)
331         cd $(DESTDIR)$(bindir); rm -f btrfsck fsck.btrfs $(progs)
332
333 ifneq ($(MAKECMDGOALS),clean)
334 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
335 endif