set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

aux_source_directory(. LIBRIME_SRC_BASE)
aux_source_directory(algo LIBRIME_SRC_ALGO)
aux_source_directory(dict LIBRIME_SRC_DICT)
aux_source_directory(gear LIBRIME_SRC_GEAR)
aux_source_directory(lever LIBRIME_SRC_LEVER)

if(BUILD_SEPARATE_LIBS)
  set(LIBRIME_SRC ${LIBRIME_SRC_BASE})
  set(LIBRIME_GEARS_SRC ${LIBRIME_SRC_ALGO} ${LIBRIME_SRC_DICT} ${LIBRIME_SRC_GEAR} ${LIBRIME_SRC_LEVER})
else(BUILD_SEPARATE_LIBS)
  set(LIBRIME_SRC ${LIBRIME_SRC_BASE} ${LIBRIME_SRC_ALGO} ${LIBRIME_SRC_DICT} ${LIBRIME_SRC_GEAR} ${LIBRIME_SRC_LEVER})
endif(BUILD_SEPARATE_LIBS)

set(RIME_OPTIONAL_DEPS "")
if(Gflags_FOUND)
  set(RIME_OPTIONAL_DEPS ${RIME_OPTIONAL_DEPS} ${Gflags_LIBRARY})
endif(Gflags_FOUND)

set(RIME_CORE_DEPS ${Boost_LIBRARIES} ${Glog_LIBRARY} ${YamlCpp_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${RIME_OPTIONAL_DEPS})
set(RIME_EXTRA_DEPS ${LevelDb_LIBRARY} ${Marisa_LIBRARY} ${Opencc_LIBRARY})

if(BUILD_SEPARATE_LIBS)
  set(RIME_DEPS ${RIME_CORE_DEPS})
  set(RIME_GEARS_DEPS ${RIME_LIBRARY} ${RIME_EXTRA_DEPS})
else(BUILD_SEPARATE_LIBS)
  set(RIME_DEPS ${RIME_CORE_DEPS} ${RIME_EXTRA_DEPS})
endif(BUILD_SEPARATE_LIBS)

if(BUILD_SHARED_LIBS)
  add_library(rime ${LIBRIME_SRC})
  target_link_libraries(rime ${RIME_DEPS})
  set_target_properties(rime PROPERTIES DEFINE_SYMBOL "RIME_EXPORTS")
  set_target_properties(rime PROPERTIES VERSION ${LIBRIME_VERSION} SOVERSION ${LIBRIME_SOVERSION})
  if(XCODE_VERSION)
    set_target_properties(rime PROPERTIES INSTALL_NAME_DIR "@rpath")
  endif(XCODE_VERSION)
  install(TARGETS rime DESTINATION ${LIB_INSTALL_DIR})
  if(BUILD_SEPARATE_LIBS)
    add_library(rime-gears ${LIBRIME_GEARS_SRC})
    target_link_libraries(rime-gears ${RIME_GEARS_DEPS})
    add_dependencies(rime-gears ${RIME_LIBRARY})
    set_target_properties(rime-gears PROPERTIES VERSION ${LIBRIME_VERSION} SOVERSION ${LIBRIME_SOVERSION})
    if(XCODE_VERSION)
      set_target_properties(rime-gears PROPERTIES INSTALL_NAME_DIR "@rpath")
    endif(XCODE_VERSION)
    install(TARGETS rime-gears DESTINATION ${LIB_INSTALL_DIR})
  endif(BUILD_SEPARATE_LIBS)
else(BUILD_SHARED_LIBS)
  add_library(rime-static STATIC ${LIBRIME_SRC})
  target_link_libraries(rime-static ${RIME_DEPS})
  set_target_properties(rime-static PROPERTIES OUTPUT_NAME "rime" PREFIX "lib")
  install(TARGETS rime-static DESTINATION ${LIB_INSTALL_DIR})
  if(BUILD_SEPARATE_LIBS)
    add_library(rime-gears-static STATIC ${LIBRIME_GEARS_SRC})
    target_link_libraries(rime-gears-static ${RIME_GEARS_DEPS})
    add_dependencies(rime-gears-static ${RIME_LIBRARY})
    set_target_properties(rime-gears-static PROPERTIES OUTPUT_NAME "rime-gears" PREFIX "lib")
    install(TARGETS rime-gears-static DESTINATION ${LIB_INSTALL_DIR})
  endif(BUILD_SEPARATE_LIBS)
endif(BUILD_SHARED_LIBS)
