# This file is part of the c2cweb package Version 1.5 (10-Nov-1996)

# GNU Makefile for c2cweb

# you should use the CWEAVE program of this package

.PHONY: default all debug documentation clean \
        do_all cweb
.CAUTIOUS: c2cweb.c


default:
	@echo " say                                "
	@echo ".                                   "
	@echo "     make <TARGET> OS=<SYSTEM>      "
	@echo ".                                   "
	@echo " SYSTEM can be one of the following:"
	@echo ".                                   "
	@echo "     unix (gcc)                     "
	@echo "     os2 bound (emx)                "
	@echo "     dos (djgpp)                    "
	@echo ".                                   "
	@echo " TARGET can be one of the following:"
	@echo ".                                   "
	@echo "     all debug                      "
	@echo "     documentation                  "
	@echo "     clean                          "


ifdef OS

  ifeq ($(OS),unix)
    CC = gcc -Wall -O
    O = .o
    LIB =
    EXE =
    CP = cp
    RM = rm -f
  endif

  ifeq ($(OS),os2)
    CC = gcc -Wall -Zomf -Zmtd -O
    O = .obj
    LIB = c2cweb.def
    EXE = .exe
    CP = copy
    RM = del
  endif

  ifeq ($(OS),dos)
    CC = gcc -Wall -O
    O = .o
    LIB =
    EXE = .exe
    CP = copy
    RM = del
  endif

  ifeq ($(OS),bound)
    CC = gcc -Wall -O
    O = .o
    LIB =
    EXE = .exe
    CP = copy
    RM = del
  endif


  ifeq ($(OS),unix)
    define sub_make
      cd cweb; make $@
    endef
  else
    define sub_make
      cd cweb
      make $@
      cd ..
    endef
  endif


  OBJ = c2cweb$O
  OUR_CWEAVE = cweb/cweave

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

  %.c: %.w %.ch
	$(CTANGLE) $^ $@
  %.c: %.w
	$(CTANGLE) $<

  %.tex: %.w %.ch
	$(OUR_CWEAVE) +ai $^ $@
  %.tex: %.w
	$(OUR_CWEAVE) +ai $<


  all:
	$(MAKE) do_all CFLAGS=-s

  debug:
	$(MAKE) do_all CFLAGS=-g

  # this builds a .dvi-file
  # for .dvi files of cweave etc. please get the original CWEB package
  documentation: c2cweb.dvi

  # remove the unnecessary files.
  clean:
	$(sub_make)
	-$(RM) *$O
	-$(RM) *.tex
	-$(RM) *.scn
	-$(RM) *.toc
	-$(RM) *.idx
	-$(RM) *.log


  do_all: cweb c2cweb$(EXE)

  cweb:
	$(sub_make)

  c2cweb$(EXE): $(OBJ)
    ifeq ($(OS),dos)
	$(CC) $(CFLAGS) -o $(basename $@) $^
	strip $(basename $@)
	coff2exe $(basename $@)
	-$(RM) $(basename $@)
    else
	$(CC) $(CFLAGS) -o $@ $^ $(LIB)
    endif

  c2cweb.dvi: c2cweb.tex cweb/cwebmaca.tex
	-$(RM) cwebmaca.tex
	$(CP) cweb/cwebmaca.tex .
	$(TEX) c2cweb

else # ifdef OS

  all debug documentation clean: default

endif

# end of Makefile