cmake_minimum_required (VERSION 3.0)

project (procd)

ADD_DEFINITIONS(-Os -ggdb -Wall -Werror --std=gnu99 -Wmissing-declarations)

set (CMAKE_MODULE_PATH "${MODULE_PATH}")

set(DISABLE_TARGET_OPTIMIZATION ON)

include (br)

file (
	GLOB_RECURSE
	init_source_files
	
	$ENV{SRC_DIR}/src/3P/procd/initd/init.c
	$ENV{SRC_DIR}/src/3P/procd/initd/early.c
	$ENV{SRC_DIR}/src/3P/procd/initd/preinit.c
	$ENV{SRC_DIR}/src/3P/procd/initd/mkdev.c
	$ENV{SRC_DIR}/src/3P/procd/watchdog.c
	$ENV{SRC_DIR}/src/3P/procd/utils/utils.c
	)

# Init
IF(NOT DISABLE_INIT)
add_executable (init ${init_source_files})
target_link_libraries (init ubox ubus)

install (TARGETS init RUNTIME DESTINATION ../sbin)
ENDIF()

# procd
file (
	GLOB_RECURSE
	procd_source_files

	$ENV{SRC_DIR}/src/3P/procd/procd.c
	$ENV{SRC_DIR}/src/3P/procd/signal.c
	$ENV{SRC_DIR}/src/3P/procd/state.c
	$ENV{SRC_DIR}/src/3P/procd/inittab.c
	$ENV{SRC_DIR}/src/3P/procd/rcS.c
	$ENV{SRC_DIR}/src/3P/procd/ubus.c
	$ENV{SRC_DIR}/src/3P/procd/system.c
	$ENV{SRC_DIR}/src/3P/procd/service/service.c
	$ENV{SRC_DIR}/src/3P/procd/service/instance.c
	$ENV{SRC_DIR}/src/3P/procd/service/validate.c
	$ENV{SRC_DIR}/src/3P/procd/service/trigger.c
	$ENV{SRC_DIR}/src/3P/procd/service/watch.c
	$ENV{SRC_DIR}/src/3P/procd/utils/utils.c
	)

IF(DISABLE_INIT)
	ADD_DEFINITIONS(-DDISABLE_INIT)
ENDIF()

IF(NOT DISABLE_INIT)
list (APPEND procd_source_files

	$ENV{SRC_DIR}/src/3P/procd/watchdog.c
	$ENV{SRC_DIR}/src/3P/procd/plug/coldplug.c
	$ENV{SRC_DIR}/src/3P/procd/plug/hotplug.c
)
ENDIF()

IF(EARLY_PATH)
  ADD_DEFINITIONS(-DEARLY_PATH="${EARLY_PATH}")
ENDIF()

IF(DEBUG)
  ADD_DEFINITIONS(-DUDEV_DEBUG -g3)
ENDIF()

add_executable (procd ${procd_source_files})
target_link_libraries (procd ubox ubus json-c blobmsg_json json_script)

install (TARGETS procd RUNTIME DESTINATION ../sbin)

# udevtrigger
add_executable (udevtrigger $ENV{SRC_DIR}/src/3P/procd/plug/udevtrigger.c)
install (TARGETS udevtrigger
	RUNTIME DESTINATION ../sbin)
