# -*- makefile -*-

#
# If you add a new variable to this file (mk/make_config),
# make sure to also add a default value to mk/defaults.
#

Shell. +=
    extract-cc(argv) =
        awk()
        case $"^bytecomp_c_compiler: "
	    println($2)

    extract-cflags(argv) =
        awk()
        case $"^bytecomp_c_compiler: "
            println($(concat $" ", $(subrange 3, $(sub $(length $*), 3), $*)))

    extract-ccomp-type(argv) =
        awk()
        case $"^ccomp_type: "
	    println($2)


# preconfig file (= written by configure). Might not exist.
public.PRECONFIG_FILE = $(file $(ROOT)/.preconfig)


#
# Function to generate the config file
#
MakeConfig(CONFIG_FILE) =
    # Now apply preconfig
    if $(file-exists $(PRECONFIG_FILE))
        include $(PRECONFIG_FILE)
        BINDIR = $(PREFIX)/bin
        LIBDIR = $(PREFIX)/lib
        MANDIR = $(PREFIX)/man
        export

    #
    # If possible, express PREFIX subdirs via $(PREFIX)/...
    #
    if $(equal $(BINDIR), $(dir $(PREFIX)/bin))
        BINDIR = $'$(PREFIX)/bin'
        export

    if $(equal $(LIBDIR), $(dir $(PREFIX)/lib))
        LIBDIR = $'$(PREFIX)/lib'
        export

    if $(equal $(MANDIR), $(dir $(PREFIX)/man))
        MANDIR = $'$(PREFIX)/man'
        export

    cc = $(shell $(OCAMLC) -config | extract-cc)
    cflags = $(shell $(OCAMLC) -config | extract-cflags)
    cflags = $(filter-out -W%, $(cflags))
    ccomptype = $(shell $(OCAMLC) -config | extract-ccomp-type)

    if $(equal $(ccomptype), cc)
        cflags = $(cflags) -Wall
        export

    CONFIG = $"""\
# Main OMake configuration file.

#
# This file ($(CONFIG_FILE)) is generated by omake using mk/make_config.
# If you want to change anything except for the variable values,
# put it into $(CONFIG_FILE).local.
#

#
# Where is the install location?  OMake installs files into the
# following directories.
#    BINDIR: program executables
#    LIBDIR: omake standard library
#    MANDIR: root directory for the manual pages
#
PREFIX = $$(dir $$'$(PREFIX)')
BINDIR = $$(dir $$"$(BINDIR)")
LIBDIR = $$(dir $$"$(LIBDIR)")
MANDIR = $$(dir $$"$(MANDIR)")

#
# What C compiler do you want to use?
# By default, this is now set to the same C compiler as used by ocaml.
#
CC = $(getenv CC, $(cc))
CFLAGS = $(getenv CFLAGS, $(cflags))
CCOMPTYPE = $(getenv CCOMPTYPE, $(ccomptype))

#
# Do you want native and/or byte code?
# By default, we make native code.  If you want
# byte code, set it to true.
#
NATIVE_ENABLED = $(NATIVE_ENABLED)
BYTE_ENABLED = $(BYTE_ENABLED)
"""$(if $(READLINE_AVAILABLE), $"""
#
# Do you want to include readline for the interactive shell?
# Your system appears to support readline, so this is enabled by default.
#
READLINE_ENABLED = $(READLINE_ENABLED)
""", $"""
#
# Your system does not appear to have the readline library development files
# installed, readline support in the interactive shell will not be available.
#
""")$(if $(NCURSES_AVAILABLE), $"""
#
# Do you want to include terminal information (terminfo) capabilities?
# Your system appears to support ncurses, so this is enabled by default.
#
NCURSES_ENABLED = $(NCURSES_ENABLED)
""", $"""
#
# Your system does not appear to have the ncurses library development files
# installed, terminal information (terminfo) capabilities will not be available.
#
""")$(if $(FAM_AVAILABLE), $"""
#
# Do you want to include filesystem monitoring? This will enable the
# omake -P and -p options, which are quite useful.
# Your system appears to support this functionality (via FAM, Gamin, kqueue, or Win32).
#
FAM_ENABLED = $(FAM_ENABLED)
""", $"""
#
# Your system does not appear to support filesystem monitoring (either FAM library,
# or Gamin library, or Kqueue event interface). Omake's -P and -p options will not
# be available.
#
""")$"""
#
# By default, how often should the .omakedb database be saved (in seconds, can be
# a floating point number).
#
DEFAULT_SAVE_INTERVAL = $(DEFAULT_SAVE_INTERVAL)
"""$(if $(OCAMLDEP_MODULES_AVAILABLE), $"""
#
# The OCAMLDEP_MODULES_ENABLED variable specifies whether to use the -modules
# option to ocamldep.  Dependency analysis is much better with this option,
# but it is also a little slower.  Also, the -modules option is only
# available with the standard OCaml distribution starting with the 3.10 release.
# OMake includes a patched ocamldep, in case you want to use it with an older release
# of OCaml.
#
OCAMLDEP_MODULES_ENABLED = $(OCAMLDEP_MODULES_ENABLED)
""", $"""
#
# Your system does not seem to have a version of the ocamldep that supports the
# new -modules option. Will use the traditional (potentially less precise)
# dependency analysis.
#
# OCAMLDEP_MODULES_ENABLED is false
""")$"""
#
# Do you want to enable native-code profiling?
# Unless you really care, you should leave this disabled.
#
NATIVE_PROFILE = $(NATIVE_PROFILE)
"""

    # Save the text in the output file
    exists = $(file-exists $(CONFIG_FILE))
    if $(exists)
        eprintln(Updating the $(CONFIG_FILE) file.)
    fprint($(CONFIG_FILE), $(CONFIG))
