mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Alastair Houghton <ahoughton@apple.com>
To: musl@lists.openwall.com
Subject: [musl] __MUSL__ macro
Date: Thu, 06 Jul 2023 11:48:04 +0100	[thread overview]
Message-ID: <309EDCC9-2402-46B5-BDBD-B96677E470DD@apple.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2272 bytes --]

Hi all,

Before I start, I’m aware of

  <https://www.openwall.com/lists/musl/2013/03/29/13>

but I *still* want to add __MUSL__ (see attached patch).

Let me explain what we’re doing, why we want it and why we think musl *should* have it.  We’re trying to add support for musl to Swift <https://swift.org <https://swiftlang.org/>> and its attendant core libraries, and there are a number of things about musl that presently differ from other platforms/C libraries we support.

Examples include the use of `union`s in `pthread_mutex_t` et al (which means that we can’t write a C++ `constexpr` function that returns one, even if all it does is return `PTHREAD_MUTEX_INITIALIZER`), the fact that it doesn’t have the `d_namlen` member of `struct dirent`, or the fact that `dladdr()` is a no-op when statically linked.

I’m aware that there are other solutions for some of these cases - but for instance the `dladdr()` issue is not something you can test at configuration time when cross-compiling, since there’s no way to know what the result would be without running a program on the target system (which you don’t necessarily have available at configuration time in that case).  Likewise, configuration time tests are slow and  aren’t even a thing in some of the projects we need to add support in, whereas we do generally have the ability to use the preprocessor.

You might say we should just bite the bullet and add configuration steps to all of the other projects, but that is honestly a non-starter; the owners of those projects are not likely to accept patches to add such a thing, even as a short-term fix, and the patches to do so would be non-trivial in a number of cases also.  In the longer term we don’t want higher level projects to need to do this kind of thing and the lower level ones could in many cases do some kind of configuration time testing, but that’s a longer term goal and it doesn’t help us to add musl support in the interim, which we would like to do.

I’ve attached a proposed patch that adds `__MUSL__` (set to the major version) and `__MUSL_MINOR__` (set to the minor version) as well as `__MUSL_PREREQ()` (which works the same way `__GLIBC_PREREQ()` does).

Kind regards,

Alastair Houghton


[-- Attachment #2: musl-version.patch --]
[-- Type: application/octet-stream, Size: 2772 bytes --]

diff --git a/Makefile b/Makefile
index e8cc4436..ea91b9be 100644
--- a/Makefile
+++ b/Makefile
@@ -34,7 +34,7 @@ CRT_OBJS = $(filter obj/crt/%,$(ALL_OBJS))
 
 AOBJS = $(LIBC_OBJS)
 LOBJS = $(LIBC_OBJS:.o=.lo)
-GENH = obj/include/bits/alltypes.h obj/include/bits/syscall.h
+GENH = obj/include/bits/alltypes.h obj/include/bits/syscall.h obj/include/features.h
 GENH_INT = obj/src/internal/version.h
 IMPH = $(addprefix $(srcdir)/, src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/locale_impl.h src/internal/libc.h)
 
@@ -95,6 +95,17 @@ $(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(ALL_OBJS:%.o=%.lo) $(GENH) $(GENH_INT): |
 $(OBJ_DIRS):
 	mkdir -p $@
 
+MUSL_VERSION:=$(shell cd $(srcdir) && sh tools/version.sh)
+empty:=
+space:=$(empty) $(empty)
+musl_ver_parts:=$(subst -git-,$(space),$(subst .,$(space),$(MUSL_VERSION)))
+MUSL_MAJOR:=$(word 1, $(musl_ver_parts))
+MUSL_MINOR:=$(word 2, $(musl_ver_parts))
+MUSL_PATCH:=$(word 3, $(musl_ver_parts))
+
+obj/include/features.h: $(srcdir)/include/features.h.in $(wildcard $(srcdir)/VERSION $(srcdir)/.git)
+	sed -e "s/@MUSL_MAJOR@/$(MUSL_MAJOR)/g;s/@MUSL_MINOR@/$(MUSL_MINOR)/g;s/@MUSL_PATCH@/$(MUSL_PATCH)/g" $< > $@
+
 obj/include/bits/alltypes.h: $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in $(srcdir)/tools/mkalltypes.sed
 	sed -f $(srcdir)/tools/mkalltypes.sed $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in > $@
 
@@ -103,7 +114,7 @@ obj/include/bits/syscall.h: $(srcdir)/arch/$(ARCH)/bits/syscall.h.in
 	sed -n -e s/__NR_/SYS_/p < $< >> $@
 
 obj/src/internal/version.h: $(wildcard $(srcdir)/VERSION $(srcdir)/.git)
-	printf '#define VERSION "%s"\n' "$$(cd $(srcdir); sh tools/version.sh)" > $@
+	printf '#define VERSION "%s"\n' "$(MUSL_VERSION)" > $@
 
 obj/src/internal/version.o obj/src/internal/version.lo: obj/src/internal/version.h
 
@@ -209,6 +220,9 @@ $(DESTDIR)$(includedir)/bits/%: obj/include/bits/%
 $(DESTDIR)$(includedir)/%: $(srcdir)/include/%
 	$(INSTALL) -D -m 644 $< $@
 
+$(DESTDIR)$(includedir)/%: obj/include/%
+	$(INSTALL) -D -m 644 $< $@
+
 $(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so
 	$(INSTALL) -D -l $(libdir)/libc.so $@ || true
 
diff --git a/include/features.h b/include/features.h.in
similarity index 80%
rename from include/features.h
rename to include/features.h.in
index 85cfb72a..07662804 100644
--- a/include/features.h
+++ b/include/features.h.in
@@ -1,6 +1,13 @@
 #ifndef _FEATURES_H
 #define _FEATURES_H
 
+#define __MUSL__       @MUSL_MAJOR@
+#define __MUSL_MINOR__ @MUSL_MINOR@
+#define __MUSL_PATCH__ @MUSL_PATCH@
+
+#define __MUSL_PREREQ(maj, min) \
+  ((__MUSL__ << 16) + __MUSL_MINOR >= ((maj) << 16) + (min))
+
 #if defined(_ALL_SOURCE) && !defined(_GNU_SOURCE)
 #define _GNU_SOURCE 1
 #endif

             reply	other threads:[~2023-07-06 10:48 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-06 10:48 Alastair Houghton [this message]
2023-07-06 12:17 ` Alex Xu
2023-07-06 16:26   ` Szabolcs Nagy
2023-07-07  7:14   ` Alastair Houghton
2023-07-07  7:30     ` A. Wilcox
2023-07-07  8:24       ` Alastair Houghton
2023-07-07 11:20     ` Laurent Bercot
2023-07-07 11:45     ` Jeffrey Walton
2023-07-07 13:53     ` Rich Felker
2023-07-07 14:18       ` Alastair Houghton
2023-07-07 12:47 ` Rich Felker
2023-07-07 13:14   ` Alastair Houghton
2023-07-07 14:19     ` Markus Wichmann
2023-07-07 14:26       ` Markus Wichmann
2023-07-07 14:46       ` Alastair Houghton
2023-07-07 15:02       ` Andrew Bell
2023-07-07 15:19         ` Markus Wichmann
2023-07-07 15:24           ` Andrew Bell
2023-07-07 15:34           ` Alastair Houghton
2023-07-07 15:45             ` Rich Felker
2023-07-07 15:58               ` Alastair Houghton
2023-07-07 15:05     ` i262jq

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=309EDCC9-2402-46B5-BDBD-B96677E470DD@apple.com \
    --to=ahoughton@apple.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).