##########################################################################
#                                                                        #
#  This file is part of Frama-C.                                         #
#                                                                        #
#  Copyright (C) 2007-2015                                               #
#    CEA (Commissariat à l'énergie atomique et aux énergies              #
#         alternatives)                                                  #
#                                                                        #
#  you can redistribute it and/or modify it under the terms of the GNU   #
#  Lesser General Public License as published by the Free Software       #
#  Foundation, version 2.1.                                              #
#                                                                        #
#  It is distributed in the hope that it will be useful,                 #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU Lesser General Public License for more details.                   #
#                                                                        #
#  See the GNU Lesser General Public License version 2.1                 #
#  for more details (enclosed in the file licenses/LGPLv2.1).            #
#                                                                        #
##########################################################################

ptests/ptests_config.ml: Makefile.generating share/Makefile.config
	$(PRINT_MAKING) $@
	$(RM) $@
	$(TOUCH) $@
	$(ECHO) \
	 "let no_native_dynlink = " \
	 $(subst yes,false,$(subst no,true,$(USABLE_NATIVE_DYNLINK))) ";;" \
	 >> $@
	$(CHMOD_RO) $@

tests/ptests_config: Makefile.generating share/Makefile.config
	$(PRINT_MAKING) $@
	$(RM) $@
	$(TOUCH) $@
	$(ECHO) "DEFAULT_SUITES=$(PLUGIN_TESTS_LIST)" >> $@
	$(ECHO) "TOPLEVEL_PATH=bin/toplevel.$(OCAMLBEST)$(EXE)" >> $@
	$(ECHO) "OCAMLRUNPARAM=" >> $@
	$(ECHO) "FRAMAC_SESSION=." >> $@
	$(ECHO) "FRAMAC_SHARE=./share" >> $@
	$(ECHO) "FRAMAC_PLUGIN=./lib/plugins" >> $@
	$(ECHO) "FRAMAC_PLUGIN_GUI=./lib/plugins/gui" >> $@
	$(ECHO) "FRAMAC_LIB=./lib/fc" >> $@
	$(CHMOD_RO) $@

share/Makefile.kernel: Makefile.generating share/Makefile.config share/Makefile.common
	$(PRINT_MAKING) $@
	$(RM) $@
	$(ECHO) "# This makefile was automatically generated." > $@
	$(ECHO) "# Do not modify." >> $@
	$(ECHO) "ifeq (\$$(FRAMAC_INTERNAL),yes)" >> $@
	$(ECHO) "DYN_BLINKFLAGS=$(filter-out $(INCLUDES), $(BLINKFLAGS)) $(foreach d, $(INCLUDES:-I%=%), -I $(FRAMAC_TOP_SRCDIR)/$(d))" >> $@
	$(ECHO) "DYN_GEN_BYTE_LIBS=$(addprefix $(FRAMAC_TOP_SRCDIR)/, $(GEN_BYTE_LIBS))" >> $@
	$(ECHO) "DYN_BYTE_LIBS=$(filter-out $(GEN_BYTE_LIBS), $(BYTE_LIBS))" >> $@
	$(ECHO) "DYN_ALL_BATCH_CMO=$(addprefix $(FRAMAC_TOP_SRCDIR)/, $(notdir $(ALL_BATCH_CMO)))" >> $@
	$(ECHO) "DYN_OLINKFLAGS=$(filter-out $(INCLUDES), $(OLINKFLAGS)) $(foreach d, $(INCLUDES:-I%=%), -I $(FRAMAC_TOP_SRCDIR)/$(d))" >> $@
	$(ECHO) "DYN_GEN_OPT_LIBS=$(addprefix $(FRAMAC_TOP_SRCDIR)/, $(GEN_OPT_LIBS))" >> $@
	$(ECHO) "DYN_OPT_LIBS=$(filter-out $(GEN_OPT_LIBS), $(OPT_LIBS))" >> $@
	$(ECHO) "DYN_ALL_BATCH_CMX=$(addprefix $(FRAMAC_TOP_SRCDIR)/, $(ALL_BATCH_CMX))" >> $@
	$(ECHO) "else" >> $@
	$(ECHO) "DYN_BLINKFLAGS=$(filter-out $(INCLUDES), $(BLINKFLAGS)) $(addprefix -I ,$(filter +%,$(INCLUDES)))" >> $@
	$(ECHO) "DYN_GEN_BYTE_LIBS=$(addprefix $(FRAMAC_LIBDIR)/, $(notdir $(GEN_BYTE_LIBS)))" >> $@
	$(ECHO) "DYN_BYTE_LIBS=$(filter-out $(GEN_BYTE_LIBS), $(BYTE_LIBS))" >> $@
	$(ECHO) "DYN_ALL_BATCH_CMO=$(addprefix $(FRAMAC_LIBDIR)/, $(notdir $(ALL_BATCH_CMO)))" >> $@
	$(ECHO) "DYN_OLINKFLAGS=$(filter-out $(INCLUDES), $(OLINKFLAGS))  $(addprefix -I ,$(filter +%,$(INCLUDES)))" >> $@
	$(ECHO) "DYN_GEN_OPT_LIBS=$(addprefix $(FRAMAC_LIBDIR)/, $(notdir $(GEN_OPT_LIBS)))" >> $@
	$(ECHO) "DYN_OPT_LIBS=$(filter-out $(GEN_OPT_LIBS), $(OPT_LIBS))" >> $@
	$(ECHO) "DYN_ALL_BATCH_CMX=$(addprefix $(FRAMAC_LIBDIR)/, $(notdir $(ALL_BATCH_CMX)))" >> $@
	$(ECHO) "endif" >> $@
	$(CHMOD_RO) $@

$(CONFIG_FILE): $(CONFIG_FILE).in VERSION share/Makefile.config Makefile.generating configure.in
	$(PRINT_MAKING) $@
	$(RM) $@
	$(SED) \
	  -e "s|@VERSION@|$(VERSION)|" \
          -e "s|@CURR_DATE@|$$(LC_ALL=C date)|" \
	  -e "s|@OCAMLC@|$(OCAMLC)|" \
          -e "s|@OCAMLOPT@|$(OCAMLOPT)|" \
          -e "s|@FRAMAC_DATADIR@|$(FRAMAC_DATADIR)|" \
	  -e "s|@FRAMAC_LIBDIR@|$(FRAMAC_LIBDIR)|" \
	  -e "s|@FRAMAC_TOP_SRCDIR@|$(FRAMAC_TOP_SRCDIR)|" \
	  -e "s|@FRAMAC_PLUGINDIR@|$(FRAMAC_PLUGINDIR)|" \
	  -e "s|@FRAMAC_DEFAULT_CPP@|$(FRAMAC_DEFAULT_CPP)|" \
	  -e "s|@FRAMAC_GNU_CPP@|$(FRAMAC_GNU_CPP)|" \
          -e "s|@DEFAULT_CPP_KEEP_COMMENTS@|$(DEFAULT_CPP_KEEP_COMMENTS)|" \
	  -e "s|@STATIC_PLUGINS@|$(STATIC_PLUGINS)|" \
          -e "s|@STATIC_GUI_PLUGINS@|$(STATIC_GUI_PLUGINS)|" \
          -e "s|@COMPILATION_UNITS@|$(COMPILATION_UNITS)|" \
          -e "s|@LIBRARY_NAMES@|$(LIBRARY_NAMES)|" \
	  -e "s|@OPTDOT@|$(OPTDOT)|" \
          -e "s|@EXE@|$(EXE)|" \
          $< > $@
	$(CHMOD_RO) $@

ifeq ("$(LOCAL_MACHDEP)","yes")

MACHDEP_PATH=share

# Create the machine dependency module
# If the cl command cannot be run then the MSVC part will be identical to GCC
.PHONY : machdep $(MACHDEP_PATH)/local_machdep.ml
machdep: $(MACHDEP_PATH)/local_machdep.ml
bin/machdep.exe: machdep

config.h: 
	$(PRINT_MAKING) $@
	$(ECHO) "missing config.h file generated at configure stage using --enable-localmachdep option."
	exit 1;

$(MACHDEP_PATH)/local_machdep.ml: \
  $(MACHDEP_PATH)/machdep.c config.h Makefile.generating
	$(PRINT_MAKING) $@
	$(RM) $@
	$(ECHO) "(* This module was generated automatically by code in Makefile and machdep.c *)" >$@
# Now generate the type definition
	$(ECHO) "open Cil_types" >> $@
	if $(CC) -D_GNUCC $< -o bin/machdep.exe ;then \
	    $(ECHO) "machdep.exe created succesfully."; \
	else \
	    $(RM) $@; exit 1; \
	fi
	$(ECHO) "let gcc = {" >>$@
	./bin/machdep.exe >>$@
	$(ECHO) "	 underscore_name = $(UNDERSCORE_NAME) ;" >> $@
	$(ECHO) "}"          >>$@
	if cl /D_MSVC $< /Febin/machdep.exe /Fobin/machdep.obj ;then \
	   $(ECHO) "let hasMSVC = true" >>$@; \
	else \
	   $(ECHO) "let hasMSVC = false" >>$@; \
	fi
	$(ECHO) "let msvc = {" >>$@
	./bin/machdep.exe >>$@
	$(ECHO) "	 underscore_name = true ;" >> $@
	$(ECHO) "}"          >>$@
	$(ECHO) \
	  "let gccHas__builtin_va_list = $(HAVE_BUILTIN_VA_LIST)" >>$@
	$(ECHO) "let __thread_is_keyword = $(THREAD_IS_KEYWORD)"  >>$@
	$(ECHO) \
	  "$@ generated. You may have this file merged into Frama-C by developers."
	$(CHMOD_RO) $@

endif



##################
# Frama-C-config #
##################

src/kernel_internals/runtime/frama_c_config.ml: src/kernel_internals/runtime/config.ml \
	src/kernel_internals/runtime/frama_c_config.ml.in Makefile.generating
	$(PRINT_MAKING) $@
	$(RM) $@
	$(ECHO) "module Filepath = struct let add_symbolic_dir _ _ = () end" >> $@
	$(ECHO) "module Config = struct" >> $@
	$(CAT) src/kernel_internals/runtime/config.ml >> $@
	$(ECHO) "end" >> $@
	$(CAT) src/kernel_internals/runtime/frama_c_config.ml.in >> $@
	$(CHMOD_RO) $@

GENERATED+= src/kernel_internals/runtime/frama_c_config.ml

bin/frama-c-config$(EXE): src/kernel_internals/runtime/frama_c_config.ml
ifeq ($(OCAMLBEST),opt)
	$(OCAMLOPT) str.cmxa $< -o $@
else
	$(OCAMLC) str.cma $< -o $@
endif


# Local Variables:
# mode: makefile
# End:
