ColinIanKing / stress-ng

This is the stress-ng upstream project git repository. stress-ng will stress test a computer system in various selectable ways. It was designed to exercise various physical subsystems of a computer as well as the various operating system kernel interfaces.
https://github.com/ColinIanKing/stress-ng
GNU General Public License v2.0
1.82k stars 290 forks source link

musl-gcc misdetected on Cygwin #425

Closed chrfranke closed 2 months ago

chrfranke commented 2 months ago

On Cygwin, stress-ng --version says that musl-gcc is used which is not the case: stress-ng, version 0.18.04 (musl-gcc 12.4.0, x86_64 CYGWIN_NT-10.0-19045 3.5.4-1.x86_64)

HAVE_COMPILER_MUSL is misdetected because __USE_GNU is undefined.

A quick fix which should also cover other use cases of the newlib libc:

--- stress-ng.h.orig   2024-09-06 23:47:15.000000000 +0200
+++ stress-ng.h       2024-09-13 15:42:06.702376600 +0200
@@ -58,7 +58,8 @@
 /* clang */
 #define HAVE_COMPILER_CLANG
 #elif defined(__GNUC__) &&     \
-      !defined(__USE_GNU)
+      !defined(__USE_GNU) &&   \
+      !defined(_NEWLIB_VERSION)
 /* musl gcc */
 #define HAVE_COMPILER_MUSL
 #define HAVE_COMPILER_GCC_OR_MUSL

Possibly better: Check for some define from some __MUSL* define if available.

ColinIanKing commented 2 months ago

I wonder what #defines are available. What's the output from the following command? musl-gcc -E -dM -xc /dev/null

chrfranke commented 2 months ago

Here the result of diff -U1 <(gcc -E -dM -xc /dev/null) <(musl-gcc -E -dM -xc /dev/null) from current Debian 12:

@@ -85,7 +85,4 @@
 #define __SIZEOF_LONG__ 8
-#define __STDC_IEC_559__ 1
-#define __STDC_ISO_10646__ 201706L
 #define __UINT16_C(c) c
 #define __DECIMAL_DIG__ 21
-#define __STDC_IEC_559_COMPLEX__ 1
 #define __FLT64_EPSILON__ 2.22044604925031308084726333618164062e-16F64
@@ -173,3 +170,2 @@
 #define __UINT64_C(c) c ## UL
-#define _STDC_PREDEF_H 1
 #define __INT_LEAST32_MAX__ 0x7fffffff
@@ -180,3 +176,2 @@
 #define __FLT32X_MIN_EXP__ (-1021)
-#define __STDC_IEC_60559_COMPLEX__ 201404L
 #define __FLT128_HAS_DENORM__ 1
@@ -210,3 +205,2 @@
 #define __USER_LABEL_PREFIX__
-#define __STDC_IEC_60559_BFP__ 201404L
 #define __SIZEOF_PTRDIFF_T__ 8

This does not show any obvious difference to heuristically distinguish musl libc from others.

A search for grep -Eir 'MUSL|VER|REV' /usr/include/x86_64-linux-musl does also not show anything which is musl related. Apparently this libc does not provide an easy way to detect it.

ColinIanKing commented 2 months ago

Looks line one can do:

echo | gcc -E -Wp,-v - 2>&1 | grep musl

also, gcc compiled binaries have __data_start and musl-gcc does not.

ColinIanKing commented 2 months ago

I've pushed the following commit that seems to address the issue when I tried it with Cygwin:

commit b537623c8caf511ede24ab01669eae98c90cecad
Author: Colin Ian King <colin.i.king@gmail.com>
Date:   Sat Sep 14 13:36:39 2024 +0100

    Makefile: make musl-gcc detection more robust
chrfranke commented 2 months ago

This method relies on the musl substring in the preset include path. May not work on systems with musl as the default libc or if the user has a custom build environment. So if the this detection is only used to inform the user that the alternative musl libc is used, it is possibly sufficient. If a behavioral change is required if musl is used instead of glibc, it is possibly not.

Musl upstream decided to avoid a __MUSL__ define, see https://wiki.musl-libc.org/faq . I disagree.