blob: b35795f5cb4034c989407ff6fab824b25f66e6e0 [file] [log] [blame]
Yingdi Yub3015bd2015-06-23 23:21:53 -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 Pesavento77f1c762019-02-19 03:20:49 -050022from waflib import Utils
Yingdi Yub3015bd2015-06-23 23:21:53 -070023from waflib.Configure import conf
24
Alexander Afanasyev750472b2016-08-05 12:07:13 -070025OPENSSL_DIR_OSX = ['/usr/local', '/opt/local', '/usr/local/opt/openssl']
Yingdi Yub3015bd2015-06-23 23:21:53 -070026OPENSSL_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 Pesavento77f1c762019-02-19 03:20:49 -050030 help='directory where OpenSSL is installed, e.g., /usr/local')
Yingdi Yub3015bd2015-06-23 23:21:53 -070031
32@conf
33def __openssl_get_version_file(self, dir):
34 try:
Davide Pesavento77f1c762019-02-19 03:20:49 -050035 return self.root.find_dir(dir).find_node('include/openssl/opensslv.h')
Yingdi Yub3015bd2015-06-23 23:21:53 -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 Pesavento77f1c762019-02-19 03:20:49 -050047 openssl_dir = []
48 if Utils.unversioned_sys_platform() == 'darwin':
Yingdi Yub3015bd2015-06-23 23:21:53 -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 Pesavento77f1c762019-02-19 03:20:49 -050066 self.start_msg('Checking for OpenSSL version')
Yingdi Yub3015bd2015-06-23 23:21:53 -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 Pesavento77f1c762019-02-19 03:20:49 -050085 atleast_version = kw.get('atleast_version', 0)
Yingdi Yub3015bd2015-06-23 23:21:53 -070086 if int(version, 16) < atleast_version:
Davide Pesavento77f1c762019-02-19 03:20:49 -050087 self.fatal('The version of OpenSSL is too old\n'
88 'Please upgrade your distribution or manually install a newer version of OpenSSL')
Yingdi Yub3015bd2015-06-23 23:21:53 -070089
Davide Pesavento77f1c762019-02-19 03:20:49 -050090 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 Yub3015bd2015-06-23 23:21:53 -070098 if root:
Davide Pesavento77f1c762019-02-19 03:20:49 -050099 kw['includes'] = '%s/include' % root
100 kw['libpath'] = '%s/lib' % root
Yingdi Yub3015bd2015-06-23 23:21:53 -0700101
Davide Pesavento77f1c762019-02-19 03:20:49 -0500102 self.check_cxx(**kw)