This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
wscript
1048 lines (890 loc) · 39.6 KB
/
wscript
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der
# angewandten Forschung e.V. All rights reserved.
#
# BSD 3-Clause License
# 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.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# 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 HOLDER 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.
#
# We kindly request you to use one or more of the following phrases to refer to
# foxBMS in your hardware, software, documentation or advertising materials:
#
# ″This product uses parts of foxBMS®″
#
# ″This product includes parts of foxBMS®″
#
# ″This product is derived from foxBMS®″
import os
import sys
import datetime
import posixpath
import re
import yaml
import jinja2
# try to use FullLoader (PyYAML5.1+ and fall back to normale Loader)
try:
from yaml import FullLoader as YAMLLoader
except ImportError:
from yaml import Loader as YAMLLoader
from waflib import Utils, Options, Errors, Logs
from waflib import Task, TaskGen
from waflib.Tools.compiler_c import c_compiler
from waflib.Tools.c import c
from waflib.Tools import c_preproc
# overwrite c task to use absolute paths for the input file
class c(Task.Task): # noqa: F811
run_str = '${CC} ${ARCH_ST:ARCH} ${CFLAGS} ${FRAMEWORKPATH_ST:FRAMEWORKPATH} ${CPPPATH_ST:INCPATHS} ${DEFINES_ST:DEFINES} ${CC_SRC_F}${SRC[0].abspath()} ${CC_TGT_F}${TGT[0].abspath()} ${CPPFLAGS}'
vars = ['CCDEPS']
ext_in = ['.h']
scan = c_preproc.scan
out = 'build'
variants = ['primary', 'secondary', 'libs', 'primary_bare', 'secondary_bare']
from waflib.Build import BuildContext, CleanContext, ListContext, StepContext # noqa: E402
for x in variants:
for y in (BuildContext,
CleanContext,
ListContext,
StepContext):
name = y.__name__.replace('Context', '').lower()
class tmp_1(y):
if name == 'build':
__doc__ = '''executes the {} of {}'''.format(name, x)
elif name == 'clean':
__doc__ = '''cleans the project {}'''.format(x)
elif name == 'list':
__doc__ = '''lists the targets to execute for {}'''.format(x)
elif name == 'step':
__doc__ = '''executes tasks in a step-by-step fashion, ''' \
'''for debugging of {}'''.format(x)
cmd = name + '_' + x
variant = x
dox = 'doxygen'
class tmp_2(BuildContext):
__doc__ = '''creates the {} documentation of {}'''.format(dox, x)
cmd = dox + '_' + x
fun = dox
variant = x
def options(opt):
opt.load('compiler_c')
opt.load('python')
opt.load(['doxygen', 'sphinx_build', 'cpplint', 'flake8', 'cppcheck'],
tooldir=os.path.join('tools', 'waftools'))
opt.add_option('-t', '--target', action='store', default='debug',
help='build target: debug (default)/release', dest='target')
opt.add_option('-l', '--libs', action='store', default='',
help='name of the library to be used')
for k in ('--targets',
'--out',
'--top',
'--prefix',
'--destdir',
'--bindir',
'--libdir',
'--msvc_version',
'--msvc_targets',
'--no-msvc-lazy',
'--force',
'--check-c-compiler'):
option = opt.parser.get_option(k)
if option:
opt.parser.remove_option(k)
mctx = waflib.Context.classes
mctx.remove(waflib.Build.InstallContext)
mctx.remove(waflib.Build.UninstallContext)
def configure(conf):
if ' ' in conf.path.abspath():
conf.fatal(f'path to foxbms must not contain spaces'
f' (current path: {conf.path.abspath()}).')
conf.load('python')
conf.check_python_version((3, 6))
# Setup the whole toolchain (compiler, interpreter etc.)
print('Compiler toolchain:')
pref = 'arm-none-eabi-' # prefix for all gcc related tools
exe_extension = ''
if sys.platform.startswith('win'):
conf.env.jinja2_newline = '\r\n'
exe_extension = '.exe'
else:
conf.env.jinja2_newline = '\n'
conf.env.CC = pref + 'gcc' + exe_extension
conf.env.AR = pref + 'ar' + exe_extension
conf.env.LINK_CC = pref + 'g++' + exe_extension
gcc_tools = 'cpp ranlib as strip objcopy objdump size gdb'.split()
for k in reversed(sorted(gcc_tools, key=len)):
conf.find_program(pref + k, var=k.upper(), mandatory=True)
for key in c_compiler: # force only using gcc
c_compiler[key] = ['gcc']
conf.load('compiler_c')
conf.load(['doxygen', 'sphinx_build', 'cpplint', 'flake8', 'cppcheck'])
print('General tools:')
conf.find_program('python', var='PYTHON', mandatory=True)
conf.find_program('git', var='GIT', mandatory=False)
# define configuration files etc.
# parsing the version info based on the general documentation
conf.env.sphinx_doc_dir = os.path.join('documentation', 'sphinx')
to_posix = (conf.env.sphinx_doc_dir).split(os.sep)
conf.env.sphinx_doc_dir_posix = posixpath.join(*(to_posix))
conf.env.sphinx_conf_path = os.path.join(conf.env.sphinx_doc_dir,
'conf.py')
version_info_file = os.path.join(conf.env.sphinx_doc_dir, 'macros.rst')
with open(version_info_file, 'r', encoding='UTF-8') as f:
txt = f.read()
rgx = r'\.\.[ ]\|version\|[ ]replace::[ ]``(\d{1,}\.\d{1,}\.\d{1,})``'
tmp_version = re.search(rgx, txt)
try:
conf.env.version = tmp_version.group(1)
except AttributeError:
err_msg = 'Could not find a version info in {}.\
'.format(version_info_file)
conf.fatal(err_msg)
conf.env.appname = 'foxbms'
conf.env.appname_prefix = conf.env.appname
conf.env.vendor = 'Fraunhofer IISB'
conf.env.version_primary = conf.env.version
conf.env.version_secondary = conf.env.version
# Setup the compiler and link flags
with open('compiler-flags.yml', 'r') as stream:
try:
compiler_flags = yaml.load(stream, Loader=YAMLLoader)
except yaml.YAMLError as exc:
conf.fatal(exc)
cflags = compiler_flags['CFLAGS']
conf.env.append_value('CFLAGS', [x for x in cflags if type(x) == str])
for x in cflags:
if type(x) is dict:
add_flag = 'CFLAGS_' + list(x.keys())[0]
conf.env.append_value(add_flag, list())
if list(x.values())[0] is None:
continue
conf.env.append_value(add_flag, *(x.values()))
conf.env.ASMFLAGS = compiler_flags['ASMFLAGS']
conf.env.LINKFLAGS = compiler_flags['LINKFLAGS']
conf.env.XLINKER = compiler_flags['XLINKER']
# get HAL version and floating point version based on compiler define and
# check if cpu and floating point version are fitting together
cpu = None
floating_point_version = None
for _cflag in conf.env.CFLAGS:
if 'mcpu' in _cflag:
cdef, cpu = _cflag.split('=')
if 'mfpu' in _cflag:
cdef, floating_point_version = _cflag.split('=')
if not cpu:
conf.fatal('cflag \'mcpu\' missing.')
if not floating_point_version:
conf.fatal('cflag \'mfpu\' missing.')
if cpu == 'cortex-m4':
conf.env.CPU_MAJOR = 'STM32F4xx'
if floating_point_version != 'fpv4-sp-d16':
conf.fatal('floating point unit flag not compatible with cpu')
else:
conf.fatal(f'cpu \'{cpu}\' is not supported')
utcnow = datetime.datetime.utcnow()
utcnow = ''.join(utcnow.isoformat('-').split('.')
[0].replace(':', '-').split('-'))
conf.env.timestamp = utcnow
conf.define('BUILD_APPNAME_PREFIX', conf.env.appname_prefix)
for x in variants:
conf.define(
('BUILD_APPNAME_{}'.format(x)).upper(),
'{}_{}'.format(conf.env.appname_prefix, x)[:14],
comment='Define is trimmed to max. 14 characters'.format(x))
conf.define('BUILD_VERSION_PRIMARY', conf.env.version_primary)
conf.define('BUILD_VERSION_SECONDARY', conf.env.version_secondary)
conf.env.target = conf.options.target
env_debug = conf.env.derive()
env_debug.detach()
env_release = conf.env.derive()
env_release.detach()
# configuration for debug
conf.setenv('debug', env_debug)
conf.define('RELEASE', 1)
conf.undefine('DEBUG')
conf.env.CFLAGS += ['-g', '-O0']
# configuration for release
conf.setenv('release', env_release)
conf.env.CFLAGS += ['-O2']
if conf.options.target == 'release':
conf.setenv('', env_release)
else:
conf.setenv('', env_debug)
env_release.store(os.path.join(out, 'env-store.log'))
config_dir = 'config'
conf.path.get_bld().make_node(config_dir).mkdir()
conf.confdir = conf.path.get_bld().find_node(config_dir)
_cmd = [Utils.subst_vars('${CC}', conf.env), '-dM', '-E', '-']
std_out, std_err = conf.cmd_and_log(_cmd, output=0, input='\n'.encode())
std_out = '/* WARNING: DO NOT EDIT */\n' \
'/* INTERNAL GCC MARCOS */\n' \
'/* FOR INFORMATION ONLY */\n' \
'\n' \
'{}'.format(std_out)
conf.confdir.make_node('gcc_builtin_macros.h').write(std_out)
header_file_name = conf.env.appname_prefix + 'config.h'
header_file_path = os.path.join(config_dir, header_file_name),
def_guard = header_file_name.upper().replace('.H', '_H_')
conf.write_config_header(header_file_path, guard=def_guard)
print('---')
print('Vendor: {}'.format(conf.env.vendor))
print('Appname prefix: {}'.format(conf.env.appname_prefix))
print('Applications: {}'.format(', '.join(variants)))
print('Version primary: {}'.format(conf.env.version_primary))
print('Version secondary: {}'.format(conf.env.version_secondary))
print('---')
print('Config header: {}'.format(conf.env.cfg_files))
print('Build configuration: {}'.format(conf.env.target))
print('---')
conf.path.get_bld().make_node('cflags.log').write('\n'.join(conf.env.CFLAGS) + '\n')
conf.path.get_bld().make_node('cflags-foxbms.log').write('\n'.join(conf.env.CFLAGS_foxbms) + '\n')
conf.path.get_bld().make_node('cflags-freertos.log').write('\n'.join(conf.env.CFLAGS_freertos) + '\n')
conf.path.get_bld().make_node('hal.log').write('\n'.join(conf.env.CFLAGS_hal) + '\n')
conf.path.get_bld().make_node('asmflags.log').write('\n'.join(conf.env.ASMFLAGS) + '\n')
conf.path.get_bld().make_node('linkflags.log').write('\n'.join(conf.env.LINKFLAGS) + '\n')
conf.path.get_bld().make_node('xlinker.log').write('\n'.join(conf.env.XLINKER) + '\n')
lib_dir = conf.path.get_bld().make_node('lib')
lib_dir.mkdir()
conf.env.append_value('LIBPATH', lib_dir.abspath())
conf.env.LIB_DIR_LIBS = lib_dir.abspath()
print(f'Additional Library directory: {lib_dir.abspath()}')
inc_dir = conf.path.get_bld().make_node('include')
inc_dir.mkdir()
conf.env.append_value('INCLUDES', inc_dir.abspath())
conf.env.INCLUDE_DIR_LIBS = inc_dir.abspath()
print(f'Additional Include directory: {inc_dir.abspath()}')
if conf.options.libs:
conf.env.USER_DEFINED_LIBS = conf.options.libs
print(f'Using library: {conf.options.libs}')
else:
conf.env.USER_DEFINED_LIBS = None
# calculate expected binary size from flasheader credentials
conf.env.flash_begin_adr = 0x080FFF48 & 0x00ffffff
conf.env.flash_header_adr = 0x080FFF00 & 0x00ffffff
conf.env.flash_end_adr = 0x080FFF4C & 0x00ffffff
# cppcheck configuration
if conf.env.CPPCHECK:
cppcheck_dir = conf.path.get_bld().make_node('cppcheck')
cppcheck_dir.mkdir()
templateLoader = jinja2.FileSystemLoader(searchpath=os.path.join('tools', 'cppcheck'))
templateEnv = jinja2.Environment(loader=templateLoader, newline_sequence=conf.env.jinja2_newline)
template = templateEnv.get_template('cppcheck.template')
outputText = template.render(
bld_dir='.',
src_dir='../../embedded-software',
inc_dirs=['../../embedded-software/libs',
'../../embedded-software/mcu-common',
'../../embedded-software/mcu-primary',
'../../embedded-software/mcu-secondary',
'../../build/primary/embedded-software/mcu-primary/src/general',
'../../build/secondary/embedded-software/mcu-secondary/src/general'],
addons=['threadsafety', 'y2038', 'cert', 'misra'])
cppcheck_cfg = cppcheck_dir.make_node('cppcheck.cppcheck')
cppcheck_cfg.write(outputText)
conf.env.cppcheck_dir = cppcheck_dir.abspath()
conf.env.cppcheck_cfg = cppcheck_cfg.abspath()
print(f'---\ncppcheck configuration: {conf.env.cppcheck_cfg}')
print('---\ngit information:')
try:
(std_out, std_err) = conf.cmd_and_log([conf.env.GIT[0], 'config', '--get', 'remote.origin.url'], output=waflib.Context.BOTH)
except Errors.WafError as e:
Logs.warn('--> directory is not a git repository')
t = ('NOREMOTE', [True])
else:
t = (std_out.strip(), [False])
conf.env.append_value('GIT_REPO_PATH', t[0])
conf.env.append_value('GIT_DIRTY_ALWAYS', t[1])
print(f'repository path: {conf.env.GIT_REPO_PATH[0]}')
if conf.env.GIT_DIRTY_ALWAYS[0]:
print(f'no remote: {conf.env.GIT_DIRTY_ALWAYS[0]}')
conf.env.FILE_TEMPLATE_C = conf.path.find_node('tools/styleguide/file-templates/template.c.jinja2').read()
conf.env.FILE_TEMPLATE_H = conf.path.find_node('tools/styleguide/file-templates/template.h.jinja2').read()
def build(bld):
import sys
import logging
from waflib import Logs
if not bld.variant:
bld.fatal(f'A {bld.cmd} variant must be specified, run \'{sys.executable} {sys.argv[0]} --help\'')
log_file = os.path.join(out, 'build_' + bld.variant + '.log')
bld.logger = Logs.make_logger(log_file, out)
hdlr = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
bld.logger.addHandler(hdlr)
if bld.variant in ('primary', 'secondary'):
bld.add_pre_fun(repostate)
bld.env.es_dir = os.path.normpath('embedded-software')
if bld.variant == 'libs':
src_dir = os.path.normpath('{}'.format(bld.variant))
if bld.cmd.startswith('clean'):
for x in bld.path.ant_glob(f'{out}/lib/**/*.a {out}/include/**/*.h'):
x.delete()
else:
# for bare build we *basically* have the same build, therefore the
# source folder stays the same, but *_bare builds do not include
# FreeRTOS
if bld.variant.endswith('_bare'):
src_dir = os.path.normpath(
'mcu-{}'.format(bld.variant.replace('_bare', '')))
bld.env.FreeRTOS_dirs = '' # no FreeRTOS in bare build
else:
src_dir = os.path.normpath('mcu-{}'.format(bld.variant))
bld.env.FreeRTOS_dirs = ' '.join([
os.path.join(bld.top_dir, bld.env.es_dir, 'mcu-freertos', 'Source'),
os.path.join(bld.top_dir, bld.env.es_dir, 'mcu-freertos', 'Source', 'include'),
os.path.join(bld.top_dir, bld.env.es_dir, 'mcu-freertos', 'Source', 'portable', 'GCC', 'ARM_CM4F')])
bld.env.mcu_dir = src_dir
bld.env.common_dir = os.path.normpath('mcu-common')
bld.env.hal_dirs = ' '.join([
os.path.join(bld.top_dir, bld.env.es_dir, 'mcu-hal', 'CMSIS', 'Device', 'ST', bld.env.CPU_MAJOR, 'Include'),
os.path.join(bld.top_dir, bld.env.es_dir, 'mcu-hal', 'CMSIS', 'Include'),
os.path.join(bld.top_dir, bld.env.es_dir, 'mcu-hal', bld.env.CPU_MAJOR + '_HAL_Driver', 'Inc')])
t = os.path.dirname(bld.env.cfg_files[0])
bld.env.append_value('INCLUDES', t)
bld.recurse(os.path.join(bld.env.es_dir, src_dir))
def repostate(bld):
Logs.info('Adding git information...')
file_name = 'gitinfo_cfg'
gitinfo_dir = os.path.join(bld.env.es_dir, bld.env.mcu_dir, 'src', 'general')
bld.path.get_bld().make_node(gitinfo_dir).mkdir()
bld.env.git_infoc = bld.path.get_bld().make_node(os.path.join(gitinfo_dir, f'{file_name}.c'))
bld.env.git_infoh = bld.path.get_bld().make_node(os.path.join(gitinfo_dir, f'{file_name}.h'))
templatec = jinja2.Environment(loader=jinja2.BaseLoader, keep_trailing_newline=True, newline_sequence=bld.env.jinja2_newline).from_string(bld.env.FILE_TEMPLATE_C)
templateh = jinja2.Environment(loader=jinja2.BaseLoader, keep_trailing_newline=True, newline_sequence=bld.env.jinja2_newline).from_string(bld.env.FILE_TEMPLATE_H)
_date = datetime.datetime.today().strftime('%d.%m.%Y')
if bld.env.GIT_REPO_PATH[0] == "NOREMOTE":
bld.env.GIT_COMMIT_ID = 'NOVALIDCOMMIT'
bld.env.CLEAN, bld.env.DIRTY = (0, 1)
bld.env.GIT_STATUS = 'GIT_DIRTY_STARTUP'
else:
try:
(std_out, std_err) = bld.cmd_and_log([bld.env.GIT[0], 'rev-parse', 'HEAD'], output=waflib.Context.BOTH)
except Errors.WafError as e:
Logs.error(e)
bld.env.GIT_COMMIT_ID = 'NOVALIDCOMMIT'
else:
bld.env.GIT_COMMIT_ID = std_out.strip()
try:
(std_out, std_err) = bld.cmd_and_log([bld.env.GIT[0], 'status'], output=waflib.Context.BOTH)
except Errors.WafError as e:
std_out = None
Logs.error(e)
else:
if "nothing to commit, working tree clean" in std_out:
bld.env.CLEAN, bld.env.DIRTY = (1, 0)
bld.env.GIT_STATUS = 'GIT_CLEAN_STARTUP'
else:
bld.env.CLEAN, bld.env.DIRTY = (0, 1)
bld.env.GIT_STATUS = 'GIT_DIRTY_STARTUP'
print(f'clean: {bld.env.CLEAN} \ndirty: {bld.env.DIRTY}')
# header file
brief = 'Contains information about the repository state'
macros = []
defs = ['''\
typedef enum {
GIT_CLEAN_STARTUP = 0,
GIT_DIRTY_STARTUP = 1,
} GIT_STARTUP_BIT_e;''',
f'''\
typedef struct {{
STD_RETURN_TYPE_e clean_repo_build;
STD_RETURN_TYPE_e allow_startup;
}} GIT_STATUS_s;''',
f'''\
typedef struct {{
char repo_url[{len(bld.env.GIT_REPO_PATH[0])+1}];
char commit_id[{len(bld.env.GIT_COMMIT_ID)+1}];
uint8_t always_dirty; /* if there is no remote, this is always set */
GIT_STARTUP_BIT_e git_startup_bit; /* set dependent on git status */
}} GIT_ValidStruct_s;''']
externvars = [
'extern GIT_STATUS_s git_status;',
'extern const GIT_ValidStruct_s git_validation;']
externfunsproto = ['extern STD_RETURN_TYPE_e GIT_checkStartup(void);']
txt_git_info_h = templateh.render(
filename=os.path.splitext(file_name)[0],
add_author_info='(autogenerated)',
filecreation=_date,
ingroup='GIT_INFO',
prefix='GIT',
brief=brief,
details='',
includes=['general.h'],
macros=macros,
defs=defs,
externvars=externvars,
externfunsproto=externfunsproto)
Logs.info(f'Created {bld.env.git_infoh.relpath()}')
bld.env.git_infoh.write(txt_git_info_h)
# implementation file
startupfun = ['''\
STD_RETURN_TYPE_e GIT_checkStartup() {
STD_RETURN_TYPE_e retval = E_NOT_OK;
if (git_validation.git_startup_bit == GIT_CLEAN_STARTUP &&
git_validation.always_dirty == 0) {
retval = E_OK;
}
return retval;
}''']
externvars = [
'''GIT_STATUS_s git_status = {0xFF, 0xFF};''',
f'''\
extern const GIT_ValidStruct_s git_validation = {{
"{bld.env.GIT_REPO_PATH[0]}", /* remote repository path */
"{bld.env.GIT_COMMIT_ID}", /* last commit hash that could be retrivied */
{int(bld.env.GIT_DIRTY_ALWAYS[0])}, /* repository has a valid remote */
{bld.env.GIT_STATUS}, /* is the repository dirty? */
}};
''']
txt_git_info_c = templatec.render(
filename=os.path.splitext(file_name)[0],
add_author_info='(autogenerated)',
inc_files=[],
filecreation=_date,
ingroup='GIT_INFO',
prefix='GIT',
brief='Contains information about the repository state',
externvars=externvars,
externfunsimpl=startupfun,
details='')
bld.env.git_infoc.write(txt_git_info_c)
Logs.info(f'Created {bld.env.git_infoc.relpath()}')
Logs.info('done...')
def doxygen(bld):
import sys
import logging
from waflib import Logs
if not bld.variant:
bld.fatal(f'A {bld.cmd} variant must be specified, run \'{sys.executable} {sys.argv[0]} --help\'')
if not bld.env.DOXYGEN:
bld.fatal(f'Doxygen was not configured. Run \'{sys.executable} {sys.argv[0]} --help\'')
_docbuilddir = os.path.normpath(bld.bldnode.abspath())
doxygen_conf_dir = os.path.join('documentation', 'doxygen')
os.makedirs(_docbuilddir, exist_ok=True)
conf_file = 'doxygen-{}.conf'.format(bld.variant)
doxygenconf = os.path.join(doxygen_conf_dir, conf_file)
log_file = os.path.join(
bld.bldnode.abspath(), 'doxygen_' + bld.variant + '.log')
bld.logger = Logs.make_logger(log_file, out)
hdlr = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
bld.logger.addHandler(hdlr)
bld(features='doxygen', doxyfile=doxygenconf)
def flake8(bld):
bld(features='flake8')
def cppcheck(bld):
class tsk_cppcheck(Task.Task):
def scan(self):
nodes = bld.path.ant_glob('**/*.c **/*.h')
return (nodes, [])
def run(self):
prg = Utils.subst_vars('${CPPCHECK}', bld.env)
cfg = Utils.subst_vars('${cppcheck_cfg}', bld.env)
cmd = [prg, f'--project={cfg}']
if bld.env.CPPCHECK_ERROR_EXITCODE:
opt = [f'--error-exitcode={bld.env.CPPCHECK_ERROR_EXITCODE}']
cmd.extend(opt)
std_out, std_err = self.generator.bld.cmd_and_log(cmd, output=waflib.Context.BOTH)
if std_err:
Logs.error(std_err)
if not std_err or bld.env.CPPCHECK_ERROR_EXITCODE == 0:
self.outputs[0].write(std_out)
@TaskGen.feature('run_cppcheck')
def add_cppcheck(self):
self.create_task('tsk_cppcheck', tgt=self.path.find_or_declare('cppcheck').make_node('cppcheck.out'))
bld(features='run_cppcheck')
def cpplint(bld):
from waflib import Logs
class tsk_cpplint(Task.Task):
def keyword(self):
return 'Linting'
def scan(self):
node = self.inputs[0]
return ([node], [])
def run(self):
cmd = [Utils.subst_vars('${CPPLINT}', bld.env)] + self.generator.env.cpplint_options + [self.inputs[0].abspath()]
Logs.debug(' '.join(cmd))
proc = Utils.subprocess.Popen(cmd, stdout=Utils.subprocess.PIPE, stderr=Utils.subprocess.PIPE, shell=True)
std_out, std_err = proc.communicate()
std_out, std_err = std_out.decode(), std_err.decode()
if proc.returncode:
Logs.error(std_err)
self.outputs[0].change_ext('.error').write(std_err)
else:
self.outputs[0].change_ext('.error').delete()
self.outputs[0].write(std_out)
def set_out_dir(bld, src_node, out_dir='cpplint', ext='.cpplint'):
my_bld_node = bld.bldnode.make_node(out_dir)
rp = src_node.get_bld().relpath()
bld_target = my_bld_node.make_node(rp + ext)
return bld_target
@TaskGen.feature('run_cpplint')
def add_cpplint(self):
srcs = bld.path.ant_glob(self.env.cpplint_src, excl=self.env.cpplint_excl)
for src_file in srcs:
tgt = set_out_dir(bld, src_file)
self.create_task('tsk_cpplint', src=src_file, tgt=tgt)
with open(bld.env.CPPLINT_CONF, 'r') as stream:
try:
cpplint_conf = yaml.load(stream, Loader=YAMLLoader)
except yaml.YAMLError as exc:
bld.fatal(exc)
src = cpplint_conf['files']['include']
filters = cpplint_conf['filter']
excl_tmp = cpplint_conf['files']['exclude']
excl = []
for x in excl_tmp:
if not x.startswith('**/'):
excl.append('**/' + x)
else:
excl.append(x)
bld.env.cpplint_options = ['--output={}'.format(cpplint_conf['output'])]
bld.env.cpplint_options += ['--linelength={}'.format(cpplint_conf['linelength'])]
bld.env.cpplint_options += ['--filter=' + ','.join(filters)]
bld.env.cpplint_src = src
bld.env.cpplint_excl = excl
bld(features='run_cpplint')
def sphinx(bld):
import sys
import logging
from waflib import Logs
if not bld.env.SPHINX_BUILD:
bld.fatal('ERROR: cannot build documentation (\'sphinx-build\' is not'
'found in PATH)')
log_file = os.path.join(bld.bldnode.abspath(), 'sphinx.log')
bld.logger = Logs.make_logger(log_file, out)
hdlr = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
bld.logger.addHandler(hdlr)
bld(features='sphinx',
config=bld.path.find_node(bld.env.sphinx_conf_path),
outdir='documentation',
version=bld.env.version,
)
def clean_all(bld):
"""cleans all parts of the project"""
from waflib import Options
commands_after = Options.commands
Options.commands = ['clean_primary', 'clean_secondary',
'clean_primary_bare', 'clean_secondary_bare',
'clean_libs']
Options.commands += commands_after
def build_all(bld):
"""builds all parts of the project (binaries and documentation)"""
from waflib import Options
commands_after = Options.commands
Options.commands = []
if bld.options.libs:
Options.commands = ['build_libs']
Options.commands += ['build_primary', 'build_secondary',
'build_primary_bare', 'build_secondary_bare',
'doxygen_primary', 'doxygen_secondary',
'doxygen_primary_bare', 'doxygen_secondary_bare',
'sphinx']
Options.commands += commands_after
def dist(conf):
conf.base_name = 'foxbms'
conf.algo = 'tar.gz'
conf.excl = out
conf.excl += ' .ws **/tools/waf*.*.**-* .lock-*'
conf.excl += ' **/.git **/.gitignore **/.gitattributes '
conf.excl += ' **/*.tar.bz2 **/*.tar.gz **/*.pyc '
def distcheck_cmd(self):
import shlex
cfg = []
if Options.options.distcheck_args:
cfg = shlex.split(Options.options.distcheck_args)
else:
cfg = [x for x in sys.argv if x.startswith('-')]
cmd = [sys.executable, sys.argv[0], 'configure', 'build_primary', 'build_secondary', 'doxygen_primary', 'doxygen_secondary', 'sphinx'] + cfg
return cmd
def check_cmd(self):
import tarfile
with tarfile.open(self.get_arch_name())as t:
for x in t:
t.extract(x)
cmd = self.make_distcheck_cmd()
ret = Utils.subprocess.Popen(cmd, cwd=self.get_base_name()).wait()
if ret:
raise Errors.WafError('distcheck failed with code % r' % ret)
def distcheck(conf):
"""creates tar.bz form the source directory and tries to run a build"""
from waflib import Scripting
Scripting.DistCheck.make_distcheck_cmd = distcheck_cmd
Scripting.DistCheck.check = check_cmd
conf.base_name = 'foxbms'
conf.excl = out
conf.excl += ' .ws **/tools/waf*.*.**-* .lock-*'
conf.excl += ' **/.git **/.gitignore **/.gitattributes '
conf.excl += ' **/*.tar.bz2 **/*.tar.gz **/*.pyc '
class tsk_cal_chksum(Task.Task):
def keyword(self):
return 'Calculating checksum'
after = ['tsk_binflashheadergen', 'tsk_binflashgen']
color = 'RED'
def run(self):
import binascii
import struct
with open(self.inputs[1].abspath(), 'rb') as pheader_buffer_file:
pheader_buffer_file.seek(self.generator.env.flash_begin_adr ^ self.generator.env.flash_header_adr)
checksum_start_address = struct.unpack('i', pheader_buffer_file.read(4))[0]
pheader_buffer_file.seek(self.generator.env.flash_end_adr ^ self.generator.env.flash_header_adr)
checksum_end_address = struct.unpack('i', pheader_buffer_file.read(4))[0]
chksum_calc_length = checksum_end_address - checksum_start_address + 1
prg_txt = self.inputs[0].read('rb')
if not chksum_calc_length == len(prg_txt):
self.generator.fatal('Checksum calculation error')
checksum = binascii.crc32(prg_txt, 0) & 0xFFFFFFFF
output = f'checksum: 0x{checksum:X}'
self.outputs[0].write(output + '\n')
print(output)
class tsk_wrt_chksum(Task.Task):
def keyword(self):
return 'Writing checksum'
after = ['tsk_cal_chksum']
color = 'RED'
def run(self):
import shutil
import struct
checksum_struct_name = 'ver_sw_validation'
checksum_position_in_struct = 0x10
date_position_in_struct = 0xA0
time_position_in_struct = 0xAC
checksum_data = yaml.load(self.inputs[1].read(), Loader=yaml.Loader)
checksum = hex(checksum_data['checksum'])
if checksum.endswith('L'):
checksum = checksum[:-1]
cmd = Utils.subst_vars('${OBJDUMP} --section=.flashheader -h', self.env)
cmd += f' {self.inputs[0].abspath()}'
std_out, std_err = self.generator.bld.cmd_and_log(cmd, output=waflib.Context.BOTH)
sectionAttributes = dict(zip(std_out.splitlines()[4].split(), std_out.splitlines()[5].split()))
cmd = Utils.subst_vars('${OBJDUMP} --section=.flashheader -t', self.env)
cmd += f' {self.inputs[0].abspath()}'
std_out, std_err = self.generator.bld.cmd_and_log(cmd, output=waflib.Context.BOTH)
std_out = [line for line in std_out.splitlines() if checksum_struct_name in line]
symbolAttributes = std_out[0].split()
positionInSection = int(symbolAttributes[0], 16) - int(sectionAttributes['LMA'], 16)
shutil.copy(self.inputs[0].abspath(), self.outputs[0].abspath())
# calculate offset of checksum in ELF file
offset = int(sectionAttributes['File'], 16) + positionInSection + checksum_position_in_struct
# create single bytes from checksum string
bytes = struct.pack('I', int(checksum, 16))
# write checksum bytes to calculated offset in ELF file
with open(self.outputs[0].abspath(), 'r+b') as fh:
fh.seek(offset)
fh.write(bytes)
# calculate offset of date in ELF file
offset = int(sectionAttributes['File'], 16) + positionInSection + date_position_in_struct
# create single bytes from date string
d = datetime.datetime.now()
bytes = d.strftime('%b %d %Y').encode()
# write date bytes to calculated offset in ELF file
with open(self.outputs[0].abspath(), 'r+b') as fh:
fh.seek(offset)
fh.write(bytes)
# calculate offset of time in ELF file
offset = int(sectionAttributes['File'], 16) + positionInSection + time_position_in_struct
# create single bytes from time string
bytes = d.strftime('%H:%M:%S').encode()
# write time bytes to calculated offset in ELF file
with open(self.outputs[0].abspath(), 'r+b') as fh:
fh.seek(offset)
fh.write(bytes)
@TaskGen.feature('chksum')
@TaskGen.before('add_hexgen_task')
@TaskGen.after('apply_link', 'add_bingen_task')
def add_chksum_task(self):
try:
link_task = self.link_task
binflashgen = self.binflashgen_task
binflashheadergen = self.binflashheadergen_task
except AttributeError:
return
self.cal_chksum_task = self.create_task('tsk_cal_chksum',
src=[binflashgen.outputs[0],
binflashheadergen.outputs[0]],
tgt=self.path.get_bld().make_node('checksum.yml'))
self.wrt_chksum_task = self.create_task('tsk_wrt_chksum',
src=[link_task.outputs[0], self.cal_chksum_task.outputs[0]],
tgt=link_task.outputs[0].change_ext(''))
class strip(Task.Task):
after = ['tsk_binflashheaderpatch']
run_str = '${STRIP} ${SRC} -o ${TGT}'
color = 'BLUE'
@TaskGen.feature('strip')
@TaskGen.after('add_chksum_task')
@TaskGen.after('add_bingen_task')
def add_strip_task(self):
try:
link_task = self.link_task
except AttributeError:
return
self.create_task('strip',
src=link_task.outputs[0],
tgt=link_task.outputs[0].change_ext('_nd.elf'))
class tsk_hexgen(Task.Task):
def keyword(self):
return 'Creating hex file'
after = ['tsk_wrt_chksum']
run_str = '${OBJCOPY} -R .ext_sdramsect_bss -R .bkp_ramsect -O ihex ${SRC} ${TGT}'
color = 'CYAN'
@TaskGen.feature('hexgen')
@TaskGen.after('add_chksum_task')
def add_hexgen_task(self):
try:
wrt_chksum_task = self.wrt_chksum_task
except AttributeError:
return
self.hexgen = self.create_task('tsk_hexgen',
src=wrt_chksum_task.outputs[0],
tgt=wrt_chksum_task.outputs[0].change_ext('.hex'))
class tsk_binflashheaderpatch(Task.Task):
def keyword(self):
return 'Patching bin flashheader'
after = ['tsk_wrt_chksum']
run_str = '${OBJCOPY} -j .flashheader -O binary ${SRC} ${TGT}'
color = 'RED'
class tsk_binflashheadergen(Task.Task):
def keyword(self):
return 'Creating bin flashheader'
run_str = '${OBJCOPY} -j .flashheader -O binary ${SRC} ${TGT}'
color = 'RED'
class tsk_binflashgen(Task.Task):
def keyword(self):
return 'Creating bin flash'
run_str = '${OBJCOPY} -R .ext_sdramsect_bss -R .bkp_ramsect -R .flashheader -O binary ${SRC} ${TGT}'
color = 'RED'
@TaskGen.feature('bingen')
@TaskGen.before('add_chksum_task')
@TaskGen.after('apply_link')
def add_bingen_task(self):
try:
link_task = self.link_task
except AttributeError:
return
self.binflashgen_task = self.create_task('tsk_binflashgen', src=link_task.outputs[0], tgt=link_task.outputs[0].change_ext('_flash.bin', '.elf.unpatched'))
self.binflashheadergen_task = self.create_task('tsk_binflashheadergen', src=link_task.outputs[0], tgt=link_task.outputs[0].change_ext('_flashheader.bin.unpatched', '.elf.unpatched'))
@TaskGen.feature('binpatch')
@TaskGen.after('add_chksum_task')
@TaskGen.after('apply_link')
def add_patch_bin_task(self):
try:
wrt_chksum_task = self.wrt_chksum_task
except AttributeError:
return
self.binflashheaderpatch_task = self.create_task('tsk_binflashheaderpatch', src=wrt_chksum_task.outputs[0], tgt=wrt_chksum_task.outputs[0].change_ext('_flashheader.bin'))
import waflib.Tools.asm # noqa: E402 import before redefining
from waflib.TaskGen import extension # noqa: E402
class Sasm(Task.Task):
color = 'BLUE'
run_str = '${CC} ${ASMFLAGS} ${CPPPATH_ST:INCPATHS} -o ${TGT} ${SRC[0].abspath()}'
@extension('.s')
def asm_hook(self, node):
name = 'Sasm'
out = node.change_ext('.o')
task = self.create_task(name, node, out)
try:
self.compiled_tasks.append(task)
except AttributeError:
self.compiled_tasks = [task]
return task
class size(Task.Task):
def keyword(self):
return 'Calculating size'
before = ['tsk_cal_chksum']
color = 'BLUE'
def run(self):
cmd = Utils.subst_vars('${SIZE}', self.env) + f' {self.inputs[0].abspath()}'
x = self.outputs[0].path_from(self.generator.path)
out, err = self.generator.bld.cmd_and_log(cmd, output=waflib.Context.BOTH, quiet=waflib.Context.STDOUT)
self.generator.path.make_node(x).write(out)
if err:
Logs.error(err)
@TaskGen.feature('size')
@TaskGen.after('apply_link')
def process_sizes(self):
if getattr(self, 'link_task', None) is None:
return
objects_to_size = []
objects_to_size.extend(self.link_task.inputs)
objects_to_size.extend(self.link_task.outputs)
for node in objects_to_size:
out = node.change_ext('.size.log')
self.create_task('size', node, out)
class tsk_check_includes(Task.Task):
before = ['size']
color = 'PINK'
def run(self):
import os
import collections
err_msg = f'{self.inputs[0].abspath()} introduces the following errors:\n'
err_ctn_missing = 0
err_ctn_duplicates = 0
incs = self.generator.bld.env.INCLUDES + [x if os.path.isabs(x) else os.path.join(self.generator.path.abspath(), x) for x in self.generator.includes]
Logs.debug('\n'.join(incs))
for x in incs:
if not os.path.isdir(x):
err_ctn_missing += 1
if err_ctn_missing == 1:
err_msg += 'The following include directories do not exist:\n'