Recent

Author Topic: [SOLVED] Ignore a missing include file  (Read 669 times)

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
[SOLVED] Ignore a missing include file
« on: August 03, 2020, 02:38:52 am »
I am writing a simple command line program that is capable of generating code in a file to be included in the next recompile, eg the output file is 'testcode.pp', and is included by this method:

procedure TestGeneratedCode;
begin
  {$I testcode.pp}
end;

How do I override the fatal message 'Fatal: Cannot open include file ”arg1”' if the file does not exist?
If the file does not exist, the procedure will actually do nothing, which is what I want.

The preferred solution is to issue a $NOTE directive to display a note during compile if the file does not exist.
« Last Edit: August 03, 2020, 05:45:34 am by Bazzao »
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Blaazen

  • Hero Member
  • *****
  • Posts: 2966
  • POKE 54296,15
    • Eye-Candy Controls
Re: Ignore a missing include file
« Reply #1 on: August 03, 2020, 02:51:19 am »
I don't know such directive. Can't you just create an empty file testcode.pp?

BTW, it is better to use *.inc extension.

EDIT: List of directives: https://www.freepascal.org/docs-html/prog/progch1.html
Lazarus 2.1.0 r64074 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Ignore a missing include file
« Reply #2 on: August 03, 2020, 02:51:45 am »
Interesting question.

To my knowledge, and unfortunately, it is not possible to realise as you described.

However, in case you are writing that include file in the same code as where the file has to be included then you should be able to solve it.

In your code you know when you write the include file or not, and in case your code takes the path of not generating the include file, then generate the include file with only one line that reads: "{$NOTE blah}" or any other directive that suits your needs.

edit: alternatively, in case you wish to place the message within your main code, you can add a $define to your include file that you can then test for inside your main code, and emit the message/warning.
« Last Edit: August 03, 2020, 03:03:45 am by TRon »

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Ignore a missing include file
« Reply #3 on: August 03, 2020, 03:04:23 am »
BTW, it is better to use *.inc extension.


Yeah, I hurriedly put together the topic, and found the default extension for an include is .pp, but I have used .inc in the past.
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Ignore a missing include file
« Reply #4 on: August 03, 2020, 03:36:24 am »
Interesting question.

To my knowledge, and unfortunately, it is not possible to realise as you described.

However, in case you are writing that include file in the same code as where the file has to be included then you should be able to solve it.

In your code you know when you write the include file or not, and in case your code takes the path of not generating the include file, then generate the include file with only one line that reads: "{$NOTE blah}" or any other directive that suits your needs.

edit: alternatively, in case you wish to place the message within your main code, you can add a $define to your include file that you can then test for inside your main code, and emit the message/warning.

Thanks TRon.

The generated file is created in a non-system temporary directory that older files get deleted, so at some stage the file would disappear.

I did consider a conditional directive, but prefered somthing like (and this is a suggestion):
{$IFEXISTS filename}
  {$I filename}
{$ELSE}
  {$NOTE filename not found therefore not included.}
{$ENDIFEXISTS}

Another thought was to generate the code and place it in the clipboard, but it is a non-windows application.

It looks like I will have to generate the code within the directory containing the application source code.

Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Ignore a missing include file
« Reply #5 on: August 03, 2020, 03:50:34 am »
Going to write some thoughts, that may or may not be relevant for your current situation.

Firstly, it is possible to add other paths that the compiler searches uses for searching include files.

That can be done by either adding a commandline option during compilation or make a change to your fpc.cfg file (not clear whether you are using Lazarus for compilation or not).

But another alternative to do that is by using: https://www.freepascal.org/docs-html/prog/progsu97.html#x105-1060001.3.13

Secondly, and this might not apply at all for your situation, files can be checked for at runtime. So, you would be able to skip the call to the routine where the file is to be included.

Thirdly you could perhaps make use of environment variables to 'toggle' the state of presence of the include file and check for that at runtime.

edit:
In case the include file, when present, always contains for instance a particular declaration you could check for that at compile time with "if declared"/"if not declared"
« Last Edit: August 03, 2020, 03:54:22 am by TRon »

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Ignore a missing include file
« Reply #6 on: August 03, 2020, 03:58:49 am »
Going to write some thoughts, that may or may not be relevant for your current situation.

Firstly, it is possible to add other paths that the compiler searches uses for searching include files.

That can be done by either adding a commandline option during compilation or make a change to your fpc.cfg file (not clear whether you are using Lazarus for compilation or not).

But another alternative to do that is by using: https://www.freepascal.org/docs-html/prog/progsu97.html#x105-1060001.3.13

Secondly, and this might not apply at all for your situation, files can be checked for at runtime. So, you would be able to skip the call to the routine where the file is to be included.

Thirdly you could perhaps make use of environment variables to 'toggle' the state of presence of the include file and check for that at runtime.

Great,

The fpc option is not desirable, however the multi path option in $INCLUDE is a workable solution. I can write the file to the temp directory, but have, in the source directory, a fixed version of the file that includes a $NOTE or $WARN message.

I shall work on that rightaway.

Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Ignore a missing include file
« Reply #7 on: August 03, 2020, 04:14:23 am »
I can write the file to the temp directory, but have, in the source directory, a fixed version of the file that includes a $NOTE or $WARN message.
That might pose a problem.

Reading the documentation in $I directive (https://www.freepascal.org/docs-html/prog/progsu40.html#x47-460001.2.40)
Quote
The compiler will look for the file to include in the following locations:
It will look in the path specified in the include file name.
It will look in the directory where the current source file is.
it will look in all directories specified in the include file search path.
Directories can be added to the include file search path with the -Fi command line option.
I have no idea what takes precedence over what when using includepath and if the exposed behaviour is something that is workable for what you want to accomplish.
« Last Edit: August 03, 2020, 04:16:04 am by TRon »

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Ignore a missing include file
« Reply #8 on: August 03, 2020, 05:01:37 am »
That might pose a problem.

Reading the documentation in $I directive (https://www.freepascal.org/docs-html/prog/progsu40.html#x47-460001.2.40)
I have no idea what takes precedence over what when using includepath and if the exposed behaviour is something that is workable for what you want to accomplish.

TRon, and future referencererererers, here is a working result:
* The $I file reference must not contain an absolute path.
* A file with the desired name must NOT be in the source code directory.
* The path (relative or absolute) where the working version of the file is housed is iinuded in the $INCLUDEPATH directive. This path MUST be first.
* The path (relative or absolute) where a default version of the file is housed is ALSO included in the $INCLUDEPATH directive. Use semicolon to separate paths.

When compiling use compile not run.
* If the working file is found, that is used. A $NOTE directive can be added inside the file.
* If the working file is missing, the default file is found, and since that contains a $NOTE directive, a note is issued in the messages area.

Very close to the desired FILEEXISTS directive that does not exist, but is requested.
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Ignore a missing include file
« Reply #9 on: August 03, 2020, 05:15:29 am »
Thank you for the extensive feedback and posting your results Bazzao.

Good to see that you were able to get something out of it that is workable for your situation, albeit perhaps not ideal.

You could always consider requesting such a feature as you initially wanted to use. However, when it comes to extending the pre-processor with functionality such requests are mostly refused because of unwanted complications or the requested feature had more to do with making the pre-processor act more like a complete macro-language.

No idea what implications such feature you wanted to use would entail or what further implications that would have, so you might be lucky. Unless you can live happily with the current solution of course  :)

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
[SOLVED] Re: Ignore a missing include file
« Reply #10 on: August 03, 2020, 05:44:10 am »
Thank you for the extensive feedback and posting your results Bazzao.

Good to see that you were able to get something out of it that is workable for your situation, albeit perhaps not ideal.

You could always consider requesting such a feature as you initially wanted to use. However, when it comes to extending the pre-processor with functionality such requests are mostly refused because of unwanted complications or the requested feature had more to do with making the pre-processor act more like a complete macro-language.

No idea what implications such feature you wanted to use would entail or what further implications that would have, so you might be lucky. Unless you can live happily with the current solution of course  :)

Thanks for your assistance TRon. It's just me and my weird programming. You are probably correct about an "improvement" request just helping to confuse everyone should the request be fulfilled.

The workaround solution will do, after all it does work.

A tip is I made a directory called "Keep" in the Lazarus source code directory, and the default file goes there, so I can remember to not delete the file.
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

MarkMLl

  • Hero Member
  • *****
  • Posts: 1374
Re: [SOLVED] Ignore a missing include file
« Reply #11 on: August 06, 2020, 04:32:05 pm »
I raised a feature request relating to this on Mantis perhaps three years ago: there is no $ifexists predicate and is not likely to be one. There is similarly no predicate for libraries, library header files, optional binaries etc., all of which can be extremely useful if the compiler is being used as a backend to something application-specific.

I'd suggest that if compiling using a Makefile or via a .lpi (i.e. lazarus or lazbuild) there could be a pre-compilation check that the .inc file existed with an empty one created iff necessary. That would keep FPC's $I happy, and a genuine (correctly-filled) .inc could end with a $define that controlled everything subsequent.

Another approach which might be worth trying, although this would be specific to a .lpi file being processed by a unix implementation (or at least by a unix-style shell) would be to put a backtick command in the "Custom options" box, i.e. something like `if [ -e ../socketnames.inc ] ; then echo '-d HasSocketnames' ; fi` which would then allow $ifdef HasSocketnames to be used in the source.

MarkMLl
« Last Edit: August 06, 2020, 05:42:35 pm by MarkMLl »
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

 

TinyPortal © 2005-2018