blob: b35795f5cb4034c989407ff6fab824b25f66e6e0 [file] [log] [blame]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07001#! /usr/bin/env python
2# encoding: utf-8
3# Yingdi Yu (UCLA) 2016
4
5'''
6
7When 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
21import re
Davide Pesavento55d98602019-10-15 22:40:05 -040022from waflib import Utils
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070023from waflib.Configure import conf
24
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070025OPENSSL_DIR_OSX = ['/usr/local', '/opt/local', '/usr/local/opt/openssl']
26OPENSSL_DIR = ['/usr', '/usr/local', '/opt/local', '/sw']
27
28def options(opt):
29 opt.add_option('--with-openssl', type='string', default=None, dest='openssl_dir',
Davide Pesavento55d98602019-10-15 22:40:05 -040030 help='directory where OpenSSL is installed, e.g., /usr/local')
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070031
32@conf
33def __openssl_get_version_file(self, dir):
34 try:
Davide Pesavento55d98602019-10-15 22:40:05 -040035 return self.root.find_dir(dir).find_node('include/openssl/opensslv.h')
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070036 except:
37 return None
38
39@conf
40def __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 Pesavento55d98602019-10-15 22:40:05 -040047 openssl_dir = []
48 if Utils.unversioned_sys_platform() == 'darwin':
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049 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
65def check_openssl(self, *k, **kw):
Davide Pesavento55d98602019-10-15 22:40:05 -040066 self.start_msg('Checking for OpenSSL version')
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067 (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 Pesavento55d98602019-10-15 22:40:05 -040085 atleast_version = kw.get('atleast_version', 0)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086 if int(version, 16) < atleast_version:
Davide Pesavento55d98602019-10-15 22:40:05 -040087 self.fatal('The version of OpenSSL is too old\n'
88 'Please upgrade your distribution or manually install a newer version of OpenSSL')
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070089
Davide Pesavento55d98602019-10-15 22:40:05 -040090 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']
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070098 if root:
Davide Pesavento55d98602019-10-15 22:40:05 -040099 kw['includes'] = '%s/include' % root
100 kw['libpath'] = '%s/lib' % root
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700101
Davide Pesavento55d98602019-10-15 22:40:05 -0400102 self.check_cxx(**kw)