From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id 77C1B3858414; Tue, 30 Apr 2024 07:55:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 77C1B3858414 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 77C1B3858414 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714463739; cv=none; b=EDmebNpJpLBvIRiATfRlgSAce4KPCroIKwpRfzJz/tdRRZUlnGrfaNKqGEHpcn862XjwYrWezIEEsGYBGn1QnhHnFW1uNZIWS7RiC3htY7DjMZirZxLC9YsY3CISQklvyQuR3afkWfRWc7EnbeaweSbYD2VX5RnyyQEfhVhrwW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714463739; c=relaxed/simple; bh=fVNkwzghCyi3SjH7hBf0yPQE5cXKCP+BSLDPnTP+hjQ=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=f++Yy/Q2kurnduHzW9y059pz5VTZlFsG7h3/VZLBDOAtWHIUlYTftZECEWWWJwxOau+mtWhU8i/jBK6ofuCqU7JvZ++WobWjpoLVLHA8jKxzH0vw48FgB9WovRfOYbXsn5WlOHD/ybXTvPtLpj/v7nE8PhvLouh39hhXIVLmZbM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-516d68d7a8bso4944329e87.1; Tue, 30 Apr 2024 00:55:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714463735; x=1715068535; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=td6x2yuj6UsqfarsI77Xf6273lvYsUXOak9m6kpFcLQ=; b=OAauG8T0UjLu/kneBklbt89Ks2FiqEo2rYGqc/Y2owjBfyLWZ9r9U5JZ2nuUfxmMDg YGc86jdgjR9/PXS6AeLbYz+RWk1TZE9YW/UsdL1KPqYJcoAdU+nkVKIFmrtwHLm9izUq kMUcy2apdgjn/wIu6d+kiJxWFJgPbECHfgVdH9ypxu/ehH4BpATa7uj0N4pjOPFR4/Xa mjG+FSNVrmxpq4RU7NHpPf9UErYPkMe46E6X/pZiFGvEfMhNn+BCh8E5vhumg67vSxgV kZQKGwC1eK6k0NJMbU+RTFposzTpjHkKUfdN7LhSUm5xZjzfHYIip/nwl5LLy0Sa7mzC LvEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714463735; x=1715068535; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=td6x2yuj6UsqfarsI77Xf6273lvYsUXOak9m6kpFcLQ=; b=Xo3lPKEbkuinevcrbGz9lPW2Bj6DflLdCPGkspy2jezelovTf+jMuXh1cwz0yvS1Hd Hvh2apWfioD46WFUDVtTTndCbTWW+LNHaPKxcs8gKP6cpCWuDZ/nPOderTkfHmAWMTCK rejHRdX/SDpo7qH0VwD8s+ckuUsFA8kWsvz2F9q0NFVM9KtwtYuwLlrW+TpI7nzlS2Lp YAM36R+ZqdhqP0a1Pi9b99DuDZWOQuk4gAePc7nnQgkPiUs83Cy+hzEkEQprMxEGAy0i gitTKnFCskszSTx9pDm6ONwTsjWnHXnSA/6a1gaDp9I/tUmqkN6B58f0/KrF43o17NuA tw+g== X-Forwarded-Encrypted: i=1; AJvYcCWo54uejvJw91i4ptIPiwjxLjqzbEH4PDS77XyVeT813ZlqWv+S7Ve0IuoEFMg2tsd/kl7IG95KvdTa9Ji1wx8p0HfV+A== X-Gm-Message-State: AOJu0YwJY/XXeXXlevPI3/I+c1ot7EHlBTnkSPrGIIMiCMWTPZkvpeJv fMk9NInAscefTxc3vobzE12F4EluatFWN4SwpLTLlV5TrR5vJMBouRPGPSPEzvonlllJBeulECr 4o/ji6ligHXLJNB95hryER3yZmB5U5X3n X-Google-Smtp-Source: AGHT+IEzi5C7gogJYM5ZSj/kY4/6ab0lUhX6UP8zYyYVUvWEfRlZZYPlJ+FRPC3We4z0e1Wh1jIoeIEgSeQg+2HHNNg= X-Received: by 2002:ac2:410f:0:b0:519:5fb9:832b with SMTP id b15-20020ac2410f000000b005195fb9832bmr582906lfi.12.1714463734636; Tue, 30 Apr 2024 00:55:34 -0700 (PDT) MIME-Version: 1.0 References: <20240419094903.512887-1-arthur.cohen@embecosm.com> In-Reply-To: <20240419094903.512887-1-arthur.cohen@embecosm.com> From: Richard Biener Date: Tue, 30 Apr 2024 09:55:23 +0200 Message-ID: Subject: Re: [PATCH] rust: Do not link with libdl and libpthread unconditionally To: Arthur Cohen Cc: gcc-patches@gcc.gnu.org, gcc-rust@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Apr 19, 2024 at 11:49=E2=80=AFAM Arthur Cohen wrote: > > Hi everyone, > > This patch checks for the presence of dlopen and pthread_create in libc. = If that is not the > case, we check for the existence of -ldl and -lpthread, as these librarie= s are required to > link the Rust runtime to our Rust frontend. > > If these libs are not present on the system, then we disable the Rust fro= ntend. > > This was tested on x86_64, in an environment with a recent GLIBC and in a= container with GLIBC > 2.27. > > Apologies for sending it in so late. For example GCC_ENABLE_PLUGINS simply does # Check -ldl saved_LIBS=3D"$LIBS" AC_SEARCH_LIBS([dlopen], [dl]) if test x"$ac_cv_search_dlopen" =3D x"-ldl"; then pluginlibs=3D"$pluginlibs -ldl" fi LIBS=3D"$saved_LIBS" which I guess would also work for pthread_create? This would simplify the code a bit. > ChangeLog: > > * Makefile.tpl: Add CRAB1_LIBS variable. > * Makefile.in: Regenerate. > * configure: Regenerate. > * configure.ac: Check if -ldl and -lpthread are needed, and if so= , add > them to CRAB1_LIBS. > > gcc/rust/ChangeLog: > > * Make-lang.in: Remove overazealous LIBS =3D -ldl -lpthread line,= link > crab1 against CRAB1_LIBS. > --- > Makefile.in | 3 + > Makefile.tpl | 3 + > configure | 157 ++++++++++++++++++++++++++++++++++++++++++ > configure.ac | 94 +++++++++++++++++++++++++ > gcc/rust/Make-lang.in | 2 +- > 5 files changed, 258 insertions(+), 1 deletion(-) > > diff --git a/Makefile.in b/Makefile.in > index db4fa6c6260..34c5550beca 100644 > --- a/Makefile.in > +++ b/Makefile.in > @@ -197,6 +197,7 @@ HOST_EXPORTS =3D \ > $(BASE_EXPORTS) \ > CC=3D"$(CC)"; export CC; \ > ADA_CFLAGS=3D"$(ADA_CFLAGS)"; export ADA_CFLAGS; \ > + CRAB1_LIBS=3D"$(CRAB1_LIBS)"; export CRAB1_LIBS; \ > CFLAGS=3D"$(CFLAGS)"; export CFLAGS; \ > CONFIG_SHELL=3D"$(SHELL)"; export CONFIG_SHELL; \ > CXX=3D"$(CXX)"; export CXX; \ > @@ -450,6 +451,8 @@ GOCFLAGS =3D $(CFLAGS) > GDCFLAGS =3D @GDCFLAGS@ > GM2FLAGS =3D $(CFLAGS) > > +CRAB1_LIBS =3D @CRAB1_LIBS@ > + > PKG_CONFIG_PATH =3D @PKG_CONFIG_PATH@ > > GUILE =3D guile > diff --git a/Makefile.tpl b/Makefile.tpl > index 1d5813cd569..8f4bf297918 100644 > --- a/Makefile.tpl > +++ b/Makefile.tpl > @@ -200,6 +200,7 @@ HOST_EXPORTS =3D \ > $(BASE_EXPORTS) \ > CC=3D"$(CC)"; export CC; \ > ADA_CFLAGS=3D"$(ADA_CFLAGS)"; export ADA_CFLAGS; \ > + CRAB1_LIBS=3D"$(CRAB1_LIBS)"; export CRAB1_LIBS; \ > CFLAGS=3D"$(CFLAGS)"; export CFLAGS; \ > CONFIG_SHELL=3D"$(SHELL)"; export CONFIG_SHELL; \ > CXX=3D"$(CXX)"; export CXX; \ > @@ -453,6 +454,8 @@ GOCFLAGS =3D $(CFLAGS) > GDCFLAGS =3D @GDCFLAGS@ > GM2FLAGS =3D $(CFLAGS) > > +CRAB1_LIBS =3D @CRAB1_LIBS@ > + > PKG_CONFIG_PATH =3D @PKG_CONFIG_PATH@ > > GUILE =3D guile > diff --git a/configure b/configure > index 3b0abeb8b2e..75b489a5f57 100755 > --- a/configure > +++ b/configure > @@ -690,6 +690,7 @@ extra_host_zlib_configure_flags > extra_host_libiberty_configure_flags > stage1_languages > host_libs_picflag > +CRAB1_LIBS > PICFLAG > host_shared > gcc_host_pie > @@ -8875,6 +8876,142 @@ fi > > > > +# Rust requires -ldl and -lpthread if you are using an old glibc that do= es not include them by > +# default, so we check for them here > + > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if libc includes libdl= and libpthread" >&5 > +$as_echo_n "checking if libc includes libdl and libpthread... " >&6; } > + > +ac_ext=3Dc > +ac_cpp=3D'$CPP $CPPFLAGS' > +ac_compile=3D'$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' > +ac_link=3D'$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest= .$ac_ext $LIBS >&5' > +ac_compiler_gnu=3D$ac_cv_c_compiler_gnu > + > + > +requires_ldl=3Dno > +requires_lpthread=3Dno > +missing_rust_dynlibs=3Dnone > + > +cat confdefs.h - <<_ACEOF >conftest.$ac_ext > +/* end confdefs.h. */ > +#include > +int > +main () > +{ > +dlopen(0,0); > + ; > + return 0; > +} > +_ACEOF > +if ac_fn_c_try_link "$LINENO"; then : > + > +else > + requires_ldl=3Dyes > + > +fi > +rm -f core conftest.err conftest.$ac_objext \ > + conftest$ac_exeext conftest.$ac_ext > + > +if test $requires_ldl =3D yes; then > + tmp_LIBS=3D$LIBS > + LIBS=3D"$LIBS -ldl" > + > + cat confdefs.h - <<_ACEOF >conftest.$ac_ext > +/* end confdefs.h. */ > +#include > +int > +main () > +{ > +dlopen(0,0); > + ; > + return 0; > +} > +_ACEOF > +if ac_fn_c_try_link "$LINENO"; then : > + CRAB1_LIBS=3D"$CRAB1_LIBS -ldl" > +else > + missing_rust_dynlibs=3D"libdl" > + > +fi > +rm -f core conftest.err conftest.$ac_objext \ > + conftest$ac_exeext conftest.$ac_ext > + > + LIBS=3D$tmp_LIBS > +fi > + > +cat confdefs.h - <<_ACEOF >conftest.$ac_ext > +/* end confdefs.h. */ > +#include > +int > +main () > +{ > +pthread_create(NULL,NULL,NULL,NULL); > + > + ; > + return 0; > +} > +_ACEOF > +if ac_fn_c_try_link "$LINENO"; then : > + > +else > + requires_lpthread=3Dyes > + > +fi > +rm -f core conftest.err conftest.$ac_objext \ > + conftest$ac_exeext conftest.$ac_ext > + > +if test $requires_lpthread =3D yes; then > + tmp_LIBS=3D$LIBS > + LIBS=3D"$LIBS -lpthread" > + > + cat confdefs.h - <<_ACEOF >conftest.$ac_ext > +/* end confdefs.h. */ > +#include > +int > +main () > +{ > +pthread_create(NULL,NULL,NULL,NULL); > + > + ; > + return 0; > +} > +_ACEOF > +if ac_fn_c_try_link "$LINENO"; then : > + CRAB1_LIBS=3D"$CRAB1_LIBS -lpthread" > +else > + missing_rust_dynlibs=3D"$missing_rust_dynlibs, libpthread" > + > +fi > +rm -f core conftest.err conftest.$ac_objext \ > + conftest$ac_exeext conftest.$ac_ext > + > + LIBS=3D$tmp_LIBS > +fi > + > +if test "$missing_rust_dynlibs" =3D "none"; then > + if test $requires_ldl =3D yes -a $requires_lpthread =3D yes; then > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: not included, but a= vailable" >&5 > +$as_echo "not included, but available" >&6; } > + else > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 > +$as_echo "yes" >&6; } > + fi > +else > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no: missing $missing_= rust_dynlibs" >&5 > +$as_echo "no: missing $missing_rust_dynlibs" >&6; } > +fi > + > +CRAB1_LIBS=3D"$CRAB1_LIBS" > + > + > +ac_ext=3Dc > +ac_cpp=3D'$CPP $CPPFLAGS' > +ac_compile=3D'$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' > +ac_link=3D'$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest= .$ac_ext $LIBS >&5' > +ac_compiler_gnu=3D$ac_cv_c_compiler_gnu > + > + > # If we are building PIC/PIE host executables, and we are building depen= dent > # libs (e.g. GMP) in-tree those libs need to be configured to generate P= IC > # code. > @@ -9115,6 +9252,26 @@ $as_echo "$as_me: WARNING: GDC is required to buil= d $language" >&2;} > ;; > esac > > + # Disable Rust if we are missing some required C libraries for t= he Rust runtime. > + case ${add_this_lang}:${language}:${missing_rust_dynlibs} in > + *:rust:none) > + # Nothing to do - we're not missing any C libraries > + ;; > + yes:rust:*) > + as_fn_error $? "some C libraries are required to build $lang= uage: $missing_rust_dynlibs" "$LINENO" 5 > + add_this_lang=3Dunsupported > + ;; > + all:rust:*) > + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: some C lib= raries are required to build $language: $missing_rust_dynlibs" >&5 > +$as_echo "$as_me: WARNING: some C libraries are required to build $langu= age: $missing_rust_dynlibs" >&2;} > + add_this_lang=3Dunsupported > + ;; > + *:rust:*) > + # Silently disable. > + add_this_lang=3Dunsupported > + ;; > + esac > + > # Disable jit if -enable-host-shared not specified > # but not if building for Mingw. All code in Windows > # is position independent code (PIC). > diff --git a/configure.ac b/configure.ac > index 042681c27be..d96b7b5432e 100644 > --- a/configure.ac > +++ b/configure.ac > @@ -2037,6 +2037,81 @@ fi > > AC_SUBST(PICFLAG) > > +# Rust requires -ldl and -lpthread if you are using an old glibc that do= es not include them by > +# default, so we check for them here > + > +AC_MSG_CHECKING([if libc includes libdl and libpthread]) > + > +AC_LANG_PUSH([C]) > + > +requires_ldl=3Dno > +requires_lpthread=3Dno > +missing_rust_dynlibs=3Dnone > + > +AC_LINK_IFELSE( > + [AC_LANG_PROGRAM( > + [#include ], > + [dlopen(0,0);], > + )], > + [], > + [requires_ldl=3Dyes] > +) > + > +if test $requires_ldl =3D yes; then > + tmp_LIBS=3D$LIBS > + LIBS=3D"$LIBS -ldl" > + > + AC_LINK_IFELSE( > + [AC_LANG_PROGRAM( > + [#include ], > + [dlopen(0,0);], > + )], > + [CRAB1_LIBS=3D"$CRAB1_LIBS -ldl"], > + [missing_rust_dynlibs=3D"libdl"] > + ) > + > + LIBS=3D$tmp_LIBS > +fi > + > +AC_LINK_IFELSE( > + [AC_LANG_PROGRAM( > + [#include ], > + [pthread_create(NULL,NULL,NULL,NULL);] > + )], > + [], > + [requires_lpthread=3Dyes] > +) > + > +if test $requires_lpthread =3D yes; then > + tmp_LIBS=3D$LIBS > + LIBS=3D"$LIBS -lpthread" > + > + AC_LINK_IFELSE( > + [AC_LANG_PROGRAM( > + [#include ], > + [pthread_create(NULL,NULL,NULL,NULL);] > + )], > + [CRAB1_LIBS=3D"$CRAB1_LIBS -lpthread"], > + [missing_rust_dynlibs=3D"$missing_rust_dynlibs, libpthread"] > + ) > + > + LIBS=3D$tmp_LIBS > +fi > + > +if test "$missing_rust_dynlibs" =3D "none"; then > + if test $requires_ldl =3D yes -a $requires_lpthread =3D yes; then > + AC_MSG_RESULT([not included, but available]) > + else > + AC_MSG_RESULT([yes]) > + fi > +else > + AC_MSG_RESULT([no: missing $missing_rust_dynlibs]) > +fi > + > +AC_SUBST(CRAB1_LIBS, "$CRAB1_LIBS") > + > +AC_LANG_POP([C]) > + > # If we are building PIC/PIE host executables, and we are building depen= dent > # libs (e.g. GMP) in-tree those libs need to be configured to generate P= IC > # code. > @@ -2274,6 +2349,25 @@ if test -d ${srcdir}/gcc; then > ;; > esac > > + # Disable Rust if we are missing some required C libraries for t= he Rust runtime. > + case ${add_this_lang}:${language}:${missing_rust_dynlibs} in > + *:rust:none) > + # Nothing to do - we're not missing any C libraries > + ;; > + yes:rust:*) > + AC_MSG_ERROR([some C libraries are required to build $langua= ge: $missing_rust_dynlibs]) > + add_this_lang=3Dunsupported > + ;; > + all:rust:*) > + AC_MSG_WARN([some C libraries are required to build $languag= e: $missing_rust_dynlibs]) > + add_this_lang=3Dunsupported > + ;; > + *:rust:*) > + # Silently disable. > + add_this_lang=3Dunsupported > + ;; > + esac > + > # Disable jit if -enable-host-shared not specified > # but not if building for Mingw. All code in Windows > # is position independent code (PIC). > diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in > index 8db04163618..bb2e246f9fa 100644 > --- a/gcc/rust/Make-lang.in > +++ b/gcc/rust/Make-lang.in > @@ -214,7 +214,7 @@ LIBPROC_MACRO_INTERNAL =3D ../libgrust/libproc_macro_= internal/libproc_macro_intern > crab1$(exeext): $(RUST_ALL_OBJS) attribs.o $(BACKEND) $(LIBDEPS) $(LIBPR= OC_MACRO_INTERNAL) $(rust.prev) > @$(call LINK_PROGRESS,$(INDEX.rust),start) > +$(LLINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ \ > - $(RUST_ALL_OBJS) attribs.o $(BACKEND) $(LIBS) $(LIBPROC_MAC= RO_INTERNAL) $(BACKENDLIBS) > + $(RUST_ALL_OBJS) attribs.o $(BACKEND) $(LIBS) $(CRAB1_LIBS)= $(LIBPROC_MACRO_INTERNAL) $(BACKENDLIBS) > @$(call LINK_PROGRESS,$(INDEX.rust),end) > > # Build hooks. > -- > 2.42.1 >