@tishlang/tish-format 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +49 -0
- package/LICENSE +13 -0
- package/README.md +138 -0
- package/bin/tish-format +0 -0
- package/crates/js_to_tish/Cargo.toml +11 -0
- package/crates/js_to_tish/README.md +18 -0
- package/crates/js_to_tish/src/error.rs +55 -0
- package/crates/js_to_tish/src/lib.rs +11 -0
- package/crates/js_to_tish/src/span_util.rs +35 -0
- package/crates/js_to_tish/src/transform/expr.rs +610 -0
- package/crates/js_to_tish/src/transform/stmt.rs +503 -0
- package/crates/js_to_tish/src/transform.rs +60 -0
- package/crates/tish/Cargo.toml +54 -0
- package/crates/tish/src/cargo_native_registry.rs +32 -0
- package/crates/tish/src/cli_help.rs +565 -0
- package/crates/tish/src/main.rs +781 -0
- package/crates/tish/src/repl_completion.rs +200 -0
- package/crates/tish/tests/cargo_example_compile.rs +67 -0
- package/crates/tish/tests/fixtures/cargo_example_project/Cargo.toml +3 -0
- package/crates/tish/tests/fixtures/cargo_example_project/crates/demo-shim/Cargo.toml +11 -0
- package/crates/tish/tests/fixtures/cargo_example_project/crates/demo-shim/src/lib.rs +12 -0
- package/crates/tish/tests/fixtures/cargo_example_project/package.json +10 -0
- package/crates/tish/tests/fixtures/cargo_example_project/src/main.tish +3 -0
- package/crates/tish/tests/integration_test.rs +1095 -0
- package/crates/tish/tests/run_optimize_stdout_parity.rs +50 -0
- package/crates/tish/tests/shortcircuit.rs +65 -0
- package/crates/tish_ast/Cargo.toml +9 -0
- package/crates/tish_ast/src/ast.rs +620 -0
- package/crates/tish_ast/src/lib.rs +5 -0
- package/crates/tish_build_utils/Cargo.toml +11 -0
- package/crates/tish_build_utils/src/lib.rs +577 -0
- package/crates/tish_builtins/Cargo.toml +20 -0
- package/crates/tish_builtins/src/array.rs +441 -0
- package/crates/tish_builtins/src/construct.rs +159 -0
- package/crates/tish_builtins/src/globals.rs +213 -0
- package/crates/tish_builtins/src/helpers.rs +35 -0
- package/crates/tish_builtins/src/lib.rs +16 -0
- package/crates/tish_builtins/src/math.rs +89 -0
- package/crates/tish_builtins/src/object.rs +36 -0
- package/crates/tish_builtins/src/string.rs +647 -0
- package/crates/tish_builtins/src/symbol.rs +83 -0
- package/crates/tish_bytecode/Cargo.toml +17 -0
- package/crates/tish_bytecode/src/chunk.rs +96 -0
- package/crates/tish_bytecode/src/compiler.rs +1760 -0
- package/crates/tish_bytecode/src/encoding.rs +100 -0
- package/crates/tish_bytecode/src/lib.rs +19 -0
- package/crates/tish_bytecode/src/opcode.rs +142 -0
- package/crates/tish_bytecode/src/peephole.rs +189 -0
- package/crates/tish_bytecode/src/serialize.rs +163 -0
- package/crates/tish_bytecode/tests/break_continue_bytecode.rs +44 -0
- package/crates/tish_bytecode/tests/constant_folding.rs +84 -0
- package/crates/tish_bytecode/tests/sort_optimization.rs +31 -0
- package/crates/tish_compile/Cargo.toml +26 -0
- package/crates/tish_compile/src/codegen.rs +5332 -0
- package/crates/tish_compile/src/infer.rs +292 -0
- package/crates/tish_compile/src/lib.rs +164 -0
- package/crates/tish_compile/src/resolve.rs +1388 -0
- package/crates/tish_compile/src/types.rs +501 -0
- package/crates/tish_compile_js/Cargo.toml +18 -0
- package/crates/tish_compile_js/examples/jsx_vdom_smoke.tish +8 -0
- package/crates/tish_compile_js/src/codegen.rs +871 -0
- package/crates/tish_compile_js/src/error.rs +20 -0
- package/crates/tish_compile_js/src/lib.rs +26 -0
- package/crates/tish_compile_js/src/tests_jsx.rs +350 -0
- package/crates/tish_compiler_wasm/Cargo.toml +21 -0
- package/crates/tish_compiler_wasm/src/lib.rs +57 -0
- package/crates/tish_compiler_wasm/src/resolve_virtual.rs +473 -0
- package/crates/tish_core/Cargo.toml +26 -0
- package/crates/tish_core/src/console_style.rs +160 -0
- package/crates/tish_core/src/json.rs +387 -0
- package/crates/tish_core/src/lib.rs +17 -0
- package/crates/tish_core/src/macros.rs +36 -0
- package/crates/tish_core/src/uri.rs +118 -0
- package/crates/tish_core/src/value.rs +696 -0
- package/crates/tish_core/src/vmref.rs +178 -0
- package/crates/tish_cranelift/Cargo.toml +19 -0
- package/crates/tish_cranelift/src/lib.rs +43 -0
- package/crates/tish_cranelift/src/link.rs +117 -0
- package/crates/tish_cranelift/src/lower.rs +85 -0
- package/crates/tish_cranelift_runtime/Cargo.toml +25 -0
- package/crates/tish_cranelift_runtime/src/lib.rs +45 -0
- package/crates/tish_eval/Cargo.toml +45 -0
- package/crates/tish_eval/src/eval.rs +3717 -0
- package/crates/tish_eval/src/http.rs +188 -0
- package/crates/tish_eval/src/lib.rs +99 -0
- package/crates/tish_eval/src/natives.rs +399 -0
- package/crates/tish_eval/src/promise.rs +179 -0
- package/crates/tish_eval/src/regex.rs +299 -0
- package/crates/tish_eval/src/timers.rs +120 -0
- package/crates/tish_eval/src/value.rs +318 -0
- package/crates/tish_eval/src/value_convert.rs +111 -0
- package/crates/tish_fmt/Cargo.toml +16 -0
- package/crates/tish_fmt/src/bin/tish-fmt.rs +41 -0
- package/crates/tish_fmt/src/lib.rs +2101 -0
- package/crates/tish_jsx_web/Cargo.toml +9 -0
- package/crates/tish_jsx_web/README.md +5 -0
- package/crates/tish_jsx_web/src/lib.rs +2 -0
- package/crates/tish_lexer/Cargo.toml +9 -0
- package/crates/tish_lexer/src/lib.rs +716 -0
- package/crates/tish_lexer/src/token.rs +163 -0
- package/crates/tish_lint/Cargo.toml +18 -0
- package/crates/tish_lint/src/bin/tish-lint.rs +195 -0
- package/crates/tish_lint/src/lib.rs +289 -0
- package/crates/tish_llvm/Cargo.toml +13 -0
- package/crates/tish_llvm/src/lib.rs +115 -0
- package/crates/tish_lsp/Cargo.toml +25 -0
- package/crates/tish_lsp/README.md +26 -0
- package/crates/tish_lsp/src/builtin_goto.rs +362 -0
- package/crates/tish_lsp/src/import_goto.rs +562 -0
- package/crates/tish_lsp/src/main.rs +1046 -0
- package/crates/tish_native/Cargo.toml +16 -0
- package/crates/tish_native/src/build.rs +427 -0
- package/crates/tish_native/src/config.rs +48 -0
- package/crates/tish_native/src/lib.rs +416 -0
- package/crates/tish_opt/Cargo.toml +13 -0
- package/crates/tish_opt/src/lib.rs +943 -0
- package/crates/tish_parser/Cargo.toml +11 -0
- package/crates/tish_parser/src/lib.rs +332 -0
- package/crates/tish_parser/src/parser.rs +2304 -0
- package/crates/tish_pg/Cargo.toml +34 -0
- package/crates/tish_pg/README.md +38 -0
- package/crates/tish_pg/src/error.rs +52 -0
- package/crates/tish_pg/src/lib.rs +955 -0
- package/crates/tish_resolve/Cargo.toml +13 -0
- package/crates/tish_resolve/src/lib.rs +3561 -0
- package/crates/tish_resolve/src/pos.rs +141 -0
- package/crates/tish_runtime/Cargo.toml +96 -0
- package/crates/tish_runtime/src/http.rs +1298 -0
- package/crates/tish_runtime/src/http_fetch.rs +471 -0
- package/crates/tish_runtime/src/http_hyper.rs +418 -0
- package/crates/tish_runtime/src/http_prefork.rs +189 -0
- package/crates/tish_runtime/src/lib.rs +1192 -0
- package/crates/tish_runtime/src/native_promise.rs +15 -0
- package/crates/tish_runtime/src/promise.rs +248 -0
- package/crates/tish_runtime/src/promise_io.rs +38 -0
- package/crates/tish_runtime/src/timers.rs +166 -0
- package/crates/tish_runtime/src/ws.rs +761 -0
- package/crates/tish_runtime/tests/fetch_readable_stream.rs +102 -0
- package/crates/tish_ui/Cargo.toml +17 -0
- package/crates/tish_ui/src/jsx.rs +682 -0
- package/crates/tish_ui/src/lib.rs +20 -0
- package/crates/tish_ui/src/runtime/hooks.rs +569 -0
- package/crates/tish_ui/src/runtime/mod.rs +180 -0
- package/crates/tish_vm/Cargo.toml +47 -0
- package/crates/tish_vm/src/lib.rs +39 -0
- package/crates/tish_vm/src/vm.rs +2192 -0
- package/crates/tish_vm/tests/fixtures/or_string_cmd.tish +2 -0
- package/crates/tish_vm/tests/lexical_scope_declare.rs +34 -0
- package/crates/tish_vm/tests/peephole_jump_chain_logical_or.rs +150 -0
- package/crates/tish_wasm/Cargo.toml +15 -0
- package/crates/tish_wasm/src/lib.rs +424 -0
- package/crates/tish_wasm_runtime/Cargo.toml +37 -0
- package/crates/tish_wasm_runtime/src/gpu.rs +413 -0
- package/crates/tish_wasm_runtime/src/lib.rs +42 -0
- package/crates/tishlang_cargo_bindgen/Cargo.toml +26 -0
- package/crates/tishlang_cargo_bindgen/src/classify.rs +263 -0
- package/crates/tishlang_cargo_bindgen/src/discover.rs +125 -0
- package/crates/tishlang_cargo_bindgen/src/infer.rs +382 -0
- package/crates/tishlang_cargo_bindgen/src/lib.rs +349 -0
- package/crates/tishlang_cargo_bindgen/src/main.rs +167 -0
- package/crates/tishlang_cargo_bindgen/src/metadata.rs +117 -0
- package/justfile +268 -0
- package/package.json +1 -1
- package/platform/darwin-arm64/tish-fmt +0 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "tishlang_native"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
description = "Native code generation backend for Tish (Cranelift / direct compile)"
|
|
6
|
+
|
|
7
|
+
license-file = { workspace = true }
|
|
8
|
+
repository = { workspace = true }
|
|
9
|
+
[dependencies]
|
|
10
|
+
tishlang_build_utils = { path = "../tish_build_utils", version = ">=0.1" }
|
|
11
|
+
tishlang_ast = { path = "../tish_ast", version = ">=0.1" }
|
|
12
|
+
tishlang_bytecode = { path = "../tish_bytecode", version = ">=0.1" }
|
|
13
|
+
tishlang_compile = { path = "../tish_compile", version = ">=0.1" }
|
|
14
|
+
tishlang_cranelift = { path = "../tish_cranelift", version = ">=0.1" }
|
|
15
|
+
tishlang_llvm = { path = "../tish_llvm", version = ">=0.1" }
|
|
16
|
+
tishlang_opt = { path = "../tish_opt", version = ">=0.1" }
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
//! Build native binary via cargo (interim path until Cranelift backend is ready).
|
|
2
|
+
|
|
3
|
+
use std::fs;
|
|
4
|
+
use std::path::Path;
|
|
5
|
+
|
|
6
|
+
use tishlang_compile::ResolvedNativeModule;
|
|
7
|
+
|
|
8
|
+
use crate::config::{NativeArtifact, NativeBuildConfig};
|
|
9
|
+
|
|
10
|
+
/// `tishlang_runtime` Cargo feature names (subset of CLI / compile feature names).
|
|
11
|
+
const RUNTIME_CARGO_FEATURES: &[&str] = &[
|
|
12
|
+
"http",
|
|
13
|
+
"http-hyper",
|
|
14
|
+
"http-io-uring",
|
|
15
|
+
"fs",
|
|
16
|
+
"process",
|
|
17
|
+
"regex",
|
|
18
|
+
"ws",
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
/// Map CLI/compile features to flags passed to `tishlang_runtime` in the temp crate's Cargo.toml.
|
|
22
|
+
/// `full` enables every optional runtime capability (matches `tish build --feature full` / LANGUAGE.md).
|
|
23
|
+
fn runtime_features_for_cargo(features: &[String]) -> Vec<String> {
|
|
24
|
+
let mut out = Vec::new();
|
|
25
|
+
for f in features {
|
|
26
|
+
if f == "full" {
|
|
27
|
+
for name in RUNTIME_CARGO_FEATURES {
|
|
28
|
+
if !out.iter().any(|x: &String| x == *name) {
|
|
29
|
+
out.push((*name).to_string());
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if RUNTIME_CARGO_FEATURES.contains(&f.as_str()) && !out.contains(f) {
|
|
35
|
+
out.push(f.clone());
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
out
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// `[profile.release]` for nested `cargo build` of generated crates.
|
|
42
|
+
fn nested_release_profile_toml() -> &'static str {
|
|
43
|
+
if std::env::var("TISH_FAST_NATIVE_BUILD").as_deref() == Ok("1") {
|
|
44
|
+
r#"[profile.release]
|
|
45
|
+
opt-level = 1
|
|
46
|
+
lto = false
|
|
47
|
+
codegen-units = 16
|
|
48
|
+
incremental = true
|
|
49
|
+
strip = false
|
|
50
|
+
debug = 0
|
|
51
|
+
panic = "abort"
|
|
52
|
+
"#
|
|
53
|
+
} else {
|
|
54
|
+
r#"[profile.release]
|
|
55
|
+
# Reduce binary size: strip symbols, abort on panic (no unwinding), single codegen unit
|
|
56
|
+
strip = true
|
|
57
|
+
panic = "abort"
|
|
58
|
+
codegen-units = 1
|
|
59
|
+
lto = "fat"
|
|
60
|
+
"#
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// Inject `mod generated_native;` after the crate attribute so the binary crate can call `crate::generated_native::…`.
|
|
65
|
+
fn inject_generated_native_mod(rust_code: &str) -> String {
|
|
66
|
+
if let Some(pos) = rust_code.find("\n\n") {
|
|
67
|
+
let (a, b) = rust_code.split_at(pos + 2);
|
|
68
|
+
format!("{}mod generated_native;\n{}", a, b)
|
|
69
|
+
} else {
|
|
70
|
+
format!("{}\n\nmod generated_native;\n", rust_code)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
pub(crate) fn rust_code_needs_tokio(rust_code: &str) -> bool {
|
|
75
|
+
rust_code.contains("#[tokio::main]") || rust_code.contains("tokio::runtime::Runtime")
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
pub fn build_via_cargo(
|
|
79
|
+
rust_code: &str,
|
|
80
|
+
native_modules: Vec<ResolvedNativeModule>,
|
|
81
|
+
output_path: &Path,
|
|
82
|
+
features: &[String],
|
|
83
|
+
extra_dependencies_toml: &str,
|
|
84
|
+
generated_native_rs: Option<&str>,
|
|
85
|
+
project_root: Option<&Path>,
|
|
86
|
+
) -> Result<(), String> {
|
|
87
|
+
build_via_cargo_with_config(
|
|
88
|
+
rust_code,
|
|
89
|
+
native_modules,
|
|
90
|
+
output_path,
|
|
91
|
+
features,
|
|
92
|
+
extra_dependencies_toml,
|
|
93
|
+
generated_native_rs,
|
|
94
|
+
project_root,
|
|
95
|
+
&NativeBuildConfig::desktop(),
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
pub fn build_via_cargo_with_config(
|
|
100
|
+
rust_code: &str,
|
|
101
|
+
native_modules: Vec<ResolvedNativeModule>,
|
|
102
|
+
output_path: &Path,
|
|
103
|
+
features: &[String],
|
|
104
|
+
extra_dependencies_toml: &str,
|
|
105
|
+
generated_native_rs: Option<&str>,
|
|
106
|
+
project_root: Option<&Path>,
|
|
107
|
+
build_config: &NativeBuildConfig,
|
|
108
|
+
) -> Result<(), String> {
|
|
109
|
+
let out_stem = output_path
|
|
110
|
+
.file_stem()
|
|
111
|
+
.and_then(|s| s.to_str())
|
|
112
|
+
.unwrap_or("tish_out");
|
|
113
|
+
let cargo_name = tishlang_build_utils::cargo_target_name(out_stem);
|
|
114
|
+
let build_dir = tishlang_build_utils::create_build_dir("tish_build", out_stem)?;
|
|
115
|
+
|
|
116
|
+
let runtime_path = tishlang_build_utils::find_runtime_path_for_project(project_root)?;
|
|
117
|
+
|
|
118
|
+
let runtime_features = runtime_features_for_cargo(features);
|
|
119
|
+
let runtime_refs: Vec<&str> = runtime_features.iter().map(String::as_str).collect();
|
|
120
|
+
let features_str = if runtime_refs.is_empty() {
|
|
121
|
+
String::new()
|
|
122
|
+
} else {
|
|
123
|
+
format!(", features = {:?}", runtime_refs)
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
let needs_tokio = rust_code_needs_tokio(rust_code);
|
|
127
|
+
let tokio_dep = if needs_tokio {
|
|
128
|
+
"\ntokio = { version = \"1\", features = [\"rt-multi-thread\", \"macros\"] }\n"
|
|
129
|
+
} else {
|
|
130
|
+
""
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
let native_deps: String = native_modules
|
|
134
|
+
.iter()
|
|
135
|
+
.filter(|m| m.use_path_dependency)
|
|
136
|
+
.map(|m| {
|
|
137
|
+
let path = m.crate_path.display().to_string().replace('\\', "/");
|
|
138
|
+
format!("{} = {{ path = {:?} }}\n", m.package_name, path)
|
|
139
|
+
})
|
|
140
|
+
.collect();
|
|
141
|
+
|
|
142
|
+
let mut more_deps = String::new();
|
|
143
|
+
more_deps.push_str(&tokio_dep);
|
|
144
|
+
if !native_deps.is_empty() {
|
|
145
|
+
more_deps.push_str(&format!("\n{}", native_deps));
|
|
146
|
+
}
|
|
147
|
+
if !extra_dependencies_toml.trim().is_empty() {
|
|
148
|
+
more_deps.push_str(&format!("\n{}", extra_dependencies_toml));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let rust_main = if generated_native_rs.is_some() {
|
|
152
|
+
inject_generated_native_mod(rust_code)
|
|
153
|
+
} else {
|
|
154
|
+
rust_code.to_string()
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
let tish_ui_path = std::path::Path::new(&runtime_path)
|
|
158
|
+
.parent()
|
|
159
|
+
.ok_or_else(|| "invalid tishlang_runtime path (no parent)".to_string())?
|
|
160
|
+
.join("tish_ui");
|
|
161
|
+
let ui_dep = if rust_code.contains("tishlang_ui") {
|
|
162
|
+
format!(
|
|
163
|
+
"\ntishlang_ui = {{ path = {:?}, default-features = false, features = [\"runtime\"] }}\n",
|
|
164
|
+
tish_ui_path.display().to_string().replace('\\', "/")
|
|
165
|
+
)
|
|
166
|
+
} else {
|
|
167
|
+
String::new()
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
let profile = nested_release_profile_toml();
|
|
171
|
+
let src_file = if build_config.artifact == NativeArtifact::StaticLib {
|
|
172
|
+
"lib.rs"
|
|
173
|
+
} else {
|
|
174
|
+
"main.rs"
|
|
175
|
+
};
|
|
176
|
+
let crate_section = if build_config.artifact == NativeArtifact::StaticLib {
|
|
177
|
+
format!(
|
|
178
|
+
r#"[lib]
|
|
179
|
+
name = "{}"
|
|
180
|
+
crate-type = ["staticlib"]
|
|
181
|
+
path = "src/lib.rs"
|
|
182
|
+
|
|
183
|
+
"#,
|
|
184
|
+
cargo_name
|
|
185
|
+
)
|
|
186
|
+
} else {
|
|
187
|
+
format!(
|
|
188
|
+
r#"[[bin]]
|
|
189
|
+
name = "{}"
|
|
190
|
+
path = "src/main.rs"
|
|
191
|
+
|
|
192
|
+
"#,
|
|
193
|
+
cargo_name
|
|
194
|
+
)
|
|
195
|
+
};
|
|
196
|
+
let cargo_toml = format!(
|
|
197
|
+
r#"[package]
|
|
198
|
+
name = "tish_output"
|
|
199
|
+
version = "0.1.0"
|
|
200
|
+
edition = "2021"
|
|
201
|
+
|
|
202
|
+
{}{}
|
|
203
|
+
[dependencies]
|
|
204
|
+
tishlang_runtime = {{ path = {:?}{} }}
|
|
205
|
+
{}{}"#,
|
|
206
|
+
crate_section, profile, runtime_path, features_str, more_deps, ui_dep
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
fs::write(build_dir.join("Cargo.toml"), cargo_toml)
|
|
210
|
+
.map_err(|e| format!("Cannot write Cargo.toml: {}", e))?;
|
|
211
|
+
if let Some(gen) = generated_native_rs {
|
|
212
|
+
fs::write(build_dir.join("src/generated_native.rs"), gen)
|
|
213
|
+
.map_err(|e| format!("Cannot write generated_native.rs: {}", e))?;
|
|
214
|
+
}
|
|
215
|
+
fs::write(build_dir.join("src").join(src_file), rust_main)
|
|
216
|
+
.map_err(|e| format!("Cannot write {}: {}", src_file, e))?;
|
|
217
|
+
|
|
218
|
+
let workspace_target = Path::new(&runtime_path)
|
|
219
|
+
.parent()
|
|
220
|
+
.and_then(|p| p.parent())
|
|
221
|
+
.map(|ws| ws.join("target"));
|
|
222
|
+
let target_dir = workspace_target.filter(|p| p.exists());
|
|
223
|
+
let cross = build_config.cargo_target.as_deref();
|
|
224
|
+
let release_sub = if let Some(triple) = cross {
|
|
225
|
+
format!("{triple}/release")
|
|
226
|
+
} else {
|
|
227
|
+
"release".to_string()
|
|
228
|
+
};
|
|
229
|
+
let binary_dir = target_dir
|
|
230
|
+
.as_ref()
|
|
231
|
+
.map(|t| t.join(&release_sub))
|
|
232
|
+
.unwrap_or_else(|| build_dir.join("target").join(&release_sub));
|
|
233
|
+
|
|
234
|
+
tishlang_build_utils::run_cargo_build(&build_dir, target_dir.as_deref(), cross)?;
|
|
235
|
+
|
|
236
|
+
let artifact = if build_config.artifact == NativeArtifact::StaticLib {
|
|
237
|
+
tishlang_build_utils::find_release_staticlib(&binary_dir, &cargo_name)?
|
|
238
|
+
} else {
|
|
239
|
+
tishlang_build_utils::find_release_binary(&binary_dir, &cargo_name)?
|
|
240
|
+
};
|
|
241
|
+
let target = if build_config.artifact == NativeArtifact::StaticLib {
|
|
242
|
+
if output_path.extension().is_some_and(|e| e == "a") {
|
|
243
|
+
output_path.to_path_buf()
|
|
244
|
+
} else if output_path.to_string_lossy().ends_with('/') || output_path.is_dir() {
|
|
245
|
+
output_path.join(format!("lib{out_stem}.a"))
|
|
246
|
+
} else {
|
|
247
|
+
output_path.with_extension("a")
|
|
248
|
+
}
|
|
249
|
+
} else {
|
|
250
|
+
tishlang_build_utils::resolve_output_path(output_path, out_stem)
|
|
251
|
+
};
|
|
252
|
+
tishlang_build_utils::copy_binary_to_output(&artifact, &target)?;
|
|
253
|
+
|
|
254
|
+
Ok(())
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/// Build several native binaries in **one** nested Cargo project (shared `tishlang_runtime` compile).
|
|
258
|
+
///
|
|
259
|
+
/// `bins` order must match `outputs`: each `(stem, rust_code, generated_native_rs)` pairs with
|
|
260
|
+
/// `outputs[i].0` (entry path — used only for validation) and `outputs[i].1` (final binary path).
|
|
261
|
+
pub(crate) fn build_many_via_cargo(
|
|
262
|
+
bins: Vec<(String, String, Option<String>)>,
|
|
263
|
+
native_modules: Vec<ResolvedNativeModule>,
|
|
264
|
+
features: &[String],
|
|
265
|
+
extra_dependencies_toml: &str,
|
|
266
|
+
needs_tokio: bool,
|
|
267
|
+
needs_ui: bool,
|
|
268
|
+
outputs: &[(&Path, &Path)],
|
|
269
|
+
project_root: Option<&Path>,
|
|
270
|
+
) -> Result<(), String> {
|
|
271
|
+
if bins.len() != outputs.len() {
|
|
272
|
+
return Err(format!(
|
|
273
|
+
"build_many_via_cargo: bins ({}) != outputs ({})",
|
|
274
|
+
bins.len(),
|
|
275
|
+
outputs.len()
|
|
276
|
+
));
|
|
277
|
+
}
|
|
278
|
+
for (i, (stem, _, _)) in bins.iter().enumerate() {
|
|
279
|
+
let entry = outputs[i].0;
|
|
280
|
+
let expect = entry.file_stem().and_then(|s| s.to_str()).unwrap_or("");
|
|
281
|
+
if expect != stem {
|
|
282
|
+
return Err(format!(
|
|
283
|
+
"build_many_via_cargo: stem mismatch at {}: {} vs {}",
|
|
284
|
+
i, stem, expect
|
|
285
|
+
));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
let batch_id = format!("many_{}", std::process::id());
|
|
290
|
+
let build_dir = tishlang_build_utils::create_build_dir("tish_build_many", &batch_id)?;
|
|
291
|
+
|
|
292
|
+
let runtime_path = tishlang_build_utils::find_runtime_path_for_project(project_root)?;
|
|
293
|
+
|
|
294
|
+
let runtime_features = runtime_features_for_cargo(features);
|
|
295
|
+
let runtime_refs: Vec<&str> = runtime_features.iter().map(String::as_str).collect();
|
|
296
|
+
let features_str = if runtime_refs.is_empty() {
|
|
297
|
+
String::new()
|
|
298
|
+
} else {
|
|
299
|
+
format!(", features = {:?}", runtime_refs)
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
let tokio_dep = if needs_tokio {
|
|
303
|
+
"\ntokio = { version = \"1\", features = [\"rt-multi-thread\", \"macros\"] }\n"
|
|
304
|
+
} else {
|
|
305
|
+
""
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
let native_deps: String = native_modules
|
|
309
|
+
.iter()
|
|
310
|
+
.filter(|m| m.use_path_dependency)
|
|
311
|
+
.map(|m| {
|
|
312
|
+
let path = m.crate_path.display().to_string().replace('\\', "/");
|
|
313
|
+
format!("{} = {{ path = {:?} }}\n", m.package_name, path)
|
|
314
|
+
})
|
|
315
|
+
.collect();
|
|
316
|
+
|
|
317
|
+
let mut more_deps = String::new();
|
|
318
|
+
more_deps.push_str(tokio_dep);
|
|
319
|
+
if !native_deps.is_empty() {
|
|
320
|
+
more_deps.push_str(&format!("\n{}", native_deps));
|
|
321
|
+
}
|
|
322
|
+
if !extra_dependencies_toml.trim().is_empty() {
|
|
323
|
+
more_deps.push_str(&format!("\n{}", extra_dependencies_toml));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
let tish_ui_path = std::path::Path::new(&runtime_path)
|
|
327
|
+
.parent()
|
|
328
|
+
.ok_or_else(|| "invalid tishlang_runtime path (no parent)".to_string())?
|
|
329
|
+
.join("tish_ui");
|
|
330
|
+
let ui_dep = if needs_ui {
|
|
331
|
+
format!(
|
|
332
|
+
"\ntishlang_ui = {{ path = {:?}, default-features = false, features = [\"runtime\"] }}\n",
|
|
333
|
+
tish_ui_path.display().to_string().replace('\\', "/")
|
|
334
|
+
)
|
|
335
|
+
} else {
|
|
336
|
+
String::new()
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
let mut bin_tables = String::new();
|
|
340
|
+
for (stem, rust_code, generated_native_rs) in &bins {
|
|
341
|
+
let bin_dir = build_dir.join("src/bin").join(stem);
|
|
342
|
+
fs::create_dir_all(&bin_dir).map_err(|e| format!("create bin dir: {}", e))?;
|
|
343
|
+
|
|
344
|
+
let rust_main = if generated_native_rs.is_some() {
|
|
345
|
+
inject_generated_native_mod(rust_code)
|
|
346
|
+
} else {
|
|
347
|
+
rust_code.clone()
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
fs::write(bin_dir.join("main.rs"), rust_main)
|
|
351
|
+
.map_err(|e| format!("write main.rs for {}: {}", stem, e))?;
|
|
352
|
+
if let Some(gen) = generated_native_rs {
|
|
353
|
+
fs::write(bin_dir.join("generated_native.rs"), gen)
|
|
354
|
+
.map_err(|e| format!("write generated_native.rs for {}: {}", stem, e))?;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
bin_tables.push_str(&format!(
|
|
358
|
+
r#"[[bin]]
|
|
359
|
+
name = "{stem}"
|
|
360
|
+
path = "src/bin/{stem}/main.rs"
|
|
361
|
+
|
|
362
|
+
"#
|
|
363
|
+
));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
let profile = nested_release_profile_toml();
|
|
367
|
+
let cargo_toml = format!(
|
|
368
|
+
r#"[package]
|
|
369
|
+
name = "tish_output_many"
|
|
370
|
+
version = "0.1.0"
|
|
371
|
+
edition = "2021"
|
|
372
|
+
|
|
373
|
+
{}{}
|
|
374
|
+
[dependencies]
|
|
375
|
+
tishlang_runtime = {{ path = {:?}{} }}
|
|
376
|
+
{}{}"#,
|
|
377
|
+
bin_tables, profile, runtime_path, features_str, more_deps, ui_dep
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
fs::write(build_dir.join("Cargo.toml"), cargo_toml)
|
|
381
|
+
.map_err(|e| format!("Cannot write Cargo.toml: {}", e))?;
|
|
382
|
+
|
|
383
|
+
let workspace_target = Path::new(&runtime_path)
|
|
384
|
+
.parent()
|
|
385
|
+
.and_then(|p| p.parent())
|
|
386
|
+
.map(|ws| ws.join("target"));
|
|
387
|
+
let target_dir = workspace_target.filter(|p| p.exists());
|
|
388
|
+
let binary_dir = target_dir
|
|
389
|
+
.as_ref()
|
|
390
|
+
.map(|t| t.join("release"))
|
|
391
|
+
.unwrap_or_else(|| build_dir.join("target").join("release"));
|
|
392
|
+
|
|
393
|
+
tishlang_build_utils::run_cargo_build(&build_dir, target_dir.as_deref(), None)?;
|
|
394
|
+
|
|
395
|
+
for i in 0..bins.len() {
|
|
396
|
+
let stem = bins[i].0.as_str();
|
|
397
|
+
let output_path = outputs[i].1;
|
|
398
|
+
let binary = tishlang_build_utils::find_release_binary(binary_dir.as_path(), stem)?;
|
|
399
|
+
let target = tishlang_build_utils::resolve_output_path(output_path, stem);
|
|
400
|
+
tishlang_build_utils::copy_binary_to_output(&binary, &target)?;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
Ok(())
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
#[cfg(test)]
|
|
407
|
+
mod tests {
|
|
408
|
+
use super::runtime_features_for_cargo;
|
|
409
|
+
|
|
410
|
+
#[test]
|
|
411
|
+
fn runtime_features_full_expands() {
|
|
412
|
+
let f = runtime_features_for_cargo(&["full".to_string()]);
|
|
413
|
+
assert!(f.contains(&"http".to_string()));
|
|
414
|
+
assert!(f.contains(&"fs".to_string()));
|
|
415
|
+
assert!(f.contains(&"process".to_string()));
|
|
416
|
+
assert!(f.contains(&"regex".to_string()));
|
|
417
|
+
assert!(f.contains(&"ws".to_string()));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
#[test]
|
|
421
|
+
fn runtime_features_merges_full_and_specific() {
|
|
422
|
+
let f = runtime_features_for_cargo(&["full".to_string(), "http".to_string()]);
|
|
423
|
+
// `full` expands to every RUNTIME_CARGO_FEATURES entry; redundant `http` must not duplicate.
|
|
424
|
+
assert_eq!(f.len(), super::RUNTIME_CARGO_FEATURES.len());
|
|
425
|
+
assert_eq!(f.iter().filter(|x| *x == "http").count(), 1);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//! Native build configuration (desktop binary vs iOS staticlib, cross-target).
|
|
2
|
+
|
|
3
|
+
use tishlang_compile::NativeEmitMode;
|
|
4
|
+
|
|
5
|
+
/// Output artifact kind for `tish build --target native`.
|
|
6
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
|
7
|
+
pub enum NativeArtifact {
|
|
8
|
+
#[default]
|
|
9
|
+
Bin,
|
|
10
|
+
StaticLib,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/// Options passed from the CLI into nested `cargo build`.
|
|
14
|
+
#[derive(Debug, Clone, Default)]
|
|
15
|
+
pub struct NativeBuildConfig {
|
|
16
|
+
pub artifact: NativeArtifact,
|
|
17
|
+
/// When set, run `cargo build --target <triple>` and skip `-C target-cpu=native`.
|
|
18
|
+
pub cargo_target: Option<String>,
|
|
19
|
+
pub emit_mode: NativeEmitMode,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl NativeBuildConfig {
|
|
23
|
+
pub fn desktop() -> Self {
|
|
24
|
+
Self::default()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
pub fn ios_staticlib(triple: &str) -> Self {
|
|
28
|
+
Self {
|
|
29
|
+
artifact: NativeArtifact::StaticLib,
|
|
30
|
+
cargo_target: Some(triple.to_string()),
|
|
31
|
+
emit_mode: NativeEmitMode::EmbeddedLib,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
pub fn is_cross_compile(&self) -> bool {
|
|
36
|
+
self.cargo_target.is_some()
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// Filter runtime features for iOS sandbox builds.
|
|
41
|
+
pub fn ios_runtime_features(features: &[String]) -> Vec<String> {
|
|
42
|
+
const ALLOW: &[&str] = &["http", "http-hyper", "regex", "timers"];
|
|
43
|
+
features
|
|
44
|
+
.iter()
|
|
45
|
+
.filter(|f| ALLOW.contains(&f.as_str()))
|
|
46
|
+
.cloned()
|
|
47
|
+
.collect()
|
|
48
|
+
}
|