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