File: make.pl

package info (click to toggle)
swi-prolog 9.2.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 84,456 kB
  • sloc: ansic: 401,705; perl: 374,799; lisp: 9,080; cpp: 8,920; java: 5,525; sh: 3,282; javascript: 2,690; python: 2,655; ruby: 1,594; yacc: 845; makefile: 440; xml: 317; sed: 12; sql: 6
file content (168 lines) | stat: -rw-r--r-- 6,002 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        jan@swi-prolog.org
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2021, SWI-Prolog Solutions b.v.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. 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.

    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.
*/

:- module(build_make,
          []).
:- use_module(tools).

:- autoload(library(apply), [maplist/3]).
:- autoload(library(error), [existence_error/2]).
:- autoload(library(filesex), [directory_file_path/3]).
:- autoload(library(lists), [max_list/2]).
:- use_module(library(debug), [debug/3]).

/** <module> Make plugin to deal with build steps.

This build plugin deals with GNU  style   packages.  It  knows about the
following programs:

  - automake to create Makefile.in from Makefile.am
  - autoheader to create `config.h.in` from `configure.in`
  - autoconf to create `configure` from `configure.in`
  - `configure` to create `Makefile`
  - `make` for the make steps.
*/

:- multifile
    prolog:build_file/2,
    prolog:build_step/4.                % Step, Tool, +State0, -State

prolog:build_file('configure',    configure).
prolog:build_file('configure.in', autoconf).
prolog:build_file('configure.ac', autoconf).
prolog:build_file('Makefile',     make).
prolog:build_file('makefile',     make).
prolog:build_file('Makefile.am',  automake).

prolog:build_step(configure, configure, State0, State) :-
    ensure_build_dir(., State0, State),
    automake(State),
    findall(Opt, configure_option(Opt), Opts),
    debug(build(configure), 'Configuring using configure', []),
    run_process(path(bash), [configure|Opts],
                [ env(State.env),
                  directory(State.bin_dir)
                ]).
prolog:build_step(configure, autoconf, State0, State) :-
    ensure_build_dir(., State0, State),
    automake(State),
    ProcessOptions = [directory(State.bin_dir), env(State.env)],
    findall(Opt, configure_option(Opt), ConfigOpts),
    debug(build(configure), 'Running autoheader', []),
    run_process(path(autoheader), [], ProcessOptions),
    debug(build(configure), 'Running autoconf', []),
    run_process(path(autoconf),   [], ProcessOptions),
    debug(build(configure), 'Configuring using configure', []),
    run_process(path(bash),       [configure|ConfigOpts], ProcessOptions).
prolog:build_step(build, make, State0, State) :-
    ensure_build_dir(., State0, State),
    debug(build(build), 'Running make', []),
    run_make(State, []).
prolog:build_step(install, make, State0, State) :-
    ensure_build_dir(., State0, State),
    debug(build(build), 'Running make', []),
    run_make(State, [install]).
prolog:build_step(test, make, State0, State) :-
    ensure_build_dir(., State0, State),
    debug(build(test), 'Running make check', []),
    run_make(State, [check]).
prolog:build_step(clean, make, State0, State) :-
    ensure_build_dir(., State0, State),
    debug(build(clean), 'Running make clean', []),
    run_make(State, [clean]).
prolog:build_step(distclean, make, State0, State) :-
    ensure_build_dir(., State0, State),
    debug(build(distclean), 'Running make distclean', []),
    run_make(State, [distclean]).

automake(State) :-
    needs_build(State.src_dir/'Makefile.in', [State.src_dir/'Makefile.am']),
    !,
    debug(build(configure), 'Running automake', []),
    run_process(path(automake), [],
                [ env(State.env),
                  directory(State.src_dir)
                ]).
automake(_).

run_make(State, Argv) :-
    make_program(State.env, Prog),
    run_process(Prog, Argv,
                [ directory(State.bin_dir),
                  env(State.env)
                ]).

make_program(BuildEnv, Prog) :-
    (   memberchk('MAKE'=Name, BuildEnv)
    ->  true
    ;   getenv('MAKE', Name)
    ),
    has_program(Name, Prog, BuildEnv),
    !.
make_program(BuildEnv, Prog) :-
    make_candidate(Name),
    has_program(Name, Prog, BuildEnv),
    !.
make_program(_, _) :-
    existence_error(program, make).

make_candidate(gmake).
make_candidate(make).

configure_option(Opt) :-
    prolog_install_prefix(Prefix),
    format(atom(Opt), '--prefix=~w', [Prefix]).

needs_build(Target, Sources) :-
    Error = error(existence_error(file, _), _),
    maplist(to_file, Sources, SourceFiles),
    catch(maplist(time_file, SourceFiles, SourceTimes),
          Error,
          fail),
    max_list(SourceTimes, SrcTime),
    maplist(to_file, Target, TargetFile),
    (   catch(to_file(TargetFile, TargetTime),
              Error, fail)
    ->  SrcTime > TargetTime
    ;   true
    ).

to_file(File, Path), atom(File) =>
    Path = File.
to_file(File, Path), string(File) =>
    atom_string(Path, File).
to_file(Dir/File, Path) =>
    directory_file_path(Dir, File, Path).