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 |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 22 | from waflib import Utils |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 23 | from waflib.Configure import conf |
| 24 | |
Alexander Afanasyev | 750472b | 2016-08-05 12:07:13 -0700 | [diff] [blame] | 25 | OPENSSL_DIR_OSX = ['/usr/local', '/opt/local', '/usr/local/opt/openssl'] |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 26 | OPENSSL_DIR = ['/usr', '/usr/local', '/opt/local', '/sw'] |
| 27 | |
| 28 | def options(opt): |
| 29 | opt.add_option('--with-openssl', type='string', default=None, dest='openssl_dir', |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 30 | help='directory where OpenSSL is installed, e.g., /usr/local') |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 31 | |
| 32 | @conf |
| 33 | def __openssl_get_version_file(self, dir): |
| 34 | try: |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 35 | return self.root.find_dir(dir).find_node('include/openssl/opensslv.h') |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 36 | except: |
| 37 | return None |
| 38 | |
| 39 | @conf |
| 40 | def __openssl_find_root_and_version_file(self, *k, **kw): |
| 41 | root = k and k[0] or kw.get('path', self.options.openssl_dir) |
| 42 | |
| 43 | file = self.__openssl_get_version_file(root) |
| 44 | if root and file: |
| 45 | return (root, file) |
| 46 | |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 47 | openssl_dir = [] |
| 48 | if Utils.unversioned_sys_platform() == 'darwin': |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 49 | openssl_dir = OPENSSL_DIR_OSX |
| 50 | else: |
| 51 | openssl_dir = OPENSSL_DIR |
| 52 | |
| 53 | if not root: |
| 54 | for dir in openssl_dir: |
| 55 | file = self.__openssl_get_version_file(dir) |
| 56 | if file: |
| 57 | return (dir, file) |
| 58 | |
| 59 | if root: |
| 60 | self.fatal('OpenSSL not found in %s' % root) |
| 61 | else: |
| 62 | self.fatal('OpenSSL not found, please provide a --with-openssl=PATH argument (see help)') |
| 63 | |
| 64 | @conf |
| 65 | def check_openssl(self, *k, **kw): |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 66 | self.start_msg('Checking for OpenSSL version') |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 67 | (root, file) = self.__openssl_find_root_and_version_file(*k, **kw) |
| 68 | |
| 69 | try: |
| 70 | txt = file.read() |
| 71 | re_version = re.compile('^#\\s*define\\s+OPENSSL_VERSION_NUMBER\\s+(.*)L', re.M) |
| 72 | version_number = re_version.search(txt) |
| 73 | |
| 74 | re_version_text = re.compile('^#\\s*define\\s+OPENSSL_VERSION_TEXT\\s+(.*)', re.M) |
| 75 | version_text = re_version_text.search(txt) |
| 76 | |
| 77 | if version_number and version_text: |
| 78 | version = version_number.group(1) |
| 79 | self.end_msg(version_text.group(1)) |
| 80 | else: |
| 81 | self.fatal('OpenSSL version file is present, but is not recognizable') |
| 82 | except: |
| 83 | self.fatal('OpenSSL version file is not found or is not usable') |
| 84 | |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 85 | atleast_version = kw.get('atleast_version', 0) |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 86 | if int(version, 16) < atleast_version: |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 87 | self.fatal('The version of OpenSSL is too old\n' |
| 88 | 'Please upgrade your distribution or manually install a newer version of OpenSSL') |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 89 | |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 90 | if 'msg' not in kw: |
| 91 | kw['msg'] = 'Checking if OpenSSL library works' |
| 92 | if 'lib' not in kw: |
| 93 | kw['lib'] = ['ssl', 'crypto'] |
| 94 | if 'uselib_store' not in kw: |
| 95 | kw['uselib_store'] = 'OPENSSL' |
| 96 | if 'define_name' not in kw: |
| 97 | kw['define_name'] = 'HAVE_%s' % kw['uselib_store'] |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 98 | if root: |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 99 | kw['includes'] = '%s/include' % root |
| 100 | kw['libpath'] = '%s/lib' % root |
Yingdi Yu | b3015bd | 2015-06-23 23:21:53 -0700 | [diff] [blame] | 101 | |
Davide Pesavento | 77f1c76 | 2019-02-19 03:20:49 -0500 | [diff] [blame] | 102 | self.check_cxx(**kw) |