#! /bin/sh
#
# freehdl-config - utility for compiling and linking programs with FreeHDL
#
# Copyright (C) 2005, 2008 Stefan Jahn <stefan@lkcc.org>
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this package; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
# Boston, MA 02110-1301, USA.  
#

option="$1"
prefix=`pkg-config --variable prefix freehdl`
exec_prefix=`pkg-config --variable exec_prefix freehdl`
includedir=`pkg-config --variable includedir freehdl`
libdir=`pkg-config --variable libdir freehdl`
version=`pkg-config --modversion freehdl`
cxxflags=`pkg-config --variable cxxflags freehdl`
cxx=`pkg-config --variable cxx freehdl`
linker=`pkg-config --variable libtoo
  (@ "struct tree_chunk_info ~a_chunk_info = {" (chunk-name ch))
  (@ "  -1,")      ; chunk_id, will be filled in by tree_register_chunk
  (@ "  \"~a\"," (chunk-name ch))
  (@ "  ~a," (length (chunk-nodes ch)))
  (@ "  ~a_kinds" (chunk-name ch))
  (@ "};")
  (@ "")
  (@ "void init_~a_chunk ()" (chunk-name ch))
  (@ "{")
  (@ "  if (~a_chunk_info.chunk_id != -1)" (chunk-name ch))
  (@ "    return;")
  (pk 'used-chunks (map chunk-name used-chunks))
  (do-list used-ch used-chunks
    (@ "  init_~a_chunk ();" (chunk-name used-ch)))
  (@ "")
  (@ "  tree_register_chunk (&~a_chunk_info);" (chunk-name ch))
  (do-list gen (reverse (chunk-generics ch))
    (@ "  ~a_generic_~a.init (\"~a\");"
       (chunk-name ch) (generic-id gen) (generic-name gen)))
  (do-list meth (reverse (chunk-methods ch))
    (let ((gen (methods-generic meth))
	  (n-chunks (length (uniq (map node-chunk (methods-nodes meth))))))
      (@ "  ~a_generic_~a.merge (~a, ctab_~a);"
	 (chunk-name (generic-chunk gen)) (generic-id gen)