Skip to content

Commit

Permalink
Fix Bugzilla 24725 - core.sys.linux: feature detect glibc functions a…
Browse files Browse the repository at this point in the history
…t build time

Signed-off-by: Andrei Horodniceanu <[email protected]>
  • Loading branch information
the-horo committed Aug 28, 2024
1 parent 4a5c56d commit 1efe4df
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions druntime/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ trace.log
/msvc*.obj
make
*.lst

# generated during the build
/src/core/sys/linux/config.d
4 changes: 4 additions & 0 deletions druntime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ $(IMPDIR)/%.h : src/%.h
@mkdir -p $(dir $@)
@cp $< $@

################### Feature tests for druntime bindings #########################

include features/GNUmakefile

######################## Build DMD if non-existent ##############################

../generated/$(OS)/$(BUILD)/$(MODEL)/dmd$(DOTEXE):
Expand Down
1 change: 1 addition & 0 deletions druntime/features/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tmp
19 changes: 19 additions & 0 deletions druntime/features/GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SED:=sed
ifeq ($(OS),$(filter $(OS),freebsd osx))
SED_INPLACE:=-i ''
else
SED_INPLACE:=-i''
endif

export FEATURE_TMPDIR:=$(CURDIR)/features/tmp
export CC

src/core/sys/linux/config.d: src/core/sys/linux/config.d.in
@cp $< $@
@if [ "$(OS)" = linux ] \
&& ./features/cc_has_function.sh 'closefrom' '#include <unistd.h>'; \
then \
$(SED) $(SED_INPLACE) "s/@HAVE_CLOSEFROM@/true/" $@; \
else \
$(SED) $(SED_INPLACE) "s/@HAVE_CLOSEFROM@/false/" $@; \
fi
35 changes: 35 additions & 0 deletions druntime/features/cc_has_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

die () {
[ $# -ne 0 ] && echo "${@}"
exit 1
}

usage () {
echo "Usage: $0 <function_name> <header>"
}

[ $# -ne 2 ] && usage && die "Invalid usage!"

tmp="${FEATURE_TMPDIR}"

mkdir -p "${tmp}" || die "Could not create temp directory"
outfile="${tmp}/check_${1}.c"

# Check taken from the meson build system
cat > "${outfile}" <<DRUNTIME_CHECK_EOF || die "Could not generate sample C program"
${2}
int main(void) {{
void *a = (void*) &${1};
long long b = (long long) a;
return (int) b;
}}
DRUNTIME_CHECK_EOF

echo -n "Checking for the existence of '${1}'... "
${CC} -o "${outfile}.prog" "${outfile}" > /dev/null 2>&1
res=$?
[ "${res}" -eq 0 ] && echo "found" || echo "NOT found"

rm -f "${outfile}" "${outfile}.prog"
exit "${res}"
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ deprecated("use _ATFILE_SOURCE")
enum __USE_ATFILE = _ATFILE_SOURCE;
deprecated("use _GNU_SOURCE")
enum __USE_GNU = _GNU_SOURCE;

enum __HAVE_CLOSEFROM = @HAVE_CLOSEFROM@;
2 changes: 2 additions & 0 deletions druntime/src/core/sys/linux/unistd.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module core.sys.linux.unistd;

public import core.sys.posix.unistd;
import core.sys.linux.config;

version (linux):
extern(C):
Expand All @@ -23,5 +24,6 @@ char* getpass(const(char)* prompt);
// Exit all threads in a process
void exit_group(int status);

static if (__HAVE_CLOSEFROM)
/// Close all open file descriptors greater or equal to `lowfd`
void closefrom(int lowfd);

0 comments on commit 1efe4df

Please sign in to comment.