#
# Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code 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 General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

ifeq (,$(_MAKEBASE_GMK))
  $(error You must include MakeBase.gmk prior to including CopyFiles.gmk)
endif

################################################################################
#
# Code for handling the SetupCopyFiles macro.
#
################################################################################

define AddFileToCopy
  # Helper macro for SetupCopyFiles
  # 1 : Source file
  # 2 : Dest file
  # 3 : Variable to add targets to
  # 4 : Macro to call for copy operation
  # 5 : Action text to log
  $2: $1
	$$(call LogInfo, $(strip $5) $$(patsubst $(OUTPUTDIR)/%,%,$$(call DecodeSpace, $$@)))
	$$($$(strip $4))

  $3 += $2
  $3_SOURCES += $1
endef

# Returns the value of the first argument
identity = \
    $(strip $1)

# Setup make rules for copying files, with an option to do more complex
# processing instead of copying.
#
# Parameter 1 is the name of the rule. This name is used as variable prefix,
# and the targets generated are listed in a variable by that name.
#
# The list of all source files is returned in $1_SOURCES.
#
# Remaining parameters are named arguments. These include:
#   SRC     : Source root dir (defaults to dir of first file)
#   DEST    : Dest root dir
#   FILES   : List of files to copy with absolute paths, or path relative to SRC.
#             Must be in SRC.
#   FLATTEN : Set to flatten the directory structure in the DEST dir.
#   MACRO   : Optionally override the default macro used for making the copy.
#             Default is 'install-file'
#   NAME_MACRO : Optionally supply a macro that rewrites the target file name
#                based on the source file name
#   LOG_ACTION : Optionally specify a different action text for log messages
SetupCopyFiles = $(NamedParamsMacroTemplate)
define SetupCopyFilesBody

  ifeq ($$($1_MACRO), )
    $1_MACRO := install-file
  endif

  # Default SRC to the dir of the first file.
  ifeq ($$($1_SRC), )
    $1_SRC := $$(dir $$(firstword $$($1_FILES)))
  endif

  ifeq ($$($1_NAME_MACRO), )
    $1_NAME_MACRO := identity
  endif

  ifeq ($$($1_LOG_ACTION), )
    $1_LOG_ACTION := Copying
  endif

  # Remove any trailing slash from SRC and DEST
  $1_SRC := $$(patsubst %/,%,$$($1_SRC))
  $1_DEST := $$(patsubst %/,%,$$($1_DEST))

  # Need to wrap arguments in DoubleDollar because of the eval nested inside an
  # eval macro body.
  $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
    $$(eval $$(call AddFileToCopy, \
        $$(call DoubleDollar, $$($1_SRC)/$$f), \
        $$(call DoubleDollar, \
            $$($1_DEST)/$$(call $$(strip $$($1_NAME_MACRO)),$$(if $$($1_FLATTEN),$$(notdir $$f),$$f)) \
        ), \
        $1, \
        $$($1_MACRO), \
        $$($1_LOG_ACTION) \
    )) \
  )

endef
