fortran-lang / fpm

Fortran Package Manager (fpm)
https://fpm.fortran-lang.org
MIT License
876 stars 99 forks source link

Split --link-flag #604

Open rouson opened 2 years ago

rouson commented 2 years ago

Description

I believe that fpm's current --link-flag argument needs to be split into two arguments named something like --link-flag (same as before) and --link-lib, where a concatenated list of any --link-lib arguments would be placed at the end of the resulting link line. The motivating use case is explained below.

I'm working on a shell-script installer for a new package. The installer first installs the prerequisite GASNet-EX, which in turn defines several GASNET_* environment variables for use in building and linking against GASNet-EX. After installing GASNet-EX on macOS, a fpm command of the following form works to build the new package and link it against GASNet-EX:

fpm build  \
  --c-compile "$GASNET_CC" \
  --c-flag "$GASNET_CFLAGS $GASNET_CPPFLAGS" \
  --link-flag "$GASNET_LDFLAG $GASNET_LIBS"

where GASNET_LIBS contains the -L and -l flags for linking against GASNet-EX.

On Ubuntu, the above command fails, but if I copy the resulting gfortran link command and reuse it with $GASNET_LIBS moved to the tail end, linking succeeds.

Expected Behaviour

Successful linking.

Version of fpm

c5ef60d

Platform and Architecture

Ubuntu via GitHub Action

Additional Information

No response

awvwgk commented 2 years ago

Preferably we could put libraries we link against in the package manifest under build.link rather than injecting them via the command line options. The --link-flag is mainly meant to add -L flags to find libraries we want to link against from the package manifest or add linker options (like -Wl,-rpath, LTO, ...).

Would adding the required libraries in build.link be sufficient for your use case?

awvwgk commented 2 years ago

The --link-flag arguments from the model are purposely put in front of the object files here

https://github.com/fortran-lang/fpm/blob/c5ef60db6629987e06002ed02a2c1d4f37e58501/src/fpm_targets.f90#L517

rouson commented 2 years ago

Would adding the required libraries in build.link be sufficient for your use case?

I didn't know about build.link. I'll try it. Thanks for the quick reply!

rouson commented 2 years ago

In my use case, building on macOS worked but not on Linux. @awvwgk, your suggestion works on Linux but breaks the macOS build. For now, I think we have to come up with a way to use different fpm.toml files on one operating system versus another. Eventually one of the solutions proposed in issue #611 will likely be best, but it still might be a good idea to also have the option to specify -L and -l arguments separately from other link arguments and have the -L and -l arguments be placed at the end of the link command.

jvdp1 commented 2 years ago

I agree with @rouson that having the option to specify separately -L and -l in the argumetns would be a nice feature. This could be helpful to support multiple compilers with a same fpm.toml, with the -l argument added to the specified libs in the fpm.toml under [link].

urbanjost commented 2 years ago

Haven't carefully read this discussion yet but I always put -L and -l on the right side of my build objects in order as a result of working with single-pass left-to-right loaders; without trouble; but since multiple -flag options are allowed, perhaps a special option like --place [left|right] that would indicate the position to place subsequent options would be a stop-gap? Although nearly a standard, not all compilers necessarily use -L and -l so just looking for those flag names might have issues

awvwgk commented 2 years ago

@rouson Would native support for pkg-config in fpm be a solution for your library? I see that there is a generation step paring the GASNet-EX pkg-config file to build the fpm.toml at

https://github.com/BerkeleyLab/caffeine/blob/96ae0a818203ed92017e79bd3debb3b2f6e3c556/install.sh#L297-L303

We discussed pkg-config support in https://github.com/fortran-lang/fpm/issues/439, but didn't arrive at an agreement whether fpm should support it or no.