btrfs-progs: Move chunk stripe size calculation function to volumes.h
[platform/upstream/btrfs-progs.git] / Makefile
1 #
2 # Basic build targets:
3 #   all         all main tools and the shared library
4 #   static      build static bnaries, requires static version of the libraries
5 #   test        run the full testsuite
6 #   install     install to default location (/usr/local)
7 #   clean       clean built binaries (not the documentation)
8 #   clean-all   clean as above, clean docs and generated files
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 #   D=dflags       dtto, turn on additional debugging features:
15 #                  verbose - print file:line along with error/warning messages
16 #                  trace   - print trace before the error/warning messages
17 #                  abort   - call abort() on first error (dumps core)
18 #                  all     - shortcut for all of the above
19 #                  asan    - enable address sanitizer compiler feature
20 #                  tsan    - enable thread sanitizer compiler feature
21 #                  ubsan   - undefined behaviour sanitizer compiler feature
22 #                  bcheck  - extended build checks
23 #   W=123          build with warnings (default: off)
24 #   DEBUG_CFLAGS   additional compiler flags for debugging build
25 #   EXTRA_CFLAGS   additional compiler flags
26 #   EXTRA_LDFLAGS  additional linker flags
27 #
28 # Testing-specific options (see also tests/README.md):
29 #   TEST=GLOB      run test(s) from directories matching GLOB
30 #   TEST_LOG=tty   print name of a command run via the execution helpers
31 #   TEST_LOG=dump  dump testing log file when a test fails
32 #
33 # Static checkers:
34 #   CHECKER        static checker binary to be called (default: sparse)
35 #   CHECKER_FLAGS  flags to pass to CHECKER, can override CFLAGS
36 #
37
38 # Export all variables to sub-makes by default
39 export
40
41 -include Makefile.inc
42 ifneq ($(MAKEFILE_INC_INCLUDED),yes)
43 $(error Makefile.inc not generated, please configure first)
44 endif
45
46 TAGS_CMD := ctags
47 CSCOPE_CMD := cscope -u -b -c -q
48
49 include Makefile.extrawarn
50
51 EXTRA_CFLAGS :=
52 EXTRA_LDFLAGS :=
53
54 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
55 DEBUG_CFLAGS_INTERNAL =
56 DEBUG_CFLAGS :=
57
58 DEBUG_LDFLAGS_DEFAULT =
59 DEBUG_LDFLAGS_INTERNAL =
60 DEBUG_LDFLAGS :=
61
62 ABSTOPDIR = $(shell pwd)
63 TOPDIR := .
64
65 # Common build flags
66 CSTD = -std=gnu90
67 CFLAGS = $(SUBST_CFLAGS) \
68          $(CSTD) \
69          -include config.h \
70          -DBTRFS_FLAT_INCLUDES \
71          -D_XOPEN_SOURCE=700  \
72          -fno-strict-aliasing \
73          -fPIC \
74          -I$(TOPDIR) \
75          -I$(TOPDIR)/kernel-lib \
76          -I$(TOPDIR)/libbtrfsutil \
77          $(EXTRAWARN_CFLAGS) \
78          $(DEBUG_CFLAGS_INTERNAL) \
79          $(EXTRA_CFLAGS)
80
81 LIBBTRFSUTIL_CFLAGS = $(SUBST_CFLAGS) \
82                       $(CSTD) \
83                       -D_GNU_SOURCE \
84                       -fPIC \
85                       -fvisibility=hidden \
86                       -I$(TOPDIR)/libbtrfsutil \
87                       $(EXTRAWARN_CFLAGS) \
88                       $(EXTRA_CFLAGS)
89
90 LDFLAGS = $(SUBST_LDFLAGS) \
91           -rdynamic -L$(TOPDIR) \
92           $(DEBUG_LDFLAGS_INTERNAL) \
93           $(EXTRA_LDFLAGS)
94
95 LIBS = $(LIBS_BASE)
96 LIBBTRFS_LIBS = $(LIBS_BASE)
97
98 # Static compilation flags
99 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
100 STATIC_LDFLAGS = -static -Wl,--gc-sections
101 STATIC_LIBS = $(STATIC_LIBS_BASE)
102
103 # don't use FORTIFY with sparse because glibc with FORTIFY can
104 # generate so many sparse errors that sparse stops parsing,
105 # which masks real errors that we want to see.
106 # Note: additional flags might get added per-target later
107 CHECKER := sparse
108 check_defs := .cc-defines.h
109 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
110         -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
111         -U_FORTIFY_SOURCE -Wdeclaration-after-statement -Wdefault-bitfield-sign
112
113 objects = ctree.o disk-io.o kernel-lib/radix-tree.o extent-tree.o print-tree.o \
114           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
115           extent-cache.o extent_io.o volumes.o utils.o repair.o \
116           qgroup.o free-space-cache.o kernel-lib/list_sort.o props.o \
117           kernel-shared/ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
118           inode.o file.o find-root.o free-space-tree.o help.o send-dump.o \
119           fsfeatures.o kernel-lib/tables.o kernel-lib/raid56.o transaction.o
120 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
121                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
122                cmds-quota.o cmds-qgroup.o cmds-replace.o check/main.o \
123                cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
124                cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
125                cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o \
126                mkfs/common.o check/mode-common.o check/mode-lowmem.o
127 libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
128                    kernel-lib/crc32c.o messages.o \
129                    uuid-tree.o utils-lib.o rbtree-utils.o
130 libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
131                kernel-lib/crc32c.h kernel-lib/list.h kerncompat.h \
132                kernel-lib/radix-tree.h kernel-lib/sizes.h kernel-lib/raid56.h \
133                extent-cache.h extent_io.h ioctl.h ctree.h btrfsck.h version.h
134 libbtrfsutil_major := $(shell sed -rn 's/^\#define BTRFS_UTIL_VERSION_MAJOR ([0-9])+$$/\1/p' libbtrfsutil/btrfsutil.h)
135 libbtrfsutil_minor := $(shell sed -rn 's/^\#define BTRFS_UTIL_VERSION_MINOR ([0-9])+$$/\1/p' libbtrfsutil/btrfsutil.h)
136 libbtrfsutil_patch := $(shell sed -rn 's/^\#define BTRFS_UTIL_VERSION_PATCH ([0-9])+$$/\1/p' libbtrfsutil/btrfsutil.h)
137 libbtrfsutil_version := $(libbtrfsutil_major).$(libbtrfsutil_minor).$(libbtrfsutil_patch)
138 libbtrfsutil_objects = libbtrfsutil/errors.o libbtrfsutil/filesystem.o \
139                        libbtrfsutil/qgroup.o
140 convert_objects = convert/main.o convert/common.o convert/source-fs.o \
141                   convert/source-ext2.o convert/source-reiserfs.o
142 mkfs_objects = mkfs/main.o mkfs/common.o mkfs/rootdir.o
143 image_objects = image/main.o image/sanitize.o
144 all_objects = $(objects) $(cmds_objects) $(libbtrfs_objects) $(convert_objects) \
145               $(mkfs_objects) $(image_objects) $(libbtrfsutil_objects)
146
147 udev_rules = 64-btrfs-dm.rules
148
149 ifeq ("$(origin V)", "command line")
150   BUILD_VERBOSE = $(V)
151 endif
152 ifndef BUILD_VERBOSE
153   BUILD_VERBOSE = 0
154 endif
155
156 ifeq ($(BUILD_VERBOSE),1)
157   Q =
158   SETUP_PY_Q =
159 else
160   Q = @
161   SETUP_PY_Q = -q
162 endif
163
164 ifeq ("$(origin D)", "command line")
165   DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
166   DEBUG_LDFLAGS_INTERNAL = $(DEBUG_LDFLAGS_DEFAULT) $(DEBUG_LDFLAGS)
167 endif
168
169 ifneq (,$(findstring verbose,$(D)))
170   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
171 endif
172
173 ifneq (,$(findstring trace,$(D)))
174   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
175 endif
176
177 ifneq (,$(findstring abort,$(D)))
178   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
179 endif
180
181 ifneq (,$(findstring all,$(D)))
182   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
183   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
184   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
185 endif
186
187 ifneq (,$(findstring asan,$(D)))
188   DEBUG_CFLAGS_INTERNAL += -fsanitize=address
189   DEBUG_LDFLAGS_INTERNAL += -fsanitize=address -lasan
190 endif
191
192 ifneq (,$(findstring tsan,$(D)))
193   DEBUG_CFLAGS_INTERNAL += -fsanitize=thread -fPIC
194   DEBUG_LDFLAGS_INTERNAL += -fsanitize=thread -ltsan -pie
195 endif
196
197 ifneq (,$(findstring ubsan,$(D)))
198   DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
199   DEBUG_LDFLAGS_INTERNAL += -fsanitize=undefined -lubsan
200 endif
201
202 ifneq (,$(findstring bcheck,$(D)))
203   DEBUG_CFLAGS_INTERNAL += -DDEBUG_BUILD_CHECKS
204 endif
205
206 MAKEOPTS = --no-print-directory Q=$(Q)
207
208 # build all by default
209 progs = $(progs_install) btrfsck btrfs-corrupt-block
210
211 # install only selected
212 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
213         btrfs-map-logical btrfs-image btrfs-zero-log \
214         btrfs-find-root btrfstune \
215         btrfs-select-super
216
217 # other tools, not built by default
218 progs_extra = btrfs-fragments btrfs-calc-size btrfs-show-super
219
220 progs_static = $(foreach p,$(progs),$(p).static)
221
222 ifneq ($(DISABLE_BTRFSCONVERT),1)
223 progs_install += btrfs-convert
224 endif
225
226 # external libs required by various binaries; for btrfs-foo,
227 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
228 btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2)
229 btrfs_convert_cflags += -DBTRFSCONVERT_REISERFS=$(BTRFSCONVERT_REISERFS)
230 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
231 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
232 btrfs_show_super_objects = cmds-inspect-dump-super.o
233 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
234 cmds_restore_cflags = -DBTRFSRESTORE_ZSTD=$(BTRFSRESTORE_ZSTD)
235
236 CHECKER_FLAGS += $(btrfs_convert_cflags)
237
238 # collect values of the variables above
239 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs) $(progs_extra)))),$($(dep)))
240
241 SUBDIRS =
242 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
243 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
244 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
245
246 ifneq ($(DISABLE_DOCUMENTATION),1)
247 BUILDDIRS += build-Documentation
248 INSTALLDIRS += install-Documentation
249 endif
250
251 .PHONY: $(SUBDIRS)
252 .PHONY: $(BUILDDIRS)
253 .PHONY: $(INSTALLDIRS)
254 .PHONY: $(TESTDIRS)
255 .PHONY: $(CLEANDIRS)
256 .PHONY: all install clean
257 .PHONY: FORCE
258
259 # Create all the static targets
260 static_objects = $(patsubst %.o, %.static.o, $(objects))
261 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
262 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
263 static_convert_objects = $(patsubst %.o, %.static.o, $(convert_objects))
264 static_mkfs_objects = $(patsubst %.o, %.static.o, $(mkfs_objects))
265 static_image_objects = $(patsubst %.o, %.static.o, $(image_objects))
266
267 libs_shared = libbtrfs.so.0.1 libbtrfsutil.so.$(libbtrfsutil_version)
268 libs_static = libbtrfs.a libbtrfsutil.a
269 libs = $(libs_shared) $(libs_static)
270 lib_links = libbtrfs.so.0 libbtrfs.so libbtrfsutil.so.$(libbtrfsutil_major) libbtrfsutil.so
271
272 # make C=1 to enable sparse
273 ifdef C
274         # We're trying to use sparse against glibc headers which go wild
275         # trying to use internal compiler macros to test features.  We
276         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
277         # 'cause sparse defines that one.
278         #
279         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
280                         grep -v __SIZE_TYPE__ > $(check_defs))
281         check = $(CHECKER)
282         check_echo = echo
283         CSTD = -std=gnu89
284 else
285         check = true
286         check_echo = true
287 endif
288
289 %.o.d: %.c
290         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
291
292 #
293 # Pick from per-file variables, btrfs_*_cflags
294 #
295 .c.o:
296         @$(check_echo) "    [SP]     $<"
297         $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
298         @echo "    [CC]     $@"
299         $(Q)$(CC) $(CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.o=%)-cflags)) \
300                 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
301
302 %.static.o: %.c
303         @echo "    [CC]     $@"
304         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags)) \
305                 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
306
307 all: $(progs) $(libs) $(lib_links) $(BUILDDIRS)
308 ifeq ($(PYTHON_BINDINGS),1)
309 all: libbtrfsutil_python
310 endif
311 $(SUBDIRS): $(BUILDDIRS)
312 $(BUILDDIRS):
313         @echo "Making all in $(patsubst build-%,%,$@)"
314         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
315
316 test-convert: btrfs btrfs-convert
317         @echo "    [TEST]   convert-tests.sh"
318         $(Q)bash tests/convert-tests.sh
319
320 test-check: test-fsck
321 test-fsck: btrfs btrfs-image btrfs-corrupt-block mkfs.btrfs btrfstune
322         @echo "    [TEST]   fsck-tests.sh"
323         $(Q)bash tests/fsck-tests.sh
324
325 test-misc: btrfs btrfs-image btrfs-corrupt-block mkfs.btrfs btrfstune fssum \
326                 btrfs-zero-log btrfs-find-root btrfs-select-super btrfs-convert
327         @echo "    [TEST]   misc-tests.sh"
328         $(Q)bash tests/misc-tests.sh
329
330 test-mkfs: btrfs mkfs.btrfs
331         @echo "    [TEST]   mkfs-tests.sh"
332         $(Q)bash tests/mkfs-tests.sh
333
334 test-fuzz: btrfs btrfs-image
335         @echo "    [TEST]   fuzz-tests.sh"
336         $(Q)bash tests/fuzz-tests.sh
337
338 test-cli: btrfs mkfs.btrfs
339         @echo "    [TEST]   cli-tests.sh"
340         $(Q)bash tests/cli-tests.sh
341
342 test-clean:
343         @echo "Cleaning tests"
344         $(Q)bash tests/clean-tests.sh
345
346 test-inst: all
347         @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
348                 echo "Test installation to $$tmpdest" && \
349                 $(MAKE) $(MAKEOPTS) DESTDIR=$$tmpdest install && \
350                 $(RM) -rf -- $$tmpdest
351
352 test: test-fsck test-mkfs test-misc test-cli test-convert test-fuzz
353
354 testsuite: btrfs-corrupt-block fssum
355         @echo "Export tests as a package"
356         $(Q)cd tests && ./export-testsuite.sh
357
358 ifeq ($(PYTHON_BINDINGS),1)
359 test-libbtrfsutil: libbtrfsutil_python
360         $(Q)cd libbtrfsutil/python; \
361                 LD_LIBRARY_PATH=../.. $(PYTHON) -m unittest discover -v tests
362
363 .PHONY: test-libbtrfsutil
364
365 test: test-libbtrfsutil
366 endif
367
368 #
369 # NOTE: For static compiles, you need to have all the required libs
370 #       static equivalent available
371 #
372 static: $(progs_static)
373
374 version.h: version.h.in configure.ac
375         @echo "    [SH]     $@"
376         $(Q)bash ./config.status --silent $@
377
378 mktables: kernel-lib/mktables.c
379         @echo "    [CC]     $@"
380         $(Q)$(CC) $(CFLAGS) $< -o $@
381
382 # the target can be regenerated manually using mktables, but a local copy is
383 # kept so the build process is simpler
384 kernel-lib/tables.c:
385         @echo "    [TABLE]  $@"
386         $(Q)./mktables > $@ || ($(RM) -f $@ && exit 1)
387
388 libbtrfs.so.0.1: $(libbtrfs_objects)
389         @echo "    [LD]     $@"
390         $(Q)$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LIBBTRFS_LIBS) \
391                 -shared -Wl,-soname,libbtrfs.so.0 -o $@
392
393 libbtrfs.a: $(libbtrfs_objects)
394         @echo "    [AR]     $@"
395         $(Q)$(AR) cr $@ $^
396
397 libbtrfs.so.0 libbtrfs.so: libbtrfs.so.0.1
398         @echo "    [LN]     $@"
399         $(Q)$(LN_S) -f $< $@
400
401 libbtrfsutil/%.o: libbtrfsutil/%.c
402         @echo "    [CC]     $@"
403         $(Q)$(CC) $(LIBBTRFSUTIL_CFLAGS) -o $@ -c $< -o $@
404
405 libbtrfsutil.so.$(libbtrfsutil_version): $(libbtrfsutil_objects)
406         @echo "    [LD]     $@"
407         $(Q)$(CC) $(LIBBTRFSUTIL_CFLAGS) $(libbtrfsutil_objects) \
408                 -shared -Wl,-soname,libbtrfsutil.so.$(libbtrfsutil_major) -o $@
409
410 libbtrfsutil.a: $(libbtrfsutil_objects)
411         @echo "    [AR]     $@"
412         $(Q)$(AR) cr $@ $^
413
414 libbtrfsutil.so.$(libbtrfsutil_major) libbtrfsutil.so: libbtrfsutil.so.$(libbtrfsutil_version)
415         @echo "    [LN]     $@"
416         $(Q)$(LN_S) -f $< $@
417
418 ifeq ($(PYTHON_BINDINGS),1)
419 libbtrfsutil_python: libbtrfsutil.so libbtrfsutil/btrfsutil.h
420         @echo "    [PY]     libbtrfsutil"
421         $(Q)cd libbtrfsutil/python; \
422                 CFLAGS= LDFLAGS= $(PYTHON) setup.py $(SETUP_PY_Q) build_ext -i build
423
424 .PHONY: libbtrfsutil_python
425 endif
426
427 # keep intermediate files from the below implicit rules around
428 .PRECIOUS: $(addsuffix .o,$(progs))
429
430 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
431 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
432 # turns them into a list of libraries to link against if they exist
433 #
434 # For static variants, use an extra $(subst) to get rid of the ".static"
435 # from the target name before translating to list of libs
436
437 btrfs-%.static: btrfs-%.static.o $(static_objects) $(patsubst %.o,%.static.o,$(standalone_deps)) $(static_libbtrfs_objects)
438         @echo "    [LD]     $@"
439         $(Q)$(CC) -o $@ $@.o $(static_objects) \
440                 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
441                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
442                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
443
444 btrfs-%: btrfs-%.o $(objects) $(standalone_deps) $(libs_static)
445         @echo "    [LD]     $@"
446         $(Q)$(CC) -o $@ $(objects) $@.o \
447                 $($(subst -,_,$@-objects)) \
448                 $(libs_static) \
449                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
450
451 btrfs: btrfs.o $(objects) $(cmds_objects) $(libs_static)
452         @echo "    [LD]     $@"
453         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
454
455 btrfs.static: btrfs.static.o $(static_objects) $(static_cmds_objects) $(static_libbtrfs_objects)
456         @echo "    [LD]     $@"
457         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
458
459 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
460 btrfsck: btrfs
461         @echo "    [LN]     $@"
462         $(Q)$(LN_S) -f btrfs btrfsck
463
464 btrfsck.static: btrfs.static
465         @echo "    [LN]     $@"
466         $(Q)$(LN_S) -f $^ $@
467
468 mkfs.btrfs: $(mkfs_objects) $(objects) $(libs_static)
469         @echo "    [LD]     $@"
470         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
471
472 mkfs.btrfs.static: $(static_mkfs_objects) $(static_objects) $(static_libbtrfs_objects)
473         @echo "    [LD]     $@"
474         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
475
476 btrfstune: btrfstune.o $(objects) $(libs_static)
477         @echo "    [LD]     $@"
478         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
479
480 btrfstune.static: btrfstune.static.o $(static_objects) $(static_libbtrfs_objects)
481         @echo "    [LD]     $@"
482         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
483
484 btrfs-image: $(image_objects) $(objects) $(libs_static)
485         @echo "    [LD]     $@"
486         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
487
488 btrfs-image.static: $(static_image_objects) $(static_objects) $(static_libbtrfs_objects)
489         @echo "    [LD]     $@"
490         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
491
492 btrfs-convert: $(convert_objects) $(objects) $(libs_static)
493         @echo "    [LD]     $@"
494         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(btrfs_convert_libs) $(LIBS)
495
496 btrfs-convert.static: $(static_convert_objects) $(static_objects) $(static_libbtrfs_objects)
497         @echo "    [LD]     $@"
498         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(btrfs_convert_libs) $(STATIC_LIBS)
499
500 dir-test: dir-test.o $(objects) $(libs)
501         @echo "    [LD]     $@"
502         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
503
504 quick-test: quick-test.o $(objects) $(libs)
505         @echo "    [LD]     $@"
506         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
507
508 ioctl-test.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
509         @echo "    [CC]   $@"
510         $(Q)$(CC) $(CFLAGS) -c $< -o $@
511
512 ioctl-test-32.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
513         @echo "    [CC32]   $@"
514         $(Q)$(CC) $(CFLAGS) -m32 -c $< -o $@
515
516 ioctl-test-64.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
517         @echo "    [CC64]   $@"
518         $(Q)$(CC) $(CFLAGS) -m64 -c $< -o $@
519
520 ioctl-test: ioctl-test.o
521         @echo "    [LD]   $@"
522         $(Q)$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
523         @echo "   ?[PAHOLE] $@.pahole"
524         -$(Q)pahole $@ > $@.pahole
525
526 ioctl-test-32: ioctl-test-32.o
527         @echo "    [LD32]   $@"
528         $(Q)$(CC) -m32 -o $@ $< $(LDFLAGS)
529         @echo "   ?[PAHOLE] $@.pahole"
530         -$(Q)pahole $@ > $@.pahole
531
532 ioctl-test-64: ioctl-test-64.o
533         @echo "    [LD64]   $@"
534         $(Q)$(CC) -m64 -o $@ $< $(LDFLAGS)
535         @echo "   ?[PAHOLE] $@.pahole"
536         -$(Q)pahole $@ > $@.pahole
537
538 test-ioctl: ioctl-test ioctl-test-32 ioctl-test-64
539         @echo "    [TEST/ioctl]"
540         $(Q)./ioctl-test > ioctl-test.log
541         $(Q)./ioctl-test-32 > ioctl-test-32.log
542         $(Q)./ioctl-test-64 > ioctl-test-64.log
543
544 library-test: library-test.c libbtrfs.so
545         @echo "    [TEST PREP]  $@"$(eval TMPD=$(shell mktemp -d))
546         $(Q)mkdir -p $(TMPD)/include/btrfs && \
547         cp $(libbtrfs_headers) $(TMPD)/include/btrfs && \
548         cd $(TMPD) && $(CC) -I$(TMPD)/include -o $@ $(addprefix $(ABSTOPDIR)/,$^) -Wl,-rpath=$(ABSTOPDIR) -lbtrfs
549         @echo "    [TEST RUN]   $@"
550         $(Q)cd $(TMPD) && ./$@
551         @echo "    [TEST CLEAN] $@"
552         $(Q)$(RM) -rf -- $(TMPD)
553
554 library-test.static: library-test.c $(libs_static)
555         @echo "    [TEST PREP]  $@"$(eval TMPD=$(shell mktemp -d))
556         $(Q)mkdir -p $(TMPD)/include/btrfs && \
557         cp $(libbtrfs_headers) $(TMPD)/include/btrfs && \
558         cd $(TMPD) && $(CC) -I$(TMPD)/include -o $@ $(addprefix $(ABSTOPDIR)/,$^) $(STATIC_LDFLAGS) $(STATIC_LIBS)
559         @echo "    [TEST RUN]   $@"
560         $(Q)cd $(TMPD) && ./$@
561         @echo "    [TEST CLEAN] $@"
562         $(Q)$(RM) -rf -- $(TMPD)
563
564 fssum: tests/fssum.c tests/sha224-256.c
565         @echo "    [LD]     $@"
566         $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
567
568 test-build: test-build-pre test-build-real
569
570 test-build-pre:
571         $(MAKE) $(MAKEOPTS) clean-all
572         ./autogen.sh
573         ./configure
574
575 test-build-real:
576         $(MAKE) $(MAKEOPTS) library-test
577         -$(MAKE) $(MAKEOPTS) library-test.static
578         $(MAKE) $(MAKEOPTS) -j 8 all
579         -$(MAKE) $(MAKEOPTS) -j 8 static
580         $(MAKE) $(MAKEOPTS) -j 8 $(progs_extra)
581
582 manpages:
583         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
584
585 tags: FORCE
586         @echo "    [TAGS]   $(TAGS_CMD)"
587         $(Q)$(TAGS_CMD) *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch]
588
589 cscope: FORCE
590         @echo "    [CSCOPE] $(CSCOPE_CMD)"
591         $(Q)ls -1 *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch] > cscope.files
592         $(Q)$(CSCOPE_CMD)
593
594 clean-all: clean clean-doc clean-gen
595
596 clean: $(CLEANDIRS)
597         @echo "Cleaning"
598         $(Q)$(RM) -f -- $(progs) *.o *.o.d \
599                 kernel-lib/*.o kernel-lib/*.o.d \
600                 kernel-shared/*.o kernel-shared/*.o.d \
601                 image/*.o image/*.o.d \
602                 convert/*.o convert/*.o.d \
603                 mkfs/*.o mkfs/*.o.d check/*.o check/*.o.d \
604               dir-test ioctl-test quick-test library-test library-test-static \
605               mktables btrfs.static mkfs.btrfs.static fssum \
606               $(check_defs) \
607               $(libs) $(lib_links) \
608               $(progs_static) $(progs_extra) \
609               libbtrfsutil/*.o libbtrfsutil/*.o.d
610 ifeq ($(PYTHON_BINDINGS),1)
611         $(Q)cd libbtrfsutil/python; \
612                 $(PYTHON) setup.py $(SETUP_PY_Q) clean -a
613 endif
614
615 clean-doc:
616         @echo "Cleaning Documentation"
617         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
618
619 clean-gen:
620         @echo "Cleaning Generated Files"
621         $(Q)$(RM) -rf -- version.h config.status config.cache connfig.log \
622                 configure.lineno config.status.lineno Makefile.inc \
623                 Documentation/Makefile tags \
624                 cscope.files cscope.out cscope.in.out cscope.po.out \
625                 config.log config.h config.h.in~ aclocal.m4 \
626                 configure autom4te.cache/ config/
627
628 $(CLEANDIRS):
629         @echo "Cleaning $(patsubst clean-%,%,$@)"
630         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
631
632 install: $(libs) $(progs_install) $(INSTALLDIRS)
633         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
634         $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
635         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
636         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
637         $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
638         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
639         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
640         cp -a $(lib_links) $(DESTDIR)$(libdir)
641         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)/btrfs
642         $(INSTALL) -m644 $(libbtrfs_headers) $(DESTDIR)$(incdir)/btrfs
643         $(INSTALL) -m644 libbtrfsutil/btrfsutil.h $(DESTDIR)$(incdir)
644 ifneq ($(udevdir),)
645         $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
646         $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
647 endif
648
649 ifeq ($(PYTHON_BINDINGS),1)
650 install_python: libbtrfsutil_python
651         $(Q)cd libbtrfsutil/python; \
652                 $(PYTHON) setup.py install --skip-build $(if $(DESTDIR),--root $(DESTDIR)) --prefix $(prefix)
653
654 .PHONY: install_python
655 endif
656
657 install-static: $(progs_static) $(INSTALLDIRS)
658         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
659         $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
660         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
661         $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
662
663 $(INSTALLDIRS):
664         @echo "Making install in $(patsubst install-%,%,$@)"
665         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
666
667 uninstall:
668         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
669         cd $(DESTDIR)$(incdir)/btrfs; $(RM) -f -- $(libbtrfs_headers)
670         $(RMDIR) -p --ignore-fail-on-non-empty -- $(DESTDIR)$(incdir)/btrfs
671         cd $(DESTDIR)$(incdir); $(RM) -f -- btrfsutil.h
672         cd $(DESTDIR)$(libdir); $(RM) -f -- $(lib_links) $(libs)
673         cd $(DESTDIR)$(bindir); $(RM) -f -- btrfsck fsck.btrfs $(progs_install)
674
675 ifneq ($(MAKECMDGOALS),clean)
676 -include $(all_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
677 endif