1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2019, VU University Amsterdam 7 CWI, Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(gpp, []). 37:- use_module(library(error)). 38 39/** <module> Use XSB gpp preprocessor 40 41This library enables using the XSB gpp preprocessor on .P files. 42Currently the `gpp` program is not provided with SWI-Prolog. If you need 43this compatibility get it from XSB. The XSB sources provide `gpp.c`, a 44simple stand alone C file that can be compiled with any C compiler. The 45`gpp` program must be installed in a directory accessible through the 46``PATH`` environment variable. The preprocessor is enabled using 47 48 :- use_module(library(dialect/xsb/gpp)). 49 50Running code through gpp can be useful for several reasons: 51 52 - Share (numeric) constants with C code. Note that SWI-Prolog's 53 [ffi](https://www.swi-prolog.org/pack/list?p=ffi) pack provides 54 an alternative for this. 55 - Provide a source translation mechanism that appeals more to e.g., 56 C programmers and can deal with partial Prolog expressions and/or 57 syntax that is illegal to some Prolog implementation. 58 - Load legacy XSB programs. 59 60There are also good reasons not to use a preprocessor: 61 62 - gpp doesn't propagate source locations, so error messages typically 63 report the wrong location. 64 - If ``#include file`` is used it will report the wrong file for 65 errors. In addition, make/0 will not be able to pick up that an 66 included file has changed and thus the main file needs to be 67 reloaded. 68 - Wrong source locations also imply tools such as edit/1 or the 69 graphical debugger no longer work. 70 - Advanced tooling such as PceEmacs cannot correctly highlight code 71 that uses the preprocessor. 72*/ 73 74:- multifile 75 prolog:open_source_hook/3, 76 prolog:gpp_options/1. 77:- dynamic 78 prolog:gpp_options/1. 79 80%! prolog:open_source_hook(+Path, -Stream, +Options) 81% 82% Implementation of the open source hook to use the XSB gpp 83% preprocessor on .P files. This requires `gpp` on the PATH 84% environment variable. 85 86prologopen_source_hook(Path, Stream, _Options) :- 87 file_name_extension(_, 'P', Path), 88 gpp_command(Path, Command), 89 open(pipe(Command), read, Stream), 90 set_stream(Stream, file_name(Path)). 91 92gpp_command(Path, Command) :- 93 gpp_executable(Gpp), 94 quoted_os_path(Gpp, OSGpp), 95 quoted_os_path(Path, OSPath), 96 gpp_option_string(Options), 97 format(string(Command), '~w ~w ~w', [OSGpp, Options, OSPath]). 98 99quoted_os_path(PrologPath, QOSPath) :- 100 prolog_to_os_filename(PrologPath, OSPath), 101 ( \+ sub_atom(OSPath, _, _, _, '"'), 102 \+ sub_atom(OSPath, _, _, _, '$') 103 -> format(string(QOSPath), '"~w"', [OSPath]) 104 ; \+ sub_atom(OSPath, _, _, _, '\'') 105 -> format(string(QOSPath), '\'~w\'', [OSPath]) 106 ; representation_error(path) 107 ). 108 109gpp_executable(Gpp) :- 110 exe_options(ExeOptions), 111 absolute_file_name(path(gpp), Gpp, ExeOptions). 112 113exe_options(Options) :- 114 current_prolog_flag(windows, true), 115 !, 116 Options = [ extensions(['',exe,com]), access(read) ]. 117exe_options(Options) :- 118 Options = [ access(execute) ]. 119 120gpp_option_string(String) :- 121 prolog:gpp_options(String), 122 !. 123gpp_option_string(String) :- 124 findall(Opt, gpp_option(Opt), Opts), 125 atomics_to_string(Opts, " ", String). 126 127gpp_option('-P'). 128gpp_option('-m'). 129gpp_option('-nostdinc'). 130%gpp_option('-nocurinc'). 131gpp_option('-DSWI_PROLOG'). 132 133%! prolog:gpp_options(-Options) 134% 135% This multifile dynamic hook can be used to specify options for the 136% gpp preprocessor. The default options are 137% 138% -P -m -nostdinc -DSWI_PROLOG