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