112c3ba2bda7a2c521ff87516bafdd6983319ed0
[platform/upstream/btrfs-progs.git] / Makefile.in
1 # btrfs-progs
2 #
3 # Basic build targets:
4 #   all         all main tools
5 #   static      build static bnaries, requires static version of the libraries
6 #   test        run the full testsuite
7 #   install     install to default location (/usr/local)
8 #   clean       clean built binaries (not the documentation)
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 #   W=123          build with warnings (default: off)
14 #   EXTRA_CFLAGS   additional compiler flags
15 #   EXTRA_LDFLAGS  additional linker flags
16 #
17 # Static checkers:
18 #   CHECKER        static checker binary to be called (default: sparse)
19 #   CHECKER_FLAGS  flags to pass to CHECKER, can override CFLAGS
20 #
21
22 # Export all variables to sub-makes by default
23 export
24
25 include Makefile.extrawarn
26
27 CC = @CC@
28 LN_S = @LN_S@
29 AR = @AR@
30 RM = @RM@
31 RMDIR = @RMDIR@
32 INSTALL = @INSTALL@
33 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
34 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
35
36 EXTRA_CFLAGS :=
37 EXTRA_LDFLAGS :=
38
39 # Common build flags
40 CFLAGS = @CFLAGS@ \
41          -include config.h \
42          -DBTRFS_FLAT_INCLUDES \
43          -D_XOPEN_SOURCE=700  \
44          -fno-strict-aliasing \
45          -fPIC $(EXTRAWARN_CFLAGS) $(EXTRA_CFLAGS)
46
47 LDFLAGS = @LDFLAGS@ \
48           -rdynamic $(EXTRA_LDFLAGS)
49
50 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
51 LIBBTRFS_LIBS = $(LIBS)
52
53 # Static compilation flags
54 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
55 STATIC_LDFLAGS = -static -Wl,--gc-sections
56 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
57               @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
58
59 # don't use FORTIFY with sparse because glibc with FORTIFY can
60 # generate so many sparse errors that sparse stops parsing,
61 # which masks real errors that we want to see.
62 CHECKER := sparse
63 check_defs := .cc-defines.h
64 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
65         -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
66         -U_FORTIFY_SOURCE
67
68 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
69           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
70           extent-cache.o extent_io.o volumes.o utils.o repair.o \
71           qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
72           ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
73           inode.o file.o find-root.o free-space-tree.o
74 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
75                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
76                cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
77                cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
78                cmds-property.o cmds-fi-usage.o
79 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
80                    uuid-tree.o utils-lib.o rbtree-utils.o
81 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
82                crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
83                extent_io.h ioctl.h ctree.h btrfsck.h version.h
84 TESTS = fsck-tests.sh convert-tests.sh
85
86 prefix ?= @prefix@
87 exec_prefix = @exec_prefix@
88 bindir = @bindir@
89 libdir ?= @libdir@
90 incdir = @includedir@/btrfs
91
92 ifeq ("$(origin V)", "command line")
93   BUILD_VERBOSE = $(V)
94 endif
95 ifndef BUILD_VERBOSE
96   BUILD_VERBOSE = 0
97 endif
98
99 ifeq ($(BUILD_VERBOSE),1)
100   Q =
101 else
102   Q = @
103 endif
104
105 MAKEOPTS = --no-print-directory Q=$(Q)
106
107 # build all by default
108 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
109
110 # install only selected
111 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
112         btrfs-map-logical btrfs-image btrfs-zero-log \
113         btrfs-find-root btrfstune btrfs-show-super \
114         btrfs-select-super
115
116 progs_extra = btrfs-fragments
117
118 progs_static = $(foreach p,$(progs),$(p).static)
119
120 ifneq ($(DISABLE_BTRFSCONVERT),1)
121 progs_install += btrfs-convert
122 endif
123
124 # external libs required by various binaries; for btrfs-foo,
125 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
126 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
127 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
128
129 SUBDIRS =
130 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
131 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
132 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
133
134 ifneq ($(DISABLE_DOCUMENTATION),1)
135 BUILDDIRS += build-Documentation
136 INSTALLDIRS += install-Documentation
137 endif
138
139 .PHONY: $(SUBDIRS)
140 .PHONY: $(BUILDDIRS)
141 .PHONY: $(INSTALLDIRS)
142 .PHONY: $(TESTDIRS)
143 .PHONY: $(CLEANDIRS)
144 .PHONY: all install clean
145
146 # Create all the static targets
147 static_objects = $(patsubst %.o, %.static.o, $(objects))
148 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
149 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
150
151 libs_shared = libbtrfs.so.0.1
152 libs_static = libbtrfs.a
153 libs = $(libs_shared) $(libs_static)
154 lib_links = libbtrfs.so.0 libbtrfs.so
155 headers = $(libbtrfs_headers)
156
157 # make C=1 to enable sparse
158 ifdef C
159         # We're trying to use sparse against glibc headers which go wild
160         # trying to use internal compiler macros to test features.  We
161         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
162         # 'cause sparse defines that one.
163         #
164         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
165                         grep -v __SIZE_TYPE__ > $(check_defs))
166         check = $(CHECKER)
167         check_echo = echo
168 else
169         check = true
170         check_echo = true
171 endif
172
173 %.o.d: %.c
174         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
175
176 .c.o:
177         @$(check_echo) "    [SP]     $<"
178         $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
179         @echo "    [CC]     $@"
180         $(Q)$(CC) $(CFLAGS) -c $<
181
182 %.static.o: %.c
183         @echo "    [CC]     $@"
184         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@
185
186 all: $(progs) $(BUILDDIRS)
187 $(SUBDIRS): $(BUILDDIRS)
188 $(BUILDDIRS):
189         @echo "Making all in $(patsubst build-%,%,$@)"
190         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
191
192 test-convert: btrfs btrfs-convert
193         @echo "    [TEST]   convert-tests.sh"
194         $(Q)bash tests/convert-tests.sh
195
196 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
197         @echo "    [TEST]   fsck-tests.sh"
198         $(Q)bash tests/fsck-tests.sh
199
200 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
201         @echo "    [TEST]   misc-tests.sh"
202         $(Q)bash tests/misc-tests.sh
203
204 test-mkfs: btrfs mkfs.btrfs
205         @echo "    [TEST]   mkfs-tests.sh"
206         $(Q)bash tests/mkfs-tests.sh
207
208 test-fuzz: btrfs
209         @echo "    [TEST]   fuzz-tests.sh"
210         $(Q)bash tests/fuzz-tests.sh
211
212 test-clean:
213         @echo "Cleaning tests"
214         $(Q)bash tests/clean-tests.sh
215
216 test: test-fsck test-mkfs test-convert test-misc test-fuzz
217
218 #
219 # NOTE: For static compiles, you need to have all the required libs
220 #       static equivalent available
221 #
222 static: $(progs_static)
223
224 version.h: version.sh version.h.in configure.ac
225         @echo "    [SH]     $@"
226         $(Q)bash ./config.status --silent $@
227
228 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
229         @echo "    [LD]     $@"
230         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
231                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
232
233 $(libs_static): $(libbtrfs_objects)
234         @echo "    [AR]     $@"
235         $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
236
237 $(lib_links):
238         @echo "    [LN]     $@"
239         $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so.0
240         $(Q)$(LN_S) -f libbtrfs.so.0.1 libbtrfs.so
241
242 # keep intermediate files from the below implicit rules around
243 .PRECIOUS: $(addsuffix .o,$(progs))
244
245 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
246 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
247 # turns them into a list of libraries to link against if they exist
248 #
249 # For static variants, use an extra $(subst) to get rid of the ".static"
250 # from the target name before translating to list of libs
251
252 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
253         @echo "    [LD]     $@"
254         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
255                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
256                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
257
258 btrfs-%: $(objects) $(libs_static) btrfs-%.o
259         @echo "    [LD]     $@"
260         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs_static) \
261                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
262
263 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs_static)
264         @echo "    [LD]     $@"
265         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
266                 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
267
268 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
269         @echo "    [LD]     $@"
270         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
271                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
272
273 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
274 btrfsck: btrfs
275         @echo "    [LN]     $@"
276         $(Q)$(LN_S) -f btrfs btrfsck
277
278 btrfsck.static: btrfs.static
279         @echo "    [LN]     $@"
280         $(Q)$(LN_S) -f $^ $@
281
282 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
283         @echo "    [LD]     $@"
284         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
285
286 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
287         @echo "    [LD]     $@"
288         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
289                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
290
291 btrfstune: $(objects) $(libs_static) btrfstune.o
292         @echo "    [LD]     $@"
293         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
294
295 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
296         @echo "    [LD]     $@"
297         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
298                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
299
300 dir-test: $(objects) $(libs) dir-test.o
301         @echo "    [LD]     $@"
302         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
303
304 quick-test: $(objects) $(libs) quick-test.o
305         @echo "    [LD]     $@"
306         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
307
308 ioctl-test: $(objects) $(libs) ioctl-test.o
309         @echo "    [LD]     $@"
310         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
311
312 send-test: $(objects) $(libs) send-test.o
313         @echo "    [LD]     $@"
314         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
315
316 library-test: $(libs_shared) library-test.o
317         @echo "    [LD]     $@"
318         $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
319
320 library-test.static: $(libs_static) library-test.o
321         @echo "    [LD]     $@"
322         $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
323
324 test-build: test-build-pre test-build-real
325
326 test-build-pre:
327         $(MAKE) clean-all
328         ./autogen.sh
329         ./configure
330
331 test-build-real:
332         $(MAKE) library-test
333         -$(MAKE) library-test.static
334         $(MAKE) -j 8 all
335         -$(MAKE) -j 8 static
336         $(MAKE) -j 8 $(progs_extra)
337
338 manpages:
339         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
340
341
342 clean-all: clean clean-doc clean-gen
343
344 clean: $(CLEANDIRS)
345         @echo "Cleaning"
346         $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
347               dir-test ioctl-test quick-test send-test library-test library-test-static \
348               btrfs.static mkfs.btrfs.static \
349               $(check_defs) \
350               $(libs) $(lib_links) \
351               $(progs_static) $(progs_extra)
352
353 clean-doc:
354         @echo "Cleaning Documentation"
355         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
356
357 clean-gen:
358         @echo "Cleaning Generated Files"
359         $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
360                 configure.lineno config.status.lineno Makefile \
361                 Documentation/Makefile \
362                 config.log config.h config.h.in~ aclocal.m4 \
363                 configure autom4te.cache/ config/
364
365 $(CLEANDIRS):
366         @echo "Cleaning $(patsubst clean-%,%,$@)"
367         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
368
369 install: $(libs) $(progs_install) $(INSTALLDIRS)
370         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
371         $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
372         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
373         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
374         $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
375         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
376         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
377         cp -a $(lib_links) $(DESTDIR)$(libdir)
378         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
379         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
380
381 install-static: $(progs_static) $(INSTALLDIRS)
382         for p in $(progs_static) ; do \
383                 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/ ; \
384         done
385         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
386         $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
387
388 $(INSTALLDIRS):
389         @echo "Making install in $(patsubst install-%,%,$@)"
390         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
391
392 uninstall:
393         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
394         cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
395         $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
396         cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
397         cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
398
399 ifneq ($(MAKECMDGOALS),clean)
400 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
401 endif