1#!/usr/bin/env python 2# 3# Builds a iPhone/iPad static library of PETSc 4# 5# Before using removed /usr/include/mpi.h and /Developer/SDKs/MacOSX10.5.sdk/usr/include/mpi.h or 6# Xcode will use those instead of the MPIuni one we point to 7# 8# export PETSC_ARCH=arch-ios 9 10# ./systems/Apple/iOS/bin/arch-ios.py [use --with-debugging=0 to get iPhone/iPad version, otherwise creates simulator version] 11# this sets up the appropriate configuration file 12# 13# ./systems/Apple/iOS/bin/iosbuilder.py 14# this creates the PETSc iPhone/iPad library 15# this will open Xcode and give you directions to follow 16# 17# open xcode/examples/examples.xcodeproj 18# Project -> Edit Project Setting -> Configuration (make sure it is Release or Debug depending on if you used --with-debugging=0) 19# Build -> Build and Debug 20# 21import os, sys 22 23sys.path.insert(0, os.path.join(os.environ['PETSC_DIR'], 'config')) 24sys.path.insert(0, os.path.join(os.environ['PETSC_DIR'], 'config', 'BuildSystem')) 25 26import script 27 28class PETScMaker(script.Script): 29 def __init__(self): 30 import RDict 31 import os 32 33 argDB = RDict.RDict(None, None, 0, 0, readonly = True) 34 argDB.saveFilename = os.path.join(os.environ['PETSC_DIR'], os.environ['PETSC_ARCH'], 'conf', 'RDict.db') 35 argDB.load() 36 script.Script.__init__(self, argDB = argDB) 37 self.log = sys.stdout 38 return 39 40 def setupModules(self): 41 self.mpi = self.framework.require('config.packages.MPI', None) 42 self.base = self.framework.require('config.base', None) 43 self.setCompilers = self.framework.require('config.setCompilers', None) 44 self.arch = self.framework.require('PETSc.utilities.arch', None) 45 self.petscdir = self.framework.require('PETSc.utilities.petscdir', None) 46 self.languages = self.framework.require('PETSc.utilities.languages', None) 47 self.debugging = self.framework.require('PETSc.utilities.debugging', None) 48 self.make = self.framework.require('config.programs', None) 49 self.compilers = self.framework.require('config.compilers', None) 50 self.types = self.framework.require('config.types', None) 51 self.headers = self.framework.require('config.headers', None) 52 self.functions = self.framework.require('config.functions', None) 53 self.libraries = self.framework.require('config.libraries', None) 54 self.scalarType = self.framework.require('PETSc.utilities.scalarTypes', None) 55 self.memAlign = self.framework.require('PETSc.utilities.memAlign', None) 56 self.libraryOptions= self.framework.require('PETSc.utilities.libraryOptions', None) 57 self.compilerFlags = self.framework.require('config.compilerFlags', self) 58 return 59 60 def setupHelp(self, help): 61 import nargs 62 63 help = script.Script.setupHelp(self, help) 64 help.addArgument('RepManager', '-rootDir', nargs.ArgDir(None, os.environ['PETSC_DIR'], 'The root directory for this build', isTemporary = 1)) 65 help.addArgument('RepManager', '-dryRun', nargs.ArgBool(None, False, 'Only output what would be run', isTemporary = 1)) 66 help.addArgument('RepManager', '-verbose', nargs.ArgInt(None, 0, 'The verbosity level', min = 0, isTemporary = 1)) 67 return help 68 69 def setup(self): 70 script.Script.setup(self) 71 self.framework = self.loadConfigure() 72 self.setupModules() 73 return 74 75 @property 76 def verbose(self): 77 '''The verbosity level''' 78 return self.argDB['verbose'] 79 80 @property 81 def dryRun(self): 82 '''Flag for only output of what would be run''' 83 return self.argDB['dryRun'] 84 85 def getPackageInfo(self): 86 packageIncludes = [] 87 packageLibs = [] 88 for p in self.framework.packages: 89 # Could put on compile line, self.addDefine('HAVE_'+i.PACKAGE, 1) 90 if hasattr(p, 'lib'): 91 if not isinstance(p.lib, list): 92 packageLibs.append(p.lib) 93 else: 94 packageLibs.extend(p.lib) 95 if hasattr(p, 'include'): 96 if not isinstance(p.include, list): 97 packageIncludes.append(p.include) 98 else: 99 packageIncludes.extend(p.include) 100 packageLibs = self.libraries.toStringNoDupes(packageLibs+self.libraries.math) 101 packageIncludes = self.headers.toStringNoDupes(packageIncludes) 102 return packageIncludes, packageLibs 103 104 def buildDir(self, dirname): 105 ''' This is run in a PETSc source directory''' 106 if self.verbose: print 'Entering '+dirname 107 os.chdir(dirname) 108 l = len(os.environ['PETSC_DIR']) 109 basedir = os.path.join(os.environ['PETSC_DIR'],os.environ['PETSC_ARCH'],'xcode-links') 110 #newdirname = os.path.join(basedir,dirname[l+1:]) 111 #os.mkdir(newdirname) 112 113 114 # Get list of source files in the directory 115 cnames = [] 116 onames = [] 117 fnames = [] 118 hnames = [] 119 for f in os.listdir(dirname): 120 ext = os.path.splitext(f)[1] 121 if ext == '.c': 122 cnames.append(f) 123 onames.append(f.replace('.c', '.o')) 124 if ext == '.h': 125 hnames.append(f) 126 if cnames: 127 if self.verbose: print 'Linking C files',cnames 128 for i in cnames: 129 j = i[l+1:] 130 if not os.path.islink(os.path.join(basedir,i)): 131 if i.endswith('openglops.c'): 132 os.symlink(os.path.join(dirname,i),os.path.join(basedir,'openglops.m')) 133 else: 134 os.symlink(os.path.join(dirname,i),os.path.join(basedir,i)) 135 # do not need to link these because xcode project points to original source code directory 136 #if hnames: 137 # if self.verbose: print 'Linking h files',hnames 138 # for i in hnames: 139 # if not os.path.islink(os.path.join(basedir,i)): 140 # os.symlink(os.path.join(dirname,i),os.path.join(basedir,i)) 141 return 142 143 def checkDir(self, dirname): 144 '''Checks whether we should recurse into this directory 145 - Excludes projects directory 146 - Excludes examples directory 147 - Excludes contrib directory 148 - Excludes tutorials directory 149 - Excludes benchmarks directory 150 - Checks makefile to see if compiler is allowed to visit this directory for this configuration''' 151# print self.functions.functions 152# print self.base.defines 153 base = os.path.basename(dirname) 154 155 if base == 'examples': return False 156 if base == 'projects': return False 157 if base.startswith('ftn-') or base.startswith('f90-'): return False 158 if base == 'contrib': return False 159 if base == 'tutorials': return False 160 if base == 'benchmarks': return False 161 if base == 'systems': return False 162 if base.startswith('arch-'): return False 163 164 import re 165 reg = re.compile(' [ ]*') 166 fname = os.path.join(dirname, 'makefile') 167 if not os.path.isfile(fname): 168 if os.path.isfile(os.path.join(dirname, 'Makefile')): print 'ERROR: Change Makefile to makefile in',dirname 169 return False 170 fd = open(fname) 171 text = fd.readline() 172 while text: 173 if text.startswith('#requires'): 174 text = text[9:-1].strip() 175 text = reg.sub(' ',text) 176 rtype = text.split(' ')[0] 177 rvalue = text.split(' ')[1] 178 179 if rvalue == "'"+'PETSC_HAVE_FORTRAN'+"'" or rvalue == "'"+'PETSC_USING_F90'+"'" or rvalue == "'"+'PETSC_USING_F2003'+"'": 180 if not hasattr(self.compilers, 'FC'): 181 if self.verbose: print 'Rejecting',dirname,'because fortran is not being used' 182 return 0 183 elif rvalue == "'"+'PETSC_USE_LOG'+"'": 184 if not self.libraryOptions.useLog: 185 if self.verbose: print 'Rejecting',dirname,'because logging is turned off' 186 return 0 187 elif rvalue == "'"+'PETSC_USE_FORTRAN_KERNELS'+"'": 188 if not self.libraryOptions.useFortranKernels: 189 if self.verbose: print 'Rejecting',dirname,'because fortran kernels are turned off' 190 return 0 191 elif rtype == 'scalar' and not self.scalarType.scalartype == rvalue: 192 if self.verbose: print 'Rejecting',dirname,'because scalar type '+self.scalarType.scalartype+' is not '+rvalue 193 return 0 194 elif rtype == 'language': 195 if rvalue == 'CXXONLY' and self.languages.clanguage == 'C': 196 if self.verbose: print 'Rejecting',dirname,'because language is '+self.languages.clanguage+' is not C++' 197 return 0 198 elif rtype == 'precision' and not rvalue == self.scalarType.precision: 199 if self.verbose: print 'Rejecting',dirname,'because precision '+self.scalarType.precision+' is not '+rvalue 200 return 0 201 elif rtype == 'package': 202 found = 0 203 if self.mpi.usingMPIUni: 204 pname = 'PETSC_HAVE_MPIUNI' 205 pname = "'"+pname+"'" 206 if pname == rvalue: found = 1 207 for i in self.framework.packages: 208 pname = 'PETSC_HAVE_'+i.PACKAGE 209 pname = "'"+pname+"'" 210 if pname == rvalue: found = 1 211 if not found: 212 if self.verbose: print 'Rejecting',dirname,'because package '+rvalue+' does not exist' 213 return 0 214 elif rtype == 'define': 215 found = 0 216 for i in self.base.defines: 217 pname = 'PETSC_'+i.upper() 218 pname = "'"+pname+"'" 219 if pname == rvalue: found = 1 220 if not found: 221 if self.verbose: print 'Rejecting',dirname,'because define '+rvalue+' does not exist' 222 return 0 223 elif rtype == 'function': 224 found = 0 225 for i in self.functions.functions: 226 pname = 'PETSC_HAVE_'+i.upper() 227 pname = "'"+pname+"'" 228# print pname 229# print rvalue 230 if pname == rvalue: found = 1 231 if not found: 232 if self.verbose: print 'Rejecting',dirname,'because function '+rvalue+' does not exist' 233 return 0 234 235 text = fd.readline() 236 fd.close() 237 return True 238 239 def buildAll(self, rootDir = None): 240 import shutil 241 self.setup() 242 if rootDir is None: 243 rootDir = self.argDB['rootDir'] 244 if not self.checkDir(rootDir): 245 print 'Nothing to be done' 246 if rootDir == os.environ['PETSC_DIR']: 247 basedir = os.path.join(self.petscdir.dir, self.arch.arch, 'xcode-links') 248 if os.path.isdir(basedir): 249 if self.verbose: print 'Removing '+basedir 250 shutil.rmtree(basedir) 251 os.mkdir(basedir) 252 for root, dirs, files in os.walk(rootDir): 253 self.buildDir(root) 254 for badDir in [d for d in dirs if not self.checkDir(os.path.join(root, d))]: 255 dirs.remove(badDir) 256 257 print 'In Xcode mouse click on xcode-links and the delete key, then' 258 print 'control mouse click on "Other Sources" and select "Add files to PETSc ...", then' 259 print 'in the finder window locate ${PETSC_DIR}/arch-ios/xcode-links and select it. Now' 260 print 'exit Xcode' 261 262 try: 263 import subprocess 264 subprocess.call('cd '+os.path.join(os.environ['PETSC_DIR'],'systems','Apple','iOS','PETSc')+';open -W PETSc.xcodeproj', shell=True) 265 except RuntimeError, e: 266 raise RuntimeError('Error opening xcode project '+str(e)) 267 268 269 sdk = ' -sdk iphonesimulator5.1 ' 270 destination = 'iphonesimulator' 271 debug = 'Debug' 272 debugdir = 'Debug-'+destination 273 if not self.compilerFlags.debugging: 274 debug = 'Release' 275 debugdir = 'Release-'+destination 276 try: 277 output,err,ret = self.executeShellCommand('cd '+os.path.join(os.environ['PETSC_DIR'],'systems','Apple','iOS','PETSc')+';xcodebuild -configuration '+debug+sdk, timeout=3000, log = self.log) 278 except RuntimeError, e: 279 raise RuntimeError('Error making iPhone/iPad version of PETSc libraries: '+str(e)) 280 281 liblocation = os.path.join(os.environ['PETSC_DIR'],'systems','Apple','iOS','PETSc','build',debugdir,'libPETSc.a') 282 if not os.path.exists(liblocation): 283 raise RuntimeError('Error library '+liblocation+' not created') 284 try: 285 output,err,ret = self.executeShellCommand('mv -f '+liblocation+' '+os.path.join(os.environ['PETSC_DIR'],os.environ['PETSC_ARCH'],'lib'), timeout=30, log = self.log) 286 except RuntimeError, e: 287 raise RuntimeError('Error copying iPhone/iPad version of PETSc libraries: '+str(e)) 288 289 return 290 291def noCheckCommand(command, status, output, error): 292 ''' Do no check result''' 293 return 294 noCheckCommand = staticmethod(noCheckCommand) 295 296if __name__ == '__main__': 297 PETScMaker().buildAll() 298