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