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