ws_main.py revision a1168864
1#!/usr/bin/env python
2# encoding: utf-8
3
4# Hanoh Haim
5# Cisco Systems, Inc.
6
7
8VERSION='0.0.1'
9APPNAME='cxx_test'
10
11import os;
12import shutil;
13import copy;
14from distutils.version import StrictVersion
15
16top = '../'
17out = 'build'
18b_path ="./build/linux/"
19
20REQUIRED_CC_VERSION = "4.7.0"
21
22class SrcGroup:
23    ' group of source by directory '
24
25    def __init__(self,dir,src_list):
26      self.dir = dir;
27      self.src_list = src_list;
28      self.group_list = None;
29      assert (type(src_list)==list)
30      assert (type(dir)==str)
31
32    def file_list (self,top):
33        ' return  the long list of the files '
34        res=''
35        for file in self.src_list:
36            res= res + top+'/'+self.dir+'/'+file+'  ';
37        return res;
38
39    def __str__ (self):
40        return (self.file_list(''));
41
42    def __repr__ (self):
43        return (self.file_list(''));
44
45
46
47class SrcGroups:
48    ' group of source groups '
49
50    def __init__(self,list_group):
51      self.list_group = list_group;
52      assert (type(list_group)==list)
53
54
55    def file_list (self,top):
56          ' return  the long list of the files '
57          res=''
58          for o in self.list_group:
59              res += o.file_list(top);
60          return res;
61
62
63    def __str__ (self):
64          return (self.file_list(''));
65
66    def __repr__ (self):
67          return (self.file_list(''));
68
69
70def options(opt):
71    opt.load('compiler_cxx')
72
73
74def verify_cc_version (env):
75    ver = '.'.join(env['CC_VERSION'])
76
77    if StrictVersion(ver) < REQUIRED_CC_VERSION:
78        print("\nMachine GCC version too low '{0}' - required at least '{1}'".format(ver, REQUIRED_CC_VERSION))
79        print( "\n*** please set a compiler using CXX / AR enviorment variables ***\n")
80        exit(-1)
81
82
83def configure(conf):
84    # start from clean
85    if 'RPATH' in os.environ:
86        conf.env.RPATH = os.environ['RPATH'].split(':')
87    else:
88        conf.env.RPATH = []
89
90    conf.load('g++')
91    verify_cc_version(conf.env)
92
93bp_sim_main = SrcGroup(dir='src',
94        src_list=['main.cpp'])
95
96bp_sim_gtest = SrcGroup(dir='src',
97        src_list=[
98             'bp_gtest.cpp',
99             'gtest/tuple_gen_test.cpp',
100             'gtest/nat_test.cpp',
101             'gtest/trex_stateless_gtest.cpp'
102             ])
103
104main_src = SrcGroup(dir='src',
105        src_list=[
106             'bp_sim.cpp',
107             'os_time.cpp',
108             'rx_check.cpp',
109             'tuple_gen.cpp',
110             'platform_cfg.cpp',
111             'utl_yaml.cpp',
112             'rx_check_header.cpp',
113             'nat_check.cpp',
114             'nat_check_flow_table.cpp',
115             'test_pkt_gen.cpp',
116             'timer_wheel_pq.cpp',
117             'time_histogram.cpp',
118             'utl_json.cpp',
119             'utl_cpuu.cpp',
120             'msg_manager.cpp',
121             'publisher/trex_publisher.cpp',
122             'latency.cpp',
123             'flow_stat.cpp',
124             'flow_stat_parser.cpp',
125             'trex_watchdog.cpp',
126             'trex_client_config.cpp',
127             'pal/linux/pal_utl.cpp',
128             'pal/linux/mbuf.cpp',
129             'pal/common/common_mbuf.cpp',
130             'sim/trex_sim_stateless.cpp',
131             'sim/trex_sim_stateful.cpp'
132             ]);
133
134cmn_src = SrcGroup(dir='src/common',
135    src_list=[
136        'gtest-all.cc',
137        'gtest_main.cc',
138        'basic_utils.cpp',
139        'captureFile.cpp',
140        'erf.cpp',
141        'pcap.cpp',
142        'base64.cpp'
143        ]);
144
145
146net_src = SrcGroup(dir='src/common/Network/Packet',
147        src_list=[
148           'CPktCmn.cpp',
149           'EthernetHeader.cpp',
150           'IPHeader.cpp',
151           'TCPHeader.cpp',
152           'TCPOptions.cpp',
153           'UDPHeader.cpp',
154           'MacAddress.cpp',
155           'VLANHeader.cpp']);
156
157# stateless code
158stateless_src = SrcGroup(dir='src/stateless/',
159                          src_list=['cp/trex_stream.cpp',
160                                    'cp/trex_stream_vm.cpp',
161                                    'cp/trex_stateless.cpp',
162                                    'cp/trex_stateless_port.cpp',
163                                    'cp/trex_streams_compiler.cpp',
164                                    'cp/trex_vm_splitter.cpp',
165                                    'cp/trex_dp_port_events.cpp',
166                                    'dp/trex_stateless_dp_core.cpp',
167                                    'messaging/trex_stateless_messaging.cpp',
168                                    ])
169# RPC code
170rpc_server_src = SrcGroup(dir='src/rpc-server/',
171                          src_list=[
172                              'trex_rpc_server.cpp',
173                              'trex_rpc_req_resp_server.cpp',
174                              'trex_rpc_async_server.cpp',
175                              'trex_rpc_jsonrpc_v2_parser.cpp',
176                              'trex_rpc_cmds_table.cpp',
177                              'trex_rpc_cmd.cpp',
178                              'trex_rpc_zip.cpp',
179
180                              'commands/trex_rpc_cmd_test.cpp',
181                              'commands/trex_rpc_cmd_general.cpp',
182                              'commands/trex_rpc_cmd_stream.cpp',
183
184                          ])
185
186
187# JSON package
188json_src = SrcGroup(dir='external_libs/json',
189                    src_list=[
190                        'jsoncpp.cpp'
191                        ])
192
193
194yaml_src = SrcGroup(dir='external_libs/yaml-cpp/src/',
195        src_list=[
196            'aliasmanager.cpp',
197            'binary.cpp',
198            'contrib/graphbuilder.cpp',
199            'contrib/graphbuilderadapter.cpp',
200            'conversion.cpp',
201            'directives.cpp',
202            'emitfromevents.cpp',
203            'emitter.cpp',
204            'emitterstate.cpp',
205            'emitterutils.cpp',
206            'exp.cpp',
207            'iterator.cpp',
208            'node.cpp',
209            'nodebuilder.cpp',
210            'nodeownership.cpp',
211            'null.cpp',
212            'ostream.cpp',
213            'parser.cpp',
214            'regex.cpp',
215            'scanner.cpp',
216            'scanscalar.cpp',
217            'scantag.cpp',
218            'scantoken.cpp',
219            'simplekey.cpp',
220            'singledocparser.cpp',
221            'stream.cpp',
222            'tag.cpp']);
223
224
225# stubs
226stubs = SrcGroup(dir='/src/stub/',
227        src_list=['zmq_stub.c'])
228
229
230bp =SrcGroups([
231                bp_sim_main,
232                bp_sim_gtest,
233                main_src,
234                cmn_src ,
235                stubs,
236                net_src ,
237                yaml_src,
238                json_src,
239                stateless_src,
240                rpc_server_src
241                ]);
242
243
244cxxflags_base =['-DWIN_UCODE_SIM',
245                '-DTREX_SIM',
246                '-D_BYTE_ORDER',
247                '-D_LITTLE_ENDIAN',
248                '-DLINUX',
249                '-D__STDC_LIMIT_MACROS',
250                '-D__STDC_FORMAT_MACROS',
251                '-g',
252                '-Wno-deprecated-declarations',
253                '-std=c++0x',
254       ];
255
256
257
258
259includes_path =''' ../src/pal/linux/
260                   ../src/pal/common/
261                   ../src/
262                   ../src/rpc-server/
263                   ../src/stateless/cp/
264                   ../src/stateless/dp/
265                   ../src/stateless/rx/
266                   ../src/stateless/messaging/
267                   ../external_libs/json/
268                   ../external_libs/zmq/include/
269                   ../external_libs/yaml-cpp/include/
270              ''';
271
272
273
274
275
276RELEASE_    = "release"
277DEBUG_      = "debug"
278PLATFORM_64 = "64"
279PLATFORM_32 = "32"
280
281
282class build_option:
283
284    def __init__(self, name, src, platform, debug_mode, is_pie, use = [], flags = [], rpath = []):
285      self.mode     = debug_mode;   ##debug,release
286      self.platform = platform; #['32','64']
287      self.is_pie = is_pie
288      self.name = name
289      self.src = src
290      self.use = use
291      self.flags = flags
292      self.rpath = rpath
293
294    def __str__(self):
295       s=self.mode+","+self.platform;
296       return (s);
297
298    def lib_name(self,lib_name_p,full_path):
299        if full_path:
300            return b_path+lib_name_p;
301        else:
302            return lib_name_p;
303    #private functions
304    def toLib (self,name,full_path = True):
305        lib_n = "lib"+name+".a";
306        return (self.lib_name(lib_n,full_path));
307
308    def toExe(self,name,full_path = True):
309        return (self.lib_name(name,full_path));
310
311    def is64Platform (self):
312        return ( self.platform == PLATFORM_64);
313
314    def isRelease (self):
315        return ( self.mode  == RELEASE_);
316
317    def isPIE (self):
318        return self.is_pie
319
320    def update_executable_name (self,name):
321        return self.update_name(name,"-")
322
323    def update_non_exe_name (self,name):
324        return self.update_name(name,"_")
325
326    def update_name (self,name,delimiter):
327        trg = copy.copy(name);
328        if self.is64Platform():
329            trg += delimiter + "64";
330        else:
331            trg += delimiter + "32";
332
333        if self.isRelease () :
334            trg += "";
335        else:
336            trg +=  delimiter + "debug";
337
338        if self.isPIE():
339            trg += delimiter + "pie"
340        return trg;
341
342    def cxxcomp_flags (self,flags):
343        result = copy.copy(flags);
344        if self.is64Platform () :
345            result+=['-m64'];
346        else:
347            result+=['-m32'];
348
349        if self.isRelease () :
350            result+=['-O2'];
351        else:
352            result+=['-O0','-DDEBUG','-D_DEBUG','-DSTILE_CPP_ASSERT','-DSTILE_SHIM_ASSERT'];
353
354        if self.isPIE():
355            result += ['-fPIE', '-DPATCH_FOR_PIE']
356
357        result += self.flags
358
359        return result;
360
361    def get_use_libs (self):
362        return self.use
363
364    def get_target (self):
365        return self.update_executable_name(self.name);
366
367    def get_flags (self):
368        return self.cxxcomp_flags(cxxflags_base);
369
370    def get_src (self):
371        return self.src.file_list(top)
372
373    def get_rpath (self):
374        return self.rpath
375
376    def get_link_flags(self):
377        # add here basic flags
378        base_flags = [];
379        if self.isPIE():
380            base_flags.append('-lstdc++')
381
382        #platform depended flags
383
384        if self.is64Platform():
385            base_flags += ['-m64']
386        else:
387            base_flags += ['-m32']
388            base_flags += ['-lrt']
389
390        if self.isPIE():
391            base_flags += ['-pie', '-DPATCH_FOR_PIE']
392
393        return base_flags;
394
395
396build_types = [
397               build_option(name = "bp-sim", src = bp, use = [''],debug_mode= DEBUG_, platform = PLATFORM_64, is_pie = False,
398                            flags = ['-Wall', '-Werror', '-Wno-sign-compare', '-Wno-strict-aliasing'],
399                            rpath = ['.']),
400
401               build_option(name = "bp-sim", src = bp, use = [''],debug_mode= RELEASE_,platform = PLATFORM_64, is_pie = False,
402                            flags = ['-Wall', '-Werror', '-Wno-sign-compare', '-Wno-strict-aliasing'],
403                            rpath = ['.']),
404
405              ]
406
407
408
409def build_prog (bld, build_obj):
410
411    bld.program(features='cxx cxxprogram',
412                includes =includes_path,
413                cxxflags =(build_obj.get_flags()+['-std=gnu++11',]),
414                linkflags = build_obj.get_link_flags(),
415                source = build_obj.get_src(),
416                use = build_obj.get_use_libs(),
417                lib = ['pthread', 'z', 'dl'],
418                rpath  = bld.env.RPATH + build_obj.get_rpath(),
419                target = build_obj.get_target())
420
421
422def build_type(bld,build_obj):
423    build_prog(bld, build_obj);
424
425
426def post_build(bld):
427    print("copy objects")
428
429    exec_p ="../scripts/"
430
431    for obj in build_types:
432        install_single_system(bld, exec_p, obj);
433
434def build(bld):
435
436    bld.add_post_fun(post_build);
437    for obj in build_types:
438        build_type(bld,obj);
439
440
441
442def build_info(bld):
443    pass;
444
445def install_single_system (bld, exec_p, build_obj):
446    o='build/linux/';
447    src_file =  os.path.realpath(o+build_obj.get_target())
448    if os.path.exists(src_file):
449        dest_file = exec_p +build_obj.get_target()
450        if not os.path.lexists(dest_file):
451            relative_path = os.path.relpath(src_file, exec_p)
452            os.symlink(relative_path, dest_file);
453
454
455
456
457