Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Pascal Language Support #388

Closed
kassane opened this issue Apr 10, 2019 · 29 comments
Closed

Suggestion: Pascal Language Support #388

kassane opened this issue Apr 10, 2019 · 29 comments

Comments

@kassane
Copy link

kassane commented Apr 10, 2019

It would be interesting to add in this project support for the pascal language adhering to compatibility with FreePascal and/or Delphi.

@waruqi
Copy link
Member

waruqi commented Apr 10, 2019

As long as it is a native binary compatible compiled language with c/c++, I will consider support it in xmake, but currently xmake is mainly focused on c/c++.

So I can't guarantee which version and time point to support it.

@waruqi waruqi added this to the todo milestone Apr 10, 2019
@waruqi
Copy link
Member

waruqi commented Oct 12, 2020

If more than 5 people request this feature, I will consider supporting it.

@tazdij
Copy link

tazdij commented May 22, 2021

I would like to voice my request for this feature. Especially considering the simplicity and flexibility of the fpc compilation process. While freepascal is nice and simple to compile for, all of the other tasks related to but not directly part of the building of the binary.

It would be nice to have the ability to conditionally select shared library file to copy into the bin folder, and other similar tasks. Anyway, I just found xmake in a recent project, and think it is really nice, and would like to be able to use it for many of my freepascal projects :)

@waruqi
Copy link
Member

waruqi commented May 22, 2021

ok, I will consider it in version 2.5.6

@waruqi waruqi modified the milestones: todo, v2.5.6 May 22, 2021
@waruqi waruqi modified the milestones: v2.5.6, v2.5.7 Jul 26, 2021
@waruqi waruqi modified the milestones: v2.5.7, v2.5.8 Aug 29, 2021
@waruqi
Copy link
Member

waruqi commented Sep 1, 2021

@waruqi
Copy link
Member

waruqi commented Sep 1, 2021

Maybe I need some example projects and makefiles to start working, such as hello world and static/shared program for free pascal.

@kassane
Copy link
Author

kassane commented Sep 1, 2021

Cross-platform tutorial/tips:
https://wiki.freepascal.org/Cross_compiling

@kassane
Copy link
Author

kassane commented Sep 1, 2021

Shared libraries:

library SimpleLib;

function MySucc(AVal : Int64) : Int64; stdcall;
begin
  Result := System.Succ(AVal);
end;

function MyPred(AVal : Int64) : Int64; stdcall;
begin
  Result := System.Pred(AVal);
end;

exports
  MySucc,
  MyPred;

end.

Command:

fpc -Sd SimpleLib.pas

Source: freepascal-wiki


Static libraries:

Wiki.FreePascal:
MacOSX libraries
FPC libraries

@waruqi
Copy link
Member

waruqi commented Sep 2, 2021

Thanks, but this can only compile a single pas file and execute the link directly.

How do I just compile and generate object files, and then call the linker to link them. like this

fpc -c -ofoo.o foo.pas
fpc -c -obar.o bar.pas
link foo.o bar.o ...

@waruqi
Copy link
Member

waruqi commented Sep 2, 2021

done, examples: https://github.com/xmake-io/xmake/tree/dev/tests/projects/pascal

add_rules("mode.debug", "mode.release")
target("test")
    set_kind("binary")
    add_files("src/*.pas")

we need install fpc compiler.

$ xmake -rv
[ 33%]: linking.release test
/usr/local/bin/fpc -O3 -Sd -Xs -k-lz -obuild/macosx/x86_64/release/test src/main.pas
[100%]: build ok!
$ xmake r
Hello, world.

@kassane
Copy link
Author

kassane commented Sep 2, 2021

Nice. I will try to test soon.

For static lib flag add:

-Xt (-static lib)

More Info

@kassane
Copy link
Author

kassane commented Sep 2, 2021

Fpc library to C app (ffi)

Doc info

@waruqi
Copy link
Member

waruqi commented Sep 2, 2021

Nice. I will try to test soon.

For static lib flag add:

-Xt (-static lib)

More Info

done

@waruqi
Copy link
Member

waruqi commented Sep 2, 2021

Fpc library to C app (ffi)

Doc info

? Does this require xmake to do something extra?

@kassane
Copy link
Author

kassane commented Sep 2, 2021

Fpc library to C app (ffi)

Doc info

? Does this require xmake to do something extra?

Before closing, it would be to have an ffi tests.

Examples [c++, rust, freepascal]:

Lib
App

@waruqi
Copy link
Member

waruqi commented Sep 2, 2021

Fpc library to C app (ffi)
Doc info

? Does this require xmake to do something extra?

Before closing, it would be to have an ffi tests.

Examples [c++, rust, freepascal]:

Lib
App

you can test it.

@kassane
Copy link
Author

kassane commented Sep 2, 2021

Ok. I wiil test now.
But first problem: Missing pascal template in shared and static libs.

@waruqi
Copy link
Member

waruqi commented Sep 2, 2021

Ok. I wiil test now.
But first problem: Missing pascal template in shared and static libs.

Because I am not familiar with pascal, there is no example of dynamic library and static library similar to hello world.

@kassane
Copy link
Author

kassane commented Sep 2, 2021

Works

C library:

#include <stdint.h>

extern int64_t fib(int64_t n)
{
    return n > 1 ? fib(n - 1) + fib(n - 2) : 1;
}

Pascal app:

program hello;

function fib(n: Int64): Int64;
  cdecl; external 'bar';

var
  Value: Integer;
begin
  Value := 5;
  WriteLn(fib(Value));
end.

XMake build:

add_rules("mode.debug", "mode.release")

target("bar")
     set_kind("static") -- works
     -- set_kind("shared") -- works
     add_files("src/bar.c")

target("hello")
    set_kind("binary")
    add_deps("bar")
    add_files("src/hello.pas")

Fail

Pascal library [shared / static]:

library libbar;

{$mode objfpc}{$H+}

function fib(n : Int64) : Int64; cdecl;
begin
  if n > 1 then
  begin
      Result := fib(n - 1) + fib(n - 2);
  end
  else
        Result := 1;
end;

exports
{$IFDEF DARWIN}  {OS X entry points}
 fib name '_fib',
{$ENDIF}
 fib;
end.

XMake build:

add_rules("mode.debug", "mode.release")

target("bar")
     set_kind("static") -- don't works
     -- set_kind("shared") -- don't works
     add_files("src/bar.pas")

target("hello")
    set_kind("binary")
    add_deps("bar")
    add_files("src/hello.pas")

hello-app w/ static-lib output:

# build
[ 33%]: linking.release libbar.a
/usr/bin/fpc -O3 -Xt -obuild/linux/x86_64/release/libbar.a src/bar.pas
[ 66%]: linking.release hello
/usr/bin/fpc -O3 -Sd -k-Lbuild/linux/x86_64/release -Xs -k-lbar -obuild/linux/x86_64/release/hello src/hello.pas
[100%]: build ok!
# run
/build/linux/x86_64/release/hello: error while loading shared libraries: libbar.a: cannot open shared object file: No such file or directory

hello-app w/ shared-lib output:

# build
[ 33%]: linking.release libbar.so
/usr/bin/fpc -O3 -Sd -Xs -obuild/linux/x86_64/release/libbar.so src/bar.pas
[ 66%]: linking.release hello
checking for flags (-k-rpath=@loader_path) ... ok
/usr/bin/fpc -O3 -Sd -k-Lbuild/linux/x86_64/release -k-rpath=$ORIGIN -Xs -k-lbar -obuild/linux/x86_64/release/hello src/hello.pas
[100%]: build ok!
➜  hello ./build/linux/x86_64/release/hello
# run
./build/linux/x86_64/release/hello: symbol lookup error: ./build/linux/x86_64/release/hello: undefined symbol: calloc, version GLIBC_2.2.5
``

@kassane
Copy link
Author

kassane commented Sep 2, 2021

New test - Works

Pascal - Shared lib

Pascal app:

program Hello;

uses sysutils, dynlibs;

procedure UseLib;
type
  TMyFunc=function (n:Integer):Integer; stdcall;
var
  MyLib: TLibHandle = dynlibs.NilHandle;
  MyFunc: TMyFunc;
  FuncResult: integer;
begin
  MyLib := LoadLibrary('libbar.' + SharedSuffix);
  if MyLib = dynlibs.NilHandle then Exit;  //Lib was not loaded successfully
  MyFunc:= TMyFunc(GetProcedureAddress(MyLib, 'fib'));
  FuncResult:= MyFunc (5);  //Executes the function
  if MyLib <>  DynLibs.NilHandle then if FreeLibrary(MyLib) then MyLib:= DynLibs.NilHandle;  //Unload the lib, if already loaded
  writeln('Result fibonacci (Lib): '+InttoStr(FuncResult));
end;

procedure Main;
begin
	UseLib;
end;

begin
  Main;
end.

Pascal lib:

library bar;

{$mode delphi} // delphi mode: fpc -Sd
// {$mode objfpc}{$H+} // fp mode: fpc -S2

function fib(n : Int64) : Int64; stdcall;
begin
  if n > 1 then
  begin
      Result := fib(n - 1) + fib(n - 2);
  end
  else
        Result := 1;
end;

exports
{$IFDEF DARWIN}  {OS X entry points}
 fib name '_fib',
{$ENDIF}
 fib;
end.

XMake build

add_rules("mode.debug", "mode.release")

target("bar")
     -- set_kind("static") -- don't works
     set_kind("shared") -- works
     add_files("src/bar.pas")

target("hello")
    set_kind("binary")
    add_deps("bar")
    add_files("src/hello.pas")

@kassane
Copy link
Author

kassane commented Sep 2, 2021

Ref.: Modern Pascal - Introduction

@waruqi
Copy link
Member

waruqi commented Sep 3, 2021

/build/linux/x86_64/release/hello: error while loading shared libraries: libbar.a: cannot open shared object file: No such file or directory

-Xt (-static lib) cannot generate static library. libbar.a is still dynamic library.

  -Xt        Link with static libraries (-static is passed to linker)

@waruqi
Copy link
Member

waruqi commented Sep 3, 2021

./build/linux/x86_64/release/hello: symbol lookup error: ./build/linux/x86_64/release/hello: undefined symbol: calloc, version GLIBC_2.2.5

I fixed it, and added shared project tempalte.

@tazdij
Copy link

tazdij commented Sep 3, 2021

Thank you, I will also run some tests with my projects, this weekend. I will try to convert a couple of them to utilize xmake, instead of FPCs built in build system.

I have been away for about 3 weeks, recovering from a nasty virus. Otherwise I would have jumped on this issue :)

@kassane
Copy link
Author

kassane commented Sep 3, 2021

I would like you to add more extensions referring to pascal like:

*.ppu, *.lpr, *.inc

Unit files: fpc -Fu

Add flag -fPIC or -Cg to shared library and XMake function to chance pascal syntax mode:

e.g.: pascal_mode()

Delphi mode: fpc -Sd or fpc -Mdelphi or fpc -Mdelphiunicode
ObjPascal: fpc -S2 or fpc -Mobjfpc
Pascal: fpc -Mfpc

@waruqi
Copy link
Member

waruqi commented Sep 4, 2021

I would like you to add more extensions referring to pascal like:

*.ppu, *.lpr, *.inc

Unit files: fpc -Fu

Add flag -fPIC or -Cg to shared library and XMake function to chance pascal syntax mode:

done

e.g.: pascal_mode()

Delphi mode: fpc -Sd or fpc -Mdelphi or fpc -Mdelphiunicode
ObjPascal: fpc -S2 or fpc -Mobjfpc
Pascal: fpc -Mfpc

you can use add_pcflags()

@waruqi
Copy link
Member

waruqi commented Sep 4, 2021

I removed the static library example, because I tried some methods and it does not support generating static library well. In addition, I saw that the official fpc did not provide support for it.

https://gitlab.com/freepascal.org/fpc/documentation/-/issues/22272

@kassane
Copy link
Author

kassane commented Sep 4, 2021

I removed the static library example, because I tried some methods and it does not support generating static library well. In addition, I saw that the official fpc did not provide support for it.

https://gitlab.com/freepascal.org/fpc/documentation/-/issues/22272

Wow! I understand.

I think it's unfortunate that you don't have static-lib support.
Then I can close this issue. Any more problems, another issue can be made for correction.

Thanks again @waruqi! Good work, XMake +1 support.

@kassane kassane closed this as completed Sep 4, 2021
@tazdij
Copy link

tazdij commented Sep 9, 2021

I want to leave a comment here, expressing my gratitude. I can now use xmake for over 75% of my projects, and it is so much better than make and cmake. Thank you bunches 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants