SET(SOURCES
    AB01MD.f AB01ND.f AB01OD.f AB04MD.f AB05MD.f AB05ND.f AB05OD.f
    AB05PD.f AB05QD.f AB05RD.f AB05SD.f AB07MD.f AB07ND.f AB08MD.f
    AB08MZ.f AB08ND.f AB08NW.f AB08NX.f AB08NY.f AB08NZ.f AB09AD.f
    AB09AX.f AB09BD.f AB09BX.f AB09CD.f AB09CX.f AB09DD.f AB09ED.f
    AB09FD.f AB09GD.f AB09HD.f AB09HX.f AB09HY.f AB09ID.f AB09IX.f
    AB09IY.f AB09JD.f AB09JV.f AB09JW.f AB09JX.f AB09KD.f AB09KX.f
    AB09MD.f AB09ND.f AB13AD.f AB13AX.f AB13BD.f AB13CD.f AB13DD.f
    AB13DX.f AB13ED.f AB13FD.f AB13HD.f AB13ID.f AB13MD.f AB8NXZ.f
    AG07BD.f AG08BD.f AG08BY.f AG08BZ.f AG8BYZ.f

    BB01AD.f BB02AD.f BB03AD.f BB04AD.f BD01AD.f BD02AD.f

    DE01OD.f DE01PD.f DF01MD.f DG01MD.f DG01ND.f DG01NY.f DG01OD.f
    DK01MD.f

    FB01QD.f FB01RD.f FB01SD.f FB01TD.f FB01VD.f FD01AD.f

    IB01AD.f IB01BD.f IB01CD.f IB01MD.f IB01MY.f IB01ND.f IB01OD.f
    IB01OY.f IB01PD.f IB01PX.f IB01PY.f IB01QD.f IB01RD.f IB03AD.f
    IB03BD.f

    MA01AD.f MA01BD.f MA01BZ.f MA01CD.f MA01DD.f MA01DZ.f MA02AD.f MA02AZ.f
    MA02BD.f MA02BZ.f MA02CD.f MA02CZ.f MA02DD.f MA02ED.f MA02ES.f MA02EZ.f
    MA02FD.f MA02GD.f MA02GZ.f MA02HD.f MA02HZ.f MA02ID.f MA02IZ.f MA02JD.f
    MA02JZ.f MA02MD.f MA02MZ.f MA02NZ.f MA02OD.f MA02OZ.f MA02PD.f MA02PZ.f
    MA02RD.f MA02SD.f MB01KD.f MB01LD.f MB01MD.f MB01ND.f MB01OC.f MB01OD.f
    MB01OE.f MB01OH.f MB01OO.f MB01OS.f MB01OT.f MB01PD.f MB01QD.f MB01RB.f
    MB01RD.f MB01RH.f MB01RT.f MB01RU.f MB01RW.f MB01RX.f MB01RY.f MB01SD.f
    MB01SS.f MB01TD.f MB01UD.f MB01UW.f MB01UX.f MB01UY.f MB01UZ.f MB01VD.f
    MB01WD.f MB01XD.f MB01XY.f MB01YD.f MB01ZD.f MB02CD.f MB02CU.f MB02CV.f
    MB02CX.f MB02CY.f MB02DD.f MB02ED.f MB02FD.f MB02GD.f MB02HD.f MB02ID.f
    MB02JD.f MB02JX.f MB02KD.f MB02MD.f MB02ND.f MB02NY.f MB02OD.f MB02PD.f
    MB02QD.f MB02QY.f MB02RD.f MB02RZ.f MB02SD.f MB02SZ.f MB02TD.f MB02TZ.f
    MB02UD.f MB02UU.f MB02UV.f MB02UW.f MB02VD.f MB02WD.f MB02XD.f MB02YD.f
    MB03AB.f MB03AD.f MB03AE.f MB03AF.f MB03AG.f MB03AH.f MB03AI.f MB03BA.f
    MB03BB.f MB03BC.f MB03BD.f MB03BE.f MB03BF.f MB03BG.f MB03BZ.f MB03CD.f
    MB03CZ.f MB03DD.f MB03DZ.f MB03ED.f MB03FD.f MB03FZ.f MB03GD.f MB03GZ.f
    MB03HD.f MB03HZ.f MB03ID.f MB03IZ.f MB03JD.f MB03JP.f MB03JZ.f MB03KA.f
    MB03KB.f MB03KC.f MB03KD.f MB03KE.f MB03LD.f MB03LF.f MB03LP.f MB03LZ.f
    MB03MD.f MB03MY.f MB03ND.f MB03NY.f MB03OD.f MB03OY.f MB03PD.f MB03PY.f
    MB03QD.f MB03QG.f MB03QV.f MB03QW.f MB03QX.f MB03QY.f MB03RD.f MB03RW.f
    MB03RX.f MB03RY.f MB03RZ.f MB03SD.f MB03TD.f MB03TS.f MB03UD.f MB03VD.f
    MB03VW.f MB03VY.f MB03WA.f MB03WD.f MB03WX.f MB03XD.f MB03XP.f MB03XS.f
    MB03XU.f MB03XZ.f MB03YA.f MB03YD.f MB03YT.f MB03ZA.f MB03ZD.f MB04AD.f
    MB04AZ.f MB04BD.f MB04BP.f MB04BZ.f MB04CD.f MB04DB.f MB04DD.f MB04DI.f
    MB04DL.f MB04DP.f MB04DS.f MB04DY.f MB04DZ.f MB04ED.f MB04FD.f MB04FP.f
    MB04GD.f MB04HD.f MB04ID.f MB04IY.f MB04IZ.f MB04JD.f MB04KD.f MB04LD.f
    MB04MD.f MB04ND.f MB04NY.f MB04OD.f MB04OW.f MB04OX.f MB04OY.f MB04PA.f
    MB04PB.f MB04PU.f MB04PY.f MB04QB.f MB04QC.f MB04QF.f MB04QS.f MB04QU.f
    MB04RB.f MB04RD.f MB04RS.f MB04RT.f MB04RU.f MB04RV.f MB04RW.f MB04RZ.f
    MB04SU.f MB04TB.f MB04TS.f MB04TT.f MB04TU.f MB04TV.f MB04TW.f MB04TX.f
    MB04TY.f MB04UD.f MB04VD.f MB04VX.f MB04WD.f MB04WP.f MB04WR.f MB04WU.f
    MB04XD.f MB04XY.f MB04YD.f MB04YW.f MB04ZD.f MB05MD.f MB05MY.f MB05ND.f
    MB05OD.f MB05OY.f MB3JZP.f MB3LZP.f MB3OYZ.f MB3PYZ.f MB4DBZ.f MB4DLZ.f
    MB4DPZ.f MC01MD.f MC01ND.f MC01OD.f MC01PD.f MC01PY.f MC01QD.f MC01RD.f
    MC01SD.f MC01SW.f MC01SX.f MC01SY.f MC01TD.f MC01VD.f MC01WD.f MC01XD.f
    MC03MD.f MC03ND.f MC03NX.f MC03NY.f MD03AD.f MD03BA.f MD03BB.f MD03BD.f
    MD03BF.f MD03BX.f

    MD03BY.f NF01AD.f NF01AY.f NF01BA.f NF01BB.f NF01BD.f NF01BE.f NF01BF.f
    NF01BP.f NF01BQ.f NF01BR.f NF01BS.f NF01BU.f NF01BV.f NF01BW.f NF01BX.f
    NF01BY.f

    SB01BD.f SB01BX.f SB01BY.f SB01DD.f SB01FY.f SB01MD.f SB02CX.f SB02MD.f
    SB02MR.f SB02MS.f SB02MT.f SB02MU.f SB02MV.f SB02MW.f SB02MX.f SB02ND.f
    SB02OD.f SB02OU.f SB02OV.f SB02OW.f SB02OX.f SB02OY.f SB02PD.f SB02QD.f
    SB02RD.f SB02RU.f SB02SD.f SB03MD.f SB03MU.f SB03MV.f SB03MW.f SB03MX.f
    SB03MY.f SB03OD.f SB03OR.f SB03OS.f SB03OT.f SB03OU.f SB03OV.f SB03OY.f
    SB03OZ.f SB03PD.f SB03QD.f SB03QX.f SB03QY.f SB03RD.f SB03SD.f SB03SX.f
    SB03SY.f SB03TD.f SB03UD.f SB04MD.f SB04MR.f SB04MU.f SB04MW.f SB04MY.f
    SB04ND.f SB04NV.f SB04NW.f SB04NX.f SB04NY.f SB04OD.f SB04OW.f SB04PD.f
    SB04PX.f SB04PY.f SB04QD.f SB04QR.f SB04QU.f SB04QY.f SB04RD.f SB04RV.f
    SB04RW.f SB04RX.f SB04RY.f SB06ND.f SB08CD.f SB08DD.f SB08ED.f SB08FD.f
    SB08GD.f SB08HD.f SB08MD.f SB08MY.f SB08ND.f SB08NY.f SB09MD.f SB10AD.f
    SB10DD.f SB10ED.f SB10FD.f SB10HD.f SB10ID.f SB10JD.f SB10KD.f SB10LD.f
    SB10MD.f SB10PD.f SB10QD.f SB10RD.f SB10SD.f SB10TD.f SB10UD.f SB10VD.f
    SB10WD.f SB10YD.f SB10ZD.f SB10ZP.f SB16AD.f SB16AY.f SB16BD.f SB16CD.f
    SB16CY.f SG02AD.f SG02CV.f SG02CW.f SG02CX.f SG02ND.f SG03AD.f SG03AX.f
    SG03AY.f SG03BD.f SG03BR.f SG03BS.f SG03BT.f SG03BU.f SG03BV.f SG03BW.f
    SG03BX.f SG03BY.f SG03BZ.f TB01ID.f TB01IZ.f TB01KD.f TB01KX.f TB01LD.f

    TB01MD.f TB01ND.f TB01PD.f TB01PX.f TB01TD.f TB01TY.f TB01UD.f TB01UX.f
    TB01UY.f TB01VD.f TB01VY.f TB01WD.f TB01WX.f TB01XD.f TB01XZ.f TB01YD.f
    TB01ZD.f TB03AD.f TB03AY.f TB04AD.f TB04AY.f TB04BD.f TB04BV.f TB04BW.f
    TB04BX.f TB04CD.f TB05AD.f TC01OD.f TC04AD.f TC05AD.f TD03AD.f TD03AY.f
    TD04AD.f TD05AD.f TF01MD.f TF01MX.f TF01MY.f TF01ND.f TF01OD.f TF01PD.f
    TF01QD.f TF01RD.f TG01AD.f TG01AZ.f TG01BD.f TG01CD.f TG01DD.f TG01ED.f
    TG01FD.f TG01FZ.f TG01GD.f TG01HD.f TG01HU.f TG01HX.f TG01HY.f TG01ID.f
    TG01JD.f TG01JY.f TG01KD.f TG01KZ.f TG01LD.f TG01LY.f TG01MD.f TG01ND.f
    TG01NX.f TG01OA.f TG01OB.f TG01OD.f TG01OZ.f TG01PD.f TG01QD.f TG01WD.f

    UD01BD.f UD01CD.f UD01DD.f UD01MD.f UD01MZ.f UD01ND.f UE01MD.f

    delctg.f select.f zelctg.f
    )


# Check for deprecated LAPACK Symbols
INCLUDE(CheckFortranFunctionExists)

MACRO(ADD_ROUTINE_IF_REQUIRED ROUTINE VAR SOURCE_FILE)
    SET(_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
    SET(CMAKE_REQUIRED_LIBRARIES PRIVATE BLAS::BLAS LAPACK::LAPACK)

    Check_Fortran_Function_Exists(${ROUTINE} ${VAR})
    IF (NOT ${VAR})
        MESSAGE(STATUS "Add ${SOURCE_FILE} to library.")
        SET(SOURCES ${SOURCES} ${SOURCE_FILE})
    ENDIF()
    SET(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES})
ENDMACRO()

ADD_ROUTINE_IF_REQUIRED(dgegs HAVE_DGEGS lapack_aux/dgegs.f)
ADD_ROUTINE_IF_REQUIRED(zgegs HAVE_ZGEGS lapack_aux/zgegs.f)
ADD_ROUTINE_IF_REQUIRED(dlatzm HAVE_DLAZTM lapack_aux/dlatzm.f)
ADD_ROUTINE_IF_REQUIRED(zlatzm HAVE_ZLAZTM lapack_aux/zlatzm.f)
ADD_ROUTINE_IF_REQUIRED(dgegv HAVE_DGEGV lapack_aux/dgegv.f)
ADD_ROUTINE_IF_REQUIRED(zgegv HAVE_ZGEGV lapack_aux/zgegv.f)

# Setup the library.

ADD_LIBRARY(slicot ${SOURCES})
TARGET_LINK_LIBRARIES(slicot PUBLIC LAPACK::LAPACK BLAS::BLAS)
SET_PROPERTY(TARGET slicot PROPERTY VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
SET_PROPERTY(TARGET slicot PROPERTY SOVERSION ${PROJECT_VERSION_MAJOR})
SET_PROPERTY(TARGET slicot PROPERTY OUTPUT_NAME ${SLICOT_NAME})

if(_is_coverage_build)
  target_link_libraries(slicot PRIVATE gcov)
  add_coverage(slicot)
endif()




ADD_LIBRARY(SLICOT::slicot ALIAS slicot)

INSTALL(TARGETS slicot
        EXPORT  slicot_targets
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        )

