drop_compile_options Function

public function drop_compile_options(link_flags, compile_flags) result(flags)

Link flags without the options that the compile flags already pass: executables are linked with both, and a metapackage can set the same option in each (OpenMP), which a strict compiler rejects when repeated (NAG: -openmp). Only an option standing alone is dropped: paths, libraries, linker flags, repeats, and options that take an argument stay with that argument. The flags are split on blanks, not parsed, so Windows paths keep their backslashes.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: link_flags
character(len=*), intent(in) :: compile_flags

Return Value character(len=:), allocatable


Variables

Type Visibility Attributes Name Initial
character(len=:), public, allocatable :: compile_tokens(:)
integer, public :: i
character(len=*), public, parameter :: keywords(*) = [character(len=15)::'-framework', '-weak_framework', '-arch', '-isysroot', '-target', '-rpath', '-u', '-T', '-z']

Options that take the next token as their argument

character(len=:), public, allocatable :: link_tokens(:)
logical, public :: lone

Functions

pure function takes_argument(token)

Whether token is an option followed by its argument (-framework Accelerate)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: token

Return Value logical


Source Code

function drop_compile_options(link_flags, compile_flags) result(flags)
    character(*), intent(in) :: link_flags, compile_flags
    character(:), allocatable :: flags

    !> Options that take the next token as their argument
    character(len=*), parameter :: keywords(*) = [character(len=15) :: '-framework', &
        & '-weak_framework', '-arch', '-isysroot', '-target', '-rpath', '-u', '-T', '-z']

    character(len=:), allocatable :: link_tokens(:), compile_tokens(:)
    integer :: i
    logical :: lone

    call split(link_flags, link_tokens)
    call split(compile_flags, compile_tokens)

    flags = ""
    do i = 1, size(link_tokens)
        lone = str_begins_with_str(link_tokens(i), '-') .and. .not.takes_argument(link_tokens(i))
        if (lone) lone = .not.(str_begins_with_str(link_tokens(i), '-l') .or. &
                               str_begins_with_str(link_tokens(i), '-L') .or. &
                               str_begins_with_str(link_tokens(i), '-Wl,'))
        if (lone .and. i > 1) lone = .not.takes_argument(link_tokens(i-1))
        if (lone) then
            if (any(compile_tokens == link_tokens(i))) cycle
        end if
        flags = flags // " " // trim(link_tokens(i))
    end do

contains

    !> Whether `token` is an option followed by its argument (`-framework Accelerate`)
    pure logical function takes_argument(token)
        character(*), intent(in) :: token
        takes_argument = str_begins_with_str(token, '-X') .or. any(keywords == token)
    end function takes_argument

end function drop_compile_options