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

Ability to pass any compiler flags directly to the compiler #1096

Open
connoraird opened this issue Jan 23, 2025 · 3 comments
Open

Ability to pass any compiler flags directly to the compiler #1096

connoraird opened this issue Jan 23, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@connoraird
Copy link

connoraird commented Jan 23, 2025

Description

It would be very useful to pass compiler flags from the fpm.toml file directly to the compilation commands. It seems that currently it is possible to pass very specific flags such as -ffree-form but something more generic would be more flexible. For example

name = "my-fortran-project"

[fortran]
source-form = "free"
user-defined-flags = "-ffree-line-length-none"

The new user-defined-flags would then be passed straight through to the compiler and the compiler allowed to catch any errors with the flags.

Possible Solution

get_feature_flags could handle a new feature user_defined_flags

function get_feature_flags(compiler, features) result(flags)
    type(compiler_t), intent(in) :: compiler
    type(fortran_features_t), intent(in) :: features
    character(:), allocatable :: flags

    flags = ""
    if (features%implicit_typing) then
        flags = flags // compiler%get_feature_flag("implicit-typing")
    else
        flags = flags // compiler%get_feature_flag("no-implicit-typing")
    end if

    if (features%implicit_external) then
        flags = flags // compiler%get_feature_flag("implicit-external")
    else
        flags = flags // compiler%get_feature_flag("no-implicit-external")
    end if

    if (allocated(features%source_form)) then
        flags = flags // compiler%get_feature_flag(features%source_form//"-form")
    end if

    if (allocated(features%user_defined_flags)) then
        flags = flags // " " // features%user_defined_flags
    end if
end function get_feature_flags

This would of course require further changes to add user-defined-flags. Could this be added everywhere source-form also is currently?

Additional Information

No response

@connoraird connoraird added the enhancement New feature or request label Jan 23, 2025
@perazz
Copy link
Member

perazz commented Jan 23, 2025

My understanding of the fortran features structure is to hide compiler details behind the flags, so I'm not 100% sure adding compiler-specific flags is the best approach here.

Regarding max line length, not all compilers support varying it but GNU definitely offers this good option.
So why don't we add it directly to the "get-feature-flags" for the free form? I think that having a generic line length is part of what a free-form source file means for most developers.

Instead of adding one more structure (remember that custom flags are passed via CLI rather than manifest), could we instead just change this:

flag_gnu_free_form = " -ffree-form", &

to this?

flag_gnu_free_form = " -ffree-form -ffree-line-length-none -ffixed-line-length-none", & 

@connoraird
Copy link
Author

Thank you for your speedy comments!

My understanding of the fortran features structure is to hide compiler details behind the flags, so I'm not 100% sure adding compiler-specific flags is the best approach here.

This is intended to not be specific to any compiler and the responsibility then is passed to the "user". But I understand that hiding compiler specifics is a design choice and therefore shouldn't be changed lightly. However, I can still see the benefit of having a way to specify any compiler flags via the manifest, rather than requiring --flag to be passed in every command.

Regarding max line length, not all compilers support varying it but GNU definitely offers this good option. So why don't we add it directly to the "get-feature-flags" for the free form? I think that having a generic line length is part of what a free-form source file means for most developers.

I think this sounds good and would solve issues with that particular flag. I shall raise another issue/PR to update the free-form but I am still interested in this mechanism of passing any compiler flag through the manifest. Happy to be proved wrong though.

@perazz
Copy link
Member

perazz commented Jan 24, 2025

I agree, would probably make the most sense to have them under the [build] table? I.e. Cargo has rustflags, we may have something like this?

[build]
link = ["blas","lapack"]
flags = ["-some-other-flag"]
c-flags = ["-c-only-flag"]
cxx-flags = ["-cpp-only-flag"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants