npm.bbclass 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. DEPENDS_prepend = "nodejs-native "
  2. RDEPENDS_${PN}_prepend = "nodejs "
  3. S = "${WORKDIR}/npmpkg"
  4. NPM_INSTALLDIR = "${D}${libdir}/node_modules/${PN}"
  5. # function maps arch names to npm arch names
  6. def npm_oe_arch_map(target_arch, d):
  7. import re
  8. if re.match('p(pc|owerpc)(|64)', target_arch): return 'ppc'
  9. elif re.match('i.86$', target_arch): return 'ia32'
  10. elif re.match('x86_64$', target_arch): return 'x64'
  11. elif re.match('arm64$', target_arch): return 'arm'
  12. return target_arch
  13. NPM_ARCH ?= "${@npm_oe_arch_map(d.getVar('TARGET_ARCH'), d)}"
  14. NPM_INSTALL_DEV = "0"
  15. npm_do_compile() {
  16. # Copy in any additionally fetched modules
  17. if [ -d ${WORKDIR}/node_modules ] ; then
  18. cp -a ${WORKDIR}/node_modules ${S}/
  19. fi
  20. # changing the home directory to the working directory, the .npmrc will
  21. # be created in this directory
  22. export HOME=${WORKDIR}
  23. if [ "${NPM_INSTALL_DEV}" = "1" ]; then
  24. npm config set dev true
  25. else
  26. npm config set dev false
  27. fi
  28. npm set cache ${WORKDIR}/npm_cache
  29. # clear cache before every build
  30. npm cache clear
  31. # Install pkg into ${S} without going to the registry
  32. if [ "${NPM_INSTALL_DEV}" = "1" ]; then
  33. npm --arch=${NPM_ARCH} --target_arch=${NPM_ARCH} --no-registry install
  34. else
  35. npm --arch=${NPM_ARCH} --target_arch=${NPM_ARCH} --production --no-registry install
  36. fi
  37. }
  38. npm_do_install() {
  39. # changing the home directory to the working directory, the .npmrc will
  40. # be created in this directory
  41. export HOME=${WORKDIR}
  42. mkdir -p ${NPM_INSTALLDIR}/
  43. npm install --prefix ${D}${prefix} -g --arch=${NPM_ARCH} --target_arch=${NPM_ARCH} --production --no-registry
  44. if [ -d ${D}${prefix}/etc ] ; then
  45. # This will be empty
  46. rmdir ${D}${prefix}/etc
  47. fi
  48. }
  49. python populate_packages_prepend () {
  50. instdir = d.expand('${D}${libdir}/node_modules/${PN}')
  51. extrapackages = oe.package.npm_split_package_dirs(instdir)
  52. pkgnames = extrapackages.keys()
  53. d.prependVar('PACKAGES', '%s ' % ' '.join(pkgnames))
  54. for pkgname in pkgnames:
  55. pkgrelpath, pdata = extrapackages[pkgname]
  56. pkgpath = '${libdir}/node_modules/${PN}/' + pkgrelpath
  57. # package names can't have underscores but npm packages sometimes use them
  58. oe_pkg_name = pkgname.replace('_', '-')
  59. expanded_pkgname = d.expand(oe_pkg_name)
  60. d.setVar('FILES_%s' % expanded_pkgname, pkgpath)
  61. if pdata:
  62. version = pdata.get('version', None)
  63. if version:
  64. d.setVar('PKGV_%s' % expanded_pkgname, version)
  65. description = pdata.get('description', None)
  66. if description:
  67. d.setVar('SUMMARY_%s' % expanded_pkgname, description.replace(u"\u2018", "'").replace(u"\u2019", "'"))
  68. d.appendVar('RDEPENDS_%s' % d.getVar('PN'), ' %s' % ' '.join(pkgnames).replace('_', '-'))
  69. }
  70. FILES_${PN} += " \
  71. ${libdir}/node_modules/${PN} \
  72. "
  73. EXPORT_FUNCTIONS do_compile do_install