]> git.saurik.com Git - apple/dyld.git/blob - testing/build_tests.py
dyld-519.2.2.tar.gz
[apple/dyld.git] / testing / build_tests.py
1 #!/usr/bin/python2.7
2
3 import plistlib
4 import string
5 import argparse
6 import sys
7 import os
8 import tempfile
9 import shutil
10 import subprocess
11
12
13 #
14 # Scan files in .dtest directory looking for BUILD: or RUN: directives.
15 # Return a dictionary of all directives.
16 #
17 def parseDirectives(testCaseSourceDir):
18 onlyLines = []
19 buildLines = []
20 runLines = []
21 minOS = ""
22 timeout = ""
23 for file in os.listdir(testCaseSourceDir):
24 if file.endswith((".c", ".cpp", ".cxx")):
25 with open(testCaseSourceDir + "/" + file) as f:
26 for line in f.read().splitlines():
27 buildIndex = string.find(line, "BUILD:")
28 if buildIndex != -1:
29 buildLines.append(line[buildIndex + 6:].lstrip())
30 runIndex = string.find(line, "RUN:")
31 if runIndex != -1:
32 runLines.append(line[runIndex+4:].lstrip())
33 onlyIndex = string.find(line, "BUILD_ONLY:")
34 if onlyIndex != -1:
35 onlyLines.append(line[onlyIndex+11:].lstrip())
36 minOsIndex = string.find(line, "BUILD_MIN_OS:")
37 if minOsIndex != -1:
38 minOS = line[minOsIndex+13:].lstrip()
39 timeoutIndex = string.find(line, "RUN_TIMEOUT:")
40 if timeoutIndex != -1:
41 timeout = line[timeoutIndex+12:].lstrip()
42
43 return {
44 "BUILD": buildLines,
45 "BUILD_ONLY": onlyLines,
46 "BUILD_MIN_OS": minOS,
47 "RUN": runLines,
48 "RUN_TIMEOUT": timeout
49 }
50
51
52 #
53 # Look at directives dictionary to see if this test should be skipped for this platform
54 #
55 def useTestCase(testCaseDirectives, platformName):
56 onlyLines = testCaseDirectives["BUILD_ONLY"]
57 for only in onlyLines:
58 if only == "MacOSX" and platformName != "macosx":
59 return False
60 if only == "iOS" and platformName != "iphoneos":
61 return False
62 return True
63
64
65 #
66 # Use BUILD directives to construct the test case
67 # Use RUN directives to generate a shell script to run test(s)
68 #
69 def buildTestCase(testCaseDirectives, testCaseSourceDir, toolsDir, sdkDir, minOsOptionsName, defaultMinOS, archOptions, testCaseDestDirBuild, testCaseDestDirRun):
70 scratchDir = tempfile.mkdtemp()
71 if testCaseDirectives["BUILD_MIN_OS"]:
72 minOS = testCaseDirectives["BUILD_MIN_OS"]
73 else:
74 minOS = defaultMinOS
75 compilerSearchOptions = " -isysroot " + sdkDir + " -I" + sdkDir + "/System/Library/Frameworks/System.framework/PrivateHeaders"
76 if minOsOptionsName == "mmacosx-version-min":
77 taskForPidCommand = "touch "
78 envEnableCommand = "touch "
79 else:
80 taskForPidCommand = "codesign --force --sign - --entitlements " + testCaseSourceDir + "/../../task_for_pid_entitlement.plist "
81 envEnableCommand = "codesign --force --sign - --entitlements " + testCaseSourceDir + "/../../get_task_allow_entitlement.plist "
82 buildSubs = {
83 "CC": toolsDir + "/usr/bin/clang " + archOptions + " -" + minOsOptionsName + "=" + str(minOS) + compilerSearchOptions,
84 "CXX": toolsDir + "/usr/bin/clang++ " + archOptions + " -" + minOsOptionsName + "=" + str(minOS) + compilerSearchOptions,
85 "BUILD_DIR": testCaseDestDirBuild,
86 "RUN_DIR": testCaseDestDirRun,
87 "TEMP_DIR": scratchDir,
88 "TASK_FOR_PID_ENABLE": taskForPidCommand,
89 "DYLD_ENV_VARS_ENABLE": envEnableCommand
90 }
91 os.makedirs(testCaseDestDirBuild)
92 os.chdir(testCaseSourceDir)
93 print >> sys.stderr, "cd " + testCaseSourceDir
94 for line in testCaseDirectives["BUILD"]:
95 cmd = string.Template(line).safe_substitute(buildSubs)
96 print >> sys.stderr, cmd
97 if "&&" in cmd:
98 result = subprocess.call(cmd, shell=True)
99 else:
100 cmdList = []
101 cmdList = string.split(cmd)
102 result = subprocess.call(cmdList)
103 if result:
104 return result
105 shutil.rmtree(scratchDir, ignore_errors=True)
106 sudoSub = ""
107 if minOsOptionsName == "mmacosx-version-min":
108 sudoSub = "sudo"
109 runSubs = {
110 "RUN_DIR": testCaseDestDirRun,
111 "REQUIRE_CRASH": "nocr -require_crash",
112 "SUDO": sudoSub,
113 }
114 runFilePath = testCaseDestDirBuild + "/run.sh"
115 with open(runFilePath, "a") as runFile:
116 runFile.write("#!/bin/sh\n")
117 runFile.write("cd " + testCaseDestDirRun + "\n")
118 os.chmod(runFilePath, 0755)
119 for runline in testCaseDirectives["RUN"]:
120 runFile.write(string.Template(runline).safe_substitute(runSubs) + "\n")
121 runFile.write("\n")
122 runFile.close()
123 return 0
124
125
126
127 #
128 # Use XCode build settings to build all unit tests for specified platform
129 # Generate a .plist for BATS to use to run all tests
130 #
131 if __name__ == "__main__":
132 dstDir = os.getenv("DSTROOT", "/tmp/dyld_tests/")
133 testsRunDstTopDir = "/AppleInternal/CoreOS/tests/dyld/"
134 testsBuildDstTopDir = dstDir + testsRunDstTopDir
135 shutil.rmtree(testsBuildDstTopDir, ignore_errors=True)
136 dyldSrcDir = os.getenv("SRCROOT", "")
137 if not dyldSrcDir:
138 dyldSrcDir = os.getcwd()
139 testsSrcTopDir = dyldSrcDir + "/testing/test-cases/"
140 sdkDir = os.getenv("SDKROOT", "")
141 if not sdkDir:
142 #sdkDir = subprocess.check_output(["xcrun", "--show-sdk-path"]).rstrip()
143 sdkDir = "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.Internal.sdk"
144 toolsDir = os.getenv("TOOLCHAIN_DIR", "/")
145 defaultMinOS = ""
146 minVersNum = "10.12"
147 minOSOption = os.getenv("DEPLOYMENT_TARGET_CLANG_FLAG_NAME", "")
148 if minOSOption:
149 minOSVersName = os.getenv("DEPLOYMENT_TARGET_CLANG_ENV_NAME", "")
150 if minOSVersName:
151 minVersNum = os.getenv(minOSVersName, "")
152 else:
153 minOSOption = "mmacosx-version-min"
154 platformName = os.getenv("PLATFORM_NAME", "osx")
155 archOptions = ""
156 archList = os.getenv("RC_ARCHS", "")
157 if archList:
158 for arch in string.split(archList, " "):
159 archOptions = archOptions + " -arch " + arch
160 else:
161 archList = os.getenv("ARCHS_STANDARD_32_64_BIT", "")
162 if platformName == "watchos":
163 archOptions = "-arch armv7k"
164 elif platformName == "appletvos":
165 archOptions = "-arch arm64"
166 else:
167 if archList:
168 for arch in string.split(archList, " "):
169 archOptions = archOptions + " -arch " + arch
170 else:
171 archOptions = "-arch x86_64"
172 allTests = []
173 for f in os.listdir(testsSrcTopDir):
174 if f.endswith(".dtest"):
175 testName = f[0:-6]
176 outDirBuild = testsBuildDstTopDir + testName
177 outDirRun = testsRunDstTopDir + testName
178 testCaseDir = testsSrcTopDir + f
179 testCaseDirectives = parseDirectives(testCaseDir)
180 if useTestCase(testCaseDirectives, platformName):
181 result = buildTestCase(testCaseDirectives, testCaseDir, toolsDir, sdkDir, minOSOption, minVersNum, archOptions, outDirBuild, outDirRun)
182 if result:
183 sys.exit(result)
184 mytest = {}
185 mytest["TestName"] = testName
186 mytest["Arch"] = "platform-native"
187 mytest["WorkingDirectory"] = testsRunDstTopDir + testName
188 mytest["Command"] = []
189 mytest["Command"].append("./run.sh")
190 for runline in testCaseDirectives["RUN"]:
191 if "$SUDO" in runline:
192 mytest["AsRoot"] = True
193 if testCaseDirectives["RUN_TIMEOUT"]:
194 mytest["Timeout"] = testCaseDirectives["RUN_TIMEOUT"]
195 allTests.append(mytest)
196 batsInfo = { "BATSConfigVersion": "0.1.0",
197 "Project": "dyld_tests",
198 "Tests": allTests }
199 batsFileDir = dstDir + "/AppleInternal/CoreOS/BATS/unit_tests/"
200 shutil.rmtree(batsFileDir, ignore_errors=True)
201 os.makedirs(batsFileDir)
202 batsFilePath = batsFileDir + "dyld.plist"
203 with open(batsFilePath, "w") as batsFile:
204 batsFile.write(plistlib.writePlistToString(batsInfo))
205 batsFile.close()
206 os.system('plutil -convert binary1 ' + batsFilePath) # convert the plist in place to binary
207 runHelper = dstDir + "/AppleInternal/CoreOS/tests/dyld/run_all_dyld_tests.sh"
208 print runHelper
209 with open(runHelper, "w") as shFile:
210 shFile.write("#!/bin/sh\n")
211 for test in allTests:
212 shFile.write(test["WorkingDirectory"] + "/run.sh\n")
213 shFile.close()
214 os.chmod(runHelper, 0755)
215
216