append /local/rnd/CFLAGS [@-I. -I.. -I@/local/pcb/librnd_prefix@/include/librnd4 -I@/local/pcb/librnd_prefix@/include/librnd4/librnd/src_3rd @?/local/pcb/librnd_extra_inc@ -I../src_3rd -I../src -I../src_3rd/liblihata @]
append /local/rnd/LDFLAGS [@@?/local/pcb/librnd_extra_ldf@@]
append /local/rnd/LDFLAGS cc/ldflags
append /local/rnd/LDFLAGS cc/rdynamic
append /local/rnd/LDFLAGS ?/local/pcb/cflags_profile
append /local/rnd/LDFLAGS ?libs/script/fungw/ldflags
append /local/rnd/LDFLAGS ?/local/librnd/fungw/ldflags
put /local/rnd/DEPCFLAGS {}
put /local/pcb/OBJS_3RDLIB {}

if /local/pcb/want_static then
	append /local/rnd/LDFLAGS { -static}
end

# static librnd link: need to get all librnd ldlibs
if /local/pcb/want_static_librnd
then
	put /local/pcb/librnd_extra_ldlibs {$(LDFLAGS_LIBRND)}
else
	put /local/pcb/librnd_extra_ldlibs {}
end

if /local/pcb/want_static_librnd then
	put /local/pcb/SO {.a}
else
	put /local/pcb/SO {.so}
end

# These files are to be generated before make dep is run
put /local/rnd/DEPDEPS { conf_core_fields.h conf_internal.c defpcb_internal.c buildin.c pcb_menu_default.c }

include [@@/local/pcb/librnd_template@/debug.tmpasm@]

# main: objects
# hidlib will be a library that provides:
#  - the plugin system
#  - the conf system
#  - some basic/generic types like pcb_coord_t
#  - the HID infra, including DAD
# but nothing else; in short the HID part of pcb-rnd for developing a
# GUI program based on pcb-rnd HIDs.

put /local/pcb/OBJS_C99 {}
put /local/pcb/OBJS {}
append /local/pcb/OBJS /local/pcb/OBJS_3RDLIB
append /local/pcb/OBJS [@
	actions_pcb.o
	attrib.o
	board.o
	brave.o
	buffer.o
	build_run.o
	change.o
	change_act.o
	conf_act.o
	conf_core.o
	conf_internal.o
	crosshair.o
	data.o
	draw.o
	drc.o
	event.o
	extobj.o
	extobj_act.o
	font.o
	font_act.o
	file_act.o
	find.o
	flag.o
	flag_str.o
	gui_act.o
	hid_cam.o
	ht_pstk.o
	ht_subc.o
	idpath.o
	insert.o
	layer.o
	layer_addr.o
	layer_grp.o
	layer_ui.o
	layer_vis.o
	main.o
	main_act.o
	move.o
	netlist.o
	netlist_act.o
	object_act.o
	obj_common.o
	obj_arc.o
	obj_arc_list.o
	obj_arc_ui.o
	obj_gfx.o
	obj_gfx_list.o
	obj_line.o
	obj_line_drcenf.o
	obj_line_list.o
	obj_pstk.o
	obj_pstk_act.o
	obj_pstk_list.o
	obj_pstk_proto.o
	obj_pinvia_therm.o
	obj_poly.o
	obj_poly_list.o
	obj_rat.o
	obj_rat_list.o
	obj_subc.o
	obj_subc_hash.o
	obj_subc_list.o
	obj_term.o
	obj_text.o
	obj_text_list.o
	operation.o
	pcb_menu_default.o
	pcb_minuid.o
	pixmap_pcb.o
	plug_import.o
	plug_io.o
	plug_footprint.o
	plug_footprint_act.o
	polygon.o
	polygon_act.o
	rats_act.o
	rats_patch.o
	remove.o
	remove_act.o
	rotate.o
	route.o
	route_style.o
	search.o
	select.o
	select_act.o
	stub_draw.o
	thermal.o
	tool_logic.o
	undo.o
	undo_act.o
	undo_old.o
	view.o
	vtpadstack.o
	vtpadstack_t.o
	vtr0.o
	vtroutestyle.o
	../src_3rd/qparse/qparse.o
	../src_3rd/libuundo/uundo.o
	../src_3rd/libuundo/uundo_debug.o
@]

put /local/pcb/LIBS_3RD [@
	../src_3rd/libminuid/libminuid.a
@]

put /local/pcb/LIBS /local/pcb/LIBS_3RD

# We do not compile these in the executable but we need rules for these for utils
# e.g. ../src_3rd/genht/htsp.o
put /local/pcb/OBJS_UTIL [@
@]

#---- modules ----#
# extra rules for modules
put /local/rnd/RULES {}
put /local/rnd/CLEANFILES {}
put /local/pcb/CLEANRULES {}
put /local/pcb/EXEDEPS {}
put /local/pcb/all {}
put /local/pcb/buildin_init {}
put /local/rnd/rules/install_ {}
put /local/rnd/rules/install {}
put /local/rnd/rules/linstall {}
put /local/rnd/rules/uninstall {}
put /local/rnd/mod/OBJS {}
put /local/rnd/mod/OBJS_C99 {}
put /local/rnd/mod/CONF {}
put /local/rnd/mod/LDFLAGS {}
put /local/rnd/mod/CFLAGS {}
put /local/rnd/mod/YACC {}
put /local/rnd/mod/LEX {}
put /local/rnd/mod/BYACCIC {}
put /local/rnd/mod/UREGLEX {}

put /local/pcb/tmpasm/buildin         {../src_plugins/Buildin.tmpasm}
put /local/pcb/tmpasm/plugin          {../src_plugins/Plugin.tmpasm}
put /local/pcb/tmpasm/disable         {../src_plugins/Disable.tmpasm}
put /local/pcb/tmpasm/common_enabled  {../src_plugins/Common_enabled.tmpasm}
put /local/pcb/tmpasm/plugin_sphash   [@@/local/pcb/librnd_template@/plugin_sphash.tmpasm@]
put /local/pcb/tmpasm/plugin_conf     [@@/local/pcb/librnd_template@/plugin_conf.tmpasm@]
put /local/pcb/tmpasm/plugin_intconf  [@@/local/pcb/librnd_template@/plugin_intconf.tmpasm@]
put /local/pcb/tmpasm/plugin_intmenu  [@@/local/pcb/librnd_template@/plugin_intmenu.tmpasm@]

include {../src_plugins/plugins_ALL.tmpasm}

# Lesstif requires that -lXm is before -lXt or else there is a runtime BadWindow error
order /local/rnd/LDFLAGS {-lXm} {before} {-lXt}

# ---- logic ----


# Clean up variables
uniq /local/pcb/OBJS
uniq /local/pcb/OBJS_C99
uniq /local/pcb/OBJS_3RDLIB
uniq /local/pcb/OBJS_UTIL
uniq /local/rnd/CFLAGS
uniq /local/pcb/CFLAGS_GENERIC
uniq /local/pcb/LIBS
uniq /local/pcb/LIBS_PRE
uniq /local/pcb/LIBS_3RD
uniq /local/pcb/EXEDEPS
uniq /local/rnd/CLEANFILES
uniq /local/rnd/DISTCLEANFILES
uniq /local/pcb/CLEANRULES
uniq /local/pcb/all
put /local/pcb/SRCS /local/pcb/OBJS 
append /local/pcb/SRCS ?/local/pcb/OBJS_C99
append /local/pcb/SRCS ?/local/pcb/OBJS_3RDLIB
gsub /local/pcb/SRCS {.o } {.c }

# apple requires multiple "-framework something" instead of -lsomething
# we need to keep all -frameworks but uniq would keep only the first if it
# is a word on its own
gsub /local/rnd/LDFLAGS {-framework } {-framework-}
uniq /local/rnd/LDFLAGS
gsub /local/rnd/LDFLAGS {-framework-} {-framework }


# workaround: lately gcc doesn't like -ansi and the ((unused)) attribute
# together, which happens in --debug for C99
put /local/pcb/C99CFLAGS /local/rnd/CFLAGS
sub /local/pcb/C99CFLAGS {-ansi } {}

# decide which poly clipping library to use
if /local/pcb/want_polybool
then
	put /local/pcb/polyclip {-lrnd-polybool}
else
	put /local/pcb/polyclip {-lrnd-poly}
end

# Makefile, static
print [@
# *** DO NOT EDIT THIS FILE ***
# This file has been generated from Makefile.in by configure
# *** DO NOT EDIT THIS FILE ***

ROOT=..

# librnd installation
LIBRND_PREFIX=@/local/pcb/librnd_prefix@
include $(LIBRND_PREFIX)/share/librnd4/librnd.mak
LIBRND_L=$(LIBRND_SODIR)


# plugin source
PLUGDIR=$(ROOT)/src_plugins

# plugin source install - so that pcb-rnd runs from source properly
PLUGIDIR=plugins

# src_3rd dir for the lib_ wrapper plugins
SRC_3RD_DIR=$(ROOT)/src_3rd

@?/local/pcb/TOPVARS@
OBJS=@/local/pcb/OBJS@
OBJS_C99=@/local/pcb/OBJS_C99@
OBJS_3RDLIB=@/local/pcb/OBJS_3RDLIB@ ../src_3rd/libminuid/*.o
OBJS_UTIL=@/local/pcb/OBJS_UTIL@
SRCS=@/local/pcb/SRCS@
CFLAGS=@?cc/argstd/std_c99@ @/local/pcb/C99CFLAGS@
C89FLAGS=@/local/pcb/c89flags@ @/local/rnd/CFLAGS@
LDFLAGS=@/local/rnd/LDFLAGS@
LIBS_PRE=@/local/pcb/LIBS_PRE@
LIBS=@/local/pcb/LIBS@ @libs/socket/socket/ldflags@ @/local/pcb/librnd_extra_ldlibs@ -lm @?/target/libs/ldl@
LIBS_3RD=@/local/pcb/LIBS_3RD@
EXEDEPS=@/local/pcb/EXEDEPS@
CLEANFILES=@/local/rnd/CLEANFILES@
CLEANRULES=@/local/pcb/CLEANRULES@
CC=@/target/cc/cc@
AR=@/target/fstools/ar@
HOSTCC=@/host/cc/cc@
LIBMINUID_CFLAGS=@/local/pcb/CFLAGS_GENERIC@
LIBMINUID_LDFLAGS=@cc/ldflags@
GENLIST_CFLAGS=@/local/pcb/CFLAGS_GENERIC@
GENLIST_LDFLAGS=@cc/ldflags@
SO=@/local/pcb/SO@
POLYCLIP=@/local/pcb/polyclip@

all:
	$(MAKE) revcheck
	$(MAKE) all_exe

include $(ROOT)/Makefile.conf

revcheck:
	cd $(ROOT)/scconfig && ./revtest Rev.stamp < Rev.tab

all_exe: pcb-rnd$(EXE) @/local/pcb/all@

pcb-rnd$(EXE): $(OBJS) $(OBJS_C99) $(EXEDEPS) $(LIBS_3RD) $(LIBRND_L)/librnd-hid$(SO) $(LIBRND_L)/librnd-poly$(SO) $(LIBRND_L)/librnd-3rd$(SO)
	$(CC) $(OBJS) $(OBJS_C99) -o pcb-rnd$(EXE) $(LIBS_PRE) \
		-lrnd-hid -lrnd-font $(POLYCLIP)  -lrnd-core -lrnd-3rd \
		$(LDFLAGS) $(LIBS)

### pcb-rnd infra ###

buildin.c: $(PLUGDIR)/.builtin.pups $(PUPLUG)
	cd $(PLUGDIR) && $(PUPLUG) buildin.c "-" < $(PLUGDIR)/.builtin.pups > $(ROOT)/src/buildin.c

buildin.h: $(PLUGDIR)/.builtin.pups $(PUPLUG)
	$(PUPLUG) buildin.h > buildin.h

../src_3rd/libminuid/libminuid.a: ../src_3rd/libminuid/libminuid.c ../src_3rd/libminuid/libminuid.h
	cd  ../src_3rd/libminuid && $(MAKE) libminuid.a LIBMINUID_CFLAGS="$(LIBMINUID_CFLAGS)" LIBMINUID_LDFLAGS="$(LIBMINUID_LDFLAGS)" CC=$(CC)
@]

print [@
##### module rules begin #####

@/local/rnd/RULES@

##### module rules end #####

### default menu embed
pcb_menu_default.c: menu-default.lht $(CQUOTE)
	$(CQUOTE) -n rnd_hidlib_default_embedded_menu <menu-default.lht > pcb_menu_default.c

pcblib_DATA= \
	default_font \
	default2.lht \
	default4.lht \
	menu-default.lht

EXTRA_DIST= \
	default_font \
	default2.lht \
	default4.lht \
	pcbtest.sh.in

all-local: pcbtest.sh

conf_core_fields.h: conf_core.h $(ROOT)/doc/conf/tree/rc.html
	AWK=@/host/fstools/awk@ $(GENCONF)  < conf_core.h > conf_core_fields.h

$(ROOT)/doc/conf/tree/rc.html: conf_core.h
	cat conf_core.h | AWK=@/host/fstools/awk@ $(GENCONF) $(ROOT)/doc/conf/tree > /dev/null

conf_internal.c: conf_core.lht $(CQUOTE)
	$(CQUOTE) -n rnd_conf_internal < conf_core.lht > conf_internal.c

defpcb_internal.c: default4.lht
	$(CQUOTE) -n default_pcb_internal < default4.lht > defpcb_internal.c

# do not make this automatic, most users won't need to regenerate font_internal.c
gen_font:
	$(FONT2C) < default_font > font_internal.c

util_deps: $(OBJS) $(OBJS_UTIL)

FORCE:


DISTCLEANFILES = Makefile.depgen core_lists.h buildin.c buildin.h conf_core_fields.h conf_internal.c defpcb_internal.c pcb_menu_default.c ../src_plugins/.builtin.pups @/local/rnd/DISTCLEANFILES@

clean: $(CLEANRULES)
	$(SCCBOX) rm -f -q  pcb-rnd $(OBJS) $(OBJS_C99) $(CLEANFILES)

distclean: FORCE
	$(SCCBOX) rm -f -q  $(DISTCLEANFILES) plugins

install_pcbrnd:
	$(SCCBOX) mkdir -p "$(BINDIR)" "$(DATADIR)" "$(LIBDIR)" "$(LIBDIR)/plugins" "$(CONFDIR)"
	$(SCCBOX) $(HOW) "pcb-rnd$(EXE)" "$(BINDIR)/pcb-rnd$(EXE)"
	$(SCCBOX) $(HOW) "conf_core.lht" "$(CONFDIR)/conf_core.lht"
	$(SCCBOX) $(HOW) "menu-default.lht" "$(CONFDIR)/menu-default.lht"
	$(SCCBOX) $(HOW) "default2.lht" "$(DATADIR)/default2.lht"
	$(SCCBOX) $(HOW) "default4.lht" "$(DATADIR)/default4.lht"
	$(SCCBOX) $(HOW) "default_font" "$(DATADIR)/default_font"@/local/rnd/rules/install_@
@]

print [@

install:
	$(MAKE) install_pcbrnd HOW="install -f"

linstall:
	$(MAKE) install_pcbrnd HOW="linstall -f"

uninstall:
	$(MAKE) install_pcbrnd HOW="uninstall"

map_plugins:
	cd ../src_plugins && PUPLUG=$(PUPLUG) $(LIBRND_LIBDIR)/plugins/map_plugins.sh ../src_plugins
@]

# generate explicit rules for .c -> .o
put /local/comp/OBJS_C89 /local/pcb/OBJS
put /local/comp/OBJS_C99 ?/local/pcb/OBJS_C99
put /local/comp/C99FLAGS {$(CFLAGS)}
put /local/comp/C89FLAGS {$(C89FLAGS)}
include [@@/local/pcb/librnd_template@/compile.tmpasm@]

print [@
# for extern utils:@]

put /local/comp/OBJS_C89 /local/pcb/OBJS_UTIL
put /local/comp/C89FLAGS {$(C89FLAGS)}
include [@@/local/pcb/librnd_template@/compile.tmpasm@]

# generate deps
put /local/dep/CFLAGS /local/rnd/CFLAGS
put /local/dep/SRCS /local/pcb/SRCS

gsub /local/pcb/DEPSRCS {.o } {.c }
append /local/dep/SRCS /local/pcb/DEPSRCS
include [@@/local/pcb/librnd_template@/cdep.tmpasm@]

redir {../src_plugins/.builtin.pups}
print [@# Autogenerated by ./configure - do NOT edit - contains the list of buildins (generated from src/Makefile.in)
@?/local/pcb/buildin_pups@
@]

