# Generate driver_config.h from the list of #defines
SET (driver_config_h "${CMAKE_CURRENT_BINARY_DIR}/driver_config.h")
FILE (WRITE ${driver_config_h} "// This file was automatically generated by the Player build system.\n")
FILE (APPEND ${driver_config_h} "// Any changes made to it will be overwritten.\n\n")
FILE (APPEND ${driver_config_h} "#define ON 1\n")
FOREACH (_define ${PLAYERDRIVER_DEFINES})
    # Blank line to keep things tidy
    FILE (APPEND ${driver_config_h} "\n")
    # Add the #define and its value if necessary
    IF (${_define})
        FILE (APPEND ${driver_config_h} "#define ${_define} ${${_define}}\n")
    ELSE (${_define})
        FILE (APPEND ${driver_config_h} "// #undef ${_define}\n")
    ENDIF (${_define})
ENDFOREACH (_define ${PLAYERDRIVER_DEFINES})
INCLUDE_DIRECTORIES (${CMAKE_CURRENT_BINARY_DIR})

# Generate the driver registry from the list of drivers
SET (driverregistry_cc "${CMAKE_CURRENT_BINARY_DIR}/driverregistry.cc")
FILE (WRITE ${driverregistry_cc} "// This file was automatically generated by the Player build system.\n")
FILE (APPEND ${driverregistry_cc} "// Any changes made to it will be overwritten.\n\n")
FILE (APPEND ${driverregistry_cc} "#include <libplayercore/playercore.h>\n\n")
FILE (APPEND ${driverregistry_cc} "#if defined (WIN32)\n")
FILE (APPEND ${driverregistry_cc} "#if defined (PLAYER_STATIC)\n")
FILE (APPEND ${driverregistry_cc} "#define PLAYERDRIVER_EXPORT\n")
FILE (APPEND ${driverregistry_cc} "#elif defined (playerdrivers_EXPORTS)\n")
FILE (APPEND ${driverregistry_cc} "#define PLAYERDRIVER_EXPORT __declspec (dllexport)\n")
FILE (APPEND ${driverregistry_cc} "#else\n")
FILE (APPEND ${driverregistry_cc} "#define PLAYERDRIVER_EXPORT __declspec (dllimport)\n")
FILE (APPEND ${driverregistry_cc} "#endif\n")
FILE (APPEND ${driverregistry_cc} "#else\n")
FILE (APPEND ${driverregistry_cc} "#define PLAYERDRIVER_EXPORT\n")
FILE (APPEND ${driverregistry_cc} "#endif\n\n")
FOREACH (driverName ${PLAYER_BUILT_DRIVERS})
    # Add the register function prototypes
    FILE (APPEND ${driverregistry_cc} "void ${driverName}_Register(DriverTable *table);\n")
ENDFOREACH (driverName ${PLAYER_BUILT_DRIVERS})
# Now call them
FILE (APPEND ${driverregistry_cc} "\nPLAYERDRIVER_EXPORT void player_register_drivers()\n{\n")
FOREACH (driverName ${PLAYER_BUILT_DRIVERS})
    FILE (APPEND ${driverregistry_cc} "  ${driverName}_Register(driverTable);\n")
ENDFOREACH (driverName ${PLAYER_BUILT_DRIVERS})
FILE (APPEND ${driverregistry_cc} "}\n")

# Create a list of the sources map
SET (allSourceNames ${PLAYER_BUILT_DRIVERS})
IF (PLAYER_BUILT_DRIVEREXTRAS)
    LIST (APPEND allSourceNames ${PLAYER_BUILT_DRIVEREXTRAS})
ENDIF (PLAYER_BUILT_DRIVEREXTRAS)
MAP_TO_LIST (driversSrcs "${allSourceNames}" PLAYER_DRIVERSLIB_SOURCES_MAP)
IF (NOT HAVE_CFMAKERAW)
    SET (playerreplaceLib playerreplace)
ENDIF (NOT HAVE_CFMAKERAW)
IF (NOT HAVE_ROUND)
    SET (playerreplaceLib playerreplace)
ELSEIF (NOT HAVE_ROUND)
    IF (NOT HAVE_RINT)
        SET (playerreplaceLib playerreplace)
    ENDIF (NOT HAVE_RINT)
ENDIF (NOT HAVE_ROUND)
IF (NOT HAVE_GETTIMEOFDAY)
    SET (playerreplaceLib playerreplace)
ENDIF (NOT HAVE_GETTIMEOFDAY)
IF (NOT HAVE_USLEEP)
    SET (playerreplaceLib playerreplace)
ENDIF (NOT HAVE_USLEEP)
IF (NOT HAVE_NANOSLEEP)
    SET (playerreplaceLib playerreplace)
ENDIF (NOT HAVE_NANOSLEEP)

FILTER_DUPLICATES (driversSrcs "${driversSrcs}")

# Set lib-wide link and include directories
IF (PLAYER_DRIVERSLIB_INCLUDEDIRS)
    FILTER_EMPTY (driverIncludeDirs "${PLAYER_DRIVERSLIB_INCLUDEDIRS}")
    FILTER_DUPLICATES (driverIncludeDirs "${driverIncludeDirs}")
    INCLUDE_DIRECTORIES (${driverIncludeDirs})
ENDIF (PLAYER_DRIVERSLIB_INCLUDEDIRS)
IF (PLAYER_DRIVERSLIB_LIBDIRS)
    FILTER_EMPTY (driverLinkDirs "${PLAYER_DRIVERSLIB_LIBDIRS}")
    FILTER_DUPLICATES (driverLinkDirs "${driverLinkDirs}")
    LINK_DIRECTORIES (${driverLinkDirs})
ENDIF (PLAYER_DRIVERSLIB_LIBDIRS)
IF (NOT PTHREAD_INCLUDE_DIR STREQUAL "")
    INCLUDE_DIRECTORIES (${PTHREAD_INCLUDE_DIR})
ENDIF (NOT PTHREAD_INCLUDE_DIR STREQUAL "")
IF (NOT PTHREAD_LIB_DIR STREQUAL "")
    LINK_DIRECTORIES (${PTHREAD_LIB_DIR})
ENDIF (NOT PTHREAD_LIB_DIR STREQUAL "")

# Actually add the target
PLAYER_ADD_LIBRARY (playerdrivers ${driver_config_h} ${driverregistry_cc} ${driversSrcs})
TARGET_LINK_LIBRARIES (playerdrivers playercore playercommon playerwkb ${playerreplaceLib})
IF (HAVE_JPEG)
    TARGET_LINK_LIBRARIES (playerdrivers playerjpeg)
ENDIF (HAVE_JPEG)
IF (PLAYER_OS_SOLARIS)
    TARGET_LINK_LIBRARIES (playerdrivers rt)
ENDIF (PLAYER_OS_SOLARIS)
IF (PLAYER_DRIVERSLIB_LINKLIBS)
    FILTER_EMPTY (driverLinkLibs "${PLAYER_DRIVERSLIB_LINKLIBS}")
    FILTER_DUPLICATES (driverLinkLibs "${driverLinkLibs}")
    TARGET_LINK_LIBRARIES (playerdrivers ${driverLinkLibs})
ENDIF (PLAYER_DRIVERSLIB_LINKLIBS)

# Set C flags on source files that need them
FOREACH (driverName ${PLAYER_BUILT_DRIVERS})
    GET_FROM_GLOBAL_MAP (cFlags PLAYER_DRIVERSLIB_CFLAGS ${driverName})
    IF (cFlags)
        GET_FROM_GLOBAL_MAP (sourceFiles PLAYER_DRIVERSLIB_SOURCES_MAP ${driverName})
        SET_SOURCE_FILES_PROPERTIES (${sourceFiles} PROPERTIES
            COMPILE_FLAGS "${cFlags}")
    ENDIF (cFlags)
ENDFOREACH (driverName ${PLAYER_BUILT_DRIVERS})
FOREACH (extraName ${PLAYER_BUILT_DRIVEREXTRAS})
    GET_FROM_GLOBAL_MAP (cFlags PLAYER_DRIVERSLIB_CFLAGS ${extraName})
    IF (cFlags)
        GET_FROM_GLOBAL_MAP (sourceFiles PLAYER_DRIVERSLIB_SOURCES_MAP ${extraName})
        SET_SOURCE_FILES_PROPERTIES (${sourceFiles} PROPERTIES
            COMPILE_FLAGS "${cFlags}")
    ENDIF (cFlags)
ENDFOREACH (extraName ${PLAYER_BUILT_DRIVEREXTRAS})

# Set the link flags for the library appropriately
IF (PLAYER_DRIVERSLIB_LINKFLAGS)
    SET_TARGET_PROPERTIES(playerdrivers PROPERTIES
        LINK_FLAGS ${PLAYER_DRIVERSLIB_LINKFLAGS})
ENDIF (PLAYER_DRIVERSLIB_LINKFLAGS)

# Package config file for libplayerdrivers
IF (PTHREAD_LIB)
    SET (PTHREAD_LIB_FLAG "-l${PTHREAD_LIB}")
ENDIF (PTHREAD_LIB)
PLAYER_MAKE_PKGCONFIG ("playerdrivers" "Player driver library - part of the Player Project"
                       "playercore ${playerreplaceLib}" "" "" "${PTHREAD_LIB_FLAG}")

PLAYER_INSTALL_HEADERS (playerdrivers driverregistry.h)
