btrfs-progs: autoconf: clean generated files, make version.h stuff more robust
[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
9 # Non-static compilation flags
10 CFLAGS = @CFLAGS@ \
11          -include config.h -Wall \
12          -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES \
13          -D_XOPEN_SOURCE=700  \
14          -fno-strict-aliasing -fPIC \
15          -rdynamic
16
17 LDFLAGS = @LDFLAGS@
18
19 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
20 LIBBTRFS_LIBS = $(LIBS)
21
22 # Static compilation flags
23 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
24 STATIC_LDFLAGS = -static -Wl,--gc-sections
25 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
26               @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
27
28 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
29           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
30           extent-cache.o extent_io.o volumes.o utils.o repair.o \
31           qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
32           ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
33           inode.o
34 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
35                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
36                cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
37                cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
38                cmds-property.o cmds-fi-disk_usage.o
39 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
40                    uuid-tree.o utils-lib.o rbtree-utils.o
41 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
42                crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
43                extent_io.h ioctl.h ctree.h btrfsck.h version.h
44 TESTS = fsck-tests.sh convert-tests.sh
45
46 prefix ?= @prefix@
47 bindir = @bindir@
48 libdir ?= @libdir@
49 incdir = @includedir@/btrfs
50
51 ifeq ("$(origin V)", "command line")
52   BUILD_VERBOSE = $(V)
53 endif
54 ifndef BUILD_VERBOSE
55   BUILD_VERBOSE = 0
56 endif
57
58 ifeq ($(BUILD_VERBOSE),1)
59   Q =
60 else
61   Q = @
62 endif
63
64 MAKEOPTS = --no-print-directory Q=$(Q)
65
66 progs = mkfs.btrfs btrfs-debug-tree btrfsck \
67         btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
68         btrfs-find-root btrfstune btrfs-show-super
69
70 progs_extra = btrfs-corrupt-block btrfs-fragments btrfs-calc-size \
71               btrfs-select-super
72
73 progs_static = $(foreach p,$(progs),$(p).static)
74
75 # external libs required by various binaries; for btrfs-foo,
76 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
77 btrfs_convert_libs = -lext2fs -lcom_err
78 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
79
80 SUBDIRS =
81 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
82 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
83 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
84
85 ifeq ($(DISABLE_BACKTRACE),1)
86 CFLAGS += -DBTRFS_DISABLE_BACKTRACE
87 endif
88
89 ifneq ($(DISABLE_DOCUMENTATION),1)
90 BUILDDIRS += build-Documentation
91 INSTALLDIRS += install-Documentation
92 endif
93
94 .PHONY: $(SUBDIRS)
95 .PHONY: $(BUILDDIRS)
96 .PHONY: $(INSTALLDIRS)
97 .PHONY: $(TESTDIRS)
98 .PHONY: $(CLEANDIRS)
99 .PHONY: all install clean
100
101 # Create all the static targets
102 static_objects = $(patsubst %.o, %.static.o, $(objects))
103 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
104 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
105
106 libs_shared = libbtrfs.so.0.1
107 libs_static = libbtrfs.a
108 libs = $(libs_shared) $(libs_static)
109 lib_links = libbtrfs.so.0 libbtrfs.so
110 headers = $(libbtrfs_headers)
111
112 # make C=1 to enable sparse
113 check_defs := .cc-defines.h 
114 ifdef C
115         #
116         # We're trying to use sparse against glibc headers which go wild
117         # trying to use internal compiler macros to test features.  We
118         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
119         # 'cause sparse defines that one.
120         #
121         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
122                         grep -v __SIZE_TYPE__ > $(check_defs))
123         check = sparse -include $(check_defs) -D__CHECKER__ \
124                 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
125         check_echo = echo
126         # don't use FORTIFY with sparse because glibc with FORTIFY can
127         # generate so many sparse errors that sparse stops parsing,
128         # which masks real errors that we want to see.
129 else
130         check = true
131         check_echo = true
132         CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
133 endif
134
135 %.o.d: %.c
136         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
137
138 .c.o:
139         @$(check_echo) "    [SP]     $<"
140         $(Q)$(check) $(CFLAGS) $<
141         @echo "    [CC]     $@"
142         $(Q)$(CC) $(CFLAGS) -c $<
143
144 %.static.o: %.c
145         @echo "    [CC]     $@"
146         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
147
148 all: $(progs) $(BUILDDIRS)
149 $(SUBDIRS): $(BUILDDIRS)
150 $(BUILDDIRS):
151         @echo "Making all in $(patsubst build-%,%,$@)"
152         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
153
154 test: btrfs btrfs-convert btrfs-image btrfs-corrupt-block
155         $(Q)for t in $(TESTS); do \
156                 echo "    [TEST]   $$t"; \
157                 bash tests/$$t || exit 1; \
158         done
159
160 #
161 # NOTE: For static compiles, you need to have all the required libs
162 #       static equivalent available
163 #
164 static: $(progs_static)
165
166 version.h: version.sh version.h.in configure.ac
167         @echo "    [SH]     $@"
168         $(Q)bash ./config.status --silent $@
169
170 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
171         @echo "    [LD]     $@"
172         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
173                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
174
175 $(libs_static): $(libbtrfs_objects)
176         @echo "    [AR]     $@"
177         $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
178
179 $(lib_links):
180         @echo "    [LN]     $@"
181         $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so.0
182         $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so
183
184 # keep intermediate files from the below implicit rules around
185 .PRECIOUS: $(addsuffix .o,$(progs))
186
187 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
188 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
189 # turns them into a list of libraries to link against if they exist
190 #
191 # For static variants, use an extra $(subst) to get rid of the ".static"
192 # from the target name before translating to list of libs
193
194 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
195         @echo "    [LD]     $@"
196         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
197                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
198                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
199
200 btrfs-%: $(objects) $(libs) btrfs-%.o
201         @echo "    [LD]     $@"
202         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs) \
203                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
204
205 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
206         @echo "    [LD]     $@"
207         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
208                 $(objects) $(libs) $(LDFLAGS) $(LIBS)
209
210 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
211         @echo "    [LD]     $@"
212         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
213                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
214
215 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
216 btrfsck: btrfs
217         @echo "    [LN]     $@"
218         $(Q)$(LN_S) -f btrfs btrfsck
219
220 btrfsck.static: btrfs.static
221         @echo "    [LN]     $@"
222         $(Q)$(LN_S) -f $^ $@
223
224 mkfs.btrfs: $(objects) $(libs) mkfs.o
225         @echo "    [LD]     $@"
226         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs) mkfs.o $(LDFLAGS) $(LIBS)
227
228 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
229         @echo "    [LD]     $@"
230         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
231                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
232
233 btrfstune: $(objects) $(libs) btrfstune.o
234         @echo "    [LD]     $@"
235         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) $(libs) btrfstune.o $(LDFLAGS) $(LIBS)
236
237 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
238         @echo "    [LD]     $@"
239         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
240                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
241
242 dir-test: $(objects) $(libs) dir-test.o
243         @echo "    [LD]     $@"
244         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
245
246 quick-test: $(objects) $(libs) quick-test.o
247         @echo "    [LD]     $@"
248         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
249
250 ioctl-test: $(objects) $(libs) ioctl-test.o
251         @echo "    [LD]     $@"
252         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
253
254 send-test: $(objects) $(libs) send-test.o
255         @echo "    [LD]     $@"
256         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
257
258 library-test: $(libs_shared) library-test.o
259         @echo "    [LD]     $@"
260         $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
261
262 library-test.static: $(libs_static) library-test.o
263         @echo "    [LD]     $@"
264         $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
265
266 test-build:
267         $(MAKE) clean-all
268         $(MAKE) library-test
269         -$(MAKE) library-test.static
270         $(MAKE) -j 8 all
271         -$(MAKE) -j 8 static
272         $(MAKE) -j 8 $(progs_extra)
273
274 manpages:
275         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
276
277
278 clean-all: clean clean-doc clean-gen
279
280 clean: $(CLEANDIRS)
281         @echo "Cleaning"
282         $(Q)rm -f $(progs) cscope.out *.o *.o.d \
283               dir-test ioctl-test quick-test send-test library-test library-test-static \
284               btrfs.static mkfs.btrfs.static \
285               $(check_defs) \
286               $(libs) $(lib_links) \
287               $(progs_static) $(progs_extra)
288
289 clean-doc:
290         @echo "Cleaning Documentation"
291         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
292
293 clean-gen:
294         @echo "Cleaning Generated Files"
295         $(Q)rm -f version.h config.status config.cache connfig.log \
296                 configure.lineno config.status.lineno Makefile
297
298 $(CLEANDIRS):
299         @echo "Cleaning $(patsubst clean-%,%,$@)"
300         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
301
302 install: $(libs) $(progs) $(INSTALLDIRS)
303         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
304         $(INSTALL) $(progs) $(DESTDIR)$(bindir)
305         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
306         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
307         $(LN_S) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
308         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
309         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
310         cp -a $(lib_links) $(DESTDIR)$(libdir)
311         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
312         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
313
314 install-static: $(progs_static) $(INSTALLDIRS)
315         for p in $(progs_static) ; do \
316                 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/`basename $$p .static` ; \
317         done
318
319 $(INSTALLDIRS):
320         @echo "Making install in $(patsubst install-%,%,$@)"
321         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
322
323 uninstall:
324         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
325         cd $(DESTDIR)$(incdir); rm -f $(headers)
326         rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
327         cd $(DESTDIR)$(libdir); rm -f $(lib_links) $(libs)
328         cd $(DESTDIR)$(bindir); rm -f btrfsck fsck.btrfs $(progs)
329
330 ifneq ($(MAKECMDGOALS),clean)
331 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
332 endif