]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/Scripts: Add python script to run a makefile
authorMichael D Kinney <michael.d.kinney@intel.com>
Wed, 2 Aug 2017 21:16:49 +0000 (14:16 -0700)
committerMichael D Kinney <michael.d.kinney@intel.com>
Fri, 11 Aug 2017 18:21:04 +0000 (11:21 -0700)
https://bugzilla.tianocore.org/show_bug.cgi?id=670

Add the python script RunMakefile.py that can be used
in a PREBUILD/POSTBUIILD action to invoke a makefile
passing in context as makefile defines.  The command
line arguments passed into RunMakefile.py are converted
to the following set of defines.

* ACTIVE_PLATFORM
* TARGET_ARCH
* TOOL_CHAIN_TAG
* CONF_DIRECTORY
* TARGET
* EXTRA_FLAGS

In addition, a makefile can access the system environment
variables including WORKSPACE and PACKAGES_PATH.

The makefile target from the following set is also passed
into the makefile.  If no target is passed into build, then
the 'all' target is used.

[all|fds|genc|genmake|clean|cleanall|cleanlib|modules|libraries|run]

A platform DSC file can use a statements in the [Defines]
section of the following form to use this script.  MAKEFILE
is a WORKSPACE or PACKAGES_PATH relative path to the makefile
to run.

[Defines]
  PREBUILD  = python BaseTools/Script/RunMakefile.py --makefile MAKEFILE
  POSTBUILD = python BaseTools/Script/RunMakefile.py --makefile MAKEFILE

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Michael Kinney <michael.d.kinney@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
BaseTools/Scripts/RunMakefile.py [new file with mode: 0644]

diff --git a/BaseTools/Scripts/RunMakefile.py b/BaseTools/Scripts/RunMakefile.py
new file mode 100644 (file)
index 0000000..48bc198
--- /dev/null
@@ -0,0 +1,178 @@
+## @file\r
+# Run a makefile as part of a PREBUILD or POSTBUILD action.\r
+#\r
+# Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
+# This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution.  The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+'''\r
+RunMakefile.py\r
+'''\r
+\r
+import os\r
+import sys\r
+import argparse\r
+import subprocess\r
+\r
+#\r
+# Globals for help information\r
+#\r
+__prog__        = 'RunMakefile'\r
+__version__     = '%s Version %s' % (__prog__, '1.0')\r
+__copyright__   = 'Copyright (c) 2017, Intel Corporation. All rights reserved.'\r
+__description__ = 'Run a makefile as part of a PREBUILD or POSTBUILD action.\n'\r
+\r
+#\r
+# Globals\r
+#\r
+gArgs = None\r
+\r
+def Log(Message):\r
+  if not gArgs.Verbose:\r
+    return\r
+  sys.stdout.write (__prog__ + ': ' + Message + '\n')\r
+\r
+def Error(Message, ExitValue=1):\r
+  sys.stderr.write (__prog__ + ': ERROR: ' + Message + '\n')\r
+  sys.exit (ExitValue)\r
+\r
+def RelativePath(target):\r
+  return os.path.relpath (target, gWorkspace)\r
+\r
+def NormalizePath(target):\r
+  if isinstance(target, tuple):\r
+    return os.path.normpath (os.path.join (*target))\r
+  else:\r
+    return os.path.normpath (target)\r
+\r
+if __name__ == '__main__':\r
+  #\r
+  # Create command line argument parser object\r
+  #\r
+  parser = argparse.ArgumentParser (\r
+                      prog = __prog__,\r
+                      version = __version__,\r
+                      description = __description__ + __copyright__,\r
+                      conflict_handler = 'resolve'\r
+                      )\r
+  parser.add_argument (\r
+           '-a', '--arch', dest = 'Arch', nargs = '+', action = 'append',\r
+           required = True,\r
+           help = '''ARCHS is one of list: IA32, X64, IPF, ARM, AARCH64 or EBC,\r
+                     which overrides target.txt's TARGET_ARCH definition. To\r
+                     specify more archs, please repeat this option.'''\r
+           )\r
+  parser.add_argument (\r
+           '-t', '--tagname', dest = 'ToolChain', required = True,\r
+           help = '''Using the Tool Chain Tagname to build the platform,\r
+                     overriding target.txt's TOOL_CHAIN_TAG definition.'''\r
+           )\r
+  parser.add_argument (\r
+           '-p', '--platform', dest = 'PlatformFile', required = True,\r
+           help = '''Build the platform specified by the DSC file name argument,\r
+                     overriding target.txt's ACTIVE_PLATFORM definition.'''\r
+           )\r
+  parser.add_argument (\r
+           '-b', '--buildtarget', dest = 'BuildTarget', required = True,\r
+           help = '''Using the TARGET to build the platform, overriding\r
+                     target.txt's TARGET definition.'''\r
+           )\r
+  parser.add_argument (\r
+           '--conf=', dest = 'ConfDirectory', required = True,\r
+           help = '''Specify the customized Conf directory.'''\r
+           )\r
+  parser.add_argument (\r
+           '-D', '--define', dest = 'Define', nargs='*', action = 'append',\r
+           help = '''Macro: "Name [= Value]".'''\r
+           )\r
+  parser.add_argument (\r
+           '--makefile', dest = 'Makefile', required = True,\r
+           help = '''Makefile to run passing in arguments as makefile defines.'''\r
+           )\r
+  parser.add_argument (\r
+           '-v', '--verbose', dest = 'Verbose', action = 'store_true',\r
+           help = '''Turn on verbose output with informational messages printed'''\r
+           )\r
+\r
+  #\r
+  # Parse command line arguments\r
+  #\r
+  gArgs, remaining = parser.parse_known_args()\r
+  gArgs.BuildType = 'all'\r
+  for BuildType in ['all', 'fds', 'genc', 'genmake', 'clean', 'cleanall', 'modules', 'libraries', 'run']:\r
+    if BuildType in remaining:\r
+      gArgs.BuildType = BuildType\r
+      remaining.remove(BuildType)\r
+      break\r
+  gArgs.Remaining = ' '.join(remaining)\r
+\r
+  #\r
+  # Start\r
+  #\r
+  Log ('Start')\r
+\r
+  #\r
+  # Find makefile in WORKSPACE or PACKAGES_PATH\r
+  #\r
+  PathList = ['']\r
+  try:\r
+    PathList.append(os.environ['WORKSPACE'])\r
+  except:\r
+    Error ('WORKSPACE environment variable not set')\r
+  try:\r
+    PathList += os.environ['PACKAGES_PATH'].split(os.pathsep)\r
+  except:\r
+    pass\r
+  for Path in PathList:\r
+    Makefile = NormalizePath((Path, gArgs.Makefile))\r
+    if os.path.exists (Makefile):\r
+      break\r
+  if not os.path.exists(Makefile):\r
+    Error ('makefile %s not found' % (gArgs.Makefile))\r
+\r
+  #\r
+  # Build command line arguments converting build arguments to makefile defines\r
+  #\r
+  CommandLine = [Makefile]\r
+  CommandLine.append('TARGET_ARCH="%s"' % (' '.join([Item[0] for Item in gArgs.Arch])))\r
+  CommandLine.append('TOOL_CHAIN_TAG="%s"' % (gArgs.ToolChain))\r
+  CommandLine.append('TARGET="%s"' % (gArgs.BuildTarget))\r
+  CommandLine.append('ACTIVE_PLATFORM="%s"' % (gArgs.PlatformFile))\r
+  CommandLine.append('CONF_DIRECTORY="%s"' % (gArgs.ConfDirectory))\r
+  if gArgs.Define:\r
+    for Item in gArgs.Define:\r
+      if '=' not in Item[0]:\r
+        continue\r
+      Item = Item[0].split('=',1)\r
+      CommandLine.append('%s="%s"' % (Item[0], Item[1]))\r
+  CommandLine.append('EXTRA_FLAGS="%s"' % (gArgs.Remaining))\r
+  CommandLine.append(gArgs.BuildType)\r
+  if sys.platform == "win32":\r
+    CommandLine = 'nmake /f %s' % (' '.join(CommandLine))\r
+  else:\r
+    CommandLine = 'make -f %s' % (' '.join(CommandLine))\r
+\r
+  #\r
+  # Run the makefile\r
+  #\r
+  try:\r
+    Process = subprocess.Popen(CommandLine, shell=True)\r
+  except:\r
+    Error ('make command not available.  Please verify PATH')\r
+  Process.communicate()\r
+\r
+  #\r
+  # Done\r
+  #\r
+  Log ('Done')\r
+\r
+  #\r
+  # Return status from running the makefile\r
+  #\r
+  sys.exit(Process.returncode)\r