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