From: Michael Tremer Date: Wed, 2 Feb 2011 16:58:06 +0000 (+0100) Subject: build-essentials: Add buildsystem includes to package. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54fb3a5ed11efafbcd0c91027a2e33ff7e025e47;p=ipfire-3.x.git build-essentials: Add buildsystem includes to package. --- diff --git a/pkgs/core/build-essentials/build-essentials.nm b/pkgs/core/build-essentials/build-essentials.nm index 0973fc8bd..537eeee75 100644 --- a/pkgs/core/build-essentials/build-essentials.nm +++ b/pkgs/core/build-essentials/build-essentials.nm @@ -26,7 +26,7 @@ include $(PKGROOT)/Include PKG_NAME = build-essentials PKG_VER = $(DISTRO_VERSION) -PKG_REL = 0 +PKG_REL = 1 PKG_MAINTAINER = Michael Tremer PKG_GROUP = Development/Tools @@ -46,11 +46,17 @@ STAGE_PREPARE = # Nothing to do here STAGE_BUILD = # Nothing to do here define STAGE_INSTALL - # Install the quality-agent + # Install the quality-agent and buildsystem header files. -mkdir -pv $(BUILDROOT)/usr/sbin + -mkdir -pv $(BUILDROOT)/usr/lib/buildsystem -mkdir -pv $(BUILDROOT)/usr/lib/quality-agent + cp -vf buildsystem/* $(BUILDROOT)/usr/lib/buildsystem + cp -vf quality-agent/quality-agent $(BUILDROOT)/usr/sbin/ cp -vrf quality-agent/quality-agent.d/* \ $(BUILDROOT)/usr/lib/quality-agent/ + + -mkdir -pv $(BUILDROOT)/etc/profile.d + cp -vf buildsystem.sh $(BUILDROOT)/etc/profile.d/ endef diff --git a/pkgs/core/build-essentials/buildsystem.sh b/pkgs/core/build-essentials/buildsystem.sh new file mode 100644 index 000000000..6e455248d --- /dev/null +++ b/pkgs/core/build-essentials/buildsystem.sh @@ -0,0 +1,8 @@ + +# Path where the include files of the buildsystem +# are located. +export BUILDSYSTEM_ROOT="/usr/lib/buildsystem" + +# XXX just for now, we need to set PKGROOT to be compatible +# with the old makefiles +export PKGROOT=${BUILDSYSTEM_ROOT} diff --git a/pkgs/core/build-essentials/buildsystem/Constants b/pkgs/core/build-essentials/buildsystem/Constants new file mode 100644 index 000000000..e882ad1b6 --- /dev/null +++ b/pkgs/core/build-essentials/buildsystem/Constants @@ -0,0 +1,148 @@ + +############################################################################### +# +# Constant definitions of the naoki build system +# +############################################################################### + +# If we are running in a chroot, we map all directories to $(BASEDIR), otherwise +# BASEDIR will be the absolute path (for gathering information from the source +# packages) +ifeq "$(CHROOT)" "1" + BASEDIR = /usr/src +endif + +# Set default directories +DIR_APP = $(DIR_SRC)/$(THISAPP) +DIR_DL = $(BASEDIR)/cache +DIR_PACKAGES = /usr/src/packages/$(PKG_ARCH) +DIR_PATCHES = $(DIR_SOURCE)/patches +DIR_SRC = /usr/src +DIR_TMP = /tmp +DIR_SOURCE = $(CURDIR) +DIR_TOOLS = $(BASEDIR)/tools + +# Directory where to search for object files +VPATH = $(DIR_DL) + +# Paths to scripts +DO_EXTRACT = tar xaf +DO_QUALITY_AGENT = $(DIR_TOOLS)/quality-agent + +############################################################################### +# +# Build environment +# +############################################################################### + +# Export CFLAGS + CXXFLAGS +export CFLAGS +export CXXFLAGS + +# Options that get passed to configure by default +CONFIGURE_OPTIONS = \ + --prefix=/usr + +############################################################################### +# +# Packager variables +# +############################################################################### +# Variables that get exported (and expanded for the sub-packages) to the +# packager process +PKG_VARIABLES = \ + CONTROL_PREIN \ + CONTROL_PREUN \ + CONTROL_POSTIN \ + CONTROL_POSTUN \ + \ + PKG_ARCH \ + PKG_BUILD_DEPS \ + PKG_DEPS \ + PKG_DESCRIPTION \ + PKG_EPOCH \ + PKG_FILES \ + PKG_GROUP \ + PKG_MAINTAINER \ + PKG_LICENSE \ + PKG_PROVIDES \ + PKG_REL \ + PKG_SUMMARY \ + PKG_URL \ + PKG_VER + +# Variables that exported to the packager process +# These reflect settings from the build system +export BUILD_DATE ?= $(shell date -u) +export BUILD_HOST ?= $(shell cat /proc/sys/kernel/hostname) +export BUILD_ID + +############################################################################### +# +# Package variables +# +############################################################################### +# +# Variables +# + +# The actual package name (the name of the directory) +PKG_NAME_REAL = $(notdir $(CURDIR)) +PKG_NAME = $(PKG_NAME_REAL) + +# Set default epoch +PKG_EPOCH = 0 + +# Shortcut to package name + version +THISAPP = $(PKG_NAME)-$(PKG_VER) +THISVER = $(PKG_EPOCH):$(PKG_VER)-$(PKG_REL) + +# All packages depend on gcc and headers by default. +PKG_BUILD_DEPS+= gcc glibc-devel kernel-headers +PKG_DEPS += + +# All PKG_OBJECTS are downloaded. This is in most cases the tarball. +PKG_OBJECTS = $(PKG_TARBALL) +OBJECTS = $(PKG_OBJECTS) + +# List of packages to build +PKG_PACKAGES = $(PKG_NAME_REAL) + +# +# Macros +# + +# Abstract variable that translates the package names to a list of filenames +PKG_PACKAGES_FILES = $(foreach package,$(PKG_PACKAGES),$(call DO_PACKAGE_FILENAME,$(package))) + +# Automatically detect all patches in "patches" +PKG_PATCHES = \ + $(foreach patch,$(wildcard $(DIR_PATCHES)/*.patch),$(notdir $(patch))) +PKG_PATCHES += \ + $(foreach patch,$(wildcard $(DIR_PATCHES)/*.patch0),$(notdir $(patch))) +PKG_PATCHES += \ + $(foreach patch,$(wildcard $(DIR_PATCHES)/*.diff),$(notdir $(patch))) + +# Dynamic command that applies all patches +DO_PATCHES = cd $(DIR_APP) && $(DIR_TOOLS)/patch $(foreach patch,$(PKG_PATCHES),$(DIR_PATCHES)/$(patch)) + +# Get a list of files that are installed automatically +PKG_DEFAULT_FILES = $(wildcard *.default) +PKG_DEFAULT_FILES += $(wildcard default/*) +PKG_INIT_FILES = $(wildcard *.init) +PKG_INIT_FILES += $(wildcard init/*.conf) +PKG_PAM_FILES = $(wildcard *.pam) +PKG_PAM_FILES += $(wildcard pam.d/*) + +############################################################################### +# +# Quality agent +# +############################################################################### + +export QUALITY_AGENT_PERMIT_NOT_FULL_RELRO +export QUALITY_AGENT_RPATH_ALLOW_ORIGIN +export QUALITY_AGENT_WHITELIST_EXECSTACK +export QUALITY_AGENT_WHITELIST_NX +export QUALITY_AGENT_WHITELIST_RPATH +export QUALITY_AGENT_WHITELIST_SONAME diff --git a/pkgs/core/build-essentials/buildsystem/Functions b/pkgs/core/build-essentials/buildsystem/Functions new file mode 100644 index 000000000..42cfa80fe --- /dev/null +++ b/pkgs/core/build-essentials/buildsystem/Functions @@ -0,0 +1,177 @@ + +############################################################################### +# +# Function definitions of the naoki build system +# +############################################################################### + +include $(PKGROOT)/gmsl + +DO_PACKAGE_FILENAME = $(1)$(call DO_PKG_SUFFIX,$(1)) + +DO_PKG_SUFFIX = \ + -$(if $(PKG_VER-$(1)),$(PKG_VER-$(1)),$(PKG_VER))-$(DISTRO_SNAME)$(DISTRO_EPOCH)-$(PKG_ARCH).$(if $(PKG_REL-$(1)),$(PKG_REL-$(1)),$(PKG_REL)).ipk + +define DO_INIT + # Run ldconfig + ldconfig +endef + +define DO_PACKAGE + @echo "#####################################################################" + @echo "# $(1) - Package build started" + @echo "#####################################################################" + + @$(foreach var,$(PKG_VARIABLES),$(if $($(var)-$(1)),$(var)="$(strip $($(var)-$(1)))",$(var)="$(strip $($(var)))")) \ + $(DIR_TOOLS)/packager $(1) $(DIR_PACKAGES)/$(call DO_PACKAGE_FILENAME,$(1)) + + @echo "#####################################################################" + @echo "# $(1) - Package build finished" + @echo "#####################################################################" + +endef + +define DO_FILELIST + @echo "# Filelist dump" + @cd $(BUILDROOT) && find -ls +endef + +define __INSTALL_DEFAULT + -mkdir -pv $(BUILDROOT)/etc/default + cd $(DIR_APP) && cp -vf $(DIR_SOURCE)/$(1) $(BUILDROOT)/etc/default/$(subst .default,,$(notdir $(1))) + +endef + +define DO_INSTALL_DEFAULT + $(foreach file,$(PKG_DEFAULT_FILES),$(call __INSTALL_DEFAULT,$(file))) +endef + +define __INSTALL_INIT + -mkdir -pv $(BUILDROOT)/etc/init + cd $(DIR_APP) && cp -vf $(DIR_SOURCE)/$(1) $(BUILDROOT)/etc/init/$(subst .init,.conf,$(notdir $(1))) + +endef + +define DO_INSTALL_INIT + $(foreach file,$(PKG_INIT_FILES),$(call __INSTALL_INIT,$(file))) +endef + +define __INSTALL_PAM + -mkdir -pv $(BUILDROOT)/etc/pam.d + cd $(DIR_APP) && cp -vf $(DIR_SOURCE)/$(1) $(BUILDROOT)/etc/pam.d/$(subst .pam,,$(notdir $(1))) + +endef + +define DO_INSTALL_PAM + $(foreach file,$(PKG_PAM_FILES),$(call __INSTALL_PAM,$(file))) +endef + +define DO_PYTHON_COMPILE + @find $(BUILDROOT) -name "*.py" | xargs $(DIR_TOOLS)/py-compile +endef + +define DO_FIX_LIBTOOL + # remove rpath from libtool + @if [ -e "libtool" ]; then \ + sed -i libtool \ + -e 's|^hardcode_libdir_flag_spec=.*|hardcode_libdir_flag_spec=""|g' \ + -e 's|^runpath_var=LD_RUN_PATH|runpath_var=DIE_RPATH_DIE|g'; \ + fi +endef + +define DO_PREPARE + # Initialize the environment at the beginning + $(DO_INIT) + + @echo "#####################################################################" + @echo "# $(PKG_NAME) - Preparation started" + @echo "#####################################################################" + + $(STAGE_PREPARE) + + @echo "#####################################################################" + @echo "# $(PKG_NAME) - Preparation finished" + @echo "#####################################################################" +endef + +define DO_BUILD + @echo "#####################################################################" + @echo "# $(PKG_NAME) - Build started" + @echo "#####################################################################" + + $(STAGE_BUILD) + + @echo "#####################################################################" + @echo "# $(PKG_NAME) - Build finished" + @echo "#####################################################################" +endef + +define DO_TEST + @echo "#####################################################################" + @echo "# $(PKG_NAME) - Test started" + @echo "#####################################################################" + + $(STAGE_TEST) + + @echo "#####################################################################" + @echo "# $(PKG_NAME) - Test finished" + @echo "#####################################################################" +endef + +define DO_INSTALL + @echo "#####################################################################" + @echo "# $(PKG_NAME) - Install started" + @echo "#####################################################################" + + -mkdir -pv $(BUILDROOT) + + $(STAGE_INSTALL) + + $(DO_INSTALL_DEFAULT) + $(DO_INSTALL_INIT) + $(DO_INSTALL_PAM) + + $(DO_PYTHON_COMPILE) + + @echo "#####################################################################" + @echo "# $(PKG_NAME) - Install finished" + @echo "#####################################################################" + + $(DO_QUALITY_AGENT) + $(DO_FILELIST) +endef + +STAGE_PACKAGE_TARGETS = $(call reverse,$(PKG_PACKAGES_FILES)) +STAGE_DONE = $(ROOT)/.done + +define STAGE_PREPARE + $(if $(PKG_TARBALL),cd $(DIR_SRC) && $(DO_EXTRACT) $(DIR_DL)/$(PKG_TARBALL)) + + $(if $(PKG_PATCHES),$(DO_PATCHES)) + + $(STAGE_PREPARE_CMDS) + $(STAGE_PREPARE_CMDS2) +endef + +STAGE_BUILD_TARGETS = + +define STAGE_BUILD + cd $(DIR_APP) && \ + $(CONFIGURE_ENVIRONMENT) \ + ./configure \ + $(CONFIGURE_OPTIONS) + + $(DO_FIX_LIBTOOL) + $(STAGE_CONFIGURE_CMDS) + + cd $(DIR_APP) && make $(STAGE_BUILD_TARGETS) $(PARALLELISMFLAGS) + $(STAGE_BUILD_CMDS) +endef + +STAGE_INSTALL_TARGETS = install + +define STAGE_INSTALL + cd $(DIR_APP) && make $(STAGE_INSTALL_TARGETS) DESTDIR=$(BUILDROOT) + + $(STAGE_INSTALL_CMDS) +endef diff --git a/pkgs/core/build-essentials/buildsystem/Include b/pkgs/core/build-essentials/buildsystem/Include new file mode 100644 index 000000000..a3d62be3d --- /dev/null +++ b/pkgs/core/build-essentials/buildsystem/Include @@ -0,0 +1,26 @@ +############################################################################### +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2007, 2008, 2009 Michael Tremer & Christian Schmidt # +# # +# 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 . # +# # +############################################################################### + +.SECONDEXPANSION: + +include $(PKGROOT)/Constants +include $(PKGROOT)/Functions +include $(PKGROOT)/Targets +include $(PKGROOT)/Templates diff --git a/pkgs/core/build-essentials/buildsystem/Targets b/pkgs/core/build-essentials/buildsystem/Targets new file mode 100644 index 000000000..e969f3cd3 --- /dev/null +++ b/pkgs/core/build-essentials/buildsystem/Targets @@ -0,0 +1,49 @@ + +############################################################################### +# +# Target definitions of the naoki build system +# +############################################################################### + +.PHONY: info +info: + @echo "PKG_ARCH=\"$(PKG_ARCH)\"" + @echo "PKG_BUILD_DEPENDENCIES=\"$(PKG_BUILD_DEPS)\"" + @echo "PKG_DEPENDENCIES=\"$(PKG_DEPS)\"" + @echo "PKG_DESCRIPTION=\"$(strip $(PKG_DESCRIPTION))\"" + @echo "PKG_EPOCH=\"$(PKG_EPOCH)\"" + @echo "PKG_GROUP=\"$(PKG_GROUP)\"" + @echo "PKG_LICENSE=\"$(PKG_LICENSE)\"" + @echo "PKG_MAINTAINER=\"$(PKG_MAINTAINER)\"" + @echo "PKG_NAME=\"$(PKG_NAME_REAL)\"" + @echo "PKG_OBJECTS=\"$(strip $(OBJECTS))\"" + @echo "PKG_PACKAGES=\"$(PKG_PACKAGES)\"" + @echo "PKG_PACKAGES_FILES=\"$(PKG_PACKAGES_FILES)\"" + @echo "PKG_PATCHES=\"$(PKG_PATCHES)\"" + @echo "PKG_VER=\"$(PKG_VER)\"" + @echo "PKG_REL=\"$(PKG_REL)\"" + @echo "PKG_SUMMARY=\"$(strip $(PKG_SUMMARY))\"" + @echo "PKG_URL=\"$(PKG_URL)\"" + @echo "SOURCE_DIR=\"$(DIR_APP)\"" + +$(OBJECTS): + @echo "Object file \"$@\" is required." >&2 + @exit 1 + +.PHONY: build +build: $(STAGE_DONE) + $(foreach package,$(call reverse,$(PKG_PACKAGES)),$(call DO_PACKAGE,$(package))) + +.PHONY: shell +shell: $(OBJECTS) + $(if $(STAGE_PREPARE),$(DO_PREPARE)) + +prepare: + $(if $(STAGE_PREPARE),$(DO_PREPARE)) + +$(STAGE_DONE): $(OBJECTS) + $(if $(STAGE_PREPARE),$(DO_PREPARE)) + $(if $(STAGE_BUILD),$(DO_BUILD)) + $(if $(STAGE_TEST),$(DO_TEST)) + $(if $(STAGE_INSTALL),$(DO_INSTALL)) + @touch $@ diff --git a/pkgs/core/build-essentials/buildsystem/Templates b/pkgs/core/build-essentials/buildsystem/Templates new file mode 100644 index 000000000..e1196ad3c --- /dev/null +++ b/pkgs/core/build-essentials/buildsystem/Templates @@ -0,0 +1,62 @@ + +############################################################################### +# +# Template definitions of the naoki build system +# +############################################################################### + +############################################################################### +# Default template +############################################################################### + +PKG_PROVIDES-$(PKG_NAME_REAL) = $(PKG_NAME_REAL)=$(THISVER) + +# Package all files by default +PKG_FILES = / + +############################################################################### +# Devel template +############################################################################### + +PKG_DESCRIPTION-$(PKG_NAME_REAL)-devel = Development files of $(THISAPP). +PKG_SUMMARY-$(PKG_NAME_REAL)-devel = $(PKG_DESCRIPTION-$(PKG_NAME_REAL)-devel) + +PKG_DEPS-$(PKG_NAME_REAL)-devel = $(PKG_NAME_REAL)=$(THISVER) + +define PKG_FILES-$(PKG_NAME_REAL)-devel + /usr/bin/*-config + /usr/include + /usr/lib/*.a + /usr/lib/pkgconfig + /usr/share/aclocal + */lib/*.so + /usr/share/*/cmake + /usr/share/man/man2 + /usr/share/man/man3 + /usr/share/vala +endef + + +############################################################################### +# Library template +############################################################################### + +PKG_DESCRIPTION-$(PKG_NAME_REAL)-libs = Library files of $(THISAPP). +PKG_SUMMARY-$(PKG_NAME_REAL)-libs = $(PKG_DESCRIPTION-$(PKG_NAME_REAL)-libs) + +PKG_DEPS-$(PKG_NAME_REAL)-libs = $(PKG_NAME_REAL)=$(THISVER) + +define PKG_FILES-$(PKG_NAME_REAL)-libs + /lib/lib*.so.* + /usr/lib/lib*.so.* +endef + +# Another naming scheme template... +PKG_DESCRIPTION-lib$(PKG_NAME_REAL) = $(PKG_DESCRIPTION-$(PKG_NAME_REAL)-libs) +PKG_SUMMARY-lib$(PKG_NAME_REAL) = $(PKG_SUMMARY-$(PKG_NAME_REAL)-libs) +PKG_FILES-lib$(PKG_NAME_REAL) = $(PKG_FILES-$(PKG_NAME_REAL)-libs) + +PKG_DESCRIPTION-lib$(PKG_NAME_REAL)-devel = $(PKG_DESCRIPTION-$(PKG_NAME_REAL)-devel) +PKG_SUMMARY-lib$(PKG_NAME_REAL)-devel = $(PKG_SUMMARY-$(PKG_NAME_REAL)-devel) +PKG_FILES-lib$(PKG_NAME_REAL)-devel = $(PKG_FILES-$(PKG_NAME_REAL)-devel) blah +PKG_BUILD_DEPS-lib$(PKG_NAME_REAL)-devel = $(PKG_BUILD_DEPS-$(PKG_NAME_REAL)-devel) diff --git a/pkgs/core/build-essentials/buildsystem/__gmsl b/pkgs/core/build-essentials/buildsystem/__gmsl new file mode 100644 index 000000000..596ff1911 --- /dev/null +++ b/pkgs/core/build-essentials/buildsystem/__gmsl @@ -0,0 +1,854 @@ +# ---------------------------------------------------------------------------- +# +# GNU Make Standard Library (GMSL) +# +# A library of functions to be used with GNU Make's $(call) that +# provides functionality not available in standard GNU Make. +# +# Copyright (c) 2005-2007 John Graham-Cumming +# +# This file is part of GMSL +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# Neither the name of the John Graham-Cumming nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# ---------------------------------------------------------------------------- + +# This is the GNU Make Standard Library version number as a list with +# three items: major, minor, revision + +gmsl_version := 1 0 11 + +# Used to output warnings and error from the library, it's possible to +# disable any warnings or errors by overriding these definitions +# manually or by setting GMSL_NO_WARNINGS or GMSL_NO_ERRORS + +__gmsl_name := GNU Make Standard Library +__gmsl_warning = $(warning $(__gmsl_name): $1) +__gmsl_error = $(error $(__gmsl_name): $1) + +ifdef GMSL_NO_WARNINGS +__gmsl_warning := +endif +ifdef GMSL_NO_ERRORS +__gmsl_error := +endif + +# If GMSL_TRACE is enabled then calls to the library functions are +# traced to stdout using warning messages with their arguments + +ifdef GMSL_TRACE +__gmsl_tr1 = $(warning $0('$1')) +__gmsl_tr2 = $(warning $0('$1','$2')) +__gmsl_tr3 = $(warning $0('$1','$2','$3')) +else +__gmsl_tr1 := +__gmsl_tr2 := +__gmsl_tr3 := +endif + +# Figure out whether we have $(eval) or not (GNU Make 3.80 and above) +# if we do not then output a warning message, if we do then some +# functions will be enabled. + +__gmsl_have_eval := $(false) +__gmsl_ignore := $(eval __gmsl_have_eval := $(true)) + +# If this is being run with Electric Cloud's emake then warn that +# their $(eval) support is incomplete. + +ifdef ECLOUD_BUILD_ID +$(warning You are using Electric Cloud's emake which has incomplete $$(eval) support) +__gmsl_have_eval := $(false) +endif + +# See if we have $(lastword) (GNU Make 3.81 and above) + +__gmsl_have_lastword := $(lastword $(false) $(true)) + +# See if we have native or and and (GNU Make 3.81 and above) + +__gmsl_have_or := $(if $(filter-out undefined, \ + $(origin or)),$(call or,$(true),$(false))) +__gmsl_have_and := $(if $(filter-out undefined, \ + $(origin and)),$(call and,$(true),$(true))) + +ifneq ($(__gmsl_have_eval),$(true)) +$(call __gmsl_warning,GNU Make $(MAKE_VERSION) does not support $$$$(eval): some functions disabled) +endif + +# ---------------------------------------------------------------------------- +# Function: gmsl_compatible +# Arguments: List containing the desired library version number (maj min rev) +# Returns: $(true) if this version of the library is compatible +# with the requested version number, otherwise $(false) +# ---------------------------------------------------------------------------- +gmsl_compatible = $(strip \ + $(if $(call gt,$(word 1,$1),$(word 1,$(gmsl_version))), \ + $(false), \ + $(if $(call lt,$(word 1,$1),$(word 1,$(gmsl_version))), \ + $(true), \ + $(if $(call gt,$(word 2,$1),$(word 2,$(gmsl_version))), \ + $(false), \ + $(if $(call lt,$(word 2,$1),$(word 2,$(gmsl_version))), \ + $(true), \ + $(call lte,$(word 3,$1),$(word 3,$(gmsl_version)))))))) + +# ########################################################################### +# LOGICAL OPERATORS +# ########################################################################### + +# not is defined in gmsl + +# ---------------------------------------------------------------------------- +# Function: and +# Arguments: Two boolean values +# Returns: Returns $(true) if both of the booleans are true +# ---------------------------------------------------------------------------- +ifneq ($(__gmsl_have_and),$(true)) +and = $(__gmsl_tr2)$(if $1,$(if $2,$(true),$(false)),$(false)) +endif + +# ---------------------------------------------------------------------------- +# Function: or +# Arguments: Two boolean values +# Returns: Returns $(true) if either of the booleans is true +# ---------------------------------------------------------------------------- +ifneq ($(__gmsl_have_or),$(true)) +or = $(__gmsl_tr2)$(if $1$2,$(true),$(false)) +endif + +# ---------------------------------------------------------------------------- +# Function: xor +# Arguments: Two boolean values +# Returns: Returns $(true) if exactly one of the booleans is true +# ---------------------------------------------------------------------------- +xor = $(__gmsl_tr2)$(if $1,$(if $2,$(false),$(true)),$(if $2,$(true),$(false))) + +# ---------------------------------------------------------------------------- +# Function: nand +# Arguments: Two boolean values +# Returns: Returns value of 'not and' +# ---------------------------------------------------------------------------- +nand = $(__gmsl_tr2)$(if $1,$(if $2,$(false),$(true)),$(true)) + +# ---------------------------------------------------------------------------- +# Function: nor +# Arguments: Two boolean values +# Returns: Returns value of 'not or' +# ---------------------------------------------------------------------------- +nor = $(__gmsl_tr2)$(if $1$2,$(false),$(true)) + +# ---------------------------------------------------------------------------- +# Function: xnor +# Arguments: Two boolean values +# Returns: Returns value of 'not xor' +# ---------------------------------------------------------------------------- +xnor =$(__gmsl_tr2)$(if $1,$(if $2,$(true),$(false)),$(if $2,$(false),$(true))) + +# ########################################################################### +# LIST MANIPULATION FUNCTIONS +# ########################################################################### + +# ---------------------------------------------------------------------------- +# Function: first (same as LISP's car, or head) +# Arguments: 1: A list +# Returns: Returns the first element of a list +# ---------------------------------------------------------------------------- +first = $(__gmsl_tr1)$(firstword $1) + +# ---------------------------------------------------------------------------- +# Function: last +# Arguments: 1: A list +# Returns: Returns the last element of a list +# ---------------------------------------------------------------------------- +ifeq ($(__gmsl_have_lastword),$(true)) +last = $(__gmsl_tr1)$(lastword $1) +else +last = $(__gmsl_tr1)$(if $1,$(word $(words $1),$1)) +endif + +# ---------------------------------------------------------------------------- +# Function: rest (same as LISP's cdr, or tail) +# Arguments: 1: A list +# Returns: Returns the list with the first element removed +# ---------------------------------------------------------------------------- +rest = $(__gmsl_tr1)$(wordlist 2,$(words $1),$1) + +# ---------------------------------------------------------------------------- +# Function: chop +# Arguments: 1: A list +# Returns: Returns the list with the last element removed +# ---------------------------------------------------------------------------- +chop = $(__gmsl_tr1)$(wordlist 2,$(words $1),x $1) + +# ---------------------------------------------------------------------------- +# Function: map +# Arguments: 1: Name of function to $(call) for each element of list +# 2: List to iterate over calling the function in 1 +# Returns: The list after calling the function on each element +# ---------------------------------------------------------------------------- +map = $(__gmsl_tr2)$(strip $(foreach a,$2,$(call $1,$a))) + +# ---------------------------------------------------------------------------- +# Function: pairmap +# Arguments: 1: Name of function to $(call) for each pair of elements +# 2: List to iterate over calling the function in 1 +# 3: Second list to iterate over calling the function in 1 +# Returns: The list after calling the function on each pair of elements +# ---------------------------------------------------------------------------- +pairmap = $(strip $(__gmsl_tr3)\ + $(if $2$3,$(call $1,$(call first,$2),$(call first,$3)) \ + $(call pairmap,$1,$(call rest,$2),$(call rest,$3)))) + +# ---------------------------------------------------------------------------- +# Function: leq +# Arguments: 1: A list to compare against... +# 2: ...this list +# Returns: Returns $(true) if the two lists are identical +# ---------------------------------------------------------------------------- +leq = $(__gmsl_tr2)$(strip $(if $(call seq,$(words $1),$(words $2)), \ + $(call __gmsl_list_equal,$1,$2),$(false))) + +__gmsl_list_equal = $(if $(strip $1), \ + $(if $(call seq,$(call first,$1),$(call first,$2)), \ + $(call __gmsl_list_equal, \ + $(call rest,$1), \ + $(call rest,$2)), \ + $(false)), \ + $(true)) + +# ---------------------------------------------------------------------------- +# Function: lne +# Arguments: 1: A list to compare against... +# 2: ...this list +# Returns: Returns $(true) if the two lists are different +# ---------------------------------------------------------------------------- +lne = $(__gmsl_tr2)$(call not,$(call leq,$1,$2)) + +# ---------------------------------------------------------------------------- +# Function: reverse +# Arguments: 1: A list to reverse +# Returns: The list with its elements in reverse order +# ---------------------------------------------------------------------------- +reverse =$(__gmsl_tr1)$(strip $(if $1,$(call reverse,$(call rest,$1)) \ + $(call first,$1))) + +# ---------------------------------------------------------------------------- +# Function: uniq +# Arguments: 1: A list from which to remove repeated elements +# Returns: The list with duplicate elements removed without reordering +# ---------------------------------------------------------------------------- +uniq = $(strip $(__gmsl_tr1)$(if $1,$(call uniq,$(call chop,$1)) \ + $(if $(filter $(call last,$1),$(call chop,$1)),,$(call last,$1)))) + +# ---------------------------------------------------------------------------- +# Function: length +# Arguments: 1: A list +# Returns: The number of elements in the list +# ---------------------------------------------------------------------------- +length = $(__gmsl_tr1)$(words $1) + +# ########################################################################### +# STRING MANIPULATION FUNCTIONS +# ########################################################################### + +# Helper function that translates any GNU Make 'true' value (i.e. a +# non-empty string) to our $(true) + +__gmsl_make_bool = $(if $(strip $1),$(true),$(false)) + +# ---------------------------------------------------------------------------- +# Function: seq +# Arguments: 1: A string to compare against... +# 2: ...this string +# Returns: Returns $(true) if the two strings are identical +# ---------------------------------------------------------------------------- +seq = $(__gmsl_tr2)$(if $(filter-out xx,x$(subst $1,,$2)$(subst $2,,$1)x),$(false),$(true)) + +# ---------------------------------------------------------------------------- +# Function: sne +# Arguments: 1: A string to compare against... +# 2: ...this string +# Returns: Returns $(true) if the two strings are not the same +# ---------------------------------------------------------------------------- +sne = $(__gmsl_tr2)$(call not,$(call seq,$1,$2)) + +# ---------------------------------------------------------------------------- +# Function: split +# Arguments: 1: The character to split on +# 2: A string to split +# Returns: Splits a string into a list separated by spaces at the split +# character in the first argument +# ---------------------------------------------------------------------------- +split = $(__gmsl_tr2)$(strip $(subst $1, ,$2)) + +# ---------------------------------------------------------------------------- +# Function: merge +# Arguments: 1: The character to put between fields +# 2: A list to merge into a string +# Returns: Merges a list into a single string, list elements are separated +# by the character in the first argument +# ---------------------------------------------------------------------------- +merge = $(__gmsl_tr2)$(strip $(if $2, \ + $(if $(call seq,1,$(words $2)), \ + $2,$(call first,$2)$1$(call merge,$1,$(call rest,$2))))) + +ifdef __gmsl_have_eval +# ---------------------------------------------------------------------------- +# Function: tr +# Arguments: 1: The list of characters to translate from +# 2: The list of characters to translate to +# 3: The text to translate +# Returns: Returns the text after translating characters +# ---------------------------------------------------------------------------- +tr = $(strip $(__gmsl_tr3)$(call assert_no_dollar,$0,$1$2$3) \ + $(eval __gmsl_t := $3) \ + $(foreach c, \ + $(join $(addsuffix :,$1),$2), \ + $(eval __gmsl_t := \ + $(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)), \ + $(__gmsl_t))))$(__gmsl_t)) + +# Common character classes for use with the tr function. Each of +# these is actually a variable declaration and must be wrapped with +# $() or ${} to be used. + +[A-Z] := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z # +[a-z] := a b c d e f g h i j k l m n o p q r s t u v w x y z # +[0-9] := 0 1 2 3 4 5 6 7 8 9 # +[A-F] := A B C D E F # + +# ---------------------------------------------------------------------------- +# Function: uc +# Arguments: 1: Text to upper case +# Returns: Returns the text in upper case +# ---------------------------------------------------------------------------- +uc = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(call tr,$([a-z]),$([A-Z]),$1) + +# ---------------------------------------------------------------------------- +# Function: lc +# Arguments: 1: Text to lower case +# Returns: Returns the text in lower case +# ---------------------------------------------------------------------------- +lc = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(call tr,$([A-Z]),$([a-z]),$1) + +# ---------------------------------------------------------------------------- +# Function: strlen +# Arguments: 1: A string +# Returns: Returns the length of the string +# ---------------------------------------------------------------------------- +__gmsl_characters := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z +__gmsl_characters += a b c d e f g h i j k l m n o p q r s t u v w x y z +__gmsl_characters += 0 1 2 3 4 5 6 7 8 9 +__gmsl_characters += ` ~ ! @ \# $$ % ^ & * ( ) - _ = + +__gmsl_characters += { } [ ] \ : ; ' " < > , . / ? | + +# Aside: if you read the above you might think that the lower-case +# letter x is missing, and that that's an error. It is missing, but +# it's not an error. __gmsl_characters is used by the strlen +# function. strlen works by transforming every character and space +# into the letter x and then counting the x's. Since there's no need +# to transform x into x I omitted it. + +# This results in __gmsl_space containing just a space + +__gmsl_space := +__gmsl_space += + +strlen = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(strip $(eval __temp := $(subst $(__gmsl_space),x,$1))$(foreach a,$(__gmsl_characters),$(eval __temp := $$(subst $$a,x,$(__temp))))$(eval __temp := $(subst x,x ,$(__temp)))$(words $(__temp))) + +# This results in __gmsl_newline containing just a newline + +define __gmsl_newline + + +endef + +# This results in __gmsl_tab containing a tab + +__gmsl_tab := # + +# ---------------------------------------------------------------------------- +# Function: substr +# Arguments: 1: A string +# 2: Start position (first character is 1) +# 3: End position (inclusive) +# Returns: A substring. +# Note: The string in $1 must not contain a § +# ---------------------------------------------------------------------------- + +substr = $(__gmsl_tr3)$(call assert_no_dollar,$0,$1$2$3)$(strip $(eval __temp := $$(subst $$(__gmsl_space),§ ,$$1))$(foreach a,$(__gmsl_characters),$(eval __temp := $$(subst $$a,$$a$$(__gmsl_space),$(__temp))))$(eval __temp := $(wordlist $2,$3,$(__temp))))$(subst §,$(__gmsl_space),$(subst $(__gmsl_space),,$(__temp))) + +endif # __gmsl_have_eval + +# ########################################################################### +# SET MANIPULATION FUNCTIONS +# ########################################################################### + +# Sets are represented by sorted, deduplicated lists. To create a set +# from a list use set_create, or start with the empty_set and +# set_insert individual elements + +# This is the empty set +empty_set := + +# ---------------------------------------------------------------------------- +# Function: set_create +# Arguments: 1: A list of set elements +# Returns: Returns the newly created set +# ---------------------------------------------------------------------------- +set_create = $(__gmsl_tr1)$(sort $1) + +# ---------------------------------------------------------------------------- +# Function: set_insert +# Arguments: 1: A single element to add to a set +# 2: A set +# Returns: Returns the set with the element added +# ---------------------------------------------------------------------------- +set_insert = $(__gmsl_tr2)$(sort $1 $2) + +# ---------------------------------------------------------------------------- +# Function: set_remove +# Arguments: 1: A single element to remove from a set +# 2: A set +# Returns: Returns the set with the element removed +# ---------------------------------------------------------------------------- +set_remove = $(__gmsl_tr2)$(filter-out $1,$2) + +# ---------------------------------------------------------------------------- +# Function: set_is_member +# Arguments: 1: A single element +# 2: A set +# Returns: Returns $(true) if the element is in the set +# ---------------------------------------------------------------------------- +set_is_member = $(__gmsl_tr2)$(if $(filter $1,$2),$(true),$(false)) + +# ---------------------------------------------------------------------------- +# Function: set_union +# Arguments: 1: A set +# 2: Another set +# Returns: Returns the union of the two sets +# ---------------------------------------------------------------------------- +set_union = $(__gmsl_tr2)$(sort $1 $2) + +# ---------------------------------------------------------------------------- +# Function: set_intersection +# Arguments: 1: A set +# 2: Another set +# Returns: Returns the intersection of the two sets +# ---------------------------------------------------------------------------- +set_intersection = $(__gmsl_tr2)$(filter $1,$2) + +# ---------------------------------------------------------------------------- +# Function: set_is_subset +# Arguments: 1: A set +# 2: Another set +# Returns: Returns $(true) if the first set is a subset of the second +# ---------------------------------------------------------------------------- +set_is_subset = $(__gmsl_tr2)$(call set_equal,$(call set_intersection,$1,$2),$1) + +# ---------------------------------------------------------------------------- +# Function: set_equal +# Arguments: 1: A set +# 2: Another set +# Returns: Returns $(true) if the two sets are identical +# ---------------------------------------------------------------------------- +set_equal = $(__gmsl_tr2)$(call seq,$1,$2) + +# ########################################################################### +# ARITHMETIC LIBRARY +# ########################################################################### + +# Integers a represented by lists with the equivalent number of x's. +# For example the number 4 is x x x x. The maximum integer that the +# library can handle as _input_ is __gmsl_input_int which is defined +# here as 65536 + +__gmsl_sixteen := x x x x x x x x x x x x x x x x +__gmsl_input_int := $(foreach a,$(__gmsl_sixteen), \ + $(foreach b,$(__gmsl_sixteen), \ + $(foreach c,$(__gmsl_sixteen), \ + $(__gmsl_sixteen))))) + +# ---------------------------------------------------------------------------- +# Function: int_decode +# Arguments: 1: A number of x's representation +# Returns: Returns the integer for human consumption that is represented +# by the string of x's +# ---------------------------------------------------------------------------- +int_decode = $(__gmsl_tr1)$(words $1) + +# ---------------------------------------------------------------------------- +# Function: int_encode +# Arguments: 1: A number in human-readable integer form +# Returns: Returns the integer encoded as a string of x's +# ---------------------------------------------------------------------------- +int_encode = $(__gmsl_tr1)$(wordlist 1,$1,$(__gmsl_input_int)) + +# The arithmetic library functions come in two forms: one form of each +# function takes integers as arguments and the other form takes the +# encoded form (x's created by a call to int_encode). For example, +# there are two plus functions: +# +# plus Called with integer arguments and returns an integer +# int_plus Called with encoded arguments and returns an encoded result +# +# plus will be slower than int_plus because its arguments and result +# have to be translated between the x's format and integers. If doing +# a complex calculation use the int_* forms with a single encoding of +# inputs and single decoding of the output. For simple calculations +# the direct forms can be used. + +# Helper function used to wrap an int_* function into a function that +# takes a pair of integers, perhaps a function and returns an integer +# result +__gmsl_int_wrap = $(call int_decode,$(call $1,$(call int_encode,$2),$(call int_encode,$3))) +__gmsl_int_wrap1 = $(call int_decode,$(call $1,$(call int_encode,$2))) +__gmsl_int_wrap2 = $(call $1,$(call int_encode,$2),$(call int_encode,$3)) + +# ---------------------------------------------------------------------------- +# Function: int_plus +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the sum of the two numbers in x's representation +# ---------------------------------------------------------------------------- +int_plus = $(strip $(__gmsl_tr2)$1 $2) + +# ---------------------------------------------------------------------------- +# Function: plus (wrapped version of int_plus) +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the sum of the two integers +# ---------------------------------------------------------------------------- +plus = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_plus,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_subtract +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the difference of the two numbers in x's representation, +# or outputs an error on a numeric underflow +# ---------------------------------------------------------------------------- +int_subtract = $(strip $(__gmsl_tr2)$(if $(call int_gte,$1,$2), \ + $(filter-out xx,$(join $1,$2)), \ + $(call __gmsl_warning,Subtraction underflow))) + +# ---------------------------------------------------------------------------- +# Function: subtract (wrapped version of int_subtract) +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the difference of the two integers, +# or outputs an error on a numeric underflow +# ---------------------------------------------------------------------------- +subtract = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_subtract,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_multiply +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the product of the two numbers in x's representation +# ---------------------------------------------------------------------------- +int_multiply = $(strip $(__gmsl_tr2)$(foreach a,$1,$2)) + +# ---------------------------------------------------------------------------- +# Function: multiply (wrapped version of int_multiply) +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the product of the two integers +# ---------------------------------------------------------------------------- +multiply = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_multiply,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_divide +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the result of integer division of argument 1 divided +# by argument 2 in x's representation +# ---------------------------------------------------------------------------- +int_divide = $(__gmsl_tr2)$(strip $(if $2, \ + $(if $(call int_gte,$1,$2), \ + x $(call int_divide,$(call int_subtract,$1,$2),$2),), \ + $(call __gmsl_error,Division by zero))) + +# ---------------------------------------------------------------------------- +# Function: divide (wrapped version of int_divide) +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the integer division of the first argument by the second +# ---------------------------------------------------------------------------- +divide = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_divide,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_max, int_min +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the maximum or minimum of its arguments in x's +# representation +# ---------------------------------------------------------------------------- +int_max = $(__gmsl_tr2)$(subst xx,x,$(join $1,$2)) +int_min = $(__gmsl_tr2)$(subst xx,x,$(filter xx,$(join $1,$2))) + +# ---------------------------------------------------------------------------- +# Function: max, min +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the maximum or minimum of its integer arguments +# ---------------------------------------------------------------------------- +max = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_max,$1,$2) +min = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_min,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_gt, int_gte, int_lt, int_lte, int_eq, int_ne +# Arguments: Two x's representation numbers to be compared +# Returns: $(true) or $(false) +# +# int_gt First argument greater than second argument +# int_gte First argument greater than or equal to second argument +# int_lt First argument less than second argument +# int_lte First argument less than or equal to second argument +# int_eq First argument is numerically equal to the second argument +# int_ne First argument is not numerically equal to the second argument +# ---------------------------------------------------------------------------- +int_gt = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(filter-out $(words $2), \ + $(words $(call int_max,$1,$2)))) +int_gte = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(call int_gt,$1,$2)$(call int_eq,$1,$2)) +int_lt = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(filter-out $(words $1), \ + $(words $(call int_max,$1,$2)))) +int_lte = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(call int_lt,$1,$2)$(call int_eq,$1,$2)) +int_eq = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(filter $(words $1),$(words $2))) +int_ne = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(filter-out $(words $1),$(words $2))) + +# ---------------------------------------------------------------------------- +# Function: gt, gte, lt, lte, eq, ne +# Arguments: Two integers to be compared +# Returns: $(true) or $(false) +# +# gt First argument greater than second argument +# gte First argument greater than or equal to second argument +# lt First argument less than second argument +# lte First argument less than or equal to second argument +# eq First argument is numerically equal to the second argument +# ne First argument is not numerically equal to the second argument +# ---------------------------------------------------------------------------- +gt = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_gt,$1,$2) +gte = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_gte,$1,$2) +lt = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_lt,$1,$2) +lte = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_lte,$1,$2) +eq = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_eq,$1,$2) +ne = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_ne,$1,$2) + +# increment adds 1 to its argument, decrement subtracts 1. Note that +# decrement does not range check and hence will not underflow, but +# will incorrectly say that 0 - 1 = 0 + +# ---------------------------------------------------------------------------- +# Function: int_inc +# Arguments: 1: A number in x's representation +# Returns: The number incremented by 1 in x's representation +# ---------------------------------------------------------------------------- +int_inc = $(strip $(__gmsl_tr1)$1 x) + +# ---------------------------------------------------------------------------- +# Function: inc +# Arguments: 1: An integer +# Returns: The argument incremented by 1 +# ---------------------------------------------------------------------------- +inc = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_inc,$1) + +# ---------------------------------------------------------------------------- +# Function: int_dec +# Arguments: 1: A number in x's representation +# Returns: The number decremented by 1 in x's representation +# ---------------------------------------------------------------------------- +int_dec = $(__gmsl_tr1)$(strip $(if $(call sne,0,$(words $1)), \ + $(wordlist 2,$(words $1),$1), \ + $(call __gmsl_warning,Decrement underflow))) + +# ---------------------------------------------------------------------------- +# Function: dec +# Arguments: 1: An integer +# Returns: The argument decremented by 1 +# ---------------------------------------------------------------------------- +dec = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_dec,$1) + +# double doubles its argument, and halve halves it + +# ---------------------------------------------------------------------------- +# Function: int_double +# Arguments: 1: A number in x's representation +# Returns: The number doubled (i.e. * 2) and returned in x's representation +# ---------------------------------------------------------------------------- +int_double = $(strip $(__gmsl_tr1)$1 $1) + +# ---------------------------------------------------------------------------- +# Function: double +# Arguments: 1: An integer +# Returns: The integer times 2 +# ---------------------------------------------------------------------------- +double = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_double,$1) + +# ---------------------------------------------------------------------------- +# Function: int_halve +# Arguments: 1: A number in x's representation +# Returns: The number halved (i.e. / 2) and returned in x's representation +# ---------------------------------------------------------------------------- +int_halve = $(__gmsl_tr1)$(strip $(subst xx,x,$(filter-out xy x y, \ + $(join $1,$(foreach a,$1,y x))))) + +# ---------------------------------------------------------------------------- +# Function: halve +# Arguments: 1: An integer +# Returns: The integer divided by 2 +# ---------------------------------------------------------------------------- +halve = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_halve,$1) + +ifdef __gmsl_have_eval +# ########################################################################### +# ASSOCIATIVE ARRAYS +# ########################################################################### + +# ---------------------------------------------------------------------------- +# Function: set +# Arguments: 1: Name of associative array +# 2: The key value to associate +# 3: The value associated with the key +# Returns: None +# ---------------------------------------------------------------------------- +set = $(__gmsl_tr3)$(call assert_no_dollar,$0,$1$2$3)$(eval __gmsl_aa_$1_$2 = $3) + +# ---------------------------------------------------------------------------- +# Function: get +# Arguments: 1: Name of associative array +# 2: The key to retrieve +# Returns: The value stored in the array for that key +# ---------------------------------------------------------------------------- +get = $(strip $(__gmsl_tr2)$(call assert_no_dollar,$0,$1$2)$(if $(filter-out undefined,$(origin __gmsl_aa_$1_$2)), \ + $(__gmsl_aa_$1_$2))) + +# ---------------------------------------------------------------------------- +# Function: keys +# Arguments: 1: Name of associative array +# Returns: Returns a list of all defined keys in the array +# ---------------------------------------------------------------------------- +keys = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(sort $(patsubst __gmsl_aa_$1_%,%, \ + $(filter __gmsl_aa_$1_%,$(.VARIABLES)))) + +# ---------------------------------------------------------------------------- +# Function: defined +# Arguments: 1: Name of associative array +# 2: The key to test +# Returns: Returns true if the key is defined (i.e. not empty) +# ---------------------------------------------------------------------------- +defined = $(__gmsl_tr2)$(call assert_no_dollar,$0,$1$2)$(call sne,$(call get,$1,$2),) + +endif # __gmsl_have_eval + +ifdef __gmsl_have_eval +# ########################################################################### +# NAMED STACKS +# ########################################################################### + +# ---------------------------------------------------------------------------- +# Function: push +# Arguments: 1: Name of stack +# 2: Value to push onto the top of the stack (must not contain +# a space) +# Returns: None +# ---------------------------------------------------------------------------- +push = $(__gmsl_tr2)$(call assert_no_dollar,$0,$1$2)$(eval __gmsl_stack_$1 := $2 $(if $(filter-out undefined,\ + $(origin __gmsl_stack_$1)),$(__gmsl_stack_$1))) + +# ---------------------------------------------------------------------------- +# Function: pop +# Arguments: 1: Name of stack +# Returns: Top element from the stack after removing it +# ---------------------------------------------------------------------------- +pop = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(strip $(if $(filter-out undefined,$(origin __gmsl_stack_$1)), \ + $(call first,$(__gmsl_stack_$1)) \ + $(eval __gmsl_stack_$1 := $(call rest,$(__gmsl_stack_$1))))) + +# ---------------------------------------------------------------------------- +# Function: peek +# Arguments: 1: Name of stack +# Returns: Top element from the stack without removing it +# ---------------------------------------------------------------------------- +peek = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(call first,$(__gmsl_stack_$1)) + +# ---------------------------------------------------------------------------- +# Function: depth +# Arguments: 1: Name of stack +# Returns: Number of items on the stack +# ---------------------------------------------------------------------------- +depth = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(words $(__gmsl_stack_$1)) + +endif # __gmsl_have_eval + +# ########################################################################### +# DEBUGGING FACILITIES +# ########################################################################### + +# ---------------------------------------------------------------------------- +# Target: gmsl-print-% +# Arguments: The % should be replaced by the name of a variable that you +# wish to print out. +# Action: Echos the name of the variable that matches the % and its value. +# For example, 'make gmsl-print-SHELL' will output the value of +# the SHELL variable +# ---------------------------------------------------------------------------- +gmsl-print-%: ; @echo $* = $($*) + +# ---------------------------------------------------------------------------- +# Function: assert +# Arguments: 1: A boolean that must be true or the assertion will fail +# 2: The message to print with the assertion +# Returns: None +# ---------------------------------------------------------------------------- +assert = $(if $1,,$(call __gmsl_error,Assertion failure: $2)) + +# ---------------------------------------------------------------------------- +# Function: assert_exists +# Arguments: 1: Name of file that must exist, if it is missing an assertion +# will be generated +# Returns: None +# ---------------------------------------------------------------------------- +assert_exists = $(call assert,$(wildcard $1),file '$1' missing) + +# ---------------------------------------------------------------------------- +# Function: assert_no_dollar +# Arguments: 1: Name of a function being executd +# 2: Arguments to check +# Returns: None +# ---------------------------------------------------------------------------- +assert_no_dollar = $(call assert,$(call not,$(findstring $$,$2)),$1 called with a dollar sign in argument) diff --git a/pkgs/core/build-essentials/buildsystem/gmsl b/pkgs/core/build-essentials/buildsystem/gmsl new file mode 100644 index 000000000..2ff28972d --- /dev/null +++ b/pkgs/core/build-essentials/buildsystem/gmsl @@ -0,0 +1,89 @@ +# ---------------------------------------------------------------------------- +# +# GNU Make Standard Library (GMSL) +# +# A library of functions to be used with GNU Make's $(call) that +# provides functionality not available in standard GNU Make. +# +# Copyright (c) 2005-2008 John Graham-Cumming +# +# This file is part of GMSL +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# Neither the name of the John Graham-Cumming nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# ---------------------------------------------------------------------------- + +# Determine if the library has already been included and if so don't +# bother including it again + +ifndef __gmsl_included + +# Standard definitions for true and false. true is any non-empty +# string, false is an empty string. These are intended for use with +# $(if). + +true := T +false := + +# ---------------------------------------------------------------------------- +# Function: not +# Arguments: 1: A boolean value +# Returns: Returns the opposite of the arg. (true -> false, false -> true) +# ---------------------------------------------------------------------------- +not = $(if $1,$(false),$(true)) + +# Prevent reinclusion of the library + +__gmsl_included := $(true) + +# Try to determine where this file is located. If the caller did +# include /foo/gmsl then extract the /foo/ so that __gmsl gets +# included transparently + +ifneq ($(MAKEFILE_LIST),) +__gmsl_root := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) + +# If there are any spaces in the path in __gmsl_root then give up + +ifeq (1,$(words $(__gmsl_root))) +__gmsl_root := $(patsubst %gmsl,%,$(__gmsl_root)) +else +__gmsl_root := +endif + +include $(__gmsl_root)__gmsl + +else + +include __gmsl + +endif + +endif # __gmsl_included +