btrfs-progs: build: add per-tool cflags variable
[platform/upstream/btrfs-progs.git] / Makefile.in
1 # btrfs-progs
2 #
3 # Basic build targets:
4 #   all         all main tools
5 #   static      build static bnaries, requires static version of the libraries
6 #   test        run the full testsuite
7 #   install     install to default location (/usr/local)
8 #   clean       clean built binaries (not the documentation)
9 #   clean-all   clean as above, clean docs and generated files
10 #
11 # Tuning by variables (environment or make arguments):
12 #   V=1            verbose, print command lines (default: quiet)
13 #   C=1            run checker before compilation (default checker: sparse)
14 #   D=1            debugging build, turn off optimizations
15 #   D=dflags       dtto, turn on additional debugging features:
16 #                  verbose - print file:line along with error/warning messages
17 #                  trace   - print trace before the error/warning messages
18 #                  abort   - call abort() on first error (dumps core)
19 #                  all     - shortcut for all of the above
20 #   W=123          build with warnings (default: off)
21 #   DEBUG_CFLAGS   additional compiler flags for debugging build
22 #   EXTRA_CFLAGS   additional compiler flags
23 #   EXTRA_LDFLAGS  additional linker flags
24 #
25 # Static checkers:
26 #   CHECKER        static checker binary to be called (default: sparse)
27 #   CHECKER_FLAGS  flags to pass to CHECKER, can override CFLAGS
28 #
29
30 # Export all variables to sub-makes by default
31 export
32
33 include Makefile.extrawarn
34
35 CC = @CC@
36 LN_S = @LN_S@
37 AR = @AR@
38 RM = @RM@
39 RMDIR = @RMDIR@
40 INSTALL = @INSTALL@
41 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
42 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
43
44 EXTRA_CFLAGS :=
45 EXTRA_LDFLAGS :=
46
47 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
48 DEBUG_CFLAGS_INTERNAL =
49 DEBUG_CFLAGS :=
50
51 # Common build flags
52 CFLAGS = @CFLAGS@ \
53          -include config.h \
54          -DBTRFS_FLAT_INCLUDES \
55          -D_XOPEN_SOURCE=700  \
56          -fno-strict-aliasing \
57          -fPIC \
58          $(EXTRAWARN_CFLAGS) \
59          $(DEBUG_CFLAGS_INTERNAL) \
60          $(EXTRA_CFLAGS)
61
62 LDFLAGS = @LDFLAGS@ \
63           -rdynamic $(EXTRA_LDFLAGS)
64
65 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
66 LIBBTRFS_LIBS = $(LIBS)
67
68 # Static compilation flags
69 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
70 STATIC_LDFLAGS = -static -Wl,--gc-sections
71 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
72               @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
73
74 # don't use FORTIFY with sparse because glibc with FORTIFY can
75 # generate so many sparse errors that sparse stops parsing,
76 # which masks real errors that we want to see.
77 CHECKER := sparse
78 check_defs := .cc-defines.h
79 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
80         -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
81         -U_FORTIFY_SOURCE
82
83 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
84           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
85           extent-cache.o extent_io.o volumes.o utils.o repair.o \
86           qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
87           ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
88           inode.o file.o find-root.o free-space-tree.o help.o
89 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
90                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
91                cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
92                cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
93                cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
94                cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o
95 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
96                    uuid-tree.o utils-lib.o rbtree-utils.o
97 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
98                crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
99                extent_io.h ioctl.h ctree.h btrfsck.h version.h
100 TESTS = fsck-tests.sh convert-tests.sh
101
102 udev_rules = 64-btrfs-dm.rules
103
104 prefix ?= @prefix@
105 exec_prefix = @exec_prefix@
106 bindir = @bindir@
107 libdir ?= @libdir@
108 incdir = @includedir@/btrfs
109 udevdir = @UDEVDIR@
110 udevruledir = ${udevdir}/rules.d
111
112 ifeq ("$(origin V)", "command line")
113   BUILD_VERBOSE = $(V)
114 endif
115 ifndef BUILD_VERBOSE
116   BUILD_VERBOSE = 0
117 endif
118
119 ifeq ($(BUILD_VERBOSE),1)
120   Q =
121 else
122   Q = @
123 endif
124
125 ifeq ("$(origin D)", "command line")
126   DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
127 endif
128
129 ifneq (,$(findstring verbose,$(D)))
130   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
131 endif
132
133 ifneq (,$(findstring trace,$(D)))
134   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
135 endif
136
137 ifneq (,$(findstring abort,$(D)))
138   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
139 endif
140
141 ifneq (,$(findstring all,$(D)))
142   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
143   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
144   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
145 endif
146
147 MAKEOPTS = --no-print-directory Q=$(Q)
148
149 # build all by default
150 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
151
152 # install only selected
153 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
154         btrfs-map-logical btrfs-image btrfs-zero-log \
155         btrfs-find-root btrfstune btrfs-show-super \
156         btrfs-select-super
157
158 progs_extra = btrfs-fragments
159
160 progs_static = $(foreach p,$(progs),$(p).static)
161
162 ifneq ($(DISABLE_BTRFSCONVERT),1)
163 progs_install += btrfs-convert
164 endif
165
166 # external libs required by various binaries; for btrfs-foo,
167 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
168 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
169 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
170 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
171 btrfs_show_super_objects = cmds-inspect-dump-super.o
172 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
173
174 # collect values of the variables above
175 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
176
177 SUBDIRS =
178 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
179 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
180 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
181
182 ifneq ($(DISABLE_DOCUMENTATION),1)
183 BUILDDIRS += build-Documentation
184 INSTALLDIRS += install-Documentation
185 endif
186
187 .PHONY: $(SUBDIRS)
188 .PHONY: $(BUILDDIRS)
189 .PHONY: $(INSTALLDIRS)
190 .PHONY: $(TESTDIRS)
191 .PHONY: $(CLEANDIRS)
192 .PHONY: all install clean
193
194 # Create all the static targets
195 static_objects = $(patsubst %.o, %.static.o, $(objects))
196 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
197 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
198
199 libs_shared = libbtrfs.so.0.1
200 libs_static = libbtrfs.a
201 libs = $(libs_shared) $(libs_static)
202 lib_links = libbtrfs.so.0 libbtrfs.so
203 headers = $(libbtrfs_headers)
204
205 # make C=1 to enable sparse
206 ifdef C
207         # We're trying to use sparse against glibc headers which go wild
208         # trying to use internal compiler macros to test features.  We
209         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
210         # 'cause sparse defines that one.
211         #
212         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
213                         grep -v __SIZE_TYPE__ > $(check_defs))
214         check = $(CHECKER)
215         check_echo = echo
216 else
217         check = true
218         check_echo = true
219 endif
220
221 %.o.d: %.c
222         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
223
224 #
225 # Pick from per-file variables, btrfs_*_cflags
226 #
227 .c.o:
228         @$(check_echo) "    [SP]     $<"
229         $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
230         @echo "    [CC]     $@"
231         $(Q)$(CC) $(CFLAGS) -c $< $($(subst -,_,$(@:%.o=%)-cflags))
232
233 %.static.o: %.c
234         @echo "    [CC]     $@"
235         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags))
236
237 all: $(progs) $(BUILDDIRS)
238 $(SUBDIRS): $(BUILDDIRS)
239 $(BUILDDIRS):
240         @echo "Making all in $(patsubst build-%,%,$@)"
241         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
242
243 test-convert: btrfs btrfs-convert
244         @echo "    [TEST]   convert-tests.sh"
245         $(Q)bash tests/convert-tests.sh
246
247 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
248         @echo "    [TEST]   fsck-tests.sh"
249         $(Q)bash tests/fsck-tests.sh
250
251 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
252         @echo "    [TEST]   misc-tests.sh"
253         $(Q)bash tests/misc-tests.sh
254
255 test-mkfs: btrfs mkfs.btrfs
256         @echo "    [TEST]   mkfs-tests.sh"
257         $(Q)bash tests/mkfs-tests.sh
258
259 test-fuzz: btrfs
260         @echo "    [TEST]   fuzz-tests.sh"
261         $(Q)bash tests/fuzz-tests.sh
262
263 test-cli: btrfs
264         @echo "    [TEST]   cli-tests.sh"
265         $(Q)bash tests/cli-tests.sh
266
267 test-clean:
268         @echo "Cleaning tests"
269         $(Q)bash tests/clean-tests.sh
270
271 test-inst: all
272         @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
273                 echo "Test installation to $$tmpdest" && \
274                 $(MAKE) DESTDIR=$$tmpdest install && \
275                 $(RM) -rf -- $$tmpdest
276
277 test: test-fsck test-mkfs test-convert test-misc test-fuzz
278
279 #
280 # NOTE: For static compiles, you need to have all the required libs
281 #       static equivalent available
282 #
283 static: $(progs_static)
284
285 version.h: version.sh version.h.in configure.ac
286         @echo "    [SH]     $@"
287         $(Q)bash ./config.status --silent $@
288
289 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
290         @echo "    [LD]     $@"
291         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
292                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
293
294 $(libs_static): $(libbtrfs_objects)
295         @echo "    [AR]     $@"
296         $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
297
298 $(lib_links):
299         @echo "    [LN]     $@"
300         $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
301
302 # keep intermediate files from the below implicit rules around
303 .PRECIOUS: $(addsuffix .o,$(progs))
304
305 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
306 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
307 # turns them into a list of libraries to link against if they exist
308 #
309 # For static variants, use an extra $(subst) to get rid of the ".static"
310 # from the target name before translating to list of libs
311
312 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects) $(patsubst %.o,%.static.o,$(standalone_deps))
313         @echo "    [LD]     $@"
314         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
315                 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
316                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
317                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
318
319 btrfs-%: $(objects) $(libs_static) btrfs-%.o $(standalone_deps)
320         @echo "    [LD]     $@"
321         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
322                 $($(subst -,_,$@-objects)) \
323                 $(libs_static) \
324                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
325
326 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
327         @echo "    [LD]     $@"
328         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
329                 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
330
331 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
332         @echo "    [LD]     $@"
333         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
334                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
335
336 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
337 btrfsck: btrfs
338         @echo "    [LN]     $@"
339         $(Q)$(LN_S) -f btrfs btrfsck
340
341 btrfsck.static: btrfs.static
342         @echo "    [LN]     $@"
343         $(Q)$(LN_S) -f $^ $@
344
345 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
346         @echo "    [LD]     $@"
347         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
348
349 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
350         @echo "    [LD]     $@"
351         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
352                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
353
354 btrfstune: $(objects) $(libs_static) btrfstune.o
355         @echo "    [LD]     $@"
356         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
357
358 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
359         @echo "    [LD]     $@"
360         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
361                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
362
363 dir-test: $(objects) $(libs) dir-test.o
364         @echo "    [LD]     $@"
365         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
366
367 quick-test: $(objects) $(libs) quick-test.o
368         @echo "    [LD]     $@"
369         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
370
371 ioctl-test: $(objects) $(libs) ioctl-test.o
372         @echo "    [LD]     $@"
373         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
374
375 send-test: $(objects) $(libs) send-test.o
376         @echo "    [LD]     $@"
377         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
378
379 library-test: $(libs_shared) library-test.o
380         @echo "    [LD]     $@"
381         $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
382
383 library-test.static: $(libs_static) library-test.o
384         @echo "    [LD]     $@"
385         $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
386
387 test-build: test-build-pre test-build-real
388
389 test-build-pre:
390         $(MAKE) clean-all
391         ./autogen.sh
392         ./configure
393
394 test-build-real:
395         $(MAKE) library-test
396         -$(MAKE) library-test.static
397         $(MAKE) -j 8 all
398         -$(MAKE) -j 8 static
399         $(MAKE) -j 8 $(progs_extra)
400
401 manpages:
402         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
403
404
405 clean-all: clean clean-doc clean-gen
406
407 clean: $(CLEANDIRS)
408         @echo "Cleaning"
409         $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
410               dir-test ioctl-test quick-test send-test library-test library-test-static \
411               btrfs.static mkfs.btrfs.static \
412               $(check_defs) \
413               $(libs) $(lib_links) \
414               $(progs_static) $(progs_extra)
415
416 clean-doc:
417         @echo "Cleaning Documentation"
418         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
419
420 clean-gen:
421         @echo "Cleaning Generated Files"
422         $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
423                 configure.lineno config.status.lineno Makefile \
424                 Documentation/Makefile \
425                 config.log config.h config.h.in~ aclocal.m4 \
426                 configure autom4te.cache/ config/
427
428 $(CLEANDIRS):
429         @echo "Cleaning $(patsubst clean-%,%,$@)"
430         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
431
432 install: $(libs) $(progs_install) $(INSTALLDIRS)
433         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
434         $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
435         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
436         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
437         $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
438         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
439         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
440         cp -a $(lib_links) $(DESTDIR)$(libdir)
441         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
442         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
443 ifneq ($(udevdir),)
444         $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
445         $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
446 endif
447
448 install-static: $(progs_static) $(INSTALLDIRS)
449         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
450         $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
451         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
452         $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
453
454 $(INSTALLDIRS):
455         @echo "Making install in $(patsubst install-%,%,$@)"
456         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
457
458 uninstall:
459         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
460         cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
461         $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
462         cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
463         cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
464
465 ifneq ($(MAKECMDGOALS),clean)
466 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
467 endif