ws_main.py revision 558ce764
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             'inet_pton.cpp',
107             'bp_sim.cpp',
108             'os_time.cpp',
109             'rx_check.cpp',
110             'tuple_gen.cpp',
111             'platform_cfg.cpp',
112             'utl_yaml.cpp',
113             'rx_check_header.cpp',
114             'nat_check.cpp',
115             'nat_check_flow_table.cpp',
116             'pkt_gen.cpp',
117             'timer_wheel_pq.cpp',
118             'time_histogram.cpp',
119             'utl_json.cpp',
120             'utl_cpuu.cpp',
121             'msg_manager.cpp',
122             'trex_port_attr.cpp',
123             'publisher/trex_publisher.cpp',
124             'stateful_rx_core.cpp',
125             'flow_stat.cpp',
126             'flow_stat_parser.cpp',
127             'trex_watchdog.cpp',
128             'trex_client_config.cpp',
129             'pal/linux/pal_utl.cpp',
130             'pal/linux/mbuf.cpp',
131             'pal/common/common_mbuf.cpp',
132             'sim/trex_sim_stateless.cpp',
133             'sim/trex_sim_stateful.cpp'
134             ]);
135
136cmn_src = SrcGroup(dir='src/common',
137    src_list=[
138        'gtest-all.cc',
139        'gtest_main.cc',
140        'basic_utils.cpp',
141        'captureFile.cpp',
142        'erf.cpp',
143        'pcap.cpp',
144        'base64.cpp'
145        ]);
146
147
148net_src = SrcGroup(dir='src/common/Network/Packet',
149        src_list=[
150           'CPktCmn.cpp',
151           'EthernetHeader.cpp',
152           'IPHeader.cpp',
153           'IPv6Header.cpp',
154           'TCPHeader.cpp',
155           'TCPOptions.cpp',
156           'UDPHeader.cpp',
157           'MacAddress.cpp',
158           'VLANHeader.cpp']);
159
160# stateless code
161stateless_src = SrcGroup(dir='src/stateless/',
162                          src_list=['cp/trex_stream.cpp',
163                                    'cp/trex_stream_vm.cpp',
164                                    'cp/trex_stateless.cpp',
165                                    'cp/trex_stateless_port.cpp',
166                                    'cp/trex_streams_compiler.cpp',
167                                    'cp/trex_vm_splitter.cpp',
168                                    'cp/trex_dp_port_events.cpp',
169                                    'dp/trex_stateless_dp_core.cpp',
170                                    'messaging/trex_stateless_messaging.cpp',
171                                    ])
172# RPC code
173rpc_server_src = SrcGroup(dir='src/rpc-server/',
174                          src_list=[
175                              'trex_rpc_server.cpp',
176                              'trex_rpc_req_resp_server.cpp',
177                              'trex_rpc_async_server.cpp',
178                              'trex_rpc_jsonrpc_v2_parser.cpp',
179                              'trex_rpc_cmds_table.cpp',
180                              'trex_rpc_cmd.cpp',
181                              'trex_rpc_zip.cpp',
182
183                              'commands/trex_rpc_cmd_test.cpp',
184                              'commands/trex_rpc_cmd_general.cpp',
185                              'commands/trex_rpc_cmd_stream.cpp',
186
187                          ])
188
189
190# JSON package
191json_src = SrcGroup(dir='external_libs/json',
192                    src_list=[
193                        'jsoncpp.cpp'
194                        ])
195
196
197yaml_src = SrcGroup(dir='external_libs/yaml-cpp/src/',
198        src_list=[
199            'aliasmanager.cpp',
200            'binary.cpp',
201            'contrib/graphbuilder.cpp',
202            'contrib/graphbuilderadapter.cpp',
203            'conversion.cpp',
204            'directives.cpp',
205            'emitfromevents.cpp',
206            'emitter.cpp',
207            'emitterstate.cpp',
208            'emitterutils.cpp',
209            'exp.cpp',
210            'iterator.cpp',
211            'node.cpp',
212            'nodebuilder.cpp',
213            'nodeownership.cpp',
214            'null.cpp',
215            'ostream.cpp',
216            'parser.cpp',
217            'regex.cpp',
218            'scanner.cpp',
219            'scanscalar.cpp',
220            'scantag.cpp',
221            'scantoken.cpp',
222            'simplekey.cpp',
223            'singledocparser.cpp',
224            'stream.cpp',
225            'tag.cpp']);
226
227
228# stubs
229stubs = SrcGroup(dir='/src/stub/',
230        src_list=['zmq_stub.c'])
231
232
233bp =SrcGroups([
234                bp_sim_main,
235                bp_sim_gtest,
236                main_src,
237                cmn_src ,
238                stubs,
239                net_src ,
240                yaml_src,
241                json_src,
242                stateless_src,
243                rpc_server_src
244                ]);
245
246
247cxxflags_base =['-DWIN_UCODE_SIM',
248                '-DTREX_SIM',
249                '-D_BYTE_ORDER',
250                '-D_LITTLE_ENDIAN',
251                '-DLINUX',
252                '-D__STDC_LIMIT_MACROS',
253                '-D__STDC_FORMAT_MACROS',
254                '-g',
255                '-Wno-deprecated-declarations',
256                '-std=c++0x',
257       ];
258
259
260
261
262includes_path =''' ../src/pal/linux/
263                   ../src/pal/common/
264                   ../src/
265                   ../src/rpc-server/
266                   ../src/stateless/cp/
267                   ../src/stateless/dp/
268                   ../src/stateless/rx/
269                   ../src/stateless/messaging/
270                   ../external_libs/json/
271                   ../external_libs/zmq/include/
272                   ../external_libs/yaml-cpp/include/
273              ''';
274
275
276
277
278
279RELEASE_    = "release"
280DEBUG_      = "debug"
281PLATFORM_64 = "64"
282PLATFORM_32 = "32"
283
284
285class build_option:
286
287    def __init__(self, name, src, platform, debug_mode, is_pie, use = [], flags = [], rpath = []):
288      self.mode     = debug_mode;   ##debug,release
289      self.platform = platform; #['32','64']
290      self.is_pie = is_pie
291      self.name = name
292      self.src = src
293      self.use = use
294      self.flags = flags
295      self.rpath = rpath
296
297    def __str__(self):
298       s=self.mode+","+self.platform;
299       return (s);
300
301    def lib_name(self,lib_name_p,full_path):
302        if full_path:
303            return b_path+lib_name_p;
304        else:
305            return lib_name_p;
306    #private functions
307    def toLib (self,name,full_path = True):
308        lib_n = "lib"+name+".a";
309        return (self.lib_name(lib_n,full_path));
310
311    def toExe(self,name,full_path = True):
312        return (self.lib_name(name,full_path));
313
314    def is64Platform (self):
315        return ( self.platform == PLATFORM_64);
316
317    def isRelease (self):
318        return ( self.mode  == RELEASE_);
319
320    def isPIE (self):
321        return self.is_pie
322
323    def update_executable_name (self,name):
324        return self.update_name(name,"-")
325
326    def update_non_exe_name (self,name):
327        return self.update_name(name,"_")
328
329    def update_name (self,name,delimiter):
330        trg = copy.copy(name);
331        if self.is64Platform():
332            trg += delimiter + "64";
333        else:
334            trg += delimiter + "32";
335
336        if self.isRelease () :
337            trg += "";
338        else:
339            trg +=  delimiter + "debug";
340
341        if self.isPIE():
342            trg += delimiter + "pie"
343        return trg;
344
345    def cxxcomp_flags (self,flags):
346        result = copy.copy(flags);
347        if self.is64Platform () :
348            result+=['-m64'];
349        else:
350            result+=['-m32'];
351
352        if self.isRelease () :
353            result+=['-O3'];
354        else:
355            result+=['-O0','-DDEBUG','-D_DEBUG','-DSTILE_CPP_ASSERT','-DSTILE_SHIM_ASSERT'];
356
357        if self.isPIE():
358            result += ['-fPIE', '-DPATCH_FOR_PIE']
359
360        result += self.flags
361
362        return result;
363
364    def get_use_libs (self):
365        return self.use
366
367    def get_target (self):
368        return self.update_executable_name(self.name);
369
370    def get_flags (self):
371        return self.cxxcomp_flags(cxxflags_base);
372
373    def get_src (self):
374        return self.src.file_list(top)
375
376    def get_rpath (self):
377        return self.rpath
378
379    def get_link_flags(self):
380        # add here basic flags
381        base_flags = [];
382        if self.isPIE():
383            base_flags.append('-lstdc++')
384
385        #platform depended flags
386
387        if self.is64Platform():
388            base_flags += ['-m64']
389        else:
390            base_flags += ['-m32']
391            base_flags += ['-lrt']
392
393        if self.isPIE():
394            base_flags += ['-pie', '-DPATCH_FOR_PIE']
395
396        return base_flags;
397
398
399build_types = [
400               build_option(name = "bp-sim", src = bp, use = [''],debug_mode= DEBUG_, platform = PLATFORM_64, is_pie = False,
401                            flags = ['-Wall', '-Werror', '-Wno-sign-compare', '-Wno-strict-aliasing'],
402                            rpath = ['.']),
403
404               build_option(name = "bp-sim", src = bp, use = [''],debug_mode= RELEASE_,platform = PLATFORM_64, is_pie = False,
405                            flags = ['-Wall', '-Werror', '-Wno-sign-compare', '-Wno-strict-aliasing'],
406                            rpath = ['.']),
407
408              ]
409
410
411
412def build_prog (bld, build_obj):
413
414    bld.program(features='cxx cxxprogram',
415                includes =includes_path,
416                cxxflags =(build_obj.get_flags()+['-std=gnu++11',]),
417                linkflags = build_obj.get_link_flags(),
418                source = build_obj.get_src(),
419                use = build_obj.get_use_libs(),
420                lib = ['pthread', 'z', 'dl'],
421                rpath  = bld.env.RPATH + build_obj.get_rpath(),
422                target = build_obj.get_target())
423
424
425def build_type(bld,build_obj):
426    build_prog(bld, build_obj);
427
428
429def post_build(bld):
430    print("copy objects")
431
432    exec_p ="../scripts/"
433
434    for obj in build_types:
435        install_single_system(bld, exec_p, obj);
436
437def build(bld):
438
439    bld.add_post_fun(post_build);
440    for obj in build_types:
441        build_type(bld,obj);
442
443
444
445def build_info(bld):
446    pass;
447
448def install_single_system (bld, exec_p, build_obj):
449    o='build/linux/';
450    src_file =  os.path.realpath(o+build_obj.get_target())
451    if os.path.exists(src_file):
452        dest_file = exec_p +build_obj.get_target()
453        if not os.path.lexists(dest_file):
454            relative_path = os.path.relpath(src_file, exec_p)
455            os.symlink(relative_path, dest_file);
456
457
458
459
460