From 1ddf50761d5c485e2f509ea935379886d50c245f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 6 Mar 2025 23:56:01 +0100 Subject: [PATCH] gocomp-script: New module. * m4/gocomp.m4: New file. * build-aux/gocomp.sh.in: New file. * modules/gocomp-script: New file. --- ChangeLog | 7 +++ build-aux/gocomp.sh.in | 23 ++++++++ m4/gocomp.m4 | 127 +++++++++++++++++++++++++++++++++++++++++ modules/gocomp-script | 20 +++++++ 4 files changed, 177 insertions(+) create mode 100644 build-aux/gocomp.sh.in create mode 100644 m4/gocomp.m4 create mode 100644 modules/gocomp-script diff --git a/ChangeLog b/ChangeLog index 13b99e0b1c..f2e73bda20 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2025-03-06 Bruno Haible + + gocomp-script: New module. + * m4/gocomp.m4: New file. + * build-aux/gocomp.sh.in: New file. + * modules/gocomp-script: New file. + 2025-03-04 Collin Funk getaddrinfo: Fix AI_NUMERICSERV configure check on GCC 14 (regr. 2025-02-12). diff --git a/build-aux/gocomp.sh.in b/build-aux/gocomp.sh.in new file mode 100644 index 0000000000..b4af4edaea --- /dev/null +++ b/build-aux/gocomp.sh.in @@ -0,0 +1,23 @@ +#!/bin/sh +# Compile a Go program. + +# Copyright (C) 2025 Free Software Foundation, Inc. +# Written by Bruno Haible , 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 . + +# Usage: /bin/sh gocomp.sh [OPTION] SOURCE.go ... + +test -z "$GO_VERBOSE" || echo "@GO@ build @GOCOMPFLAGS@ $@" +exec @GO@ build @GOCOMPFLAGS@ "$@" diff --git a/m4/gocomp.m4 b/m4/gocomp.m4 new file mode 100644 index 0000000000..f739927e1f --- /dev/null +++ b/m4/gocomp.m4 @@ -0,0 +1,127 @@ +# gocomp.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 two Go implementations, that differ essentially only regarding +# the compiler and the used runtime version: +# * The golang Go is the original implementation, typically a little more +# up-to-date. It supports only the major computing platforms. +# * The GCC Go implementation is part of the GNU Compiler Collection. +# It lags behind a bit. It supports all platforms supported by GCC. +# The golang Go implementation produces smaller executables, and the +# binary packages needed for development are smaller as well. +# +# Therefore the preferred implementation is +# * golang where available, +# * GCC on the platforms where golang is not available. This includes in +# particular: +# - Linux/alpha, Linux/hppa, Linux/m68k, Linux/sparc, Linux/powerpc (32-bit) +# - GNU/Hurd +# - Solaris/sparc +# - Haiku + +# Sets GO_CHOICE to the preferred Go compiler implementation: +# 'golang' or 'gcc' or 'any' or 'no'. +# Here +# - 'golang' means the original Go compiler. +# - 'gcc' means GCC Go compiler. +# The runtime library of both is the same (possibly in different versions, +# though). +AC_DEFUN([gt_GO_CHOICE], +[ + AC_MSG_CHECKING([for preferred Go compiler]) + AC_ARG_ENABLE([go], + [ --enable-go[[=IMPL]] choose preferred Go compiler (golang, gcc)], + [GO_CHOICE="$enableval"], + [GO_CHOICE=any]) + AC_SUBST([GO_CHOICE]) + AC_MSG_RESULT([$GO_CHOICE]) +]) + +# How to run Go programs? +# Assume you want to distribute a Go program. In which form? And what are +# the dependencies to do so? +# +# There are three ways to do so: +# +# * Run the program by compiling it on-the-fly in a temporary directory +# and running it from there: +# ${GO} run foo.go +# You would distribute foo.go (very small). +# But the dependencies are large: the Go development environment. +# On Ubuntu 24.04 this is: +# - with 'golang': the package 'golang-go' +# 11 MB for /usr/lib/go-1.22/bin/go, +# 85 MB for /usr/lib/go-1.22/pkg/tool/linux_amd64/* +# = 96 MB in total +# - with 'gccgo': the package 'gccgo-13' +# 60 MB for /usr/lib/x86_64-linux-gnu/libgo.so.22.0.0, +# > 40 MB for /usr/libexec/gcc/x86_64-linux-gnu/13/go1 +# and /usr/bin/x86_64-linux-gnu-go-13, +# 116 MB for /usr/lib/gcc/x86_64-linux-gnu/13/libgo.a +# = 216 MB in total. +# +# * You distribute the binary, linked against the shared Go runtime library. +# Running the program is just invoking that binary. +# On Ubuntu the dependencies are: +# - with 'golang': unsupported. +# - with 'gccgo': the package 'libgo22' +# 60 MB for /usr/lib/x86_64-linux-gnu/libgo.so.22.0.0 +# = 60 MB in total. +# +# * You distribute the binary, linked statically with the needed parts +# of the Go runtime library: +# ${GO} build ${GOCOMPFLAGS} foo.go +# Running the program is just invoking that binary. +# On Ubuntu the dependencies are: +# - with 'golang': No dependencies; the binary is statically linked. +# The stripped executable's size is >= 1 MB. +# - with 'gccgo': No dependencies; the binary is statically linked +# (when using GOCOMPFLAGS='-static') or statically linked except for +# the standard C library (when using GOCOMPFLAGS='-static-libgo'). +# The stripped executable's size is 5 MB or 6 MB, respectively. +# Distros generally prefer avoiding dynamic linking with libc, +# so let's use that. +# +# It is clear that the third approach will be preferred for small programs. + +# Prerequisites of gocomp.sh. +# Checks for a Go implementation. +# Sets HAVE_GOCOMP to nonempty if gocomp.sh will work. +# Also sets GO and GOCOMPFLAGS (options that can be used with "$GO build"). +AC_DEFUN([gt_GOCOMP], +[ + AC_REQUIRE([gt_GO_CHOICE]) + AC_MSG_CHECKING([for Go compiler]) + HAVE_GOCOMP=1 + pushdef([AC_MSG_CHECKING],[:])dnl + pushdef([AC_CHECKING],[:])dnl + pushdef([AC_MSG_RESULT],[:])dnl + dnl Prefer golang over gcc by default, because it produces much smaller + dnl executables (see above). + if test "$GO_CHOICE" = gcc; then + AC_PATH_PROGS([GO], [go-30 go-29 go-28 go-27 go-26 go-25 go-24 go-23 go-22 go-21 go-20 go-19 go-18 go-17 go-16 go-15 go-14 go-13 go-12 go-11 go-10 go-9 go-8 go-7 go-6 go-5 go]) + else + AC_PATH_PROGS([GO], [go go-30 go-29 go-28 go-27 go-26 go-25 go-24 go-23 go-22 go-21 go-20 go-19 go-18 go-17 go-16 go-15 go-14 go-13 go-12 go-11 go-10 go-9 go-8 go-7 go-6 go-5]) + fi + popdef([AC_MSG_RESULT])dnl + popdef([AC_CHECKING])dnl + popdef([AC_MSG_CHECKING])dnl + if test -n "$GO"; then + case "$GO" in + go | */go ) GOCOMPFLAGS= ;; + *) GOCOMPFLAGS='-gccgoflags=-static-libgo' ;; + esac + ac_result="$GO" + else + HAVE_GOCOMP= + ac_result="no" + fi + AC_MSG_RESULT([$ac_result]) + AC_SUBST([GO]) + AC_SUBST([GOCOMPFLAGS]) +]) diff --git a/modules/gocomp-script b/modules/gocomp-script new file mode 100644 index 0000000000..647fd242e3 --- /dev/null +++ b/modules/gocomp-script @@ -0,0 +1,20 @@ +Description: +Script to compile a Go program. + +Files: +build-aux/gocomp.sh.in +m4/gocomp.m4 + +Depends-on: + +configure.ac: +AC_REQUIRE([gt_GOCOMP]) +AC_CONFIG_FILES([gocomp.sh:build-aux/gocomp.sh.in]) + +Makefile.am: + +License: +GPLed build tool + +Maintainer: +all -- 2.39.5