This patch adds a module 'dcomp-script', that supports compiling D source
code to binary from within a shell script or Makefile. Thus it's the basis
for writing D programs with the GNU Build System.

D has its own build system, called 'dub' [1], but that's overkill for
simple executables with few dependencies. The 'dcomp-script' module ignores
'dub' and instead invokes the D compiler directly.

[1] https://dub.pm/


2025-03-31  Bruno Haible  <br...@clisp.org>

        dcomp-script: New module.
        * m4/dcomp.m4: New file.
        * build-aux/dcomp.sh.in: New file.
        * modules/dcomp-script: New file.

================================= m4/dcomp.m4 =================================
# dcomp.m4
# serial 1
dnl Copyright (C) 2025 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl This file is offered as-is, without any warranty.

# There are three D implementations, see
# <https://en.wikipedia.org/wiki/D_(programming_language)#Implementations>
# <https://wiki.dlang.org/Compilers>
# <https://dub.pm/dub-reference/build_settings/#buildoptions>
# Although each has different possible options, a few options are accepted by
# all of the implementations: -c, -I, -g, -O.  Some essential options, however,
# are different:
#          gdc          ldc2                  dmd
#          ----------   -------------------   ------------
#          -oFILE       -of=FILE, --of=FILE   -of=FILE
#          -lLIBRARY    -L -lLIBRARY          -L=-lLIBRARY
#          -LDIR        -L -LDIR              -L=-LDIR
#          -Wl,OPTION   -L OPTION             -L=OPTION

# Checks for a D implementation.
# Sets DC and DFLAGS (options that can be used with "$DC").
AC_DEFUN([gt_DCOMP],
[
  AC_MSG_CHECKING([for D compiler])
  pushdef([AC_MSG_CHECKING],[:])dnl
  pushdef([AC_CHECKING],[:])dnl
  pushdef([AC_MSG_RESULT],[:])dnl
  AC_ARG_VAR([DC], [D compiler command])
  AC_ARG_VAR([DFLAGS], [D compiler options])
  dnl On OpenBSD, gdc is called 'egdc' and works less well than dmd.
  AC_CHECK_TOOLS([DC], [gdc ldc2 dmd egdc])
  popdef([AC_MSG_RESULT])dnl
  popdef([AC_CHECKING])dnl
  popdef([AC_MSG_CHECKING])dnl
  if test -n "$DC"; then
    ac_result="$DC"
  else
    ac_result="no"
  fi
  AC_MSG_RESULT([$ac_result])
  AC_SUBST([DC])
  if test -z "$DFLAGS" && test -n "$DC"; then
    case `$DC --version | sed -e 1q` in
      gdc* | egdc* | LDC*) DFLAGS="-g -O2" ;;
      *)                   DFLAGS="-g -O" ;;
    esac
  fi
  AC_SUBST([DFLAGS])
])
============================ build-aux/dcomp.sh.in ============================
#!/bin/sh
# Compile a D program, library, or compilation unit.

# Copyright (C) 2025 Free Software Foundation, Inc.
# Written by Bruno Haible <br...@clisp.org>, 2025.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Usage: /bin/sh dcomp.sh [OPTION] SOURCE.d ...
# where the supported OPTIONs are:
#   -I DIR
#   -c
#   -g
#   -O          (mapped to -O or -O2, depending on implementation)
#   -o FILE     (for libtool compatility)
#   -lLIBRARY   (for libtool compatility)
#   -LDIR       (for libtool compatility)
#   -pthread    (for libtool compatility)
#   -Wl,OPTION  (for libtool compatility)

# Find out which implementation we are using.
case `@DC@ --version | sed -e 1q` in
  gdc* | egdc*) flavor=gdc ;;
  LDC*) flavor=ldc ;;
  DMD*) flavor=dmd ;;
  *)
    echo "Warning: implementation flavor of '"'@DC@'"' not recognized." 1>&2
    flavor=dmd ;;
esac

# In order to construct a command that invokes the D compiler, we need 'eval',
# because some of the arguments may contain spaces.
options_for_print=
options_for_eval=
# Protecting special characters, hiding them from 'eval':
# Double each backslash.
sed_protect_1='s/\\/\\\\/g'
# Escape each dollar, backquote, double-quote.
sed_protect_2a='s/\$/\\$/g'
sed_protect_2b='s/`/\\`/g'
sed_protect_2c='s/"/\\"/g'
# Add double-quotes at the beginning and end of the word.
sed_protect_3a='1s/^/"/'
sed_protect_3b='$s/$/"/'
func_add_word_to_options ()
{
  options_for_print="${options_for_print:+$options_for_print }$1"
  word_protected=`echo "$1" | sed -e "$sed_protect_1" -e "$sed_protect_2a" -e 
"$sed_protect_2b" -e "$sed_protect_2c" -e "$sed_protect_3a" -e 
"$sed_protect_3b"`
  options_for_eval="${options_for_eval:+$options_for_eval }$word_protected"
}

# Process the arguments.
next_is_arg_of=
for arg
do
  if test -z "$next_is_arg_of"; then
    case "$arg" in
      -I | -l | -L)
        echo "dcomp: Unsupported option: $arg. Combine with next argument." 1>&2
        exit 1
        ;;
      -I* | -c | -g)
        func_add_word_to_options "$arg"
        ;;
      -O)
        case "$flavor" in
          gdc | ldc) func_add_word_to_options "-O2" ;;
          dmd)       func_add_word_to_options "-O" ;;
        esac
        ;;
      -o) next_is_arg_of='o' ;;
      -l* | -L* | -pthread)
        case "$arg" in
          -pthread) arg='-lpthread' ;;
        esac
        case "$flavor" in
          gdc) func_add_word_to_options "$arg" ;;
          ldc) func_add_word_to_options '-L'; func_add_word_to_options "$arg" ;;
          dmd) func_add_word_to_options "-L=$arg" ;;
        esac
        ;;
      -Wl,*)
        if test "$flavor" = gdc; then
          func_add_word_to_options "$arg"
        else
          option=`echo "$arg" | sed -e 's/^-Wl,//'`
          case "$flavor" in
            ldc) func_add_word_to_options '-L'; func_add_word_to_options 
"$option" ;;
            dmd) func_add_word_to_options "-L=$option" ;;
          esac
        fi
        ;;
      -*)
        echo "dcomp: Unsupported option: $arg" 1>&2
        exit 1
        ;;
      *)
        # dmd rejects shared library file names such as libfoo.so.1.3:
        # "Error: unrecognized file extension 3"
        if test "$flavor" = dmd \
           && case `basename "$arg"` in lib*.so.*) true ;; *) false ;; esac; 
then
          func_add_word_to_options "-L=$arg"
        else
          func_add_word_to_options "$arg"
        fi
        ;;
    esac
  else
    case "$next_is_arg_of" in
      o)
        case "$flavor" in
          gdc) func_add_word_to_options '-o'; func_add_word_to_options "$arg" ;;
          *)   func_add_word_to_options "-of=$arg" ;;
        esac
        ;;
    esac
    next_is_arg_of=
  fi
done
if test -n "$next_is_arg_of"; then
  echo "dcomp: missing argument to option -$next_is_arg_of" 1>&2
  exit 1
fi

# Execute the command.
test -z "$D_VERBOSE" || echo "@DC@ @DFLAGS@ $options_for_print" 1>&2
eval "@DC@ @DFLAGS@ $options_for_eval"
exit $?
============================ modules/dcomp-script ============================
Description:
Support for compiling D programs.

Files:
build-aux/dcomp.sh.in
m4/dcomp.m4

Depends-on:

configure.ac:
AC_REQUIRE([gt_DCOMP])
AC_CONFIG_FILES([dcomp.sh:build-aux/dcomp.sh.in])

Makefile.am:

License:
GPLed build tool

Maintainer:
all




Reply via email to