Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame^] | 1 | #! /usr/bin/env python |
| 2 | # encoding: utf-8 |
| 3 | # Yingdi Yu (UCLA) 2016 |
| 4 | |
| 5 | ''' |
| 6 | |
| 7 | When using this tool, the wscript will look like: |
| 8 | |
| 9 | def options(opt): |
| 10 | opt.load('openssl') |
| 11 | |
| 12 | def configure(conf): |
| 13 | conf.load('compiler_cxx openssl') |
| 14 | conf.check_openssl() |
| 15 | |
| 16 | def build(bld): |
| 17 | bld(source='main.cpp', target='app', use='OPENSSL') |
| 18 | |
| 19 | ''' |
| 20 | |
| 21 | import re |
| 22 | from waflib import Utils, Logs |
| 23 | from waflib.Configure import conf |
| 24 | |
| 25 | OPENSSL_VERSION_FILE = 'opensslv.h' |
| 26 | OPENSSL_DIR_OSX = ['/usr/local', '/opt/local'] |
| 27 | OPENSSL_DIR = ['/usr', '/usr/local', '/opt/local', '/sw'] |
| 28 | |
| 29 | def options(opt): |
| 30 | opt.add_option('--with-openssl', type='string', default=None, dest='openssl_dir', |
| 31 | help='''Path to where OpenSSL is installed, e.g., /usr/local''') |
| 32 | |
| 33 | |
| 34 | @conf |
| 35 | def __openssl_get_version_file(self, dir): |
| 36 | try: |
| 37 | return self.root.find_dir(dir).find_node('%s/%s' % ('include/openssl', |
| 38 | OPENSSL_VERSION_FILE)) |
| 39 | except: |
| 40 | return None |
| 41 | |
| 42 | @conf |
| 43 | def __openssl_find_root_and_version_file(self, *k, **kw): |
| 44 | root = k and k[0] or kw.get('path', self.options.openssl_dir) |
| 45 | |
| 46 | file = self.__openssl_get_version_file(root) |
| 47 | if root and file: |
| 48 | return (root, file) |
| 49 | |
| 50 | openssl_dir = []; |
| 51 | if Utils.unversioned_sys_platform() == "darwin": |
| 52 | openssl_dir = OPENSSL_DIR_OSX |
| 53 | else: |
| 54 | openssl_dir = OPENSSL_DIR |
| 55 | |
| 56 | if not root: |
| 57 | for dir in openssl_dir: |
| 58 | file = self.__openssl_get_version_file(dir) |
| 59 | if file: |
| 60 | return (dir, file) |
| 61 | |
| 62 | if root: |
| 63 | self.fatal('OpenSSL not found in %s' % root) |
| 64 | else: |
| 65 | self.fatal('OpenSSL not found, please provide a --with-openssl=PATH argument (see help)') |
| 66 | |
| 67 | @conf |
| 68 | def check_openssl(self, *k, **kw): |
| 69 | atleast_version = kw.get('atleast_version', 0) |
| 70 | var = kw.get('uselib_store', 'OPENSSL') |
| 71 | |
| 72 | self.start_msg('Checking for OpenSSL lib') |
| 73 | (root, file) = self.__openssl_find_root_and_version_file(*k, **kw) |
| 74 | |
| 75 | try: |
| 76 | txt = file.read() |
| 77 | re_version = re.compile('^#\\s*define\\s+OPENSSL_VERSION_NUMBER\\s+(.*)L', re.M) |
| 78 | version_number = re_version.search(txt) |
| 79 | |
| 80 | re_version_text = re.compile('^#\\s*define\\s+OPENSSL_VERSION_TEXT\\s+(.*)', re.M) |
| 81 | version_text = re_version_text.search(txt) |
| 82 | |
| 83 | if version_number and version_text: |
| 84 | version = version_number.group(1) |
| 85 | self.end_msg(version_text.group(1)) |
| 86 | else: |
| 87 | self.fatal('OpenSSL version file is present, but is not recognizable') |
| 88 | except: |
| 89 | self.fatal('OpenSSL version file is not found or is not usable') |
| 90 | |
| 91 | if int(version, 16) < atleast_version: |
| 92 | self.fatal("The version of OpenSSL is too old\n" + |
| 93 | "Please upgrade your distribution or install newer version of OpenSSL library") |
| 94 | |
| 95 | kwargs = {} |
| 96 | if root: |
| 97 | kwargs['includes'] = "%s/include" % root |
| 98 | kwargs['libpath'] = "%s/lib" % root |
| 99 | |
| 100 | libcrypto = self.check_cxx(lib=['ssl', 'crypto'], |
| 101 | msg='Checking if OpenSSL library works', |
| 102 | define_name='HAVE_%s' % var, |
| 103 | uselib_store=var, |
| 104 | mandatory=True, |
| 105 | **kwargs) |