* * *

Author Topic: Problems using CodeTools [SOLVED]  (Read 596 times)

carl_caulkett

  • Full Member
  • ***
  • Posts: 243
Problems using CodeTools [SOLVED]
« on: July 17, 2017, 05:37:49 pm »
Lazarus 1.8.0 RC3
macOS 10.12.5

I've started to experiment with the CodeTools facilities but I am having problems whereby the CodeToolBoss.SimpleInit(ConfigFilename); is failing to work properly. It returns:
Code: Pascal  [Select]
  1. TCodeToolManager.SimpleInit Config=codetools.config
  2. TCodeToolManager.SimpleInit PP=/usr/local/bin/fpc FPCDIR=/Users/carlca/freepascal/fpc LAZARUSDIR=/Users/carlca/pascal/lazarus FPCTARGET=
  3. Scanning FPC sources may take a while ...
  4. Warning: [CreateFPCSrcTemplate] FPCSrcDir does not exist: FPCSrcDir="/Users/carlca/freepascal/fpc/"
  5. Exception: loading failed preferencesunit.pas
  6.  

The code itself is this:
Code: Pascal  [Select]
  1. program methodgroups;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils, CodeToolManager, CodeCache, DefineTemplates, FileProcs, LazUtf8;
  7.  
  8. const
  9.   ConfigFilename = 'codetools.config';
  10.  
  11. var
  12.   Code: TCodeBuffer;
  13.   FileName: string;
  14.   CodeTool: TCodeTool;
  15.  
  16. begin
  17.   if (ParamCount <> 1) then
  18.     begin
  19.       writeln('Usage:');
  20.       writeln('  ', ParamStr(0));
  21.       writeln('  ', ParamStr(0),' <filename>');
  22.     end;
  23.   FileName := ParamStr(1);
  24.   try
  25.     CodeToolBoss.SimpleInit(ConfigFilename);
  26.     Code := CodeToolBoss.LoadFile(FileName, False, False);
  27.     if Code = nil then
  28.       raise Exception.Create('loading failed ' + FileName);
  29.     writeln(GetEnvironmentVariableUtf8('FPCSrcDir'));
  30.     CodeToolBoss.Explore(Code, CodeTool, True, True);
  31.   except
  32.      on E: Exception do
  33.        writeln('Exception: ' + E.Message);
  34.   end;
  35. end.
  36.  

From debugging, it looks as if it's not finding anything in the FPCDIR environment variable and is supplying its own default of ~/freepascal/fpc, which in my case is not there. I'm a little puzzled as I'm sure I installed the FPC source as one of the three install downloads, but I'm not sure which folder on my system is regarded as the FPCDIR. Can anyone help here?

Thanks,
Carl
« Last Edit: July 19, 2017, 01:06:11 am by carl_caulkett »
macOS 10.12.6
Lazarus 1.8.0 RC4
FPC 3.0.2

Thaddy

  • Hero Member
  • *****
  • Posts: 4412
Re: Confusion over location of FPCSrcDir
« Reply #1 on: July 17, 2017, 06:19:48 pm »
Usually there is a fpc.cfg leftover. ( /etc/fpc.cfg) either adapt or delete it.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

carl_caulkett

  • Full Member
  • ***
  • Posts: 243
Problems using CodeTools [SOLVED]
« Reply #2 on: July 17, 2017, 09:04:03 pm »
I renamed the /etc/fpc.cfg to /etc/fpc.cfg.OLD, whereupon, my Lazarus refused to build anything. I promptly renamed /etc/fpc.cfg.OLD back to /etc/fpc.cfg again. Now I can build again.
« Last Edit: July 19, 2017, 01:06:36 am by carl_caulkett »
macOS 10.12.6
Lazarus 1.8.0 RC4
FPC 3.0.2

Thaddy

  • Hero Member
  • *****
  • Posts: 4412
Re: Confusion over location of FPCSrcDir
« Reply #3 on: July 17, 2017, 09:22:56 pm »
I renamed the /etc/fpc.cfg to /etc/fpc.cfg.OLD, whereupon, my Lazarus refused to build anything. I promptly renamed /etc/fpc.cfg.OLD back to /etc/fpc.cfg again. Now I can build again.

If there is a conflict, usually that's the one that caused it. Technically it *should* also be the correct one because of linux search order.
Problem is that it won't get removed by the package managers when purging a previous install. Which in turn leads to your kind of problem after a re-install using a different directory.
« Last Edit: July 17, 2017, 09:27:03 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Cyrax

  • Hero Member
  • *****
  • Posts: 535
Re: Confusion over location of FPCSrcDir
« Reply #4 on: July 18, 2017, 09:41:25 am »
You need to generate local fpc.cfg config file by using fpcmkcfg utility. I suggest generating the config file in the same place where the fpc binary resides. Then rename the fpc binary e.g to fpc_old and use following bash script snippet (name it to fpc) to pass the configuration file to renamed compiler executable.

Quote
#!/bin/bash
SCRIPT_FILENAME="$(readlink -f "${BASH_SOURCE[0]}")"
SCRIPT_DIR0="$(dirname "${SCRIPT_FILENAME}")"
SCRIPT_DIR="$(cd "${SCRIPT_DIR0}" && pwd)"

"${SCRIPT_DIR}/fpc_old" -n @"${SCRIPT_DIR}/fpc.cfg" "$@"


I use following bash script snippet to generate fpc.cfg file and make my installation of FPC fully portable. Whenever you move FPC to different directory, run this and you get fully working FPC distribution in seconds.

Quote
#!/bin/bash

function do_setup {
   
local BIN_TARGET=bin/i386-linux/

local SCRIPT_FILENAME="$(readlink -f "${BASH_SOURCE[0]}")"
local SCRIPT_DIR0="$(dirname "${SCRIPT_FILENAME}")"
local SCRIPT_DIR="$(cd "${SCRIPT_DIR0}" && pwd)"

"${SCRIPT_DIR}/${BIN_TARGET}fpcmkcfg" -o "${SCRIPT_DIR}/${BIN_TARGET}fpc.cfg" -d basepath="${SCRIPT_DIR}" -d sharepath="${SCRIPT_DIR}"

echo "#IFDEF LINUX" >> "${SCRIPT_DIR}/${BIN_TARGET}fpc.cfg"
echo "  #IFDEF CPU32" >> "${SCRIPT_DIR}/${BIN_TARGET}fpc.cfg"
echo "    -FCi686-w64-mingw32-windres" >> "${SCRIPT_DIR}/${BIN_TARGET}fpc.cfg"
echo "  #ENDIF" >> "${SCRIPT_DIR}/${BIN_TARGET}fpc.cfg"
echo "#ENDIF" >> "${SCRIPT_DIR}/${BIN_TARGET}fpc.cfg"

}

do_setup

These snippets may need adaptations for Mac OS UNIX environment.

carl_caulkett

  • Full Member
  • ***
  • Posts: 243
Re: Problems using CodeTools [SOLVED]
« Reply #5 on: July 18, 2017, 11:58:52 pm »

Getting back to the real issue here. From debugging CodeTools I can see that CodeToolBoss.SimpleInit(ConfigFilename); ends up calling  TCodeToolsOptions.InitWithEnvironmentVariables;

This method attempts to read the following environment variable: PP, FPCDIR, LAZARUSDIR, FPCTARGET and FPCTARGETCPU.

If it doesn't find them, it then sets default values, but unfortunately these defaults bare no resemblance to my actual folder structure.

For example, the variable FPCSrcDir which is supposed to be the value of FPCDIR gets initialised to "~/freepascal/fpc".

I experimented with adding the line export FPCDIR=/usr/local/share/fpcsrc to my .bash_profile but I've had problems debugging this because any attempt to use a GUI for debugging (Xcode in this case) means that GetEnvironmentVariableUtf8 doesn't work. GetEnvironmentVariableUtf8 does work ok if I just use a console app - I just can't debug it the usual way.

So basically I need values for PP, FPCTARGET and FPCTARGETCPU. Can anyone help?
« Last Edit: July 19, 2017, 01:06:54 am by carl_caulkett »
macOS 10.12.6
Lazarus 1.8.0 RC4
FPC 3.0.2

carl_caulkett

  • Full Member
  • ***
  • Posts: 243
Re: Problems using CodeTools [SOLVED]
« Reply #6 on: July 19, 2017, 01:34:44 am »
Lazarus 1.8.0 RC3
macOS 10.12.5

I think I've found out all of the environment variables I need. On a default set of installations on the Apple Mac, they are:
Code: Pascal  [Select]
  1. PP=/usr/local/bin/fpc
  2. FPCDIR=/usr/local/share/fpcsrc
  3. LAZARUSDIR=/Developer/lazarus
  4. FPCTARGET=i386-darwin
  5. FPCTARGETCPU=i386
  6.  

If anyone is doing CodeTools stuff on an Apple Mac under Sierra and wants to debug then use the guide on this page to set up Xcode.
https://macpgmr.github.io/ObjP/ProjectXC.html
There are a few things to remember. Try to put your code that you want debugged in a class in a .pas file and call that class from a console app. Xcode debugging doesn't seem to work directly in .lpr files.

You'll need to add the environment variables to Xcode. Select Product -> Scheme -> Edit Scheme and add the environment variables here along with any runtime parameters, such as a .pas file parameter. See the attached image as a guide.

Have a look at the end of this thread for some hopefully solved issues about project name case. https://forum.lazarus.freepascal.org/index.php/topic,37588.0.html

Using this setup I have managed to debug in Xcode to the end of this method without raising any exceptions. I just need to figure out how the CodeToolBoss.Explore(Code, CodeTool, True, True); stuff works next. But that can wait until the morning!

Code: Pascal  [Select]
  1. procedure TcaMethodGroup.RunMethodGroup;
  2. var
  3.   Code: TCodeBuffer;
  4.   FileName: string;
  5.   CodeTool: TCodeTool;
  6. begin
  7.   FileName := ParamStr(1);
  8.   try
  9.     CodeToolBoss.SimpleInit(ConfigFilename);
  10.     writeln('passed simple init');
  11.     Code := CodeToolBoss.LoadFile(FileName, False, False);
  12.     if Code = nil then
  13.       raise Exception.Create('loading failed ' + FileName);
  14.     writeln('passed Code nil check');
  15.     writeln('FPCDIR=' + GetEnvironmentVariableUtf8('FPCDIR'));
  16.     writeln('FPCTARGET=' + GetEnvironmentVariableUtf8('FPCTARGET'));
  17.     writeln('passed env variables');
  18.     CodeToolBoss.Explore(Code, CodeTool, True, True);
  19.   except
  20.     on E: Exception do
  21.       writeln('Exception: ' + E.Message);
  22.   end;
  23. end;
  24.  

I'm quite tired I may have missed some important details out. Just ask if necessary.

Cheers,
Carl
macOS 10.12.6
Lazarus 1.8.0 RC4
FPC 3.0.2

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus