cmake_minimum_required(VERSION 3.0)

project (ubus)

set (CMAKE_MODULE_PATH "${MODULE_PATH}")

set(DISABLE_TARGET_OPTIMIZATION ON)

include (br)

include_directories ($ENV{SRC_DIR}/src/3P/ubus)

ADD_DEFINITIONS (-Werror --std=gnu99 -Wmissing-declarations -D_GNU_SOURCE)

ADD_DEFINITIONS (-DUBUS_MAX_MSGLEN=1048576)
ADD_DEFINITIONS (-DUBUS_UNIX_SOCKET="/tmp/ubus.sock")

# ubus library
file (
	GLOB_RECURSE
	source_files
	
	$ENV{SRC_DIR}/src/3P/ubus/libubus.c
	$ENV{SRC_DIR}/src/3P/ubus/libubus-io.c
	$ENV{SRC_DIR}/src/3P/ubus/libubus-obj.c
	$ENV{SRC_DIR}/src/3P/ubus/libubus-sub.c
	$ENV{SRC_DIR}/src/3P/ubus/libubus-req.c
	$ENV{SRC_DIR}/src/3P/ubus/libubus-acl.c
	)

# Library
add_library (ubus SHARED ${source_files})
target_link_libraries (ubus LINK_PUBLIC ubox)

target_include_directories (ubus PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

install (TARGETS ubus LIBRARY DESTINATION local/lib)

file (GLOB ubus_headers $ENV{SRC_DIR}/src/3P/ubus/*.h)
install (FILES ${ubus_headers} DESTINATION include/ubus)


# Daemon ubusd
file (
	GLOB_RECURSE
	ubusd_source_files
	
	$ENV{SRC_DIR}/src/3P/ubus/ubusd.c
	$ENV{SRC_DIR}/src/3P/ubus/ubusd_id.c
	$ENV{SRC_DIR}/src/3P/ubus/ubusd_obj.c
	$ENV{SRC_DIR}/src/3P/ubus/ubusd_proto.c
	$ENV{SRC_DIR}/src/3P/ubus/ubusd_event.c
	$ENV{SRC_DIR}/src/3P/ubus/ubusd_acl.c 
	$ENV{SRC_DIR}/src/3P/ubus/ubusd_monitor.c
	)

# Daemon ubusd
add_executable (ubusd ${ubusd_source_files})
target_link_libraries (ubusd ubox blobmsg_json)

install (TARGETS ubusd RUNTIME DESTINATION ../sbin)

# cmd line tool.
add_executable (ucli $ENV{SRC_DIR}/src/3P/ubus/cli.c)
set_target_properties (ucli PROPERTIES OUTPUT_NAME ubus)
target_link_libraries (ucli LINK_PUBLIC ubox ubus blobmsg_json)

install (TARGETS ucli RUNTIME DESTINATION bin)