Skip to content

Commit 946d4f1

Browse files
Change SCons output structure (algorithm-archivists#963)
1 parent 6b592d5 commit 946d4f1

File tree

16 files changed

+103
-92
lines changed

16 files changed

+103
-92
lines changed

SConstruct

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ Currently, the aim is to provide a way to compile or copy the implementation fil
88
To 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

1010
from pathlib import Path
11+
from collections import namedtuple
1112
import os
1213

14+
1315
rust_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+
2630
env['CFLAGS'] = '-Wall -Wextra -Werror'
2731
env['CXXFLAGS'] = '-std=c++17'
2832
env['ASFLAGS'] = '--64'
2933

3034
# Add other languages here when you want to add language targets
3135
# Put 'name_of_language_directory' : 'file_extension'
3236
languages = {
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
4146
env.C = env.Program
4247
env.CPlusPlus = env.Program
4348
env.X64 = env.Program
4449
env.Fortran = env.Program
4550

46-
Export('env')
51+
for language in languages:
52+
Alias(language, f'#/build/{language}')
4753

4854
sconscripts = []
4955
files_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')
6380
for 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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('*')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
dirname = Path.cwd().parents[1].stem
5-
6-
env.C(f'#/build/c/{dirname}', Glob('*.c'), LIBS='m')
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.C(build_target, str(file_info.path), LIBS='m')
6+
env.Alias(str(file_info.chapter), build_result)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('*')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
dirname = Path.cwd().parents[1].stem
5-
6-
env.X64(f'#/build/asm-x64/{dirname}', Glob('*.s'), LIBS=['m'], LINKFLAGS='-no-pie')
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.X64(build_target, str(file_info.path), LIBS='m', LINKFLAGS='-no-pie')
6+
env.Alias(str(file_info.chapter), build_result)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('*')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
dirname = Path.cwd().parents[1].stem
5-
6-
env.C(f'#/build/c/{dirname}', Glob('*.c'), LIBS=['m', 'fftw3'])
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.C(build_target, str(file_info.path), LIBS=['m', 'fftw3'])
6+
env.Alias(str(file_info.chapter), build_result)

contents/euclidean_algorithm/code/fortran/SConscript

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('*')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
dirname = Path.cwd().parents[1].stem
5-
6-
env.X64(f'#/build/asm-x64/{dirname}', Glob('*.s'), LIBS='m', LINKFLAGS='-no-pie')
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.X64(build_target, str(file_info.path), LIBS='m', LINKFLAGS='-no-pie')
6+
env.Alias(str(file_info.chapter), build_result)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('*')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
dirname = Path.cwd().parents[1].stem
5-
6-
env.C(f'#/build/c/{dirname}', Glob('*.c'), LIBS='m')
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.C(build_target, str(file_info.path), LIBS='m')
6+
env.Alias(str(file_info.chapter), build_result)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('*')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
dirname = Path.cwd().parents[1].stem
5-
6-
env.C(f'#/build/c/{dirname}', Glob('*.c'), LIBS='m')
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.C(build_target, str(file_info.path), LIBS='m')
6+
env.Alias(str(file_info.chapter), build_result)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('*')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
dirname = Path.cwd().parents[1].stem
5-
6-
env.C(f'#/build/c/{dirname}', Glob('*.c'), LIBS=['m', 'fftw3'])
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.C(build_target, str(file_info.path), LIBS=['m', 'fftw3'])
6+
env.Alias(str(file_info.chapter), build_result)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('*')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
dirname = Path.cwd().parents[1].stem
5-
6-
env.CPlusPlus(f'#/build/cpp/{dirname}', Glob('*.cpp'), LIBS=['m', 'fftw3'])
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.CPlusPlus(build_target, str(file_info.path), LIBS=['m', 'fftw3'])
6+
env.Alias(str(file_info.chapter), build_result)

sconscripts/asm-x64_SConscript

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import('files_to_compile language env')
2-
from pathlib import Path
1+
Import('files_to_compile env')
32

4-
for file in files_to_compile:
5-
chapter_name = file.parent.parent.parent.stem
6-
env.X64(f'#/build/{language}/{chapter_name}', str(file), LINKFLAGS='-no-pie')
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.X64(build_target, str(file_info.path), LINKFLAGS='-no-pie')
6+
env.Alias(str(file_info.chapter), build_result)

sconscripts/c_SConscript

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Import('files_to_compile env')
2-
from pathlib import Path
32

4-
for file in files_to_compile:
5-
chapter_name = file.parent.parent.parent.stem
6-
env.C(f'#/build/c/{chapter_name}', str(file))
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.C(build_target, str(file_info.path))
6+
env.Alias(str(file_info.chapter), build_result)

sconscripts/cpp_SConscript

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Import('files_to_compile env')
2-
from pathlib import Path
32

4-
for file in files_to_compile:
5-
chapter_name = file.parent.parent.parent.stem
6-
env.CPlusPlus(f'#/build/cpp/{chapter_name}', str(file))
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.CPlusPlus(build_target, str(file_info.path))
6+
env.Alias(str(file_info.chapter), build_result)

sconscripts/fortran_SConscript

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Import('files_to_compile env')
2-
from pathlib import Path
32

4-
for file in files_to_compile:
5-
chapter_name = file.parent.parent.parent.stem
6-
env.Fortran(f'#/build/fortran/{chapter_name}', str(file))
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.Fortran(build_target, str(file_info.path))
6+
env.Alias(str(file_info.chapter), build_result)

sconscripts/go_SConscript

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Import('files_to_compile env')
2-
from pathlib import Path
32

4-
for file in files_to_compile:
5-
chapter_name = file.parent.parent.parent.stem
6-
env.Go(f'#/build/go/{chapter_name}', str(file))
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.Go(build_target, str(file_info.path))
6+
env.Alias(str(file_info.chapter), build_result)

sconscripts/rust_SConscript

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
Import('files_to_compile env')
2-
from pathlib import Path
32

4-
for file in files_to_compile:
5-
chapter_name = file.parent.parent.parent.stem
6-
if (file.parent / 'Cargo.toml').exists():
7-
env.cargo(target=f'#/build/rust/{chapter_name}',
8-
source=str(file),
9-
MANIFEST=str(file.parent / 'Cargo.toml'),
10-
SOURCE_DIR=str(file.parent))
11-
env.Clean('rust', str(file.parent / 'target'))
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
if (file_info.path.parent / 'Cargo.toml').exists():
6+
build_result = env.cargo(target=build_target,
7+
source=str(file_info.path),
8+
MANIFEST=str(file_info.path.parent / 'Cargo.toml'),
9+
SOURCE_DIR=str(file_info.path.parent))
10+
env.Clean('rust', str(file_info.path.parent / 'target'))
1211
else:
13-
env.rustc(f'#/build/rust/{chapter_name}', str(file))
14-
env.Clean('rust', f'#/build/rust/{chapter_name}.pdb')
12+
build_result = env.rustc(build_target, str(file_info.path))
13+
env.Clean('rust', f'{build_target}.pdb')
14+
15+
env.Alias(str(file_info.chapter), build_result)

0 commit comments

Comments
 (0)