/*
 * Copyright (c) 2002, The Tendra Project <http://www.ten15.org/>
 * 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 unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 *    		 Crown Copyright (c) 1997
 *
 *    This TenDRA(r) Computer Program is subject to Copyright
 *    owned by the United Kingdom Secretary of State for Defence
 *    acting through the Defence Evaluation and Research Agency
 *    (DERA).  It is made available to Recipients with a
 *    royalty-free licence for its use, reproduction, transfer
 *    to other parties and amendment for any purpose not excluding
 *    product development provided that any such use et cetera
 *    shall be deemed to be acceptance of the following conditions:-
 *
 *        (1) Its Recipients shall ensure that this Notice is
 *        reproduced upon any copies or amended versions of it;
 *
 *        (2) Any amended version of it shall be clearly marked to
 *        show both the nature of and the organisation responsible
 *        for the relevant amendment or amendments;
 *
 *        (3) Its onward transfer from a recipient to another
 *        party shall be deemed to be that party's acceptance of
 *        these conditions;
 *
 *        (4) DERA gives no warranty or assurance as to its
 *        quality or suitability for any purpose and DERA accepts
 *        no liability whatsoever in relation to any use to which
 *        it may be put.
 *
 * $TenDRA: tendra/src/tools/tspec/makefile.c,v 1.12 2005/11/06 10:07:02 stefanf Exp $
 */


#include "config.h"
#include "fmm.h"
#include "msgcat.h"
#include "tenapp.h"

#include "object.h"
#include "hash.h"
#include "name.h"
#include "str_array.h"
#include "type.h"
#include "utility.h"


/*
 *    IS A C FILE NEEDED IN THE MAKEFILE?
 *
 *    This routine tests whether a C file with information i is needed in the
 *    makefile for the given api.
 */

static boolean
need_cfile(const info *i, const char *api)
{
	if (restrict_depth && !streq (api, i->api)) return (0);
	return (i->implemented && i->tokens && i->src);
}


/*
 *    IS A HEADER FILE NEEDED IN THE MAKEFILE?
 *
 *    This routine tests whether a header file with information i is needed
 *    in the makefile for the given api.
 */

static boolean
need_header(const info *i, const char *api)
{
	if (restrict_depth && !streq (api, i->api)) return (0);
	return (i->implemented);
}


/*
 *    PRINT A MAKEFILE
 *
 *    This routine prints a makefile to build a TDF library for the API api
 *    from the list of files f.
 */

void
print_makefile(char *api, hash_elem *f)
{
	struct str_array sa;
	char *nm;
	FILE *output;
	hash_elem *e;
	char *api2 = hack_name (api, "_Aa0");
	int ls = output_src_len;
	hash_table *subdirs = make_hash_table("Subdirs");
	int idx, firstline, size;

	/* Open output file */
	nm = MAKEFILE;
	nm = string_printf (nm, output_src_dir, api, api2);
	if (verbose > 1) IGNORE printf ("Creating %s ...", nm);
	create_dir (nm);
	output = fopen (nm, "w");
	if (output == null) {
		MSG_cant_open_output_file (nm);
		return;
	}

	/* Print file header */
	IGNORE fprintf (output, "# AUTOMATICALLY GENERATED BY %s %s\n",
					progname, progvers);
	IGNORE fprintf (output, "# MAKEFILE FOR %s API\n\n", api);

	IGNORE fprintf (output, "TL=\t%s.tl\n\n", api);
	IGNORE fprintf (output,
"TCCFLAGS+=\t-f${SRC_DIR}/src/lib/machines/${SRC_MACHINES}/startup/%s.h\n\n",
		api);

	/* Print the list of files */
	IGNORE fputs ("SRCS=\t", output);
	str_array_init (&sa);
	for (e = f; e != null; e = e->next) {
		info *i = e->obj->u.u_info;
		if (need_cfile (i, api)) str_array_add (&sa, basename (i->src));
	}

	str_array_sort (&sa);
	size = str_array_size (&sa);
	for (idx = 0; idx < size; idx++) {
		IGNORE fprintf (output, "%s%s", idx == 0 ? "" : " \\\n\t",
			str_array_get (&sa, idx));
	}
	IGNORE fputs ("\n\n", output);

	/* Print all headers we want to install */
	IGNORE fprintf (output, "HEADERS=");
	str_array_clear (&sa);
	for (e = f; e != null; e = e->next) {
		info *i = e->obj->u.u_info;
		char *p;
		object *o;

		if (!need_header (i, api))
			continue;

		if (i->file == NULL || strstr(i->file, "dummy") != NULL ||
			i->api == NULL  || strstr(i->api, "dummy") != NULL)
			continue;

		str_array_add (&sa, i->incl + ls);

		buffer[0] = '\0';
		IGNORE strncat(buffer, i->incl + ls, buffsize);
		if ((p = strrchr(buffer, '/')) != NULL) {
			*p = '\0';
			/* Add the directory if it isn't in the subdirs hash table yet */
			if (search_hash(subdirs, buffer, 0) == NULL) {
				p = xalloc(strlen(buffer) + 1);
				IGNORE strcpy(p, buffer);
				o = make_object(p, TYPE_VOID);
				IGNORE add_hash(subdirs, o, 0);
			}
		}
	}
	str_array_sort (&sa);
	size = str_array_size (&sa);
	for (idx = 0; idx < size; idx++) {
		IGNORE fprintf (output, "%s%s", idx == 0 ? "" : " \\\n\t",
			str_array_get (&sa, idx));
	}
	IGNORE fputs ("\n\n", output);

	/* Print the list of subdirectories we have to create */
	IGNORE fputs ("DIRS=\t", output);
	firstline = 1;
	for (idx = 0; idx < ARRAY_SIZE (subdirs->array); idx++)
		for (e = subdirs->array[idx]; e != null; e = e->next) {
			IGNORE fprintf (output, "%s%s", firstline ? "" : " \\\n\t",
			   	e->obj->name);
			firstline = 0;
		}
	IGNORE fputs ("\n", output);

	str_array_destroy (&sa);
	IGNORE fclose (output);
	return;
}


syntax highlighted by Code2HTML, v. 0.9.1