#
# NrrdIO: C library for NRRD file IO (with optional compressions)
# Copyright (C) 2009--2026  University of Chicago
# Copyright (C) 2005--2008  Gordon Kindlmann
# Copyright (C) 1998--2004  University of Utah
#
# This software is provided 'as-is', without any express or implied
# warranty.  In no event will the authors be held liable for any
# damages arising from the use of this software.
#
# Permission is granted to anyone to use this software for any
# purpose, including commercial applications, and to alter it and
# redistribute it freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must
#    not claim that you wrote the original software. If you use this
#    software in a product, an acknowledgment in the product
#    documentation would be appreciated but is not required.
#
# 2. Altered source versions must be plainly marked as such, and must
#    not be misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source distribution.
#

### The process of creating the NrrdIO source distribution from a Teem source
### distribution is automated, and is controlled by this GNUmakefile.
###
### First, set environment variable TEEM_SRC_ROOT to root of Teem source: the
### full path to the directory containing `src` (with `src/air`, `src/biff`,
### `src/nrrd` subidrectories). Then run:
###
###    ./0-gen.sh       # for generating regular NrrdIO
###
### or:
###
###    ./0-gen.sh itk   # for generating NrrdIO for ITK
###
### This pre-GNUmakefile file is the single place where the required Teem source
### files are listed, and this file is only used for creating the NrrdIO source
### distribution (not for compiling it)

$(if $(TEEM_SRC_ROOT),,\
$(warning *)\
$(warning * Environment variable TEEM_SRC_ROOT not set. This)\
$(warning * needs to be set to the directory containing the)\
$(warning * src and include directories of the Teem source)\
$(warning * distribution (rather than a Teem installation))\
$(warning *)\
$(error Make quitting))

### Like Teem, we rely on clang-format for auto-formatting sources
### (The .clang-format is copied from Teem)
CLANG_FORMAT = clang-format --style=file --assume-filename=foo.c

### The following sources are pretty much stubs, to create the symbols,
### but not the functionality of different formats and the bzip2 encoding.
### As such, they are NOT copied from Teem but are made for NrrdIO.
###
NEED_NOT = formatEPS.c formatPNG.c formatPNM.c \
  formatText.c formatVTK.c

# Gone in TeemV2: TEEM_HDRS = $(addprefix teem/, teemDio.h teemPng.h teemQnanhibit.h)

### NEED_{AIR,BIFF,NRRD}: the source files from teem that we need
### NEED_HDRS: the headers that we need
###
NEED_AIR = $(addprefix air/, \
  754.c mop.c array.c parseAir.c \
  sane.c endianAir.c string.c enum.c miscAir.c)
NEED_BIFF = $(addprefix biff/, biffbiff.c biffmsg.c)
NEED_NRRD = $(addprefix nrrd/, \
  accessors.c defaultsNrrd.c enumsNrrd.c arraysNrrd.c methodsNrrd.c \
  reorder.c axis.c simple.c comment.c keyvalue.c endianNrrd.c \
  parseNrrd.c gzio.c read.c write.c format.c formatNRRD.c \
  encodingRaw.c encodingAscii.c encodingHex.c encodingGzip.c encodingZRL.c \
  encodingBzip2.c encoding.c subset.c)
NEED_SRCS = $(NEED_AIR) $(NEED_BIFF) $(NEED_NRRD)
NEED_PUB_HDRS = air/air.h biff/biff.h \
  $(addprefix nrrd/, nrrdDefines.h nrrdEnums.h nrrdMacros.h nrrd.h)
NEED_PRIV_HDRS = air/privateAir.h biff/privateBiff.h nrrd/privateNrrd.h

### Building NrrdIO requires processed sources from air, biff, and nrrd
###
.PHONY: all
ifeq (undefined,$(origin ITK_NRRDIO))
  NRRDIO_H = NrrdIO.h
else
  NRRDIO_H = NrrdIO.h.in
endif
all: $(NEED_SRCS) $(NEED_PRIV_HDRS) $(NRRDIO_H) NrrdIO_Srcs.txt

### NrrdIO.h is basically the result of cat'ing together all the
### teem headers in $(NEED_HDRS), but we do need to "unteem" them.
### In the case of ITK, we have to put this into NrrdIO.h.in, so
### that ITK's CMake can process the #cmakedefine line.  But we still
### need a NrrdIO.h to build the library here, in order to run nm to get
### all the symbols for name mangling
###
$(NEED_PUB_HDRS):
	./tail.py < $(TEEM_SRC_ROOT)/src/$@ \
          | ./unteem.py \
          | grep -v HAS_BEEN_INCLUDED > $(notdir $@)
$(NEED_PRIV_HDRS):
	./tail.py < $(TEEM_SRC_ROOT)/src/$@ \
          | cat preamble.c - \
          | ./unteem.py \
          | grep -v HAS_BEEN_INCLUDED \
          | $(CLANG_FORMAT) > $(notdir $@)
ifeq (undefined,$(origin ITK_NRRDIO))
$(NRRDIO_H): $(NEED_PUB_HDRS)
	cat preamble.c $(notdir $(NEED_PUB_HDRS)) \
          | $(CLANG_FORMAT) > $(NRRDIO_H)
	rm -f $(notdir $(NEED_PUB_HDRS))
else
$(NRRDIO_H): $(NEED_PUB_HDRS)
	cat preamble.c $(notdir $(NEED_PUB_HDRS)) \
          | $(CLANG_FORMAT) > $(NRRDIO_H)
	grep -v cmakedefine NrrdIO.h.in > NrrdIO.h
	rm -f $(notdir $(NEED_PUB_HDRS))
endif

### NrrdIO_Srcs.txt is a list of all the source files that must be
### compiled together to create libNrrdIO
###
NrrdIO_Srcs.txt:
	echo $(notdir $(NEED_SRCS)) $(NEED_NOT) > $@

### The rest: by design, these targets have the library names as part
### of the name (e.g. nrrd/simple.c) and that allows us to locate the
### source file without any VPATH games
###
%.h %.c:
	./tail.py < $(TEEM_SRC_ROOT)/src/$@ \
          | cat preamble.c - \
          | ./unteem.py \
          | grep -v HAS_BEEN_INCLUDED \
          | $(CLANG_FORMAT) > $(notdir $@)
### To start from scratch
###
clean:
	rm -f $(notdir $(NEED_SRCS) \
                       $(NEED_PUB_HDRS) $(NEED_PRIV_HDRS)) \
              NrrdIO.in.h NrrdIO.h NrrdIO_Srcs.txt \
              *.o libNrrdIO.a sampleIO
