@@ -8,8 +8,10 @@ Currently, the aim is to provide a way to compile or copy the implementation fil
88To run the compilation for all implementations in one language, e.g. C, run the command `scons build/c`, and the resulting executables will be available in the `build/c` directory, each in their respective algorithm directory, containing the executable."""
99
1010from pathlib import Path
11+ from collections import namedtuple
1112import os
1213
14+
1315rust_cargo_builder = Builder (action = ['cargo build --bins --manifest-path $MANIFEST' ,
1416 Move ('$TARGET$PROGSUFFIX' , '$SOURCE_DIR/target/debug/main$PROGSUFFIX' )])
1517
@@ -23,48 +25,62 @@ env = Environment(ENV=os.environ,
2325 'Go' : go_builder },
2426 tools = ['gcc' , 'gnulink' , 'g++' , 'gas' , 'gfortran' ])
2527
28+ Export ('env' )
29+
2630env ['CFLAGS' ] = '-Wall -Wextra -Werror'
2731env ['CXXFLAGS' ] = '-std=c++17'
2832env ['ASFLAGS' ] = '--64'
2933
3034# Add other languages here when you want to add language targets
3135# Put 'name_of_language_directory' : 'file_extension'
3236languages = {
33- 'c' : 'c' ,
37+ 'c' : 'c' ,
3438 'cpp' : 'cpp' ,
3539 'asm-x64' : 's' ,
3640 'rust' : 'rs' ,
3741 'go' : 'go' ,
3842 'fortran' : 'f90' ,
3943}
4044
45+ # Do not add new Builders here, add them to the BUILDERS argument in the call to Environment above
4146env .C = env .Program
4247env .CPlusPlus = env .Program
4348env .X64 = env .Program
4449env .Fortran = env .Program
4550
46- Export ('env' )
51+ for language in languages :
52+ Alias (language , f'#/build/{ language } ' )
4753
4854sconscripts = []
4955files_to_compile = {language : [] for language in languages }
5056
51- for chapter_dir in Path .cwd ().joinpath ('contents' ).iterdir ():
52- if (code_dir := (chapter_dir / 'code' )).exists ():
53- for path in code_dir .iterdir ():
54- if path .stem in languages :
55- # Check for overriding sconscript
56- if (sconscript_path := path / 'SConscript' ).exists ():
57- sconscripts .append (sconscript_path )
58- SConscript (sconscript_path , exports = 'env' )
57+ FileInformation = namedtuple ('FileInformation' , ['path' , 'chapter' , 'language' ])
58+
59+
60+ contents_path = Path .cwd ().joinpath ('contents' )
61+ for chapter_dir in contents_path .iterdir ():
62+ for code_dir in chapter_dir .glob ('**/code' ):
63+ # For nested chapters e.g. contents/convolutions/1d/
64+ extended_chapter_path = code_dir .relative_to (contents_path ).parent
65+
66+ for language_dir in code_dir .iterdir ():
67+ if (language := language_dir .stem ) in languages :
68+ new_files = [FileInformation (path = file_path ,
69+ chapter = extended_chapter_path ,
70+ language = language )
71+ for file_path in language_dir .glob (f'**/*.{ languages [language ]} ' )
72+ ]
73+ # Check for overriding SConscript
74+ if (sconscript_path := language_dir / 'SConscript' ).exists ():
75+ SConscript (sconscript_path , exports = {'files_to_compile' : new_files })
5976 else :
60- files_to_compile [path . stem ].extend (path . glob ( f'*. { languages [ path . stem ] } ' ) )
77+ files_to_compile [language ].extend (new_files )
6178
62- sconscript_dir_path = Path ('sconscripts' )
79+ sconscript_dir_path = Path . cwd (). joinpath ('sconscripts' )
6380for language , files in files_to_compile .items ():
6481 if files :
6582 if (sconscript_path := sconscript_dir_path / f"{ language } _SConscript" ).exists ():
66- SConscript (sconscript_path , exports = {'files_to_compile' : files ,
67- 'language' : language })
83+ SConscript (sconscript_path , exports = {'files_to_compile' : files })
6884 else :
6985 print (f'{ language } file found at { files [0 ]} , but no sconscript file is present ' )
7086
0 commit comments