# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1599599078 -7200
#      Tue Sep 08 23:04:38 2020 +0200
# Node ID 052c1042c4d8c95432ee586b4b1c87237356cf20
# Parent  1b16d40c6611da074f0298646dfc43e1a8627215
Add build rule for build-octave (bug #49503).

* src/build-octave.mk: Add new build rule for build-octave which can be used for
cross-building binary packages that depend on Octave as a build tool.
* dist-files.mk: Add new file to list.
* index.html: Add new package to list.
* src/of-image.mk, src/of-statistics.mk: Add optional dependency on build-octave.
* configure.ac: Add new configure switch "--disable-system-octave" that is
needed to build the build-octave package.
* Makefile.in: Add variable "USE_SYSTEM_OCTAVE". Exclude build-octave from the
default build tools and build it only if a package explicitly depends on it.
Display warning about missing native Octave version only if necessary.

diff -r 1b16d40c6611 -r 052c1042c4d8 Makefile.in
--- a/Makefile.in	Tue Sep 08 16:04:48 2020 +0200
+++ b/Makefile.in	Tue Sep 08 23:04:38 2020 +0200
@@ -65,6 +65,12 @@
 # if cross, compiling, this also means we will attempt to create cross mkoctfile
 ENABLE_BINARY_PACKAGES := @ENABLE_BINARY_PACKAGES@
 
+# Set to "no" to build a minimal Octave that runs on the build system.
+# That version can be used for cross-building binary packages that depend
+# on Octave as a build tool. If "yes", it is tried to use an Octave that
+# is already installed on the build system (default is "yes").
+USE_SYSTEM_OCTAVE := @USE_SYSTEM_OCTAVE@
+
 # create and install devel tools (default no)
 ENABLE_DEVEL_TOOLS := @ENABLE_DEVEL_TOOLS@
 
@@ -257,6 +263,8 @@
 
 BUILD_COMPILER_TOOLS := build-gcc build-binutils
 BUILD_TOOLS := $(ALL_BUILD_TOOLS)
+# build-octave is added as an explicit dependency in the of- packages that need it
+BUILD_TOOLS := $(filter-out build-octave, $(BUILD_TOOLS))
 # Building flex for native mingw fails, so disable it.
 ifeq ($(MXE_NATIVE_MINGW_BUILD),yes)
   REQUIREMENTS += flex
@@ -307,6 +315,7 @@
 TMP_DIR     = $(TOP_BUILD_DIR)/tmp-$(1)
 MAKEFILE   := $(TOP_BUILD_DIR)/Makefile
 PKGS       := $(filter-out $(ALL_BUILD_TOOLS), $(shell $(SED) -n 's/^.* class="package">\([^<]*\)<.*$$/\1/p' '$(TOP_DIR)/index.html'))
+PKGS += build-octave
 
 ENV_PATH := $(PATH)
 PATH := $(BUILD_TOOLS_PREFIX)/bin:$(PATH)
@@ -723,9 +732,13 @@
 	@echo '[check requirements]'
 	$(foreach REQUIREMENT,$(REQUIREMENTS),$(call CHECK_REQUIREMENT,$(REQUIREMENT)))
 ifneq ($(MXE_NATIVE_BUILD),yes)
+  ifeq ($(USE_SYSTEM_OCTAVE),yes)
+    ifeq ($(ENABLE_BINARY_PACKAGES),yes)
 	@if ! octave -W --version | head -1 | grep ' \($($(OCTAVE_TARGET)_VERSION)\)$$' >/dev/null; then \
-	  echo "Warning - could not find native build version $($(OCTAVE_TARGET)_VERSION) of octave - some packages may fail, but continuing"; \
+	  echo "Warning - could not find native build version $($(OCTAVE_TARGET)_VERSION) of Octave - some packages may fail, but continuing"; \
 	fi
+    endif
+  endif
 endif
 	@[ -d '$(STAMP_DIR)' ] || mkdir -p '$(STAMP_DIR)'
 	@if test "$(USE_SYSTEM_GCC)" = yes; then \
diff -r 1b16d40c6611 -r 052c1042c4d8 configure.ac
--- a/configure.ac	Tue Sep 08 16:04:48 2020 +0200
+++ b/configure.ac	Tue Sep 08 23:04:38 2020 +0200
@@ -326,6 +326,16 @@
    esac], [ENABLE_BINARY_PACKAGES=no])
 AC_SUBST(ENABLE_BINARY_PACKAGES)
 
+AC_ARG_ENABLE([system-octave],
+  [AS_HELP_STRING([--disable-system-octave],
+    [Build a minimal Octave that runs on the build system. This only applies if binary packages are cross-built (default: enable)])],
+  [case "$enableval" in
+     no) USE_SYSTEM_OCTAVE=no ;;
+     yes) USE_SYSTEM_OCTAVE=yes ;;
+     *) AC_MSG_ERROR([bad value "$enableval" for --disable-system-octave]) ;;
+   esac], [USE_SYSTEM_OCTAVE=yes])
+AC_SUBST(USE_SYSTEM_OCTAVE)
+
 AC_ARG_ENABLE([devel-tools],
   [AS_HELP_STRING([--enable-devel-tools],
     [Enable building developer tools such as gdb (default: disable)])],
diff -r 1b16d40c6611 -r 052c1042c4d8 dist-files.mk
--- a/dist-files.mk	Tue Sep 08 16:04:48 2020 +0200
+++ b/dist-files.mk	Tue Sep 08 23:04:38 2020 +0200
@@ -41,6 +41,7 @@
   build-meson.mk \
   build-msvctools.mk \
   build-ninja.mk \
+  build-octave.mk \
   build-perl.mk \
   build-pkg-config-1-fixes.patch \
   build-pkg-config.mk \
diff -r 1b16d40c6611 -r 052c1042c4d8 index.html
--- a/index.html	Tue Sep 08 16:04:48 2020 +0200
+++ b/index.html	Tue Sep 08 23:04:38 2020 +0200
@@ -1050,6 +1050,10 @@
         <td class="website"><a href="https://ninja-build.org/">build-ninja</a></td>
     </tr>
     <tr>
+        <td class="package">build-octave</td>
+        <td class="website">Minimal version of <a href="http://www.octave.org">Octave</a> for cross-compiling some binary packages</td>
+    </tr>
+    <tr>
         <td class="package">build-perl</td>
         <td class="website"><a href="http://www.perl.org">perl</a></td>
     </tr>
diff -r 1b16d40c6611 -r 052c1042c4d8 src/build-octave.mk
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/build-octave.mk	Tue Sep 08 23:04:38 2020 +0200
@@ -0,0 +1,49 @@
+# This file is part of MXE.
+# See index.html for further information.
+
+## This package is intended for building a minimal version of Octave that
+## runs on the build system.  Some Octave Forge packages need Octave as a
+## build tool.  Whereever possible, packages should try not to depend on
+## Octave as a build tool.
+## It is assumed that the necessary dependencies for a native build are
+## present on the build system.
+
+PKG             := build-octave
+$(PKG)_IGNORE   :=
+$(PKG)_VERSION  := $($(OCTAVE_TARGET)_VERSION)
+$(PKG)_CHECKSUM := $($(OCTAVE_TARGET)_CHECKSUM)
+$(PKG)_SUBDIR   := $($(OCTAVE_TARGET)_SUBDIR)
+$(PKG)_FILE     := $($(OCTAVE_TARGET)_FILE)
+$(PKG)_URL      := $($(OCTAVE_TARGET)_URL)
+$(PKG)_DEPS     :=
+# $(PKG)_DEPS     := blas arpack curl epstool ghostscript gl2ps gnuplot lapack pcre pstoedit qhull qrupdate
+
+define $(PKG)_UPDATE
+    echo 'Warning: Updates are disabled for package build-octave.' >&2;
+    echo $($(PKG)_VERSION)
+endef
+
+$(PKG)_ENV_FLAGS := \
+    PKG_CONFIG_PATH='$(BUILD_PKG_CONFIG_PATH)' \
+    LD_LIBRARY_PATH='$(LD_LIBRARY_PATH)' \
+    PATH='$(ENV_PATH)'
+
+define $(PKG)_BUILD
+    mkdir '$(1)/.build'
+    cd '$(1)/.build' && \
+        $($(PKG)_ENV_FLAGS) '$(1)/configure' --prefix=$(ROOT_PREFIX) \
+        --disable-docs --disable-fftw-threads --disable-java --disable-jit \
+        --disable-openmp --disable-readline --without-amd --without-arpack \
+        --without-bz2 --without-camd --without-ccolamd --without-cholmod \
+        --without-colamd --without-curl --without-cxsparse --without-fftw3 \
+        --without-fftw3f --without-fltk --without-fontconfig \
+        --without-framework-opengl --without-freetype --without-glpk \
+        --without-hdf5 --without-klu --without-magick --without-opengl \
+        --without-openssl --without-osmesa --without-portaudio \
+        --without-qhull --without-qrupdate --without-qscintilla \
+        --without-qt --without-sndfile --without-sundials_ida \
+        --without-sundials_nvecserial --without-umfpack --without-x \
+        --without-z
+
+    $($(PKG)_ENV_FLAGS) $(MAKE) all -C '$(1)/.build' -j '$(JOBS)' install
+endef
diff -r 1b16d40c6611 -r 052c1042c4d8 src/of-image.mk
--- a/src/of-image.mk	Tue Sep 08 16:04:48 2020 +0200
+++ b/src/of-image.mk	Tue Sep 08 23:04:38 2020 +0200
@@ -9,10 +9,15 @@
 $(PKG)_SUBDIR   := image-$($(PKG)_VERSION)
 $(PKG)_FILE     := image-$($(PKG)_VERSION).tar.gz
 $(PKG)_URL      := '$(OCTAVE_FORGE_BASE_URL)/$($(PKG)_FILE)/download'
-$(PKG)_DEPS     := 
+$(PKG)_DEPS     :=
 
 ifeq ($(ENABLE_BINARY_PACKAGES),yes)
     $(PKG)_DEPS += $(OCTAVE_TARGET)
+    ifeq ($(MXE_NATIVE_BUILD),no)
+        ifeq ($(USE_SYSTEM_OCTAVE),no)
+            $(PKG)_DEPS += build-octave
+        endif
+    endif
 endif
 
 define $(PKG)_UPDATE
diff -r 1b16d40c6611 -r 052c1042c4d8 src/of-statistics.mk
--- a/src/of-statistics.mk	Tue Sep 08 16:04:48 2020 +0200
+++ b/src/of-statistics.mk	Tue Sep 08 23:04:38 2020 +0200
@@ -13,6 +13,11 @@
 
 ifeq ($(ENABLE_BINARY_PACKAGES),yes)
     $(PKG)_DEPS += $(OCTAVE_TARGET)
+    ifeq ($(MXE_NATIVE_BUILD),no)
+        ifeq ($(USE_SYSTEM_OCTAVE),no)
+            $(PKG)_DEPS += build-octave
+        endif
+    endif
 endif
 
 define $(PKG)_UPDATE
