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
|
use structopt::StructOpt;
use toml::Value;
use std::path::{Path, PathBuf};
#[derive(Debug, StructOpt)]
struct Cli {
/// fpm command
command: String,
/// Directory for all generated artifacts
#[structopt(long, name="DIRECTORY", default_value = "target")]
target_dir : PathBuf,
}
fn collect_source_files() -> Vec<String> {
let mut files: Vec<String> = Vec::new();
for entry in std::fs::read_dir(".").unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if !path.is_dir() {
let ext = match path.extension() {
None => "None",
Some(ext) => ext.to_str().unwrap(),
};
if ext == "f90" {
files.push(path.to_str().unwrap().to_string());
}
}
}
files
}
fn build(target_dir: &Path) {
println!("TARGET_DIR: {}", target_dir.to_str().unwrap());
let value = std::fs::read_to_string("fpm.toml")
.unwrap()
.parse::<Value>().unwrap();
println!("TOML: {:?}", value);
let files = collect_source_files();
let mut files2: String = String::new();
for file in &files {
println!("File: {}", file);
if !file.ends_with("main.f90") {
files2 = files2 + " " + &file.replace("\\", "/");
}
}
println!("Files: {:?}", files);
let s = format!("\
cmake_minimum_required(VERSION 3.5.0 FATAL_ERROR)
enable_language(Fortran)
project(p1)
add_executable(p1 main.f90 {})
", files2);
std::fs::write("CMakeLists.txt", s).unwrap();
let mut args: Vec<&str> = vec![];
if cfg!(windows) {
args.extend(vec!["-G", "MinGW Makefiles",
"-DCMAKE_SH=CMAKE_SH-NOTFOUND"])
};
args.extend(vec!["-B", "build", "."]);
println!("[+] cmake {:?}", args);
let output = std::process::Command::new("cmake")
.args(&args)
.env("FC", "gfortran")
.output().unwrap();
println!("status: {}", output.status);
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
if !output.status.success() {
panic!("Command failed.")
}
println!("");
let args = vec!["--build", "build"];
println!("[+] cmake {:?}", args);
let output = std::process::Command::new("cmake")
.args(&args)
.output().unwrap();
println!("status: {}", output.status);
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
if !output.status.success() {
panic!("Command failed.")
}
}
fn run() {
let output = std::process::Command::new("build/p1")
.output().unwrap();
println!("status: {}", output.status);
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
if !output.status.success() {
panic!("Command failed.")
}
}
fn main() {
let args = Cli::from_args();
println!("{:?}", args);
if args.command == "build" {
println!("Command: build");
build(args.target_dir.as_path());
} else if args.command == "run" {
println!("Command: run");
build(args.target_dir.as_path());
run();
} else {
panic!("Unknown command");
}
}
|