<'
#
# Copyright (c) 2012-2015, 2017-2018, 2020-2021, 2023-2024, 2026 LAAS/CNRS
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without   fee is hereby granted, provided   that the above  copyright
# notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS  SOFTWARE INCLUDING ALL  IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR  BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR  ANY DAMAGES WHATSOEVER RESULTING  FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION,   ARISING OUT OF OR IN    CONNECTION WITH THE USE   OR
# PERFORMANCE OF THIS SOFTWARE.
#
#                                            Anthony Mallet on Tue Jan  3 2012
#

# check arguments
if {[llength $argv] != 4} {
  error "expected arguments: component types input names"
}
lassign $argv component types input names

# compute handy shortcuts
set comp [$component name]
'>

ACLOCAL_AMFLAGS=	-I autoconf

# --- ros configuration variables ------------------------------------------

ROS_ENV=

# Store rospack cache locally, since it is architecture dependant
ROS_ENV+= ROS_HOME=${top_builddir}/ros-cache

# ROS_ROOT is normally obsolete, but still required for gendeps
ROS_ENV+= ROS_ROOT=${rospack_prefix}/share/ros

# ROS_PACKAGE_PATH points to the ros hierarchy inside generated sources
ROS_ENV+= \
  ROS_PACKAGE_PATH=${abs_top_srcdir}/<"$comp">_ros:${std_msgs_prefix}/share

# PATH (rospack)
ROS_ENV+= PATH=${PATH}:${rospack_prefix}/bin

# PYTHONPATH (rospkg, roslib) set by configure
ROS_ENV+= PYTHONPATH=${PYTHONPATH}

# LD_LIBRARY_PATH for rospack binary lacking RPATH
ROS_ENV+= LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${rospack_prefix}/lib


# --- component ------------------------------------------------------------

bin_PROGRAMS=	<"$comp">-ros

<"$comp">_ros_SOURCES=\
	src/<"$comp">_c_types.h			\
	src/<"$comp">_typecopy.h		\
	src/<"$comp">_serialize.h		\
	src/<"$comp">_port.h			\
	src/<"$comp">_remote.h			\
	src/<"$comp">_service.h			\
	src/<"$comp">_action.h			\
	src/<"$comp">_internals.h		\
	src/prof.h				\
						\
	src/<"$comp">_main.cc			\
	src/<"$comp">_activities.cc		\
	src/<"$comp">_control-task.cc		\
	src/<"$comp">_control-codels.cc		\
<'foreach t [$component tasks] {'>
	src/<"$comp">_<"[$t name]">-exec-task.cc\
<'}'>
	src/<"$comp">_remote.cc			\
	src/<"$comp">_port.cc			\
	src/<"$comp">_json.cc			\
	src/<"$comp">_exception.cc		\
	src/prof.c

<"$comp">_ros_CFLAGS=\
	${PTHREAD_CFLAGS}
<"$comp">_ros_CXXFLAGS=\
	${PTHREAD_CFLAGS}

<"$comp">_ros_CPPFLAGS=\
	${<"$comp">_CFLAGS}			\
	${genom3_CFLAGS}			\
	${ROS_CFLAGS}				\
	${BOOST_CPPFLAGS}

<"$comp">_ros_LDFLAGS=\
	${ROS_LDFLAGS}

<"$comp">_ros_LDADD=\
	${<"$comp">_LIBS}			\
	${genom3_LIBS}				\
	${ROS_LIBS}				\
	${PTHREAD_LIBS}

# --- regen ----------------------------------------------------------------

.PHONY: regen
$(srcdir)/depend.am regen: <"$input">
	${genom3} <"[dotgen genom cmdline]">		\
	  <"[dotgen template dir]"> -C .		\
	  <"[join [list {*}$input {*}$names] { }]">

include $(srcdir)/depend.am


# --- generated msg and md5 files ------------------------------------------

<'set msgdir ${comp}_ros/msg'>
<'set srvdir ${comp}_ros/srv'>
<'set actdir ${comp}_ros/action'>
<'set actpfix {'>
<'  Action ActionFeedback ActionGoal ActionResult Feedback Goal Result'>
<'}'>
<'set msgfiles [list]'>
<'set md5files [list]'>

<'foreach t $types {'>
<'  if {[catch {$t masquerade ros}]} {'>
<'    lappend md5files $msgdir/[rosmsg_cname $t].msg'>
<'  }'>
<'}'>

<'foreach s [concat [$component services] [$component remotes]] {'>
<'  switch [$s kind] {'>
<'    activity {'>
<'      lappend msgfiles [$s name]'>
<'      foreach p $actpfix {'>
<'        lappend md5files $msgdir/[$s name]$p.msg'>
<'      }'>
<'    }'>
<'    default {'>
<'      lappend md5files $srvdir/[$s name].srv'>
<'    }'>
<'  }'>
<'}'>

genmd5_cmd=\
  ${MSGMD5}								\
    -f '\#define genom_md5_'$${base}' "%s"'				\
    --dcat '\#define genom_dcat_'$${base}' "%s"'			\
    -M -o "$$dst" "$$src"
genaction_cmd=\
  ${GENACTION} -o '<"$msgdir">' "$$src"

genmd5_rule=\
  dst='${@:.d=}';							\
  src="$${dst%.md5}";							\
  base="$${src\#\#*/}"; base="$${base%.srv}"; base="$${base%.msg}";	\
  echo ${genmd5_cmd}; ${ROS_ENV} ${genmd5_cmd}
genaction_rule=\
  echo ${genaction_cmd}; ${ROS_ENV} ${genaction_cmd}

BUILT_SOURCES=

<'foreach m $msgfiles {'>
<'  foreach p $actpfix {'>
BUILT_SOURCES+=	<"$msgdir">/<"$m$p">.msg
<'  }'>

<'foreach p $actpfix {'><"$msgdir">/<"$m$p">.msg <'}'>: \
  <"$actdir">/<"$m">.action
	@src='<"$actdir">/<"$m">.action'; ${genaction_rule}

<'}'>

<'foreach m $md5files {'>
BUILT_SOURCES+=	<"$m">.md5 <"$m">.md5.d

<"$m">.md5 <"$m">.md5.d: <"$m"> ${MSGMD5}
	@${genmd5_rule}

-include <"$m">.md5.d

<'}'>


# --- cleaning -------------------------------------------------------------

CLEANFILES=	${BUILT_SOURCES}

clean-local:
	-rm -rf ${ROS_HOME}
