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