# ================================================================
# NOTE: This makefile is not intended to be used in a packaging system --
# rather, Miller uses autconfig for that. This makefile is intended for users
# who prefer (for whatever reason) to bypass autoconfig.  Please also see
# http://johnkerl.org/miller/doc/build.html#Without_using_autoconfig
# ================================================================

# ================================================================
# Travis does "export CC=gcc", "export CC=clang" so we can pick those up via
# "make -e" in ../.travis.yml.  Note that "CC?=gcc", without make -e, results
# in CC being expanded to cc on my OSX laptop, which is not OK.  Hence make -e.
CC=gcc
CFLAGS=-std=gnu99
IFLAGS=-I. -I..

WFLAGS=-Wall
# Worth exploring ... but needs handling for unused parameters in functions which comply with interfaces.
# Best option I'm aware of is to replace "void foo(int bar) {...}" with "void foo(int) {...}" throughout.
# WFLAGS=-Wall -Wextra -Werror
# WFLAGS=-Wall -Wextra -pedantic-errors -Werror
# WFLAGS=-Wall -Wextra -pedantic-errors -Werror=unused-variable

LFLAGS=-lm -lpcreposix

# You can do make -e INSTALLDIR=/path/to/somewhere/else/bin
INSTALLDIR=/usr/local/bin

CCOPT=$(CC) $(CFLAGS) $(IFLAGS) $(WFLAGS) -O3
CCDEBUG=$(CC) -g $(CFLAGS) $(IFLAGS) $(WFLAGS)

# clang ASAN. Use -O1 for debug mode to (among other things) disable inlining.
#CCOPT=clang -fsanitize=address -fno-omit-frame-pointer $(CFLAGS) $(IFLAGS) $(WFLAGS)
#CCDEBUG=clang -g -fsanitize=address -fno-omit-frame-pointer $(CFLAGS) $(IFLAGS) $(WFLAGS)

# ----------------------------------------------------------------
# Miller source except DSL
NON_DSL_SRCS = \
  *.c \
  cli/*.c \
  lib/*.c \
  containers/*.c \
  aux/*.c \
  stream/*.c \
  input/*.c \
  dsl/*.c \
  mapping/*.c \
  output/*.c

# DSL
DSL_OBJS = \
  ./parsing/mlr_dsl_parse.o \
  ./parsing/mlr_dsl_lexer.o \
  ./parsing/mlr_dsl_wrapper.o

# Unit-test code
TEST_ARGPARSE_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlrregex.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  cli/argparse.c \
  containers/slls.c \
  containers/sllv.c \
  lib/string_array.c \
  unit_test/test_argparse.c

TEST_BYTE_READERS_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlrescape.c \
  lib/mlr_test_util.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  input/string_byte_reader.c \
  input/stdio_byte_reader.c \
  input/mmap_byte_reader.c \
  unit_test/test_byte_readers.c

TEST_LINE_READERS_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlrescape.c \
  lib/context.c \
  lib/mlr_test_util.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  input/line_readers.c \
  unit_test/test_line_readers.c

TEST_PEEK_FILE_READER_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlr_test_util.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  input/string_byte_reader.c \
  input/peek_file_reader.c \
  unit_test/test_peek_file_reader.c

TEST_LREC_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/context.c \
  lib/mlrdatetime.c \
  lib/mlrescape.c \
  lib/mlrregex.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  lib/string_array.c \
  containers/mlrval.c \
  containers/mvfuncs.c \
  containers/lrec.c \
  containers/header_keeper.c \
  containers/sllv.c \
  containers/slls.c \
  containers/rslls.c \
  containers/lhmsv.c \
  containers/lhmslv.c \
  containers/sllmv.c \
  containers/mlhmmv.c \
  input/line_readers.c \
  input/file_reader_mmap.c \
  input/file_reader_stdio.c \
  input/file_ingestor_stdio.c \
  input/lrec_reader_mmap_csvlite.c \
  input/lrec_reader_stdio_csvlite.c \
  input/lrec_reader_mmap_dkvp.c \
  input/lrec_reader_stdio_dkvp.c \
  input/lrec_reader_mmap_nidx.c \
  input/lrec_reader_stdio_nidx.c \
  input/lrec_reader_mmap_xtab.c \
  input/lrec_reader_stdio_xtab.c \
  input/lrec_reader_mmap_json.c \
  input/lrec_reader_stdio_json.c \
  input/mlr_json_adapter.c \
  input/json_parser.c \
  unit_test/test_lrec.c

TEST_MULTIPLE_CONTAINERS_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/context.c \
  lib/mlrdatetime.c \
  lib/mlrescape.c \
  lib/mlrregex.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  containers/lrec.c \
  containers/header_keeper.c \
  containers/sllv.c \
  containers/slls.c \
  containers/rslls.c \
  lib/string_array.c \
  containers/hss.c \
  containers/mlrval.c \
  containers/mvfuncs.c \
  containers/lhmsi.c \
  containers/lhmsll.c \
  containers/lhmss.c \
  containers/lhmsv.c \
  containers/lhms2v.c \
  containers/lhmslv.c \
  containers/lhmsmv.c \
  containers/loop_stack.c \
  containers/percentile_keeper.c \
  containers/top_keeper.c \
  containers/dheap.c \
  input/line_readers.c \
  input/file_reader_mmap.c \
  input/file_reader_stdio.c \
  input/file_ingestor_stdio.c \
  input/lrec_reader_mmap_csvlite.c \
  input/lrec_reader_stdio_csvlite.c \
  input/lrec_reader_mmap_dkvp.c \
  input/lrec_reader_stdio_dkvp.c \
  input/lrec_reader_mmap_nidx.c \
  input/lrec_reader_stdio_nidx.c \
  input/lrec_reader_mmap_xtab.c \
  input/lrec_reader_stdio_xtab.c \
  input/lrec_reader_mmap_json.c \
  input/lrec_reader_stdio_json.c \
  input/mlr_json_adapter.c \
  input/json_parser.c \
  unit_test/test_multiple_containers.c

TEST_MLHMMV_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlrdatetime.c \
  lib/string_builder.c \
  lib/string_array.c \
  lib/mlrregex.c \
  lib/mlr_globals.c \
  containers/mlrval.c \
  containers/mvfuncs.c \
  containers/mlhmmv.c \
  containers/sllmv.c \
  containers/sllv.c \
  containers/slls.c \
  containers/lrec.c \
  unit_test/test_mlhmmv.c

TEST_MLRUTIL_SRCS = \
  lib/mlr_globals.c \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/string_builder.c \
  unit_test/test_mlrutil.c

TEST_MLRREGEX_SRCS = \
  lib/mlr_globals.c \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlrregex.c \
  lib/string_builder.c \
  lib/string_array.c \
  unit_test/test_mlrregex.c

TEST_STRING_BUILDER_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  unit_test/test_string_builder.c

TEST_PARSE_TRIE_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  containers/parse_trie.c \
  unit_test/test_parse_trie.c

TEST_RVAL_EVALUATORS_SRCS = \
  lib/mlr_globals.c \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlrdatetime.c \
  lib/mlrregex.c \
  lib/mlrmath.c \
  lib/string_builder.c \
  lib/string_array.c \
  containers/mlrval.c \
  containers/mvfuncs.c \
  containers/xvfuncs.c \
  containers/sllv.c \
  containers/slls.c \
  containers/sllmv.c \
  containers/lrec.c \
  containers/lhmsv.c \
  containers/lhmsi.c \
  containers/lhmsll.c \
  containers/mlhmmv.c \
  containers/lhmsmv.c \
  containers/hss.c \
  containers/mixutil.c \
  containers/loop_stack.c \
  containers/local_stack.c \
  containers/type_decl.c \
  dsl/mlr_dsl_ast.c \
  dsl/function_manager.c \
  dsl/keylist_evaluators.c \
  dsl/rval_expr_evaluators.c \
  dsl/rxval_expr_evaluators.c \
  dsl/rval_func_evaluators.c \
  dsl/rxval_func_evaluators.c \
  dsl/rval_list_evaluators.c \
  dsl/mlr_dsl_stack_allocate.c \
  dsl/mlr_dsl_blocked_ast.c \
  dsl/mlr_dsl_cst.c \
  dsl/mlr_dsl_cst_condish_statements.c \
  dsl/mlr_dsl_cst_for_map_statements.c \
  dsl/mlr_dsl_cst_for_srec_statements.c \
  dsl/mlr_dsl_cst_func_subr.c \
  dsl/mlr_dsl_cst_keywords.c \
  dsl/mlr_dsl_cst_loop_control_statements.c \
  dsl/mlr_dsl_cst_map_assignment_statements.c \
  dsl/mlr_dsl_cst_output_statements.c \
  dsl/mlr_dsl_cst_return_statements.c \
  dsl/mlr_dsl_cst_scalar_assignment_statements.c \
  dsl/mlr_dsl_cst_statements.c \
  dsl/mlr_dsl_cst_triple_for_statements.c \
  dsl/mlr_dsl_cst_unset_statements.c \
  output/lrec_writer_csv.c \
  output/lrec_writer_csvlite.c \
  output/lrec_writer_dkvp.c \
  output/lrec_writer_json.c \
  output/lrec_writer_markdown.c \
  output/lrec_writer_nidx.c \
  output/lrec_writer_pprint.c \
  output/lrec_writer_xtab.c \
  output/lrec_writers.c \
  output/multi_lrec_writer.c \
  output/multi_out.c \
  unit_test/test_rval_evaluators.c

TEST_JOIN_BUCKET_KEEPER_SRCS = \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/mlrescape.c \
  lib/mlr_globals.c \
  lib/string_builder.c \
  lib/context.c \
  containers/parse_trie.c \
  containers/lrec.c \
  containers/sllv.c \
  containers/rslls.c \
  containers/slls.c \
  containers/lhmslv.c \
  containers/hss.c \
  containers/mixutil.c \
  containers/header_keeper.c \
  containers/join_bucket_keeper.c \
  input/mmap_byte_reader.c \
  input/stdio_byte_reader.c \
  input/line_readers.c \
  input/lrec_reader_in_memory.c \
  input/lrec_readers.c \
  input/lrec_reader_mmap_csv.c \
  input/lrec_reader_stdio_csv.c \
  input/lrec_reader_mmap_csvlite.c \
  input/lrec_reader_stdio_csvlite.c \
  input/lrec_reader_mmap_dkvp.c \
  input/lrec_reader_stdio_dkvp.c \
  input/lrec_reader_mmap_nidx.c \
  input/lrec_reader_stdio_nidx.c \
  input/lrec_reader_mmap_xtab.c \
  input/lrec_reader_stdio_xtab.c \
  input/lrec_reader_mmap_json.c \
  input/lrec_reader_stdio_json.c \
  input/mlr_json_adapter.c \
  input/json_parser.c \
  input/file_reader_mmap.c \
  input/file_reader_stdio.c \
  input/file_ingestor_stdio.c \
  input/peek_file_reader.c \
  unit_test/test_join_bucket_keeper.c

EXPERIMENTAL_READER_SRCS = \
  lib/mlrutil.c \
  lib/mlrdatetime.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  lib/context.c \
  lib/mlrescape.c \
  lib/mlrregex.c \
  lib/mlr_globals.c \
  lib/string_array.c \
  lib/string_builder.c \
  input/stdio_byte_reader.c \
  input/file_reader_mmap.c \
  input/line_readers.c \
  containers/parse_trie.c \
  experimental/getlines.c

EXPERIMENTAL_JSON_VG_MEM_SRCS = \
  lib/mlr_globals.c \
  lib/mlrutil.c \
  lib/mlr_arch.c \
  lib/mtrand.c \
  input/json_parser.c \
  experimental/json_vg_mem.c

# ================================================================
# User-make: creates the executable and runs unit & regression tests
# This is the default target for anyone pulling the repo and trying to
# build it to be able to use it. It just needs flex and the C compiler.
#top: mlr tests
# xxx temp:
top: mlr mlrg tests

install: mlr tests
	cp mlr $(INSTALLDIR)
installhome: mlr tests
	cp mlr $(HOME)/bin

# ================================================================
tags: .always
	ctags -R .

mlr: .always parsing
	$(CCOPT) $(NON_DSL_SRCS) $(DSL_OBJS) $(LFLAGS) -o mlr
# On x86_64/Centos I had to first do
# sudo yum install glibc-devel glibc-static
mlr.static: .always parsing
	$(CCOPT) -static $(NON_DSL_SRCS) $(DSL_OBJS) $(LFLAGS) -o mlr.static

parsing: .always
	make -C parsing -f Makefile.no-autoconfig mlr_dsl_parse.o
	make -C parsing -f Makefile.no-autoconfig mlr_dsl_lexer.o
	make -C parsing -f Makefile.no-autoconfig mlr_dsl_wrapper.o

two: mlr mlrg

# ----------------------------------------------------------------
# Other executable variants

# Debug version
mlrg: .always parsing
	$(CCDEBUG) $(NON_DSL_SRCS) $(DSL_OBJS) $(LFLAGS) -o mlrg

# Debug version with local-stack verbosity
mlrt: .always parsing
	$(CCDEBUG) -DLOCAL_STACK_TRACE_ENABLE -DLOCAL_STACK_BOUNDS_CHECK_ENABLE \
	  $(NON_DSL_SRCS) $(DSL_OBJS) $(LFLAGS) -o mlrt

# Profile version. Usage:
# * make mlrp
# * mlrp {arguments>
# * gprof mlrp gmon.out > myfile.txt
# Note: works on Linux; not on OSX.
mlrp: .always parsing
	$(CCDEBUG) -g -pg $(NON_DSL_SRCS) $(DSL_OBJS) $(LFLAGS) -o mlrp

# ================================================================
tests: unit-test reg-test

unit-test: test-mlrutil test-mlrregex test-argparse test-line-readers test-byte-readers test-peek-file-reader test-parse-trie test-lrec test-multiple-containers test-mlhmmv test-string-builder test-rval-evaluators test-join-bucket-keeper
	./test-mlrutil
	./test-mlrregex
	./test-argparse
	./test-line-readers
	./test-byte-readers
	./test-peek-file-reader
	./test-parse-trie
	./test-lrec
	./test-multiple-containers
	./test-mlhmmv
	./test-string-builder
	./test-rval-evaluators
	./test-join-bucket-keeper
	@echo
	@echo DONE

reg-test:
	./reg_test/run

# ----------------------------------------------------------------
# Run this after unit-test expected output has changed, and is verified to be
# OK. (Example: after adding new test cases in test/run.)
regtest-copy taters:
	cp output-regtest/out reg_test/expected
regtest-copy-dev:
	cp output-regtest/out-dev reg_test/expected

# ----------------------------------------------------------------
# Unit-test executables

test-argparse: .always
	$(CCDEBUG) $(TEST_ARGPARSE_SRCS) $(LFLAGS) -o test-argparse

test-byte-readers: .always
	$(CCDEBUG) $(TEST_BYTE_READERS_SRCS) $(LFLAGS) -o test-byte-readers

test-line-readers: .always
	$(CCDEBUG) $(TEST_LINE_READERS_SRCS) $(LFLAGS) -o test-line-readers

test-peek-file-reader: .always
	$(CCDEBUG) $(TEST_PEEK_FILE_READER_SRCS) $(LFLAGS) -o test-peek-file-reader

test-lrec: .always
	$(CCDEBUG) $(TEST_LREC_SRCS) $(LFLAGS) -o test-lrec -lm

test-multiple-containers: .always
	$(CCDEBUG) $(TEST_MULTIPLE_CONTAINERS_SRCS) $(LFLAGS) -o test-multiple-containers -lm

test-mlhmmv: .always
	$(CCDEBUG) $(TEST_MLHMMV_SRCS) $(LFLAGS) -o test-mlhmmv -lm

test-mlrutil: .always
	$(CCDEBUG) $(TEST_MLRUTIL_SRCS) $(LFLAGS) -o test-mlrutil -lm

test-mlrregex: .always
	$(CCDEBUG) $(TEST_MLRREGEX_SRCS) $(LFLAGS) -o test-mlrregex

test-string-builder: .always
	$(CCDEBUG) $(TEST_STRING_BUILDER_SRCS) $(LFLAGS) -o test-string-builder

test-parse-trie: .always
	$(CCDEBUG) $(TEST_PARSE_TRIE_SRCS) $(LFLAGS) -o test-parse-trie

test-rval-evaluators: .always
	$(CCDEBUG) $(TEST_RVAL_EVALUATORS_SRCS) $(LFLAGS) -o test-rval-evaluators -lm

test-join-bucket-keeper: .always
	$(CCDEBUG) $(TEST_JOIN_BUCKET_KEEPER_SRCS) $(LFLAGS) -o test-join-bucket-keeper -lm

# ----------------------------------------------------------------
# Standalone mains

getl: .always
	$(CCOPT) $(EXPERIMENTAL_READER_SRCS) $(LFLAGS) -o getl

json-vg-mem: .always
	$(CCDEBUG) $(EXPERIMENTAL_JSON_VG_MEM_SRCS) $(LFLAGS) -o json-vg-mem

# ================================================================
# BSD can't handle rm -v, alas
clean:
	@rm -f mlr mlrd mlrg mlrp tester
	@make -C parsing -f Makefile.no-autoconfig clean

perfclean profclean:
	@rm -f gmon.out perf.data perf.data.old

.always:
	@true
