Alexander Afanasyev | 6133f9a | 2013-07-14 10:58:26 -0700 | [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 | |
| 8 | def options(opt): |
| 9 | opt.tool_options('ndnx') |
| 10 | |
| 11 | def configure(conf): |
| 12 | conf.load('compiler_c ndnx') |
| 13 | |
| 14 | def build(bld): |
| 15 | bld(source='main.cpp', target='app', use='NDNX') |
| 16 | |
| 17 | Options are generated, in order to specify the location of ndnx includes/libraries. |
| 18 | |
| 19 | |
| 20 | ''' |
| 21 | import sys, re |
| 22 | from waflib import Utils, Logs, Errors, Options, ConfigSet |
| 23 | from waflib.Configure import conf |
| 24 | |
| 25 | NDNX_DIR=['/usr','/usr/local','/opt/local','/sw'] |
| 26 | NDNX_VERSION_FILE='ccn/ccn.h' |
| 27 | NDNX_VERSION_CODE=''' |
| 28 | #include <ccn/ccn.h> |
| 29 | #include <stdio.h> |
| 30 | int main() { printf ("%d.%d.%d", ((CCN_API_VERSION/100000) % 100), ((CCN_API_VERSION/1000) % 100), (CCN_API_VERSION % 1000)); return 0; } |
| 31 | ''' |
| 32 | |
| 33 | @conf |
| 34 | def __ndnx_get_version_file(self,dir): |
| 35 | # Logs.pprint ('CYAN', ' + %s/%s/%s' % (dir, 'include', NDNX_VERSION_FILE)) |
| 36 | try: |
| 37 | return self.root.find_dir(dir).find_node('%s/%s' % ('include', NDNX_VERSION_FILE)) |
| 38 | except: |
| 39 | return None |
| 40 | @conf |
| 41 | def ndnx_get_version(self,dir): |
| 42 | val=self.check_cc(fragment=NDNX_VERSION_CODE,includes=['%s/%s' % (dir, 'include')],execute=True,define_ret = True, mandatory=True) |
| 43 | return val |
| 44 | @conf |
| 45 | def ndnx_get_root(self,*k,**kw): |
| 46 | root=Options.options.ndnx_dir or (k and k[0]) or kw.get('path',None) |
| 47 | |
| 48 | if root: |
| 49 | if self.__ndnx_get_version_file(root): |
| 50 | return root |
| 51 | self.fatal('NDNx not found in %s'%root) |
| 52 | |
| 53 | for dir in NDNX_DIR: |
| 54 | if self.__ndnx_get_version_file(dir): |
| 55 | return dir |
| 56 | self.fatal('NDNx not found, please provide a --ndnx argument (see help)') |
| 57 | |
| 58 | @conf |
| 59 | def check_openssl(self,*k,**kw): |
| 60 | root = k and k[0] or kw.get('path',None) or Options.options.openssl |
| 61 | mandatory = kw.get('mandatory', True) |
| 62 | var = kw.get('var', 'SSL') |
| 63 | |
| 64 | CODE = """ |
| 65 | #include <openssl/crypto.h> |
| 66 | #include <stdio.h> |
| 67 | |
| 68 | int main(int argc, char **argv) { |
| 69 | (void)argc; |
| 70 | printf ("%s", argv[0]); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | """ |
| 75 | if root: |
| 76 | testApp = self.check_cc (lib=['ssl', 'crypto'], |
| 77 | header_name='openssl/crypto.h', |
| 78 | define_name='HAVE_%s' % var, |
| 79 | uselib_store=var, |
| 80 | mandatory = mandatory, |
| 81 | cflags="-I%s/include" % root, |
| 82 | linkflags="-L%s/lib" % root, |
| 83 | execute = True, fragment = CODE, define_ret = True) |
| 84 | else: |
| 85 | testApp = libcrypto = self.check_cc (lib=['ssl', 'crypto'], |
| 86 | header_name='openssl/crypto.h', |
| 87 | define_name='HAVE_%s' % var, |
| 88 | uselib_store=var, |
| 89 | mandatory = mandatory, |
| 90 | execute = True, fragment = CODE, define_ret = True) |
| 91 | |
| 92 | if not testApp: |
| 93 | return |
| 94 | |
| 95 | self.start_msg ('Checking if selected openssl matches NDNx') |
| 96 | |
| 97 | ndn_var = kw.get('ndn_var', "NDNX") |
| 98 | if Utils.unversioned_sys_platform () == "darwin": |
| 99 | def otool (binary): |
| 100 | p = Utils.subprocess.Popen (['/usr/bin/otool', '-L', binary], |
| 101 | stdout = Utils.subprocess.PIPE, ) |
| 102 | for line in p.communicate()[0].split ('\n'): |
| 103 | if re.match ('.*/libcrypto\..*', line): |
| 104 | return line |
| 105 | |
| 106 | selected_crypto = otool (testApp) |
| 107 | ccnd_crypto = otool ('%s/bin/ccnd' % self.env['%s_ROOT' % ndn_var]) |
| 108 | |
| 109 | if ccnd_crypto != selected_crypto: |
| 110 | self.fatal ("Selected openssl does not match used to compile NDNx (%s != %s)" % |
| 111 | (selected_crypto.strip (), ccnd_crypto.strip ())) |
| 112 | self.end_msg (True) |
| 113 | |
| 114 | elif Utils.unversioned_sys_platform () == "linux" or Utils.unversioned_sys_platform () == "freebsd": |
| 115 | def ldd (binary): |
| 116 | p = Utils.subprocess.Popen (['/usr/bin/ldd', binary], |
| 117 | stdout = Utils.subprocess.PIPE, ) |
| 118 | for line in p.communicate()[0].split ('\n'): |
| 119 | if re.match ('libcrypto\..*', line): |
| 120 | return line |
| 121 | |
| 122 | selected_crypto = ldd (testApp) |
| 123 | ccnd_crypto = ldd ('%s/bin/ccnd' % self.env['%s_ROOT' % ndn_var]) |
| 124 | |
| 125 | if ccnd_crypto != selected_crypto: |
| 126 | self.fatal ("Selected openssl does not match used to compile NDNx (%s != %s)" % |
| 127 | (selected_crypto.strip (), ccnd_crypto.strip ())) |
| 128 | self.end_msg (True) |
| 129 | else: |
| 130 | self.end_msg ("Don't know how to check", 'YELLOW') |
| 131 | |
| 132 | @conf |
| 133 | def check_ndnx(self,*k,**kw): |
| 134 | if not self.env['CC']: |
| 135 | self.fatal('load a c compiler first, conf.load("compiler_c")') |
| 136 | |
| 137 | var=kw.get('uselib_store', 'NDNX') |
| 138 | self.start_msg('Checking for NDNx') |
| 139 | root = self.ndnx_get_root(*k,**kw); |
| 140 | self.env.NDNX_VERSION=self.ndnx_get_version(root) |
| 141 | |
| 142 | self.env['INCLUDES_%s' % var]= '%s/%s' % (root, "include"); |
| 143 | self.env['LIB_%s' % var] = "ccn" |
| 144 | self.env['LIBPATH_%s' % var] = '%s/%s' % (root, "lib") |
| 145 | |
| 146 | self.env['%s_ROOT' % var] = root |
| 147 | |
| 148 | self.end_msg("%s in %s " % (self.env.NDNX_VERSION, root)) |
| 149 | if Logs.verbose: |
| 150 | Logs.pprint('CYAN',' NDNx include : %s'%self.env['INCLUDES_%s' % var]) |
| 151 | Logs.pprint('CYAN',' NDNx lib : %s'%self.env['LIB_%s' % var]) |
| 152 | Logs.pprint('CYAN',' NDNx libpath : %s'%self.env['LIBPATH_%s' % var]) |
| 153 | |
| 154 | def options(opt): |
| 155 | """ |
| 156 | NDNx options |
| 157 | """ |
| 158 | ndnopt = opt.add_option_group("NDNx Options") |
| 159 | ndnopt.add_option('--ndnx',type='string',default=None,dest='ndnx_dir',help='''path to where NDNx is installed, e.g. /usr/local''') |
| 160 | ndnopt.add_option('--openssl',type='string',default='',dest='openssl',help='''path to openssl, should be the same NDNx is compiled against''') |