Skip to content

Conversation

@demotomohiro
Copy link
Contributor

No description provided.

## it should also get OS from GCC so that csource can be build with more GCC for cross compilation.
## but parsing OS name from `gcc -dumpmachine` is hard:
## https://wiki.osdev.org/Target_Triplet
ucpu := $(shell sh -c 'gcc -dumpmachine | cut --delimiter=- --field=1 | tr "[:upper:]" "[:lower:]"')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long options are a GNUism so this won't work on FreeBSD etc. cut -d - -f 1 seems preferable.
Also, later down the file there's this line:

mycpu = $(shell /bin/sh -c '"$(CC)" -dumpmachine | sed "s/-.*//"')

clang supports dumpmachine too, so maybe $(CC) is better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I wasn't aware there is already code calls $(CC) -dumpmachine.

@Araq
Copy link
Member

Araq commented Dec 21, 2025

I don't see why we would prefer GCC's ideas over the OS's ideas for OS/CPU detection. For example, what if we build with clang?

@demotomohiro
Copy link
Contributor Author

On Raspberry Pi 5 with 32-bit OS,

$ gcc -dumpmachine
arm-linux-gnueabihf
$ uname -m
aarch64

So if uname -m is used to detect CPU, csource for 64bit aarch64 is compiled with GCC targets 32bit arm, and fail to compile.

I think using GCC for OS/CPU detection works better when you want to cross compile csource and set PATH to use GCC for cross compile.
clang also supports -dumpmachine command that works on 32-bit Raspberry Pi OS.

Other ways to detect CPU architecture without C compiler are:

  • Run following Python code:
import platform

print(platform.architecture()[0])
print(platform.machine())

Output on 32bit Raspberry Pi OS:

32bit
aarch64
  • Run file command with any executable to get the CPU architecture of it and parse output:
file `which bash`

Output on 32bit Raspberry Pi OS:

/usr/bin/bash: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=db2e74d1b1322683c707352cc7b0de9e6205acdb, for GNU/Linux 3.2.0, stripped

@Araq
Copy link
Member

Araq commented Dec 22, 2025

There is nothing to detect here, if uname -m is broken you go and fix your operating system instead of an endless amount of hacks. On a 32bit OS with a 32bit GCC why the fuck would it say arm64, there is no way in hell you can use these "64" bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants