1########################################################################
2##
3## Copyright (C) 2016-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn {} {} build (@var{builddir}, @var{tarballs}, @var{verbose})
28## Prepare binary packages from Octave source packages.
29##
30## Boils down to (for each in @var{tarballs}):
31##
32## @enumerate
33## @item untar the tarball in @var{builddir};
34##
35## @item build anything necessary (configure and make);
36##
37## @item repackage specifying the build arch in the tarball filename.
38## @end enumerate
39##
40## @end deftypefn
41
42function build (builddir, tarballs, verbose)
43
44  if (nargin != 3)
45    print_usage ();
46  endif
47
48  if (! isfolder (builddir))
49    warning ("creating build directory %s", builddir);
50    [status, msg] = mkdir (builddir);
51    if (status != 1)
52      error ("could not create installation directory: %s", msg);
53    endif
54  endif
55
56  for i = 1:numel (tarballs)
57    filelist = unpack (tarballs{i}, builddir);
58
59    ## We want the path for the package root but we can't assume that
60    ## exists in the filelist (see patch #9030).  So we deduce it from
61    ## the path of the DESCRIPTION file (smallest in case there's another
62    ## file named DESCRIPTION somewhere).
63    desc_pos = regexp (filelist, "DESCRIPTION$");
64    desc_mask = ! cellfun ("isempty", desc_pos);
65    [~, desc_r_idx] = min ([desc_pos{desc_mask}]);
66    desc_path = fullfile (builddir, filelist(desc_mask){desc_r_idx});
67    build_root = desc_path(1:end-12); # do not include the last filesep
68
69    desc = get_description (desc_path);
70
71    ## If there is no configure or Makefile within src/, there is nothing
72    ## to do to prepare a "binary" package.  We only repackage to add more
73    ## info to the tarball filename (version and arch).
74    if (! exist (fullfile (build_root, "src", "configure"), "file")
75        && ! exist (fullfile (build_root, "src", "Makefile"), "file"))
76      arch_abi = "any-none";
77    else
78      arch_abi = getarch ();
79      configure_make (desc, build_root, verbose);
80      unlink (fullfile (build_root, "src", "configure"));
81      unlink (fullfile (build_root, "src", "Makefile"));
82    endif
83    tar_name = [desc.name "-" desc.version "-" arch_abi ".tar"];
84    tar_path = fullfile (builddir, tar_name);
85
86    ## Figure out the directory name of the build.  Note that fileparts
87    ## gets confused with the version string (the periods makes it think
88    ## it's a file extension).
89    [~, package_root, package_ext] = fileparts (build_root);
90    package_root = [package_root, package_ext];
91
92    tar (tar_path, package_root, builddir);
93    gzip (tar_path, builddir);
94    rmdir (build_root, "s");
95
96    ## Currently does nothing because gzip() removes the original tar
97    ## file but that should change in the future (bug #43431).
98    unlink (tar_path);
99  endfor
100
101endfunction
102