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