-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpybuild.py
1532 lines (1407 loc) · 60.5 KB
/
pybuild.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
import argparse
import datetime
import os
import platform
import site
import sys
import sysconfig
import toml
#import tomllib
from colorama import Fore, Back, Style
from pathlib import Path
from pygit2 import Repository
global ARGS
global CONFIG
MSG_COLOR = r'\033[0;33m' # orange
ERROR_COLOR = r'\033[0;31m' # red
HEAD_COLOR = r'\033[1;34m' # bold blue
COLOR_OFF = r'\033[0m'
def _github_actions():
if 'GITHUB_ACTIONS' in os.environ:
return True
return False
def _github_branch():
return Repository('.').head.shorthand
def _project_dir():
return os.path.dirname(__file__)
def _project_path():
return os.path.abspath(_project_dir())
def _config_path(name: str):
path = os.path.join(_project_dir(), name)
path = os.path.abspath(path)
return path
def _scripts_path():
dir = CONFIG['project']['dir']['scripts']
path = os.path.abspath(dir)
return path
def _main_script_path():
name = 'main.sh'
path = os.path.join(_scripts_path(), name)
return path
def _echo_cmd():
if _enable_backslash_escapes():
return 'echo -e'
return 'echo'
def _echo_msg(msg:str):
return f'{_echo_cmd()} "{MSG_COLOR}:::::: {msg}{COLOR_OFF}"'
def _echo_progress_msg(current: int, total: int, msg: str):
progress = _compiling_progress(current, total)
msg = f"[{progress:>3}%] {msg}"
return _echo_msg(msg)
def _echo_header(msg:str):
msg = f'{HEAD_COLOR}:::::: {msg} ::::::{COLOR_OFF}'
sep = ':' * (len(msg) - len(f'{HEAD_COLOR}') - len(f'{COLOR_OFF}'))
lines = []
lines.append(f'{_echo_cmd()} ""')
lines.append(f'{_echo_cmd()} "{HEAD_COLOR}{sep}{COLOR_OFF}"')
lines.append(f'{_echo_cmd()} "{HEAD_COLOR}{msg}{COLOR_OFF}"')
lines.append(f'{_echo_cmd()} "{HEAD_COLOR}{sep}{COLOR_OFF}"')
return lines
def _print_msg(msg:str):
print(Fore.GREEN + f':::::: {msg}' + Style.RESET_ALL)
def _print_error_msg(msg:str):
print(Fore.RED + f':::::: ERROR: {msg}' + Style.RESET_ALL)
def _processor():
processor = platform.processor()
processor = processor.split()[0] # get the 1st word from string, such as 'Intel64 Family 6 Model 154 Stepping 3, GenuineIntel'
return processor
def _platform():
platform = 'macos' # default
if ARGS.platform:
platform = ARGS.platform.lower()
return platform
def _platform_tag():
tag = sysconfig.get_platform()
if _platform() == 'macos': # replaces, e.g., 'macosx-14-arm64' with 'macosx-14_0-arm64'
tag = tag.split('-') # 'macosx-14-arm64' => ['macosx', '14', 'arm64']
tag[1] = f'{tag[1]}_0' # ['macosx', '14', 'arm64'] => ['macosx', '14_0', 'arm64']
tag = '-'.join(tag) # ['macosx', '14_0', 'arm64'] => 'macosx-14_0-arm64'
tag = tag.replace('-', '_')
tag = tag.replace('.', '_')
return tag
def _platform_tag_github_ci(): # sysconf always returns 'macosx_10_9_universal2' on GH macOS...
tag = sysconfig.get_platform()
if _platform() == 'macos':
version = platform.mac_ver()[0] # e.g., '14.5'
version = version.split('.') # '14.5' => ['14', '5']
version[1] = '0' # ['14', '5'] => ['14', '0']
version = '.'.join(version[:2]) # ['14', '0'] => '14.0'; ['12', '0', '5'] => '12.0'
machine = platform.mac_ver()[2] # e.g., 'arm64'
tag = f'macosx-{version}-{machine}'
tag = tag.replace('-', '_')
tag = tag.replace('.', '_')
return tag
def _python_version_full(): # full version, e.g., '3.11.6'
return platform.python_version()
def _python_version_short(): # short version, e.g., '311'
return sysconfig.get_config_var('py_version_nodot')
def _python_tag(): # tag, e.g., 'py311'
return f'py{_python_version_short()}'
def _python_site_packages():
site_packages = site.getsitepackages()
site_packages = site_packages[0] # get the first location from the list
return site_packages
def _python_lib():
if _platform() == 'macos':
#python_lib = '`python3-config --ldflags --embed`'
python_lib = '`pkg-config --libs python3-embed`'
elif _platform() == 'linux':
python_lib = ''
elif _platform() == 'windows':
lib_file = f'python{_python_version_short()}.lib'
python_lib = os.path.join(_python_site_packages(), 'libs', lib_file)
else:
raise Exception(f'Unsupported platform {_platform()}')
return python_lib
def _ifport_lib():
try:
ifport_lib = CONFIG['build'][_platform()][_compiler_name()]
except KeyError:
ifport_lib = ''
return ifport_lib
def _initial_python_wheel_tags():
return {
'python_tag': 'py3',
'abi_tag': 'none',
'platform_tag': 'any'
}
def _new_python_wheel_tags():
return {
'python_tag': _python_tag(),
'abi_tag': 'none',
'platform_tag': _platform_tag_github_ci()
}
def _initial_wheel_name():
dist_package_name = PYPROJECT['project']['name']
dist_package_version = PYPROJECT['project']['version']
python_tag = _initial_python_wheel_tags()['python_tag']
abi_tag = _initial_python_wheel_tags()['abi_tag']
platform_tag = _initial_python_wheel_tags()['platform_tag']
name = f'{dist_package_name}-{dist_package_version}-{python_tag}-{abi_tag}-{platform_tag}.whl'
return name
def _new_wheel_name():
dist_package_name = PYPROJECT['project']['name']
dist_package_version = PYPROJECT['project']['version']
python_tag = _new_python_wheel_tags()['python_tag']
abi_tag = _new_python_wheel_tags()['abi_tag']
platform_tag = _new_python_wheel_tags()['platform_tag']
name = f'{dist_package_name}-{dist_package_version}-{python_tag}-{abi_tag}-{platform_tag}.whl'
return name
def _fix_file_permissions(path: str):
os.chmod(path, 0o777)
def _write_lines_to_file(lines: list, name: str):
path = os.path.join(_scripts_path(), name)
with open(path, 'w') as file:
for line in lines:
line = _apply_bash_syntax_if_needed(line)
file.write(line + '\n')
_fix_file_permissions(path)
def _total_src_file_count(modules: str):
count = 0
for module in CONFIG[modules]:
if 'main-file' in module:
count += 1
if 'components-dir' in module and 'components-files' in module:
for component_file in module['components-files']:
count += 1
return count
def _bash_syntax():
bash_syntax = False # if '--bash-syntax' is undefined
if ARGS.bash_syntax:
bash_syntax = ARGS.bash_syntax
return bash_syntax
def _apply_bash_syntax_if_needed(line: str):
if _bash_syntax():
line = line.replace('\\', '/') # change path separators
line = line.replace('/033', r'\033') # fix colors after previous step
return line
def _enable_backslash_escapes():
enable_backslash_escapes = False # if '--enable-backslash-escapes' is undefined
if ARGS.enable_backslash_escapes:
enable_backslash_escapes = ARGS.enable_backslash_escapes
return enable_backslash_escapes
def _print_wheel_dir():
wheel_dir = CONFIG['pycfml']['dir']['dist-wheel']
print(wheel_dir)
def _print_release_version():
release_version = PYPROJECT['project']['version']
release_version = f'v{release_version}'
print(release_version)
def _print_release_title():
release_version = PYPROJECT['project']['version']
dt = datetime.datetime.now()
build_date = f'{dt.day} {dt:%b} {dt.year}' # e.g. 3 Jun 2021
release_title = f'Version {release_version} ({build_date})'
print(release_title)
def _compiler_name():
compiler = 'gfortran' # default
if ARGS.compiler:
compiler = ARGS.compiler.lower()
return compiler
def _compiler_options():
compiler = _compiler_name()
mode = _compiling_mode()
options = ''
for build in CONFIG['build-configs']:
if _platform() in build['platforms'] and compiler in build['compilers']:
options = f"{build['modes']['base']}"
if build['modes'][mode]:
options += f" {build['modes'][mode]}"
break
return options
def _obj_ext():
compiler = _compiler_name()
ext = ''
for build in CONFIG['build-configs']:
if _platform() in build['platforms'] and compiler in build['compilers']:
ext = build['obj-ext']
break
return ext
def _compiler_build_shared_template():
compiler = _compiler_name()
template = ''
for build in CONFIG['build-configs']:
if _platform() in build['platforms'] and compiler in build['compilers']:
template = build['build-shared']
break
return template
def _compiler_build_exe_template():
compiler = _compiler_name()
template = ''
for build in CONFIG['build-configs']:
if _platform() in build['platforms'] and compiler in build['compilers']:
template = build['build-exe']
break
return template
def _compiler_obj_ext():
compiler = _compiler_name()
obj_ext = ''
for build in CONFIG['build-configs']:
if _platform() in build['platforms'] and compiler in build['compilers']:
obj_ext = build['obj-ext']
break
return obj_ext
def _compiling_mode():
mode = 'debug' # default
if ARGS.mode:
mode = ARGS.mode.lower()
return mode
def _compiling_progress(current: int, total: int):
progress = round(current / total * 100)
return progress
def _compile_obj_script_line(src_path: str,
include_path: str=''):
compiler = _compiler_name()
options = _compiler_options()
template_cmd = CONFIG['template']['build-obj']
if not include_path:
template_cmd = template_cmd.replace(' -I {INCLUDE}', '')
else:
template_cmd = template_cmd.replace('{INCLUDE}', include_path)
cmd = template_cmd
cmd = cmd.replace('{COMPILER}', compiler)
cmd = cmd.replace('{OPTIONS}', options)
cmd = cmd.replace('{PATH}', src_path)
return cmd
def _compile_pycfml_shared_obj_or_dynamic_lib_script_line():
src_name = CONFIG['pycfml']['src-name']
shared_lib_ext = CONFIG['build']['shared-lib-ext'][_platform()]
cfml_lib_name = CONFIG['cfml']['static-lib-name']
cfml_dist_dir = CONFIG['cfml']['dir']['dist']
cfml_dist_path = os.path.join(_project_path(), cfml_dist_dir)
cfml_lib_dist_dir = CONFIG['cfml']['dir']['dist-lib']
cfml_lib_dist_path = os.path.join(_project_path(), cfml_lib_dist_dir)
cmd = _compiler_build_shared_template()
cmd = cmd.replace('{COMPILER}', _compiler_name())
cmd = cmd.replace('{PATH}', src_name)
cmd = cmd.replace('{OBJ_EXT}', _compiler_obj_ext())
#cmd = cmd.replace('{PATH}.{OBJ_EXT}', f'*.{obj_ext}') # CFML_Wraps.o Wraps_*.o crysfml08lib.o
cmd = cmd.replace('{EXT}', shared_lib_ext)
cmd = cmd.replace('{CFML_LIB_PATH}', cfml_lib_dist_path)
cmd = cmd.replace('{CFML_LIB_NAME}', cfml_lib_name)
cmd = cmd.replace('{IFPORT_LIB}', _ifport_lib())
cmd = cmd.replace('{PYTHON_LIB}', _python_lib())
return cmd
def _compile_objs_script_lines(modules: str,
src_path: str,
include_path: str=''):
compiler = _compiler_name()
options = _compiler_options()
src_ext = CONFIG['build']['src-ext']
template_cmd = CONFIG['template']['build-obj']
if not include_path:
template_cmd = template_cmd.replace(' -I {INCLUDE}', '')
else:
template_cmd = template_cmd.replace('{INCLUDE}', include_path)
total = _total_src_file_count(modules)
current = 0
lines = []
for module in CONFIG[modules]:
if 'main-file' in module:
current += 1
name = f'{module["main-file"]}.{src_ext}'
msg = _echo_progress_msg(current, total, f'{name}')
lines.append(msg)
path = os.path.join(src_path, name)
cmd = template_cmd
cmd = cmd.replace('{COMPILER}', compiler)
cmd = cmd.replace('{OPTIONS}', options)
cmd = cmd.replace('{PATH}', path)
lines.append(cmd)
if 'components-dir' in module and 'components-files' in module:
components_dir = module['components-dir']
for component_file in module['components-files']:
current += 1
name = f'{component_file}.{src_ext}'
path = os.path.join(components_dir, name)
msg = _echo_progress_msg(current, total, f'{components_dir}/{name}')
lines.append(msg)
path = os.path.join(src_path, path)
cmd = template_cmd
cmd = cmd.replace('{COMPILER}', compiler)
cmd = cmd.replace('{OPTIONS}', options)
cmd = cmd.replace('{PATH}', path)
cmd = f'{cmd}&' # start this bash command in background for parallel compilation
lines.append(cmd)
if current % 11 == 0: # do not parallelise for more than 10 compilations
lines.append('wait') # wait for all parallel bash commands to finish
lines.append('wait') # wait for all parallel bash commands to finish
return lines
def _compile_shared_objs_or_dynamic_libs_script_lines(modules: str):
shared_lib_ext = CONFIG['build']['shared-lib-ext'][_platform()]
cfml_lib_name = CONFIG['cfml']['static-lib-name']
cfml_dist_dir = CONFIG['cfml']['dir']['dist']
cfml_dist_path = os.path.join(_project_path(), cfml_dist_dir)
cfml_lib_dist_dir = CONFIG['cfml']['dir']['dist-lib']
cfml_lib_dist_path = os.path.join(_project_path(), cfml_lib_dist_dir)
template_cmd = _compiler_build_shared_template()
total = _total_src_file_count(modules)
current = 0
lines = []
for module in CONFIG[modules]:
if 'main-file' in module:
current += 1
name = f'{module["main-file"]}'
msg = _echo_progress_msg(current, total, f'{name}.{_obj_ext()}')
lines.append(msg)
cmd = template_cmd
cmd = cmd.replace('{COMPILER}', _compiler_name())
cmd = cmd.replace('{PATH}', name)
cmd = cmd.replace('{OBJ_EXT}', _compiler_obj_ext())
cmd = cmd.replace('{EXT}', shared_lib_ext)
cmd = cmd.replace('{CFML_LIB_PATH}', cfml_lib_dist_path)
cmd = cmd.replace('{CFML_LIB_NAME}', cfml_lib_name)
cmd = cmd.replace('{IFPORT_LIB}', _ifport_lib())
cmd = cmd.replace('{PYTHON_LIB}', _python_lib())
#lines.append(f"echo '>>>>> {cmd}'")
lines.append(cmd)
return lines
def _compile_executables_script_lines(modules: str,
src_path: str,
include_path: str,
lib_dir: str,
lib_name: str):
src_ext = CONFIG['build']['src-ext']
lib_ext = CONFIG['build']['static-lib-ext'][_platform()]
template_cmd = _compiler_build_exe_template()
compiler = _compiler_name()
options = _compiler_options()
total = _total_src_file_count(modules)
current = 0
lines = []
for test in CONFIG[modules]:
current += 1
dir = test["main-dir"]
main_name = test["main-file"]
source_name = f'{main_name}.{src_ext}'
msg = _echo_progress_msg(current, total, f"{source_name}")
lines.append(msg)
source_path = os.path.join(src_path, dir, source_name)
cmd = template_cmd
cmd = cmd.replace('{COMPILER}', compiler)
cmd = cmd.replace('{OPTIONS}', options)
cmd = cmd.replace('{EXE_NAME}', main_name)
cmd = cmd.replace('{SOURCE_PATH}', source_path)
cmd = cmd.replace('{CFML_INCLUDE_PATH}', include_path)
cmd = cmd.replace('{CFML_LIB_DIR}', lib_dir)
cmd = cmd.replace('{CFML_LIB_NAME}', lib_name)
cmd = cmd.replace('{LIB_EXT}', lib_ext)
#lines.append(f"echo '>>>>> {cmd}'")
lines.append(cmd)
return lines
def _run_subprocess(cmd:str):
result = None
p = subprocess.run(cmd, shell=True, text=True, capture_output=True)
result = p.stdout
return result
def parsed_args():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--create-scripts",
action='store_true',
help="create scripts for building and testing")
parser.add_argument("--platform",
default="macos",
choices=['macos', 'linux', 'windows'],
type=str.lower,
help="platform identifier")
parser.add_argument("--compiler",
default="gfortran",
choices=['gfortran', 'nagfor', 'ifx', 'ifort'],
type=str.lower,
help="fortran compiler")
parser.add_argument("--mode",
default="debug",
choices=['debug', 'release'],
type=str.lower,
help="compiling mode")
parser.add_argument("--bash-syntax",
action='store_true',
help="force bash shell syntax")
parser.add_argument("--enable-backslash-escapes",
action='store_true',
help="enable interpret backslash escapes (needed for GitHub CI)")
parser.add_argument("--print-wheel-dir",
action='store_true',
help="print pycfml wheel directory name")
parser.add_argument("--print-release-version",
action='store_true',
help="print pycfml package release version")
parser.add_argument("--print-release-title",
action='store_true',
help="print pycfml package release title")
return parser.parse_args()
def loaded_pyproject():
path = os.path.join(_project_dir(), 'pyproject.toml')
#with open(path, 'rb') as f:
#pyproject = tomllib.load(f)
pyproject = toml.load(path)
return pyproject
def loaded_config(name: str):
path = _config_path(name)
#with open(path, 'rb') as f:
#config = tomllib.load(f)
config = toml.load(path)
if _bash_syntax():
for idx, build in enumerate(config['build-configs']):
config['build-configs'][idx]['build-shared'] = build['build-shared'].replace('/', '-')
config['build-configs'][idx]['build-exe'] = build['build-exe'].replace('/', '-')
config['build-configs'][idx]['modes']['base'] = build['modes']['base'].replace('/', '-')
config['build-configs'][idx]['modes']['debug'] = build['modes']['debug'].replace('/', '-')
config['build-configs'][idx]['modes']['release'] = build['modes']['release'].replace('/', '-')
return config
def clear_main_script():
path = _main_script_path()
if not os.path.isfile(path):
file = Path(path)
file.parent.mkdir(exist_ok=True, parents=True)
with open(path, 'w') as file:
pass
def append_to_main_script(obj: str | list):
path = _main_script_path()
if isinstance(obj, str):
lines = [obj]
elif isinstance(obj, list):
lines = obj
with open(path, 'a') as file:
for line in lines:
line = _apply_bash_syntax_if_needed(line)
file.write(line + '\n')
_fix_file_permissions(path)
def add_main_script_header(txt: str):
header = _echo_header(txt)
append_to_main_script(header)
def print_build_variables():
lines = []
msg = _echo_msg(f"Platform: {_platform()}")
lines.append(msg)
msg = _echo_msg(f"Processor: {_processor()}")
lines.append(msg)
msg = _echo_msg(f"Compiling mode: {_compiling_mode()}")
lines.append(msg)
#msg = _echo_msg(f"Compiler options '{_compiler_options()}'")
#lines.append(msg)
msg = _echo_msg(f"Fortran compiler: {_compiler_name()}")
lines.append(msg)
msg = _echo_msg(f"Python version: {_python_version_full()}")
lines.append(msg)
msg = _echo_msg(f"Python tag: {_python_tag()}")
lines.append(msg)
msg = _echo_msg(f"Python site packages: {_python_site_packages()}")
lines.append(msg)
msg = _echo_msg(f"Python lib: {_python_lib()}")
lines.append(msg)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def create_cfml_repo_dir():
repo_dir = CONFIG['cfml']['dir']['repo']
repo_path = os.path.join(_project_path(), repo_dir)
lines = []
msg = _echo_msg(f"Deleting build dir '{repo_dir}'")
lines.append(msg)
cmd = f'rm -rf {repo_path}'
lines.append(cmd)
msg = _echo_msg(f"Creating build dir '{repo_dir}'")
lines.append(msg)
cmd = f'mkdir -p {repo_path}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def download_cfml_repo():
project_name = CONFIG['cfml']['log-name']
url = CONFIG['cfml']['git']['url']
branch = CONFIG['cfml']['git']['branch']
out_dir = CONFIG['cfml']['dir']['repo']
out_path = os.path.abspath(out_dir)
lines = []
msg = _echo_msg(f"Downloading {project_name} ('{branch}' branch) to '{out_dir}' from {url}")
lines.append(msg)
cmd = CONFIG['template']['clone-repo']
cmd = cmd.replace('{BRANCH}', branch)
cmd = cmd.replace('{URL}', url)
cmd = cmd.replace('{OUT_PATH}', out_path)
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def create_cfml_build_dir():
build_dir = CONFIG['cfml']['dir']['build-obj']
build_path = os.path.join(_project_path(), build_dir)
lines = []
msg = _echo_msg(f"Deleting build dir '{build_dir}'")
lines.append(msg)
cmd = f'rm -rf {build_path}'
lines.append(cmd)
msg = _echo_msg(f"Creating build dir '{build_dir}'")
lines.append(msg)
cmd = f'mkdir -p {build_path}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def rename_global_deps_file():
src_ext = CONFIG['build']['src-ext']
src_dir = CONFIG['cfml']['dir']['repo-src']
if _platform() == 'macos':
platform_suffix = 'MacOS'
elif _platform() == 'linux':
platform_suffix = 'Linux'
elif _platform() == 'windows':
platform_suffix = 'Windows'
compiler = _compiler_name()
if compiler in ['gfortran', 'nagfor']:
compiler_suffix = 'GFOR'
elif compiler in ['ifort', 'ifx']:
compiler_suffix = 'IFOR'
from_name = f'CFML_GlobalDeps_{platform_suffix}_{compiler_suffix}.{src_ext}'
from_relpath = os.path.join(src_dir, from_name)
from_abspath = os.path.join(_project_path(), from_relpath)
to_name = f'CFML_GlobalDeps.{src_ext}'
to_relpath = os.path.join(src_dir, to_name)
to_abspath = os.path.join(_project_path(), to_relpath)
lines = []
msg = _echo_msg(f"Copying '{from_relpath}' to '{to_relpath}'")
lines.append(msg)
cmd = f'cp {from_abspath} {to_abspath}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def build_cfml_modules_obj():
project_name = CONFIG['cfml']['log-name']
src_dir = CONFIG['cfml']['dir']['repo-src']
src_path = os.path.join(_project_path(), src_dir)
build_dir = CONFIG['cfml']['dir']['build-obj']
build_path = os.path.join(_project_path(), build_dir)
lines = []
msg = _echo_msg(f"Entering build dir '{build_dir}'")
lines.append(msg)
cmd = f'cd {build_path}'
lines.append(cmd)
msg = _echo_msg(f"Building fortran objects for {project_name} modules")
lines.append(msg)
compile_lines = _compile_objs_script_lines('src-cfml-modules',
src_path)
lines.extend(compile_lines)
msg = _echo_msg(f"Exiting build dir '{build_dir}'")
lines.append(msg)
cmd = f'cd {_project_path()}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def delete_renamed_global_deps_file():
src_ext = CONFIG['build']['src-ext']
src_dir = CONFIG['cfml']['dir']['repo-src']
name = f'CFML_GlobalDeps.{src_ext}'
relpath = os.path.join(src_dir, name)
abspath = os.path.join(_project_path(), relpath)
lines = []
msg = _echo_msg(f"Deleting previously created '{relpath}'")
lines.append(msg)
cmd = f'rm {abspath}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def build_cfml_static_lib():
lib_ext = CONFIG['build']['static-lib-ext'][_platform()]
static_lib_prefix = CONFIG['build']['static-lib-prefix'][_platform()]
lib_name = CONFIG['cfml']['static-lib-name']
lib_name = f'{static_lib_prefix}{lib_name}'
build_dir = CONFIG['cfml']['dir']['build-obj']
build_path = os.path.join(_project_path(), build_dir)
lines = []
msg = _echo_msg(f"Entering build dir '{build_dir}'")
lines.append(msg)
cmd = f'cd {build_path}'
lines.append(cmd)
msg = _echo_msg(f"Creating fortran static library '{lib_name}.{lib_ext}'")
lines.append(msg)
cmd = CONFIG['template']['build-static'][_platform()]
cmd = cmd.replace('{LIB}', lib_name)
cmd = cmd.replace('{OBJ_EXT}', _obj_ext())
lines.append(cmd)
msg = _echo_msg(f"Exiting build dir '{build_dir}'")
lines.append(msg)
cmd = f'cd {_project_path()}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def create_cfml_dist_dir():
dist_dir = CONFIG['cfml']['dir']['dist']
lib_dir = CONFIG['cfml']['dir']['dist-lib']
include_dir = CONFIG['cfml']['dir']['dist-include']
progs_dir = CONFIG['cfml']['dir']['dist-progs']
lines = []
msg = _echo_msg(f"Deleting dist dir '{dist_dir}'")
lines.append(msg)
cmd = f'rm -rf {dist_dir}'
lines.append(cmd)
msg = _echo_msg(f"Creating dist dir '{dist_dir}'")
lines.append(msg)
cmd = f'mkdir -p {dist_dir}'
lines.append(cmd)
msg = _echo_msg(f"Creating dist dir '{lib_dir}'")
lines.append(msg)
cmd = f'mkdir -p {lib_dir}'
lines.append(cmd)
msg = _echo_msg(f"Creating dist dir '{include_dir}'")
lines.append(msg)
cmd = f'mkdir -p {include_dir}'
lines.append(cmd)
msg = _echo_msg(f"Creating dist dir '{progs_dir}'")
lines.append(msg)
cmd = f'mkdir -p {progs_dir}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def move_built_to_cfml_dist():
lib_ext = CONFIG['build']['static-lib-ext'][_platform()]
static_lib_prefix = CONFIG['build']['static-lib-prefix'][_platform()]
lib_name = CONFIG['cfml']['static-lib-name']
lib_name = f'{static_lib_prefix}{lib_name}'
build_dir = CONFIG['cfml']['dir']['build-obj']
build_path = os.path.join(_project_path(), build_dir)
lib_dist_dir = CONFIG['cfml']['dir']['dist-lib']
lib_dist_path = os.path.join(_project_path(), lib_dist_dir)
include_dist_dir = CONFIG['cfml']['dir']['dist-include']
include_dist_path = os.path.join(_project_path(), include_dist_dir)
lines = []
msg = _echo_msg(f"Moving built lib '{lib_name}.{lib_ext}' to dist dir '{lib_dist_dir}'")
lines.append(msg)
from_path = os.path.join(build_path, f'{lib_name}.{lib_ext}')
cmd = f'mv {from_path} {lib_dist_path}'
lines.append(cmd)
msg = _echo_msg(f"Moving built modules '*.*mod' to dist dir '{include_dist_dir}'")
lines.append(msg)
from_path = os.path.join(build_path, '*.*mod')
cmd = f'mv {from_path} {include_dist_path}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def build_cfml_test_programs():
project_name = CONFIG['cfml']['log-name']
src_dir = CONFIG['cfml']['dir']['repo-tests']
src_path = os.path.join(_project_path(), src_dir)
#build_dir = os.path.join('tests', 'functional_tests', 'cfml')
build_dir = CONFIG['cfml']['dir']['dist-progs']
build_path = os.path.join(_project_path(), build_dir)
dist_dir = CONFIG['cfml']['dir']['dist']
include_dir = CONFIG['cfml']['dir']['dist-include']
include_path = os.path.join(_project_path(), include_dir)
lib_dir = CONFIG['cfml']['dir']['dist-lib']
lib_path = os.path.join(_project_path(), lib_dir)
lib_name = CONFIG['cfml']['static-lib-name']
lines = []
msg = _echo_msg(f"Entering build dir '{build_dir}'")
lines.append(msg)
cmd = f'cd {build_path}'
lines.append(cmd)
msg = _echo_msg(f"Building test programs for {project_name}")
lines.append(msg)
compile_lines = _compile_executables_script_lines('src-cfml-tests',
src_path, include_path,
lib_path,
lib_name)
lines.extend(compile_lines)
msg = _echo_msg(f"Deleting *.mod/*.smod files in '{build_dir}'")
lines.append(msg)
cmd = f'rm -rf *.*mod'
lines.append(cmd)
msg = _echo_msg(f"Exiting build dir '{build_dir}'")
lines.append(msg)
cmd = f'cd {_project_path()}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def copy_cfml_test_programs_to_tests_dir():
progs_relpath = CONFIG['cfml']['dir']['dist-progs']
progs_abspath = os.path.join(_project_path(), progs_relpath)
from_path = os.path.join(progs_abspath, '*')
tests_relpath = os.path.join('tests', 'functional_tests', 'CFML')
tests_abspath = os.path.join(_project_path(), tests_relpath)
to_path = tests_abspath
lines = []
msg = _echo_msg(f"Copying all files from '{progs_relpath}' to '{tests_relpath}'")
lines.append(msg)
cmd = f'cp {from_path} {to_path}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def run_cfml_functional_tests_no_benchmarks():
tests_relpath = os.path.join('tests', 'functional_tests', 'CFML')
tests_abspath = os.path.join(_project_path(), tests_relpath)
lines = []
#msg = _echo_msg(f"Entering tests dir '{tests_relpath}'")
#lines.append(msg)
#cmd = f'cd {tests_relpath}'
#lines.append(cmd)
msg = _echo_msg(f"Running functional tests from '{tests_relpath}'")
lines.append(msg)
cmd = CONFIG['template']['run-tests']
cmd = cmd.replace('{PATH}', tests_abspath)
lines.append(cmd)
#msg = _echo_msg(f"Exiting tests dir '{tests_relpath}'")
#lines.append(msg)
#cmd = f'cd {_project_path()}'
#lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def run_cfml_functional_tests_with_benchmarks():
project_name = CONFIG['cfml']['log-name']
relpath = os.path.join('tests', 'functional_tests', 'CFML')
abspath = os.path.join(_project_path(), relpath)
lines = []
msg = _echo_msg(f"Running functional tests with benchmarks from '{relpath}'")
lines.append(msg)
cmd = CONFIG['template']['run-benchmarks']['base']
if _github_branch() == 'develop':
cmd += ' ' + CONFIG['template']['run-benchmarks']['develop-branch']
else:
cmd += ' ' + CONFIG['template']['run-benchmarks']['non-develop-branch']
cmd = cmd.replace('{PATH}', abspath)
cmd = cmd.replace('{PROJECT}', project_name)
if _github_actions():
cmd = cmd.replace('{RUNNER}', 'github')
else:
cmd = cmd.replace('{RUNNER}', 'local')
cmd = cmd.replace('{COMPILER}', _compiler_name())
cmd = cmd.replace('{PROCESSOR}', _processor())
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def copy_powder_mod_to_pycfml_repo():
CFML = CONFIG['cfml']['log-name']
cfml_repo_dir = CONFIG['cfml']['dir']['repo']
cfml_repo_path = os.path.join(_project_path(), cfml_repo_dir)
pyCFML = CONFIG['pycfml']['log-name']
pycfml_repo_dir = CONFIG['pycfml']['dir2']['repo']
pycfml_src_dir = CONFIG['pycfml']['dir2']['repo-src']
pycfml_src_path = os.path.join(_project_path(), pycfml_repo_dir, pycfml_src_dir)
src_ext = CONFIG['build']['src-ext']
lines = []
from_relpath = os.path.join('Testing', 'Powder', 'Test_2', 'fortran', 'src', f'powder_mod.{src_ext}')
from_abspath = os.path.join(cfml_repo_path, from_relpath)
msg = _echo_msg(f"Copying '{from_relpath}' to '{pycfml_repo_dir}/{pycfml_src_dir}'")
lines.append(msg)
cmd = f'cp {from_abspath} {pycfml_src_path}'
lines.append(cmd)
from_relpath = os.path.join('Testing', 'Powder', 'Test_3', f'powder_mod_2.{src_ext}')
from_abspath = os.path.join(cfml_repo_path, from_relpath)
msg = _echo_msg(f"Copying '{from_relpath}' to '{pycfml_repo_dir}/{pycfml_src_dir}'")
lines.append(msg)
cmd = f'cp {from_abspath} {pycfml_src_path}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def create_pycfml_src_dir():
src_dir = CONFIG['pycfml']['dir']['build-src']
lines = []
msg = _echo_msg(f"Deleting src dir '{src_dir}'")
lines.append(msg)
cmd = f'rm -rf {src_dir}'
lines.append(cmd)
msg = _echo_msg(f"Creating src dir '{src_dir}'")
lines.append(msg)
cmd = f'mkdir -p {src_dir}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def create_pycfml_build_dir():
build_dir = CONFIG['pycfml']['dir']['build-obj']
lines = []
msg = _echo_msg(f"Deleting build dir '{build_dir}'")
lines.append(msg)
cmd = f'rm -rf {build_dir}'
lines.append(cmd)
msg = _echo_msg(f"Creating build dir '{build_dir}'")
lines.append(msg)
cmd = f'mkdir -p {build_dir}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def create_pycfml_src():
project_name = CONFIG['pycfml']['log-name']
apigen_relpath = CONFIG['cfml']['scripts']['pyapigen']
apigen_parent_relpath = Path(apigen_relpath).parent.as_posix()
apigen_file = Path(apigen_relpath).name
build_relpath = CONFIG['pycfml']['dir']['build-src']
build_abspath = os.path.join(_project_path(), build_relpath)
lines = []
msg = _echo_msg(f"Entering Python API gen dir '{apigen_parent_relpath}'")
lines.append(msg)
cmd = f'cd {apigen_parent_relpath}'
lines.append(cmd)
msg = _echo_msg(f"Creating {project_name} source code with '{apigen_file}'")
lines.append(msg)
cmd = CONFIG['template']['run-python']
cmd = cmd.replace('{PATH}', apigen_file)
cmd = cmd.replace('{OPTIONS}', f'--verbose False --scripts False --build {build_abspath}')
lines.append(cmd)
msg = _echo_msg(f"Exiting build dir '{apigen_parent_relpath}'")
lines.append(msg)
cmd = f'cd {_project_path()}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def build_pycfml_modules_obj():
project_name = CONFIG['cfml']['log-name']
src_relpath = CONFIG['pycfml']['dir']['build-src-fortran']
src_abspath = os.path.join(_project_path(), src_relpath)
build_relpath = CONFIG['pycfml']['dir']['build-obj']
include_relpath = CONFIG['cfml']['dir']['dist-include']
include_abspath = os.path.join(_project_path(), include_relpath)
lines = []
msg = _echo_msg(f"Entering build dir '{build_relpath}'")
lines.append(msg)
cmd = f'cd {build_relpath}'
lines.append(cmd)
msg = _echo_msg(f"Building fortran objects for {project_name} modules")
lines.append(msg)
compile_lines = _compile_objs_script_lines('src-cfml-wraps',
src_abspath,
include_abspath)
lines.extend(compile_lines)
msg = _echo_msg(f"Exiting build dir '{build_relpath}'")
lines.append(msg)
cmd = f'cd {_project_path()}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def build_pycfml_lib_obj():
project_name = CONFIG['pycfml']['log-name']
src_ext = CONFIG['build']['src-ext']
pycfml_src_name = CONFIG['pycfml']['src-name']
pycfml_src_file = f'{pycfml_src_name}.{src_ext}'
src_relpath = CONFIG['pycfml']['dir']['build-src-fortran']
src_abspath = os.path.join(_project_path(), src_relpath, pycfml_src_file)
build_relpath = CONFIG['pycfml']['dir']['build-obj']
include_relpath = CONFIG['cfml']['dir']['dist-include']
include_abspath = os.path.join(_project_path(), include_relpath)
lines = []
msg = _echo_msg(f"Entering build dir '{build_relpath}'")
lines.append(msg)
cmd = f'cd {build_relpath}'
lines.append(cmd)
msg = _echo_msg(f"Building fortran object for {project_name} library")
lines.append(msg)
compile_line = _compile_obj_script_line(src_abspath, include_abspath)
lines.append(compile_line)
msg = _echo_msg(f"Exiting build dir '{build_relpath}'")
lines.append(msg)
cmd = f'cd {_project_path()}'
lines.append(cmd)
script_name = f'{sys._getframe().f_code.co_name}.sh'
_write_lines_to_file(lines, script_name)
append_to_main_script(lines)
def build_pycfml_shared_obj_or_dynamic_lib():
build_relpath = CONFIG['pycfml']['dir']['build-obj']
lib_name = CONFIG['pycfml']['src-name'] # NEED FIX: use CONFIG['pycfml']['dynamic-lib-name']
lib_ext = CONFIG['build']['shared-lib-ext'][_platform()]
lines = []
msg = _echo_msg(f"Entering build dir '{build_relpath}'")
lines.append(msg)
cmd = f'cd {build_relpath}'
lines.append(cmd)
msg = _echo_msg(f"Building fortran shared obj or dynamic lib '{lib_name}.{lib_ext}'")