mirror of
https://github.com/dexoron/dcr.git
synced 2026-10-21 14:38:30 +00:00
Custom output directory via build.target
This commit is contained in:
@@ -118,6 +118,7 @@ Two profiles are supported:
|
|||||||
- `--release` - built-in flags per compiler
|
- `--release` - built-in flags per compiler
|
||||||
|
|
||||||
Custom flags can be added in `dcr.toml` via `build.cflags` and `build.ldflags`.
|
Custom flags can be added in `dcr.toml` via `build.cflags` and `build.ldflags`.
|
||||||
|
You can also set `build.target` to override the output directory (profile-independent).
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
The main project file is `dcr.toml`.
|
The main project file is `dcr.toml`.
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ version = "0.1.0"
|
|||||||
language = "c"
|
language = "c"
|
||||||
standard = "c11"
|
standard = "c11"
|
||||||
compiler = "gcc"
|
compiler = "gcc"
|
||||||
|
# Output directory for binary (optional)
|
||||||
|
target = "./dist"
|
||||||
# Custom compile/link flags (optional)
|
# Custom compile/link flags (optional)
|
||||||
cflags = ["-D_POSIX_C_SOURCE=200809L", "-Wall", "-Wextra", "-O2", "-Ilib/frecli"]
|
cflags = ["-D_POSIX_C_SOURCE=200809L", "-Wall", "-Wextra", "-O2", "-Ilib/frecli"]
|
||||||
ldflags = ["-Llib/frecli", "-lfrecli", "-lm"]
|
ldflags = ["-Llib/frecli", "-lfrecli", "-lm"]
|
||||||
@@ -31,6 +33,7 @@ frecli = { path = "./lib/frecli", include = ["."], lib = ["."], libs = ["frecli"
|
|||||||
- `language` (string, required): `c`, `c++`, `cpp`, or `cxx`
|
- `language` (string, required): `c`, `c++`, `cpp`, or `cxx`
|
||||||
- `standard` (string, required): e.g. `c99`, `c11`, `c++17`
|
- `standard` (string, required): e.g. `c99`, `c11`, `c++17`
|
||||||
- `compiler` (string, required): `gcc`, `clang`, `cl`(msvc), or `clang-cl`
|
- `compiler` (string, required): `gcc`, `clang`, `cl`(msvc), or `clang-cl`
|
||||||
|
- `target` (string, optional): output directory for the binary (profile-independent)
|
||||||
- `cflags` (string[], optional): additional compile flags
|
- `cflags` (string[], optional): additional compile flags
|
||||||
- `ldflags` (string[], optional): additional link flags
|
- `ldflags` (string[], optional): additional link flags
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ You can add custom compile and link flags in `[build]`:
|
|||||||
[build]
|
[build]
|
||||||
compiler = "gcc"
|
compiler = "gcc"
|
||||||
standard = "c99"
|
standard = "c99"
|
||||||
|
target = "./dist"
|
||||||
cflags = ["-D_POSIX_C_SOURCE=200809L", "-Wall", "-Wextra", "-O2", "-Ilib/frecli"]
|
cflags = ["-D_POSIX_C_SOURCE=200809L", "-Wall", "-Wextra", "-O2", "-Ilib/frecli"]
|
||||||
ldflags = ["-Llib/frecli", "-lfrecli", "-lm"]
|
ldflags = ["-Llib/frecli", "-lfrecli", "-lm"]
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ hello/
|
|||||||
- `--release` — встроенные флаги для каждого компилятора
|
- `--release` — встроенные флаги для каждого компилятора
|
||||||
|
|
||||||
Кастомные флаги можно добавить через `build.cflags` и `build.ldflags` в `dcr.toml`.
|
Кастомные флаги можно добавить через `build.cflags` и `build.ldflags` в `dcr.toml`.
|
||||||
|
Можно указать `build.target`, чтобы задать директорию выхода бинарника (без привязки к профилю).
|
||||||
|
|
||||||
## Конфигурация
|
## Конфигурация
|
||||||
Основной файл проекта — `dcr.toml`.
|
Основной файл проекта — `dcr.toml`.
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ pub fn build(args: &[String]) -> i32 {
|
|||||||
let project_compiler = get_config_str(&config, "build.compiler");
|
let project_compiler = get_config_str(&config, "build.compiler");
|
||||||
let build_language = get_config_str(&config, "build.language");
|
let build_language = get_config_str(&config, "build.language");
|
||||||
let build_standard = get_config_str(&config, "build.standard");
|
let build_standard = get_config_str(&config, "build.standard");
|
||||||
|
let build_target = get_config_str(&config, "build.target");
|
||||||
let build_cflags = match get_config_list(&config, "build.cflags") {
|
let build_cflags = match get_config_list(&config, "build.cflags") {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
@@ -52,7 +53,7 @@ pub fn build(args: &[String]) -> i32 {
|
|||||||
colored(&project_compiler, BOLD_GREEN)
|
colored(&project_compiler, BOLD_GREEN)
|
||||||
);
|
);
|
||||||
|
|
||||||
ensure_target_dirs(&items, &active_profile);
|
ensure_target_dirs(&items, &active_profile, normalize_target(&build_target));
|
||||||
|
|
||||||
let project_root = match std::env::current_dir() {
|
let project_root = match std::env::current_dir() {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
@@ -74,6 +75,7 @@ pub fn build(args: &[String]) -> i32 {
|
|||||||
compiler: &project_compiler,
|
compiler: &project_compiler,
|
||||||
language: &build_language,
|
language: &build_language,
|
||||||
standard: &build_standard,
|
standard: &build_standard,
|
||||||
|
target_dir: normalize_target(&build_target),
|
||||||
include_dirs: &resolved.include_dirs,
|
include_dirs: &resolved.include_dirs,
|
||||||
lib_dirs: &resolved.lib_dirs,
|
lib_dirs: &resolved.lib_dirs,
|
||||||
libs: &resolved.libs,
|
libs: &resolved.libs,
|
||||||
@@ -138,7 +140,11 @@ fn get_config_list(config: &Config, key: &str) -> Result<Vec<String>, String> {
|
|||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_target_dirs(items: &[String], profile: &str) {
|
fn ensure_target_dirs(items: &[String], profile: &str, target_dir: Option<&str>) {
|
||||||
|
if let Some(dir) = target_dir {
|
||||||
|
let _ = fs::create_dir_all(dir);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if !items.contains(&"target".to_string()) {
|
if !items.contains(&"target".to_string()) {
|
||||||
let _ = fs::create_dir("./target");
|
let _ = fs::create_dir("./target");
|
||||||
}
|
}
|
||||||
@@ -162,3 +168,12 @@ fn run_build(ctx: &BuildContext) -> Result<f64, String> {
|
|||||||
Err(_) => Err("Build failed".to_string()),
|
Err(_) => Err("Build failed".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn normalize_target(target: &str) -> Option<&str> {
|
||||||
|
let trimmed = target.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(trimmed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
use crate::cli::build::build;
|
use crate::cli::build::build;
|
||||||
use crate::config::{PROFILE, flags};
|
use crate::config::{PROFILE, flags};
|
||||||
use crate::core::config::Config;
|
use crate::core::config::Config;
|
||||||
|
use crate::platform;
|
||||||
use crate::utils::fs::check_dir;
|
use crate::utils::fs::check_dir;
|
||||||
use crate::utils::log::{error, warn};
|
use crate::utils::log::{error, warn};
|
||||||
use crate::utils::text::{BOLD_GREEN, colored};
|
use crate::utils::text::{BOLD_GREEN, colored};
|
||||||
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
pub fn run(args: &[String]) -> i32 {
|
pub fn run(args: &[String]) -> i32 {
|
||||||
@@ -26,6 +28,11 @@ pub fn run(args: &[String]) -> i32 {
|
|||||||
.get("package.name")
|
.get("package.name")
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("");
|
.unwrap_or("");
|
||||||
|
let target_dir = config
|
||||||
|
.get("build.target")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty());
|
||||||
|
|
||||||
if let Some(first_arg) = args.first() {
|
if let Some(first_arg) = args.first() {
|
||||||
if first_arg.starts_with("--") {
|
if first_arg.starts_with("--") {
|
||||||
@@ -43,24 +50,34 @@ pub fn run(args: &[String]) -> i32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let build_status = build(args);
|
let build_status = build(args);
|
||||||
|
let bin_path = platform::bin_path(&active_profile, project_name, target_dir);
|
||||||
if build_status == 0 {
|
if build_status == 0 {
|
||||||
println!(
|
println!("\n {} {}", colored("Running", BOLD_GREEN), bin_path);
|
||||||
"\n {} target/{active_profile}/{project_name}",
|
|
||||||
colored("Running", BOLD_GREEN)
|
|
||||||
);
|
|
||||||
println!("--------------------------------");
|
println!("--------------------------------");
|
||||||
return Command::new(format!("./target/{active_profile}/{project_name}"))
|
return Command::new(bin_path)
|
||||||
.status()
|
.status()
|
||||||
.map(|status| status.code().unwrap_or(1))
|
.map(|status| status.code().unwrap_or(1))
|
||||||
.unwrap_or(1);
|
.unwrap_or(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if check_dir(Some(&format!("target/{active_profile}")))
|
if target_dir.is_none()
|
||||||
.unwrap_or_default()
|
&& check_dir(Some(&format!("target/{active_profile}")))
|
||||||
.contains(&active_profile)
|
.unwrap_or_default()
|
||||||
|
.contains(&active_profile)
|
||||||
{
|
{
|
||||||
warn("Launch of the latest release");
|
warn("Launch of the latest release");
|
||||||
return Command::new(format!("./target/{active_profile}/{project_name}"))
|
return Command::new(platform::bin_path(
|
||||||
|
&active_profile,
|
||||||
|
project_name,
|
||||||
|
target_dir,
|
||||||
|
))
|
||||||
|
.status()
|
||||||
|
.map(|status| status.code().unwrap_or(1))
|
||||||
|
.unwrap_or(1);
|
||||||
|
}
|
||||||
|
if target_dir.is_some() && Path::new(&bin_path).exists() {
|
||||||
|
warn("Launch of the latest release");
|
||||||
|
return Command::new(bin_path)
|
||||||
.status()
|
.status()
|
||||||
.map(|status| status.code().unwrap_or(1))
|
.map(|status| status.code().unwrap_or(1))
|
||||||
.unwrap_or(1);
|
.unwrap_or(1);
|
||||||
|
|||||||
@@ -37,8 +37,11 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
|
|||||||
for flag in ctx.ldflags {
|
for flag in ctx.ldflags {
|
||||||
cmd.arg(flag);
|
cmd.arg(flag);
|
||||||
}
|
}
|
||||||
cmd.arg("-o")
|
cmd.arg("-o").arg(platform::bin_path(
|
||||||
.arg(platform::bin_path(ctx.profile, ctx.project_name));
|
ctx.profile,
|
||||||
|
ctx.project_name,
|
||||||
|
ctx.target_dir,
|
||||||
|
));
|
||||||
|
|
||||||
match cmd.status() {
|
match cmd.status() {
|
||||||
Ok(status) if status.success() => {
|
Ok(status) if status.success() => {
|
||||||
|
|||||||
@@ -37,8 +37,11 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
|
|||||||
for flag in ctx.ldflags {
|
for flag in ctx.ldflags {
|
||||||
cmd.arg(flag);
|
cmd.arg(flag);
|
||||||
}
|
}
|
||||||
cmd.arg("-o")
|
cmd.arg("-o").arg(platform::bin_path(
|
||||||
.arg(platform::bin_path(ctx.profile, ctx.project_name));
|
ctx.profile,
|
||||||
|
ctx.project_name,
|
||||||
|
ctx.target_dir,
|
||||||
|
));
|
||||||
|
|
||||||
match cmd.status() {
|
match cmd.status() {
|
||||||
Ok(status) if status.success() => {
|
Ok(status) if status.success() => {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ pub struct BuildContext<'a> {
|
|||||||
pub compiler: &'a str,
|
pub compiler: &'a str,
|
||||||
pub language: &'a str,
|
pub language: &'a str,
|
||||||
pub standard: &'a str,
|
pub standard: &'a str,
|
||||||
|
pub target_dir: Option<&'a str>,
|
||||||
pub include_dirs: &'a [String],
|
pub include_dirs: &'a [String],
|
||||||
pub lib_dirs: &'a [String],
|
pub lib_dirs: &'a [String],
|
||||||
pub libs: &'a [String],
|
pub libs: &'a [String],
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
|
|||||||
}
|
}
|
||||||
cmd.arg(format!(
|
cmd.arg(format!(
|
||||||
"/Fe:{}",
|
"/Fe:{}",
|
||||||
platform::bin_path(ctx.profile, ctx.project_name)
|
platform::bin_path(ctx.profile, ctx.project_name, ctx.target_dir)
|
||||||
));
|
));
|
||||||
|
|
||||||
match cmd.status() {
|
match cmd.status() {
|
||||||
|
|||||||
@@ -216,16 +216,19 @@ fn format_toml(value: &Value) -> Result<String, ConfigError> {
|
|||||||
out.push_str("[build]\n");
|
out.push_str("[build]\n");
|
||||||
out.push_str(&format!("language = \"{language}\"\n"));
|
out.push_str(&format!("language = \"{language}\"\n"));
|
||||||
out.push_str(&format!("standard = \"{standard}\"\n"));
|
out.push_str(&format!("standard = \"{standard}\"\n"));
|
||||||
out.push_str(&format!("compiler = \"{compiler}\"\n\n"));
|
out.push_str(&format!("compiler = \"{compiler}\"\n"));
|
||||||
|
if let Some(target) = build.get("target").and_then(|v| v.as_str()) {
|
||||||
|
if !target.trim().is_empty() {
|
||||||
|
out.push_str(&format!("target = \"{target}\"\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Some(cflags) = build.get("cflags") {
|
if let Some(cflags) = build.get("cflags") {
|
||||||
out.push_str(&format!("cflags = {}\n", format_string_array(cflags)));
|
out.push_str(&format!("cflags = {}\n", format_string_array(cflags)));
|
||||||
}
|
}
|
||||||
if let Some(ldflags) = build.get("ldflags") {
|
if let Some(ldflags) = build.get("ldflags") {
|
||||||
out.push_str(&format!("ldflags = {}\n", format_string_array(ldflags)));
|
out.push_str(&format!("ldflags = {}\n", format_string_array(ldflags)));
|
||||||
}
|
}
|
||||||
if build.contains_key("cflags") || build.contains_key("ldflags") {
|
out.push('\n');
|
||||||
out.push('\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
out.push_str("[dependencies]\n");
|
out.push_str("[dependencies]\n");
|
||||||
if !deps.is_empty() {
|
if !deps.is_empty() {
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
pub fn bin_path(profile: &str, name: &str) -> String {
|
pub fn bin_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
|
||||||
format!("./target/{profile}/{name}")
|
match target_dir {
|
||||||
|
Some(dir) => format!("{}/{}", dir.trim_end_matches('/'), name),
|
||||||
|
None => format!("./target/{profile}/{name}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
pub fn bin_path(profile: &str, name: &str) -> String {
|
pub fn bin_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
|
||||||
format!("./target/{profile}/{name}")
|
match target_dir {
|
||||||
|
Some(dir) => format!("{}/{}", dir.trim_end_matches('/'), name),
|
||||||
|
None => format!("./target/{profile}/{name}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,21 +5,24 @@ pub mod macos;
|
|||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
pub mod windows;
|
pub mod windows;
|
||||||
|
|
||||||
pub fn bin_path(profile: &str, name: &str) -> String {
|
pub fn bin_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
linux::bin_path(profile, name)
|
linux::bin_path(profile, name, target_dir)
|
||||||
}
|
}
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
{
|
{
|
||||||
return macos::bin_path(profile, name);
|
return macos::bin_path(profile, name, target_dir);
|
||||||
}
|
}
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
{
|
{
|
||||||
return windows::bin_path(profile, name);
|
return windows::bin_path(profile, name, target_dir);
|
||||||
}
|
}
|
||||||
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
|
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
|
||||||
{
|
{
|
||||||
format!("./target/{profile}/{name}")
|
match target_dir {
|
||||||
|
Some(dir) => format!("{}/{}", dir.trim_end_matches('/'), name),
|
||||||
|
None => format!("./target/{profile}/{name}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
pub fn bin_path(profile: &str, name: &str) -> String {
|
pub fn bin_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
|
||||||
format!("./target/{profile}/{name}.exe")
|
match target_dir {
|
||||||
|
Some(dir) => format!("{}/{}.exe", dir.trim_end_matches('/'), name),
|
||||||
|
None => format!("./target/{profile}/{name}.exe"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user