1# Project name and version
2export RC_ProjectName := Libsyscall
3export RC_ProjectSourceVersion := 2050
4
5# Build tools used
6CC := $(shell xcrun -find clang)
7MIG := $(shell xcrun -find mig)
8LIBTOOL := $(shell xcrun -find libtool)
9
10# User-specified variables
11RC_ARCHS ?= armv7
12RC_PLATFORM ?= MacOSX
13DESTDIR ?= /
14
15# Path to xnu kernel source
16KERNEL_SOURCE ?= ../
17
18# Local project variables
19export SRCROOT := $(CURDIR)
20
21ifndef SDKROOT
22	export SDKROOT := $(shell xcrun -show-sdk-path)
23endif
24
25ifndef DEVELOPER_DIR
26	export DEVELOPER_DIR := $(shell xcode-select -print-path)
27endif
28
29export ARCHS := $(RC_ARCHS)
30export PLATFORM := $(RC_PLATFORM)
31
32BUILDDIR := Build
33BUILDROOT := $(BUILDDIR)/$(ARCHS)
34BUILDOBJS := $(BUILDROOT)/Objects
35GENROOT := $(BUILDROOT)/GeneratedSources
36
37export OBJROOT := $(SRCROOT)
38export DSTROOT := /tmp/$(RC_ProjectName).dst
39export DERIVED_SOURCES_DIR := $(GENROOT)
40
41# Our library name
42LIB := libsystem_kernel
43
44# C compiler flags
45CFLAGS :=
46
47CFLAGS_ARGS := \
48	-arch $(ARCHS) \
49	-Os \
50	-fstrict-aliasing \
51	-fpascal-strings \
52	-fmessage-length=0 \
53	-std=gnu99 \
54	-fdollars-in-identifiers \
55	-no-cpp-precomp \
56	-fno-common \
57	-fno-stack-protector \
58	-pipe
59
60CFLAGS_DEFS := \
61	-DLIBSYSCALL_INTERFACE \
62	-D__DARWIN_VERS_1050=1 \
63	-DSYSCALL_PRE1050 \
64	-DCF_OPEN_SOURCE \
65	-DCF_EXCLUDE_CSTD_HEADERS \
66	-DDEBUG \
67	-D_FORTIFY_SOURCE=0
68#	-DTARGET_OS_EMBEDDED=1
69
70CFLAGS_WARNS := \
71	-Wno-trigraphs \
72	-Wno-missing-field-initializers \
73	-Wno-missing-prototypes \
74	-Wreturn-type -Wformat \
75	-Wno-missing-braces \
76	-Wparentheses -Wswitch \
77	-Wno-unused-function \
78	-Wno-unused-label \
79	-Wno-unused-parameter \
80	-Wunused-variable \
81	-Wunused-value \
82	-Wno-empty-body \
83	-Wno-uninitialized \
84	-Wno-unknown-pragmas \
85	-Wno-shadow \
86	-Wno-four-char-constants \
87	-Wno-conversion \
88	-Wno-constant-conversion \
89	-Wno-int-conversion \
90	-Wno-enum-conversion \
91	-Wno-shorten-64-to-32 \
92	-Wpointer-sign \
93	-Wnewline-eof \
94	-Wmost
95
96CFLAGS_INCS := \
97	-I$(GENROOT) \
98	-I$(SDKROOT)/System/Library/Frameworks/System.framework/Versions/B/PrivateHeaders
99
100# Mach Interface Generator flags
101MIGFLAGS += $(CFLAGS_INCS)
102
103# Libtool flags
104LIBTOOL_FLAGS += -arch_only $(ARCHS) -syslibroot $(SDKROOT) -L$(BUILDROOT) 
105
106# Subdirectories in this project
107MODULES := \
108	custom \
109	mach \
110	mach/arm \
111	mach/servers \
112	wrappers \
113	wrappers/unix03 \
114	wrappers/legacy \
115	wrappers/cancelable
116
117# Populate our CFLAGS
118CFLAGS += $(CFLAGS_ARGS)
119CFLAGS += $(CFLAGS_DEFS)
120CFLAGS += $(patsubst %,-I%/,$(MODULES))
121CFLAGS += $(CFLAGS_INCS)
122CFLAGS += $(CFLAGS_WARNS)
123
124# Misc. source code variables
125S_SRCS :=
126C_SRCS :=
127MIG_USR_SRCS :=
128MIG_SRV_SRCS :=
129
130# Pull in information from throughout the project
131include $(patsubst %,%/module.mk,$(MODULES))
132
133# Compiled objects to resolve from source code
134OBJS := \
135	$(patsubst %.defs,%Server.o, $(filter %.defs,$(MIG_SRV_SRCS))) \
136	$(patsubst %.defs,%User.o, $(filter %.defs,$(MIG_USR_SRCS))) \
137	$(patsubst %.defs,%User.o, $(filter %.defs,$(MIG_SRV_SRCS))) \
138	$(patsubst %.s,%.o, $(filter %.s,$(S_SRCS))) \
139	$(patsubst %.c,%.o, $(filter %.c,$(C_SRCS)))
140
141# Compilation rules for various source code types
142%.o: %.c
143	$(eval obj_uuid := $(shell uuidgen -r))
144	$(CC) -x c $(CFLAGS) -c $< -o $(BUILDOBJS)/$(notdir $*-$(obj_uuid).o)
145	@echo "$(BUILDOBJS)/$(notdir $*-$(obj_uuid).o)" >> $(BUILDOBJS)/Objects.list
146
147%.o: %.s
148	$(eval obj_uuid := $(shell uuidgen -r))
149	$(CC) -x assembler-with-cpp $(CFLAGS) -c $< -o $(BUILDOBJS)/$(notdir $*-$(obj_uuid).o)
150	@echo "$(BUILDOBJS)/$(notdir $*-$(obj_uuid).o)" >> $(BUILDOBJS)/Objects.list
151
152%User.o: %.defs
153	$(eval obj_uuid := $(shell uuidgen -r))
154	$(MIG) $(MIGFLAGS) -arch $(ARCHS) -cc $(CC) -user $(GENROOT)/$(notdir $*User.c) -server /dev/null -header $(GENROOT)/$(notdir $*.h) -sheader /dev/null -dheader /dev/null $*.defs
155	$(CC) -x c $(CFLAGS) -c $(GENROOT)/$(notdir $*User.c) -o $(BUILDOBJS)/$(notdir $*User-$(obj_uuid).o)
156	@echo "$(BUILDOBJS)/$(notdir $*User-$(obj_uuid).o)" >> $(BUILDOBJS)/Objects.list
157	rm -f $(notdir $**.h) $(notdir $**.d)
158
159%Server.o: %.defs
160	$(eval obj_uuid := $(shell uuidgen -r))
161	$(MIG) $(MIGFLAGS) -arch $(ARCHS) -cc $(CC) -server $(GENROOT)/$(notdir $*Server.c) -user $(GENROOT)/$(notdir $*User.c) -header $(GENROOT)/$(notdir $*.h) -sheader $(GENROOT)/$(notdir $*Server.h) -dheader /dev/null $*.defs
162	$(CC) -x c $(CFLAGS) -c $(GENROOT)/$(notdir $*Server.c) -o $(BUILDOBJS)/$(notdir $*Server-$(obj_uuid).o)
163	@echo "$(BUILDOBJS)/$(notdir $*Server-$(obj_uuid).o)" >> $(BUILDOBJS)/Objects.list
164	rm -f $(notdir $**.h) $(notdir $**.d)
165
166# Main project rules
167.PHONY: all install clean
168.SECONDARY: $(LIB)
169
170all: setup $(LIB)
171
172setup:
173	rm -rf $(BUILDROOT)
174	mkdir -p $(GENROOT)
175	mkdir -p $(BUILDOBJS)/sys
176	$(DEVELOPER_DIR)/Makefiles/bin/version.pl $(RC_ProjectName) > $(GENROOT)/$(RC_ProjectName)_version.c
177	$(CC) $(CFLAGS) -c $(GENROOT)/$(RC_ProjectName)_version.c -o $(BUILDROOT)/$(RC_ProjectName)_version.o
178	xcodescripts/create-syscalls.pl $(KERNEL_SOURCE)/bsd/kern/syscalls.master custom/ Platforms/ $(PLATFORM) $(BUILDOBJS)/sys
179	xcodescripts/compile-syscalls.pl $(BUILDOBJS)/sys/stubs.list $(BUILDROOT)/syscalls.a
180	xcodescripts/mach_install_mig.sh
181
182$(LIB): $(OBJS)
183	$(LIBTOOL) $(LIBTOOL_FLAGS) -static -filelist $(BUILDOBJS)/Objects.list $(BUILDROOT)/syscalls.a $(BUILDROOT)/$(RC_ProjectName)_version.o -o $(BUILDROOT)/$@.a
184	$(CC) -arch $(ARCHS) -dynamiclib -isysroot $(SDKROOT) -L$(BUILDROOT) -F$(BUILDROOT) -install_name /usr/lib/system/$(LIB).dylib -nostdlib -mmacosx-version-min=10.8 -dead_strip -umbrella System -all_load -lsystem_kernel -single_module -compatibility_version 1 -o $(BUILDROOT)/$(LIB).dylib
185
186install: all
187	install -d $(DESTDIR)/usr/lib/system
188	install -d $(DESTDIR)/usr/include/servers
189	install -d $(DESTDIR)/usr/include/mach/arm
190	rsync -avPr $(DSTROOT)/ $(DESTDIR)/
191	install -m 755 $(BUILDROOT)/$(LIB).dylib $(DESTDIR)/usr/lib/system/$(LIB).dylib
192
193clean:
194	rm -rf $(BUILDDIR) $(DSTROOT)
195