btrfs-progs: build btrfs.static needs libbtrfsutil to build
[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/subvolume.o libbtrfsutil/qgroup.o \
140                        libbtrfsutil/stubs.o
141 convert_objects = convert/main.o convert/common.o convert/source-fs.o \
142                   convert/source-ext2.o convert/source-reiserfs.o
143 mkfs_objects = mkfs/main.o mkfs/common.o mkfs/rootdir.o
144 image_objects = image/main.o image/sanitize.o
145 all_objects = $(objects) $(cmds_objects) $(libbtrfs_objects) $(convert_objects) \
146               $(mkfs_objects) $(image_objects) $(libbtrfsutil_objects)
147
148 udev_rules = 64-btrfs-dm.rules
149
150 ifeq ("$(origin V)", "command line")
151   BUILD_VERBOSE = $(V)
152 endif
153 ifndef BUILD_VERBOSE
154   BUILD_VERBOSE = 0
155 endif
156
157 ifeq ($(BUILD_VERBOSE),1)
158   Q =
159   SETUP_PY_Q =
160 else
161   Q = @
162   SETUP_PY_Q = -q
163 endif
164
165 ifeq ("$(origin D)", "command line")
166   DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
167   DEBUG_LDFLAGS_INTERNAL = $(DEBUG_LDFLAGS_DEFAULT) $(DEBUG_LDFLAGS)
168 endif
169
170 ifneq (,$(findstring verbose,$(D)))
171   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
172 endif
173
174 ifneq (,$(findstring trace,$(D)))
175   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
176 endif
177
178 ifneq (,$(findstring abort,$(D)))
179   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
180 endif
181
182 ifneq (,$(findstring all,$(D)))
183   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
184   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
185   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
186 endif
187
188 ifneq (,$(findstring asan,$(D)))
189   DEBUG_CFLAGS_INTERNAL += -fsanitize=address
190   DEBUG_LDFLAGS_INTERNAL += -fsanitize=address -lasan
191 endif
192
193 ifneq (,$(findstring tsan,$(D)))
194   DEBUG_CFLAGS_INTERNAL += -fsanitize=thread -fPIC
195   DEBUG_LDFLAGS_INTERNAL += -fsanitize=thread -ltsan -pie
196 endif
197
198 ifneq (,$(findstring ubsan,$(D)))
199   DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
200   DEBUG_LDFLAGS_INTERNAL += -fsanitize=undefined -lubsan
201 endif
202
203 ifneq (,$(findstring bcheck,$(D)))
204   DEBUG_CFLAGS_INTERNAL += -DDEBUG_BUILD_CHECKS
205 endif
206
207 MAKEOPTS = --no-print-directory Q=$(Q)
208
209 # build all by default
210 progs = $(progs_install) btrfsck btrfs-corrupt-block
211
212 # install only selected
213 progs_install = btrfs mkfs.btrfs btrfs-map-logical btrfs-image \
214         btrfs-find-root btrfstune \
215         btrfs-select-super
216
217 # other tools, not built by default
218 progs_extra = btrfs-fragments
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 cmds_restore_cflags = -DBTRFSRESTORE_ZSTD=$(BTRFSRESTORE_ZSTD)
232
233 CHECKER_FLAGS += $(btrfs_convert_cflags)
234
235 # collect values of the variables above
236 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs) $(progs_extra)))),$($(dep)))
237
238 SUBDIRS =
239 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
240 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
241 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
242
243 ifneq ($(DISABLE_DOCUMENTATION),1)
244 BUILDDIRS += build-Documentation
245 INSTALLDIRS += install-Documentation
246 endif
247
248 .PHONY: $(SUBDIRS)
249 .PHONY: $(BUILDDIRS)
250 .PHONY: $(INSTALLDIRS)
251 .PHONY: $(TESTDIRS)
252 .PHONY: $(CLEANDIRS)
253 .PHONY: all install clean
254 .PHONY: FORCE
255
256 # Create all the static targets
257 static_objects = $(patsubst %.o, %.static.o, $(objects))
258 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
259 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
260 static_libbtrfsutil_objects = $(patsubst %.o, %.static.o, $(libbtrfsutil_objects))
261 static_convert_objects = $(patsubst %.o, %.static.o, $(convert_objects))
262 static_mkfs_objects = $(patsubst %.o, %.static.o, $(mkfs_objects))
263 static_image_objects = $(patsubst %.o, %.static.o, $(image_objects))
264
265 libs_shared = libbtrfs.so.0.1 libbtrfsutil.so.$(libbtrfsutil_version)
266 libs_static = libbtrfs.a libbtrfsutil.a
267 libs = $(libs_shared) $(libs_static)
268 lib_links = libbtrfs.so.0 libbtrfs.so libbtrfsutil.so.$(libbtrfsutil_major) libbtrfsutil.so
269
270 # make C=1 to enable sparse
271 ifdef C
272         # We're trying to use sparse against glibc headers which go wild
273         # trying to use internal compiler macros to test features.  We
274         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
275         # 'cause sparse defines that one.
276         #
277         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
278                         grep -v __SIZE_TYPE__ > $(check_defs))
279         check = $(CHECKER)
280         check_echo = echo
281         CSTD = -std=gnu89
282 else
283         check = true
284         check_echo = true
285 endif
286
287 %.o.d: %.c
288         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
289
290 #
291 # Pick from per-file variables, btrfs_*_cflags
292 #
293 .c.o:
294         @$(check_echo) "    [SP]     $<"
295         $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
296         @echo "    [CC]     $@"
297         $(Q)$(CC) $(CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.o=%)-cflags)) \
298                 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
299
300 %.static.o: %.c
301         @echo "    [CC]     $@"
302         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags)) \
303                 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
304
305 all: $(progs) $(libs) $(lib_links) $(BUILDDIRS)
306 ifeq ($(PYTHON_BINDINGS),1)
307 all: libbtrfsutil_python
308 endif
309 $(SUBDIRS): $(BUILDDIRS)
310 $(BUILDDIRS):
311         @echo "Making all in $(patsubst build-%,%,$@)"
312         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
313
314 test-convert: btrfs btrfs-convert
315         @echo "    [TEST]   convert-tests.sh"
316         $(Q)bash tests/convert-tests.sh
317
318 test-check: test-fsck
319 test-fsck: btrfs btrfs-image btrfs-corrupt-block mkfs.btrfs btrfstune
320         @echo "    [TEST]   fsck-tests.sh"
321         $(Q)bash tests/fsck-tests.sh
322
323 test-misc: btrfs btrfs-image btrfs-corrupt-block mkfs.btrfs btrfstune fssum \
324                 btrfs-find-root btrfs-select-super btrfs-convert
325         @echo "    [TEST]   misc-tests.sh"
326         $(Q)bash tests/misc-tests.sh
327
328 test-mkfs: btrfs mkfs.btrfs
329         @echo "    [TEST]   mkfs-tests.sh"
330         $(Q)bash tests/mkfs-tests.sh
331
332 test-fuzz: btrfs btrfs-image
333         @echo "    [TEST]   fuzz-tests.sh"
334         $(Q)bash tests/fuzz-tests.sh
335
336 test-cli: btrfs mkfs.btrfs
337         @echo "    [TEST]   cli-tests.sh"
338         $(Q)bash tests/cli-tests.sh
339
340 test-clean:
341         @echo "Cleaning tests"
342         $(Q)bash tests/clean-tests.sh
343
344 test-inst: all
345         @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
346                 echo "Test installation to $$tmpdest" && \
347                 $(MAKE) $(MAKEOPTS) DESTDIR=$$tmpdest install && \
348                 $(RM) -rf -- $$tmpdest
349
350 test: test-fsck test-mkfs test-misc test-cli test-convert test-fuzz
351
352 testsuite: btrfs-corrupt-block fssum
353         @echo "Export tests as a package"
354         $(Q)cd tests && ./export-testsuite.sh
355
356 ifeq ($(PYTHON_BINDINGS),1)
357 test-libbtrfsutil: libbtrfsutil_python mkfs.btrfs
358         $(Q)cd libbtrfsutil/python; \
359                 LD_LIBRARY_PATH=../.. $(PYTHON) -m unittest discover -v tests
360
361 .PHONY: test-libbtrfsutil
362
363 test: test-libbtrfsutil
364 endif
365
366 #
367 # NOTE: For static compiles, you need to have all the required libs
368 #       static equivalent available
369 #
370 static: $(progs_static)
371
372 version.h: version.h.in configure.ac
373         @echo "    [SH]     $@"
374         $(Q)bash ./config.status --silent $@
375
376 mktables: kernel-lib/mktables.c
377         @echo "    [CC]     $@"
378         $(Q)$(CC) $(CFLAGS) $< -o $@
379
380 # the target can be regenerated manually using mktables, but a local copy is
381 # kept so the build process is simpler
382 kernel-lib/tables.c:
383         @echo "    [TABLE]  $@"
384         $(Q)./mktables > $@ || ($(RM) -f $@ && exit 1)
385
386 libbtrfs.so.0.1: $(libbtrfs_objects)
387         @echo "    [LD]     $@"
388         $(Q)$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LIBBTRFS_LIBS) \
389                 -shared -Wl,-soname,libbtrfs.so.0 -o $@
390
391 libbtrfs.a: $(libbtrfs_objects)
392         @echo "    [AR]     $@"
393         $(Q)$(AR) cr $@ $^
394
395 libbtrfs.so.0 libbtrfs.so: libbtrfs.so.0.1
396         @echo "    [LN]     $@"
397         $(Q)$(LN_S) -f $< $@
398
399 libbtrfsutil/%.o: libbtrfsutil/%.c
400         @echo "    [CC]     $@"
401         $(Q)$(CC) $(LIBBTRFSUTIL_CFLAGS) -o $@ -c $< -o $@
402
403 libbtrfsutil.so.$(libbtrfsutil_version): $(libbtrfsutil_objects)
404         @echo "    [LD]     $@"
405         $(Q)$(CC) $(LIBBTRFSUTIL_CFLAGS) $(libbtrfsutil_objects) \
406                 -shared -Wl,-soname,libbtrfsutil.so.$(libbtrfsutil_major) -o $@
407
408 libbtrfsutil.a: $(libbtrfsutil_objects)
409         @echo "    [AR]     $@"
410         $(Q)$(AR) cr $@ $^
411
412 libbtrfsutil.so.$(libbtrfsutil_major) libbtrfsutil.so: libbtrfsutil.so.$(libbtrfsutil_version)
413         @echo "    [LN]     $@"
414         $(Q)$(LN_S) -f $< $@
415
416 ifeq ($(PYTHON_BINDINGS),1)
417 libbtrfsutil_python: libbtrfsutil.so.$(libbtrfsutil_major) libbtrfsutil.so libbtrfsutil/btrfsutil.h
418         @echo "    [PY]     libbtrfsutil"
419         $(Q)cd libbtrfsutil/python; \
420                 CFLAGS= LDFLAGS= $(PYTHON) setup.py $(SETUP_PY_Q) build_ext -i build
421
422 .PHONY: libbtrfsutil_python
423 endif
424
425 # keep intermediate files from the below implicit rules around
426 .PRECIOUS: $(addsuffix .o,$(progs))
427
428 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
429 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
430 # turns them into a list of libraries to link against if they exist
431 #
432 # For static variants, use an extra $(subst) to get rid of the ".static"
433 # from the target name before translating to list of libs
434
435 btrfs-%.static: btrfs-%.static.o $(static_objects) $(patsubst %.o,%.static.o,$(standalone_deps)) $(static_libbtrfs_objects)
436         @echo "    [LD]     $@"
437         $(Q)$(CC) -o $@ $@.o $(static_objects) \
438                 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
439                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
440                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
441
442 btrfs-%: btrfs-%.o $(objects) $(standalone_deps) $(libs_static)
443         @echo "    [LD]     $@"
444         $(Q)$(CC) -o $@ $(objects) $@.o \
445                 $($(subst -,_,$@-objects)) \
446                 $(libs_static) \
447                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
448
449 btrfs: btrfs.o $(objects) $(cmds_objects) $(libs_static)
450         @echo "    [LD]     $@"
451         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
452
453 btrfs.static: btrfs.static.o $(static_objects) $(static_cmds_objects) $(static_libbtrfs_objects) $(static_libbtrfsutil_objects)
454         @echo "    [LD]     $@"
455         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
456
457 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
458 btrfsck: btrfs
459         @echo "    [LN]     $@"
460         $(Q)$(LN_S) -f btrfs btrfsck
461
462 btrfsck.static: btrfs.static
463         @echo "    [LN]     $@"
464         $(Q)$(LN_S) -f $^ $@
465
466 mkfs.btrfs: $(mkfs_objects) $(objects) $(libs_static)
467         @echo "    [LD]     $@"
468         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
469
470 mkfs.btrfs.static: $(static_mkfs_objects) $(static_objects) $(static_libbtrfs_objects)
471         @echo "    [LD]     $@"
472         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
473
474 btrfstune: btrfstune.o $(objects) $(libs_static)
475         @echo "    [LD]     $@"
476         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
477
478 btrfstune.static: btrfstune.static.o $(static_objects) $(static_libbtrfs_objects)
479         @echo "    [LD]     $@"
480         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
481
482 btrfs-image: $(image_objects) $(objects) $(libs_static)
483         @echo "    [LD]     $@"
484         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
485
486 btrfs-image.static: $(static_image_objects) $(static_objects) $(static_libbtrfs_objects)
487         @echo "    [LD]     $@"
488         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
489
490 btrfs-convert: $(convert_objects) $(objects) $(libs_static)
491         @echo "    [LD]     $@"
492         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(btrfs_convert_libs) $(LIBS)
493
494 btrfs-convert.static: $(static_convert_objects) $(static_objects) $(static_libbtrfs_objects)
495         @echo "    [LD]     $@"
496         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(btrfs_convert_libs) $(STATIC_LIBS)
497
498 dir-test: dir-test.o $(objects) $(libs)
499         @echo "    [LD]     $@"
500         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
501
502 quick-test: quick-test.o $(objects) $(libs)
503         @echo "    [LD]     $@"
504         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
505
506 ioctl-test.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
507         @echo "    [CC]   $@"
508         $(Q)$(CC) $(CFLAGS) -c $< -o $@
509
510 ioctl-test-32.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
511         @echo "    [CC32]   $@"
512         $(Q)$(CC) $(CFLAGS) -m32 -c $< -o $@
513
514 ioctl-test-64.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
515         @echo "    [CC64]   $@"
516         $(Q)$(CC) $(CFLAGS) -m64 -c $< -o $@
517
518 ioctl-test: ioctl-test.o
519         @echo "    [LD]   $@"
520         $(Q)$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
521         @echo "   ?[PAHOLE] $@.pahole"
522         -$(Q)pahole $@ > $@.pahole
523
524 ioctl-test-32: ioctl-test-32.o
525         @echo "    [LD32]   $@"
526         $(Q)$(CC) -m32 -o $@ $< $(LDFLAGS)
527         @echo "   ?[PAHOLE] $@.pahole"
528         -$(Q)pahole $@ > $@.pahole
529
530 ioctl-test-64: ioctl-test-64.o
531         @echo "    [LD64]   $@"
532         $(Q)$(CC) -m64 -o $@ $< $(LDFLAGS)
533         @echo "   ?[PAHOLE] $@.pahole"
534         -$(Q)pahole $@ > $@.pahole
535
536 test-ioctl: ioctl-test ioctl-test-32 ioctl-test-64
537         @echo "    [TEST/ioctl]"
538         $(Q)./ioctl-test > ioctl-test.log
539         $(Q)./ioctl-test-32 > ioctl-test-32.log
540         $(Q)./ioctl-test-64 > ioctl-test-64.log
541
542 library-test: library-test.c libbtrfs.so
543         @echo "    [TEST PREP]  $@"$(eval TMPD=$(shell mktemp -d))
544         $(Q)mkdir -p $(TMPD)/include/btrfs && \
545         cp $(libbtrfs_headers) $(TMPD)/include/btrfs && \
546         cd $(TMPD) && $(CC) -I$(TMPD)/include -o $@ $(addprefix $(ABSTOPDIR)/,$^) -Wl,-rpath=$(ABSTOPDIR) -lbtrfs
547         @echo "    [TEST RUN]   $@"
548         $(Q)cd $(TMPD) && ./$@
549         @echo "    [TEST CLEAN] $@"
550         $(Q)$(RM) -rf -- $(TMPD)
551
552 library-test.static: library-test.c $(libs_static)
553         @echo "    [TEST PREP]  $@"$(eval TMPD=$(shell mktemp -d))
554         $(Q)mkdir -p $(TMPD)/include/btrfs && \
555         cp $(libbtrfs_headers) $(TMPD)/include/btrfs && \
556         cd $(TMPD) && $(CC) -I$(TMPD)/include -o $@ $(addprefix $(ABSTOPDIR)/,$^) $(STATIC_LDFLAGS) $(STATIC_LIBS)
557         @echo "    [TEST RUN]   $@"
558         $(Q)cd $(TMPD) && ./$@
559         @echo "    [TEST CLEAN] $@"
560         $(Q)$(RM) -rf -- $(TMPD)
561
562 fssum: tests/fssum.c tests/sha224-256.c
563         @echo "    [LD]     $@"
564         $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
565
566 test-build: test-build-pre test-build-real
567
568 test-build-pre:
569         $(MAKE) $(MAKEOPTS) clean-all
570         ./autogen.sh
571         ./configure
572
573 test-build-real:
574         $(MAKE) $(MAKEOPTS) library-test
575         -$(MAKE) $(MAKEOPTS) library-test.static
576         $(MAKE) $(MAKEOPTS) -j 8 all
577         -$(MAKE) $(MAKEOPTS) -j 8 static
578         $(MAKE) $(MAKEOPTS) -j 8 $(progs_extra)
579
580 manpages:
581         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
582
583 tags: FORCE
584         @echo "    [TAGS]   $(TAGS_CMD)"
585         $(Q)$(TAGS_CMD) *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch] \
586                 check/*.[ch] kernel-lib/*.[ch] kernel-shared/*.[ch] \
587                 libbtrfsutil/*.[ch]
588
589 cscope: FORCE
590         @echo "    [CSCOPE] $(CSCOPE_CMD)"
591         $(Q)ls -1 *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch] check/*.[ch] \
592                 kernel-lib/*.[ch] kernel-shared/*.[ch] libbtrfsutil/*.[ch] \
593                 > cscope.files
594         $(Q)$(CSCOPE_CMD)
595
596 clean-all: clean clean-doc clean-gen
597
598 clean: $(CLEANDIRS)
599         @echo "Cleaning"
600         $(Q)$(RM) -f -- $(progs) *.o *.o.d \
601                 kernel-lib/*.o kernel-lib/*.o.d \
602                 kernel-shared/*.o kernel-shared/*.o.d \
603                 image/*.o image/*.o.d \
604                 convert/*.o convert/*.o.d \
605                 mkfs/*.o mkfs/*.o.d check/*.o check/*.o.d \
606               dir-test ioctl-test quick-test library-test library-test-static \
607               mktables btrfs.static mkfs.btrfs.static fssum \
608               $(check_defs) \
609               $(libs) $(lib_links) \
610               $(progs_static) $(progs_extra) \
611               libbtrfsutil/*.o libbtrfsutil/*.o.d
612 ifeq ($(PYTHON_BINDINGS),1)
613         $(Q)cd libbtrfsutil/python; \
614                 $(PYTHON) setup.py $(SETUP_PY_Q) clean -a
615 endif
616
617 clean-doc:
618         @echo "Cleaning Documentation"
619         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
620
621 clean-gen:
622         @echo "Cleaning Generated Files"
623         $(Q)$(RM) -rf -- version.h config.status config.cache connfig.log \
624                 configure.lineno config.status.lineno Makefile.inc \
625                 Documentation/Makefile tags \
626                 cscope.files cscope.out cscope.in.out cscope.po.out \
627                 config.log config.h config.h.in~ aclocal.m4 \
628                 configure autom4te.cache/ config/
629
630 $(CLEANDIRS):
631         @echo "Cleaning $(patsubst clean-%,%,$@)"
632         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
633
634 install: $(libs) $(progs_install) $(INSTALLDIRS)
635         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
636         $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
637         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
638         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
639         $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
640         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
641         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
642         cp -d $(lib_links) $(DESTDIR)$(libdir)
643         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)/btrfs
644         $(INSTALL) -m644 $(libbtrfs_headers) $(DESTDIR)$(incdir)/btrfs
645         $(INSTALL) -m644 libbtrfsutil/btrfsutil.h $(DESTDIR)$(incdir)
646 ifneq ($(udevdir),)
647         $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
648         $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
649 endif
650
651 ifeq ($(PYTHON_BINDINGS),1)
652 install_python: libbtrfsutil_python
653         $(Q)cd libbtrfsutil/python; \
654                 $(PYTHON) setup.py install --skip-build $(if $(DESTDIR),--root $(DESTDIR)) --prefix $(prefix)
655
656 .PHONY: install_python
657 endif
658
659 install-static: $(progs_static) $(INSTALLDIRS)
660         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
661         $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
662         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
663         $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
664
665 $(INSTALLDIRS):
666         @echo "Making install in $(patsubst install-%,%,$@)"
667         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
668
669 uninstall:
670         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
671         cd $(DESTDIR)$(incdir)/btrfs; $(RM) -f -- $(libbtrfs_headers)
672         $(RMDIR) -p --ignore-fail-on-non-empty -- $(DESTDIR)$(incdir)/btrfs
673         cd $(DESTDIR)$(incdir); $(RM) -f -- btrfsutil.h
674         cd $(DESTDIR)$(libdir); $(RM) -f -- $(lib_links) $(libs)
675         cd $(DESTDIR)$(bindir); $(RM) -f -- btrfsck fsck.btrfs $(progs_install)
676
677 ifneq ($(MAKECMDGOALS),clean)
678 -include $(all_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
679 endif