diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f6b28278c..1e36ca89b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -628,6 +628,17 @@ jobs: # aarch64 must actually have run, not been skipped for want of qemu. echo "$out" | grep -q 'aarch64-linux-musl: built, ran the spec' + # A macOS dylib cross-linked on Linux: its install name and minimum are + # read from the Mach-O load commands. A Mac host rewrites the id after the + # link, so only a non-Mac host catches a cross link that left it unset. + - name: Cross-build a macOS dylib and check its identity + env: + AETHER_CACHE_DIR: ${{ github.workspace }}/.ci-cache/ae-cross + run: | + sh tests/integration/cross_macos_dylib_identity/test_cross_macos_dylib_identity.sh \ + | tee "$RUNNER_TEMP/macos-dylib.out" + grep -q '\[PASS\]' "$RUNNER_TEMP/macos-dylib.out" + - name: Save zig + SQLite cross object cache (merge to main, on miss only) if: >- github.event_name == 'push' && github.ref == 'refs/heads/main' diff --git a/compiler/aether_module.c b/compiler/aether_module.c index 1b78a648e..ca754cf29 100644 --- a/compiler/aether_module.c +++ b/compiler/aether_module.c @@ -702,6 +702,14 @@ const char* module_leaf_of_file(const char* path, char* buf, size_t cap) { return buf; } +/* Does `import (*)` bind `name`? What the module exports (all + * of it when it lists no exports), apart from a leading-underscore name, + * which is private either way. */ +int module_glob_binds(AetherModule* module, const char* name) { + if (!module || !name || !name[0] || name[0] == '_') return 0; + return module->export_count == 0 || module_exports_symbol(module, name); +} + int module_is_exported(AetherModule* module, const char* symbol) { if (!module) return 0; @@ -3214,6 +3222,16 @@ static void rename_refs_before_binding_in(ASTNode* scope, const char* prefix, names_free(&bound); } +/* Is `name`, in a body renamed under `prefix`, a module-level `var` the + * rename binds? One of `consts`, the module's own when the body is the + * module's, the imported ones when it is an importer's: a `var` the module + * keeps private is no cell of an importer's, whose `text = ...` or loop + * variable `text` is its own local (it was written to the module's cell, + * through `import m (*)`, as a write in the module's own body is). */ +static int bound_module_var(const char* prefix, const ModuleNames* consts, const char* name) { + return names_has(consts, name) && name_is_module_global_var(prefix, name); +} + // Recursively rename intra-module function calls and constant references in a cloned AST. // local_names/local_count: names bound in the enclosing function (params + locals) that shadow constants. static void rename_intra_module_refs(ASTNode* node, const char* prefix, @@ -3227,6 +3245,25 @@ static void rename_intra_module_refs(ASTNode* node, const char* prefix, * called `f` turned it into `_f`, a member T does not have). */ if (node->type == AST_OFFSETOF) return; + /* A struct literal's field initialiser (`Font { size: 2 }`) and a + * struct pattern's field (`Font{ size: s }`) are AST_ASSIGNMENTs whose + * value is the FIELD's name, never a variable: the module-var write + * rename below took `size: 2` for a write to a module's `var size` and + * the C named a field `_size` the struct does not have. Only + * the initialiser (or sub-pattern) is renamed. */ + if (node->type == AST_STRUCT_LITERAL || node->type == AST_PATTERN_STRUCT) { + for (int i = 0; i < node->child_count; i++) { + ASTNode* c = node->children[i]; + if (c && c->type == AST_ASSIGNMENT) { + for (int j = 0; j < c->child_count; j++) + rename_intra_module_refs(c->children[j], prefix, funcs, consts, locals); + } else { + rename_intra_module_refs(c, prefix, funcs, consts, locals); + } + } + return; + } + /* #3088: in `ns.member`, a namespace the module imports is that module, * whatever function of its own the module names `ns`: a function has * no fields, as the checker reads it (value_hides_namespace). Renamed @@ -3237,7 +3274,7 @@ static void rename_intra_module_refs(ASTNode* node, const char* prefix, node->children[0]->type == AST_IDENTIFIER && node->children[0]->value && names_has(funcs, node->children[0]->value) && !names_has(consts, node->children[0]->value) && - !name_is_module_global_var(prefix, node->children[0]->value) && + !bound_module_var(prefix, consts, node->children[0]->value) && !names_has(locals, node->children[0]->value)) { AetherModule* self = module_find_by_namespace(prefix); if (self && self->name && module_sees_namespace(self->name, node->children[0]->value) && @@ -3274,7 +3311,7 @@ static void rename_intra_module_refs(ASTNode* node, const char* prefix, * and above, but the call kept the bare name and the checker then * reported "Undefined function". A local of that name shadows it. */ if (!renamed && !names_has(locals, node->value) && - name_is_module_global_var(prefix, node->value)) { + bound_module_var(prefix, consts, node->value)) { char* prefixed = mod_strf("%s_%s", prefix, node->value); free(node->value); node->value = prefixed; @@ -3289,7 +3326,7 @@ static void rename_intra_module_refs(ASTNode* node, const char* prefix, const char* dot = renamed ? NULL : strchr(node->value, '.'); if (dot && dot != node->value && !strchr(dot + 1, '.')) { char* recv = mod_strf("%.*s", (int)(dot - node->value), node->value); - if (!names_has(locals, recv) && name_is_module_global_var(prefix, recv)) { + if (!names_has(locals, recv) && bound_module_var(prefix, consts, recv)) { char* prefixed = mod_strf("%s_%s%s", prefix, recv, dot); free(node->value); node->value = prefixed; @@ -3309,7 +3346,7 @@ static void rename_intra_module_refs(ASTNode* node, const char* prefix, * `const` lowers to a `#define`, and a `name = expr` against it is a real * shadowing local that must stay local. */ if ((node->type == AST_VARIABLE_DECLARATION || node->type == AST_ASSIGNMENT) && - node->value && name_is_module_global_var(prefix, node->value)) { + node->value && bound_module_var(prefix, consts, node->value)) { char* prefixed = mod_strf("%s_%s", prefix, node->value); free(node->value); node->value = prefixed; @@ -3381,7 +3418,7 @@ static void rename_intra_module_refs(ASTNode* node, const char* prefix, ModuleNames own = {0}; for (int i = 0; i < clo.count; i++) { if (!names_has(locals, clo.names[i]) && - !name_is_module_global_var(prefix, clo.names[i])) + !bound_module_var(prefix, consts, clo.names[i])) names_add(&own, clo.names[i]); } rename_refs_before_binding_in(node, prefix, funcs, &own); @@ -3392,7 +3429,7 @@ static void rename_intra_module_refs(ASTNode* node, const char* prefix, } ModuleNames kept = {0}; for (int i = 0; i < clo.count; i++) { - if (!name_is_module_global_var(prefix, clo.names[i])) names_add(&kept, clo.names[i]); + if (!bound_module_var(prefix, consts, clo.names[i])) names_add(&kept, clo.names[i]); } names_free(&clo); for (int i = 0; i < node->child_count; i++) { @@ -3424,7 +3461,7 @@ static void rename_intra_module_refs(ASTNode* node, const char* prefix, * global_var, so it's correctly retained. */ ModuleNames kept = {0}; for (int i = 0; i < nested.count; i++) { - if (!name_is_module_global_var(prefix, nested.names[i])) names_add(&kept, nested.names[i]); + if (!bound_module_var(prefix, consts, nested.names[i])) names_add(&kept, nested.names[i]); } names_free(&nested); rename_refs_before_binding_in(node, prefix, funcs, &kept); @@ -4112,8 +4149,9 @@ static void apply_inherited_selective_imports(ASTNode* clone, ASTNode* mod_ast) if (is_glob) { // #896: the glob selection is M's whole export surface. Take every - // func/const M defines (skipping leading-underscore privates, the - // same privacy rule the typecheck-time glob expansion applies) so + // func/const M exports (module_glob_binds: what its `exports` list + // names, and never a leading-underscore private -- the same rule + // the typecheck-time glob expansion applies) so // a bare `clean(...)` in M's merged body is rewritten to the // prefixed `fs_clean(...)` the transitive pass pulls in — exactly // what the selective and qualified forms already get. @@ -4124,13 +4162,13 @@ static void apply_inherited_selective_imports(ASTNode* clone, ASTNode* mod_ast) // `_` before this runs; this keeps M's own externs, which // keep their bare name, from being rewritten to the glob's. for (int k = 0; k < sub_func_count; k++) { - if (sub_func_names[k] && sub_func_names[k][0] != '_' && + if (module_glob_binds(sub_mod, sub_func_names[k]) && !module_defines_own_name(mod_ast, sub_func_names[k])) { names_add(&sel_funcs, sub_func_names[k]); } } for (int k = 0; k < sub_const_count; k++) { - if (sub_const_names[k] && sub_const_names[k][0] != '_' && + if (module_glob_binds(sub_mod, sub_const_names[k]) && !module_defines_own_name(mod_ast, sub_const_names[k])) { names_add(&sel_consts, sub_const_names[k]); } @@ -4150,7 +4188,7 @@ static void apply_inherited_selective_imports(ASTNode* clone, ASTNode* mod_ast) if (strncmp(ext->value, sub_ns, sub_ns_len) != 0 || ext->value[sub_ns_len] != '_') continue; const char* tail = ext->value + sub_ns_len + 1; - if (!*tail || tail[0] == '_') continue; + if (!module_glob_binds(sub_mod, tail)) continue; if (module_defines_own_name(mod_ast, tail)) continue; if (names_has(&sel_funcs, tail)) continue; names_add(&sel_funcs, tail); @@ -5380,6 +5418,10 @@ void module_merge_into_program(ASTNode* program) { for (int k = 0; k < mod_const_count; k++) { const char* cn = mod_const_names[k]; if (!cn || cn[0] == '_' || module_defines_own_name(program, cn)) continue; + /* Only what the module exports, as the checker binds it: + * a private `var text` renamed the importer's own loop + * variable `text` to the module's cell. */ + if (!module_glob_binds(mod, cn)) continue; names_add(&sel_consts, cn); } } @@ -5739,13 +5781,48 @@ static int prune_is_function_scope(const ASTNode* node) { node->type == AST_MAIN_FUNCTION; } +/* The function the walk is in: every name it binds (`locals`), and the + * ones bound so far in source order (`bound`), as far as the walk can + * tell: a block's bindings are dropped as it ends. */ +typedef struct { + NameSet locals; + const char** bound; + int bound_count; + int bound_cap; +} PruneScope; + /* A bare (undotted) name the enclosing function binds as a local. */ -static int prune_is_local(const NameSet* locals, const char* name) { - return locals && !strchr(name, '.') && nameset_contains(locals, name); +static int prune_is_local(const PruneScope* scope, const char* name) { + return scope && !strchr(name, '.') && nameset_contains(&scope->locals, name); +} + +/* Is the local `name` bound at this point of the walk? A call spelt like a + * local is a call through it only from its binding on: in `button = + * button(t)` the call on the right comes first and is the function, as + * the checker resolves it (docs/language-reference.md: a new local shadows + * a function of that name "for the rest of its scope"). Taken for the + * local, the call seeded nothing and the prune dropped the function its + * C still called. A binding the walk cannot place (one not at the level of + * a block, say) counts as unbound, which only seeds more. */ +static int prune_is_bound(const PruneScope* scope, const char* name) { + for (int i = scope ? scope->bound_count - 1 : -1; i >= 0; i--) + if (strcmp(scope->bound[i], name) == 0) return 1; + return 0; +} + +static void prune_bind(PruneScope* scope, const char* name) { + if (scope->bound_count >= scope->bound_cap) { + int new_cap = scope->bound_cap ? scope->bound_cap * 2 : 16; + const char** nb = realloc(scope->bound, sizeof(const char*) * (size_t)new_cap); + if (!nb) return; /* unbound: seeds more, never less */ + scope->bound = nb; + scope->bound_cap = new_cap; + } + scope->bound[scope->bound_count++] = name; } static void prune_collect_calls_in(ASTNode* node, NameSet* seen, NameStack* worklist, - const NameSet* locals, int in_dsl_block); + PruneScope* scope, int in_dsl_block); static void prune_collect_calls(ASTNode* node, NameSet* seen, NameStack* worklist) { prune_collect_calls_in(node, seen, worklist, NULL, 0); @@ -5756,8 +5833,9 @@ static void prune_collect_calls(ASTNode* node, NameSet* seen, NameStack* worklis * of the enclosing function's names (#333): such a call is a reference to * every function it may name, a local's name included. */ static void prune_collect_calls_in(ASTNode* node, NameSet* seen, NameStack* worklist, - const NameSet* locals, int in_dsl_block) { + PruneScope* scope, int in_dsl_block) { if (!node) return; + int bound_mark = scope ? scope->bound_count : 0; /* A `hide` / `seal except` list names what a block may reach; it calls * nothing. Walked, `seal except bytes` kept every bytes function a * name matched, so a seal changed what reached the C. */ @@ -5771,8 +5849,8 @@ static void prune_collect_calls_in(ASTNode* node, NameSet* seen, NameStack* work return; } if (prune_is_function_scope(node)) { - NameSet own = {0}; - prune_collect_locals(node, &own); + PruneScope own = {0}; + prune_collect_locals(node, &own.locals); for (int i = 0; i < node->child_count; i++) { prune_collect_calls_in(node->children[i], seen, worklist, &own, in_dsl_block); } @@ -5781,11 +5859,13 @@ static void prune_collect_calls_in(ASTNode* node, NameSet* seen, NameStack* work namestack_push(worklist, node->annotation); } } - nameset_free(&own); + nameset_free(&own.locals); + free(own.bound); return; } if (node->type == AST_FUNCTION_CALL && node->value && - (in_dsl_block || !prune_is_local(locals, node->value))) { + (in_dsl_block || !prune_is_local(scope, node->value) || + !prune_is_bound(scope, node->value))) { if (nameset_add(seen, node->value)) { namestack_push(worklist, node->value); } @@ -5824,7 +5904,7 @@ static void prune_collect_calls_in(ASTNode* node, NameSet* seen, NameStack* work // every identifier name is a sound over-approximation: non-function // names won't match any function definition and harmlessly dead-end. if (node->type == AST_IDENTIFIER && node->value && - !prune_is_local(locals, node->value)) { + !prune_is_local(scope, node->value)) { if (nameset_add(seen, node->value)) { namestack_push(worklist, node->value); } @@ -5871,7 +5951,15 @@ static void prune_collect_calls_in(ASTNode* node, NameSet* seen, NameStack* work int dsl = in_dsl_block || (node->type == AST_FUNCTION_CALL && node->value && strchr(node->value, '.') && ast_is_trailing_block(c)); - prune_collect_calls_in(c, seen, worklist, locals, dsl); + prune_collect_calls_in(c, seen, worklist, scope, dsl); + } + /* What the node bound inside it ends with it; a binding itself stays + * bound for the rest of its block (its initialiser, walked above, came + * before it). */ + if (scope) { + scope->bound_count = bound_mark; + if (prune_binds_name(node) && !strchr(node->value, '.')) + prune_bind(scope, node->value); } } diff --git a/compiler/aether_module.h b/compiler/aether_module.h index ca7d412e4..80623890e 100644 --- a/compiler/aether_module.h +++ b/compiler/aether_module.h @@ -131,6 +131,7 @@ int module_is_exported(AetherModule* module, const char* symbol); /* #2172: exported as `name` or as the prefixed `_name` a qualified * call resolves to. What export enforcement asks. */ int module_exports_symbol(AetherModule* module, const char* name); +int module_glob_binds(AetherModule* module, const char* name); /* #924: if `module` re-exports `symbol` (lists it in `exports` but imports * it from elsewhere), return the module that actually defines it (resolved * transitively). NULL when `module` itself is the definer. */ diff --git a/compiler/analysis/typechecker.c b/compiler/analysis/typechecker.c index 679c745a5..fbefa5fd8 100644 --- a/compiler/analysis/typechecker.c +++ b/compiler/analysis/typechecker.c @@ -8362,6 +8362,11 @@ static int typecheck_program_checked(ASTNode* program) { if (sym->name[ns_len] != '_') continue; const char* tail = sym->name + ns_len + 1; if (!*tail || *tail == '_') continue; // private / malformed + /* A glob binds what the module exports, as `ns.name` + * reaches it: a module's private `var text` was bound + * too, and an importer's own loop variable or local + * `text` became the module's cell. */ + if (is_export_blocked(ns, tail)) continue; if (glob_count >= glob_cap) { glob_cap = glob_cap == 0 ? 16 : glob_cap * 2; glob_names = (const char**)aether_xrealloc( @@ -22238,8 +22243,17 @@ static int typecheck_function_call_inner(ASTNode* call, SymbolTable* table) { * module's merged body, whose bare names are its own. */ if (symbol && call->value && !strchr(call->value, '.') && !table->merged_from && !symbol->import_bound && find_import_alias(call->value)) { - Symbol* aliased = lookup_qualified_symbol(table, find_import_alias(call->value)); - if (aliased && aliased->is_function) symbol = aliased; + /* Not over a local or parameter of the name: from its binding on + * it hides the imported function, as it hides the program's own + * (#2683, E0301 below). Taken for the import, `button = 0` then + * `button = button(t)` called it, and the prune, which read the + * call as the local's, had dropped the function. */ + SymbolTable* root = table; + while (root->parent) root = root->parent; + if (lookup_symbol_local(root, call->value) == symbol) { + Symbol* aliased = lookup_qualified_symbol(table, find_import_alias(call->value)); + if (aliased && aliased->is_function) symbol = aliased; + } } /* #924 re-export: a `hub.fn(...)` call where hub re-exports fn from @@ -22498,9 +22512,13 @@ static int typecheck_function_call_inner(ASTNode* call, SymbolTable* table) { Symbol* fn = lookup_symbol_local(root, call->value); if (fn && fn->is_function) { const char* hint = ""; - error_msg = aether_internf( - "'%s' is a local here, which hides the function '%s'", - call->value, call->value); + /* An imported one is named as its module has it. */ + const char* imported = fn->import_bound ? find_import_alias(call->value) : NULL; + error_msg = imported + ? aether_internf("'%s' is a local here, which hides the imported function '%s'", + call->value, imported) + : aether_internf("'%s' is a local here, which hides the function '%s'", + call->value, call->value); hint = aether_internf( "a local's scope starts after its initializer and lasts to the end " "of the function, so the function cannot be called as '%s' here; " diff --git a/docs/build-system.md b/docs/build-system.md index 87e39dc23..c57ea1c5b 100644 --- a/docs/build-system.md +++ b/docs/build-system.md @@ -1083,6 +1083,20 @@ an executable link is the whole increment. | `*-macos`, `*-ios` | `.dylib` | `-dynamiclib -install_name @rpath/` | | `wasm32-*` | `.wasm` | `--no-entry --gc-sections` + `--export=` per symbol | +A macOS dylib gets the same identity on every host: its `LC_ID_DYLIB` is +`@rpath/`, as the native macOS build sets it, so a program linked +against it finds it through its own rpath. Every macOS cross build (library or +executable) is stamped with an explicit deployment minimum, **macOS 13.0** by +default rather than zig's own default (15.0); set `AETHER_MACOS_MIN` to choose +another, as `AETHER_IOS_MIN` does for iOS: + +```bash +AETHER_MACOS_MIN=14.0 ae build --target=aarch64-macos --emit=lib mylib.ae -o libmylib.dylib +``` + +An ELF `.so` gets no `DT_SONAME`, as on the native path: a consumer linking +`-lmylib` records `libmylib.so`. + Windows needs `--export-all-symbols` for the reason the native path documents (#993): GCC's auto-export heuristic switches off as soon as any symbol carries an explicit `__declspec(dllexport)`, and the `aether_` catalog exports diff --git a/docs/language-reference.md b/docs/language-reference.md index 59fb87c91..f5b5f48cd 100644 --- a/docs/language-reference.md +++ b/docs/language-reference.md @@ -4221,6 +4221,11 @@ This is Aether's spelling for what other languages call "unqualified import" (Rust's `use mod::*;`, Java's `import static mod.*`, Python's `from mod import *`). +In a module with an `exports (…)` list, public means listed: a function, +constant or module-level `var` the list leaves out is not bound by the +glob, so a local, loop variable or parameter of the importer spelt like +one is the importer's own, never the module's cell. + ```aether import std.math (*) diff --git a/new_changelogs/20261124T200149Z-fixed-glob-import-struct-field-name.md b/new_changelogs/20261124T200149Z-fixed-glob-import-struct-field-name.md new file mode 100644 index 000000000..3a5a99318 --- /dev/null +++ b/new_changelogs/20261124T200149Z-fixed-glob-import-struct-field-name.md @@ -0,0 +1,6 @@ +- **A struct literal's field is no longer renamed after a module `var` of + the same name.** A module with `var size` and a struct field `size` had + every `Font { size: 2 }` (in its own functions, and in a file that + glob-imports it) lowered to a field `_size` the struct does not + have: the merge took the field initialiser for a write to the var. The + field of a struct literal or a struct pattern is never a variable. diff --git a/new_changelogs/20261124T200317Z-fixed-glob-import-binds-exports-only.md b/new_changelogs/20261124T200317Z-fixed-glob-import-binds-exports-only.md new file mode 100644 index 000000000..e939ca2c3 --- /dev/null +++ b/new_changelogs/20261124T200317Z-fixed-glob-import-binds-exports-only.md @@ -0,0 +1,9 @@ +- **`import m (*)` binds what `m` exports, not its private `var`s.** The + glob bound every name of the module, a private module-level `var text` + included, so an importer's own loop variable or local `text` was the + module's cell (`m_text = ` on a `string[]`, failing in the C, + or a silent write to the module's state). A glob now binds only what + the module's `exports (…)` list names (everything, without a list), in + a program and in a module that glob-imports another, and a write to a + module `var` is renamed to the module's cell only when the import binds + that var. diff --git a/new_changelogs/20261124T200554Z-fixed-glob-import-local-named-like-fn.md b/new_changelogs/20261124T200554Z-fixed-glob-import-local-named-like-fn.md new file mode 100644 index 000000000..c2d2272c0 --- /dev/null +++ b/new_changelogs/20261124T200554Z-fixed-glob-import-local-named-like-fn.md @@ -0,0 +1,8 @@ +- **A local named like an imported function no longer breaks the build.** + After `import m (*)` (or `import m (button)`), `button = button(t)` in a + loop called an undeclared `m_button` in the C: the dead-code prune read + the right-hand call as a call through the local and dropped the + function. A call before the local's binding is the function again, and + one after it, which the checker used to take for the import as well, is + now E0301 "'button' is a local here, which hides the imported function + 'm.button'", as for a program's own function (#2683). diff --git a/new_changelogs/20261124T205927Z-fixed-cross-macos-dylib-identity.md b/new_changelogs/20261124T205927Z-fixed-cross-macos-dylib-identity.md new file mode 100644 index 000000000..62d89b1f8 --- /dev/null +++ b/new_changelogs/20261124T205927Z-fixed-cross-macos-dylib-identity.md @@ -0,0 +1,11 @@ +- **A macOS `--emit=lib` cross build from Linux or FreeBSD is now a loadable + dylib.** zig spells the macOS targets `aarch64-macos-none`, with no + "-apple-" in them, so the cross link took them for ELF: no install name + (zig recorded the `-o` build path as the dylib's id, and every program + linked against it failed with "Library not loaded"), GNU strip flags under + `--size`, and zig's default minimum of macOS 15.0. Every Apple spelling now + links as Mach-O: `-dynamiclib -install_name @rpath/`, as the native + macOS build sets it, and Apple ld's `-x -dead_strip`. Every macOS cross build + is stamped with an explicit minimum, macOS 13.0 by default, overridden with + `AETHER_MACOS_MIN`. A Mac host had hidden the id half by rewriting it with + install_name_tool afterwards. diff --git a/tests/ae_sweep_prune.txt b/tests/ae_sweep_prune.txt index 559c26ab2..4940709c9 100644 --- a/tests/ae_sweep_prune.txt +++ b/tests/ae_sweep_prune.txt @@ -408,3 +408,7 @@ tests/integration/http_server_h2_drain_host/ # deadline.ae runs from its driver, which also runs it with BROKEN_CANCELLER=1 # to show the descriptor race check can fail. tests/integration/tcp_deadline_cancel/ +# shadowed.ae is an E0301 fixture; test_glob_import_local_named_like_fn.sh runs both. +tests/integration/glob_import_local_named_like_fn/ +# mylib.ae is a library (no main) the driver cross-builds for macOS; test_cross_macos_dylib_identity.sh runs it. +tests/integration/cross_macos_dylib_identity/ diff --git a/tests/integration/cross_macos_dylib_identity/consumer.c b/tests/integration/cross_macos_dylib_identity/consumer.c new file mode 100644 index 000000000..3d64b0958 --- /dev/null +++ b/tests/integration/cross_macos_dylib_identity/consumer.c @@ -0,0 +1,4 @@ +/* Links against the cross-built dylib by -l, then calls through it. Run on a + * macOS host only: it proves dyld finds the library by its install name. */ +int aether_greet(void); +int main(void) { return aether_greet() == 42 ? 0 : 1; } diff --git a/tests/integration/cross_macos_dylib_identity/macho_id.c b/tests/integration/cross_macos_dylib_identity/macho_id.c new file mode 100644 index 000000000..80443c0b3 --- /dev/null +++ b/tests/integration/cross_macos_dylib_identity/macho_id.c @@ -0,0 +1,60 @@ +/* Print a 64-bit Mach-O dylib's identity as the dynamic loader reads it: + * + * type=dylib + * id= + * minos= + * + * Read straight from the load commands so the test runs on a Linux host, + * where there is no otool. Little-endian only (every macOS architecture ae + * targets is). Exits 2 on a file that is not a 64-bit Mach-O. */ +#include +#include +#include +#include + +static uint32_t rd32(const unsigned char* p) { + return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; +} + +static void print_version(const char* key, uint32_t v) { + unsigned major = v >> 16, minor = (v >> 8) & 0xff, patch = v & 0xff; + if (patch) printf("%s=%u.%u.%u\n", key, major, minor, patch); + else printf("%s=%u.%u\n", key, major, minor); +} + +int main(int argc, char** argv) { + if (argc != 2) { fprintf(stderr, "usage: macho_id FILE\n"); return 2; } + FILE* f = fopen(argv[1], "rb"); + if (!f) { perror(argv[1]); return 2; } + fseek(f, 0, SEEK_END); + long n = ftell(f); + fseek(f, 0, SEEK_SET); + if (n < 32) { fprintf(stderr, "%s: too short\n", argv[1]); return 2; } + unsigned char* b = malloc((size_t)n); + if (!b || fread(b, 1, (size_t)n, f) != (size_t)n) { fprintf(stderr, "read failed\n"); return 2; } + fclose(f); + + if (rd32(b) != 0xfeedfacfu) { fprintf(stderr, "%s: not a 64-bit Mach-O\n", argv[1]); return 2; } + uint32_t filetype = rd32(b + 12), ncmds = rd32(b + 16), sizeofcmds = rd32(b + 20); + printf("type=%s\n", filetype == 6 ? "dylib" : filetype == 2 ? "execute" : "other"); + if (32 + (uint64_t)sizeofcmds > (uint64_t)n) { fprintf(stderr, "load commands truncated\n"); return 2; } + + size_t off = 32; + for (uint32_t i = 0; i < ncmds; i++) { + if (off + 8 > 32 + (size_t)sizeofcmds) break; + uint32_t cmd = rd32(b + off), size = rd32(b + off + 4); + if (size < 8 || off + size > 32 + (size_t)sizeofcmds) break; + if (cmd == 0xd && size >= 24) { /* LC_ID_DYLIB */ + uint32_t name = rd32(b + off + 8); + if (name < size) printf("id=%.*s\n", (int)strnlen((const char*)b + off + name, size - name), + (const char*)b + off + name); + } else if (cmd == 0x32 && size >= 24) { /* LC_BUILD_VERSION */ + print_version("minos", rd32(b + off + 12)); + } else if (cmd == 0x24 && size >= 16) { /* LC_VERSION_MIN_MACOSX */ + print_version("minos", rd32(b + off + 8)); + } + off += size; + } + free(b); + return 0; +} diff --git a/tests/integration/cross_macos_dylib_identity/mylib.ae b/tests/integration/cross_macos_dylib_identity/mylib.ae new file mode 100644 index 000000000..1b8e4d95f --- /dev/null +++ b/tests/integration/cross_macos_dylib_identity/mylib.ae @@ -0,0 +1,6 @@ +// Library-shaped source for the macOS dylib identity test: no main, one +// exported function. + +greet() -> int { + return 42 +} diff --git a/tests/integration/cross_macos_dylib_identity/test_cross_macos_dylib_identity.sh b/tests/integration/cross_macos_dylib_identity/test_cross_macos_dylib_identity.sh new file mode 100755 index 000000000..ce9e3666e --- /dev/null +++ b/tests/integration/cross_macos_dylib_identity/test_cross_macos_dylib_identity.sh @@ -0,0 +1,92 @@ +#!/bin/sh +# A macOS `--emit=lib` cross build carries a loadable identity, whatever host +# ran it. +# +# zig spells the macOS targets `aarch64-macos-none` / `x86_64-macos-none`, with +# no "-apple-" in them, and the cross link took only "-apple-" for an Apple +# target. A macOS dylib linked on Linux or FreeBSD therefore went through the +# ELF arm: no -install_name, so zig recorded the -o path as LC_ID_DYLIB (a +# release built on CI said /home/runner/work/.../libfoo.so, and every program +# linked against it failed with "Library not loaded"), and no deployment +# target, so the minimum was zig's default (15.0) rather than one ae chose. +# A Mac host rewrote the id afterwards with install_name_tool, which hid the +# first half there. +# +# Asserts, reading the load commands directly (no otool on a Linux runner): +# - the artifact is a Mach-O dylib, its LC_ID_DYLIB @rpath/; +# - its minos is ae's documented default (13.0), and AETHER_MACOS_MIN +# overrides it; +# - --size links with the Mach-O strip spellings (x86_64 build); +# - on a macOS host of the same architecture, a C program linked -lgreet +# loads the library by that id and calls into it. +# +# Skips without zig or a host C compiler. Cost: two cross links, each +# compiling the runtime and stdlib for its target. + +set -e + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" +AE="$ROOT/build/ae" +NAME=cross_macos_dylib_identity +HOSTCC="${CC:-cc}" + +if ! command -v zig >/dev/null 2>&1; then + echo " [SKIP] $NAME: zig not on PATH" + exit 0 +fi +if ! command -v "$HOSTCC" >/dev/null 2>&1; then + echo " [SKIP] $NAME: no host C compiler ($HOSTCC)" + exit 0 +fi + +T="$(mktemp -d)" +trap 'rm -rf "$T"' EXIT INT TERM + +fail() { echo " [FAIL] $NAME: $*"; exit 1; } + +"$HOSTCC" -O2 -o "$T/macho_id" "$SCRIPT_DIR/macho_id.c" || fail "could not build macho_id" + +# check +check() { + "$T/macho_id" "$1" > "$T/id.txt" || fail "$1 is not a 64-bit Mach-O" + grep -qx 'type=dylib' "$T/id.txt" || fail "$1 is not a dylib: $(cat "$T/id.txt")" + want_id="@rpath/$(basename "$1")" + got_id="$(sed -n 's/^id=//p' "$T/id.txt")" + [ "$got_id" = "$want_id" ] || fail "$1: LC_ID_DYLIB is '$got_id', want '$want_id'" + got_min="$(sed -n 's/^minos=//p' "$T/id.txt")" + [ "$got_min" = "$2" ] || fail "$1: minos is '$got_min', want '$2'" +} + +# Deep build directory, so a recorded -o path is unmistakable. +mkdir -p "$T/build/out/arm64" "$T/build/out/x86_64" + +if ! "$AE" build --target=aarch64-macos --emit=lib "$SCRIPT_DIR/mylib.ae" \ + -o "$T/build/out/arm64/libgreet.dylib" > "$T/arm64.log" 2>&1; then + sed 's/^/ /' "$T/arm64.log" | tail -15 + fail "aarch64-macos --emit=lib did not build" +fi +check "$T/build/out/arm64/libgreet.dylib" 13.0 + +if ! AETHER_MACOS_MIN=14.2 "$AE" build --size --target=x86_64-macos --emit=lib \ + "$SCRIPT_DIR/mylib.ae" -o "$T/build/out/x86_64/libgreet.dylib" > "$T/x86_64.log" 2>&1; then + sed 's/^/ /' "$T/x86_64.log" | tail -15 + fail "x86_64-macos --size --emit=lib did not build" +fi +check "$T/build/out/x86_64/libgreet.dylib" 14.2 + +# A consumer resolves the library through the id: only where it can run. +if [ "$(uname -s)" = Darwin ]; then + case "$(uname -m)" in + arm64) dir="$T/build/out/arm64" ;; + x86_64) dir="$T/build/out/x86_64" ;; + *) dir="" ;; + esac + if [ -n "$dir" ]; then + "$HOSTCC" -o "$T/consumer" "$SCRIPT_DIR/consumer.c" -L"$dir" -lgreet \ + -Wl,-rpath,"$dir" || fail "consumer did not link against libgreet.dylib" + "$T/consumer" || fail "consumer did not load libgreet.dylib or got the wrong answer" + fi +fi + +echo " [PASS] $NAME" diff --git a/tests/integration/glob_import_local_named_like_fn/lib/buttons.ae b/tests/integration/glob_import_local_named_like_fn/lib/buttons.ae new file mode 100644 index 000000000..347b3332b --- /dev/null +++ b/tests/integration/glob_import_local_named_like_fn/lib/buttons.ae @@ -0,0 +1,9 @@ +// A module exporting a function the importer also uses as a local's name. +exports(button) + +var text: string[] = [] + +button(t: string) -> int { + text.push(t) + return text.len +} diff --git a/tests/integration/glob_import_local_named_like_fn/main.ae b/tests/integration/glob_import_local_named_like_fn/main.ae new file mode 100644 index 000000000..77c7694c9 --- /dev/null +++ b/tests/integration/glob_import_local_named_like_fn/main.ae @@ -0,0 +1,8 @@ +import buttons(*) + +main() { + for t in ["a", "b"] { + button = button(t) + println(button) + } +} diff --git a/tests/integration/glob_import_local_named_like_fn/shadowed.ae b/tests/integration/glob_import_local_named_like_fn/shadowed.ae new file mode 100644 index 000000000..af3e46151 --- /dev/null +++ b/tests/integration/glob_import_local_named_like_fn/shadowed.ae @@ -0,0 +1,7 @@ +import buttons(*) + +main() { + button = 0 + button = button("a") + println(button) +} diff --git a/tests/integration/glob_import_local_named_like_fn/test_glob_import_local_named_like_fn.sh b/tests/integration/glob_import_local_named_like_fn/test_glob_import_local_named_like_fn.sh new file mode 100755 index 000000000..0df6dc0f5 --- /dev/null +++ b/tests/integration/glob_import_local_named_like_fn/test_glob_import_local_named_like_fn.sh @@ -0,0 +1,54 @@ +#!/bin/sh +# A local of the importer named like a glob-imported function. +# `button = button(t)` declares a local `button` that hides the function +# from its binding on (docs/language-reference.md, #2683); the call on its +# own right-hand side comes before the binding, so it is the module's +# function, and the checker resolves it so. The dead-code prune took every +# call spelt like a local of the function for a call through that local and +# dropped buttons_button, so the C called an undeclared function. +# +# A call after the binding is through the local, which is no function: the +# checker took it for the import instead and the C called the dropped +# function. It is E0301 now, as for a program's own function, naming the +# local and the imported function. +# +# Acceptance: +# 1. main.ae prints exactly: 1 2 +# 2. shadowed.ae fails to build with E0301 naming 'button' and +# 'buttons.button', not with a C compiler error. + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" +AE="$ROOT/build/ae" +NAME=glob_import_local_named_like_fn + +out=$( cd "$SCRIPT_DIR" && AETHER_HOME="" "$AE" run main.ae 2>&1 ) +rc=$? +if [ "$rc" -ne 0 ]; then + echo " [FAIL] $NAME: main.ae errored (rc=$rc)" + echo "$out" | head -20 | sed 's/^/ /' + exit 1 +fi + +got=$(printf '%s\n' "$out" | grep -v '^[[:space:]]*$' | tr '\n' ' ' | sed 's/ *$//') +if [ "$got" != "1 2" ]; then + echo " [FAIL] $NAME: expected '1 2', got '$got'" + echo "$out" | head -20 | sed 's/^/ /' + exit 1 +fi + +out=$( cd "$SCRIPT_DIR" && AETHER_HOME="" "$AE" run shadowed.ae 2>&1 ) +rc=$? +if [ "$rc" -eq 0 ]; then + echo " [FAIL] $NAME: shadowed.ae built; a call through the local must be E0301" + echo "$out" | head -20 | sed 's/^/ /' + exit 1 +fi +if ! printf '%s\n' "$out" | grep -q "E0301.*'button' is a local here, which hides the imported function 'buttons.button'"; then + echo " [FAIL] $NAME: shadowed.ae did not fail with the E0301 naming both" + echo "$out" | head -20 | sed 's/^/ /' + exit 1 +fi + +echo " [PASS] $NAME" +exit 0 diff --git a/tests/integration/glob_import_private_var_hidden/lib/shout.ae b/tests/integration/glob_import_private_var_hidden/lib/shout.ae new file mode 100644 index 000000000..4350477a4 --- /dev/null +++ b/tests/integration/glob_import_private_var_hidden/lib/shout.ae @@ -0,0 +1,11 @@ +// A module that glob-imports textlib and has a local named like textlib's +// private `var text`: the local is its own, not textlib's cell. +import textlib(*) + +exports(shout) + +shout(t: string) -> string { + text = "${t}!" + record(text) + return text +} diff --git a/tests/integration/glob_import_private_var_hidden/lib/textlib.ae b/tests/integration/glob_import_private_var_hidden/lib/textlib.ae new file mode 100644 index 000000000..0034b07c9 --- /dev/null +++ b/tests/integration/glob_import_private_var_hidden/lib/textlib.ae @@ -0,0 +1,18 @@ +// A module with private module-level `var`s. A glob import brings in what +// the module exports; its private vars are not visible to the importer. +exports(record, count, LIMIT) + +const LIMIT = 10 + +var size: int = 0 +var text: string[] = [] + +record(t: string) -> int { + text.push(t) + size = text.len + return size +} + +count() -> int { + return text.len +} diff --git a/tests/integration/glob_import_private_var_hidden/main.ae b/tests/integration/glob_import_private_var_hidden/main.ae new file mode 100644 index 000000000..13ccbd23a --- /dev/null +++ b/tests/integration/glob_import_private_var_hidden/main.ae @@ -0,0 +1,15 @@ +import textlib(*) +import shout(*) + +main() { + for text in ["a", "b"] { + println(text) + } + size = 7 + println(size) + println(count()) + println(LIMIT) + println(record("c")) + println(shout("d")) + println(count()) +} diff --git a/tests/integration/glob_import_private_var_hidden/test_glob_import_private_var_hidden.sh b/tests/integration/glob_import_private_var_hidden/test_glob_import_private_var_hidden.sh new file mode 100755 index 000000000..5a7a45da5 --- /dev/null +++ b/tests/integration/glob_import_private_var_hidden/test_glob_import_private_var_hidden.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# A glob import binds what the module exports, nothing else. +# lib/textlib.ae has private module-level `var`s `text` and `size`. +# `import textlib (*)` bound them in the importer anyway, so its loop +# variable `text` and its local `size` were the module's cells +# (`textlib_text = ` on a string[] in the C). A private var is not +# visible to the importer, and an importer's local always shadows; the +# same for a module that glob-imports it (lib/shout.ae). +# +# Acceptance, main.ae prints exactly: a b 7 0 10 1 d! 2 + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" +AE="$ROOT/build/ae" +NAME=glob_import_private_var_hidden + +out=$( cd "$SCRIPT_DIR" && AETHER_HOME="" "$AE" run main.ae 2>&1 ) +rc=$? +if [ "$rc" -ne 0 ]; then + echo " [FAIL] $NAME: program errored (rc=$rc)" + echo "$out" | head -20 | sed 's/^/ /' + exit 1 +fi + +got=$(printf '%s\n' "$out" | grep -v '^[[:space:]]*$' | tr '\n' ' ' | sed 's/ *$//') +if [ "$got" != "a b 7 0 10 1 d! 2" ]; then + echo " [FAIL] $NAME: expected 'a b 7 0 10 1 d! 2', got '$got'" + echo "$out" | head -20 | sed 's/^/ /' + exit 1 +fi + +echo " [PASS] $NAME" +exit 0 diff --git a/tests/integration/glob_import_struct_field_name/lib/fontlib.ae b/tests/integration/glob_import_struct_field_name/lib/fontlib.ae new file mode 100644 index 000000000..3cfcd96de --- /dev/null +++ b/tests/integration/glob_import_struct_field_name/lib/fontlib.ae @@ -0,0 +1,20 @@ +// A module whose private module-level `var` is spelt like a field of the +// struct it exports. The field name in a struct literal (and in a field +// read) is never a reference to that var. +exports(Font, make_font, font_size) + +struct Font { + face: string, + size: int +} + +var size: int = 0 + +make_font(face: string, n: int) -> Font { + size = size + 1 + return Font { face: face, size: n } +} + +font_size(f: Font) -> int { + return f.size + size * 100 +} diff --git a/tests/integration/glob_import_struct_field_name/main.ae b/tests/integration/glob_import_struct_field_name/main.ae new file mode 100644 index 000000000..4fc8d2712 --- /dev/null +++ b/tests/integration/glob_import_struct_field_name/main.ae @@ -0,0 +1,12 @@ +import fontlib(*) + +const f = Font { face: "mono", size: 2 } + +main() { + g = Font { face: "serif", size: 3 } + println(f.size) + println(g.size) + h = make_font("sans", 4) + println(h.size) + println(font_size(h)) +} diff --git a/tests/integration/glob_import_struct_field_name/test_glob_import_struct_field_name.sh b/tests/integration/glob_import_struct_field_name/test_glob_import_struct_field_name.sh new file mode 100755 index 000000000..dc880b376 --- /dev/null +++ b/tests/integration/glob_import_struct_field_name/test_glob_import_struct_field_name.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# A field name in a struct literal is never a reference to a variable. +# lib/fontlib.ae has a private module-level `var size` beside +# `struct Font { face, size }`. The merge's rename of a module var's writes +# took the literal's `size: n` field initialiser (an AST_ASSIGNMENT naming the +# field) for a write to the var, so the C named a field `fontlib_size` the +# struct does not have: in the module's own make_font, and through the glob +# in the importer's `Font { size: 2 }`. +# +# Acceptance, main.ae prints exactly: 2 3 4 104 (104 = field 4 + 100 * the +# var, written once by make_font). + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" +AE="$ROOT/build/ae" +NAME=glob_import_struct_field_name + +out=$( cd "$SCRIPT_DIR" && AETHER_HOME="" "$AE" run main.ae 2>&1 ) +rc=$? +if [ "$rc" -ne 0 ]; then + echo " [FAIL] $NAME: program errored (rc=$rc)" + echo "$out" | head -20 | sed 's/^/ /' + exit 1 +fi + +got=$(printf '%s\n' "$out" | grep -v '^[[:space:]]*$' | tr '\n' ' ' | sed 's/ *$//') +if [ "$got" != "2 3 4 104" ]; then + echo " [FAIL] $NAME: expected '2 3 4 104', got '$got'" + echo "$out" | head -20 | sed 's/^/ /' + exit 1 +fi + +echo " [PASS] $NAME" +exit 0 diff --git a/tests/integration/tcp_deadline_cancel/deadline.ae b/tests/integration/tcp_deadline_cancel/deadline.ae index 39e952dae..11f116a0f 100644 --- a/tests/integration/tcp_deadline_cancel/deadline.ae +++ b/tests/integration/tcp_deadline_cancel/deadline.ae @@ -328,11 +328,24 @@ main() { r = 0 while r < race_rounds && why == "" { c, ce = tcp.cancel_new() - if ce != "" { why = "cancel_new: ${ce}" } - done = sync.atomic_new(0) + if ce != "" { + why = "round ${r}: cancel_new: ${ce}" + break + } + // Dialled with the cancel bound and no deadline, then given the + // few-ms deadline once connected: the race is the deadline and the + // cancel against the read and the close, and a deadline that a slow + // or loaded host spends on the dial itself would end the round + // before it starts (connect_until refuses it: "deadline exceeded"). + sock, de = tcp.connect_until("127.0.0.1", pe.port, 0, c) + if de != "" { + why = "round ${r}: dial: ${de}" + tcp.cancel_free(c) + break + } jitter = (now() % 7) as int - sock, de = tcp.connect_until("127.0.0.1", pe.port, now() + 3 + jitter, c) - if de != "" { why = "dial: ${de}" } + tcp.set_deadline(sock, now() + 3 + jitter, c) + done = sync.atomic_new(0) old_fd = tcp.fd(sock) tcp.cancel_retain(c) // the canceller's own reference worker.run_detached(|| { @@ -359,21 +372,24 @@ main() { // Decoys open at once, taking the numbers just freed. d1, d1e = tcp.connect("127.0.0.1", pe.port) d2, d2e = tcp.connect("127.0.0.1", pe.port) - if d1e != "" || d2e != "" { why = "decoy dial: ${d1e} ${d2e}" } - if tcp.fd(d1) == old_fd || tcp.fd(d2) == old_fd { reused = reused + 1 } while sync.atomic_load(done) == 0 { sleep(1) } - // The canceller has finished: both decoys must still be what they were. - _w1, w1e = tcp.write(d1, "p1") - _w2, w2e = tcp.write(d2, "p2") - x1, _n1, x1e = tcp.read_n(d1, 8) - x2, _n2, x2e = tcp.read_n(d2, 8) - if w1e != "" || w2e != "" || x1e != "" || x2e != "" || x1 != "p1" || x2 != "p2" { - why = "round ${r}: a decoy was disturbed: [${w1e}] [${w2e}] [${x1e}] [${x2e}] [${x1}] [${x2}]" - } else { - races_ok = races_ok + 1 + // The first thing wrong in a round is the one reported. + if why == "" && (d1e != "" || d2e != "") { why = "round ${r}: decoy dial: [${d1e}] [${d2e}]" } + if why == "" { + if tcp.fd(d1) == old_fd || tcp.fd(d2) == old_fd { reused = reused + 1 } + // The canceller has finished: both decoys must still be what they were. + _w1, w1e = tcp.write(d1, "p1") + _w2, w2e = tcp.write(d2, "p2") + x1, _n1, x1e = tcp.read_n(d1, 8) + x2, _n2, x2e = tcp.read_n(d2, 8) + if w1e != "" || w2e != "" || x1e != "" || x2e != "" || x1 != "p1" || x2 != "p2" { + why = "round ${r}: a decoy was disturbed: [${w1e}] [${w2e}] [${x1e}] [${x2e}] [${x1}] [${x2}]" + } else { + races_ok = races_ok + 1 + } } - tcp.close(d1) - tcp.close(d2) + if d1 != null { tcp.close(d1) } + if d2 != null { tcp.close(d2) } if (r % 2) == 1 { tcp.cancel_free(c) } sync.atomic_free(done) r = r + 1 diff --git a/tools/ae.c b/tools/ae.c index d53ad5758..6c78b567f 100644 --- a/tools/ae.c +++ b/tools/ae.c @@ -1481,7 +1481,7 @@ static const char* shared_lib_ext_for(const char* target) { const char* z = cross_target_to_zig(target); const char* t = z ? z : target; if (strstr(t, "windows")) return ".dll"; - if (cross_target_is_apple(t) || strstr(t, "macos")) return ".dylib"; + if (cross_target_is_apple(t)) return ".dylib"; if (strstr(t, "wasm")) return ".wasm"; return ".so"; } @@ -12357,7 +12357,7 @@ static int cmd_build(int argc, char** argv) { if (is_cross) { // Apple targets (iOS) are driven by Xcode/xcrun rather than zig; // the checks below fork on this. - int is_apple_target = cross_target_is_apple(ztriple); + int is_apple_target = cross_target_uses_xcode(ztriple); // Cross builds produce executables or portable C source. --emit=lib / // --emit=both / --emit=obj would emit library-shaped C (no main) and // then LINK it, which the executable link rejects. Reject those up diff --git a/tools/ae_cross.c b/tools/ae_cross.c index 85e1e24ec..84f84b83d 100644 --- a/tools/ae_cross.c +++ b/tools/ae_cross.c @@ -219,11 +219,54 @@ static bool cross_target_is_wasi(const char* ztriple) { } /* True when `triple` (a cross_target_to_zig result) names an Apple target that - * must be driven by the Xcode toolchain rather than zig. */ -bool cross_target_is_apple(const char* triple) { + * must be driven by the Xcode toolchain rather than zig: the clang iOS triples + * cross_ios_triple composes, which are the only ones spelled "-apple-". */ +bool cross_target_uses_xcode(const char* triple) { return triple && strstr(triple, "-apple-") != NULL; } +/* True when `triple` names ANY Apple platform, so the artifact is Mach-O and + * links as one (-dynamiclib, an install name, Apple ld's strip spellings), + * whichever driver builds it. Every spelling counts: zig's macOS triples are + * `aarch64-macos-none` and carry no "-apple-", and taking only "-apple-" sent + * a macOS dylib linked on Linux or FreeBSD through the ELF arm with no install + * name, so zig recorded the -o path as its id. Both zig (`-macos`, `-ios`) and + * clang (`-apple-`, `-darwin`) forms, and the other Apple OS names, so a + * triple added later is not missed the same way. */ +bool cross_target_is_apple(const char* triple) { + if (!triple) return false; + static const char* const spellings[] = { + "-apple-", "-macos", "-darwin", "-ios", "-tvos", "-watchos", + "-visionos", "-xros", + }; + for (size_t i = 0; i < sizeof(spellings) / sizeof(spellings[0]); i++) + if (strstr(triple, spellings[i])) return true; + return false; +} + +/* The macOS deployment minimum a zig macOS cross build is stamped with + * (LC_BUILD_VERSION minos). Named explicitly because zig's default is its own + * newest supported release (15.0 for zig 0.16/0.17), not the host's and not + * anything ae chose: a library built that way refused to load on anything + * older. 13.0 (Ventura) is the documented floor (docs/build-system.md): an + * explicit choice that reaches back further than zig's default, and the one a + * Swift package declaring `.macOS(.v13)` can link. + * AETHER_MACOS_MIN overrides it, as AETHER_IOS_MIN does for iOS; only digits + * and dots are taken, since the value goes into the compiler's -target. */ +#define CROSS_MACOS_MIN_DEFAULT "13.0" +static const char* cross_macos_min(void) { + const char* v = getenv("AETHER_MACOS_MIN"); + if (!v || !*v) return CROSS_MACOS_MIN_DEFAULT; + for (const char* c = v; *c; c++) { + if (!((*c >= '0' && *c <= '9') || *c == '.')) { + fprintf(stderr, "Warning: ignoring AETHER_MACOS_MIN=%s (want a version " + "like 13.0); using %s\n", v, CROSS_MACOS_MIN_DEFAULT); + return CROSS_MACOS_MIN_DEFAULT; + } + } + return v; +} + /* Resolve an SDK name to its filesystem root via `xcrun --show-sdk-path`. * Asking xcrun rather than hardcoding /Applications/Xcode.app/... is what * makes this work with a relocated Xcode, a beta Xcode, or a DEVELOPER_DIR @@ -277,7 +320,7 @@ int cross_target_os_arch(const char* t, const char** os, const char** arch) { } const char* cross_apple_sdk(const char* triple) { - if (!cross_target_is_apple(triple)) return NULL; + if (!cross_target_uses_xcode(triple)) return NULL; /* Catalyst is checked first: its triple carries "-ios" like the device * arm, but it builds against the macOS SDK. Testing "-simulator" or * "-ios" ahead of it would send it to the wrong SDK and fail the link @@ -299,7 +342,7 @@ const char* cross_apple_sdk(const char* triple) { * Returns false (having printed the reason) if the Apple SDK cannot be found. */ static bool cross_toolchain(const char* ztriple, char* cc_out, size_t cc_sz, char* ar_out, size_t ar_sz) { - if (cross_target_is_apple(ztriple)) { + if (cross_target_uses_xcode(ztriple)) { const char* sdk = cross_apple_sdk(ztriple); char sysroot[2048]; if (!cross_apple_sdk_path(sdk, sysroot, sizeof(sysroot))) { @@ -315,7 +358,16 @@ static bool cross_toolchain(const char* ztriple, char* cc_out, size_t cc_sz, if (ar_out) snprintf(ar_out, ar_sz, "xcrun --sdk %s ar", sdk); return true; } - snprintf(cc_out, cc_sz, "zig cc -target %s", ztriple); + /* A zig macOS triple gets its deployment minimum spelled into it + * (`aarch64-macos.13.0-none`), which is how zig stamps minos. */ + const char* macos = strstr(ztriple, "-macos-"); + if (macos) { + snprintf(cc_out, cc_sz, "zig cc -target %.*s-macos.%s-%s", + (int)(macos - ztriple), ztriple, cross_macos_min(), + macos + strlen("-macos-")); + } else { + snprintf(cc_out, cc_sz, "zig cc -target %s", ztriple); + } if (ar_out) snprintf(ar_out, ar_sz, "zig ar"); return true; } @@ -773,7 +825,7 @@ int run_cross_compile_obj(const char* c_file, const char* obj_file, * miniaudio (always compiled into the runtime) must fall back to its null * backend or ANY macos cross-compile fails. */ char feature_defs[512] = AETHER_REQUIRED_DEFINES; - if (strstr(ztriple, "macos") || strstr(ztriple, "-apple-ios")) { + if (cross_target_is_apple(ztriple)) { strncat(feature_defs, " -DMA_NO_COREAUDIO", sizeof(feature_defs) - strlen(feature_defs) - 1); } @@ -1247,7 +1299,7 @@ int run_cross_build(const char* c_file, const char* out_file, * so aether_audio.c stops being valid C there and fails the compile for * every program, audio or not. MA_NO_COREAUDIO selects the null backend on * both, so std.audio reports unavailable at runtime instead. */ - if (strstr(ztriple, "macos") || strstr(ztriple, "-apple-ios")) { + if (cross_target_is_apple(ztriple)) { strncat(feature_defs, " -DMA_NO_COREAUDIO", sizeof(feature_defs) - strlen(feature_defs) - 1); } @@ -1779,7 +1831,10 @@ int run_cross_build(const char* c_file, const char* out_file, * -dynamiclib and an -install_name: without one the load command * records the BUILD path, and the dylib then fails to load from * inside an .app bundle. @rpath/ is what an Xcode "Embed - * Frameworks" phase expects. */ + * Frameworks" phase expects, and what the native macOS build sets + * (install_name_tool -id after its link). This holds for a zig + * macOS link on any host: Linux and FreeBSD have no + * install_name_tool to fix it afterwards. */ char apple_lib_flags[1152]; apple_lib_flags[0] = '\0'; if (is_apple && emit_lib) { diff --git a/tools/ae_internal.h b/tools/ae_internal.h index 3d1c7e7ff..501cdcb40 100644 --- a/tools/ae_internal.h +++ b/tools/ae_internal.h @@ -353,6 +353,7 @@ void cache_depfile_path(const char* ae_file, char* out, size_t outsz); const char* cross_target_to_zig(const char* t); int cross_target_os_arch(const char* t, const char** os, const char** arch); bool cross_target_is_apple(const char* triple); +bool cross_target_uses_xcode(const char* triple); const char* cross_apple_sdk(const char* triple); bool cross_uses_unsupported_module(const char* file, char* which, size_t wsz); /* #1648: compile one generated .c to a target-format object (zig cc -c),