make/common/Execute.gmk
author ihse
Wed, 06 Feb 2019 21:31:36 +0100
branchihse-setupexecute-branch
changeset 57155 ba61956ea598
parent 57139 94ff48d6eee4
child 57158 003703d03633
permissions -rw-r--r--
Convert some more to SetupExecute.

#
# Copyright (c) 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 Execute.gmk)
endif

################################################################################
#
# Code for handling the SetupExecute macro.
#
################################################################################


################################################################################
# Setup make rules for executing an arbitrary command.
#
# 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.
#
# Remaining parameters are named arguments. These include:
#   COMMAND     : The command to execute
#   POST_COMMAND:
#   OUTPUT_DIR  : The directory that will contain the result from the command
#   OUTPUT_FILE : Use this if the command results in a single output file.
#   INFO        : Message to display at LOG=info level when running command (optional).
#   DEPS        : Dependencies for the execution to take place
#

# Setup make rules for copying files, with an option to do more complex
SetupExecute = $(NamedParamsMacroTemplate)
define SetupExecuteBody
  ifeq ($$($1_COMMAND), )
    $$(error COMMAND is missing in SetupExecute $1)
  endif

  $1_BASE := $$($1_OUTPUT_DIR)/_$1
  $1_MARKER := $$($1_BASE).marker

  ifeq ($$($1_INFO), )
    $1_INFO := Running commands for $1
  endif

  ifeq ($$($1_OUTPUT_DIR)$$($1_OUTPUT_FILE), )
    $$(error OUTPUT_DIR or OUTPUT_FILE is required in SetupExecute $1)
  endif

  ifneq ($$($1_OUTPUT_DIR), )
    ifneq ($$($1_OUTPUT_FILE), )
      $$(error Cannot specify both OUTPUT_DIR and OUTPUT_FILE in SetupExecute $1)
    endif
    $1_RESULT := $$($1_MARKER)
  else
    # If we have a single output file, we don't need a separate marker
    $1_RESULT := $$($1_OUTPUT_FILE)
  endif

  $$($1_RESULT): $$($1_DEPS)
	$$(call LogInfo, $$($1_INFO))
	$$(call MakeDir, $$(@D))
	$$(call ExecuteWithLog, $$($1_BASE), \
	    $$($1_COMMAND))
        ifeq ($$($1_RESULT), $$($1_MARKER))
	  $$(TOUCH) $$@
        endif

  # Export all our generated targets in $1, and the final target in $1_TARGET.
  $1 := $$($1_RESULT)
  $1_TARGET := $$($1_RESULT)

  ifneq ($$($1_POST_COMMAND), )
    $1_POST_MARKER := $$($1_BASE)_post.marker

    $$($1_POST_MARKER): $$($1_MARKER)
	$$(call ExecuteWithLog, $$($1_BASE), \
	    $$($1_POST_COMMAND))
	$$(TOUCH) $$@

    $1 += $$($1_POST_MARKER)
    $1_TARGET := $$($1_POST_MARKER)
  endif

endef