1# SPDX-License-Identifier: GPL-2.0
2include ../../scripts/Makefile.include
3include ../../scripts/utilities.mak		# QUIET_CLEAN
4
5ifeq ($(srctree),)
6srctree := $(patsubst %/,%,$(dir $(CURDIR)))
7srctree := $(patsubst %/,%,$(dir $(srctree)))
8srctree := $(patsubst %/,%,$(dir $(srctree)))
9#$(info Determined 'srctree' to be $(srctree))
10endif
11
12CC ?= $(CROSS_COMPILE)gcc
13AR ?= $(CROSS_COMPILE)ar
14LD ?= $(CROSS_COMPILE)ld
15
16MAKEFLAGS += --no-print-directory
17
18INSTALL = install
19
20
21# Use DESTDIR for installing into a different root directory.
22# This is useful for building a package. The program will be
23# installed in this directory as if it was the root directory.
24# Then the build tool can move it later.
25DESTDIR ?=
26DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
27
28LIBFILE = $(OUTPUT)libapi.a
29
30CFLAGS := $(EXTRA_WARNINGS) $(EXTRA_CFLAGS)
31CFLAGS += -ggdb3 -Wall -Wextra -std=gnu99 -U_FORTIFY_SOURCE -fPIC
32
33ifeq ($(DEBUG),0)
34ifeq ($(CC_NO_CLANG), 0)
35  CFLAGS += -O3
36else
37  CFLAGS += -O6
38endif
39endif
40
41ifeq ($(DEBUG),0)
42  CFLAGS += -D_FORTIFY_SOURCE
43endif
44
45# Treat warnings as errors unless directed not to
46ifneq ($(WERROR),0)
47  CFLAGS += -Werror
48endif
49
50CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
51CFLAGS += -I$(srctree)/tools/lib/api
52CFLAGS += -I$(srctree)/tools/include
53
54RM = rm -f
55
56API_IN := $(OUTPUT)libapi-in.o
57
58ifeq ($(LP64), 1)
59  libdir_relative = lib64
60else
61  libdir_relative = lib
62endif
63
64prefix ?=
65libdir = $(prefix)/$(libdir_relative)
66
67# Shell quotes
68libdir_SQ = $(subst ','\'',$(libdir))
69
70all:
71
72export srctree OUTPUT CC LD CFLAGS V
73include $(srctree)/tools/build/Makefile.include
74include $(srctree)/tools/scripts/Makefile.include
75
76all: fixdep $(LIBFILE)
77
78$(API_IN): FORCE
79	@$(MAKE) $(build)=libapi
80
81$(LIBFILE): $(API_IN)
82	$(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(API_IN)
83
84define do_install_mkdir
85	if [ ! -d '$(DESTDIR_SQ)$1' ]; then             \
86		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
87	fi
88endef
89
90define do_install
91	if [ ! -d '$2' ]; then             \
92		$(INSTALL) -d -m 755 '$2'; \
93	fi;                                \
94	$(INSTALL) $1 $(if $3,-m $3,) '$2'
95endef
96
97install_lib: $(LIBFILE)
98	$(call QUIET_INSTALL, $(LIBFILE)) \
99		$(call do_install_mkdir,$(libdir_SQ)); \
100		cp -fpR $(LIBFILE) $(DESTDIR)$(libdir_SQ)
101
102HDRS := cpu.h debug.h io.h
103FD_HDRS := fd/array.h
104FS_HDRS := fs/fs.h fs/tracing_path.h
105INSTALL_HDRS_PFX := $(DESTDIR)$(prefix)/include/api
106INSTALL_HDRS := $(addprefix $(INSTALL_HDRS_PFX)/, $(HDRS))
107INSTALL_FD_HDRS := $(addprefix $(INSTALL_HDRS_PFX)/, $(FD_HDRS))
108INSTALL_FS_HDRS := $(addprefix $(INSTALL_HDRS_PFX)/, $(FS_HDRS))
109
110$(INSTALL_HDRS): $(INSTALL_HDRS_PFX)/%.h: %.h
111	$(call QUIET_INSTALL, $@) \
112		$(call do_install,$<,$(INSTALL_HDRS_PFX)/,644)
113
114$(INSTALL_FD_HDRS): $(INSTALL_HDRS_PFX)/fd/%.h: fd/%.h
115	$(call QUIET_INSTALL, $@) \
116		$(call do_install,$<,$(INSTALL_HDRS_PFX)/fd/,644)
117
118$(INSTALL_FS_HDRS): $(INSTALL_HDRS_PFX)/fs/%.h: fs/%.h
119	$(call QUIET_INSTALL, $@) \
120		$(call do_install,$<,$(INSTALL_HDRS_PFX)/fs/,644)
121
122install_headers: $(INSTALL_HDRS) $(INSTALL_FD_HDRS) $(INSTALL_FS_HDRS)
123	$(call QUIET_INSTALL, libapi_headers)
124
125install: install_lib install_headers
126
127clean:
128	$(call QUIET_CLEAN, libapi) $(RM) $(LIBFILE); \
129	find $(or $(OUTPUT),.) -name \*.o -or -name \*.o.cmd -or -name \*.o.d | xargs $(RM)
130
131FORCE:
132
133.PHONY: clean FORCE
134