summaryrefslogtreecommitdiffstats
path: root/netservices.py
blob: 4e25d516bf7412929bd0aeba5331029c816a2ddd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# SPDX-License-Identifier: BSD-2-Clause

#  Copyright (C) 2022 On-Line Applications Research Corporation (OAR)
#  Written by Kinsey Moore <kinsey.moore@oarcorp.com>
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.

import json
import os

import rtems_waf.rtems as rtems

def removeprefix(data, prefix):
    if data.startswith(prefix):
        return data[len(prefix):]
    return data


def options(opt):
    copts = opt.option_groups['configure options']
    copts.add_option('--optimization',
                     default='-O2',
                     dest='optimization',
                     help='Optimaization level (default: %default)')


def add_flags(flags, new_flags):
    for flag in new_flags:
        if flag not in flags:
            flags.append(flag)


def check_net_lib(conf, lib, name):
    net_name = 'NET_' + name.upper()
    conf.check_cc(lib=lib,
                  ldflags=['-lrtemsdefaultconfig'],
                  uselib_store=net_name, mandatory=False)
    if 'LIB_' + net_name in conf.env:
        conf.env['LDFLAGS_' + net_name] = []
        conf.env.STACK_NAME = name
        return True
    if 'LIB_NET_LIBBSD' in conf.env:
        conf.env.LIB_NET_LIBBSD = ['bsd', 'm']
    return False


def bsp_configure(conf, arch_bsp):
    conf.env.OPTIMIZATION = conf.options.optimization
    conf.env.LIB += ['m']

    section_flags = ["-fdata-sections", "-ffunction-sections"]
    add_flags(conf.env.CFLAGS, section_flags)
    add_flags(conf.env.CXXFLAGS, section_flags)

    stacks = [check_net_lib(conf, 'bsd', 'libbsd'),
              check_net_lib(conf, 'networking', 'legacy'),
              check_net_lib(conf, 'lwip', 'lwip')]
    stack_count = stacks.count(True)
    if stack_count == 0:
        conf.fatal('No networking stack found')
    if stack_count != 1:
        conf.fatal('More than one networking stack found')


def build(bld):
    stack_name = bld.env.STACK_NAME
    stack_use = 'NET_' + stack_name.upper()
    stack_def = 'RTEMS_NET_' + stack_name.upper()
    stack_root = 'stack/' + stack_name
    stack_inc = str(bld.path.find_node(stack_root + '/include'))
    stack_common_inc = 'stack/common/include'
    stack_adapter_source = stack_root + '/net_adapter.c'

    ns_cflags = ['-g', '-Wall', bld.env.OPTIMIZATION]

    ntp_source_files = []
    ntp_incl = [stack_inc, stack_common_inc]

    arch_lib_path = rtems.arch_bsp_lib_path(bld.env.RTEMS_VERSION,
                                            bld.env.RTEMS_ARCH_BSP)

    with open('ntp-file-import.json', 'r') as cf:
        files = json.load(cf)
        for f in files['source-files-to-import']:
            ntp_source_files.append(os.path.join('./bsd', f))
        for f in files['header-paths-to-import']:
            ntp_incl.append(os.path.join('./bsd', f))

    bld.stlib(features='c',
              target='ntp',
              source=ntp_source_files,
              includes=ntp_incl + [stack_inc + '/ntp'],
              cflags=ns_cflags,
              defines=[stack_def, 'HAVE_CONFIG_H=1'],
              use=[stack_use])
    bld.install_files("${PREFIX}/" + arch_lib_path, ["libntp.a"])

    ttcp_incl = [stack_inc, stack_common_inc, 'ttcp/include']

    ttcp_source_files = ['ttcp/ttcp.c']

    bld.stlib(features='c',
              target='ttcp',
              source=ttcp_source_files,
              includes=ttcp_incl,
              cflags=ns_cflags,
              defines=[stack_def],
              use=[stack_use])
    bld.install_files("${PREFIX}/" + arch_lib_path, ["libttcp.a"])

    def install_headers(root_path):
        for root, dirs, files in os.walk(root_path):
            for name in files:
                ext = os.path.splitext(name)[1]
                src_root = os.path.split(root)
                path = os.path.join(src_root[0], src_root[1])
                if ext == '.h':
                    subpath = removeprefix(removeprefix(path, root_path), "/")
                    bld.install_files(
                        os.path.join("${PREFIX}",
                                     arch_lib_path,
                                     "include",
                                     subpath),
                        os.path.join(path, name)
                    )

    [install_headers(path) for path in ntp_incl]

    libs = ['rtemstest']

    ntp_test_incl = ntp_incl + ['testsuites', stack_common_inc]
    ntp_test_sources = ['testsuites/ntp01/test_main.c',
                        stack_adapter_source]

    bld.program(features='c',
                target='ntp01.exe',
                source=' '.join(ntp_test_sources),
                cflags=ns_cflags,
                includes=ntp_test_incl,
                defines=[stack_def],
                lib=libs,
                use=['ntp', stack_use])

    ttcp_test_incl = ttcp_incl + ['testsuites', stack_common_inc]
    ttcp_test_sources = ['testsuites/ttcpshell01/test_main.c']
    ttcp_test_sources += [stack_adapter_source]

    bld.program(features='c',
                target='ttcpshell01.exe',
                source=' '.join(ttcp_test_sources),
                cflags=ns_cflags,
                defines=[stack_def],
                includes=ttcp_test_incl,
                lib=libs,
                use=['ttcp',  stack_use])

    tlnt_test_incl = [stack_inc, stack_common_inc, 'testsuites']
    tlnt_test_sources = ['testsuites/telnetd01/init.c']
    tlnt_test_sources += [stack_adapter_source]

    bld.program(features='c',
                target='telnetd01.exe',
                source=' '.join(tlnt_test_sources),
                cflags=ns_cflags,
                defines=[stack_def],
                includes=tlnt_test_incl,
                lib=['telnetd'] + libs,
                use=[stack_use])