1 % (c) 2009-2024 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(tools_platform, [
6 platform_is_64_bit/0,
7 max_tagged_integer/1,
8 max_tagged_pow2/1,
9 is_tagged_integer/1,
10 host_platform/1,
11 map_host_platform/2,
12 host_processor/1,
13 map_host_processor/2
14 ]).
15
16 :- use_module(module_information).
17
18 :- module_info(group, infrastructure).
19 :- module_info(description, 'Utilities for getting information about the platform/OS, seperated out from tools.pl to avoid cyclic module dependencies.').
20
21 % declare these predicates before imports so that we can use them in compile-time directives
22
23 platform_is_64_bit :-
24 max_tagged_pow2(Exp),
25 Exp > 32.
26
27 % On SICStus, max_tagged_integer equals:
28 % * 268435455 on 32-bit systems, i.e. 1<<28 - 1
29 % * 1152921504606846975 on 64-bit systems, i.e. 1<<60 - 1
30 :- if(current_prolog_flag(max_tagged_integer,1152921504606846975)).
31 max_tagged_integer(1152921504606846975).
32 :- else.
33 max_tagged_integer(X) :- current_prolog_flag(max_tagged_integer,X).
34 :- endif.
35
36 :- if(current_prolog_flag(max_tagged_integer,1152921504606846975)).
37 max_tagged_pow2(60). % pre-computed value, for efficiency
38 :- else.
39 max_tagged_pow2(Exp) :-
40 current_prolog_flag(max_tagged_integer,X),
41 Exp is msb(X) + 1.
42 :- endif.
43
44 is_tagged_integer(X) :-
45 max_tagged_integer(Lim), X =< Lim,
46 current_prolog_flag(min_tagged_integer,Low), X >= Low.
47
48 host_platform(Res) :-
49 current_prolog_flag(host_type,HostType),
50 atom_codes(HostType,AsciiList),
51 map_host_platform(AsciiList,Res),!.
52
53 map_host_platform(HT,Platform) :-
54 % Split at first '-' to remove the architecture prefix
55 append(_,[0'-|HT2],HT),
56 !,
57 (phrase(map_host_platform1(Platform),HT2,_) -> true ; Platform = unknown).
58
59 map_host_platform1(darwin) --> "darwin".
60 map_host_platform1(windows) --> "win".
61 map_host_platform1(linux) --> "linux".
62
63 % on Windows XP: 'x86-win32-nt-4'
64 % on MacPro: 'x86_64-darwin-10.6.0'
65 % on Linux: 'x86-linux-glibc2.7'
66
67 host_processor(Res) :-
68 current_prolog_flag(host_type,HostType),
69 atom_codes(HostType,AsciiList),
70 map_host_processor(AsciiList,Res),!.
71
72 map_host_processor(HT,Processor) :-
73 % Split at first '-' to remove the platform/os sufffix
74 append(HT1,[0'-|_],HT),
75 !,
76 (phrase(map_host_proc1(Processor),HT1,_) -> true ; Processor = unknown).
77
78 map_host_proc1(aarch64) --> "arm64".
79 map_host_proc1(aarch64) --> "aarch64".
80 map_host_proc1(x86_64) --> "x64".
81 map_host_proc1(x86_64) --> "x86_64", !.
82 map_host_proc1(x86) --> "x86".
83 map_host_proc1(x86) --> "i386".
84
85 % Tests for these predicates are placed in tools.pl to avoid module load order issues
86 % (especially on SWI, because pathes.pl must define its library(avl) term expansion before any modules that use library(avl)).