forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
214 lines (176 loc) · 6.2 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
################################################################################
# Note: this makefile is used only for the libkxld build alias. It is not part
# of the regular kernel build process.
################################################################################
# Version
COMPATIBILITY_VERSION=1
CURRENT_VERSION=1
# Paths
ifndef DSTROOT
DSTROOT=./BUILD/dst
endif
ifndef OBJROOT
OBJROOT=./BUILD/obj
endif
ifndef SYMROOT
SYMROOT=./BUILD/sym
endif
ifdef SRCROOT
EXTHDRSRC=$(SRCROOT)/EXTERNAL_HEADERS
HDRSRC=$(SRCROOT)/libkern/libkern
OBJSRC=$(SRCROOT)/libkern/kxld
else
SRCROOT=.
EXTHDRSRC=$(SRCROOT)/../../EXTERNAL_HEADERS
HDRSRC=$(SRCROOT)/../libkern
OBJSRC=$(SRCROOT)
ROOT=BUILD
endif
ifdef RC_CFLAGS
ARCHS=$(addprefix -arch , $(RC_ARCHS))
else
ARCHS=-arch x86_64
RC_CFLAGS=$(ARCHS) -pipe
endif
ifdef INSTALL_LOCATION
override DSTROOT := $(DSTROOT)/$(INSTALL_LOCATION)
endif
PRODUCT_TYPE ?= DYLIB
HDRDST=$(DSTROOT)/usr/local/include
DYLIBDST=$(DSTROOT)/usr/lib/system
ARCHIVEDST=$(DSTROOT)/usr/local/lib
LIBKXLD_DYLIB=libkxld.dylib
LIBKXLD_ARCHIVE=libkxld.a
LIBKXLD_DSYM=$(LIBKXLD_DYLIB).dSYM
LIBKXLD_INSTALLNAME=/usr/lib/system/$(LIBKXLD_DYLIB)
LIBKXLDOBJ_DYLIB=$(OBJROOT)/$(LIBKXLD_DYLIB)
LIBKXLDOBJ_ARCHIVE=$(OBJROOT)/$(LIBKXLD_ARCHIVE)
LIBKXLDSYM_DYLIB=$(SYMROOT)/$(LIBKXLD_DYLIB)
LIBKXLDSYM_DYLIB_DSYM=$(SYMROOT)/$(LIBKXLD_DSYM)
LIBKXLDSYM_ARCHIVE=$(SYMROOT)/$(LIBKXLD_ARCHIVE)
LIBKXLDDST_DYLIB=$(DYLIBDST)/$(LIBKXLD_DYLIB)
LIBKXLDDST_ARCHIVE=$(ARCHIVEDST)/$(LIBKXLD_ARCHIVE)
TESTSRC=$(SRCROOT)/tests
TESTDST=./BUILD/tests
# Flags
ifdef SDKROOT
SDK_DIR := $(shell xcodebuild -version -sdk $(SDKROOT) Path)
endif
ifeq ($(strip $(SDK_DIR)),)
SDK_DIR := /
endif
DEFINES = -DPRIVATE
CFLAGS=-std=c99 -Wall -Wextra -Werror -pedantic -Wformat=2 -Wcast-align \
-Wwrite-strings -Wshorten-64-to-32 -Wshadow -Winit-self -Wpointer-arith \
-Wno-format-y2k -W -Wstrict-prototypes -Wmissing-prototypes -Wreturn-type \
-Wcast-qual -Wwrite-strings -Wswitch -Wcast-align -Wbad-function-cast \
-Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls -g
LDFLAGS=$(ARCHS) -dynamiclib -install_name $(LIBKXLD_INSTALLNAME) \
-compatibility_version $(COMPATIBILITY_VERSION) \
-current_version $(CURRENT_VERSION) -lstdc++
INCLUDES=-I$(HDRSRC) -isystem $(EXTHDRSRC)
ifneq ($(SDK_DIR),/)
CFLAGS += -isysroot $(SDK_DIR)
LDFLAGS += -isysroot $(SDK_DIR)
endif
# Tools
CC = xcrun -sdk $(SDK_DIR) clang
CLANG_ANALYZER = clang --analyze
LIBTOOL = xcrun -sdk $(SDK_DIR) libtool
STRIP = xcrun -sdk $(SDK_DIR) strip
DSYMUTIL = xcrun -sdk $(SDK_DIR) dsymutil
# Files
HDR_NAMES=kxld.h kxld_types.h prelink.h
OBJ_NAMES=kxld.o kxld_array.o kxld_copyright.o kxld_demangle.o kxld_dict.o \
kxld_kext.o kxld_object.o kxld_reloc.o kxld_sect.o kxld_seg.o \
kxld_srcversion.o kxld_splitinfolc.o kxld_sym.o kxld_symtab.o kxld_util.o kxld_uuid.o \
kxld_vtable.o kxld_versionmin.o
HDRS=$(addprefix $(HDRSRC)/, $(HDR_NAMES))
OBJS=$(addprefix $(OBJROOT)/, $(OBJ_NAMES))
$(shell [ -d $(OBJROOT) ] || mkdir -p $(OBJROOT))
# Implicit rules
%.o : %.c
$(OBJROOT)/%.o : $(OBJSRC)/%.c
$(CC) $(RC_CFLAGS) $(CFLAGS) $(DEFINES) $(OPTIM) $(INCLUDES) -c $< -o $@
$(OBJROOT)/%.o : $(TESTSRC)/%.c
$(CC) $(RC_CFLAGS) $(CFLAGS) $(DEFINES) -O0 -DDEBUG $(INCLUDES) -I$(SRCROOT) -c $< -o $@
SRCROOTESC=$(subst /,\/,$(SRCROOT))
OBJROOTESC=$(subst /,\/,$(OBJROOT))
SEDOBJS=sed -E 's/(^[a-z_]+)\.o/$(OBJROOTESC)\/\1\.o $(OBJROOTESC)\/\1\.d/'
SEDSRCS=sed -E 's/ ([a-z_]+\.[ch])/ $(SRCROOTESC)\/\1/g'
$(OBJROOT)/%.d: $(OBJSRC)/%.c
@set -e; rm -f $@; \
$(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -MM $< | $(SEDOBJS) | $(SEDSRCS) > $@;
# Rules
release: OPTIM=-Os -dynamic
release: build
debug: OPTIM=-O0 -DDEBUG -dynamic
debug: build
profile: OPTIM=-Os -pg -dynamic
profile: build
tests: OPTIM=-O0 -DDEBUG
tests: kxld_array_test kxld_dict_test copyrighttest
build: $(LIBKXLDSYM_$(PRODUCT_TYPE))
$(LIBKXLDSYM_DYLIB): $(LIBKXLDOBJ_DYLIB)
@mkdir -p $(SYMROOT)
install -c -m 644 $< $@
$(DSYMUTIL) $@ -o $(LIBKXLDSYM_DYLIB_DSYM)
$(LIBKXLDSYM_ARCHIVE): $(LIBKXLDOBJ_ARCHIVE)
@mkdir -p $(SYMROOT)
install -c -m 644 $< $@
$(LIBKXLDOBJ_DYLIB): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^
$(LIBKXLDOBJ_ARCHIVE): $(OBJS)
$(LIBTOOL) -static -o $@ $^
installhdrs:
@mkdir -p $(HDRDST)
install -o 0 -g 0 -c -m 444 $(HDRS) $(HDRDST)
install: release installhdrs $(LIBKXLDDST_$(PRODUCT_TYPE))
$(LIBKXLDDST_DYLIB): $(LIBKXLDSYM_DYLIB)
@mkdir -p $(DYLIBDST)
install -o 0 -g 0 -c -m 555 $< $@
$(STRIP) -S -x $@
$(LIBKXLDDST_ARCHIVE): $(LIBKXLDSYM_ARCHIVE)
@mkdir -p $(ARCHIVEDST)
install -o 0 -g 0 -c -m 555 $< $@
KEXTCOPYOBJS=$(OBJROOT)/kextcopyright.o $(OBJROOT)/kxld_copyright.o $(OBJROOT)/kxld_util.o
kextcopyright: $(TESTDST)/kextcopyright
$(TESTDST)/kextcopyright: $(KEXTCOPYOBJS)
@mkdir -p $(TESTDST)
$(CC) $(ARCHS) $(KEXTCOPYOBJS) -framework CoreFoundation -o $(OBJROOT)/kextcopyright
install -c -m 755 $(OBJROOT)/kextcopyright $@
TESTOBJS=$(OBJROOT)/kxld_test.o $(OBJROOT)/kxld_util.o
ARRAYOBJS=$(OBJROOT)/kxld_array_test.o $(OBJROOT)/kxld_array.o $(TESTOBJS)
kxld_array_test: $(TESTDST)/kxld_array_test
$(TESTDST)/kxld_array_test: $(ARRAYOBJS)
@mkdir -p $(TESTDST)
$(CC) $(ARCHS) $(ARRAYOBJS) -o $(OBJROOT)/kxld_array_test
install -c -m 755 $(OBJROOT)/kxld_array_test $@
DICTOBJS=$(OBJROOT)/kxld_dict_test.o $(OBJROOT)/kxld_dict.o $(OBJROOT)/kxld_array.o $(TESTOBJS)
kxld_dict_test: $(TESTDST)/kxld_dict_test
$(TESTDST)/kxld_dict_test: $(DICTOBJS)
@mkdir -p $(TESTDST)
$(CC) $(ARCHS) $(DICTOBJS) -o $(OBJROOT)/kxld_dict_test
install -c -m 755 $(OBJROOT)/kxld_dict_test $@
COPYTESTOBJS=$(OBJROOT)/kxld_copyright.o $(OBJROOT)/kxld_util.o
copyrighttest: OPTIM+=-DTEST
copyrighttest: $(TESTDST)/copyrighttest
$(TESTDST)/copyrighttest: $(COPYTESTOBJS)
@mkdir -p $(TESTDST)
$(CC) $(ARCHS) $(COPYTESTOBJS) -framework CoreFoundation -framework IOKit -o $(OBJROOT)/copyrighttest
install -c -m 755 $(OBJROOT)/copyrighttest $@
analyze:
@$(CLANG_ANALYZER) $(CFLAGS) $(INCLUDES) $(filter-out WKdm%.c,$(wildcard *.c))
@$(CLANG_ANALYZER) $(CFLAGS) $(INCLUDES) -I$(SRCROOT) tests/*.c
@rm -f *.plist
clean:
@rm -rf $(OBJROOT)/*
fullclean:
ifdef ROOT
@rm -rf $(ROOT)
else
@rm -rf $(OBJROOT) $(DSTROOT) $(SYMROOT)
endif
# Automatically build dependency information when .c or .h files change based
# on implicit rule for .d:.c
-include $(OBJS:.o=.d)