View source with formatted comments or as raw
    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)  2018-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(prolog_config,
   37	  [ prolog_dump_runtime_variables/0,
   38	    apple_bundle_libdir/1
   39	  ]).   40
   41/** <module> Provide configuration information
   42
   43This module provides information about   the configuration to facilitate
   44linking against Prolog, embedding Prolog or calling Prolog.
   45*/
   46
   47:- multifile
   48    prolog:runtime_config/2.   49
   50%!  prolog_dump_runtime_variables
   51%
   52%   Dump the current configuration in shell   format.  This predicate is
   53%   called  when  Prolog  is  started    using  the  commandline  option
   54%   `--dump-runtime-variables`
   55
   56prolog_dump_runtime_variables :-
   57    (   '$cmd_option_val'(config, Format),
   58	Format \== ''
   59    ->  prolog_dump_runtime_variables(Format)
   60    ;   prolog_dump_runtime_variables(sh)
   61    ).
   62
   63prolog_dump_runtime_variables(Format) :-
   64    print_flag(home,                      'PLBASE',     Format),
   65    print_flag(arch,                      'PLARCH',     Format),
   66    print_flag(address_bits,              'PLBITS',     Format),
   67    print_flag(version,                   'PLVERSION',  Format),
   68    print_flag(shared_object_extension,   'PLSOEXT',    Format),
   69    print_flag(shared_object_search_path, 'PLSOPATH',   Format),
   70    print_flag(c_libdir,                  'PLLIBDIR',   Format),
   71    print_flag(c_lib,                     'PLLIB',      Format),
   72    print_flag(libswipl,                  'PLLIBSWIPL', Format),
   73    print_flag(open_shared_object,        'PLSHARED',   Format),
   74    print_flag(threads,                   'PLTHREADS',  Format).
   75
   76print_flag(Flag, Var, Format) :-
   77    (   prolog:runtime_config(Flag, Value)
   78    ->  print_config(Format, Var, Value)
   79    ;   flag_value(Flag, Value)
   80    ->  print_config(Format, Var, Value)
   81    ;   true
   82    ).
   83
   84flag_value(Flag, Value) :-
   85    boolean_flag(Flag),
   86    (   current_prolog_flag(Flag, true)
   87    ->  Value = yes
   88    ;   Value = no
   89    ).
   90flag_value(c_libdir, Value) :-
   91    current_prolog_flag(home, Home),
   92    (   current_prolog_flag(c_libdir, Rel)
   93    ->  atomic_list_concat([Home, Rel], /, Value)
   94    ;   current_prolog_flag(msys2, true)
   95    ->  current_prolog_flag(arch, Arch),
   96        atomic_list_concat([Home, bin, Arch], /, Value)
   97    ;   current_prolog_flag(windows, true)
   98    ->  atomic_list_concat([Home, bin], /, Value)
   99    ;   apple_bundle_libdir(LibDir)
  100    ->  Value = LibDir
  101    ;   current_prolog_flag(arch, Arch)
  102    ->  atomic_list_concat([Home, lib, Arch], /, Value)
  103    ).
  104flag_value(c_lib, '-lswipl').
  105flag_value(Flag, Value) :-
  106    current_prolog_flag(Flag, Value).
  107
  108%!  apple_bundle_libdir(-LibDir)
  109%
  110%   If we are part of a MacOS bundle   the C libraries are in the bundle
  111%   ``Frameworks``  directory  and  the  executable  is  in  the  bundle
  112%   ``MacOS`` directory.
  113
  114apple_bundle_libdir(LibDir) :-
  115    current_prolog_flag(apple, true),
  116    current_prolog_flag(executable, Exe),
  117    file_directory_name(Exe, ExeDir),
  118    file_base_name(ExeDir, 'MacOS'),
  119    file_directory_name(ExeDir, ContentsDir),
  120    file_base_name(ContentsDir, 'Contents'),
  121    atomic_list_concat([ContentsDir, 'Frameworks'], /, LibDir),
  122    exists_directory(LibDir).
  123
  124boolean_flag(threads).
  125boolean_flag(open_shared_object).
  126
  127print_config(sh, Var, Value) :-
  128    format('~w=\"~w\";~n', [Var, Value]).
  129print_config(cmd, Var, Value) :-
  130    (   file_var(Var)
  131    ->  prolog_to_os_filename(Value, OSValue),
  132	format('SET ~w=~w~n', [Var, OSValue])
  133    ;   format('SET ~w=~w~n', [Var, Value])
  134    ).
  135
  136file_var('PLBASE')