Recent

Author Topic: Advise on how to create a package for both Lazarus and Delphi?  (Read 5583 times)

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Hi, someone has a better idea on how to make a package both for delphi and lazarus?

This will be a private package, so I will not make it public.

I need to use some libs with lazarus, and some other with delphi.

What I planned to do, is to have a common component, that calls code from another object, that object is separated in two files, both having the same functionality.

Say:
- component (TComponent descendant, to place in form, non visual)
-- uses 'connection_lazarus'
-- uses 'connection_delphi'

depending of course of the platform used.

I already found
Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2. // fpc unit
  3. {$ELSE}
  4. // delphi unit
  5. {$ENDIF}

I know that packages are handled in a different way, but I'm new to Delphi. Never made a Delphi package.

What I know, is that in delphi the 'Register' method must be into a separate file. So we have design and runtime separated. These must be 2 different packages, or just 2 different files?

And any sample 'cross product' packages to see and study are welcome.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #1 on: April 19, 2018, 01:42:20 am »
Hi, someone has a better idea on how to make a package both for delphi and lazarus?

If I understand you correctly you can't do that. You have to have two different packages the format of the package file is diffrent the options for the compiler etc are different you can only auto convert from to the other. You can have the same units for both though or different based on your needs/choices.

I need to use some libs with lazarus, and some other with delphi.
since there is no way to use a single package for both just add different units to each package.

What I planned to do, is to have a common component, that calls code from another object, that object is separated in two files, both having the same functionality.

Say:
- component (TComponent descendant, to place in form, non visual)
-- uses 'connection_lazarus'
-- uses 'connection_delphi'

depending of course of the platform used.

I already found
Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2. // fpc unit
  3. {$ELSE}
  4. // delphi unit
  5. {$ENDIF}
that's all there is to it as long as you are talking about the uses of your units and your packages.

I know that packages are handled in a different way, but I'm new to Delphi. Never made a Delphi package.
no not really in delphi packages are dlls registered to load at start up and thats where the differences end.

What I know, is that in delphi the 'Register' method must be into a separate file.
Not to my knowledge eg http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Declaring_the_Register_Procedure it has a couple of conditions not normally part of the language but the register procedure can be in any unit you want I can't remember if you could have multiple of them I do remember that I made sure that only a single procedure exists per package but I don't recall being a requirement I remember it mostly as my choice.
So we have design and runtime separated. These must be 2 different packages, or just 2 different files?
depends what you are talking about design and runtime packages. In delphi a design time package is a package that has in its requirements the IDE's interfaces and defines/creates its own editors to extend the IDE. They are usually the only ones loaded at runtime and those link to the runtime packages.

It is a good practise to keep those two groups seperate because design time code is usually not shared with your application so you have smaller applications and cleaner, resources like component palette icons etc that are only used by the IDE while designing an application do not become part of the final product.


And any sample 'cross product' packages to see and study are welcome.
sorry nothing of the top of my head. the ports are usually one way from delphi to lazarus they usually use the same units but that means its polluted with {$ifdef fpc} etc that makes working with them a bit to strainous s I keep them seperate since delphi is the mature environment I usually do not need to change much in it so when a new feature is added is first added in delphi and ported to lazarus after debugging or lately only added in C# and not at all in delphi nor laazarus.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #2 on: April 19, 2018, 01:52:43 am »
Thanks! Really awesome answer. I'm now a bit more conscious of what I need to do.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #3 on: April 19, 2018, 03:53:03 am »
if you need to share some common code that isn't GUI stuff, you can do all of that in a DLL, does not matter which RAD you
use, Delphi or Lazarus
 
 I suppose you could even share some visual items as long as you use the direct windows API method in the DLL which means
writing forms the old way..

The only true wisdom is knowing you know nothing

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #4 on: April 19, 2018, 04:48:30 am »
Thanks! Really awesome answer. I'm now a bit more conscious of what I need to do.

Back in the day, I wrote some design components for ExtPascal that had both .dpk and .lpk packages. You can see them here under ExtP_Toolkit:

https://github.com/farshadmohajeri/extpascal

Both extp_ctrls and extp_grids have both Delphi and Lazarus packages.

Thaddy

  • Hero Member
  • *****
  • Posts: 14163
  • Probably until I exterminate Putin.
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #5 on: April 19, 2018, 06:56:52 am »
I usually do only:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
for non-visual packages and make sure the same units compile in both Delphi and Freepascal. That is most of the time easy.
I have some code as {$mode delphiunicode} IOW try to avoid unit duplication because of dialect and don't ever use {$mode objfpc} if you need to share code between the two flavors. That's asking for trouble. I only have separate dpk and lpk projects because of differing format but the sourcecode is the same and does usually not contain any other $ifdef than the above except for platform defines.
« Last Edit: April 19, 2018, 07:04:51 am by Thaddy »
Specialize a type, not a var.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #6 on: April 19, 2018, 01:28:38 pm »
if you need to share some common code that isn't GUI stuff, you can do all of that in a DLL, does not matter which RAD you
use, Delphi or Lazarus
 
 I suppose you could even share some visual items as long as you use the direct windows API method in the DLL which means
writing forms the old way..



And what happens with strings? If I compile with FPC, I must use delphi unicode mode?

Also is possible to instantiate objects, and return a reference to it? (Having all methods, properties, etc..)

Thanks! Really awesome answer. I'm now a bit more conscious of what I need to do.

Back in the day, I wrote some design components for ExtPascal that had both .dpk and .lpk packages. You can see them here under ExtP_Toolkit:

https://github.com/farshadmohajeri/extpascal

Both extp_ctrls and extp_grids have both Delphi and Lazarus packages.


Thanks. I will look at it.

I usually do only:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
for non-visual packages and make sure the same units compile in both Delphi and Freepascal. That is most of the time easy.
I have some code as {$mode delphiunicode} IOW try to avoid unit duplication because of dialect and don't ever use {$mode objfpc} if you need to share code between the two flavors. That's asking for trouble. I only have separate dpk and lpk projects because of differing format but the sourcecode is the same and does usually not contain any other $ifdef than the above except for platform defines.

Thankyou, I will include that for sure.
« Last Edit: April 19, 2018, 01:56:25 pm by lainz »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #7 on: April 19, 2018, 02:05:47 pm »
if you need to share some common code that isn't GUI stuff, you can do all of that in a DLL, does not matter which RAD you
use, Delphi or Lazarus
 
 I suppose you could even share some visual items as long as you use the direct windows API method in the DLL which means
writing forms the old way..



And what happens with strings? If I compile with FPC, I must use delphi unicode?
why? do you do any processing? Or you simple pass it along to the underline api? If you just use the underline APIs then don't worry about it string will do just fine.
Also is possible to instantiate objects, and return a reference to it? (Having all methods, properties, etc..)

Oh from the dll? having the dll written in lazarus/fpc and use it from delphi or vice versa! No. You have to remember that when you pass objects between dlls and exe with out dynamic packages, you only pass a pointer on the other side and then you attach to that pointer a known vmt which is then used to translate calls to addresses, since the vmts between delphi and fpc are incompatible (the lower you inherit in the hierarcy the better your chances for compatibility) you will have a hell of a time to make this work, and even if you did manage to make it work you would have to take in to account the differences in strings and other data types as well mostly to make sure that the conversions are done properly between object and APIs.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14163
  • Probably until I exterminate Putin.
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #8 on: April 19, 2018, 02:12:29 pm »
If you just use the underline APIs then don't worry about it string will do just fine.
NOT! Which string type? Silly.... >:D >:D >:( >:( The string type can break your code very easy when it is just "string"
{quote]
Also is possible to instantiate objects, and return a reference to it? (Having all methods, properties, etc..)
Yes that is possible, but difficult.
Quote
Oh from the dll? having the dll written in lazarus/fpc and use it from delphi or vice versa! No. You have to remember that when you pass objects between dlls and exe with out dynamic packages, you only pass a pointer on the other side and then you attach to that pointer a known vmt which is then used to translate calls to addresses, since the vmts between delphi and fpc are incompatible (the lower you inherit in the hierarcy the better your chances for compatibility) you will have a hell of a time to make this work, and even if you did manage to make it work you would have to take in to account the differences in strings and other data types as well mostly to make sure that the conversions are done properly between object and APIs.
Don't do that, indeed... DLL's have relocatable code. Shoot yourself in the foot.
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #9 on: April 19, 2018, 02:24:32 pm »
If you just use the underline APIs then don't worry about it string will do just fine.
NOT! Which string type? Silly.... >:D >:D >:( >:( The string type can break your code very easy when it is just "string"

any string type, when used in lazarus will use the correct encoding for lazarus and when used in delphi it will use the correct encoding for delphi. There is a slight problem when strings are exchanged between lazarus and delphi but that has nothing to do with string type in the components.
{quote]
Also is possible to instantiate objects, and return a reference to it? (Having all methods, properties, etc..)
Yes that is possible, but difficult.
Quote
Oh from the dll? having the dll written in lazarus/fpc and use it from delphi or vice versa! No. You have to remember that when you pass objects between dlls and exe with out dynamic packages, you only pass a pointer on the other side and then you attach to that pointer a known vmt which is then used to translate calls to addresses, since the vmts between delphi and fpc are incompatible (the lower you inherit in the hierarcy the better your chances for compatibility) you will have a hell of a time to make this work, and even if you did manage to make it work you would have to take in to account the differences in strings and other data types as well mostly to make sure that the conversions are done properly between object and APIs.
Don't do that, indeed... DLL's have relocatable code. Shoot yourself in the foot.
well some times it is required for a pleasurable time, but yeah I agree avoid shooting your self in the foot if possible.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

balazsszekely

  • Guest
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #10 on: April 19, 2018, 02:51:29 pm »
What delphi version do you wish to support(pre D2009 - ansistring, post D2009 - UTF16)? What platform are you targeting with the delphi version(pre XE2 - windows only, post XE2 - OSX, IOS, etc)? I understand that the package is private, but at least can you tell us the category(graphics, networking, GUI, etc...). The answer highly depends on the above questions.
I would create a separate package for each supported versions: D7, D2007, D2009,...DXE8, Lazarus then a common directory for the sharable sources, resources, etc... and separate directories for the specific units.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #11 on: April 19, 2018, 03:16:17 pm »
Hi, I will try to avoid the dll stuff.

@GetMem, the package is to connect to a new generation of fiscal printer of my country (Argentina), and it will communicate with JSON, I will use fphttpclient in Lazarus and Indy for Delphi, as was assigned by my boos. We're using only Delphi 10.

And yes, I already have 2 units (with no code yet), but I wish to share the component (say TFiscalPrinter) between Delphi and Lazarus. The specific things for Delphi and FPC are in 2 separate units (communication with fphttpcliend and Indy, say TJSONCommunication). But with same object name, I only add an ifdef to load the correct file.

balazsszekely

  • Guest
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #12 on: April 19, 2018, 05:09:10 pm »
@lainz
Quote
The package is to connect to a new generation of fiscal printer of my country (Argentina), and it will communicate with JSON, I will use fphttpclient in Lazarus and Indy for Delphi, as was assigned by my boos. We're using only Delphi 10.
And yes, I already have 2 units (with no code yet), but I wish to share the component (say TFiscalPrinter) between Delphi and Lazarus. The specific things for Delphi and FPC are in 2 separate units (communication with fphttpcliend and Indy, say TJSONCommunication). But with same object name, I only add an ifdef to load the correct file.

Then this is relatively simple to handle. For downloading the JSON, use Indy both for Lazarus and Delphi(it works fine, believe me I tested this for years). For string type use unicodestring(same thing in both cases). In some special cases you will need to do a typecast in lazarus, but the typecast will also work in delphi. The only problem is the parsing of the json, which must be ifdef-ed. I would say you can have almost the same code both for Delphi and the Lazarus package(90%).

Thaddy

  • Hero Member
  • *****
  • Posts: 14163
  • Probably until I exterminate Putin.
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #13 on: April 19, 2018, 05:20:40 pm »
any string type, when used in lazarus will use the correct encoding for lazarus and when used in delphi it will use the correct encoding for delphi. There is a slight problem when strings are exchanged between lazarus and delphi but that has nothing to do with string type in the components.
You obviously didn't convert too many Delphi projects. The string type (UTF8) in Lazarus is a hazard and a trap. Same goes for older Delphi (ansi) to newer Delphi (unicode16). Be warned and don't fell asleep.
Although it is adverised as running smoothly - and most of the time it does as advertised - ANY routine that does string manipulation needs to be scrutinised.
Specialize a type, not a var.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Advise on how to create a package for both Lazarus and Delphi?
« Reply #14 on: April 19, 2018, 08:03:39 pm »
Thanks @GetMem.

I can't choose the library because is not my decision.

But I will try your approach for any future component I do for my personal projects.

 

TinyPortal © 2005-2018