<'
#
# Copyright (c) 2010-2014, 2019-2020, 2022-2023, 2026 LAAS/CNRS
# All rights reserved.
#
# Redistribution  and  use  in  source  and binary  forms,  with  or  without
# modification, are permitted provided that the following conditions are met:
#
#   1. Redistributions of  source  code must retain the  above copyright
#      notice and this list of conditions.
#   2. Redistributions in binary form must reproduce the above copyright
#      notice and  this list of  conditions in the  documentation and/or
#      other materials provided with the distribution.
#
# 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 Thu Jan 28 2010
#

# check arguments
if {[llength $argv] != 4} {
  error "expected argument: components externs input lang"
}
lassign $argv components externs input lang
lang $lang

# generate copyright notice (if any)
if {![catch {dotgen input notice} text]} {
  puts #[join [split $text "\n"] "\n#"]
}

# compare two object by name
proc namecmp {a b} { string compare [$a name] [$b name] }

'>
lib_LTLIBRARIES =

<'set hlist [dict create]'>
<'foreach c $components {'>
<'  set e [expr {[llength $externs] ? $externs : $c}]'>
<'  set if [$c lang]'>
<'  set h [$c name]_${if}_types[fileext header]'>
<'  dict set hlist $h $if'>
<'  set tasks [list]'>
<'  foreach codel [$c codels fsm] {'>
<'    if {[$codel loc context] ni $e} continue'>
<'    set t [$codel task]'>
<'    if {$t ni $tasks} { lappend tasks $t }'>
<'  }'>
# <"[$c name]"> codels library
lib_LTLIBRARIES += lib<"[$c name]">_codels.la

lib<"[$c name]">_codels_la_SOURCES  =	<"$h">
lib<"[$c name]">_codels_la_SOURCES +=	<"[$c name]_codels[fileext]">
<'  foreach t [lsort -command namecmp $tasks] {'>
lib<"[$c name]">_codels_la_SOURCES +=	<"[$c name]_[$t name]_codels[fileext]">
<'  }'>

lib<"[$c name]">_codels_la_CPPFLAGS =	$(requires_CFLAGS)
lib<"[$c name]">_codels_la_LIBADD   =	$(requires_LIBS)
lib<"[$c name]">_codels_la_CPPFLAGS+=	$(codels_requires_CFLAGS)
lib<"[$c name]">_codels_la_LIBADD  +=	$(codels_requires_LIBS)
lib<"[$c name]">_codels_la_LDFLAGS  =	-release $(PACKAGE_VERSION)

<'}'>

# idl mappings
BUILT_SOURCES=	<"[dict keys $hlist]">
CLEANFILES=	${BUILT_SOURCES}
<'dict for {h l} $hlist {'>

<"$h">: ${top_srcdir}/<"$input">
	${GENOM3} <"[dotgen genom cmdline]"> mappings \
	  -MD -MF .deps/$@.d -MT $@ --signature -l <"$l"> $< >$@

-include .deps/<"$h">.d
<'}'>
