#!/usr/bin/make -f

#
# \brief  Build binary archives from source
# \author Norman Feske
# \date   2017-03-16
#

define HELP_MESSAGE

  Build binary archives from source archives stored in the depot

  usage:

    $(firstword $(MAKEFILE_LIST)) <archive-path>...

  The <archive-path> argument denotes the archive to create in the
  form of a path. The first path element correponds to the identity
  of the archive creator, the second element corresponds to the type
  of the archive (bin or pkg), the third element specifies the target
  architectures (e.g., x86_64), and the fourth element is the name
  of the corresponding source archive including the version.

  E.g., the user 'alan' may build the following archives:

    alan/bin/x86_64/zlib/<version>  - a binary archive of the zlib
                                      library with the specified
                                      version, built for the 64-bit
                                      x86 architecture

    alan/pkg/x86_32/wm/<version>    - all binary archives needed by
                                      the 'wm' package archive, built
                                      for the 32-bit x86 architecture

  The following arguments tweak the operation of the tool:

    REBUILD=1          Replace existing archives with freshly created
                       ones.

    VERBOSE=           Show individual operations.

    -j<N>              Enable the parallel creation of packages where
                       <N> denotes the level of parallelism.

    KEEP_BUILD_DIR=1   Do not remove build directories of built binary
                       packages. This is useful for debugging build
                       problems.

    CCACHE=1           Compiler calls will use the C/C++ compiler cache.

    DBG=1              Build 'dbg' archives containing debug info files
                       in addition to the corresponding 'bin' archives.
endef

export GENODE_DIR := $(realpath $(dir $(MAKEFILE_LIST))/../..)

include $(GENODE_DIR)/tool/depot/mk/front_end.inc
include $(GENODE_DIR)/tool/depot/mk/categorize_args.inc


#
# Collect dependencies for all specified arguments
#
# The following accessor functions used by 'mk/dependencies.inc'.
#

_file_within_archive = $(wildcard $(DEPOT_DIR)/$1/$2)

api_file       = $(call _file_within_archive,$1,api)
used_apis_file = $(call _file_within_archive,$1,used_apis)

_pkg_archives_content = $(call file_content,$(call _file_within_archive,$1,archives))

pkg_src_archives = $(call grep_archive_type,src,$(call _pkg_archives_content,$1))
pkg_raw_archives = $(call grep_archive_type,raw,$(call _pkg_archives_content,$1))
pkg_pkg_archives = $(call grep_archive_type,pkg,$(call _pkg_archives_content,$1))

include $(GENODE_DIR)/tool/depot/mk/dependencies.inc


#
# Detect missing source archives
#

archive_exists_in_depot = $(wildcard $(DEPOT_DIR)/$1)

MISSING_ARCHIVES := $(sort \
                       $(foreach A,${ARCHIVES(bin)},\
                          $(if $(call archive_exists_in_depot,$(call src_of_bin,$A)),,$A)))

checked_source_archives_exist:
ifneq ($(MISSING_ARCHIVES),)
	@echo "Error: archives missing in the depot ($(MISSING_ARCHIVES))"; false
endif


#
# Generate makefile for archive-build stage
#
# The target pattern rules are needed to make sure that 'build_bin_archive'
# is called only once for each bin/dbg archive pair.
#

# determine binary-archive path within the depot
_dst_bin_spec_path = $(call archive_user,$1)/bin/$(call bin_archive_spec,$1)/
dst_bin_archive_path = $(call _dst_bin_spec_path,$1)$(call bin_archive_recipe,$1)/$(call bin_archive_version,$1)
dst_bin_archive_path_pattern = $(call _dst_bin_spec_path,$1)$(call bin_archive_recipe,$1)/%

# determine debug-archive path within the depot
_dst_dbg_spec_path = $(call archive_user,$1)/dbg/$(call bin_archive_spec,$1)/
dst_dbg_archive_path = $(call _dst_dbg_spec_path,$1)$(call bin_archive_recipe,$1)/$(call bin_archive_version,$1)
dst_dbg_archive_path_pattern = $(call _dst_dbg_spec_path,$1)$(call bin_archive_recipe,$1)/%

BUILD_MK_FILE := $(DEPOT_DIR)/var/build.mk

.PHONY: $(BUILD_MK_FILE)

wipe_existing_archives:
	$(VERBOSE)rm -rf $(addprefix $(DEPOT_DIR)/,\
	                    $(foreach A,${ARCHIVES(bin)},$(call dst_bin_archive_path,$A)))
	$(VERBOSE)rm -rf $(addprefix $(DEPOT_DIR)/,\
	                    $(foreach A,${ARCHIVES(dbg)},$(call dst_dbg_archive_path,$A)))

$(BUILD_MK_FILE): checked_source_archives_exist checked_no_uncategorized
	$(VERBOSE)mkdir -p $(dir $@)
	$(VERBOSE)( echo -e "all:\n"; \
	            echo "TOOL_DIR := $(GENODE_DIR)/tool"; \
	             $(foreach A,${ARCHIVES(bin)},\
	                bin_target=$(call dst_bin_archive_path,$A); \
	                bin_target_pattern=$(call dst_bin_archive_path_pattern,$A); \
	                dbg_target=$(call dst_dbg_archive_path,$A); \
	                dbg_target_pattern=$(call dst_dbg_archive_path_pattern,$A); \
	                user=$(call archive_user,$A); \
	                recipe=$(call bin_archive_recipe,$A); \
	                version=$(call bin_archive_version,$A); \
	                spec=$(call bin_archive_spec,$A); \
	                echo ""; \
	                echo "TARGETS += $$bin_target"; \
	                echo "TOOL($$bin_target) := build_bin_archive"; \
	                echo "ARGS($$bin_target) := $$recipe/$$version USER=$$user SPEC=$$spec"; \
	                echo ""; \
	                echo "ifneq (\$$(DBG),)"; \
	                echo "TARGETS += $$dbg_target"; \
	                echo "TOOL($$dbg_target) := build_bin_archive"; \
	                echo "ARGS($$dbg_target) := $$recipe/$$version USER=$$user SPEC=$$spec"; \
	                echo "endif"; \
	                echo -e "\n$$bin_target_pattern $$dbg_target_pattern:"; \
	                echo -e "\t\$$(MAKE) -f \$$(TOOL_DIR)/depot/mk/\$${TOOL(\$$@)}" \
	                        "\$${ARGS(\$$@)} CCACHE=\$$(CCACHE) VERBOSE=\$$(VERBOSE)\n"; \
	             ) \
	             echo -e "\nall: \$$(TARGETS)"; \
	          ) > $@


#
# Invoke sub make to process generated makefile
#
execute_generated_build_mk_file: $(BUILD_MK_FILE)
	$(VERBOSE)$(MAKE) $(if $(VERBOSE),--quiet) -f $(BUILD_MK_FILE) \
	                  -C $(DEPOT_DIR) VERBOSE=$(VERBOSE)

ifneq ($(REBUILD),)
execute_generated_build_mk_file: wipe_existing_archives
endif

$(MAKECMDGOALS): execute_generated_build_mk_file
	@true

