From 389fc4e5c92c7f087b731d38a6a6857d5b6023eb Mon Sep 17 00:00:00 2001 From: teo1747 Date: Mon, 9 Nov 2026 00:17:06 +0100 Subject: [PATCH] driver: with no --target=, compile for the machine EmbCC runs on `embcc hello.c -o hello` built for x86_64-elf, a freestanding kernel target, whatever the machine was. On a Mac it then failed to link ("entry symbol '_start' is undefined"). GCC and clang build for the host when nothing names a target, and EmbCC now does too: aarch64-apple-darwin on an arm64 Mac, x86_64-apple-darwin on an Intel one, x86_64-linux-gnu or aarch64-linux-gnu on Linux, x86_64-windows-gnu on Windows, and x86_64-emblink for the compiler EmbCC builds for EmbLinkOS. The host is read from the compiler that built EmbCC, so the default is baked in, as GCC's is. On a host with no hosted target, or in a compiler built without that host's family (make TARGETS=), it is x86_64-elf or the first family built, as before. EMBCC_DEFAULT_TARGET, a compiled-in DEFAULT_TARGET and --target= still win, in that order. What relied on the old default now names it: check-self, libcxx-x86_64, tools/gen-selfhost-ref.sh and tools/os-stage.sh. tests/run.sh exports EMBCC_DEFAULT_TARGET=x86_64-elf, because the suite's tests were written against x86_64-elf as the target nothing names, and name every other one. default-target.sh unsets it and expects the host. The manual, the README and the roadmap say so, and the roadmap's open decision is closed. EmbLinkOS's mathself recipe calls embcc with no --target= and needs --target=x86_64-elf (its Makefile, lines 1616-1618). --- Makefile | 4 ++-- README.md | 13 ++++++----- docs/manual/embedded.md | 5 ++-- docs/manual/getting-started.md | 2 +- docs/manual/invoking.md | 16 ++++++++++--- docs/manual/targets.md | 7 +++--- docs/roadmap.md | 9 ++++---- src/arch/target.c | 42 ++++++++++++++++++++++++++++++---- tests/golden/default-target.sh | 20 ++++++++++++---- tests/run.sh | 6 +++++ tools/gen-selfhost-ref.sh | 2 +- tools/os-stage.sh | 2 +- 12 files changed, 95 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 372776d06..f4a592651 100644 --- a/Makefile +++ b/Makefile @@ -786,7 +786,7 @@ check: embcc $(if $(call built,x86),libc-x86_64 libcxx-x86_64) .PHONY: check-self check-self: embcc @n=0; bad=0; for f in $(SRCS); do n=$$((n + 1)); \ - ./embcc -std=c99 -Wall -Wextra -Werror -O2 -Isrc -c -o /dev/null $$f || \ + ./embcc --target=x86_64-elf -std=c99 -Wall -Wextra -Werror -O2 -Isrc -c -o /dev/null $$f || \ { echo "check-self: $$f"; bad=$$((bad + 1)); }; done; \ echo "check-self: EmbCC compiles $$((n - bad)) of its $$n sources"; [ $$bad -eq 0 ] @@ -1246,7 +1246,7 @@ libcxx-x86_64: embcc embar @mkdir -p $(BUILD)/libcxx/x86_64 @for f in $(LIBCXX_SRCS); do \ o=$(BUILD)/libcxx/x86_64/$$(basename $$f .cc).o; \ - ./embcc -c -O2 -x c++ $(LIBCXX_INC) $$f -o $$o || exit 1; \ + ./embcc --target=x86_64-elf -c -O2 -x c++ $(LIBCXX_INC) $$f -o $$o || exit 1; \ done @rm -f $(BUILD)/libcxx/x86_64/libcxx.a @$${EMBCC_X86_AR:-./embar} rcs $(BUILD)/libcxx/x86_64/libcxx.a \ diff --git a/README.md b/README.md index edbb95993..a2fb77365 100644 --- a/README.md +++ b/README.md @@ -215,16 +215,17 @@ described in [Getting started](docs/manual/getting-started.md). EmbCC is also a compiler for operating systems and hosted programs: x86-64 and AArch64 kernels (EmbLinkOS among them), and Linux, macOS and -Windows objects. On Linux x86-64, `make libc-linux-x86_64` builds the C -library, and `./embcc --target=x86_64-linux-gnu -O2 hello.c -o hello` -compiles and links a static executable. On macOS, EmbCC writes Mach-O -objects for the system linker: +Windows objects. With no `--target=`, it compiles for the machine it +runs on, as GCC and clang do: ```sh -./embcc --target=aarch64-apple-darwin --opt=speed -c hello.c -o hello.o -cc hello.o -o hello +./embcc -O2 hello.c -o hello && ./hello ``` +On Linux that is a static executable against EmbCC's own C library, +which `make` builds. On macOS it is a Mach-O program linked by the +system linker. A board or a kernel is always named with `--target=`. + `x86_64-elf` and `aarch64-elf` build freestanding kernels; `embld --embx` writes EmbLinkOS images. diff --git a/docs/manual/embedded.md b/docs/manual/embedded.md index f89dba021..e3f4c4c81 100644 --- a/docs/manual/embedded.md +++ b/docs/manual/embedded.md @@ -1576,8 +1576,9 @@ instruction. Fences compile to nothing. ## x86-64 kernels and EmbLinkOS -`x86_64-elf` is the freestanding x86-64 target and the default when no -`--target=` is given. `x86_64-emblink` is the same code generator with +`x86_64-elf` is the freestanding x86-64 target; name it with +`--target=x86_64-elf`, since the default is the machine EmbCC runs on. +`x86_64-emblink` is the same code generator with `__EmbLinkOS__`, `__emblink__` and `__emblink` predefined, for EmbLinkOS userland. diff --git a/docs/manual/getting-started.md b/docs/manual/getting-started.md index 8bf62d950..3584c2026 100644 --- a/docs/manual/getting-started.md +++ b/docs/manual/getting-started.md @@ -460,7 +460,7 @@ a warning and no debug information for the Darwin targets. See ### Bare-metal x86-64 and EmbLinkOS -`x86_64-elf` (the default target) and `x86_64-emblink` are freestanding: +`x86_64-elf` and `x86_64-emblink` are freestanding: `embcc` links them only when the program defines its own `_start`. Normally you compile with `-c` and link with [`embld`](tools/embld.md) together with your start-up code, as described in diff --git a/docs/manual/invoking.md b/docs/manual/invoking.md index 837b27452..eb705fedf 100644 --- a/docs/manual/invoking.md +++ b/docs/manual/invoking.md @@ -733,9 +733,19 @@ With no `--target=`, the target is chosen in this order: 2. the `DEFAULT_TARGET` the compiler was built with (`make DEFAULT_TARGET=riscv32-unknown-elf`; see [Getting started](getting-started.md#choosing-the-default-target)); -3. `x86_64-elf`, or in a compiler built without x86 (`make TARGETS=`; see - [Choosing the targets](getting-started.md#choosing-the-targets)) the - first family it has: `thumbv7m-none-eabi` for `TARGETS=arm`. +3. the machine the compiler runs on, as GCC and clang do: + `aarch64-apple-darwin` on an arm64 Mac, `x86_64-apple-darwin` on an + Intel one, `x86_64-linux-gnu` or `aarch64-linux-gnu` on Linux, + `x86_64-windows-gnu` on Windows, `x86_64-emblink` on EmbLinkOS. So + `embcc hello.c -o hello` builds a program that runs here; +4. where the compiler was built without that machine's family + (`make TARGETS=`; see + [Choosing the targets](getting-started.md#choosing-the-targets)) or + on a system it has no hosted target for, `x86_64-elf`, or without x86 + the first family it has: `thumbv7m-none-eabi` for `TARGETS=arm`. + +A build that compiles for a board or a kernel says so with `--target=`, +as it would with clang. `--target=` on the command line overrides both. A default that is not an accepted triple stops every invocation, naming where it came from: diff --git a/docs/manual/targets.md b/docs/manual/targets.md index 440061d93..bfdfadf23 100644 --- a/docs/manual/targets.md +++ b/docs/manual/targets.md @@ -93,9 +93,10 @@ embcc: x86_64 ... ``` -With no `--target=`, EmbCC compiles for `x86_64-elf`, unless the compiler -was built with `make DEFAULT_TARGET=TRIPLE` or the environment sets -`EMBCC_DEFAULT_TARGET`. +With no `--target=`, EmbCC compiles for the machine it runs on, as GCC +and clang do, unless the compiler was built with `make +DEFAULT_TARGET=TRIPLE` or the environment sets `EMBCC_DEFAULT_TARGET` +([The default target](invoking.md#the-default-target)). ### `EMBCC_DEFAULT_TARGET` diff --git a/docs/roadmap.md b/docs/roadmap.md index 18cd207d5..761860755 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -24,7 +24,7 @@ feeling. "Today" is the state at 0.0.1-beta (October 2026). | Coverage | Every construct EmbCC refuses is either implemented or listed in the manual as permanent, with the reason and a workaround. At least 99% of the GCC torture files that GCC accepts are accepted. | 110 distinct backend refusals and 134 front-end and driver refusals in the source. 95.4% of the torture files GCC accepts are accepted. | | Code size | `-Os`, object by object against clang: at most 1.10 on Cortex-M and RISC-V, 1.25 on AVR. | Cortex-M4 1.14, Cortex-M0 1.17, RV32 1.28, RV64 1.28, AVR 1.53, x86-64 1.70, AArch64 1.49; without math, Cortex-M4 1.08 and RISC-V 1.12 (`tools/scoreboard.sh`, 2026-10-24). | | Debugging | `-g` changes no code (already true). A variable held in a register is shown by the debugger, not reported as optimized out. Breakpoints, stepping and locals are checked through GDB on every board family EmbSim or QEMU runs. | `-g` changes no code on any target. No location lists yet, so register-held variables show as ``. `-g` is refused for Windows. | -| Stable interface | EmbCC's own option names are final. GCC spellings are accepted by one compatibility layer. Removing anything follows the deprecation policy below. The default target is settled. | EmbCC's own option vocabulary is in place. The GCC compatibility layer is still spread through the driver. The default target is `x86_64-elf`. | +| Stable interface | EmbCC's own option names are final. GCC spellings are accepted by one compatibility layer. Removing anything follows the deprecation policy below. The default target is settled. | EmbCC's own option vocabulary is in place. The GCC compatibility layer is still spread through the driver. The default target is the machine EmbCC runs on, as with GCC and clang (decided November 2026). | | Real hardware | Programs built by EmbCC verified on at least one real board per major family: Cortex-M0+, Cortex-M3/M4, AVR, RISC-V, PIC32. `embflash` checked with a real probe. | Verified on QEMU and EmbSim only. | | Installing | Linux and macOS (x86-64 and arm64) packages from every release, a Homebrew tap, and `install.sh` with no arguments installing the latest release. A Windows host build. | Linux and macOS tarballs and `.deb` files from the release workflow. No Homebrew tap repository yet. No Windows host build. | | Documentation | The manual and the status page are re-audited against the compiler before each release. A getting-started page for each EmbSim board. Migration guides from `arm-none-eabi-gcc`, `avr-gcc` and XC32. | The manual is complete, but the status page has drifted (it still lists C++ as refused on Cortex-M). The general [Migrating from GCC](manual/migrating-from-gcc.md) page exists; none yet for a particular board toolchain. | @@ -80,9 +80,8 @@ not at all. - The GCC compatibility layer: the driver reads options by identity from one table; every GCC spelling is an alias in one module, documented on a migration page. -- The default target: decide whether `embcc` with no `--target` builds - for the host (as clang does) or stays `x86_64-elf`, and ship the - decision with a warning period. +- The default target: done. `embcc` with no `--target` builds for the + machine it runs on, as clang does. - The source layout: the planned move to `compiler/`, `targets/`, `runtime/`, `sim/`, `tools/` happens now or is dropped. Either way, the build and install interface (`make`, `./embcc`, `embld`) stays. @@ -175,7 +174,7 @@ Each of these changes the plan, and none can be made by code alone: | Decision | Needed by | Options | |---|---|---| -| The default target | 0.2.0 | The host, as clang does; or `x86_64-elf`, as now. EmbLinkOS's build relies on today's default and would need `--target=x86_64-elf` first. | +| The default target | done | The host, as clang does (November 2026). A build for a board or a kernel names its target. EmbLinkOS's `mathself` recipe relied on the old default and needs `--target=x86_64-elf`. | | The source layout | 0.2.0 | Move to the planned layout before 1.0, or keep `src/`. | | Which refusals are permanent | 0.5.0 | Nested functions, C++ on AVR, GCC options for other compilers' ABIs (`-mabicalls`, RX `-m64bit-doubles`, ...). | | Hardware for validation | 0.9.0 | The six boards above and a debug probe (a CMSIS-DAP or J-Link, and a PICkit or MPLAB Snap for PIC32). | diff --git a/src/arch/target.c b/src/arch/target.c index e4da4b494..7ab12c81b 100644 --- a/src/arch/target.c +++ b/src/arch/target.c @@ -119,8 +119,44 @@ const char *target_default_name(void) return t && *t ? t : NULL; } +/* The machine this compiler runs on, as a target: what GCC and clang + * build for when no --target= is given. Read from the compiler that built + * this one, so a compiler built on an arm64 Mac targets arm64 macOS and + * one built on Linux targets Linux, and a compiler EmbCC builds for + * EmbLinkOS targets EmbLinkOS. NULL for a host EmbCC has no hosted target + * for, which then falls back as before. */ +static const char *host_triple(void) +{ +#if defined(__emblink__) || defined(__EmbLinkOS__) +# if defined(__aarch64__) + return "aarch64-emblink"; +# else + return "x86_64-emblink"; +# endif +#elif defined(__APPLE__) && defined(__aarch64__) + return "aarch64-apple-darwin"; +#elif defined(__APPLE__) && defined(__x86_64__) + return "x86_64-apple-darwin"; +#elif defined(__linux__) && defined(__x86_64__) + return "x86_64-linux-gnu"; +#elif defined(__linux__) && defined(__aarch64__) + return "aarch64-linux-gnu"; +#elif defined(_WIN32) && (defined(__x86_64__) || defined(_M_X64)) + return "x86_64-windows-gnu"; +#else + return NULL; +#endif +} + const char *target_builtin_default(void) { + /* the host, where its family is built in (make TARGETS=) */ + const char *h = host_triple(); + enum target_arch a; + enum target_os os; + enum target_fmt fmt; + if (h && target_from_triple(h, &a, &os, &fmt) && target_arch_built(a)) + return h; if (FAMILY_BUILT(x86)) return "x86_64-elf"; for (int i = 0; i < target_family_count(); i++) @@ -240,10 +276,8 @@ int target_apply_default(const char **bad) enum target_os os; enum target_fmt fmt; - /* the built-in default stands -- x86_64-elf, unless this compiler was - * built without x86 (make TARGETS=), and then its first family's */ - if (!t && FAMILY_BUILT(x86)) - return 1; + /* else the built-in default: the machine this compiler runs on, as + * GCC and clang build for it (target_builtin_default) */ if (!t) t = target_builtin_default(); if (!target_from_triple(t, &a, &os, &fmt)) { diff --git a/tests/golden/default-target.sh b/tests/golden/default-target.sh index 41787462f..3f5a0e04f 100755 --- a/tests/golden/default-target.sh +++ b/tests/golden/default-target.sh @@ -1,5 +1,6 @@ #!/bin/sh -# The CONFIGURED default target: `embcc main.c` compiles for the board. +# The default target: the machine EmbCC runs on, as with GCC and clang, +# and the CONFIGURED one, with which `embcc main.c` compiles for the board. # # A person whose work is one machine should not type --target= a hundred # times a day, and a Makefile that inherited no environment should still @@ -41,9 +42,18 @@ fail() { echo "$@"; exit 1; } mach() { "$EMBCC" "$@" -dumpmachine 2>&1; } # ---- 1. the order ----------------------------------------------------- +# With nothing configured, the machine this runs on, as GCC and clang +# build for it (`embcc hello.c -o hello` makes a program for here) +case "$(uname -s)-$(uname -m)" in + Darwin-arm64) host=aarch64-apple-darwin ;; + Darwin-x86_64) host=x86_64-apple-darwin ;; + Linux-x86_64) host=x86_64-linux-gnu ;; + Linux-aarch64) host=aarch64-linux-gnu ;; + *) host=x86_64-elf ;; +esac got=$(mach) -[ "$got" = x86_64-elf ] || - fail "with nothing configured the default is '$got', not x86_64-elf" +[ "$got" = "$host" ] || + fail "with nothing configured the default is '$got', not this host's $host" got=$(EMBCC_DEFAULT_TARGET=riscv32-unknown-elf mach) [ "$got" = riscv32-unknown-elf ] || @@ -52,7 +62,7 @@ got=$(EMBCC_DEFAULT_TARGET=riscv32-unknown-elf mach) got=$(EMBCC_DEFAULT_TARGET=riscv32-unknown-elf mach --target=aarch64-elf) [ "$got" = aarch64-elf ] || fail "--target= did not override the environment: got '$got'" -echo " --target= > EMBCC_DEFAULT_TARGET > x86_64-elf" +echo " --target= > EMBCC_DEFAULT_TARGET > the host ($host)" # ---- 2. the refusal --------------------------------------------------- # In a subshell: `VAR=val func` leaves VAR set in the CALLING shell when @@ -130,7 +140,7 @@ echo " and a default makes the code --target= makes" # now, around this very script, so if the driver ever reads it the suite # retargets itself. got=$(EMBCC_TARGET=riscv64-unknown-elf mach) -[ "$got" = x86_64-elf ] || +[ "$got" = "$host" ] || fail "the driver read EMBCC_TARGET (got '$got') -- that name belongs to tests/lib.sh, and honouring it retargets the whole suite" echo " EMBCC_TARGET is the harness's, and the driver leaves it alone" diff --git a/tests/run.sh b/tests/run.sh index d46195bef..7eae42c7b 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -93,6 +93,12 @@ fi cd "$(dirname "$0")/.." EMBCC=${EMBCC_ARG:-$PWD/embcc} export EMBCC +# The suite's tests were written against x86_64-elf as the target nothing +# names, and name every other one: they compile for it on any host. The +# compiler's own default is the machine it runs on (default-target.sh, +# which unsets this, tests that), and a person's EMBCC_DEFAULT_TARGET for +# a board must not retarget the suite. +export EMBCC_DEFAULT_TARGET=x86_64-elf # Turn on the optimizer's IR verifier for the whole suite: every -O1/-O2 compile # (optimizer.sh, regalloc-O2.sh, the on-metal kernel build) then checks that no diff --git a/tools/gen-selfhost-ref.sh b/tools/gen-selfhost-ref.sh index e795de2ba..56db3a09b 100755 --- a/tools/gen-selfhost-ref.sh +++ b/tools/gen-selfhost-ref.sh @@ -16,7 +16,7 @@ SRCS=$(make -pn 2>/dev/null | sed -n 's/^SRCS := //p' | head -1) [ -n "$SRCS" ] || { echo "gen-selfhost-ref: cannot read SRCS from the Makefile" >&2; exit 1; } rm -rf ref; mkdir -p ref for f in $SRCS; do - ./embcc -c "$f" $INCS -o "ref/$(echo "${f#src/}" | tr / _ | sed 's/\.c$//').o" + ./embcc --target=x86_64-elf -c "$f" $INCS -o "ref/$(echo "${f#src/}" | tr / _ | sed 's/\.c$//').o" done echo "wrote $(ls ref/*.o | wc -l) reference objects" ./embld -o "$OS_BUILD/embcc.elf" "$OS_BUILD/crt0.o" "$OS_BUILD/syscalls.o" \ diff --git a/tools/os-stage.sh b/tools/os-stage.sh index 494c258f5..5e9e06d7a 100755 --- a/tools/os-stage.sh +++ b/tools/os-stage.sh @@ -60,7 +60,7 @@ ELF="$OUT/$NAME.elf" rm -f "$OBJ" "$ELF" echo "embcc -c $SRC -o $OBJ" -./embcc -c "$SRC" -o "$OBJ" +./embcc --target=x86_64-elf -c "$SRC" -o "$OBJ" # -nostartfiles: crt0.o provides _start, there are no crtX files here. # -static: the kernel binds a shared libembk.so only for UI apps; an M1 program