9 Commits

Author SHA1 Message Date
4e3601b1df Changed: openssl dependency is now conditional (cfg(not(windows))) — no longer pulled in on Windows, fixing Windows CI builds that lack Perl Locale::Maketext::Simple. On Linux (including musl cross-compilation) the vendored OpenSSL build is unchanged. 2026-05-26 17:57:37 +08:00
7969209eae Added:
- `build.kind = "efi"` — UEFI PE32+ executable support. Links with `-shared -nostdlib -Wl,-dll -Wl,--subsystem,10`, output via `efi_path()` with `.efi` extension, rejected by `dcr run`.
- `build.kind = "elf"` — bare-metal ELF executable support. Uses `elf_path()` output path, rejected by `dcr run`, no extra linker flags.

Fixed:

- `dcr run` with `--target` or `build.target` now correctly finds the binary in the arch-specific target directory.
- `dcr run` checks profile-specific `build.{profile}.kind` before rejecting library builds (previously only checked `build.kind`).
- `dcr clean` now reads `build.target` from `dcr.toml` when no `--target` flag is given.
- Linux: target directory is now consistent — `target/<arch>-unknown-linux-gnu/<profile>/` (no stale `target/<profile>/`).
- `dcr.lock` is now populated with all resolved dependencies (registry, path, git) instead of being always empty.
- Git dependencies are now properly recognized and recorded in `dcr.lock`.
- `is_registry_dep` correctly identifies version-based registry dependency tables (`{ version = "..." }`) and rejects tables with unknown keys.
- `dcr test` accepts `--debug`/`--release` flags (defaults to `debug` profile instead of always `release`).
- Build fingerprint cache (`build_cache_path`) now includes `target_dir` — different targets no longer share a single cache entry.
- `object_path` no longer hardcodes the `src/` prefix — works correctly with custom `build.roots` (e.g. `roots = ["lib"]`).
- `dcr update` now generates `.exe` candidates for all Windows targets, not just `x86_64-pc-windows-msvc`.
- Compiler existence is verified before starting the build (clear error if compiler is not found in PATH).
- Consolidated duplicate `OUTPUT_MUTEX` definitions — MSVC backend now uses the shared mutex from `common.rs`.
- Registry cache root directory is created if missing (prevents cryptic errors when `~/.dcr/` does not exist).
2026-05-26 16:38:23 +08:00
adfa24f8f4 feat: add AGENTS.md for quick start and CI guidelines; update documentation for dcr add and dcr build commands 2026-05-22 15:14:56 +08:00
ef2fb1e8e4 feat: update version to 0.6.7, enhance build configuration, and add dcr tree command 2026-05-22 14:37:45 +08:00
9ad968374e fix: target system
add: new field in dcr.toml - filename, extension
2026-05-21 00:02:24 +08:00
c4a49cb345 feat: add openssl dependency and update git2 configuration 2026-05-18 16:58:44 +08:00
527a789410 fix: update git2 dependency to use vendored-openssl 2026-05-18 16:46:11 +08:00
6b9e731c7b fix: update git2 dependency to disable default features and enable https 2026-05-18 16:41:01 +08:00
b2854db91b refactor: simplify Linux cross-compilation setup and install cargo-zigbuild 2026-05-18 16:31:29 +08:00
42 changed files with 788 additions and 236 deletions

View File

@@ -112,27 +112,11 @@ jobs:
name: release-notes name: release-notes
path: .github path: .github
- name: Install apt packages - name: Install cargo-zigbuild (Linux cross-compilation)
if: runner.os == 'Linux' && matrix.apt != '' if: runner.os == 'Linux'
run: | run: |
sudo apt-get update pip install cargo-zigbuild
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu musl-tools echo "$HOME/.local/bin" >> $GITHUB_PATH
case "${{ matrix.target }}" in
aarch64-unknown-linux-gnu)
echo "CC_aarch64-unknown-linux-gnu=aarch64-linux-gnu-gcc" >> $GITHUB_ENV
echo "AR_aarch64-unknown-linux-gnu=aarch64-linux-gnu-ar" >> $GITHUB_ENV
;;
x86_64-unknown-linux-musl)
echo "CC_x86_64-unknown-linux-musl=x86_64-linux-musl-gcc" >> $GITHUB_ENV
;;
aarch64-unknown-linux-musl)
curl -L https://musl.cc/aarch64-linux-musl-cross.tgz -o /tmp/musl-aarch64.tgz
sudo tar xzf /tmp/musl-aarch64.tgz -C /opt
echo "/opt/aarch64-linux-musl-cross/bin" >> $GITHUB_PATH
echo "CC_aarch64-unknown-linux-musl=aarch64-linux-musl-gcc" >> $GITHUB_ENV
echo "AR_aarch64-unknown-linux-musl=aarch64-linux-musl-ar" >> $GITHUB_ENV
;;
esac
- name: Setup LLVM + MinGW (gnullvm) - name: Setup LLVM + MinGW (gnullvm)
if: contains(matrix.target, 'gnullvm') if: contains(matrix.target, 'gnullvm')
@@ -148,17 +132,12 @@ jobs:
- name: Build - name: Build
shell: bash shell: bash
env:
MATRIX_ENV_CC: ${{ matrix.env_cc }}
MATRIX_ENV_LINKER: ${{ matrix.env_linker }}
run: | run: |
if [[ -n "${MATRIX_ENV_CC:-}" ]]; then if [ "${{ runner.os }}" = "Linux" ] && [ "${{ matrix.target }}" != "x86_64-unknown-linux-gnu" ]; then
export "${MATRIX_ENV_CC?}" cargo zigbuild --release --target ${{ matrix.target }}
else
cargo build --release --target ${{ matrix.target }}
fi fi
if [[ -n "${MATRIX_ENV_LINKER:-}" ]]; then
export "${MATRIX_ENV_LINKER?}"
fi
cargo build --release --target ${{ matrix.target }}
- name: Derive version string - name: Derive version string
shell: bash shell: bash

4
.gitignore vendored
View File

@@ -2,4 +2,6 @@ target/
sandbox/** sandbox/**
.idea/ .idea/
.vscode/ .vscode/
.env .env
personal/

View File

@@ -1,77 +0,0 @@
# AGENT.md
## Project Overview
**DCR (Dexoron Cargo Realization)** — CLI tool for managing C/C++/ASM projects in a Cargo-style.
- **Implementation Language**: Rust (edition 2024)
- **License**: MIT
- **Repository**: [github.com/dexoron/dcr](https://github.com/dexoron/dcr)
## Architecture
### Source Tree (`src/`)
```
src/
├── main.rs # Entry point, CLI command routing
├── config.rs # Global constants
├── cli/ # CLI command implementation
│ ├── new.rs # dcr new <name>
│ ├── init.rs # dcr init
│ ├── setup.rs # dcr setup (registry initialization)
│ ├── add.rs # dcr add <name>
│ ├── build.rs # dcr build
│ ├── run.rs # dcr run
│ ├── clean.rs # dcr clean
│ ├── help.rs # dcr --help
│ └── flag_update.rs # dcr --update
├── core/ # Business logic
│ ├── config.rs # dcr.toml parsing
│ ├── registry.rs # Package index management (RegistryManager)
│ ├── deps/ # Dependency resolution (path, git)
│ ├── runner.rs # Binary execution
│ ├── workspace.rs # [workspace] support
│ └── builder/ # Compiler backends (gcc, clang, msvc, nasm, gas)
├── platform/ # Platform-specific logic
└── utils/ # Helper utilities
```
### Key Concepts
- **`~/.dcr/`**: Centralized storage for registries and configurations.
- **`config.toml`**: File in `~/.dcr/` defining available package indices and their priorities.
- **`dcr.toml`**: Local project configuration.
- **Registry System**: `RegistryManager` handles package discovery across multiple indices based on priority.
## Supported Platforms
| Platform | Target triple |
|---|---|
| Linux | `x86_64-unknown-linux-gnu` |
| macOS Intel | `x86_64-apple-darwin` |
| macOS ARM | `aarch64-apple-darwin` |
| Windows | `x86_64-pc-windows-msvc` |
## Dependencies (Cargo)
| Crate | Purpose |
|---|---|
| `reqwest` | HTTP requests |
| `serde` | Serialization |
| `toml` | Configuration parsing |
| `git2` | Git dependency handling |
## Development
### Build & Run
```bash
cargo build --release
./target/release/dcr setup
```
## Code Conventions
- Rust stable edition 2024.
- `cargo fmt` and `cargo clippy -- -D warnings`.
- CLI output in English.
- Use `utils::log` for messaging.

56
AGENTS.md Normal file
View File

@@ -0,0 +1,56 @@
# AGENTS.md
## Quick start
```bash
cargo build --release && ./target/release/dcr setup
```
## Pre-PR (CI) gate
```bash
cargo fmt --all --check
cargo clippy --all-targets --all-features -- -D warnings
cargo check --all-targets --all-features
cargo test
```
CI (`.github/workflows/ci.yml`) runs fmt, clippy, check — **not** tests.
## Tests
- Integration tests in `tests/` build the dcr binary and run it in temp dirs under `sandbox/cli-tests/` (gitignored).
- Some tests **skip** if no C compiler (`gcc`/`clang`/`cc`) is found at runtime.
- Compiler can be overridden via `DCR_COMPILER` or `DCR_CC` env vars.
- Tests use `$CARGO_BIN_EXE_dcr` when available, else `target/debug/dcr`.
## Architecture
- **Single Rust package** (edition 2024), not a workspace. Only one binary: `dcr`.
- **Entrypoint**: `src/main.rs` — matches `args[1]` against command names.
- **Compiler backends** in `src/core/builder/`: `unix_cc` (gcc/clang), `msvc` (cl/clang-cl), `gas`, `nasm`. Dispatched by matching compiler name string.
- **Config**: `dcr.toml` (per-project), `~/.dcr/config.toml` (registry), `~/.dcr/index.json` (index). Defaults: `language=c`, `standard=c11`, `compiler=clang`, `kind=bin`.
- **`build.rs`** embeds `TARGET` as `DCR_TARGET` env var (shown in `--version`).
- **CLI output** must be in English. Use `utils::log` for messaging.
## Key commands
| Command | Notes |
|---|---|
| `dcr new <name>` | Creates project dir |
| `dcr init` | Init from CWD (must be empty) |
| `dcr build [--debug\|--release] [--target <triple>]` | Default profile: `--debug` |
| `dcr run [--debug\|--release]` | Only for `kind = "bin"` |
| `dcr clean [--debug\|--release] [--all]` | `--all` for workspace roots |
| `dcr tree` | Dependency tree viewer |
| `dcr test [--init]` | Runs/test project tests |
| `dcr gen <vscode\|clion\|compile-commands>` | IDE integration |
| `dcr --update` | Self-update from GitHub Releases |
## Cross-compilation
`--target` accepts short names (`linux`, `macos`, `windows`) or full triples. In CI, Linux cross-builds use `cargo-zigbuild`. When `build.target` is set in `dcr.toml`, DCR auto-injects `--target=<value>` into cflags (clang). Bare-metal targets (containing `none`/`-elf`/`eabi`/`baremetal`) skip DCR's default compiler flags.
## Dependencies (Cargo)
reqwest (blocking+json+rustls-tls), serde, toml, git2, openssl (vendored), self-replace, sha2, ctrlc.

View File

@@ -1,5 +1,47 @@
# Changelog # Changelog
## [0.6.7] - 2026-05-26
Added:
- `build.kind = "efi"` — UEFI PE32+ executable support. Links with `-shared -nostdlib -Wl,-dll -Wl,--subsystem,10`, output via `efi_path()` with `.efi` extension, rejected by `dcr run`.
- `build.kind = "elf"` — bare-metal ELF executable support. Uses `elf_path()` output path, rejected by `dcr run`, no extra linker flags.
- `build.filename` and `build.extension` in `dcr.toml` — complete control over the final artifact name without relying on `package.name`.
Example:
```toml
[build]
filename = "KERNEL"
extension = "EFI"
```
Produces `KERNEL.EFI` (works for `bin`, `staticlib`, and `sharedlib`).
- Automatic injection of `--target=<build.target>` into compiler flags when `build.target` is set in `dcr.toml`. This greatly simplifies clang-based cross-compilation (especially for bare-metal targets like `aarch64-none-elf`).
- Bare-metal targets (containing `none`, `-elf`, `eabi`, `baremetal`) no longer receive DCR's internal default flags (`-g`, `-Wall`, `-Wextra`, `-fno-omit-frame-pointer`, `-DDCR_DEBUG`, etc.). Prevents unwanted sections (`.comment`, debug info, etc.) that break custom linker scripts when `inherit = true`.
- `build.ldscript` in `dcr.toml` — linker script path passed as `-T <path>` to the linker. Essential for bare-metal/embedded/freestanding targets.
- `dcr build --verbose` — prints compiler/linker command lines (also works with `DCR_DEBUG` env var).
- `dcr add <name>` without a source argument — if a registry is configured, DCR looks up the package by name and adds it as a version dependency.
Fixed:
- `build.target` declared in `dcr.toml` is now correctly used as the default target even when `--target` is not passed on the command line (previously the host target was always forced for config resolution).
- `dcr run` with `--target` or `build.target` now correctly finds the binary in the arch-specific target directory.
- `dcr run` checks profile-specific `build.{profile}.kind` before rejecting library builds (previously only checked `build.kind`).
- `dcr clean` now reads `build.target` from `dcr.toml` when no `--target` flag is given.
- Linux: target directory is now consistent — `target/<arch>-unknown-linux-gnu/<profile>/` (no stale `target/<profile>/`).
- `dcr.lock` is now populated with all resolved dependencies (registry, path, git) instead of being always empty.
- Git dependencies are now properly recognized and recorded in `dcr.lock`.
- `is_registry_dep` correctly identifies version-based registry dependency tables (`{ version = "..." }`) and rejects tables with unknown keys.
- `dcr test` accepts `--debug`/`--release` flags (defaults to `debug` profile instead of always `release`).
- Build fingerprint cache (`build_cache_path`) now includes `target_dir` — different targets no longer share a single cache entry.
- `object_path` no longer hardcodes the `src/` prefix — works correctly with custom `build.roots` (e.g. `roots = ["lib"]`).
- `dcr update` now generates `.exe` candidates for all Windows targets, not just `x86_64-pc-windows-msvc`.
- Compiler existence is verified before starting the build (clear error if compiler is not found in PATH).
- Consolidated duplicate `OUTPUT_MUTEX` definitions — MSVC backend now uses the shared mutex from `common.rs`.
- Registry cache root directory is created if missing (prevents cryptic errors when `~/.dcr/` does not exist).
Changed:
- `openssl` dependency is now conditional (`cfg(not(windows))`) — no longer pulled in on Windows, fixing Windows CI builds that lack Perl `Locale::Maketext::Simple`. On Linux (including musl cross-compilation) the vendored OpenSSL build is unchanged.
## [0.6.6] - 2026-05-18 ## [0.6.6] - 2026-05-18
Fixed: Fixed:
@@ -29,8 +71,9 @@ between `cli/build.rs`, `cli/run.rs`, `cli/clean.rs`, `cli/gen.rs`
`package_root_from_registry_info`, `registry_include_dir`, `registry_lib_dir`, `package_root_from_registry_info`, `registry_include_dir`, `registry_lib_dir`,
`path_from_string_dep` `path_from_string_dep`
- Unit tests for `register.rs` (`is_registry_dep`, `package_root_from_registry_info`), - Unit tests for `register.rs` (`is_registry_dep`, `package_root_from_registry_info`),
`deps/mod.rs` (`path_dep_path`, `push_default_lib_dirs`), `deps/mod.rs` (`path_dep_path`, `push_default_lib_dirs`),
`utils/build.rs` (`normalize_target_os`, `parse_version_info`) `utils/build.rs` (`normalize_target_os`, `parse_version_info`)
- `dcr tree` command — visual dependency tree viewer (similar to `cargo tree`)
Removed: Removed:

53
Cargo.lock generated
View File

@@ -112,11 +112,12 @@ dependencies = [
[[package]] [[package]]
name = "dcr" name = "dcr"
version = "0.6.6" version = "0.6.7"
dependencies = [ dependencies = [
"ctrlc", "ctrlc",
"git2", "git2",
"glob", "glob",
"openssl",
"reqwest", "reqwest",
"self-replace", "self-replace",
"serde", "serde",
@@ -192,6 +193,21 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "foreign-types"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [
"foreign-types-shared",
]
[[package]]
name = "foreign-types-shared"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]] [[package]]
name = "form_urlencoded" name = "form_urlencoded"
version = "1.2.2" version = "1.2.2"
@@ -730,12 +746,46 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "openssl"
version = "0.10.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf0b434746ee2832f4f0baf10137e1cabb18cbe6912c69e2e33263c45250f542"
dependencies = [
"bitflags",
"cfg-if",
"foreign-types",
"libc",
"openssl-macros",
"openssl-sys",
]
[[package]]
name = "openssl-macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "openssl-probe" name = "openssl-probe"
version = "0.1.6" version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
[[package]]
name = "openssl-src"
version = "300.6.0+3.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8e8cbfd3a4a8c8f089147fd7aaa33cf8c7450c4d09f8f80698a0cf093abeff4"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "openssl-sys" name = "openssl-sys"
version = "0.9.115" version = "0.9.115"
@@ -744,6 +794,7 @@ checksum = "158fe5b292746440aa6e7a7e690e55aeb72d41505e2804c23c6973ad0e9c9781"
dependencies = [ dependencies = [
"cc", "cc",
"libc", "libc",
"openssl-src",
"pkg-config", "pkg-config",
"vcpkg", "vcpkg",
] ]

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "dcr" name = "dcr"
version = "0.6.6" version = "0.6.7"
edition = "2024" edition = "2024"
build = "build.rs" build = "build.rs"
@@ -14,3 +14,6 @@ toml = "1.0.3"
git2 = "0.20.4" git2 = "0.20.4"
serde_json = "1" serde_json = "1"
ctrlc = "3" ctrlc = "3"
[target.'cfg(not(windows))'.dependencies]
openssl = { version = "0.10", features = ["vendored"] }

View File

@@ -16,7 +16,7 @@ The current implementation is written in Rust.
- Clean build artifacts - Clean build artifacts
- Generate IDE integration files (VS Code, CLion) and compilation databases - Generate IDE integration files (VS Code, CLion) and compilation databases
- Generate a minimal C project template - Generate a minimal C project template
- Build static and shared libraries - Build static, shared libraries, UEFI PE32+ executables, and bare-metal ELF executables
- Dependency management: - Dependency management:
- Git and Path dependencies with `dcr.lock` - Git and Path dependencies with `dcr.lock`
- Registry-based dependency resolution - Registry-based dependency resolution
@@ -154,7 +154,9 @@ You can also set `build.target` to override the output directory (profile-indepe
You can set `build.platform` to pass `-march=<platform>` (GCC/Clang) or `/arch:*` (MSVC) where supported. You can set `build.platform` to pass `-march=<platform>` (GCC/Clang) or `/arch:*` (MSVC) where supported.
Use `build.kind = "staticlib"` to build a static library instead of a binary. Use `build.kind = "staticlib"` to build a static library instead of a binary.
Use `build.kind = "sharedlib"` to build a shared library (`.so`/`.dylib`/`.dll`). Use `build.kind = "sharedlib"` to build a shared library (`.so`/`.dylib`/`.dll`).
`dcr run` is only for `build.kind = "bin"` and will fail for libraries. Use `build.kind = "efi"` to build a UEFI PE32+ executable (`.efi`).
Use `build.kind = "elf"` to build a bare-metal ELF executable.
`dcr run` is only for `build.kind = "bin"` and will fail for libraries, EFI, and ELF.
Use `build.exclude`/`build.include` to control source/header collection. Use `build.exclude`/`build.include` to control source/header collection.
Use `build.roots` and `build.src_disable` to replace the default `src/` root. Use `build.roots` and `build.src_disable` to replace the default `src/` root.
Use `build.steps` and `build.post_steps` to run custom commands. Use `build.steps` and `build.post_steps` to run custom commands.
@@ -203,7 +205,7 @@ type = "app" # or "lib", "none"
language = "c" language = "c"
standard = "c11" standard = "c11"
compiler = "clang" compiler = "clang"
kind = "bin" # "bin", "staticlib", "sharedlib" kind = "bin" # "bin", "staticlib", "sharedlib", "efi", "elf"
# Optional platform hint # Optional platform hint
# platform = "x86_64" # platform = "x86_64"
# Optional custom flags # Optional custom flags

View File

@@ -27,6 +27,7 @@
* [dcr clean](commands/dcr-clean.md) * [dcr clean](commands/dcr-clean.md)
* [dcr gen](commands/dcr-gen.md) * [dcr gen](commands/dcr-gen.md)
* [dcr test](commands/dcr-test.md) * [dcr test](commands/dcr-test.md)
* [dcr tree](commands/dcr-tree.md)
## Dependencies & Build ## Dependencies & Build

View File

@@ -5,11 +5,15 @@ Add a dependency to `dcr.toml`.
## Usage ## Usage
```bash ```bash
dcr add <name> <source> [options] dcr add <name> [source] [options]
``` ```
If `<source>` is omitted and a registry is configured, DCR will look up the
package by name in the registry and add it as a version dependency.
## Sources ## Sources
- *omitted*: Look up in the configured registry.
- `path:./path/to/lib`: Local directory. - `path:./path/to/lib`: Local directory.
- `github:user/repo`: GitHub repository. - `github:user/repo`: GitHub repository.
- `gitlab:user/repo`: GitLab repository. - `gitlab:user/repo`: GitLab repository.
@@ -29,4 +33,5 @@ dcr add my_lib path:./libs/my_lib
dcr add cool_lib github:user/cool_lib dcr add cool_lib github:user/cool_lib
dcr add shared_lib gitlab:org/shared --branch develop dcr add shared_lib gitlab:org/shared --branch develop
dcr add custom_lib git:git.internal.com/r/lib --tag v1.0.0 dcr add custom_lib git:git.internal.com/r/lib --tag v1.0.0
dcr add mylib # Looks up in registry
``` ```

View File

@@ -25,6 +25,8 @@ dcr build --debug --clean
## Config values used ## Config values used
- `package.name` - `package.name`
- `build.filename`
- `build.extension`
- `build.compiler` - `build.compiler`
- `build.language` - `build.language`
- `build.standard` - `build.standard`
@@ -33,6 +35,7 @@ dcr build --debug --clean
- `build.platform` - `build.platform`
- `build.cflags` - `build.cflags`
- `build.ldflags` - `build.ldflags`
- `build.ldscript`
- `build.debug` / `build.release` - `build.debug` / `build.release`
- `build.exclude` - `build.exclude`
- `build.include` - `build.include`
@@ -56,11 +59,16 @@ dcr build --debug --clean
- Profile flag (`--debug` / `--release`) can appear in any argument position (duplicates are rejected). - Profile flag (`--debug` / `--release`) can appear in any argument position (duplicates are rejected).
- Unknown profile flags return an error. - Unknown profile flags return an error.
- `--target <triple>` builds for specified target (short names: `linux`, `macos`, `windows`). - `--target <triple>` builds for specified target (short names: `linux`, `macos`, `windows`).
- `--verbose` prints the compiler/linker command lines to stderr (also works via `DCR_DEBUG` env var).
- If no `--target`, builds for `build.targets` or native. - If no `--target`, builds for `build.targets` or native.
- Incremental rebuild checks source/object mtime and tracked header dependencies. - Incremental rebuild checks source/object mtime and tracked header dependencies.
- `--force` skips build cache checks and recompiles. - `--force` skips build cache checks and recompiles.
- `--clean` removes `target/<profile>` and `build.clean` paths before building. - `--clean` removes `target/<profile>` and `build.clean` paths before building.
- `build.filename` + `build.extension` allow full control over the final artifact name (e.g. `KERNEL.BIN` instead of using `package.name`). When `build.kind = "efi"`, the output is a PE32+ executable with `.efi` extension.
- `build.kind = "efi"` links with `-shared -nostdlib -Wl,-dll -Wl,--subsystem,10` for UEFI PE32+ format. Use `build.filename` and `build.extension` for full control over the artifact name.
- `build.kind = "elf"` produces a bare-metal ELF executable. Linking flags are user-controlled via `build.ldflags` (no PIE/CRT defaults are added).
- Default GCC/Clang profile flags: `debug` -> `-O0 -g -Wall -Wextra -fno-omit-frame-pointer -DDCR_DEBUG`, `release` -> `-O3 -DNDEBUG`. - Default GCC/Clang profile flags: `debug` -> `-O0 -g -Wall -Wextra -fno-omit-frame-pointer -DDCR_DEBUG`, `release` -> `-O3 -DNDEBUG`.
These defaults are **not applied** for bare-metal targets (e.g. `aarch64-none-elf`, targets containing `none` / `-elf` / `eabi`).
- For `language = "asm"` with `compiler = "as"`/`"gas"`, use `.s` files (no preprocessing). For `.S`, use `gcc` or `clang`. - For `language = "asm"` with `compiler = "as"`/`"gas"`, use `.s` files (no preprocessing). For `.S`, use `gcc` or `clang`.
- In workspace root, `dcr build` builds all members in dependency order. - In workspace root, `dcr build` builds all members in dependency order.
- `build.exclude` removes paths from source/header collection; `build.include` re-allows matching paths and has priority over `exclude`. - `build.exclude` removes paths from source/header collection; `build.include` re-allows matching paths and has priority over `exclude`.

View File

@@ -15,7 +15,7 @@ dcr clean --release --all
## Behavior ## Behavior
- Without arguments: removes entire `target/`. - Without arguments: removes entire `target/`.
- With profile: removes only `target/<profile>/`. - With profile: removes only the profile-specific directory. Respects `build.target` from config: if `build.target = "aarch64-unknown-linux-gnu"`, cleans `target/aarch64-unknown-linux-gnu/<profile>/`.
- With `--all`: also cleans all workspace members. - With `--all`: also cleans all workspace members.
- Additionally removes paths from `build.clean` if configured. - Additionally removes paths from `build.clean` if configured.
- `build.clean` supports `{profile}` and version placeholders. - `build.clean` supports `{profile}` and version placeholders.

View File

@@ -23,7 +23,7 @@ dcr run --debug --clean
## Restrictions ## Restrictions
- If `build.kind = "staticlib"` or `build.kind = "sharedlib"`, `run` exits with error. - If `build.kind` (or `build.{profile}.kind`) is `"staticlib"` or `"sharedlib"`, `run` exits with error.
- The profile is selected from the first argument. - The profile is selected from the first argument.
- `--target <triple>` runs the binary for specified target (short names: `linux`, `macos`, `windows`). - `--target <triple>` runs the binary for specified target (short names: `linux`, `macos`, `windows`).
- `--force` skips build cache checks and recompiles. - `--force` skips build cache checks and recompiles.

View File

@@ -6,6 +6,7 @@ Runs the project's test suite.
```sh ```sh
dcr test dcr test
dcr test --debug
dcr test --release dcr test --release
dcr test --target linux dcr test --target linux
dcr test --init dcr test --init
@@ -39,6 +40,6 @@ DCR uses a simple test framework:
- Test binary is linked with the main project (for lib projects) or standalone. - Test binary is linked with the main project (for lib projects) or standalone.
- `--init` creates `tests/dcr_test.h` and `tests/test.c` if missing. - `--init` creates `tests/dcr_test.h` and `tests/test.c` if missing.
- Tests run in release mode by default for performance. - Tests run in debug mode by default. Use `--release` for release mode.
- Custom test logic can be added by editing `tests/test.c`. - Custom test logic can be added by editing `tests/test.c`.
- Fails if no `tests/test.c` exists (run `dcr test --init` first). - Fails if no `tests/test.c` exists (run `dcr test --init` first).

22
docs/commands/dcr-tree.md Normal file
View File

@@ -0,0 +1,22 @@
# dcr tree
Displays a visual dependency tree for the current project (similar to `cargo tree`).
## Usage
```sh
dcr tree
dcr tree --invert
dcr tree -p some-package
```
## What it shows
- All direct and transitive dependencies
- Version of each dependency
- Whether a dependency is a registry, git, or path dependency
## Notes
- Useful for debugging dependency issues and understanding the build graph.
- Output format is inspired by `cargo tree`.

View File

@@ -8,6 +8,8 @@ language = "c"
standard = "c11" standard = "c11"
compiler = "clang" compiler = "clang"
kind = "bin" kind = "bin"
# filename = "KERNEL"
# extension = "BIN"
# optional: # optional:
# inherit = true # Inherit from base (default true) # inherit = true # Inherit from base (default true)
# targets = ["linux", "macos"] # Multiple targets to build # targets = ["linux", "macos"] # Multiple targets to build
@@ -31,13 +33,17 @@ kind = "bin"
Mixed languages can be specified as an array, for example `["c", "c++", "asm"]`. Mixed languages can be specified as an array, for example `["c", "c++", "asm"]`.
- `standard` (string, required): language standard passed to compiler. - `standard` (string, required): language standard passed to compiler.
- `compiler` (string, required): compiler command (for example `clang`, `gcc`, `cl`). - `compiler` (string, required): compiler command (for example `clang`, `gcc`, `cl`).
- `kind` (string, required): `bin`, `staticlib`, or `sharedlib`. - `kind` (string, required): `bin`, `staticlib`, `sharedlib`, `efi`, or `elf`.
- `filename` (string, optional): custom output filename (without extension). Overrides `package.name` for the final artifact.
- `extension` (string, optional): custom file extension without the leading dot (e.g. `BIN`, `efi`, `EFI`). Combined with `filename`.
- `inherit` (bool, optional): inherit settings from base `[build]` (default `true`). If `false`, only use target/profile specific settings. - `inherit` (bool, optional): inherit settings from base `[build]` (default `true`). If `false`, only use target/profile specific settings.
- `targets` (string array, optional): list of targets to build simultaneously. Supports short names (`linux`, `macos`, `windows`). - `targets` (string array, optional): list of targets to build simultaneously. Supports short names (`linux`, `macos`, `windows`).
- `target` (string, optional): custom output directory for final artifact. - `target` (string, optional): custom output directory for final artifact.
When set, DCR automatically injects `--target=<value>` into `cflags` (useful for clang cross-compilation).
- `platform` (string, optional): architecture hint for compiler (used for `-march` or `/arch`). - `platform` (string, optional): architecture hint for compiler (used for `-march` or `/arch`).
- `cflags` (string array, optional): extra compile flags. - `cflags` (string array, optional): extra compile flags.
- `ldflags` (string array, optional): extra link flags. - `ldflags` (string array, optional): extra link flags.
- `ldscript` (string, optional): linker script path. Passed as `-T <path>` to the linker. Useful for bare-metal/embedded/freestanding targets.
- `exclude` (string array, optional): paths or glob patterns to skip during source/header collection. - `exclude` (string array, optional): paths or glob patterns to skip during source/header collection.
- `include` (string array, optional): allowlist paths or globs that override `exclude`. - `include` (string array, optional): allowlist paths or globs that override `exclude`.
- `include` directory entries (non-glob) are also passed to the compiler as `-I` include paths. - `include` directory entries (non-glob) are also passed to the compiler as `-I` include paths.
@@ -145,4 +151,4 @@ rcc = "rcc"
- Compiler backend is selected by compiler string (`gcc`, `clang`, `cl`, `clang-cl`). - Compiler backend is selected by compiler string (`gcc`, `clang`, `cl`, `clang-cl`).
- Empty or unknown compiler value falls back to clang-like build path. - Empty or unknown compiler value falls back to clang-like build path.
- `run` is not allowed for library kinds (`staticlib`, `sharedlib`). - `run` is not allowed for library kinds (`staticlib`, `sharedlib`), `efi`, or `elf`.

View File

@@ -178,6 +178,8 @@ compiler = "aarch64-linux-gnu-gcc"' >> dcr.toml
dcr build --target aarch64-linux-gnu --release dcr build --target aarch64-linux-gnu --release
``` ```
**Note:** If you set `target = "aarch64-linux-gnu"` (or any value) directly under `[build]`, DCR will automatically add `--target=...` to cflags when using clang. You usually don't need to repeat it manually.
### Windows Build on Linux ### Windows Build on Linux
```bash ```bash

View File

@@ -49,7 +49,7 @@ dcr clean --release --all
## Notes ## Notes
- `run` is unavailable for `build.kind = "staticlib"` and `build.kind = "sharedlib"`. - `run` is unavailable for `build.kind = "staticlib"`, `build.kind = "sharedlib"`, `build.kind = "efi"`, and `build.kind = "elf"`.
- Build profile flag (`--debug` / `--release`) can be passed in any argument position. - Build profile flag (`--debug` / `--release`) can be passed in any argument position.
- Unknown profile flags produce an error. - Unknown profile flags produce an error.
- In workspace root, `clean --all` cleans all member projects. - In workspace root, `clean --all` cleans all member projects.

View File

@@ -7,9 +7,11 @@ dcr new <name>
dcr init dcr init
dcr setup dcr setup
dcr add <name> <source>(optional) dcr add <name> <source>(optional)
dcr build [--debug|--release] dcr build [--debug|--release] [--target <triple>]
dcr run [--debug|--release] dcr run [--debug|--release]
dcr clean [--debug|--release] [--all] dcr clean [--debug|--release] [--all]
dcr tree
dcr test
dcr gen <subcommand> dcr gen <subcommand>
dcr --help dcr --help
dcr --version dcr --version
@@ -18,11 +20,13 @@ dcr --update
## Notes on argument parsing ## Notes on argument parsing
- `build`/`run` parse flags in any order (`--debug|--release`, `--force`, `--clean`) and reject duplicates. - `build`/`run` parse flags in any order (`--debug|--release`, `--force`, `--clean`, `--verbose`) and reject duplicates.
- `new` requires exactly one argument. - `new` requires exactly one argument.
- `init` and `--update` do not accept extra arguments. - `init` and `--update` do not accept extra arguments.
- `clean` accepts `--debug|--release` and optional `--all`. - `clean` accepts `--debug|--release` and optional `--all`.
- `gen` supports `project-info`, `compile-commands`, `vscode`, `clion`. - `gen` supports `project-info`, `compile-commands`, `vscode`, `clion`.
- `tree` shows a dependency tree (similar to `cargo tree`).
- `test` / `tests` runs the project's tests.
## Exit behavior overview ## Exit behavior overview

View File

@@ -12,10 +12,13 @@ language = "c|c++|cpp|cxx|asm" # required (string or array)
standard = "string" # required standard = "string" # required
compiler = "string" # required compiler = "string" # required
kind = "bin|staticlib|sharedlib" # required kind = "bin|staticlib|sharedlib" # required
filename = "string" # optional (custom output name without extension)
extension = "string" # optional (custom extension without dot, e.g. "BIN")
target = "string" # optional target = "string" # optional
platform = "string" # optional platform = "string" # optional
cflags = ["string", "..."] # optional cflags = ["string", "..."] # optional
ldflags = ["string", "..."] # optional ldflags = ["string", "..."] # optional
ldscript = "string" # optional (linker script, passed as -T<path>)
exclude = ["string", "..."] # optional exclude = ["string", "..."] # optional
include = ["string", "..."] # optional include = ["string", "..."] # optional
roots = ["string", "..."] # optional roots = ["string", "..."] # optional

View File

@@ -1,4 +1,5 @@
use crate::core::config::Config; use crate::core::config::Config;
use crate::core::deps::register;
use crate::utils::fs::find_project_root; use crate::utils::fs::find_project_root;
use crate::utils::log::error; use crate::utils::log::error;
use crate::utils::text::{BOLD_GREEN, colored}; use crate::utils::text::{BOLD_GREEN, colored};
@@ -12,6 +13,7 @@ pub struct AddArgs {
pub branch: Option<String>, pub branch: Option<String>,
pub tag: Option<String>, pub tag: Option<String>,
pub rev: Option<String>, pub rev: Option<String>,
pub version_from_registry: Option<String>,
} }
pub fn add(args: &[String]) -> i32 { pub fn add(args: &[String]) -> i32 {
@@ -71,6 +73,8 @@ pub fn add(args: &[String]) -> i32 {
let mut table = Map::new(); let mut table = Map::new();
table.insert("path".to_string(), Value::String(path)); table.insert("path".to_string(), Value::String(path));
Value::Table(table) Value::Table(table)
} else if let Some(version) = add_args.version_from_registry {
Value::String(version)
} else { } else {
error("Dependency source (path or git) must be provided"); error("Dependency source (path or git) must be provided");
return 1; return 1;
@@ -124,8 +128,38 @@ fn parse_add_args(args: &[String]) -> Result<AddArgs, i32> {
} }
source_spec = s.clone(); source_spec = s.clone();
} else { } else {
error("Dependency source is required"); // No source provided — try registry auto-lookup
return Err(1); match register::resolve_package_from_registry(&name) {
Ok(info) => {
let version = info
.get("latest_version")
.or_else(|| info.get("version"))
.and_then(|v| v.as_str())
.ok_or_else(|| {
(
"Registry entry for `".to_string() + &name + "` has no version field",
1,
)
})
.map_err(|(msg, code)| {
error(&msg);
code
})?;
return Ok(AddArgs {
name,
path: None,
git: None,
branch: None,
tag: None,
rev: None,
version_from_registry: Some(version.to_string()),
});
}
Err(e) => {
error(&format!("Cannot add `{}` without source: {}", name, e));
return Err(1);
}
}
} }
while let Some(arg) = iter.next() { while let Some(arg) = iter.next() {
@@ -197,5 +231,6 @@ fn parse_add_args(args: &[String]) -> Result<AddArgs, i32> {
branch, branch,
tag, tag,
rev, rev,
version_from_registry: None,
}) })
} }

View File

@@ -50,7 +50,15 @@ pub fn build(args: &[String]) -> i32 {
Err(code) => return code, Err(code) => return code,
}; };
// If no target specified, use default host target for target-specific config if flags.target.is_none() {
let config_path = root.join("dcr.toml");
if let Ok(config) = Config::open(config_path.to_str().unwrap()) {
let bt = get_build_string_with_profile(&config, "target", "debug");
if !bt.is_empty() {
flags.target = Some(bt);
}
}
}
if flags.target.is_none() { if flags.target.is_none() {
let default_target = if cfg!(target_os = "linux") { let default_target = if cfg!(target_os = "linux") {
"x86_64-unknown-linux-gnu" "x86_64-unknown-linux-gnu"
@@ -69,7 +77,13 @@ pub fn build(args: &[String]) -> i32 {
let _ = clean(&clean_args); let _ = clean(&clean_args);
} }
match with_dir(&root, || { match with_dir(&root, || {
build_from_root(&root, &flags.profile, flags.target.as_deref(), flags.force) build_from_root(
&root,
&flags.profile,
flags.target.as_deref(),
flags.force,
flags.verbose,
)
}) { }) {
Ok(()) => 0, Ok(()) => 0,
Err(msg) => { Err(msg) => {
@@ -341,9 +355,15 @@ fn ensure_target_dirs(items: &[String], profile: &str, target_dir: Option<String
if let Some(dir) = &target_dir { if let Some(dir) = &target_dir {
let _ = fs::create_dir_all(dir); let _ = fs::create_dir_all(dir);
} else { } else {
let default_dir = if cfg!(target_os = "linux") {
let arch = std::env::consts::ARCH;
format!("{arch}-unknown-linux-gnu/{profile}")
} else {
profile.to_string()
};
let target_items = check_dir(Some("target")).unwrap_or_default(); let target_items = check_dir(Some("target")).unwrap_or_default();
if !target_items.contains(&profile.to_string()) { if !target_items.contains(&default_dir) {
let _ = fs::create_dir(format!("./target/{profile}")); let _ = fs::create_dir_all(format!("./target/{default_dir}"));
} }
} }
} }
@@ -369,6 +389,7 @@ fn build_from_root(
profile: &str, profile: &str,
target: Option<&str>, target: Option<&str>,
force: bool, force: bool,
verbose: bool,
) -> Result<(), String> { ) -> Result<(), String> {
let config = Config::open("./dcr.toml").map_err(|err| err.to_string())?; let config = Config::open("./dcr.toml").map_err(|err| err.to_string())?;
let project_name = get_config_str(&config, "package.name"); let project_name = get_config_str(&config, "package.name");
@@ -410,12 +431,19 @@ fn build_from_root(
); );
} }
if let Some(workspace) = parse_workspace(&config, profile, build_target.as_deref(), root)? { if let Some(workspace) = parse_workspace(&config, profile, build_target.as_deref(), root)? {
build_workspace(&workspace, profile, build_target.as_deref(), force)?; build_workspace(&workspace, profile, build_target.as_deref(), force, verbose)?;
let excludes: Vec<std::path::PathBuf> = let excludes: Vec<std::path::PathBuf> =
workspace.members.iter().map(|m| m.path.clone()).collect(); workspace.members.iter().map(|m| m.path.clone()).collect();
build_project_at(root, profile, build_target.as_deref(), &excludes, force)?; build_project_at(
root,
profile,
build_target.as_deref(),
&excludes,
force,
verbose,
)?;
} else { } else {
build_project_at(root, profile, build_target.as_deref(), &[], force)?; build_project_at(root, profile, build_target.as_deref(), &[], force, verbose)?;
} }
} }
let elapsed = ((start_time.elapsed().as_secs_f64() * 100.0).trunc()) / 100.0; let elapsed = ((start_time.elapsed().as_secs_f64() * 100.0).trunc()) / 100.0;
@@ -432,9 +460,10 @@ fn build_workspace(
profile: &str, profile: &str,
target: Option<&str>, target: Option<&str>,
force: bool, force: bool,
verbose: bool,
) -> Result<(), String> { ) -> Result<(), String> {
for member in &workspace.members { for member in &workspace.members {
build_project_at(&member.path, profile, target, &[], force)?; build_project_at(&member.path, profile, target, &[], force, verbose)?;
} }
Ok(()) Ok(())
} }
@@ -445,6 +474,7 @@ fn build_project_at(
target: Option<&str>, target: Option<&str>,
exclude_dirs: &[std::path::PathBuf], exclude_dirs: &[std::path::PathBuf],
force: bool, force: bool,
verbose: bool,
) -> Result<(), String> { ) -> Result<(), String> {
with_dir(project_root, || { with_dir(project_root, || {
check_interrupted()?; check_interrupted()?;
@@ -490,10 +520,32 @@ fn build_project_at(
.or_else(|| get_config_opt(&config, "toolchain.moc")); .or_else(|| get_config_opt(&config, "toolchain.moc"));
let tc_rcc = get_config_value(&config, "toolchain", "rcc", profile, build_target) let tc_rcc = get_config_value(&config, "toolchain", "rcc", profile, build_target)
.or_else(|| get_config_opt(&config, "toolchain.rcc")); .or_else(|| get_config_opt(&config, "toolchain.rcc"));
let build_cflags = let mut build_cflags =
get_list_with_profile_and_target(&config, "cflags", profile, build_target)?; get_list_with_profile_and_target(&config, "cflags", profile, build_target)?;
let build_ldflags = if let Some(t) = build_target
&& !t.trim().is_empty()
{
let target_flag = format!("--target={}", t.trim());
if !build_cflags
.iter()
.any(|f| f == &target_flag || f.starts_with("--target="))
{
build_cflags.insert(0, target_flag);
}
}
let mut build_ldflags =
get_list_with_profile_and_target(&config, "ldflags", profile, build_target)?; get_list_with_profile_and_target(&config, "ldflags", profile, build_target)?;
let build_ldscript =
get_string_with_profile_and_target(&config, "ldscript", profile, build_target);
if !build_ldscript.is_empty() {
build_ldflags.push(format!("-T{}", build_ldscript));
}
// Новые поля: filename и extension
let output_filename =
get_string_with_profile_and_target(&config, "filename", profile, build_target);
let output_extension =
get_string_with_profile_and_target(&config, "extension", profile, build_target);
let build_excludes = let build_excludes =
get_list_with_profile_and_target(&config, "exclude", profile, build_target)?; get_list_with_profile_and_target(&config, "exclude", profile, build_target)?;
let build_includes = let build_includes =
@@ -557,7 +609,7 @@ fn build_project_at(
dep_root.display() dep_root.display()
)); ));
} }
build_project_at(&dep_root, profile, build_target, &[], force)?; build_project_at(&dep_root, profile, build_target, &[], force, verbose)?;
print!( print!(
"\r{:100}\r {} {} v{}", "\r{:100}\r {} {} v{}",
"", "",
@@ -672,6 +724,17 @@ fn build_project_at(
libs: &resolved.libs, libs: &resolved.libs,
cflags: &resolved_cflags, cflags: &resolved_cflags,
ldflags: &resolved_ldflags, ldflags: &resolved_ldflags,
output_filename: if output_filename.is_empty() {
None
} else {
Some(output_filename.as_str())
},
output_extension: if output_extension.is_empty() {
None
} else {
Some(output_extension.as_str())
},
verbose,
}; };
if std::env::var("DCR_DEBUG").is_ok() { if std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] debug: compiler={}", ctx.compiler); eprintln!("[dcr] debug: compiler={}", ctx.compiler);
@@ -1260,18 +1323,35 @@ fn write_build_fingerprint(ctx: &BuildContext, fingerprint: &str) -> Result<(),
} }
fn build_cache_path(profile: &str, target_dir: Option<&str>) -> std::path::PathBuf { fn build_cache_path(profile: &str, target_dir: Option<&str>) -> std::path::PathBuf {
let _ = target_dir; match target_dir {
Path::new("./target").join(profile).join(".dcr-build.hash") Some(dir) => Path::new(dir).join(".dcr-build.hash"),
None => Path::new("./target").join(profile).join(".dcr-build.hash"),
}
} }
fn build_output_path(ctx: &BuildContext) -> String { fn build_output_path(ctx: &BuildContext) -> String {
let name = ctx.output_filename.unwrap_or(ctx.project_name);
let ext = ctx.output_extension.unwrap_or("");
let final_name = if ext.is_empty() {
name.to_string()
} else {
format!("{}.{}", name, ext)
};
if ctx.kind == "staticlib" { if ctx.kind == "staticlib" {
return crate::platform::lib_path(ctx.profile, ctx.project_name, ctx.target_dir); return crate::platform::lib_path(ctx.profile, &final_name, ctx.target_dir);
} }
if ctx.kind == "sharedlib" { if ctx.kind == "sharedlib" {
return crate::platform::shared_lib_path(ctx.profile, ctx.project_name, ctx.target_dir); return crate::platform::shared_lib_path(ctx.profile, &final_name, ctx.target_dir);
} }
crate::platform::bin_path(ctx.profile, ctx.project_name, ctx.target_dir) if ctx.kind == "efi" {
return crate::platform::efi_path(ctx.profile, &final_name, ctx.target_dir);
}
if ctx.kind == "elf" {
return crate::platform::elf_path(ctx.profile, &final_name, ctx.target_dir);
}
crate::platform::bin_path(ctx.profile, &final_name, ctx.target_dir)
} }
fn collect_header_files( fn collect_header_files(

View File

@@ -91,17 +91,27 @@ fn parse_clean_flags(args: &[String]) -> Result<CleanFlags, String> {
fn clean_from_root(root: &Path, flags: &CleanFlags) -> Result<(), String> { fn clean_from_root(root: &Path, flags: &CleanFlags) -> Result<(), String> {
let config = Config::open("./dcr.toml").map_err(|err| err.to_string())?; let config = Config::open("./dcr.toml").map_err(|err| err.to_string())?;
let target = flags.target.clone().or_else(|| { let target = flags
Some(if cfg!(target_os = "linux") { .target
"x86_64-unknown-linux-gnu".to_string() .clone()
} else if cfg!(target_os = "macos") { .or_else(|| {
"x86_64-apple-darwin".to_string() config
} else if cfg!(target_os = "windows") { .get("build.target")
"x86_64-pc-windows-msvc".to_string() .and_then(|v| v.as_str())
} else { .map(|s| s.trim().to_string())
"unknown".to_string() .filter(|s| !s.is_empty())
}) })
}); .or_else(|| {
Some(if cfg!(target_os = "linux") {
"x86_64-unknown-linux-gnu".to_string()
} else if cfg!(target_os = "macos") {
"x86_64-apple-darwin".to_string()
} else if cfg!(target_os = "windows") {
"x86_64-pc-windows-msvc".to_string()
} else {
"unknown".to_string()
})
});
if flags.all if flags.all
&& let Some(workspace) = parse_workspace( && let Some(workspace) = parse_workspace(

View File

@@ -138,8 +138,8 @@ fn download_asset(client: &Client, url: &str) -> Result<Vec<u8>, String> {
fn asset_candidates(target: &str) -> Vec<String> { fn asset_candidates(target: &str) -> Vec<String> {
let mut names = vec![format!("dcr-{target}")]; let mut names = vec![format!("dcr-{target}")];
if target == "x86_64-pc-windows-msvc" { if target.contains("-windows-") || target.ends_with("-windows") {
names.push("dcr-x86_64-pc-windows-msvc.exe".to_string()); names.push(format!("dcr-{target}.exe"));
} }
names names

View File

@@ -6,6 +6,7 @@ pub struct BuildRunFlags {
pub target: Option<String>, pub target: Option<String>,
pub force: bool, pub force: bool,
pub clean: bool, pub clean: bool,
pub verbose: bool,
} }
pub fn parse_build_run_flags(args: &[String]) -> Result<BuildRunFlags, i32> { pub fn parse_build_run_flags(args: &[String]) -> Result<BuildRunFlags, i32> {
@@ -13,6 +14,7 @@ pub fn parse_build_run_flags(args: &[String]) -> Result<BuildRunFlags, i32> {
let mut target = None; let mut target = None;
let mut force = false; let mut force = false;
let mut clean = false; let mut clean = false;
let mut verbose = false;
let mut iter = args.iter(); let mut iter = args.iter();
while let Some(arg) = iter.next() { while let Some(arg) = iter.next() {
@@ -29,6 +31,10 @@ pub fn parse_build_run_flags(args: &[String]) -> Result<BuildRunFlags, i32> {
clean = true; clean = true;
continue; continue;
} }
if candidate == "verbose" {
verbose = true;
continue;
}
if candidate == "target" { if candidate == "target" {
if let Some(t) = iter.next() { if let Some(t) = iter.next() {
target = Some(t.clone()); target = Some(t.clone());
@@ -55,5 +61,6 @@ pub fn parse_build_run_flags(args: &[String]) -> Result<BuildRunFlags, i32> {
target, target,
force, force,
clean, clean,
verbose,
}) })
} }

View File

@@ -337,6 +337,9 @@ fn collect_project_info_inner(root: &Path, profile: &str) -> Result<ProjectInfo,
libs: &resolved.libs, libs: &resolved.libs,
cflags: &resolved_cflags, cflags: &resolved_cflags,
ldflags: &resolved_ldflags, ldflags: &resolved_ldflags,
output_filename: None,
output_extension: None,
verbose: false,
}; };
let sources = collect_sources(&ctx).map_err(|e| format!("Failed to collect sources: {e}"))?; let sources = collect_sources(&ctx).map_err(|e| format!("Failed to collect sources: {e}"))?;

View File

@@ -69,15 +69,6 @@ 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 build_kind = config
.get("build.kind")
.and_then(|v| v.as_str())
.unwrap_or("");
let target_dir = config
.get("build.target")
.and_then(|v| v.as_str())
.map(|s| s.trim())
.filter(|s| !s.is_empty());
let mut flags = match parse_build_run_flags(args) { let mut flags = match parse_build_run_flags(args) {
Ok(v) => v, Ok(v) => v,
@@ -98,6 +89,12 @@ pub fn run(args: &[String]) -> i32 {
flags.target = Some(default_target.to_string()); flags.target = Some(default_target.to_string());
} }
let build_kind = config
.get(&format!("build.{}.kind", flags.profile))
.and_then(|v| v.as_str())
.or_else(|| config.get("build.kind").and_then(|v| v.as_str()))
.unwrap_or("");
let normalized_target_dir = flags let normalized_target_dir = flags
.target .target
.as_ref() .as_ref()
@@ -110,7 +107,9 @@ pub fn run(args: &[String]) -> i32 {
let run_cmd = get_run_cmd(&config, &flags.profile, flags.target.as_deref(), version); let run_cmd = get_run_cmd(&config, &flags.profile, flags.target.as_deref(), version);
let kind = build_kind.trim(); let kind = build_kind.trim();
if run_cmd.is_none() && (kind == "staticlib" || kind == "sharedlib") { if run_cmd.is_none()
&& (kind == "staticlib" || kind == "sharedlib" || kind == "efi" || kind == "elf")
{
error("Cannot run library build"); error("Cannot run library build");
return 1; return 1;
} }
@@ -128,13 +127,21 @@ pub fn run(args: &[String]) -> i32 {
} }
println!("\n {} {}", colored("Running", BOLD_GREEN), bin_path); println!("\n {} {}", colored("Running", BOLD_GREEN), bin_path);
println!("--------------------------------"); println!("--------------------------------");
return run_binary(project_name, &flags.profile, target_dir); return run_binary(
project_name,
&flags.profile,
normalized_target_dir.as_deref(),
);
} }
let fallback_code = if let Some(cmd) = run_cmd { let fallback_code = if let Some(cmd) = run_cmd {
run_shell(&cmd) run_shell(&cmd)
} else { } else {
run_binary(project_name, &flags.profile, target_dir) run_binary(
project_name,
&flags.profile,
normalized_target_dir.as_deref(),
)
}; };
if fallback_code != 1 { if fallback_code != 1 {
return fallback_code; return fallback_code;

View File

@@ -16,10 +16,11 @@ const BOLD_BLUE: &str = "\x1b[1m\x1b[94m";
pub fn test(args: &[String]) -> i32 { pub fn test(args: &[String]) -> i32 {
let mut init_header = false; let mut init_header = false;
let mut profile = "debug";
for arg in args { for arg in args {
if arg == "--help" { if arg == "--help" {
println!("USAGE:"); println!("USAGE:");
println!(" dcr test [--init]"); println!(" dcr test [--init] [--debug | --release]");
println!(); println!();
println!("ALIASES:"); println!("ALIASES:");
println!(" dcr tests"); println!(" dcr tests");
@@ -29,12 +30,18 @@ pub fn test(args: &[String]) -> i32 {
println!(); println!();
println!("OPTIONS:"); println!("OPTIONS:");
println!(" --init Create tests/dcr_test.h in current project"); println!(" --init Create tests/dcr_test.h in current project");
println!(" --debug Build and run tests with debug profile (default)");
println!(" --release Build and run tests with release profile");
return 0; return 0;
} }
if arg == "--init" { if arg == "--init" {
init_header = true; init_header = true;
continue; continue;
} }
if arg == "--debug" || arg == "--release" {
profile = arg.trim_start_matches("--");
continue;
}
error("Unknown argument"); error("Unknown argument");
return 1; return 1;
} }
@@ -68,7 +75,7 @@ pub fn test(args: &[String]) -> i32 {
} }
} }
match with_dir(&root, run_testsuite) { match with_dir(&root, || run_testsuite(profile)) {
Ok(code) => code, Ok(code) => code,
Err(msg) => { Err(msg) => {
error(&msg); error(&msg);
@@ -77,13 +84,12 @@ pub fn test(args: &[String]) -> i32 {
} }
} }
fn run_testsuite() -> Result<i32, String> { fn run_testsuite(profile: &str) -> Result<i32, String> {
if build::build(&[String::from("--release")]) != 0 { if build::build(&[format!("--{profile}")]) != 0 {
return Ok(1); return Ok(1);
} }
let config = Config::open("./dcr.toml").map_err(|_| "Failed to read dcr.toml".to_string())?; let config = Config::open("./dcr.toml").map_err(|_| "Failed to read dcr.toml".to_string())?;
let profile = "release";
let name = get_config_str(&config, "package.name"); let name = get_config_str(&config, "package.name");
let language = get_language_with_profile(&config, profile)?; let language = get_language_with_profile(&config, profile)?;
let compiler_cfg = get_string_with_profile(&config, "compiler", profile); let compiler_cfg = get_string_with_profile(&config, "compiler", profile);

View File

@@ -5,6 +5,10 @@ use std::sync::{Mutex, OnceLock};
static OUTPUT_MUTEX: OnceLock<Mutex<()>> = OnceLock::new(); static OUTPUT_MUTEX: OnceLock<Mutex<()>> = OnceLock::new();
pub fn get_output_lock() -> &'static Mutex<()> {
OUTPUT_MUTEX.get_or_init(|| Mutex::new(()))
}
pub fn collect_sources( pub fn collect_sources(
roots: &[PathBuf], roots: &[PathBuf],
extensions: &[&str], extensions: &[&str],
@@ -230,10 +234,16 @@ pub fn normalize_source_path(path: &Path) -> String {
pub fn object_path(obj_dir: &Path, source: &str, obj_ext: &str) -> String { pub fn object_path(obj_dir: &Path, source: &str, obj_ext: &str) -> String {
let src_path = Path::new(source); let src_path = Path::new(source);
let rel = src_path let stripped = src_path.strip_prefix("./").unwrap_or(src_path);
.strip_prefix("./src") let rel = stripped
.or_else(|_| src_path.strip_prefix("src")) .components()
.unwrap_or(src_path); .skip(1)
.collect::<std::path::PathBuf>();
let rel = if rel.components().next().is_some() {
rel
} else {
stripped.to_path_buf()
};
let mut out = obj_dir.join(rel); let mut out = obj_dir.join(rel);
out.set_extension(obj_ext.trim_start_matches('.')); out.set_extension(obj_ext.trim_start_matches('.'));
out.to_string_lossy().to_string() out.to_string_lossy().to_string()

View File

@@ -18,7 +18,10 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
}; };
let start_time = Instant::now(); let start_time = Instant::now();
let sources = collect_sources(ctx)?; let sources = collect_sources(ctx)?;
let obj_dir = Path::new("./target").join(ctx.profile).join("obj"); let obj_dir = match ctx.target_dir {
Some(dir) => Path::new(dir).join("obj"),
None => Path::new("./target").join(ctx.profile).join("obj"),
};
let objects = build_objects(assembler, &sources, &obj_dir, ctx, "o")?; let objects = build_objects(assembler, &sources, &obj_dir, ctx, "o")?;
if ctx.kind == "staticlib" { if ctx.kind == "staticlib" {
@@ -37,7 +40,7 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
for obj in &objects { for obj in &objects {
cmd.arg(obj); cmd.arg(obj);
} }
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }
match cmd.status() { match cmd.status() {
@@ -70,14 +73,24 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
for flag in ctx.ldflags { for flag in ctx.ldflags {
cmd.arg(flag); cmd.arg(flag);
} }
let out_path = if ctx.kind == "sharedlib" { let name = ctx.output_filename.unwrap_or(ctx.project_name);
platform::shared_lib_path(ctx.profile, ctx.project_name, ctx.target_dir) let ext = ctx.output_extension.unwrap_or("");
let final_name = if ext.is_empty() {
name.to_string()
} else { } else {
platform::bin_path(ctx.profile, ctx.project_name, ctx.target_dir) format!("{}.{}", name, ext)
};
let out_path = if ctx.kind == "sharedlib" {
platform::shared_lib_path(ctx.profile, &final_name, ctx.target_dir)
} else if ctx.kind == "elf" {
platform::elf_path(ctx.profile, &final_name, ctx.target_dir)
} else {
platform::bin_path(ctx.profile, &final_name, ctx.target_dir)
}; };
cmd.arg("-o").arg(out_path); cmd.arg("-o").arg(out_path);
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }
match cmd.status() { match cmd.status() {

View File

@@ -26,10 +26,19 @@ pub struct BuildContext<'a> {
pub libs: &'a [String], pub libs: &'a [String],
pub cflags: &'a [String], pub cflags: &'a [String],
pub ldflags: &'a [String], pub ldflags: &'a [String],
pub output_filename: Option<&'a str>,
pub output_extension: Option<&'a str>,
pub verbose: bool,
} }
pub fn build(ctx: &BuildContext) -> Result<f64, String> { pub fn build(ctx: &BuildContext) -> Result<f64, String> {
let compiler = ctx.compiler.to_lowercase(); let compiler = ctx.compiler.to_lowercase();
if !check_compiler_exists(ctx.compiler) {
return Err(format!(
"Compiler not found: {}. Make sure it is installed and available in PATH.",
ctx.compiler
));
}
if compiler.contains("clang-cl") { if compiler.contains("clang-cl") {
return msvc::build(ctx); return msvc::build(ctx);
} }
@@ -45,6 +54,14 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
unix_cc::build(ctx) unix_cc::build(ctx)
} }
fn check_compiler_exists(compiler: &str) -> bool {
let name = if compiler.is_empty() { "cc" } else { compiler };
std::process::Command::new(name)
.arg("--version")
.output()
.is_ok()
}
pub fn collect_sources(ctx: &BuildContext) -> Result<Vec<String>, String> { pub fn collect_sources(ctx: &BuildContext) -> Result<Vec<String>, String> {
let compiler = ctx.compiler.to_lowercase(); let compiler = ctx.compiler.to_lowercase();
if compiler.contains("clang-cl") || compiler == "cl" || compiler.contains("msvc") { if compiler.contains("clang-cl") || compiler == "cl" || compiler.contains("msvc") {

View File

@@ -18,7 +18,10 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
} }
let start_time = Instant::now(); let start_time = Instant::now();
let sources = collect_sources(ctx)?; let sources = collect_sources(ctx)?;
let obj_dir = Path::new("./target").join(ctx.profile).join("obj"); let obj_dir = match ctx.target_dir {
Some(dir) => Path::new(dir).join("obj"),
None => Path::new("./target").join(ctx.profile).join("obj"),
};
let objects = build_objects(compiler, &sources, &obj_dir, ctx, "obj")?; let objects = build_objects(compiler, &sources, &obj_dir, ctx, "obj")?;
if ctx.kind == "staticlib" { if ctx.kind == "staticlib" {
@@ -28,7 +31,7 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
for obj in &objects { for obj in &objects {
cmd.arg(obj); cmd.arg(obj);
} }
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }
match cmd.status() { match cmd.status() {
@@ -92,12 +95,14 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
} }
let out_path = if ctx.kind == "sharedlib" { let out_path = if ctx.kind == "sharedlib" {
platform::shared_lib_path(ctx.profile, ctx.project_name, ctx.target_dir) platform::shared_lib_path(ctx.profile, ctx.project_name, ctx.target_dir)
} else if ctx.kind == "elf" {
platform::elf_path(ctx.profile, ctx.project_name, ctx.target_dir)
} else { } else {
platform::bin_path(ctx.profile, ctx.project_name, ctx.target_dir) platform::bin_path(ctx.profile, ctx.project_name, ctx.target_dir)
}; };
cmd.arg(format!("/Fe:{out_path}")); cmd.arg(format!("/Fe:{out_path}"));
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }
match cmd.status() { match cmd.status() {
@@ -185,8 +190,6 @@ fn default_flags(profile: &str) -> &'static [&'static str] {
} }
} }
static OUTPUT_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn build_objects( fn build_objects(
compiler: &str, compiler: &str,
sources: &[String], sources: &[String],
@@ -282,7 +285,7 @@ fn build_object(
} }
} }
let _lock = OUTPUT_MUTEX.lock().unwrap(); let _lock = common::get_output_lock().lock().unwrap();
if !output.status.success() { if !output.status.success() {
eprint!("{}", clean_stdout); eprint!("{}", clean_stdout);

View File

@@ -18,7 +18,10 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
}; };
let start_time = Instant::now(); let start_time = Instant::now();
let sources = collect_sources(ctx)?; let sources = collect_sources(ctx)?;
let obj_dir = Path::new("./target").join(ctx.profile).join("obj"); let obj_dir = match ctx.target_dir {
Some(dir) => Path::new(dir).join("obj"),
None => Path::new("./target").join(ctx.profile).join("obj"),
};
let objects = build_objects(assembler, &sources, &obj_dir, ctx, "o")?; let objects = build_objects(assembler, &sources, &obj_dir, ctx, "o")?;
if ctx.kind == "staticlib" { if ctx.kind == "staticlib" {
@@ -37,7 +40,7 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
for obj in &objects { for obj in &objects {
cmd.arg(obj); cmd.arg(obj);
} }
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }
match cmd.status() { match cmd.status() {
@@ -70,14 +73,24 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
for flag in ctx.ldflags { for flag in ctx.ldflags {
cmd.arg(flag); cmd.arg(flag);
} }
let out_path = if ctx.kind == "sharedlib" { let name = ctx.output_filename.unwrap_or(ctx.project_name);
platform::shared_lib_path(ctx.profile, ctx.project_name, ctx.target_dir) let ext = ctx.output_extension.unwrap_or("");
let final_name = if ext.is_empty() {
name.to_string()
} else { } else {
platform::bin_path(ctx.profile, ctx.project_name, ctx.target_dir) format!("{}.{}", name, ext)
};
let out_path = if ctx.kind == "sharedlib" {
platform::shared_lib_path(ctx.profile, &final_name, ctx.target_dir)
} else if ctx.kind == "elf" {
platform::elf_path(ctx.profile, &final_name, ctx.target_dir)
} else {
platform::bin_path(ctx.profile, &final_name, ctx.target_dir)
}; };
cmd.arg("-o").arg(out_path); cmd.arg("-o").arg(out_path);
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }
match cmd.status() { match cmd.status() {
@@ -147,7 +160,7 @@ fn build_object(
cmd.arg(flag); cmd.arg(flag);
} }
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }

View File

@@ -20,7 +20,10 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
ctx.exclude_dirs, ctx.exclude_dirs,
ctx.include_paths, ctx.include_paths,
)?; )?;
let obj_dir = Path::new("./target").join(ctx.profile).join("obj"); let obj_dir = match ctx.target_dir {
Some(dir) => Path::new(dir).join("obj"),
None => Path::new("./target").join(ctx.profile).join("obj"),
};
let objects = build_objects(compiler, &sources, &obj_dir, ctx, "o")?; let objects = build_objects(compiler, &sources, &obj_dir, ctx, "o")?;
if ctx.kind == "staticlib" { if ctx.kind == "staticlib" {
@@ -30,7 +33,7 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
for obj in &objects { for obj in &objects {
cmd.arg(obj); cmd.arg(obj);
} }
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }
match cmd.status() { match cmd.status() {
@@ -51,6 +54,12 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
cmd.arg("-shared"); cmd.arg("-shared");
} }
} }
if ctx.kind == "efi" {
cmd.arg("-shared");
cmd.arg("-nostdlib");
cmd.arg("-Wl,-dll");
cmd.arg("-Wl,--subsystem,10");
}
for obj in &objects { for obj in &objects {
cmd.arg(obj); cmd.arg(obj);
} }
@@ -63,14 +72,26 @@ pub fn build(ctx: &BuildContext) -> Result<f64, String> {
for flag in ctx.ldflags { for flag in ctx.ldflags {
cmd.arg(flag); cmd.arg(flag);
} }
let out_path = if ctx.kind == "sharedlib" { let name = ctx.output_filename.unwrap_or(ctx.project_name);
platform::shared_lib_path(ctx.profile, ctx.project_name, ctx.target_dir) let ext = ctx.output_extension.unwrap_or("");
let final_name = if ext.is_empty() {
name.to_string()
} else { } else {
platform::bin_path(ctx.profile, ctx.project_name, ctx.target_dir) format!("{}.{}", name, ext)
};
let out_path = if ctx.kind == "sharedlib" {
platform::shared_lib_path(ctx.profile, &final_name, ctx.target_dir)
} else if ctx.kind == "efi" {
platform::efi_path(ctx.profile, &final_name, ctx.target_dir)
} else if ctx.kind == "elf" {
platform::elf_path(ctx.profile, &final_name, ctx.target_dir)
} else {
platform::bin_path(ctx.profile, &final_name, ctx.target_dir)
}; };
cmd.arg("-o").arg(out_path); cmd.arg("-o").arg(out_path);
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }
match cmd.status() { match cmd.status() {
@@ -125,6 +146,19 @@ fn default_flags(profile: &str) -> &'static [&'static str] {
} }
} }
fn is_bare_metal_target(target: Option<&str>) -> bool {
if let Some(t) = target {
let lower = t.to_lowercase();
lower.contains("none")
|| lower.contains("-elf")
|| lower.contains("eabi")
|| lower.contains("baremetal")
|| lower.contains("bare-metal")
} else {
false
}
}
fn build_objects( fn build_objects(
compiler: &str, compiler: &str,
sources: &[String], sources: &[String],
@@ -179,7 +213,8 @@ fn build_object(
cmd.arg(format!("-std={}", ctx.standard)); cmd.arg(format!("-std={}", ctx.standard));
} }
if ctx.cflags.is_empty() { let use_dcr_defaults = ctx.cflags.is_empty() && !is_bare_metal_target(ctx.target);
if use_dcr_defaults {
for flag in default_flags(ctx.profile) { for flag in default_flags(ctx.profile) {
cmd.arg(flag); cmd.arg(flag);
} }
@@ -196,7 +231,7 @@ fn build_object(
let d_path = Path::new(obj_path).with_extension("d"); let d_path = Path::new(obj_path).with_extension("d");
cmd.arg("-MMD").arg("-MF").arg(&d_path); cmd.arg("-MMD").arg("-MF").arg(&d_path);
if std::env::var("DCR_DEBUG").is_ok() { if ctx.verbose || std::env::var("DCR_DEBUG").is_ok() {
eprintln!("[dcr] {:?}", cmd); eprintln!("[dcr] {:?}", cmd);
} }

View File

@@ -10,6 +10,7 @@ const DEFAULT_LANGUAGE: &str = "c";
const DEFAULT_STANDARD: &str = "c11"; const DEFAULT_STANDARD: &str = "c11";
const DEFAULT_COMPILER: &str = "clang"; const DEFAULT_COMPILER: &str = "clang";
const DEFAULT_KIND: &str = "bin"; const DEFAULT_KIND: &str = "bin";
const VALID_KINDS: &[&str] = &["bin", "staticlib", "sharedlib", "efi", "elf"];
#[derive(Debug)] #[derive(Debug)]
pub enum ConfigError { pub enum ConfigError {
@@ -242,7 +243,7 @@ impl Config {
validate_toolchain(self.typed.toolchain.as_ref())?; validate_toolchain(self.typed.toolchain.as_ref())?;
if let Some(kind) = &self.typed.build.kind { if let Some(kind) = &self.typed.build.kind {
let kind = kind.trim(); let kind = kind.trim();
if !kind.is_empty() && kind != "bin" && kind != "staticlib" && kind != "sharedlib" { if !kind.is_empty() && !VALID_KINDS.contains(&kind) {
return Err(ConfigError::Invalid("build.kind is invalid".into())); return Err(ConfigError::Invalid("build.kind is invalid".into()));
} }
} }
@@ -410,7 +411,7 @@ impl Config {
} }
if let Some(kind) = table.get("kind").and_then(|v| v.as_str()) { if let Some(kind) = table.get("kind").and_then(|v| v.as_str()) {
let kind = kind.trim(); let kind = kind.trim();
if !kind.is_empty() && kind != "bin" && kind != "staticlib" && kind != "sharedlib" { if !kind.is_empty() && !VALID_KINDS.contains(&kind) {
return Err(ConfigError::Invalid(format!( return Err(ConfigError::Invalid(format!(
"build.{profile}.kind is invalid" "build.{profile}.kind is invalid"
))); )));
@@ -919,14 +920,16 @@ mod tests {
#[test] #[test]
fn validate_valid_kinds() { fn validate_valid_kinds() {
for kind in ["bin", "staticlib", "sharedlib"] { for &kind in VALID_KINDS {
let dir = temp_dir(&format!("kind_{kind}")); let dir = temp_dir("valid_kind");
let content = format!( let toml = format!(
"[package]\nname = \"test\"\nversion = \"0.1.0\"\n\n[build]\nlanguage = \"c\"\nstandard = \"c11\"\ncompiler = \"clang\"\nkind = \"{kind}\"\n\n[dependencies]\n" "[package]\nname = \"test\"\nversion = \"0.1.0\"\n\n[build]\nlanguage = \"c\"\nstandard = \"c11\"\ncompiler = \"clang\"\nkind = \"{kind}\"\n\n[dependencies]\n"
); );
let path = write_toml_file(&dir, &content); let path = write_toml_file(&dir, &toml);
let result = Config::open(&path.to_string_lossy()); assert!(
assert!(result.is_ok(), "kind '{kind}' should be valid"); Config::open(&path.to_string_lossy()).is_ok(),
"kind '{kind}' should be valid"
);
} }
} }

View File

@@ -5,8 +5,20 @@ pub mod register;
use crate::core::config::Config; use crate::core::config::Config;
use crate::core::deps::common::ResolvedDeps; use crate::core::deps::common::ResolvedDeps;
use crate::core::deps::lock::{DepLock, write_lock};
use std::path::Path; use std::path::Path;
fn dep_version(path: &Path) -> String {
Config::open(&path.join("dcr.toml").to_string_lossy())
.ok()
.and_then(|c| {
c.get("package.version")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
})
.unwrap_or_default()
}
pub fn resolve_deps( pub fn resolve_deps(
config: &Config, config: &Config,
_profile: &str, _profile: &str,
@@ -26,7 +38,7 @@ pub fn resolve_deps(
.to_string(); .to_string();
let deps_table = config.get("dependencies").and_then(|v| v.as_table()); let deps_table = config.get("dependencies").and_then(|v| v.as_table());
let lock_packages = Vec::new(); let mut lock_packages: Vec<DepLock> = Vec::new();
if let Some(deps) = deps_table { if let Some(deps) = deps_table {
for (name, value) in deps { for (name, value) in deps {
@@ -45,6 +57,23 @@ pub fn resolve_deps(
.to_string(), .to_string(),
); );
resolved.libs.push(name.clone()); resolved.libs.push(name.clone());
lock_packages.push(DepLock {
name: name.clone(),
version: pkg_info
.get("version")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string(),
checksum: String::new(),
source: format!(
"registry+{}",
pkg_info
.get("registry_url")
.and_then(|v| v.as_str())
.unwrap_or("https://dcr-registry.pages.dev")
),
});
} else if let Some(path) = path_dep_path(value) { } else if let Some(path) = path_dep_path(value) {
let dep_root = project_root.join(path); let dep_root = project_root.join(path);
if let Some(table) = value.as_table() { if let Some(table) = value.as_table() {
@@ -86,11 +115,25 @@ pub fn resolve_deps(
push_default_lib_dirs(&mut resolved.lib_dirs, &dep_root); push_default_lib_dirs(&mut resolved.lib_dirs, &dep_root);
resolved.libs.push(name.clone()); resolved.libs.push(name.clone());
} }
lock_packages.push(DepLock {
name: name.clone(),
version: dep_version(&dep_root),
checksum: String::new(),
source: format!("path+{}", dep_root.display()),
});
} else if let Some(git_info) = git_dep(value) {
lock_packages.push(DepLock {
name: name.clone(),
version: git_info.version.unwrap_or_default(),
checksum: String::new(),
source: format!("git+{}", git_info.url),
});
} }
} }
} }
crate::core::deps::lock::write_lock( write_lock(
project_root, project_root,
&project_name, &project_name,
&project_version, &project_version,
@@ -100,6 +143,24 @@ pub fn resolve_deps(
Ok(resolved) Ok(resolved)
} }
struct GitDep<'a> {
url: &'a str,
version: Option<String>,
}
fn git_dep(value: &toml::Value) -> Option<GitDep<'_>> {
if let Some(table) = value.as_table()
&& let Some(url) = table.get("git").and_then(|v| v.as_str())
{
let version = table
.get("version")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
return Some(GitDep { url, version });
}
None
}
fn path_dep_path(value: &toml::Value) -> Option<&str> { fn path_dep_path(value: &toml::Value) -> Option<&str> {
if let Some(table) = value.as_table() { if let Some(table) = value.as_table() {
return table.get("path").and_then(|v| v.as_str()); return table.get("path").and_then(|v| v.as_str());

View File

@@ -15,9 +15,23 @@ pub struct DcrConfig {
pub registry: std::collections::HashMap<String, Registry>, pub registry: std::collections::HashMap<String, Registry>,
} }
fn home_dir() -> Option<PathBuf> {
if let Ok(home) = std::env::var("HOME") {
return Some(PathBuf::from(home));
}
if let Ok(profile) = std::env::var("USERPROFILE") {
return Some(PathBuf::from(profile));
}
None
}
fn dcr_config_dir() -> Option<PathBuf> {
home_dir().map(|h| h.join(".dcr"))
}
pub fn get_registry_config() -> Option<DcrConfig> { pub fn get_registry_config() -> Option<DcrConfig> {
let home = std::env::var("HOME").ok()?; let home = home_dir()?;
let config_path = Path::new(&home).join(".dcr/config.toml"); let config_path = home.join(".dcr/config.toml");
if !config_path.exists() { if !config_path.exists() {
return None; return None;
} }
@@ -30,16 +44,25 @@ pub fn get_index_path() -> PathBuf {
std::env::var("DCR_INDEX_PATH") std::env::var("DCR_INDEX_PATH")
.map(PathBuf::from) .map(PathBuf::from)
.unwrap_or_else(|_| { .unwrap_or_else(|_| {
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string()); dcr_config_dir()
Path::new(&home).join(".dcr").join("index.json") .unwrap_or_else(|| PathBuf::from("."))
.join("index.json")
}) })
} }
pub fn get_registry_cache_root() -> PathBuf { pub fn get_registry_cache_root() -> PathBuf {
get_index_path() let index_path = get_index_path();
.parent() if let Some(parent) = index_path.parent() {
.map(Path::to_path_buf) if parent.exists() {
.unwrap_or_else(|| PathBuf::from(".")) return parent.to_path_buf();
}
if let Err(e) = std::fs::create_dir_all(parent) {
eprintln!("Warning: failed to create registry cache directory: {e}");
}
parent.to_path_buf()
} else {
PathBuf::from(".")
}
} }
pub fn package_root_from_registry_info(pkg_info: &JsonValue) -> Result<PathBuf, String> { pub fn package_root_from_registry_info(pkg_info: &JsonValue) -> Result<PathBuf, String> {
@@ -109,12 +132,16 @@ pub fn is_registry_dep(value: &TomlValue) -> bool {
return !is_path_like_string(raw) && !is_git_like_string(raw); return !is_path_like_string(raw) && !is_git_like_string(raw);
} }
if let Some(table) = value.as_table() { if let Some(table) = value.as_table() {
return !table.contains_key("git") if table.contains_key("git") || table.contains_key("path") || table.contains_key("url") {
&& !table.contains_key("path") return false;
&& !table.contains_key("url") }
&& !table.contains_key("version"); table.contains_key("version")
|| table.contains_key("features")
|| table.contains_key("optional")
|| table.contains_key("registry")
} else {
false
} }
false
} }
pub fn path_from_string_dep(value: &TomlValue) -> Option<&str> { pub fn path_from_string_dep(value: &TomlValue) -> Option<&str> {

View File

@@ -20,10 +20,21 @@ pub struct RegistryManager {
pub path: PathBuf, pub path: PathBuf,
} }
fn home_dir() -> Option<PathBuf> {
if let Ok(home) = std::env::var("HOME") {
return Some(PathBuf::from(home));
}
if let Ok(profile) = std::env::var("USERPROFILE") {
return Some(PathBuf::from(profile));
}
None
}
impl RegistryManager { impl RegistryManager {
pub fn load() -> Result<Self, Box<dyn std::error::Error>> { pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
let home = std::env::var("HOME")?; let dcr_dir = home_dir()
let dcr_dir = PathBuf::from(home).join(".dcr"); .ok_or("Cannot determine home directory")?
.join(".dcr");
let config_path = dcr_dir.join("config.toml"); let config_path = dcr_dir.join("config.toml");
if !config_path.exists() { if !config_path.exists() {

View File

@@ -12,14 +12,44 @@ pub fn bin_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
pub fn lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String { pub fn lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir { match target_dir {
Some(dir) => format!("{}/lib{}.a", dir.trim_end_matches('/'), name), Some(dir) => format!("{}/lib{}.a", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/lib{name}.a"), None => {
let arch = std::env::consts::ARCH;
let target = format!("{arch}-unknown-linux-gnu");
format!("./target/{target}/{profile}/lib{name}.a")
}
}
}
pub fn elf_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir {
Some(dir) => format!("{}/{}", dir.trim_end_matches('/'), name),
None => {
let arch = std::env::consts::ARCH;
let target = format!("{arch}-unknown-linux-gnu");
format!("./target/{target}/{profile}/{name}")
}
}
}
pub fn efi_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir {
Some(dir) => format!("{}/{}.efi", dir.trim_end_matches('/'), name),
None => {
let arch = std::env::consts::ARCH;
let target = format!("{arch}-unknown-linux-gnu");
format!("./target/{target}/{profile}/{name}.efi")
}
} }
} }
pub fn shared_lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String { pub fn shared_lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir { match target_dir {
Some(dir) => format!("{}/lib{}.so", dir.trim_end_matches('/'), name), Some(dir) => format!("{}/lib{}.so", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/lib{name}.so"), None => {
let arch = std::env::consts::ARCH;
let target = format!("{arch}-unknown-linux-gnu");
format!("./target/{target}/{profile}/lib{name}.so")
}
} }
} }
@@ -53,10 +83,9 @@ mod tests {
#[test] #[test]
fn lib_path_default() { fn lib_path_default() {
assert_eq!( let arch = std::env::consts::ARCH;
lib_path("debug", "mylib", None), let expected = format!("./target/{arch}-unknown-linux-gnu/debug/libmylib.a");
"./target/debug/libmylib.a" assert_eq!(lib_path("debug", "mylib", None), expected);
);
} }
#[test] #[test]
@@ -66,10 +95,9 @@ mod tests {
#[test] #[test]
fn shared_lib_path_default() { fn shared_lib_path_default() {
assert_eq!( let arch = std::env::consts::ARCH;
shared_lib_path("debug", "mylib", None), let expected = format!("./target/{arch}-unknown-linux-gnu/debug/libmylib.so");
"./target/debug/libmylib.so" assert_eq!(shared_lib_path("debug", "mylib", None), expected);
);
} }
#[test] #[test]

View File

@@ -12,6 +12,20 @@ pub fn lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
} }
} }
pub fn elf_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir {
Some(dir) => format!("{}/{}", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/{name}"),
}
}
pub fn efi_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir {
Some(dir) => format!("{}/{}.efi", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/{name}.efi"),
}
}
pub fn shared_lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String { pub fn shared_lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir { match target_dir {
Some(dir) => format!("{}/lib{}.dylib", dir.trim_end_matches('/'), name), Some(dir) => format!("{}/lib{}.dylib", dir.trim_end_matches('/'), name),

View File

@@ -27,6 +27,50 @@ pub fn bin_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
} }
} }
pub fn elf_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
#[cfg(target_os = "linux")]
{
linux::elf_path(profile, name, target_dir)
}
#[cfg(target_os = "macos")]
{
return macos::elf_path(profile, name, target_dir);
}
#[cfg(target_os = "windows")]
{
return windows::elf_path(profile, name, target_dir);
}
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
{
match target_dir {
Some(dir) => format!("{}/{}", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/{name}"),
}
}
}
pub fn efi_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
#[cfg(target_os = "linux")]
{
linux::efi_path(profile, name, target_dir)
}
#[cfg(target_os = "macos")]
{
return macos::efi_path(profile, name, target_dir);
}
#[cfg(target_os = "windows")]
{
return windows::efi_path(profile, name, target_dir);
}
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
{
match target_dir {
Some(dir) => format!("{}/{}.efi", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/{name}.efi"),
}
}
}
pub fn lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String { pub fn lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {

View File

@@ -12,6 +12,20 @@ pub fn lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
} }
} }
pub fn elf_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir {
Some(dir) => format!("{}/{}.exe", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/{name}.exe"),
}
}
pub fn efi_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir {
Some(dir) => format!("{}/{}.efi", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/{name}.efi"),
}
}
pub fn shared_lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String { pub fn shared_lib_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir { match target_dir {
Some(dir) => format!("{}/{}.dll", dir.trim_end_matches('/'), name), Some(dir) => format!("{}/{}.dll", dir.trim_end_matches('/'), name),