-
Notifications
You must be signed in to change notification settings - Fork 18
/
cue.py
1498 lines (1255 loc) · 53.5 KB
/
cue.py
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
"""EPICS CI build script for Linux/MacOS/Windows on Travis/GitLab/AppVeyor/GitHub-Actions
"""
from __future__ import print_function
import sys, os, stat, shlex, shutil
import fileinput
import logging
import re
import time
import threading
from glob import glob
import subprocess as sp
import sysconfig
import shutil
logger = logging.getLogger(__name__)
# Keep track of all files we write/append for later logging
_realopen = open
_modified_files = set()
def open(fname, mode='r'):
F = _realopen(fname, mode)
if 'w' in mode or 'a' in mode:
_modified_files.add(os.path.normpath(os.path.abspath(fname)))
return F
def log_modified():
for fname in _modified_files:
with Folded(os.path.basename(fname), 'Contents of '+fname):
with open(fname, 'r') as F:
sys.stdout.write(F.read())
sys.stdout.write(os.linesep)
def whereis(cmd):
if hasattr(shutil, 'which'): # >= py3.3
loc = shutil.which(cmd)
print('{0}Found exec {1} at {2!r} {3}'.format(ANSI_CYAN, cmd, loc, ANSI_RESET))
def prepare_env():
'''HACK
github actions yaml configuration doesn't allow
conditional (un)setting of environments, only values.
Currently this script treats unset and empty environment
variables differently.
While this is the case, we unset any empty environment variables.
'''
toclear = tuple(k for k,v in os.environ.items() if len(v.strip())==0)
for var in toclear:
print('{0}Clearing empty environment variable {1}{2}'.format(ANSI_CYAN, var, ANSI_RESET))
del os.environ[var]
# Detect the service and set up context hash accordingly
def detect_context():
global homedir
buildconfig = 'default'
ci['cachedir'] = os.path.join(homedir, '.cache')
if 'TRAVIS' in os.environ:
ci['service'] = 'travis'
ci['os'] = os.environ['TRAVIS_OS_NAME']
ci['platform'] = 'x64'
ci['compiler'] = os.environ['TRAVIS_COMPILER']
ci['choco'] += ['strawberryperl']
if re.match(r'^vs', ci['compiler']):
# Only Visual Studio 2017 available
ci['compiler'] = 'vs2017'
if 'BCFG' in os.environ:
buildconfig = os.environ['BCFG'].lower()
if 'GITLAB_CI' in os.environ:
ci['service'] = 'gitlab'
ci['os'] = 'linux'
ci['platform'] = 'x64'
ci['sudo'] = [] # No sudo in GitLab Docker containers
ci['cachedir'] = os.path.join(curdir, '.cache') # No caches outside project directory
if 'CMP' in os.environ:
ci['compiler'] = os.environ['CMP']
if 'BCFG' in os.environ:
buildconfig = os.environ['BCFG'].lower()
if 'APPVEYOR' in os.environ:
ci['service'] = 'appveyor'
if re.match(r'^Visual', os.environ['APPVEYOR_BUILD_WORKER_IMAGE']):
ci['os'] = 'windows'
elif re.match(r'^Ubuntu', os.environ['APPVEYOR_BUILD_WORKER_IMAGE']):
ci['os'] = 'linux'
elif re.match(r'^macOS', os.environ['APPVEYOR_BUILD_WORKER_IMAGE']):
ci['os'] = 'osx'
ci['platform'] = os.environ['PLATFORM'].lower()
if 'CMP' in os.environ:
ci['compiler'] = os.environ['CMP']
buildconfig = os.environ['CONFIGURATION'].lower()
if 'GITHUB_ACTIONS' in os.environ:
ci['service'] = 'github-actions'
if os.environ['RUNNER_OS'] == 'macOS':
ci['os'] = 'osx'
else:
ci['os'] = os.environ['RUNNER_OS'].lower()
ci['platform'] = 'x64'
if 'CMP' in os.environ:
ci['compiler'] = os.environ['CMP']
ci['choco'] += ['strawberryperl']
if 'BCFG' in os.environ:
buildconfig = os.environ['BCFG'].lower()
if re.search('static', buildconfig):
ci['static'] = True
if re.search('debug', buildconfig):
ci['debug'] = True
if 'STATIC' in os.environ:
print("{0}WARNING: Variable 'STATIC' not supported anymore; use 'BCFG' instead{1}"
.format(ANSI_RED, ANSI_RESET))
sys.stdout.flush()
if not re.match(r'^((default|static|shared|dynamic|optimized|debug)-?)+$', buildconfig):
print("{0}WARNING: Unrecognized build configuration setting '{1}'{2}"
.format(ANSI_RED, buildconfig, ANSI_RESET))
sys.stdout.flush()
if ci['static']:
ci['configuration'] = 'static'
else:
ci['configuration'] = 'shared'
if ci['debug']:
ci['configuration'] += '-debug'
else:
ci['configuration'] += '-optimized'
ci['scriptsdir'] = os.path.abspath(os.path.dirname(sys.argv[0]))
if 'CACHEDIR' in os.environ:
ci['cachedir'] = os.environ['CACHEDIR']
if 'CHOCO' in os.environ:
if os.environ['CHOCO'] == 'NO':
ci['choco'] = []
else:
ci['choco'].extend(os.environ['CHOCO'].split())
if 'APT' in os.environ:
ci['apt'].extend(os.environ['APT'].split())
if 'BREW' in os.environ:
ci['homebrew'].extend(os.environ['BREW'].split())
ci['test'] = True
if 'TEST' in os.environ and os.environ['TEST'].lower() == 'no':
ci['test'] = False
ci['parallel_make'] = 2
if 'PARALLEL_MAKE' in os.environ:
ci['parallel_make'] = int(os.environ['PARALLEL_MAKE'])
ci['clean_deps'] = True
if 'CLEAN_DEPS' in os.environ and os.environ['CLEAN_DEPS'].lower() == 'no':
ci['clean_deps'] = False
logger.debug('Detected a build hosted on %s, using %s on %s (%s) configured as %s '
+ '(test: %s, clean_deps: %s)',
ci['service'], ci['compiler'], ci['os'], ci['platform'], ci['configuration'],
ci['test'], ci['clean_deps'])
curdir = os.getcwd()
ci = {}
seen_setups = []
modules_to_compile = []
setup = {}
places = {}
extra_makeargs = []
make_timeout = 0.
is_base314 = False
is_make3 = False
has_test_results = False
silent_dep_builds = True
skip_dep_builds = False
do_recompile = False
installed_7z = False
def clear_lists():
global is_base314, has_test_results, silent_dep_builds, is_make3
global _modified_files, do_recompile, building_base
del seen_setups[:]
del modules_to_compile[:]
del extra_makeargs[:]
setup.clear()
places.clear()
is_base314 = False
is_make3 = False
has_test_results = False
silent_dep_builds = True
do_recompile = False
building_base = False
_modified_files = set()
ci['service'] = '<none>'
ci['os'] = '<unknown>'
ci['platform'] = '<unknown>'
ci['compiler'] = '<unknown>'
ci['static'] = False
ci['debug'] = False
ci['configuration'] = '<unknown>'
ci['scriptsdir'] = ''
ci['cachedir'] = ''
ci['choco'] = ['make']
ci['apt'] = []
ci['homebrew'] = []
ci['sudo'] = ['sudo']
clear_lists()
if 'BASE' in os.environ and os.environ['BASE'] == 'SELF':
building_base = True
skip_dep_builds = True
places['EPICS_BASE'] = curdir
# Setup ANSI Colors
ANSI_RED = "\033[31;1m"
ANSI_GREEN = "\033[32;1m"
ANSI_YELLOW = "\033[33;1m"
ANSI_BLUE = "\033[34;1m"
ANSI_MAGENTA = "\033[35;1m"
ANSI_CYAN = "\033[36;1m"
ANSI_RESET = "\033[0m"
ANSI_CLEAR = "\033[0K"
# Travis log fold control
# from https://github.com/travis-ci/travis-rubies/blob/build/build.sh
# GitHub Actions fold control
# from https://github.com/actions/toolkit/blob/master/docs/commands.md#group-and-ungroup-log-lines
def fold_start(tag, title):
if ci['service'] == 'travis':
print('travis_fold:start:{0}{1}{2}{3}'
.format(tag, ANSI_YELLOW, title, ANSI_RESET))
elif ci['service'] == 'github-actions':
print('::group::{0}{1}{2}'
.format(ANSI_YELLOW, title, ANSI_RESET))
elif ci['service'] == 'appveyor':
print('{0}===== \\/ \\/ \\/ ===== START: {1} ====={2}'
.format(ANSI_YELLOW, title, ANSI_RESET))
sys.stdout.flush()
def fold_end(tag, title):
if ci['service'] == 'travis':
print('\ntravis_fold:end:{0}\r'
.format(tag), end='')
elif ci['service'] == 'github-actions':
print('::endgroup::'
.format(ANSI_YELLOW, title, ANSI_RESET))
elif ci['service'] == 'appveyor':
print('{0}----- /\\ /\\ /\\ ----- END: {1} -----{2}'
.format(ANSI_YELLOW, title, ANSI_RESET))
sys.stdout.flush()
class Folded(object):
def __init__(self, tag, title):
self.tag, self.title = tag, title
def __enter__(self):
fold_start(self.tag, self.title)
def __exit__(self,A,B,C):
fold_end(self.tag, self.title)
homedir = curdir
if 'HomeDrive' in os.environ:
homedir = os.path.join(os.getenv('HomeDrive'), os.getenv('HomePath'))
elif 'HOME' in os.environ:
homedir = os.getenv('HOME')
toolsdir = os.path.join(homedir, '.tools')
vcvars_table = {
# https://en.wikipedia.org/wiki/Microsoft_Visual_Studio#History
'vs2022': [r'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat',
r'C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat'],
'vs2019': [r'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat',
r'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat'],
'vs2017': [r'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat',
r'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat',
r'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat'],
'vs2015': [r'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat'],
'vs2013': [r'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat'],
'vs2012': [r'C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat'],
'vs2010': [r'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat'],
'vs2008': [r'C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat'],
}
vcvars_found = {}
for key in vcvars_table:
for dir in vcvars_table[key]:
if os.path.exists(dir):
vcvars_found[key] = dir
def modlist():
if building_base:
ret = []
else:
for var in ['ADD_MODULES', 'MODULES']:
setup.setdefault(var, '')
if var in os.environ:
setup[var] = os.environ[var]
logger.debug('ENV assignment: %s = %s', var, setup[var])
ret = ['BASE'] + setup['ADD_MODULES'].upper().split() + setup['MODULES'].upper().split()
return ret
def host_info():
print('{0}Build using {1} compiler on {2} ({3}) hosted by {4}{5}'
.format(ANSI_CYAN, ci['compiler'], ci['os'], ci['platform'], ci['service'], ANSI_RESET))
print('{0}Python setup{1}'.format(ANSI_CYAN, ANSI_RESET))
print(sys.version)
print('PYTHONPATH')
for dname in sys.path:
print(' ', dname)
print('platform =', sysconfig.get_platform())
if ci['os'] == 'windows':
print('{0}Available Visual Studio versions{1}'.format(ANSI_CYAN, ANSI_RESET))
for comp in vcvars_found:
print(comp, 'in', vcvars_found[comp])
sys.stdout.flush()
# Error-handler to make shutil.rmtree delete read-only files on Windows
def remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)
# source_set(setup)
#
# Source a settings file (extension .set) found in the setup_dirs path
# May be called recursively (from within a setup file)
def source_set(name):
# allowed separators: colon or whitespace
setup_dirs = os.getenv('SETUP_PATH', "").replace(':', ' ').split()
if len(setup_dirs) == 0:
raise NameError("{0}Search path for setup files (SETUP_PATH) is empty{1}".format(ANSI_RED, ANSI_RESET))
for set_dir in setup_dirs:
set_file = os.path.join(set_dir, name) + ".set"
if set_file in seen_setups:
print("Ignoring already included setup file {0}".format(set_file))
return
if os.path.isfile(set_file):
seen_setups.append(set_file)
print("Opening setup file {0}".format(set_file))
sys.stdout.flush()
with open(set_file) as fp:
for line in fp:
if not line.strip() or line.strip()[0] == '#':
continue
if line.startswith("include"):
logger.debug('%s: Found include directive, reading %s next',
set_file, line.split()[1])
source_set(line.split()[1])
continue
assign = line.replace('"', '').strip().split("=", 1)
setup.setdefault(assign[0], os.getenv(assign[0], ""))
if not setup[assign[0]].strip():
logger.debug('%s: setup[%s] = %s', set_file, assign[0], assign[1])
setup[assign[0]] = assign[1]
logger.debug('Done with setup file %s', set_file)
break
else:
raise NameError("{0}Setup file {1}.set does not exist in SETUP_PATH search path ({2}){3}"
.format(ANSI_RED, name, setup_dirs, ANSI_RESET))
# update_release_local(var, location)
# var name of the variable to set in RELEASE.local
# location location (absolute path) of where variable should point to
#
# Manipulate RELEASE.local in the cache location:
# - replace "$var=$location" line if it exists and has changed
# - otherwise add "$var=$location" line and possibly move EPICS_BASE=... line to the end
# Set places[var] = location
def update_release_local(var, location):
release_local = os.path.join(ci['cachedir'], 'RELEASE.local')
updated_line = '{0}={1}'.format(var, location.replace('\\', '/'))
places[var] = location
if not os.path.exists(release_local):
logger.debug('RELEASE.local does not exist, creating it')
try:
os.makedirs(ci['cachedir'])
except:
pass
touch = open(release_local, 'w')
touch.close()
base_line = ''
found = False
logger.debug("Opening RELEASE.local for adding '%s'", updated_line)
for line in fileinput.input(release_local, inplace=1):
output_line = line.strip()
if 'EPICS_BASE=' in line:
base_line = line.strip()
logger.debug("Found EPICS_BASE line '%s', not writing it", base_line)
continue
elif '{0}='.format(var) in line:
logger.debug("Found '%s=' line, replacing", var)
found = True
output_line = updated_line
logger.debug("Writing line to RELEASE.local: '%s'", output_line)
print(output_line)
fileinput.close()
release_local = open(release_local, "a")
if not found:
logger.debug("Adding new definition: '%s'", updated_line)
print(updated_line, file=release_local)
if base_line:
logger.debug("Writing EPICS_BASE line: '%s'", base_line)
print(base_line, file=release_local)
release_local.close()
def set_setup_from_env(dep):
for postf in ['', '_DIRNAME', '_REPONAME', '_REPOOWNER', '_REPOURL',
'_VARNAME', '_RECURSIVE', '_DEPTH', '_HOOK']:
if dep + postf in os.environ:
setup[dep + postf] = os.environ[dep + postf]
logger.debug('ENV assignment: %s = %s', dep + postf, setup[dep + postf])
def call_git(args, **kws):
if 'cwd' in kws:
place = kws['cwd']
else:
place = os.getcwd()
logger.debug("EXEC '%s' in %s", ' '.join(['git'] + args), place)
sys.stdout.flush()
exitcode = sp.call(['git'] + args, **kws)
logger.debug('EXEC DONE')
return exitcode
def call_make(args=None, **kws):
global make_timeout
if args is None:
args = []
place = kws.get('cwd', os.getcwd())
parallel = kws.pop('parallel', ci['parallel_make'])
silent = kws.pop('silent', False)
use_extra = kws.pop('use_extra', False)
# no parallel make for Base 3.14
if parallel <= 0 or is_base314:
makeargs = []
else:
makeargs = ['-j{0}'.format(parallel)]
if not is_make3:
makeargs += ['-Otarget']
if silent:
makeargs += ['-s']
if use_extra:
makeargs += extra_makeargs
logger.debug("EXEC '%s' in %s", ' '.join(['make'] + makeargs + args), place)
sys.stdout.flush()
sys.stderr.flush()
child = sp.Popen(['make'] + makeargs + args, **kws)
if make_timeout:
def expire(child):
logger.error('Timeout')
child.terminate()
timer = threading.Timer(make_timeout, expire, args=(child,))
timer.start()
exitcode = child.wait()
if make_timeout:
timer.cancel()
logger.debug('EXEC DONE')
if exitcode != 0:
sys.exit(exitcode)
def apply_patch(file, **kws):
place = kws.get('cwd', os.getcwd())
print('Applying patch {0} in {1}'.format(file, place))
logger.debug("EXEC '%s' in %s", ' '.join(['patch', '-p1', '-i', file]), place)
sys.stdout.flush()
sp.check_call(['patch', '-p1', '-i', file], cwd=place)
logger.debug('EXEC DONE')
def extract_archive(file, **kws):
place = kws.get('cwd', os.getcwd())
print('Extracting archive {0} in {1}'.format(file, place))
logger.debug("EXEC '%s' in %s", ' '.join(['7z', 'x', '-aoa', '-bd', file]), place)
sys.stdout.flush()
sp.check_call(['7z', 'x', '-aoa', '-bd', file], cwd=place)
logger.debug('EXEC DONE')
def get_git_hash(place):
logger.debug("EXEC 'git log -n1 --pretty=format:%%H' in %s", place)
sys.stdout.flush()
head = sp.check_output(['git', 'log', '-n1', '--pretty=format:%H'], cwd=place).decode()
logger.debug('EXEC DONE')
return head
def complete_setup(dep):
set_setup_from_env(dep)
setup.setdefault(dep, 'master')
setup.setdefault(dep + "_DIRNAME", dep.lower())
setup.setdefault(dep + "_REPONAME", dep.lower())
setup.setdefault('REPOOWNER', 'epics-modules')
setup.setdefault(dep + "_REPOOWNER", setup['REPOOWNER'])
setup.setdefault(dep + "_REPOURL", 'https://github.com/{0}/{1}.git'
.format(setup[dep + '_REPOOWNER'], setup[dep + '_REPONAME']))
setup.setdefault(dep + "_VARNAME", dep)
setup.setdefault(dep + "_RECURSIVE", 'YES')
setup.setdefault(dep + "_DEPTH", -1)
# add_dependency(dep, tag)
#
# Add a dependency to the cache area:
# - check out (recursive if configured) in the CACHE area unless it already exists and the
# required commit has been built
# - Defaults:
# $dep_DIRNAME = lower case ($dep)
# $dep_REPONAME = lower case ($dep)
# $dep_REPOURL = GitHub / $dep_REPOOWNER (or $REPOOWNER or epics-modules) / $dep_REPONAME .git
# $dep_VARNAME = $dep
# $dep_DEPTH = 5
# $dep_RECURSIVE = 1/YES (0/NO to for a flat clone)
# - Add $dep_VARNAME line to the RELEASE.local file in the cache area (unless already there)
# - Add full path to $modules_to_compile
def add_dependency(dep):
global do_recompile
recurse = setup[dep + '_RECURSIVE'].lower()
if recurse not in ['0', 'no']:
recursearg = ["--recursive"]
elif recurse not in ['1', 'yes']:
recursearg = []
else:
raise RuntimeError("Invalid value for {}_RECURSIVE='{}' not 0/NO/1/YES".format(dep, recurse))
deptharg = {
'-1': ['--depth', '5'],
'0': [],
}.get(str(setup[dep + '_DEPTH']), ['--depth', str(setup[dep + '_DEPTH'])])
tag = setup[dep]
logger.debug('Adding dependency %s with tag %s', dep, setup[dep])
# determine if dep points to a valid release or branch
if call_git(['ls-remote', '--quiet', '--exit-code', '--refs', setup[dep + '_REPOURL'], tag]):
raise RuntimeError("{0}{1} is neither a tag nor a branch name for {2} ({3}){4}"
.format(ANSI_RED, tag, dep, setup[dep + '_REPOURL'], ANSI_RESET))
dirname = setup[dep + '_DIRNAME'] + '-{0}'.format(tag)
place = os.path.join(ci['cachedir'], dirname)
checked_file = os.path.join(place, "checked_out")
if os.path.isdir(place):
logger.debug('Dependency %s: directory %s exists, comparing checked-out commit', dep, place)
# check HEAD commit against the hash in marker file
if os.path.exists(checked_file):
with open(checked_file, 'r') as bfile:
checked_out = bfile.read().strip()
bfile.close()
else:
checked_out = 'never'
head = get_git_hash(place)
logger.debug('Found checked_out commit %s, git head is %s', checked_out, head)
if head != checked_out:
logger.debug('Dependency %s out of date - removing', dep)
shutil.rmtree(place, onerror=remove_readonly)
else:
print('Found {0} of dependency {1} up-to-date in {2}'.format(tag, dep, place))
sys.stdout.flush()
if not os.path.isdir(place):
if not os.path.isdir(ci['cachedir']):
os.makedirs(ci['cachedir'])
# clone dependency
print('Cloning {0} of dependency {1} into {2}'
.format(tag, dep, place))
sys.stdout.flush()
call_git(['clone', '--quiet'] + deptharg + recursearg + ['--branch', tag, setup[dep + '_REPOURL'], dirname],
cwd=ci['cachedir'])
sp.check_call(['git', 'log', '-n1'], cwd=place)
logger.debug('Setting do_recompile = True (all following modules will be recompiled')
do_recompile = True
if dep == 'BASE':
# add MSI 1.7 to Base 3.14
versionfile = os.path.join(place, 'configure', 'CONFIG_BASE_VERSION')
if os.path.exists(versionfile):
with open(versionfile) as f:
if 'BASE_3_14=YES' in f.read():
print('Adding MSI 1.7 to {0}'.format(place))
sys.stdout.flush()
sp.check_call(['patch', '-p1', '-i', os.path.join(ci['scriptsdir'], 'add-msi-to-314.patch')],
cwd=place)
else:
# force including RELEASE.local for non-base modules by overwriting their configure/RELEASE
release = os.path.join(place, "configure", "RELEASE")
if os.path.exists(release):
with open(release, 'w') as fout:
print('-include $(TOP)/../RELEASE.local', file=fout)
# Apply HOOK
if dep + '_HOOK' in setup:
hook = setup[dep + '_HOOK']
hook_file = os.path.join(curdir, hook)
hook_ext = os.path.splitext(hook_file)[1]
if os.path.exists(hook_file):
if hook_ext == '.patch':
apply_patch(hook_file, cwd=place)
elif hook_ext in ('.zip', '.7z'):
extract_archive(hook_file, cwd=place)
elif hook_ext == '.py':
print('Running py hook {0} in {1}'.format(hook, place))
sp.check_call([sys.executable, hook_file], cwd=place)
else:
print('Running hook {0} in {1}'.format(hook, place))
sys.stdout.flush()
sp.check_call(hook_file, shell=True, cwd=place)
else:
print('Skipping invalid hook {0} in {1}'.format(hook, place))
# write checked out commit hash to marker file
head = get_git_hash(place)
logger.debug('Writing hash of checked-out dependency (%s) to marker file', head)
with open(checked_file, "w") as fout:
print(head, file=fout)
fout.close()
if do_recompile:
modules_to_compile.append(dep)
update_release_local(setup[dep + "_VARNAME"], place)
def detect_epics_host_arch():
if ci['os'] == 'windows':
if re.match(r'^vs', ci['compiler']):
# there is no combined static and debug EPICS_HOST_ARCH target,
# so a combined debug and static target will appear to be just static
# but debug will have been specified in CONFIG_SITE by prepare()
hostarchsuffix = ''
if ci['debug']:
hostarchsuffix = '-debug'
if ci['static']:
hostarchsuffix = '-static'
if ci['platform'] == 'x86':
os.environ['EPICS_HOST_ARCH'] = 'win32-x86' + hostarchsuffix
elif ci['platform'] == 'x64':
os.environ['EPICS_HOST_ARCH'] = 'windows-x64' + hostarchsuffix
elif ci['compiler'] == 'gcc':
if ci['platform'] == 'x86':
os.environ['EPICS_HOST_ARCH'] = 'win32-x86-mingw'
elif ci['platform'] == 'x64':
os.environ['EPICS_HOST_ARCH'] = 'windows-x64-mingw'
if 'EPICS_HOST_ARCH' not in os.environ:
logger.debug('Running script to detect EPICS host architecture in %s', places['EPICS_BASE'])
os.environ['EPICS_HOST_ARCH'] = 'unknown'
eha_scripts = [
os.path.join(places['EPICS_BASE'], 'src', 'tools', 'EpicsHostArch.pl'),
os.path.join(places['EPICS_BASE'], 'startup', 'EpicsHostArch.pl'),
]
for eha in eha_scripts:
if os.path.exists(eha):
os.environ['EPICS_HOST_ARCH'] = sp.check_output(['perl', eha]).decode('ascii').strip()
logger.debug('%s returned: %s',
eha, os.environ['EPICS_HOST_ARCH'])
break
def setup_for_build(args):
global is_base314, has_test_results, is_make3
dllpaths = []
logger.debug('Setting up the build environment')
if ci['os'] == 'windows':
if os.path.exists(r'C:\Strawberry\perl\bin'):
# Put strawberry perl in front of the PATH (so that Git Perl is further behind)
# Put Chocolatey\bin ahead to select correct make.exe
logger.debug('Adding Strawberry Perl in front of the PATH')
os.environ['PATH'] = os.pathsep.join([r'C:\ProgramData\Chocolatey\bin',
r'C:\Strawberry\c\bin',
r'C:\Strawberry\perl\site\bin',
r'C:\Strawberry\perl\bin',
os.environ['PATH']])
if ci['service'] == 'appveyor' and ci['compiler'] == 'gcc':
logger.debug('Adding AppVeyor MSYS2/MinGW installation to PATH and INCLUDE')
if 'INCLUDE' not in os.environ:
os.environ['INCLUDE'] = ''
if ci['platform'] == 'x86':
os.environ['INCLUDE'] = os.pathsep.join(
[r'C:\msys64\mingw32\include',
os.environ['INCLUDE']])
os.environ['PATH'] = os.pathsep.join([r'C:\msys64\mingw32\bin',
os.environ['PATH']])
elif ci['platform'] == 'x64':
os.environ['INCLUDE'] = os.pathsep.join(
[r'C:\msys64\mingw64\include',
os.environ['INCLUDE']])
os.environ['PATH'] = os.pathsep.join([r'C:\msys64\mingw64\bin',
os.environ['PATH']])
# Find BASE location
if not building_base:
with open(os.path.join(ci['cachedir'], 'RELEASE.local'), 'r') as f:
lines = f.readlines()
for line in lines:
(mod, place) = line.strip().split('=')
if mod == 'EPICS_BASE':
places['EPICS_BASE'] = place
else:
places['EPICS_BASE'] = '.'
logger.debug('Using EPICS Base at %s', places['EPICS_BASE'])
detect_epics_host_arch()
if ci['os'] == 'windows':
if not building_base:
with open(os.path.join(ci['cachedir'], 'RELEASE.local'), 'r') as f:
lines = f.readlines()
for line in lines:
(mod, place) = line.strip().split('=')
bin_dir = os.path.join(place, 'bin', os.environ['EPICS_HOST_ARCH'])
if os.path.isdir(bin_dir):
dllpaths.append(bin_dir)
# Add DLL location to PATH
bin_dir = os.path.join(os.getcwd(), 'bin', os.environ['EPICS_HOST_ARCH'])
if os.path.isdir(bin_dir):
dllpaths.append(bin_dir)
os.environ['PATH'] = os.pathsep.join(dllpaths + [os.environ['PATH']])
logger.debug('DLL paths added to PATH: %s', os.pathsep.join(dllpaths))
cfg_base_version = os.path.join(places['EPICS_BASE'], 'configure', 'CONFIG_BASE_VERSION')
if os.path.exists(cfg_base_version):
with open(cfg_base_version) as myfile:
if 'BASE_3_14=YES' in myfile.read():
is_base314 = True
logger.debug('Check if EPICS Base is a 3.14 series: %s', is_base314)
if not is_base314:
rules_build = os.path.join(places['EPICS_BASE'], 'configure', 'RULES_BUILD')
if os.path.exists(rules_build):
with open(rules_build) as myfile:
for line in myfile:
if re.match('^test-results:', line):
has_test_results = True
# apparently %CD% is handled automagically, so use getcwd() instead
os.environ['TOP'] = os.getcwd()
os.environ['MAKE'] = 'make'
os.environ['EPICS_BASE'] = places['EPICS_BASE']
changed_vars = set()
for extra_env_var in args.extra_env_vars:
try:
key_value = extra_env_var.split('=')
key = key_value[0]
value = key_value[1]
expanded_value = value.format(**os.environ)
# Update the environment right now so later variables have access
if key in os.environ:
old_value = [os.environ[key]]
else:
old_value = []
os.environ[key] = os.pathsep.join(old_value + [expanded_value])
changed_vars.add(key)
except KeyError:
print('Environment')
[print(' ', K, '=', repr(V)) for K, V in os.environ.items()]
raise
for key in changed_vars:
print("{0}{2} = {3}{1}".format(ANSI_CYAN, ANSI_RESET, key, os.environ[key]))
# os.environ completely updated at this point
logger.debug('Final PATH')
for loc in os.environ['PATH'].split(os.pathsep):
logger.debug(' %r', loc)
# Check make version
if re.match(r'^GNU Make 3', sp.check_output(['make', '-v']).decode('ascii')):
is_make3 = True
logger.debug('Check if make is a 3.x series: %s', is_make3)
# Add EXTRA make arguments
for tag in ['EXTRA', 'EXTRA1', 'EXTRA2', 'EXTRA3', 'EXTRA4', 'EXTRA5']:
val = os.environ.get(tag, "")
if len(val)>0:
extra_makeargs.extend(shlex.split(val))
def fix_etc_hosts():
# Several travis-ci images throw us a curveball in /etc/hosts
# by including two entries for localhost. The first for 127.0.1.1
# causes epicsSockResolveTest to fail.
# cat /etc/hosts
# ...
# 127.0.1.1 localhost localhost ip4-loopback
# 127.0.0.1 localhost nettuno travis vagrant travis-job-....
logger.debug("EXEC sudo sed -ie '/^127\\.0\\.1\\.1/ s|localhost\\s*||g' /etc/hosts")
sys.stdout.flush()
sp.call(['sudo', 'sed', '-ie', '/^127\\.0\\.1\\.1/ s|localhost\\s*||g', '/etc/hosts'])
logger.debug('EXEC DONE')
def edit_make_file(mode, path, values):
"""Edit an EPICS Make file.
mode should be either "a" or "w", as for the open function.
path should be a list, e.g. ["configure", "CONFIG_SITE"]
values should be a dictionary of values to edit. If the value starts with
a "+" the value will be appended.
Example usage:
edit_make_file("a", ["configure", "CONFIG_SITE"], {
"VARIABLE": "value",
"APPENDED_VARIABLE": "+value",
})
"""
with open(os.path.join(places["EPICS_BASE"], *path), mode) as f:
for variable, value in values.items():
if value.startswith("+"):
op = "+="
value = value[1:]
else:
op = "="
f.write(variable + op + value + "\n")
def handle_old_cross_variables():
if "CI_CROSS_TARGETS" not in os.environ:
os.environ["CI_CROSS_TARGETS"] = ""
if "RTEMS" in os.environ:
if 'RTEMS_TARGET' in os.environ:
rtems_target = os.environ['RTEMS_TARGET']
else:
if os.environ['RTEMS'] == '5':
rtems_target = 'RTEMS-pc686-qemu'
else:
rtems_target = 'RTEMS-pc386'
if os.path.exists(os.path.join(places['EPICS_BASE'], 'configure', 'os',
'CONFIG.Common.RTEMS-pc386-qemu')):
# Base 3.15 doesn't have -qemu target architecture
rtems_target = 'RTEMS-pc386-qemu'
new_cross_target = ":" + rtems_target + "@" + os.environ["RTEMS"]
os.environ["CI_CROSS_TARGETS"] += new_cross_target
print(
"{0}WARNING: deprecated RTEMS environment variable was specified." \
" Please add '{1}' to CI_CROSS_TARGETS instead.{2}".format(
ANSI_RED, new_cross_target, ANSI_RESET
)
)
logger.debug('Replaced deprecated RTEMS target with new entry in CI_CROSS_TARGETS: %s', new_cross_target)
if "WINE" in os.environ:
if os.environ['WINE'] == '32':
new_cross_target = ":win32-x86-mingw"
else:
new_cross_target = ":windows-x64-mingw"
os.environ["CI_CROSS_TARGETS"] += new_cross_target
print(
"{0}WARNING: deprecated WINE environment variable was specified." \
" Please add '{1}' to CI_CROSS_TARGETS instead.{2}".format(
ANSI_RED, new_cross_target, ANSI_RESET
)
)
logger.debug('Replaced deprecated WINE target with new entry in CI_CROSS_TARGETS: %s', new_cross_target)
def prepare_cross_compilation(cross_target_info):
"""Prepare the configuration for a single value of the CI_CROSS_TARGETS
variable.
See the README.md file for more information on this variable."""
cross_target_info = cross_target_info.split("@")
if len(cross_target_info) == 2:
target_param = cross_target_info[1]
else:
target_param = None
target = cross_target_info[0]
if target.startswith("RTEMS-"):
prepare_rtems_cross(target, target_param)
elif target.endswith("-mingw"):
prepare_wine_cross(target)
elif target.startswith("linux-"):
prepare_linux_cross(target, target_param)
else:
raise ValueError(
"Unknown CI_CROSS_TARGETS {0}. "
"Please see the ci-scripts README for available values.".format(target)
)
def prepare_rtems_cross(epics_arch, version):
"""Prepare the configuration for RTEMS cross-compilation for the given
RTEMS version.
If version is None, it defaults to version 5 for RTEMS-pc686-*, 4.10
otherwise."""
if version is None:
if epics_arch.startswith("RTEMS-pc686"):
version = "5"
else:
version = "4.10"
# eg. "RTEMS-pc386" or "RTEMS-pc386-qemu" -> "pc386"
rtems_bsp = re.match("^RTEMS-([^-]*)(?:-qemu)?$", epics_arch).group(1)
print("Cross compiler RTEMS{0} @ {1}".format(version, epics_arch))
if ci["os"] == "linux":
download_rtems(version, rtems_bsp)
edit_make_file(
"a",
["configure", "os", "CONFIG_SITE.Common.RTEMS"],
{
"RTEMS_VERSION": version,
"RTEMS_BASE": "/opt/rtems/" + version,
},
)
edit_make_file(
"a",
["configure", "CONFIG_SITE"],
{"CROSS_COMPILER_TARGET_ARCHS": epics_arch},
)
ci["apt"].extend(
["re2c", "g++-mingw-w64-i686", "g++-mingw-w64-x86-64", "qemu-system-x86"]
)
def download_rtems(version, rtems_bsp):
rsb_release = os.environ.get("RSB_BUILD", "20210306")
tar_name = "{0}-rtems{1}.tar.xz".format(rtems_bsp, version)
print("Downloading RTEMS {0} cross compiler: {1}".format(version, tar_name))
sys.stdout.flush()
sp.check_call(
[
"curl",
"-fsSL",
"--retry",
"3",
"-o",
tar_name,
"https://github.com/mdavidsaver/rsb/releases/download/{0}%2F{1}/{2}".format(
version, rsb_release, tar_name
),
],
cwd=toolsdir,
)
sudo_prefix = []
if ci["service"] == "github-actions":
sudo_prefix = ["sudo"]
sp.check_call(
sudo_prefix + ["tar", "-C", "/", "-xmJ", "-f", os.path.join(toolsdir, tar_name)]
)
os.remove(os.path.join(toolsdir, tar_name))
for rtems_cc in glob("/opt/rtems/*/bin/*-gcc"):
print("{0}{1} --version{2}".format(ANSI_CYAN, rtems_cc, ANSI_RESET))
sys.stdout.flush()
sp.check_call([rtems_cc, "--version"])