# $Id$
#
# Ultra-flat Makefile for DJGPP (also works for Cygwin).
# Does NOT depend on or use configure.
#
# Works for simple build but *not* for development (no clean, dist, etc).
# Also, WARNING, no header dependencies are included!
#
# How to compile:
#  make -fMkfiles/Makefile.dj

CFLAGS=-DHAVE_CONFIG_H -IMkfiles/dj -O -I.
CC?=gcc
BUILDCC?=$(CC)

all: yasm

LIBYASM_OBJS= \
 libyasm/arch.o \
 libyasm/assocdat.o \
 libyasm/bitvect.o \
 libyasm/bc-align.o \
 libyasm/bc-data.o \
 libyasm/bc-incbin.o \
 libyasm/bc-insn.o \
 libyasm/bc-org.o \
 libyasm/bc-reserve.o \
 libyasm/bytecode.o \
 libyasm/errwarn.o \
 libyasm/expr.o \
 libyasm/file.o \
 libyasm/floatnum.o \
 libyasm/hamt.o \
 libyasm/intnum.o \
 libyasm/inttree.o \
 libyasm/linemap.o \
 libyasm/md5.o \
 libyasm/mergesort.o \
 libyasm/phash.o \
 libyasm/section.o \
 libyasm/strcasecmp.o \
 libyasm/strsep.o \
 libyasm/symrec.o \
 libyasm/valparam.o \
 libyasm/value.o \
 libyasm/xmalloc.o \
 libyasm/xstrdup.o \
 module.o

MODULES_ARCH_X86_OBJS= \
 modules/arch/x86/x86arch.o \
 modules/arch/x86/x86bc.o \
 modules/arch/x86/x86expr.o \
 modules/arch/x86/x86id.o
YASM_MODULES=arch_x86

#MODULES_ARCH_LC3B_OBJS= \
# modules/arch/lc3b/lc3barch.o \
# modules/arch/lc3b/lc3bbc.o \
# lc3bid.o
#YASM_MODULES+=arch_lc3b

MODULES_ARCH_OBJS= \
 $(MODULES_ARCH_X86_OBJS) \
# $(MODULES_ARCH_LC3B_OBJS)

MODULES_DBGFMTS_OBJS= \
 modules/dbgfmts/null/null-dbgfmt.o \
 modules/dbgfmts/stabs/stabs-dbgfmt.o \
 modules/dbgfmts/codeview/cv-dbgfmt.o \
 modules/dbgfmts/codeview/cv-symline.o \
 modules/dbgfmts/codeview/cv-type.o \
 modules/dbgfmts/dwarf2/dwarf2-dbgfmt.o \
 modules/dbgfmts/dwarf2/dwarf2-line.o \
 modules/dbgfmts/dwarf2/dwarf2-info.o \
 modules/dbgfmts/dwarf2/dwarf2-aranges.o
YASM_MODULES+=dbgfmt_null
YASM_MODULES+=dbgfmt_stabs
YASM_MODULES+=dbgfmt_cv8
YASM_MODULES+=dbgfmt_dwarf2

MODULES_LISTFMTS_OBJS= \
 modules/listfmts/nasm/nasm-listfmt.o
YASM_MODULES+=listfmt_nasm

MODULES_OBJFMTS_OBJS= \
 modules/objfmts/dbg/dbg-objfmt.o \
 modules/objfmts/bin/bin-objfmt.o \
 modules/objfmts/coff/coff-objfmt.o \
 modules/objfmts/coff/win64-except.o \
 modules/objfmts/elf/elf.o \
 modules/objfmts/elf/elf-x86-x86.o \
 modules/objfmts/elf/elf-x86-amd64.o \
 modules/objfmts/elf/elf-objfmt.o \
 modules/objfmts/macho/macho-objfmt.o \
 modules/objfmts/rdf/rdf-objfmt.o \
 modules/objfmts/xdf/xdf-objfmt.o
YASM_MODULES+=objfmt_dbg
YASM_MODULES+=objfmt_bin
YASM_MODULES+=objfmt_coff objfmt_win32 objfmt_win64 objfmt_x64
YASM_MODULES+=objfmt_elf objfmt_elf32 objfmt_elf64
YASM_MODULES+=objfmt_macho objfmt_macho32 objfmt_macho64
YASM_MODULES+=objfmt_rdf
YASM_MODULES+=objfmt_xdf

MODULES_PARSERS_OBJS= \
 modules/parsers/nasm/nasm-parser.o \
 modules/parsers/nasm/nasm-parse.o \
 nasm-token.o \
 modules/parsers/gas/gas-parser.o \
 modules/parsers/gas/gas-parse.o \
 gas-token.o
YASM_MODULES+=parser_nasm
YASM_MODULES+=parser_gas parser_gnu

MODULES_PREPROCS_NASM_OBJS= \
 modules/preprocs/nasm/nasm-eval.o \
 modules/preprocs/nasm/nasm-pp.o \
 modules/preprocs/nasm/nasm-preproc.o \
 modules/preprocs/nasm/nasmlib.o
YASM_MODULES+=preproc_nasm

MODULES_PREPROCS_RAW_OBJS = \
 modules/preprocs/raw/raw-preproc.o
YASM_MODULES+=preproc_raw

MODULES_PREPROCS_OBJS = \
 $(MODULES_PREPROCS_NASM_OBJS) \
 $(MODULES_PREPROCS_RAW_OBJS)

MODULES_OBJS = \
 $(MODULES_ARCH_OBJS) \
 $(MODULES_DBGFMTS_OBJS) \
 $(MODULES_LISTFMTS_OBJS) \
 $(MODULES_OBJFMTS_OBJS) \
 $(MODULES_OPTIMIZERS_OBJS) \
 $(MODULES_PARSERS_OBJS) \
 $(MODULES_PREPROCS_OBJS)

YASM_OBJS= \
 frontends/yasm/yasm.o \
 frontends/yasm/yasm-options.o \
 $(LIBYASM_OBJS) \
 $(MODULES_OBJS)

genstring: genstring.c
	$(BUILDCC) -o $@ $<

license.c: COPYING genstring
	./genstring license_msg $@ COPYING

frontends/yasm/yasm.c: license.c

genmacro: modules/preprocs/nasm/genmacro.c
	$(BUILDCC) -o $@ $<

nasm-macros.c: modules/preprocs/nasm/standard.mac version.mac genmacro
	./genmacro modules/preprocs/nasm/standard.mac version.mac

modules/preprocs/nasm/nasm-pp.c: nasm-macros.c

genversion: modules/preprocs/nasm/genversion.c
	$(BUILDCC) -IMkfiles/dj -o $@ $<

version.mac: genversion
	./genversion $@

genmodule: libyasm/genmodule.c
	$(BUILDCC) -o $@ $<

module.c: libyasm/module.in genmodule
	./genmodule libyasm/module.in Mkfiles/Makefile.dj

x86parse.c: modules/arch/x86/x86parse.gap gap
	./gap modules/arch/x86/x86parse.gap $@

modules/arch/x86/x86id.c: x86parse.c

lc3bid.c: modules/arch/lc3b/lc3bid.re re2c
	./re2c -s -o $@ modules/arch/lc3b/lc3bid.re

gas-token.c: modules/parsers/gas/gas-token.re re2c
	./re2c -b -o $@ modules/parsers/gas/gas-token.re

nasm-token.c: modules/parsers/nasm/nasm-token.re re2c
	./re2c -b -o $@ modules/parsers/nasm/nasm-token.re

RE2C_SRCS= \
	tools/re2c/main.c \
	tools/re2c/code.c \
	tools/re2c/dfa.c \
	tools/re2c/parser.c \
	tools/re2c/actions.c \
	tools/re2c/scanner.c \
	tools/re2c/mbo_getopt.c \
	tools/re2c/substr.c \
	tools/re2c/translate.c

re2c: $(RE2C_SRCS)
	$(BUILDCC) -I. -o re2c $(RE2C_SRCS)

GAP_SRCS= \
	tools/gap/gap.c \
	tools/gap/perfect.c \
	libyasm/phash.c \
	libyasm/xmalloc.c \
	libyasm/xstrdup.c

gap: $(GAP_SRCS)
	$(BUILDCC) -I. -o gap $(GAP_SRCS)

yasm: $(YASM_OBJS)
	$(CC) -o yasm $(YASM_OBJS)

.c.o:
	$(CC) -c $(CFLAGS) -o $@ $<

