From 81ccfd7c3f9f6f2ff47a79409ae95520faf9d0e9 Mon Sep 17 00:00:00 2001 From: Matt Schulte Date: Thu, 23 Mar 2023 16:47:19 -0700 Subject: [PATCH 1/4] Add a num_cpus() function This is useful for when you are calling `make` from Just and want to pass a `-j` argument --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + README.md | 1 + src/function.rs | 6 ++++++ src/justfile.rs | 4 ++-- tests/functions.rs | 21 +++++++++++---------- 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 431621abfe..aa8d4ab917 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -370,6 +370,7 @@ dependencies = [ "lexiclean", "libc", "log", + "num_cpus", "pretty_assertions", "regex", "serde", @@ -442,6 +443,16 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "num_cpus" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" +dependencies = [ + "hermit-abi 0.2.6", + "libc", +] + [[package]] name = "once_cell" version = "1.17.2" diff --git a/Cargo.toml b/Cargo.toml index 065a0ab179..f1b8051004 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ heck = "0.4.0" lexiclean = "0.0.1" libc = "0.2.0" log = "0.4.4" +num_cpus = "1.15.0" regex = "1.5.4" serde = { version = "1.0.130", features = ["derive", "rc"] } serde_json = "1.0.68" diff --git a/README.md b/README.md index 3742c3fd00..b8ab674944 100644 --- a/README.md +++ b/README.md @@ -1079,6 +1079,7 @@ Done! #### System Information - `arch()` — Instruction set architecture. Possible values are: `"aarch64"`, `"arm"`, `"asmjs"`, `"hexagon"`, `"mips"`, `"msp430"`, `"powerpc"`, `"powerpc64"`, `"s390x"`, `"sparc"`, `"wasm32"`, `"x86"`, `"x86_64"`, and `"xcore"`. +- `num_cpus()` - Number of logical cores this process could try to use. - `os()` — Operating system. Possible values are: `"android"`, `"bitrig"`, `"dragonfly"`, `"emscripten"`, `"freebsd"`, `"haiku"`, `"ios"`, `"linux"`, `"macos"`, `"netbsd"`, `"openbsd"`, `"solaris"`, and `"windows"`. - `os_family()` — Operating system family; possible values are: `"unix"` and `"windows"`. diff --git a/src/function.rs b/src/function.rs index 8102c8aadb..6d3e262b00 100644 --- a/src/function.rs +++ b/src/function.rs @@ -38,6 +38,7 @@ pub(crate) fn get(name: &str) -> Option { "kebabcase" => Unary(kebabcase), "lowercamelcase" => Unary(lowercamelcase), "lowercase" => Unary(lowercase), + "num_cpus" =>Nullary(num_cpus), "os" => Nullary(os), "os_family" => Nullary(os_family), "parent_directory" => Unary(parent_directory), @@ -270,6 +271,11 @@ fn lowercase(_context: &FunctionContext, s: &str) -> Result { Ok(s.to_lowercase()) } +fn num_cpus(_context: &FunctionContext) -> Result { + let num = num_cpus::get(); + Ok(num.to_string()) +} + fn os(_context: &FunctionContext) -> Result { Ok(target::os().to_owned()) } diff --git a/src/justfile.rs b/src/justfile.rs index 1919999548..a8ae40627b 100644 --- a/src/justfile.rs +++ b/src/justfile.rs @@ -930,11 +930,11 @@ c := a + b + a + b", x := arch() a: - {{os()}} {{os_family()}}", + {{os()}} {{os_family()}} {{num_cpus()}}", "x := arch() a: - {{ os() }} {{ os_family() }}", + {{ os() }} {{ os_family() }} {{ num_cpus() }}", } test! { diff --git a/tests/functions.rs b/tests/functions.rs index 4503243cf1..5511329adc 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -4,10 +4,10 @@ test! { name: test_os_arch_functions_in_interpolation, justfile: r#" foo: - echo {{arch()}} {{os()}} {{os_family()}} + echo {{arch()}} {{os()}} {{os_family()}} {{num_cpus()}} "#, - stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(), - stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(), + stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(), + stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(), } test! { @@ -16,12 +16,13 @@ test! { a := arch() o := os() f := os_family() +n := num_cpus() foo: - echo {{a}} {{o}} {{f}} + echo {{a}} {{o}} {{f}} {{n}} "#, - stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(), - stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(), + stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(), + stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(), } #[cfg(not(windows))] @@ -246,11 +247,11 @@ test! { test! { name: test_os_arch_functions_in_default, justfile: r#" -foo a=arch() o=os() f=os_family(): - echo {{a}} {{o}} {{f}} +foo a=arch() o=os() f=os_family() n=num_cpus(): + echo {{a}} {{o}} {{f}} {{n}} "#, - stdout: format!("{} {} {}\n", target::arch(), target::os(), target::family()).as_str(), - stderr: format!("echo {} {} {}\n", target::arch(), target::os(), target::family()).as_str(), + stdout: format!("{} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(), + stderr: format!("echo {} {} {} {}\n", target::arch(), target::os(), target::family(), num_cpus::get()).as_str(), } test! { From e109710538fcaa37833ea694819337d73ee895fb Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 2 Aug 2023 16:45:47 -0700 Subject: [PATCH 2/4] Reword --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b8ab674944..061de68c69 100644 --- a/README.md +++ b/README.md @@ -1079,7 +1079,7 @@ Done! #### System Information - `arch()` — Instruction set architecture. Possible values are: `"aarch64"`, `"arm"`, `"asmjs"`, `"hexagon"`, `"mips"`, `"msp430"`, `"powerpc"`, `"powerpc64"`, `"s390x"`, `"sparc"`, `"wasm32"`, `"x86"`, `"x86_64"`, and `"xcore"`. -- `num_cpus()` - Number of logical cores this process could try to use. +- `num_cpus()` - Number of logical CPUs. - `os()` — Operating system. Possible values are: `"android"`, `"bitrig"`, `"dragonfly"`, `"emscripten"`, `"freebsd"`, `"haiku"`, `"ios"`, `"linux"`, `"macos"`, `"netbsd"`, `"openbsd"`, `"solaris"`, and `"windows"`. - `os_family()` — Operating system family; possible values are: `"unix"` and `"windows"`. From 5a8d25b9d0a306e4749af03f24c538175948d665 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 2 Aug 2023 16:46:53 -0700 Subject: [PATCH 3/4] Format --- src/function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/function.rs b/src/function.rs index 6d3e262b00..81481a97d3 100644 --- a/src/function.rs +++ b/src/function.rs @@ -38,7 +38,7 @@ pub(crate) fn get(name: &str) -> Option { "kebabcase" => Unary(kebabcase), "lowercamelcase" => Unary(lowercamelcase), "lowercase" => Unary(lowercase), - "num_cpus" =>Nullary(num_cpus), + "num_cpus" => Nullary(num_cpus), "os" => Nullary(os), "os_family" => Nullary(os_family), "parent_directory" => Unary(parent_directory), From c3d7a77c687eaaf54bd8ea25586ded4811623349 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 2 Aug 2023 16:49:18 -0700 Subject: [PATCH 4/4] Update lockfile --- Cargo.lock | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index aa8d4ab917..87415daf3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,6 +301,15 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.1"