diff --git a/.gitignore b/.gitignore index 231da8ea1..4d5a431b0 100644 --- a/.gitignore +++ b/.gitignore @@ -524,3 +524,7 @@ vscode/ # SCons build directory build/ + +# Cargo artifacts +Cargo.lock +target/ diff --git a/Dockerfile b/Dockerfile index d6ecdfc45..a0808c1ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT} # [Optional] Uncomment this section to install additional OS packages. RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ - && 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 + && 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 # Setup Crystal 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 RUN sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D9D33FCD84D82C17288BA03B3C9A6980F827E01E RUN sudo add-apt-repository 'deb http://ppa.launchpad.net/plt/racket/ubuntu focal main' +# Setup Rust +RUN curl https://sh.rustup.rs -sSf | sh -s -- -y + # Setup Scratch ## using 1.x right now.... in future checkout snap or adobe air? diff --git a/SConstruct b/SConstruct index 93354e152..1a20bf633 100644 --- a/SConstruct +++ b/SConstruct @@ -10,17 +10,22 @@ To run the compilation for all implementations in one language, e.g. C, run the from pathlib import Path import os -env = Environment(ENV={'PATH': os.environ['PATH']}, - tools=['gcc', 'gnulink', 'g++', 'gas']) +rust_cargo_builder = Builder(action=['cargo build --bins --manifest-path $MANIFEST', + Move('$TARGET$PROGSUFFIX', '$SOURCE_DIR/target/debug/main$PROGSUFFIX')]) -env['ASFLAGS'] = '--64' +rust_rustc_builder = Builder(action='rustc $SOURCE -o $TARGET$PROGSUFFIX') + +env = Environment(ENV=os.environ, + BUILDERS={'rustc': rust_rustc_builder, 'cargo': rust_cargo_builder}, + tools=['gcc', 'gnulink', 'g++', 'gas']) env['CCFLAGS'] = '' env['CXXFLAGS'] = '-std=c++17' +env['ASFLAGS'] = '--64' # Add other languages here when you want to add language targets # Put 'name_of_language_directory' : 'file_extension' -languages = {'c': 'c', 'cpp': 'cpp', 'asm-x64': 's'} +languages = {'c': 'c', 'cpp': 'cpp', 'asm-x64': 's', 'rust': 'rs'} env.C = env.Program env.CPlusPlus = env.Program diff --git a/contents/barnsley/code/rust/Cargo.toml b/contents/barnsley/code/rust/Cargo.toml index 40780594a..52505634b 100644 --- a/contents/barnsley/code/rust/Cargo.toml +++ b/contents/barnsley/code/rust/Cargo.toml @@ -1,9 +1,13 @@ [package] -name = "rust" +name = "barnsley" version = "0.1.0" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rand = "0.8.4" \ No newline at end of file +rand = "0.8.4" + +[[bin]] +path = "./barnsley.rs" +name = "main" \ No newline at end of file diff --git a/contents/barnsley/code/rust/src/main.rs b/contents/barnsley/code/rust/barnsley.rs similarity index 100% rename from contents/barnsley/code/rust/src/main.rs rename to contents/barnsley/code/rust/barnsley.rs diff --git a/contents/cooley_tukey/code/rust/Cargo.toml b/contents/cooley_tukey/code/rust/Cargo.toml new file mode 100644 index 000000000..0cba5179d --- /dev/null +++ b/contents/cooley_tukey/code/rust/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.7.3" +rustfft = "4.1.0" + +[[bin]] +path = "./fft.rs" +name = "main" \ No newline at end of file diff --git a/contents/huffman_encoding/code/rust/Cargo.toml b/contents/huffman_encoding/code/rust/Cargo.toml new file mode 100644 index 000000000..1add99ad2 --- /dev/null +++ b/contents/huffman_encoding/code/rust/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "huffman" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.10.1" + +[[bin]] +path = "./huffman.rs" +name = "main" \ No newline at end of file diff --git a/contents/monte_carlo_integration/code/rust/Cargo.toml b/contents/monte_carlo_integration/code/rust/Cargo.toml new file mode 100644 index 000000000..17ff7f385 --- /dev/null +++ b/contents/monte_carlo_integration/code/rust/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "montecarlo" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.4" + +[[bin]] +path = "./monte_carlo.rs" +name = "main" \ No newline at end of file diff --git a/contents/split-operator_method/code/rust/Cargo.toml b/contents/split-operator_method/code/rust/Cargo.toml new file mode 100644 index 000000000..def85c23e --- /dev/null +++ b/contents/split-operator_method/code/rust/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "splitop" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rustfft = "4.1.0" + +[[bin]] +path = "./split_op.rs" +name = "main" \ No newline at end of file diff --git a/contents/split-operator_method/code/rust/split_op.rs b/contents/split-operator_method/code/rust/split_op.rs index 7269f5148..d29e16a2e 100644 --- a/contents/split-operator_method/code/rust/split_op.rs +++ b/contents/split-operator_method/code/rust/split_op.rs @@ -1,7 +1,6 @@ -extern crate num; extern crate rustfft; -use num::complex::Complex; +use rustfft::num_complex::Complex; use rustfft::FFTplanner; use std::f64::consts::PI; use std::fs::File; @@ -95,7 +94,7 @@ fn fft(x: &mut Vec>, inverse: bool) { let mut y = vec![Complex::new(0.0_f64, 0.0_f64); x.len()]; let mut p = FFTplanner::new(inverse); let fft = p.plan_fft(x.len()); - fft.process(x, &mut y); + fft.process(x.as_mut_slice(), y.as_mut_slice()); for i in 0..x.len() { x[i] = y[i] / (x.len() as f64).sqrt(); diff --git a/sconscripts/rust_SConscript b/sconscripts/rust_SConscript new file mode 100644 index 000000000..b9cf669c7 --- /dev/null +++ b/sconscripts/rust_SConscript @@ -0,0 +1,14 @@ +Import('files_to_compile env') +from pathlib import Path + +for file in files_to_compile: + chapter_name = file.parent.parent.parent.stem + if (file.parent / 'Cargo.toml').exists(): + env.cargo(target=f'#/build/rust/{chapter_name}', + source=str(file), + MANIFEST=str(file.parent / 'Cargo.toml'), + SOURCE_DIR=str(file.parent)) + env.Clean('rust', str(file.parent / 'target')) + else: + env.rustc(f'#/build/rust/{chapter_name}', str(file)) + env.Clean('rust', f'#/build/rust/{chapter_name}.pdb')