blob: 71d8bdf3b8a97ce6030888da14b756efadcba3b5 [file] [log] [blame]
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -07001#! /usr/bin/env python
2# encoding: utf-8
3
4'''
5
6When using this tool, the wscript will look like:
7
8 def options(opt):
9 opt.tool_options('protobuf', tooldir=["waf-tools"])
10
11 def configure(conf):
12 conf.load('compiler_cxx protobuf')
13
14 def build(bld):
15 bld(source='main.cpp', target='app', use='PROTOBUF')
16
17Options are generated, in order to specify the location of protobuf includes/libraries.
18
19
20'''
21import sys
22import re
23from waflib import Utils,Logs,Errors
24from waflib.Configure import conf
25PROTOBUF_DIR=['/usr','/usr/local','/opt/local','/sw']
26PROTOBUF_VERSION_FILE='google/protobuf/stubs/common.h'
27PROTOBUF_VERSION_CODE='''
28#include <iostream>
29#include <google/protobuf/stubs/common.h>
30int main() { std::cout << GOOGLE_PROTOBUF_VERSION ;}
31'''
32
33def options(opt):
34 opt.add_option('--protobuf',type='string',default='',dest='protobuf_dir',help='''path to where protobuf is installed, e.g. /usr/local''')
35@conf
36def __protobuf_get_version_file(self,dir):
37 try:
38 return self.root.find_dir(dir).find_node('%s/%s' % ('include', PROTOBUF_VERSION_FILE))
39 except:
40 return None
41@conf
42def protobuf_get_version(self,dir):
43 val=self.check_cxx(fragment=PROTOBUF_VERSION_CODE,includes=['%s/%s' % (dir, 'include')], execute=True, define_ret = True, mandatory=True)
44 return val
45@conf
46def protobuf_get_root(self,*k,**kw):
47 root=k and k[0]or kw.get('path',None)
48 # Logs.pprint ('RED', ' %s' %root)
49 if root and self.__protobuf_get_version_file(root):
50 return root
51 for dir in PROTOBUF_DIR:
52 if self.__protobuf_get_version_file(dir):
53 return dir
54 if root:
55 self.fatal('Protobuf not found in %s'%root)
56 else:
57 self.fatal('Protobuf not found, please provide a --protobuf argument (see help)')
58@conf
59def check_protobuf(self,*k,**kw):
60 if not self.env['CXX']:
61 self.fatal('load a c++ compiler first, conf.load("compiler_cxx")')
62
63 var=kw.get('uselib_store','PROTOBUF')
64 self.start_msg('Checking Protocol Buffer')
65 root = self.protobuf_get_root(*k,**kw);
66 self.env.PROTOBUF_VERSION=self.protobuf_get_version(root)
67
68 self.env['INCLUDES_%s'%var]= '%s/%s' % (root, "include");
69 self.env['LIB_%s'%var] = "protobuf"
70 self.env['LIBPATH_%s'%var] = '%s/%s' % (root, "lib")
71
72 self.end_msg(self.env.PROTOBUF_VERSION)
73 if Logs.verbose:
74 Logs.pprint('CYAN',' Protocol Buffer include : %s'%self.env['INCLUDES_%s'%var])
75 Logs.pprint('CYAN',' Protocol Buffer lib : %s'%self.env['LIB_%s'%var])
76 Logs.pprint('CYAN',' Protocol Buffer libpath : %s'%self.env['LIBPATH_%s'%var])