Skip to content

CMake helper function 'find_link_library' is broken. #1113

@WildCard65

Description

@WildCard65

The current implementation of the custom helper function 'find_link_library' is

function(find_link_library libname outlibname)
  check_linker_flag(C "-l${libname}" mi_has_lib${libname})
  if (mi_has_lib${libname})
    message(VERBOSE "link library: -l${libname}")
    set(${outlibname} ${libname} PARENT_SCOPE)
  else()
    find_library(MI_LIBPATH libname)
    if (MI_LIBPATH)
      message(VERBOSE "link library ${libname} at ${MI_LIBPATH}")
      set(${outlibname} ${MI_LIBPATH} PARENT_SCOPE)
    else()
      message(VERBOSE "link library not found: ${libname}")
      set(${outlibname} "" PARENT_SCOPE)
    endif()
  endif()
endfunction()

As it currently stands, it is 100% broken in the event the linker flag check fails, this is because it passes "libname" instead of "${libname}" to find_library AND will silently abort if MI_LIBPATH is set to a library path already (in a previous call).

To fix this, obviously "libname" -> "${libname}" is required, but one of two solutions can be deployed for the variable situation:

  1. Explicitly set "MI_LIBPATH" to the value "MI_LIBPATH-NOTFOUND" to force find_library to run, this can be a normal variable.
  2. Do what the linker flag check does and append "${libname}" to the test variable: "MI_LIBPATH" -> "MI_LIBPATH${libname}".

Markdown is hiding underscores in the suggested fixes for the MI_LIBPATH variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions