Compiler directive to distinguish between ubuntu and windows


#1

Hello,

I am looking for compiler directive to distinguish between running on ubunutu or windows, but haven’t found it yet.

I have some log file path information that is different on either platform and want to condition compile each one of them.

Any pointers would be much appreciated.

thank you,

Dan

p.s. is there a unified approach for paths that works across all platforms ?


#2

I hope I understand your question.

There is the current_prolog_flag/2 predicate, see here. Towards the top you see an example of writing Windows-specific code. The relevant keys are I guess windows, apple, unix?


#3

Perhaps you can use the following statement. Hope can help,
Regarde

find_OS(windows):-
current_prolog_flag(windows, true).
find_OS(unix):-
current_prolog_flag(unix, true).

consult_according_2_platform:-
find_OS(OpSystem),
load_code_according_to_file_system(OpSystem).


#4

And since recently emscripten (for web assembly) and android :slight_smile:


#5

Thank you.

Yes, current_prolog_flag should work for me …

Dan


#6

for now i just did something like the following: which returns the right log_file when requested. Seems to work.

log_file(‘c:/file.text’) :-
current_prolog_flag(windows, true).

log_file(’/home/user/file.text’) :-
current_prolog_flag(windows, true).


#7

If you write it this way, you won’t leave a choice point:


#8

I can guess what you wrote but I don’t see it. I don’t know if you accidentally something or if the new forum swallowed your markdown.


#9

This is what I wrote (and it seems to have been swallowed by using email):

log_file(Path) :- % This code hasn't been tested
(  current_prolog_flag(unix, true)
-> absolute_file_name('~/file.text', Path)
;  current_prolog_flag(windows, true)
-> Path = 'c:/file.text'
;  current_prolog_flag(arch, Arch),
   throw(error(unknown_os(arch=Arch), _))
).

(You can also prevent choice points by using cuts, of course)


#10

Hi Peter,

It seems to work well for me.

For each OS there is only one choice available. the predicate must be called with an unbound variable though.

I added a third clause with a default file name without path info, assuming that it would create the log file in any default folder where prolog is running – so at least the system won’t fail – hopefully.

Dan


#11

Your code works, but it leaves a choice point on Unix (it doesn’t on Windows):

log_file('/home/user/file.text') :-
    current_prolog_flag(unix, true).
log_file('c:/file.text') :-
    current_prolog_flag(windows, true).

You can see this when you run it (on Unix):

$ swipl --no-tty -q
1 ?- [log_file].
true.

2 ?- log_file(LogFile).
LogFile = '/home/user/file.text' ;
false.

(If there were no choice point, you wouldn’t see the false.)

You can avoid the choice point by either using the if-then-else style or by adding cuts as follows (the cut in the 2nd clause isn’t necessary, but makes it easier to add a 3rd clause, for example with a default or to throw an error):

log_file('/home/user/file.text') :-
    current_prolog_flag(unix, true),
    !.
log_file('c:/file.text') :-
    current_prolog_flag(windows, true),
    !.

#12

(Also, I would suggest using ~ instead of /home/user on Unix, to make it easier for other people to use your code … absolute_file_name/2 and friends can also be useful.)


#13

thank you.

in the PDT prolog console it seems to complete with true. However, adding the cuts is a good idea – since, there should be no choice points.

thank you,

Dan

p.s. thank you for pointing me to the the absolute_file_name and related predicates – i will try to use them –


#14

Good advice by Peter. Also look for expand_file_name/2 to expand ~ and $VAR and, on Windows, win_folder/2 that allows you to find the well known places on Windows.