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