Skip to content

Commit 18eebd6

Browse files
Add Rust to SCons (#945)
* Add rust builder and build script * Move file to be discovered by SCons, and update Cargo.toml to reflect this * Add rustc to SCons * Add Cargo.toml files for code requiring libraries * Add cargo building when Cargo.toml is present * Fix copying fail on linux due to directory name and file name collision * Add build artifacts to SCons clean command and .gitignore * Fix Not a directory issue due to getting parent directory of a file * Remove redefinition of languages dictionary * Add rustc and cargo install to docker * Update Cooley-Tukey to use correct version of crates * Update split-operator method to use correct library versions and apply fixes from #688
1 parent 9d9e1cb commit 18eebd6

File tree

11 files changed

+92
-10
lines changed

11 files changed

+92
-10
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,7 @@ vscode/
524524

525525
# SCons build directory
526526
build/
527+
528+
# Cargo artifacts
529+
Cargo.lock
530+
target/

Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
66

77
# [Optional] Uncomment this section to install additional OS packages.
88
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
9-
&& apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk rustc libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch libfftw3-dev cmake
9+
&& apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch libfftw3-dev cmake
1010

1111
# Setup Crystal
1212
RUN echo 'deb http://download.opensuse.org/repositories/devel:/languages:/crystal/xUbuntu_20.04/ /' | sudo tee /etc/apt/sources.list.d/devel:languages:crystal.list
@@ -72,6 +72,9 @@ RUN sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu fo
7272
RUN sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D9D33FCD84D82C17288BA03B3C9A6980F827E01E
7373
RUN sudo add-apt-repository 'deb http://ppa.launchpad.net/plt/racket/ubuntu focal main'
7474

75+
# Setup Rust
76+
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
77+
7578
# Setup Scratch
7679
## using 1.x right now.... in future checkout snap or adobe air?
7780

SConstruct

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ To run the compilation for all implementations in one language, e.g. C, run the
1010
from pathlib import Path
1111
import os
1212

13-
env = Environment(ENV={'PATH': os.environ['PATH']},
14-
tools=['gcc', 'gnulink', 'g++', 'gas'])
13+
rust_cargo_builder = Builder(action=['cargo build --bins --manifest-path $MANIFEST',
14+
Move('$TARGET$PROGSUFFIX', '$SOURCE_DIR/target/debug/main$PROGSUFFIX')])
1515

16-
env['ASFLAGS'] = '--64'
16+
rust_rustc_builder = Builder(action='rustc $SOURCE -o $TARGET$PROGSUFFIX')
17+
18+
env = Environment(ENV=os.environ,
19+
BUILDERS={'rustc': rust_rustc_builder, 'cargo': rust_cargo_builder},
20+
tools=['gcc', 'gnulink', 'g++', 'gas'])
1721

1822
env['CCFLAGS'] = ''
1923
env['CXXFLAGS'] = '-std=c++17'
24+
env['ASFLAGS'] = '--64'
2025

2126
# Add other languages here when you want to add language targets
2227
# Put 'name_of_language_directory' : 'file_extension'
23-
languages = {'c': 'c', 'cpp': 'cpp', 'asm-x64': 's'}
28+
languages = {'c': 'c', 'cpp': 'cpp', 'asm-x64': 's', 'rust': 'rs'}
2429

2530
env.C = env.Program
2631
env.CPlusPlus = env.Program
+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
[package]
2-
name = "rust"
2+
name = "barnsley"
33
version = "0.1.0"
44
edition = "2018"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
rand = "0.8.4"
9+
rand = "0.8.4"
10+
11+
[[bin]]
12+
path = "./barnsley.rs"
13+
name = "main"
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rand = "0.7.3"
10+
rustfft = "4.1.0"
11+
12+
[[bin]]
13+
path = "./fft.rs"
14+
name = "main"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "huffman"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
itertools = "0.10.1"
10+
11+
[[bin]]
12+
path = "./huffman.rs"
13+
name = "main"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "montecarlo"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rand = "0.8.4"
10+
11+
[[bin]]
12+
path = "./monte_carlo.rs"
13+
name = "main"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "splitop"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rustfft = "4.1.0"
10+
11+
[[bin]]
12+
path = "./split_op.rs"
13+
name = "main"

contents/split-operator_method/code/rust/split_op.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
extern crate num;
21
extern crate rustfft;
32

4-
use num::complex::Complex;
3+
use rustfft::num_complex::Complex;
54
use rustfft::FFTplanner;
65
use std::f64::consts::PI;
76
use std::fs::File;
@@ -95,7 +94,7 @@ fn fft(x: &mut Vec<Complex<f64>>, inverse: bool) {
9594
let mut y = vec![Complex::new(0.0_f64, 0.0_f64); x.len()];
9695
let mut p = FFTplanner::new(inverse);
9796
let fft = p.plan_fft(x.len());
98-
fft.process(x, &mut y);
97+
fft.process(x.as_mut_slice(), y.as_mut_slice());
9998

10099
for i in 0..x.len() {
101100
x[i] = y[i] / (x.len() as f64).sqrt();

sconscripts/rust_SConscript

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Import('files_to_compile env')
2+
from pathlib import Path
3+
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'))
12+
else:
13+
env.rustc(f'#/build/rust/{chapter_name}', str(file))
14+
env.Clean('rust', f'#/build/rust/{chapter_name}.pdb')

0 commit comments

Comments
 (0)