11cb0ef41Sopenharmony_ci#!/usr/bin/env python
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cifrom __future__ import print_function
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciimport ast
61cb0ef41Sopenharmony_ciimport errno
71cb0ef41Sopenharmony_ciimport os
81cb0ef41Sopenharmony_ciimport shutil
91cb0ef41Sopenharmony_ciimport sys
101cb0ef41Sopenharmony_ciimport re
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci# set at init time
131cb0ef41Sopenharmony_cinode_prefix = '/usr/local' # PREFIX variable from Makefile
141cb0ef41Sopenharmony_ciinstall_path = '' # base target directory (DESTDIR + PREFIX from Makefile)
151cb0ef41Sopenharmony_citarget_defaults = None
161cb0ef41Sopenharmony_civariables = None
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cidef abspath(*args):
191cb0ef41Sopenharmony_ci  path = os.path.join(*args)
201cb0ef41Sopenharmony_ci  return os.path.abspath(path)
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cidef load_config():
231cb0ef41Sopenharmony_ci  with open('config.gypi') as f:
241cb0ef41Sopenharmony_ci    return ast.literal_eval(f.read())
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cidef try_unlink(path):
271cb0ef41Sopenharmony_ci  try:
281cb0ef41Sopenharmony_ci    os.unlink(path)
291cb0ef41Sopenharmony_ci  except OSError as e:
301cb0ef41Sopenharmony_ci    if e.errno != errno.ENOENT: raise
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cidef try_symlink(source_path, link_path):
331cb0ef41Sopenharmony_ci  print('symlinking %s -> %s' % (source_path, link_path))
341cb0ef41Sopenharmony_ci  try_unlink(link_path)
351cb0ef41Sopenharmony_ci  try_mkdir_r(os.path.dirname(link_path))
361cb0ef41Sopenharmony_ci  os.symlink(source_path, link_path)
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cidef try_mkdir_r(path):
391cb0ef41Sopenharmony_ci  try:
401cb0ef41Sopenharmony_ci    os.makedirs(path)
411cb0ef41Sopenharmony_ci  except OSError as e:
421cb0ef41Sopenharmony_ci    if e.errno != errno.EEXIST: raise
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cidef try_rmdir_r(path):
451cb0ef41Sopenharmony_ci  path = abspath(path)
461cb0ef41Sopenharmony_ci  while path.startswith(install_path):
471cb0ef41Sopenharmony_ci    try:
481cb0ef41Sopenharmony_ci      os.rmdir(path)
491cb0ef41Sopenharmony_ci    except OSError as e:
501cb0ef41Sopenharmony_ci      if e.errno == errno.ENOTEMPTY: return
511cb0ef41Sopenharmony_ci      if e.errno == errno.ENOENT: return
521cb0ef41Sopenharmony_ci      raise
531cb0ef41Sopenharmony_ci    path = abspath(path, '..')
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cidef mkpaths(path, dst):
561cb0ef41Sopenharmony_ci  if dst.endswith('/'):
571cb0ef41Sopenharmony_ci    target_path = abspath(install_path, dst, os.path.basename(path))
581cb0ef41Sopenharmony_ci  else:
591cb0ef41Sopenharmony_ci    target_path = abspath(install_path, dst)
601cb0ef41Sopenharmony_ci  return path, target_path
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cidef try_copy(path, dst):
631cb0ef41Sopenharmony_ci  source_path, target_path = mkpaths(path, dst)
641cb0ef41Sopenharmony_ci  print('installing %s' % target_path)
651cb0ef41Sopenharmony_ci  try_mkdir_r(os.path.dirname(target_path))
661cb0ef41Sopenharmony_ci  try_unlink(target_path) # prevent ETXTBSY errors
671cb0ef41Sopenharmony_ci  return shutil.copy2(source_path, target_path)
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cidef try_remove(path, dst):
701cb0ef41Sopenharmony_ci  source_path, target_path = mkpaths(path, dst)
711cb0ef41Sopenharmony_ci  print('removing %s' % target_path)
721cb0ef41Sopenharmony_ci  try_unlink(target_path)
731cb0ef41Sopenharmony_ci  try_rmdir_r(os.path.dirname(target_path))
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_cidef install(paths, dst):
761cb0ef41Sopenharmony_ci  for path in paths:
771cb0ef41Sopenharmony_ci    try_copy(path, dst)
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_cidef uninstall(paths, dst):
801cb0ef41Sopenharmony_ci  for path in paths:
811cb0ef41Sopenharmony_ci    try_remove(path, dst)
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_cidef package_files(action, name, bins):
841cb0ef41Sopenharmony_ci  target_path = 'lib/node_modules/' + name + '/'
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  # don't install npm if the target path is a symlink, it probably means
871cb0ef41Sopenharmony_ci  # that a dev version of npm is installed there
881cb0ef41Sopenharmony_ci  if os.path.islink(abspath(install_path, target_path)): return
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  # npm has a *lot* of files and it'd be a pain to maintain a fixed list here
911cb0ef41Sopenharmony_ci  # so we walk its source directory instead...
921cb0ef41Sopenharmony_ci  root = 'deps/' + name
931cb0ef41Sopenharmony_ci  for dirname, subdirs, basenames in os.walk(root, topdown=True):
941cb0ef41Sopenharmony_ci    subdirs[:] = [subdir for subdir in subdirs if subdir != 'test']
951cb0ef41Sopenharmony_ci    paths = [os.path.join(dirname, basename) for basename in basenames]
961cb0ef41Sopenharmony_ci    action(paths, target_path + dirname[len(root) + 1:] + '/')
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  # create/remove symlinks
991cb0ef41Sopenharmony_ci  for bin_name, bin_target in bins.items():
1001cb0ef41Sopenharmony_ci    link_path = abspath(install_path, 'bin/' + bin_name)
1011cb0ef41Sopenharmony_ci    if action == uninstall:
1021cb0ef41Sopenharmony_ci      action([link_path], 'bin/' + bin_name)
1031cb0ef41Sopenharmony_ci    elif action == install:
1041cb0ef41Sopenharmony_ci      try_symlink('../lib/node_modules/' + name + '/' + bin_target, link_path)
1051cb0ef41Sopenharmony_ci    else:
1061cb0ef41Sopenharmony_ci      assert 0  # unhandled action type
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_cidef npm_files(action):
1091cb0ef41Sopenharmony_ci  package_files(action, 'npm', {
1101cb0ef41Sopenharmony_ci    'npm': 'bin/npm-cli.js',
1111cb0ef41Sopenharmony_ci    'npx': 'bin/npx-cli.js',
1121cb0ef41Sopenharmony_ci  })
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_cidef corepack_files(action):
1151cb0ef41Sopenharmony_ci  package_files(action, 'corepack', {
1161cb0ef41Sopenharmony_ci    'corepack': 'dist/corepack.js',
1171cb0ef41Sopenharmony_ci#   Not the default just yet:
1181cb0ef41Sopenharmony_ci#   'yarn': 'dist/yarn.js',
1191cb0ef41Sopenharmony_ci#   'yarnpkg': 'dist/yarn.js',
1201cb0ef41Sopenharmony_ci#   'pnpm': 'dist/pnpm.js',
1211cb0ef41Sopenharmony_ci#   'pnpx': 'dist/pnpx.js',
1221cb0ef41Sopenharmony_ci  })
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  # On z/OS, we install node-gyp for convenience, as some vendors don't have
1251cb0ef41Sopenharmony_ci  # external access and may want to build native addons.
1261cb0ef41Sopenharmony_ci  if sys.platform == 'zos':
1271cb0ef41Sopenharmony_ci    link_path = abspath(install_path, 'bin/node-gyp')
1281cb0ef41Sopenharmony_ci    if action == uninstall:
1291cb0ef41Sopenharmony_ci      action([link_path], 'bin/node-gyp')
1301cb0ef41Sopenharmony_ci    elif action == install:
1311cb0ef41Sopenharmony_ci      try_symlink('../lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js', link_path)
1321cb0ef41Sopenharmony_ci    else:
1331cb0ef41Sopenharmony_ci      assert 0 # unhandled action type
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_cidef subdir_files(path, dest, action):
1361cb0ef41Sopenharmony_ci  ret = {}
1371cb0ef41Sopenharmony_ci  for dirpath, dirnames, filenames in os.walk(path):
1381cb0ef41Sopenharmony_ci    files_in_path = [dirpath + '/' + f for f in filenames if f.endswith('.h')]
1391cb0ef41Sopenharmony_ci    ret[dest + dirpath.replace(path, '')] = files_in_path
1401cb0ef41Sopenharmony_ci  for subdir, files_in_path in ret.items():
1411cb0ef41Sopenharmony_ci    action(files_in_path, subdir + '/')
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_cidef files(action):
1441cb0ef41Sopenharmony_ci  is_windows = sys.platform == 'win32'
1451cb0ef41Sopenharmony_ci  output_file = 'node'
1461cb0ef41Sopenharmony_ci  output_prefix = 'out/Release/'
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  if is_windows:
1491cb0ef41Sopenharmony_ci    output_file += '.exe'
1501cb0ef41Sopenharmony_ci  action([output_prefix + output_file], 'bin/' + output_file)
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  if 'true' == variables.get('node_shared'):
1531cb0ef41Sopenharmony_ci    if is_windows:
1541cb0ef41Sopenharmony_ci      action([output_prefix + 'libnode.dll'], 'bin/libnode.dll')
1551cb0ef41Sopenharmony_ci      action([output_prefix + 'libnode.lib'], 'lib/libnode.lib')
1561cb0ef41Sopenharmony_ci    elif sys.platform == 'zos':
1571cb0ef41Sopenharmony_ci      # GYP will output to lib.target; see _InstallableTargetInstallPath
1581cb0ef41Sopenharmony_ci      # function in tools/gyp/pylib/gyp/generator/make.py
1591cb0ef41Sopenharmony_ci      output_prefix += 'lib.target/'
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci      output_lib = 'libnode.' + variables.get('shlib_suffix')
1621cb0ef41Sopenharmony_ci      action([output_prefix + output_lib], 'lib/' + output_lib)
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci      # create libnode.x that references libnode.so (C++ addons compat)
1651cb0ef41Sopenharmony_ci      os.system(os.path.dirname(os.path.realpath(__file__)) +
1661cb0ef41Sopenharmony_ci                '/zos/modifysidedeck.sh ' +
1671cb0ef41Sopenharmony_ci                abspath(install_path, 'lib/' + output_lib) + ' ' +
1681cb0ef41Sopenharmony_ci                abspath(install_path, 'lib/libnode.x') + ' libnode.so')
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci      # install libnode.version.so
1711cb0ef41Sopenharmony_ci      so_name = 'libnode.' + re.sub(r'\.x$', '.so', variables.get('shlib_suffix'))
1721cb0ef41Sopenharmony_ci      action([output_prefix + so_name], variables.get('libdir') + '/' + so_name)
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci      # create symlink of libnode.so -> libnode.version.so (C++ addons compat)
1751cb0ef41Sopenharmony_ci      link_path = abspath(install_path, 'lib/libnode.so')
1761cb0ef41Sopenharmony_ci      try_symlink(so_name, link_path)
1771cb0ef41Sopenharmony_ci    else:
1781cb0ef41Sopenharmony_ci      output_lib = 'libnode.' + variables.get('shlib_suffix')
1791cb0ef41Sopenharmony_ci      action([output_prefix + output_lib], variables.get('libdir') + '/' + output_lib)
1801cb0ef41Sopenharmony_ci  if 'true' == variables.get('node_use_dtrace'):
1811cb0ef41Sopenharmony_ci    action(['out/Release/node.d'], 'lib/dtrace/node.d')
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci  # behave similarly for systemtap
1841cb0ef41Sopenharmony_ci  action(['src/node.stp'], 'share/systemtap/tapset/')
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci  action(['deps/v8/tools/gdbinit'], 'share/doc/node/')
1871cb0ef41Sopenharmony_ci  action(['deps/v8/tools/lldb_commands.py'], 'share/doc/node/')
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci  if 'freebsd' in sys.platform or 'openbsd' in sys.platform:
1901cb0ef41Sopenharmony_ci    action(['doc/node.1'], 'man/man1/')
1911cb0ef41Sopenharmony_ci  else:
1921cb0ef41Sopenharmony_ci    action(['doc/node.1'], 'share/man/man1/')
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci  if 'true' == variables.get('node_install_npm'):
1951cb0ef41Sopenharmony_ci    npm_files(action)
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci  if 'true' == variables.get('node_install_corepack'):
1981cb0ef41Sopenharmony_ci    corepack_files(action)
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  headers(action)
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_cidef headers(action):
2031cb0ef41Sopenharmony_ci  def wanted_v8_headers(files_arg, dest):
2041cb0ef41Sopenharmony_ci    v8_headers = [
2051cb0ef41Sopenharmony_ci      'deps/v8/include/cppgc/common.h',
2061cb0ef41Sopenharmony_ci      'deps/v8/include/libplatform/libplatform.h',
2071cb0ef41Sopenharmony_ci      'deps/v8/include/libplatform/libplatform-export.h',
2081cb0ef41Sopenharmony_ci      'deps/v8/include/libplatform/v8-tracing.h',
2091cb0ef41Sopenharmony_ci      'deps/v8/include/v8.h',
2101cb0ef41Sopenharmony_ci      'deps/v8/include/v8-array-buffer.h',
2111cb0ef41Sopenharmony_ci      'deps/v8/include/v8-callbacks.h',
2121cb0ef41Sopenharmony_ci      'deps/v8/include/v8-container.h',
2131cb0ef41Sopenharmony_ci      'deps/v8/include/v8-context.h',
2141cb0ef41Sopenharmony_ci      'deps/v8/include/v8-data.h',
2151cb0ef41Sopenharmony_ci      'deps/v8/include/v8-date.h',
2161cb0ef41Sopenharmony_ci      'deps/v8/include/v8-debug.h',
2171cb0ef41Sopenharmony_ci      'deps/v8/include/v8-embedder-heap.h',
2181cb0ef41Sopenharmony_ci      'deps/v8/include/v8-embedder-state-scope.h',
2191cb0ef41Sopenharmony_ci      'deps/v8/include/v8-exception.h',
2201cb0ef41Sopenharmony_ci      'deps/v8/include/v8-extension.h',
2211cb0ef41Sopenharmony_ci      'deps/v8/include/v8-external.h',
2221cb0ef41Sopenharmony_ci      'deps/v8/include/v8-forward.h',
2231cb0ef41Sopenharmony_ci      'deps/v8/include/v8-function-callback.h',
2241cb0ef41Sopenharmony_ci      'deps/v8/include/v8-function.h',
2251cb0ef41Sopenharmony_ci      'deps/v8/include/v8-initialization.h',
2261cb0ef41Sopenharmony_ci      'deps/v8/include/v8-internal.h',
2271cb0ef41Sopenharmony_ci      'deps/v8/include/v8-isolate.h',
2281cb0ef41Sopenharmony_ci      'deps/v8/include/v8-json.h',
2291cb0ef41Sopenharmony_ci      'deps/v8/include/v8-local-handle.h',
2301cb0ef41Sopenharmony_ci      'deps/v8/include/v8-locker.h',
2311cb0ef41Sopenharmony_ci      'deps/v8/include/v8-maybe.h',
2321cb0ef41Sopenharmony_ci      'deps/v8/include/v8-memory-span.h',
2331cb0ef41Sopenharmony_ci      'deps/v8/include/v8-message.h',
2341cb0ef41Sopenharmony_ci      'deps/v8/include/v8-microtask-queue.h',
2351cb0ef41Sopenharmony_ci      'deps/v8/include/v8-microtask.h',
2361cb0ef41Sopenharmony_ci      'deps/v8/include/v8-object.h',
2371cb0ef41Sopenharmony_ci      'deps/v8/include/v8-persistent-handle.h',
2381cb0ef41Sopenharmony_ci      'deps/v8/include/v8-platform.h',
2391cb0ef41Sopenharmony_ci      'deps/v8/include/v8-primitive-object.h',
2401cb0ef41Sopenharmony_ci      'deps/v8/include/v8-primitive.h',
2411cb0ef41Sopenharmony_ci      'deps/v8/include/v8-profiler.h',
2421cb0ef41Sopenharmony_ci      'deps/v8/include/v8-promise.h',
2431cb0ef41Sopenharmony_ci      'deps/v8/include/v8-proxy.h',
2441cb0ef41Sopenharmony_ci      'deps/v8/include/v8-regexp.h',
2451cb0ef41Sopenharmony_ci      'deps/v8/include/v8-script.h',
2461cb0ef41Sopenharmony_ci      'deps/v8/include/v8-snapshot.h',
2471cb0ef41Sopenharmony_ci      'deps/v8/include/v8-statistics.h',
2481cb0ef41Sopenharmony_ci      'deps/v8/include/v8-template.h',
2491cb0ef41Sopenharmony_ci      'deps/v8/include/v8-traced-handle.h',
2501cb0ef41Sopenharmony_ci      'deps/v8/include/v8-typed-array.h',
2511cb0ef41Sopenharmony_ci      'deps/v8/include/v8-unwinder.h',
2521cb0ef41Sopenharmony_ci      'deps/v8/include/v8-value-serializer.h',
2531cb0ef41Sopenharmony_ci      'deps/v8/include/v8-value.h',
2541cb0ef41Sopenharmony_ci      'deps/v8/include/v8-version.h',
2551cb0ef41Sopenharmony_ci      'deps/v8/include/v8-wasm.h',
2561cb0ef41Sopenharmony_ci      'deps/v8/include/v8-weak-callback-info.h',
2571cb0ef41Sopenharmony_ci      'deps/v8/include/v8config.h',
2581cb0ef41Sopenharmony_ci    ]
2591cb0ef41Sopenharmony_ci    files_arg = [name for name in files_arg if name in v8_headers]
2601cb0ef41Sopenharmony_ci    action(files_arg, dest)
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ci  def wanted_zoslib_headers(files_arg, dest):
2631cb0ef41Sopenharmony_ci    import glob
2641cb0ef41Sopenharmony_ci    zoslib_headers = glob.glob(zoslibinc + '/*.h')
2651cb0ef41Sopenharmony_ci    files_arg = [name for name in files_arg if name in zoslib_headers]
2661cb0ef41Sopenharmony_ci    action(files_arg, dest)
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ci  action([
2691cb0ef41Sopenharmony_ci    'common.gypi',
2701cb0ef41Sopenharmony_ci    'config.gypi',
2711cb0ef41Sopenharmony_ci    'src/node.h',
2721cb0ef41Sopenharmony_ci    'src/node_api.h',
2731cb0ef41Sopenharmony_ci    'src/js_native_api.h',
2741cb0ef41Sopenharmony_ci    'src/js_native_api_types.h',
2751cb0ef41Sopenharmony_ci    'src/node_api_types.h',
2761cb0ef41Sopenharmony_ci    'src/node_buffer.h',
2771cb0ef41Sopenharmony_ci    'src/node_object_wrap.h',
2781cb0ef41Sopenharmony_ci    'src/node_version.h',
2791cb0ef41Sopenharmony_ci  ], 'include/node/')
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci  # Add the expfile that is created on AIX
2821cb0ef41Sopenharmony_ci  if sys.platform.startswith('aix') or sys.platform == "os400":
2831cb0ef41Sopenharmony_ci    action(['out/Release/node.exp'], 'include/node/')
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ci  subdir_files('deps/v8/include', 'include/node/', wanted_v8_headers)
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ci  if 'false' == variables.get('node_shared_libuv'):
2881cb0ef41Sopenharmony_ci    subdir_files('deps/uv/include', 'include/node/', action)
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci  if 'true' == variables.get('node_use_openssl') and \
2911cb0ef41Sopenharmony_ci     'false' == variables.get('node_shared_openssl'):
2921cb0ef41Sopenharmony_ci    subdir_files('deps/openssl/openssl/include/openssl', 'include/node/openssl/', action)
2931cb0ef41Sopenharmony_ci    subdir_files('deps/openssl/config/archs', 'include/node/openssl/archs', action)
2941cb0ef41Sopenharmony_ci    subdir_files('deps/openssl/config', 'include/node/openssl', action)
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_ci  if 'false' == variables.get('node_shared_zlib'):
2971cb0ef41Sopenharmony_ci    action([
2981cb0ef41Sopenharmony_ci      'deps/zlib/zconf.h',
2991cb0ef41Sopenharmony_ci      'deps/zlib/zlib.h',
3001cb0ef41Sopenharmony_ci    ], 'include/node/')
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci  if sys.platform == 'zos':
3031cb0ef41Sopenharmony_ci    zoslibinc = os.environ.get('ZOSLIB_INCLUDES')
3041cb0ef41Sopenharmony_ci    if not zoslibinc:
3051cb0ef41Sopenharmony_ci      raise RuntimeError('Environment variable ZOSLIB_INCLUDES is not set\n')
3061cb0ef41Sopenharmony_ci    if not os.path.isfile(zoslibinc + '/zos-base.h'):
3071cb0ef41Sopenharmony_ci      raise RuntimeError('ZOSLIB_INCLUDES is not set to a valid location\n')
3081cb0ef41Sopenharmony_ci    subdir_files(zoslibinc, 'include/node/zoslib/', wanted_zoslib_headers)
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_cidef run(args):
3111cb0ef41Sopenharmony_ci  global node_prefix, install_path, target_defaults, variables
3121cb0ef41Sopenharmony_ci
3131cb0ef41Sopenharmony_ci  # chdir to the project's top-level directory
3141cb0ef41Sopenharmony_ci  os.chdir(abspath(os.path.dirname(__file__), '..'))
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_ci  conf = load_config()
3171cb0ef41Sopenharmony_ci  variables = conf['variables']
3181cb0ef41Sopenharmony_ci  target_defaults = conf['target_defaults']
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci  # argv[2] is a custom install prefix for packagers (think DESTDIR)
3211cb0ef41Sopenharmony_ci  # argv[3] is a custom install prefix (think PREFIX)
3221cb0ef41Sopenharmony_ci  # Difference is that dst_dir won't be included in shebang lines etc.
3231cb0ef41Sopenharmony_ci  dst_dir = args[2] if len(args) > 2 else ''
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci  if len(args) > 3:
3261cb0ef41Sopenharmony_ci    node_prefix = args[3]
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci  # install_path thus becomes the base target directory.
3291cb0ef41Sopenharmony_ci  install_path = dst_dir + node_prefix + '/'
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci  cmd = args[1] if len(args) > 1 else 'install'
3321cb0ef41Sopenharmony_ci
3331cb0ef41Sopenharmony_ci  if os.environ.get('HEADERS_ONLY'):
3341cb0ef41Sopenharmony_ci    if cmd == 'install':
3351cb0ef41Sopenharmony_ci      headers(install)
3361cb0ef41Sopenharmony_ci      return
3371cb0ef41Sopenharmony_ci    if cmd == 'uninstall':
3381cb0ef41Sopenharmony_ci      headers(uninstall)
3391cb0ef41Sopenharmony_ci      return
3401cb0ef41Sopenharmony_ci  else:
3411cb0ef41Sopenharmony_ci    if cmd == 'install':
3421cb0ef41Sopenharmony_ci      files(install)
3431cb0ef41Sopenharmony_ci      return
3441cb0ef41Sopenharmony_ci    if cmd == 'uninstall':
3451cb0ef41Sopenharmony_ci      files(uninstall)
3461cb0ef41Sopenharmony_ci      return
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ci  raise RuntimeError('Bad command: %s\n' % cmd)
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ciif __name__ == '__main__':
3511cb0ef41Sopenharmony_ci  run(sys.argv[:])
352