1SDKROOT ?= /
2Product=$(shell tconf --product)
3Embedded=$(shell tconf --test TARGET_OS_EMBEDDED)
4
5SDKVERSION=$(shell xcodebuild -sdk $(SDKROOT) -version SDKVersion | head -1)
6
7ifeq "$(Embedded)" "YES"
8XILogFLAG =
9SDKPATH = $(shell xcodebuild -sdk $(SDKROOT) -version Path)
10CFLAGS += -isysroot $(SDKPATH) -miphoneos-version-min=$(SDKVERSION)
11LIBFLAGS += -isysroot $(SDKPATH) -miphoneos-version-min=$(SDKVERSION)
12else
13XILogFLAG = -framework XILog
14CFLAGS += -mmacosx-version-min=$(SDKVERSION)
15LIBFLAGS += -mmacosx-version-min=$(SDKVERSION)
16endif
17
18HOSTCC = cc
19CC = xcrun -sdk $(SDKROOT) cc
20
21ifdef RC_BUILDIT
22DOING_BUILDIT=yes
23endif
24
25ifdef RC_OS
26DOING_BUILDIT=yes
27endif
28
29ifdef DOING_BUILDIT
30include $(MAKEFILEPATH)/CoreOS/ReleaseControl/Common.make
31MY_ARCH = $(patsubst %, -arch %, $(RC_ARCHS)) 
32install:: xnu_quick_test
33else
34	ifndef SRCROOT
35		SRCROOT=$(shell /bin/pwd)
36	endif
37	ifndef OBJROOT
38		OBJROOT=$(SRCROOT)/BUILD/obj
39	endif
40	ifndef DSTROOT
41		DSTROOT=$(SRCROOT)/BUILD/dst
42	endif
43	
44	ifndef ARCH
45		ARCH=i386 x86_64
46		# this hack should be removed once tconf gets
47		# <rdar://problem/6618734>
48		ifeq "$(Product)" "iPhone"
49		ARCH=armv7
50		endif
51		ifeq "$(Product)" "AppleTV"
52		ARCH=i386
53		endif
54	endif
55	
56	ifdef ARCH
57		MY_ARCH = $(patsubst %, -arch %, $(ARCH)) # allows building multiple archs.
58	endif
59
60	CFLAGS += $(MY_ARCH)
61endif
62
63
64CFLAGS += -g -I $(SDKPATH)/System/Library/Frameworks/System.framework/Versions/B/PrivateHeaders/ -F/AppleInternal/Library/Frameworks/ $(MORECFLAGS) -Wno-deprecated-declarations
65LIBFLAGS += -I $(SDKPATH)/System/Library/Frameworks/System.framework/Versions/B/PrivateHeaders  -F/AppleInternal/Library/Frameworks/ $(XILogFLAG)
66
67# The current implementation of the content protection test requires IOKit.
68ifeq "$(Product)" "iPhone"
69LIBFLAGS += -framework IOKit
70endif
71
72MY_OBJECTS = $(OBJROOT)/main.o $(OBJROOT)/memory_tests.o $(OBJROOT)/misc.o \
73			 $(OBJROOT)/sema_tests.o $(OBJROOT)/shared_memory_tests.o \
74			 $(OBJROOT)/socket_tests.o $(OBJROOT)/tests.o \
75			 $(OBJROOT)/xattr_tests.o $(OBJROOT)/kqueue_tests.o \
76			 $(OBJROOT)/machvm_tests.o $(OBJROOT)/commpage_tests.o \
77			 $(OBJROOT)/atomic_fifo_queue_test.o $(OBJROOT)/sched_tests.o \
78			 $(OBJROOT)/pipes_tests.o
79
80ifneq "$(Product)" "iPhone"
81MY_OBJECTS += $(OBJROOT)/32bit_inode_tests.o
82else
83MY_OBJECTS += $(OBJROOT)/content_protection_test.o
84endif
85
86# In networked home directories, the chown will fail; we notice and print a helpful message
87CHOWN_COMMAND=sudo chown root $(DSTROOT)/xnu_quick_test
88PERM_ADVICE="\tYou'll have to set the executable's permissions yourself: chown to root and chmod to 4755. You may need to move to a local volume to do that."
89xnu_quick_test : $(OBJROOT) $(DSTROOT) $(MY_OBJECTS) helpers
90	sudo rm -rf $(DSTROOT)/xnu_quick_test
91	$(CC) $(MY_ARCH) $(LIBFLAGS) -o $(DSTROOT)/xnu_quick_test $(MY_OBJECTS)
92	@echo $(CHOWN_COMMAND) # Hack so we don't echo help-message echo
93	@$(CHOWN_COMMAND) || echo $(PERM_ADVICE)
94	sudo chmod 4755 $(DSTROOT)/xnu_quick_test
95
96# This target is defined for testbots. 
97# Before compiling this target, MORECFLAGS must be set to "-D RUN_UNDER_TESTBOTS=1", check README file for more details
98# NOTE: -f[ailures] MAX_FAILS_ALLOWED option is set to 100 to make sure we completely run the test suite and 
99# report all the failures.
100
101testbots: xnu_quick_test 
102	@(cd $(DSTROOT) ; ./xnu_quick_test -f 100)	
103
104# The helper binaries are used to test exec()'ing between 64bit and 32bit. 
105# Creates test binaries with page zero sizes = 4KB and 4GB. Also creates 32-bit
106# helper processes for the 64-bit version of xnu_quick_test to test the conversion
107# from a 32-bit process to a 64-bit process.
108helpers : helpers/sleep.c helpers/launch.c helpers/arch.c helpers/data_exec.c helperdir $(OBJROOT)/misc.o
109ifneq "$(Product)" "iPhone"
110	$(CC) -arch i386                              helpers/sleep.c -o $(DSTROOT)/helpers/sleep-i386
111endif
112ifeq "$(Product)" "MacOSX"
113	$(CC) -arch x86_64 -pagezero_size 0x100000000 helpers/sleep.c -o $(DSTROOT)/helpers/sleep-x86_64-4G
114	$(CC) -arch x86_64 -pagezero_size 0x1000      helpers/sleep.c -o $(DSTROOT)/helpers/sleep-x86_64-4K
115endif
116ifneq "$(Product)" "iPhone"
117	$(CC) $(LIBFLAGS) -arch i386	$(OBJROOT)/misc.o helpers/launch.c -o $(DSTROOT)/helpers/launch-i386
118endif
119ifeq "$(Product)" "MacOSX"
120	$(CC) $(LIBFLAGS) -arch x86_64	$(OBJROOT)/misc.o helpers/launch.c -o $(DSTROOT)/helpers/launch-x86_64
121	$(CC) $(MY_ARCH) 	helpers/arch.c -o $(DSTROOT)/helpers/arch
122	$(CC) $(MY_ARCH) 	helpers/data_exec.c -o $(DSTROOT)/helpers/data_exec
123	$(CC) -arch i386 	-DNXDATA32TESTNONX helpers/data_exec.c -o $(DSTROOT)/helpers/data_exec32nonxspawn
124
125endif
126ifeq "$(Product)" "iPhone"
127	$(CC) $(CFLAGS) helpers/sleep.c -o $(DSTROOT)/helpers/sleep-arm
128	$(CC) $(LIBFLAGS) $(CFLAGS) $(OBJROOT)/misc.o helpers/launch.c -o $(DSTROOT)/helpers/launch-arm
129	$(CC) $(MY_ARCH) $(CFLAGS) helpers/arch.c -o $(DSTROOT)/helpers/arch
130endif
131	
132	
133helperdir :
134	mkdir -p $(DSTROOT)/helpers
135
136$(OBJROOT) :
137	mkdir -p $(OBJROOT);
138	
139$(DSTROOT) :
140	mkdir -p $(DSTROOT);
141
142$(OBJROOT)/main.o : main.c tests.h
143	$(CC) $(CFLAGS) -c main.c  -o $@
144	
145$(OBJROOT)/memory_tests.o : memory_tests.c tests.h
146	$(CC) $(CFLAGS) -c memory_tests.c  -o $@
147
148# misc.o has to be built 3-way for the helpers to link
149$(OBJROOT)/misc.o : misc.c tests.h
150ifeq "$(Product)" "iPhone"
151	$(CC) -arch armv7 $(CFLAGS) -c misc.c   -o $@
152else
153	$(CC) -arch i386 -arch x86_64 $(CFLAGS) -c misc.c   -o $@
154endif
155	
156$(OBJROOT)/sema_tests.o : sema_tests.c tests.h
157	$(CC) $(CFLAGS) -c sema_tests.c   -o $@
158	
159$(OBJROOT)/shared_memory_tests.o : shared_memory_tests.c tests.h
160	$(CC) $(CFLAGS) -c shared_memory_tests.c   -o $@
161
162$(OBJROOT)/socket_tests.o : socket_tests.c tests.h
163	$(CC) $(CFLAGS) -c socket_tests.c   -o $@
164
165$(OBJROOT)/tests.o : tests.c tests.h
166	$(CC) $(CFLAGS) -c tests.c    -o $@
167
168$(OBJROOT)/xattr_tests.o : xattr_tests.c tests.h
169	$(CC) $(CFLAGS) -c xattr_tests.c    -o $@
170
171$(OBJROOT)/machvm_tests.o : machvm_tests.c tests.h
172	$(CC) $(CFLAGS) -c machvm_tests.c    -o $@
173
174$(OBJROOT)/sched_tests.o : sched_tests.c tests.h
175	$(CC) $(CFLAGS) -c sched_tests.c    -o $@
176
177$(OBJROOT)/kqueue_tests.o : kqueue_tests.c tests.h
178	$(CC) $(CFLAGS) -c kqueue_tests.c   -o $@
179
180$(OBJROOT)/32bit_inode_tests.o : 32bit_inode_tests.c tests.h
181	$(CC) $(CFLAGS) -c 32bit_inode_tests.c    -o $@
182
183$(OBJROOT)/commpage_tests.o : commpage_tests.c tests.h
184	$(CC) $(CFLAGS) -c commpage_tests.c    -o $@
185	
186$(OBJROOT)/atomic_fifo_queue_test.o : atomic_fifo_queue_test.c tests.h
187	$(CC) $(CFLAGS) -c atomic_fifo_queue_test.c    -o $@
188
189$(OBJROOT)/content_protection_test.o : content_protection_test.c tests.h
190	$(CC) $(CFLAGS) -c content_protection_test.c -o $@
191
192$(OBJROOT)/pipes_tests.o : pipes_tests.c tests.h
193	$(CC) $(CFLAGS) -c pipes_tests.c -o $@
194
195ifndef DOING_BUILDIT
196.PHONY : clean
197clean :
198	sudo rm -Rf $(DSTROOT)/xnu_quick_test
199	sudo rm -Rf $(DSTROOT)/helpers/*
200	rm -Rf $(OBJROOT)/*.o
201endif
202