include_directories( . )

add_definitions(-Dasm=__asm__ -D_INTL_REDIRECT_MACROS)
add_definitions(-DLOCALEDIR=\\"${CMAKE_INSTALL_PREFIX}/locale\\")
add_definitions(-DPLUGIN_PATH=\\"${CMAKE_INSTALL_PREFIX}/lib/vlc\\")

# vlc
add_executable(vlc vlc.c)
target_link_libraries(vlc libvlc)

install_targets(/bin vlc)

# libvlc

set( SOURCES_libvlc_beos misc/beos_specific.cpp )
set( SOURCES_libvlc_darwin misc/darwin_specific.c )
set( SOURCES_libvlc_win32 misc/win32_specific.c )
set( SOURCES_libvlc_dirent extras/dirent.c )
set( SOURCES_libvlc_getopt misc/getopt.c misc/getopt.h misc/getopt1.c )
set( SOURCES_libvlc_common
    libvlc.c
    libvlc-common.c
    libvlc.h
    libvlc-module.c
    interface/interface.h
    interface/interface.c
    interface/intf_eject.c
    interface/interaction.c
    playlist/playlist_internal.h
    playlist/thread.c
    playlist/control.c
    playlist/engine.c
    playlist/sort.c
    playlist/loadsave.c
    playlist/tree.c
    playlist/item.c
    playlist/search.c
    playlist/services_discovery.c
    input/item.c
    input/access.c
    input/clock.c
    input/control.c
    input/decoder.c
    input/decoder_synchro.c
    input/demux.c
    input/es_out.c
    input/input.c
    input/meta.c
    input/input_internal.h
    input/vlm_internal.h
    input/stream.c
    input/mem_stream.c
    input/subtitles.c
    input/var.c
    video_output/video_output.c
    video_output/vout_pictures.c
    video_output/vout_pictures.h
    video_output/video_text.c
    video_output/video_widgets.c
    video_output/vout_subpictures.c
    video_output/vout_intf.c
    audio_output/aout_internal.h
    audio_output/common.c
    audio_output/dec.c
    audio_output/filters.c
    audio_output/input.c
    audio_output/mixer.c
    audio_output/output.c
    audio_output/intf.c
    stream_output/stream_output.c
    stream_output/stream_output.h
    stream_output/announce.c
    stream_output/profiles.c
    stream_output/sap.c
    stream_output/sdp.c
    osd/osd.c
    osd/osd_text.c
    osd/osd_widgets.c
    network/acl.c
    network/getaddrinfo.c
    network/io.c
    network/error.c
    network/tcp.c
    network/udp.c
    network/httpd.c
    network/rootwrap.c
    network/tls.c
    network/poll.c
    text/charset.c
    text/strings.c
    text/unicode.c
    text/wincp.c
    text/iso_lang.c
    text/iso-639_def.h
    misc/md5.c
    misc/rand.c
    misc/mtime.c
    misc/block.c
    ${CMAKE_BINARY_DIR}/src/modules/builtin.h
    modules/modules.h
    modules/modules.c
    modules/cache.c
    modules/entry.c
    modules/os.c
    misc/threads.c
    misc/stats.c
    misc/cpu.c
    config/configuration.h
    config/core.c
    config/chain.c
    config/file.c
    config/cmdline.c
    misc/events.c
    misc/image.c
    misc/messages.c
    misc/objects.c
    misc/variables.h
    misc/variables.c
    misc/error.c
    misc/update.c
    ${CMAKE_BINARY_DIR}/src/misc/revision.c
    input/vlm.c
    misc/xml.c
    misc/devices.c
    extras/libc.c
    ${CMAKE_BINARY_DIR}/include/vlc_about.h )

set( SOURCES_libvlc ${SOURCES_libvlc_common} )

if(SYS_DARWIN)
    set( SOURCES_libvlc ${SOURCES_libvlc} ${SOURCES_libvlc_darwin} )
endif(SYS_DARWIN)

add_library(libvlc SHARED ${SOURCES_libvlc})
install_targets(/lib libvlc)

set_target_properties(libvlc PROPERTIES OUTPUT_NAME vlc)
set_target_properties(libvlc PROPERTIES SOVERSION 1)
set_target_properties(libvlc PROPERTIES VERSION 1.0.0)

target_link_libraries(libvlc ${LIBM})
target_link_libraries(libvlc ${LIBRT})
target_link_libraries(libvlc ${LIBICONV})
target_link_libraries(libvlc ${Dlopen_LIBRARIES})


# libvlc-control
set( SOURCES_libvlc_control
     control/libvlc_internal.h
     control/core.c
     control/log.c
     control/playlist.c
     control/vlm.c
     control/video.c
     control/audio.c
     control/dynamic_media_list.c
     control/event.c
     control/flat_media_list_view.c
     control/hierarchical_media_list_view.c
     control/hierarchical_node_media_list_view.c
     control/media_descriptor.c
     control/media_instance.c
     control/media_list.c
     control/media_list_path.h
     control/media_list_player.c
     control/media_list_view.c
     control/media_library.c
     control/mediacontrol_internal.h
     control/mediacontrol_core.c
     control/mediacontrol_util.c
     control/mediacontrol_audio_video.c
     control/media_discoverer.c
     control/tag_query.c )

add_library(libvlc-control SHARED ${SOURCES_libvlc_control})
install_targets(/lib libvlc-control)

set_target_properties(libvlc-control PROPERTIES OUTPUT_NAME vlc-control)
set_target_properties(libvlc-control PROPERTIES SOVERSION 0)
set_target_properties(libvlc PROPERTIES VERSION 0.0.0)

target_link_libraries(libvlc-control libvlc)

##########################################################
# Libraries

# Carbon
if(APPLE)
  find_library(CARBON_FRAMEWORK Carbon)
  target_link_libraries(libvlc ${CARBON_FRAMEWORK})
endif(APPLE)

##########################################################
# revision.c

set(rev "${CMAKE_BINARY_DIR}/src/misc/revision.c")
add_custom_command(
OUTPUT ${rev}
COMMAND    rm -f ${rev} ${rev}.tmp
COMMAND    mkdir -p `dirname ${rev}`
COMMAND    echo "/* AUTOGENERATED FILE - DO NOT EDIT */" > ${rev}.tmp
COMMAND    printf "const char psz_vlc_changeset[] = \"" >> ${rev}.tmp
COMMAND    sh -c "printf `LANG=C svnversion ${CMAKE_SOURCE_DIR} || printf \"exported\"`" >> ${rev}.tmp
COMMAND    echo "\";" >> ${rev}.tmp
COMMAND    mv -f ${rev}.tmp ${rev}
VERBATIM
)
set(rev)

##########################################################
# builtin.h
# No real use for this one, we just used to need it.
# We should really remove that.
set(builtin "${CMAKE_BINARY_DIR}/src/modules/builtin.h")

add_custom_command(
OUTPUT ${builtin}
COMMAND    mkdir -p `dirname ${builtin}`
COMMAND    echo "/* Auto generated by ${CMAKE_CURRENT_BUILD_DIR}/CMakeLists.txt */" > ${builtin}
COMMAND    echo "int vlc_entry__main( module_t* );" >> ${builtin}
COMMAND    echo "#define ALLOCATE_ALL_BUILTINS() do {} while(0)" >> ${builtin}
VERBATIM
)
set(should)

##########################################################
# vlc_about.h
set(about "${CMAKE_BINARY_DIR}/include/vlc_about.h")

add_custom_command(
OUTPUT ${about}
COMMAND rm -f -- "${about}.tmp"
COMMAND    mkdir -p `dirname ${about}`
COMMAND mkdir -p -- ../include
COMMAND echo "/* Automatically generated file - DO NOT EDIT */" > "${about}.tmp"
COMMAND echo "static const char psz_license[] =" >> "${about}.tmp"
COMMAND cat ${CMAKE_SOURCE_DIR}/COPYING | sed s/\"/''/g | awk "{ print \"\\\"\"$0\"\\\\n\\\"\" }" >> "${about}.tmp"
COMMAND echo ";" >> "${about}.tmp"
COMMAND echo "static const char psz_thanks[] =" >> "${about}.tmp"
COMMAND grep -v '$$Id:'  ${CMAKE_SOURCE_DIR}/THANKS | sed "s/\"/''/g" | awk "{ print  \"\\\"\"$0\"\\\\n\\\"\" }"|sed "s/\"<.*.> \"//" >> "${about}.tmp"
COMMAND echo ";" >> "${about}.tmp"
COMMAND echo "static const char psz_authors[] =" >> "${about}.tmp"
COMMAND grep N: ${CMAKE_SOURCE_DIR}/AUTHORS | cut -d "\ " -f 2- | sed "s/\"/''/g" | awk "{ print  \"\\\"\"$0\"\\\\n\\\"\" }" >> "${about}.tmp"
COMMAND echo ";" >> "${about}.tmp"
COMMAND mv -f -- "${about}.tmp" "${about}"
DEPENDS ${CMAKE_SOURCE_DIR}/AUTHORS ${CMAKE_SOURCE_DIR}/THANKS ${CMAKE_SOURCE_DIR}/COPYING
VERBATIM)
set(about)


##########################################################
# SET_TARGET_PROPERTIES

# FIXME: Should probably be autodetected in configure?
if(NOT APPLE)
   set(LINK_FLAGS "-z defs --as-needed")
endif(NOT APPLE)

set_target_properties(libvlc libvlc-control PROPERTIES LINK_FLAGS "${LINK_FLAGS} ${CMAKE_THREAD_LIBS_INIT}")
set_target_properties(libvlc libvlc-control vlc PROPERTIES COMPILE_FLAGS
    "-Wall -Wextra -Wsign-compare -Wundef -Wpointer-arith -Wbad-function-cast -Wcast-align -Wwrite-strings -Wmissing-prototypes -I${CMAKE_BINARY_DIR}/src -I${CMAKE_BINARY_DIR}/include" )

##########################################################
# Tests

add_executable(testapi   control/testapi.c)
add_executable(i18n_atof test/i18n_atof.c)
add_executable(url       test/url.c)
add_executable(utf8      test/utf8.c)

target_link_libraries(testapi   libvlc-control)
target_link_libraries(i18n_atof libvlc)
target_link_libraries(url       libvlc)
target_link_libraries(utf8      libvlc)

add_test(testapi   ${EXECUTABLE_OUTPUT_PATH}/testapi)
add_test(i18n_atof ${EXECUTABLE_OUTPUT_PATH}/i18n_atof)
add_test(url       ${EXECUTABLE_OUTPUT_PATH}/url)
add_test(utf8      ${EXECUTABLE_OUTPUT_PATH}/utf8)
