Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # encoding: utf-8 |
| 3 | |
| 4 | ''' |
| 5 | |
| 6 | When using this tool, the wscript will look like: |
| 7 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 8 | def options(opt): |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 9 | opt.load('compiler_cxx cryptopp') |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 11 | def configure(conf): |
| 12 | conf.load('compiler_cxx cryptopp') |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 13 | conf.check_cryptopp() |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 14 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 15 | def build(bld): |
| 16 | bld(source='main.cpp', target='app', use='CRYPTOPP') |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 17 | |
| 18 | Options are generated, in order to specify the location of cryptopp includes/libraries. |
| 19 | |
| 20 | |
| 21 | ''' |
| 22 | import sys |
| 23 | import re |
| 24 | from waflib import Utils,Logs,Errors |
| 25 | from waflib.Configure import conf |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 26 | CRYPTOPP_DIR = ['/usr', '/usr/local', '/opt/local', '/sw', '/usr/local/ndn', '/opt/ndn'] |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 27 | CRYPTOPP_VERSION_FILE = 'config.h' |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 00f855e | 2015-05-28 14:38:43 -0700 | [diff] [blame] | 29 | CRYPTOPP_CHECK_FRAGMENT = ''' |
| 30 | #include "../../src/security/cryptopp.hpp" |
| 31 | #include <iostream> |
| 32 | |
| 33 | int |
| 34 | main() |
| 35 | { |
| 36 | using namespace CryptoPP; |
| 37 | |
| 38 | std::string buffer = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; |
| 39 | SHA256 hash; |
| 40 | StringSource(buffer, true, new HashFilter(hash, new FileSink(std::cout))); |
| 41 | StringSource(reinterpret_cast<const uint8_t*>(buffer.c_str()), buffer.size(), |
| 42 | true, new HashFilter(hash, new FileSink(std::cout))); |
| 43 | return 0; |
| 44 | } |
| 45 | ''' |
| 46 | |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 47 | def options(opt): |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 48 | opt.add_option('--with-cryptopp', type='string', default=None, dest='cryptopp_dir', |
| 49 | help='''Path to where CryptoPP is installed, e.g., /usr/local''') |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 51 | @conf |
| 52 | def __cryptopp_get_version_file(self, dir): |
| 53 | try: |
| 54 | return self.root.find_dir(dir).find_node('%s/%s' % ('include/cryptopp', |
| 55 | CRYPTOPP_VERSION_FILE)) |
| 56 | except: |
| 57 | return None |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 59 | @conf |
| 60 | def __cryptopp_find_root_and_version_file(self, *k, **kw): |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 61 | root = k and k[0] or kw.get('path', self.options.cryptopp_dir) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 63 | file = self.__cryptopp_get_version_file(root) |
| 64 | if root and file: |
| 65 | return (root, file) |
| 66 | for dir in CRYPTOPP_DIR: |
| 67 | file = self.__cryptopp_get_version_file(dir) |
| 68 | if file: |
| 69 | return (dir, file) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 71 | if root: |
| 72 | self.fatal('CryptoPP not found in %s' % root) |
| 73 | else: |
Mathias Gibbens | 16f24c5 | 2014-05-06 17:12:27 -0700 | [diff] [blame] | 74 | self.fatal('CryptoPP not found, please provide a --with-cryptopp=PATH argument (see help)') |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 75 | |
| 76 | @conf |
| 77 | def check_cryptopp(self, *k, **kw): |
| 78 | if not self.env['CXX']: |
| 79 | self.fatal('Load a c++ compiler first, e.g., conf.load("compiler_cxx")') |
| 80 | |
Alexander Afanasyev | fff47d6 | 2014-05-11 19:24:46 -0700 | [diff] [blame] | 81 | var = kw.get('uselib_store', 'CRYPTOPP') |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 82 | mandatory = kw.get('mandatory', True) |
| 83 | |
Alexander Afanasyev | fff47d6 | 2014-05-11 19:24:46 -0700 | [diff] [blame] | 84 | use = kw.get('use', 'PTHREAD') |
| 85 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 86 | self.start_msg('Checking Crypto++ lib') |
| 87 | (root, file) = self.__cryptopp_find_root_and_version_file(*k, **kw) |
| 88 | |
| 89 | try: |
| 90 | txt = file.read() |
| 91 | re_version = re.compile('^#define\\s+CRYPTOPP_VERSION\\s+(.*)', re.M) |
| 92 | match = re_version.search(txt) |
| 93 | |
| 94 | if match: |
| 95 | self.env.CRYPTOPP_VERSION = match.group(1) |
| 96 | self.end_msg(self.env.CRYPTOPP_VERSION) |
| 97 | else: |
| 98 | self.fatal('CryptoPP files are present, but are not recognizable') |
| 99 | except: |
| 100 | self.fatal('CryptoPP not found or is not usable') |
| 101 | |
Alexander Afanasyev | 00f855e | 2015-05-28 14:38:43 -0700 | [diff] [blame] | 102 | isLibWorking = False |
| 103 | for defines in ['', 'CRYPTOPP_DISABLE_ASM']: |
| 104 | try: |
| 105 | self.check_cxx(msg='Checking if CryptoPP library works', |
| 106 | fragment=CRYPTOPP_CHECK_FRAGMENT, |
| 107 | lib='cryptopp', |
| 108 | includes="%s/include" % root, |
| 109 | libpath="%s/lib" % root, |
| 110 | mandatory=True, |
| 111 | use=use, |
| 112 | defines=defines, |
| 113 | uselib_store=var) |
| 114 | isLibWorking = True |
| 115 | break |
| 116 | except: |
| 117 | # try another flags |
| 118 | pass |
| 119 | |
| 120 | if mandatory and not isLibWorking: |
| 121 | self.fatal('CryptoPP is present, but is not usable') |