Recent

Author Topic: FileExists macro/preprocessor  (Read 1257 times)

MrDan

  • Newbie
  • Posts: 2
FileExists macro/preprocessor
« on: September 23, 2024, 01:57:09 pm »
Hi, is there a way to check if file exists as part of fpc pre-processor?
something like this:
Code: Pascal  [Select][+][-]
  1. {$if exists(FileName)}
  2.   {$include FileName}
  3. {$endif}
  4.  
if not would there be any problem to add this?

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: FileExists macro/preprocessor
« Reply #1 on: September 23, 2024, 02:15:06 pm »
I'm trying to wrap my mind around the fact, that you want to include a file (or not) at COMPILE time.

From here: https://www.freepascal.org/docs-html/3.2.0/prog/progsu40.html#x47-460001.2.40
Quote
If the file with the given filename exists, it will be included.
Counter-Conclusion: If it doesn't exist, it will not be included
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: FileExists macro/preprocessor
« Reply #2 on: September 23, 2024, 02:21:00 pm »
see also this thread
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: FileExists macro/preprocessor
« Reply #3 on: September 23, 2024, 02:25:19 pm »
see also this thread
Ah... OK, so if the File doesn't exist, an INCLUDE-Directive throws an error?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8022
Re: FileExists macro/preprocessor
« Reply #4 on: September 23, 2024, 02:30:11 pm »
Ah... OK, so if the File doesn't exist, an INCLUDE-Directive throws an error?

Correct, I've raised this before in the context of (interface units to) libraries etc. and was told that not only did such a thing not exist but that it wasn't going to happen.

The choices are either to write your own preprocessor ** or to use runtime library loading (i.e. put the optional code into a DLL/so).

** Which is not as far out as it sounds: preprocessor reads a list of files to check and emits its own .inc file with a $define for each file which was actually present. That file is included into each file of the project, at which point the $include directives can be guarded by an $ifdef.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: FileExists macro/preprocessor
« Reply #5 on: September 23, 2024, 02:40:28 pm »
Ah... OK, so if the File doesn't exist, an INCLUDE-Directive throws an error?
Indeed, as explained by Mark.

To provide a concrete example:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. begin
  4.   {$I example.inc}
  5. end.

will result in a fatal error:
Code: [Select]
fpc -B test.pas
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test.pas
test.pas(4,4) Fatal: Cannot open include file "example.inc"
Fatal: Compilation aborted

which indeed is (or can be) a bit of an annoyance.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: FileExists macro/preprocessor
« Reply #6 on: September 23, 2024, 02:45:58 pm »
Nutshell: https://forum.lazarus.freepascal.org/index.php/topic,50867.msg372248.html#msg372248
With "empty" file for Default (different location)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MrDan

  • Newbie
  • Posts: 2
Re: FileExists macro/preprocessor
« Reply #7 on: September 23, 2024, 03:20:33 pm »
Thank you all for the input.
so in short the functionality doesn't exist and is unlikely to be added.
I do already have a custom preprocessor, but it just felts like this kind of functionality should already exist.

Nutshell: https://forum.lazarus.freepascal.org/index.php/topic,50867.msg372248.html#msg372248
With "empty" file for Default (different location)
this seems to be the most simple workaround. the choice of which file is included would be controlled by the includes path order, which is a bit inconvenient
« Last Edit: September 23, 2024, 04:23:03 pm by MrDan »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8022
Re: FileExists macro/preprocessor
« Reply #8 on: September 23, 2024, 03:34:03 pm »
I do already have a custom preprocessor, but it just felt like this kind of functionality should already exist.

I for one agree. But the nearest we have to it is setting strings either using $define or at the project/Makefile level.

Code: [Select]
# PREREQUISITE: FPC (Free Pascal Compiler), v2.6.0 but preferably v3.0.2 or above.

############

# FPCFLAGS can usefully be transcribed from the Lazarus IDE's "Show Options" output.

FPC=/usr/local/bin/fpc
FPCFLAGS=-O3 -CX -XX -k--build-id
DEFINES=-dHAS_SVN -dHAS_FPC_SVN
INIFILES=../inifiles/trunk
SERIALCOMMS=../serialcomms/trunk
SCPISRV=../scpisrv/trunk
CPU=$(shell uname -m | sed 's/i686/i386/' | sed 's/armv7l/arm/')
OPSYS=$(shell uname -o | sed 's/GNU\/Linux/linux/')
FPCSrcDir=/usr/local/src.fpc/$(shell $(FPC) -iV)/compiler

Above from a project in my repo, note the DEFINES line.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: FileExists macro/preprocessor
« Reply #9 on: September 23, 2024, 03:34:41 pm »
this seems to be the most simple workaround. the choice of which file is included would be controlled by the includes path order, which is a bit inconvenient
Nevermind, that you HAVE to provide an empty file for any files you want to include or not

Kinda like
Default --> ../Default
Real --> ../Include


--> {$INCLUDEPATH ../Include;../Default}
« Last Edit: September 23, 2024, 03:36:37 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8022
Re: FileExists macro/preprocessor
« Reply #10 on: September 23, 2024, 03:51:40 pm »
Nevermind, that you HAVE to provide an empty file for any files you want to include or not

However, that can be a dummy that's created by the project "do this before compiling" or by a Makefile, it doesn't matter what's in it and it can be recreated with impunity.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: FileExists macro/preprocessor
« Reply #11 on: September 23, 2024, 11:17:14 pm »
Hi, is there a way to check if file exists as part of fpc pre-processor?

I'll just leave this here (needs 3.3.1).

MarkMLl

  • Hero Member
  • *****
  • Posts: 8022
Re: FileExists macro/preprocessor
« Reply #12 on: September 24, 2024, 08:36:28 am »
I'll just leave this here (needs 3.3.1).

Ah. Nice one!

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16167
  • Censorship about opinions does not belong here.
Re: FileExists macro/preprocessor
« Reply #13 on: September 24, 2024, 08:47:11 am »
A really nice one, and we all missed that? Usually I am quite up-to-date regarding feature commits, but I missed that one.
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8022
Re: FileExists macro/preprocessor
« Reply #14 on: September 24, 2024, 10:22:12 am »
A really nice one, and we all missed that? Usually I am quite up-to-date regarding feature commits, but I missed that one.

I now need to work out how to wrestle with the new bugtracker to mark the feature request I logged years ago as satisfied.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018