targets.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict'
  2. const common = require('./common')
  3. const execSync = require('child_process').execSync
  4. const semver = require('semver')
  5. const officialArchs = ['ia32', 'x64', 'armv7l', 'arm64', 'mips64el']
  6. const officialPlatforms = ['darwin', 'linux', 'mas', 'win32']
  7. const officialPlatformArchCombos = {
  8. darwin: ['x64'],
  9. linux: ['ia32', 'x64', 'armv7l', 'arm64', 'mips64el'],
  10. mas: ['x64'],
  11. win32: ['ia32', 'x64']
  12. }
  13. const minimumLinuxArchBuildVersions = {
  14. arm64: '1.8.0',
  15. mips64el: '1.8.2-beta.5'
  16. }
  17. // Maps to module filename for each platform (lazy-required if used)
  18. const osModules = {
  19. darwin: './mac',
  20. linux: './linux',
  21. mas: './mac', // map to darwin
  22. win32: './win32'
  23. }
  24. const supported = {
  25. arch: new Set(officialArchs),
  26. platform: new Set(officialPlatforms)
  27. }
  28. function createPlatformArchPairs (opts, selectedPlatforms, selectedArchs, ignoreFunc) {
  29. let combinations = []
  30. for (const arch of selectedArchs) {
  31. for (const platform of selectedPlatforms) {
  32. if (usingOfficialElectronPackages(opts)) {
  33. if (!validOfficialPlatformArch(opts, platform, arch)) {
  34. warnIfAllNotSpecified(opts, `The platform/arch combination ${platform}/${arch} is not currently supported by Electron Packager`)
  35. continue
  36. } else if (platform === 'linux') {
  37. const minimumBuildVersion = minimumLinuxArchBuildVersions[arch]
  38. if (minimumBuildVersion && !officialLinuxBuildExists(opts, minimumBuildVersion)) {
  39. warnIfAllNotSpecified(opts, `Official linux/${arch} support only exists in Electron ${minimumBuildVersion} and above`)
  40. continue
  41. }
  42. }
  43. if (typeof ignoreFunc === 'function' && ignoreFunc(platform, arch)) continue
  44. }
  45. combinations.push([platform, arch])
  46. }
  47. }
  48. return combinations
  49. }
  50. function unsupportedListOption (name, value, supported) {
  51. return new Error(`Unsupported ${name}=${value} (${typeof value}); must be a string matching: ${Array.from(supported.values()).join(', ')}`)
  52. }
  53. function usingOfficialElectronPackages (opts) {
  54. return !opts.download || !opts.download.hasOwnProperty('mirror')
  55. }
  56. function validOfficialPlatformArch (opts, platform, arch) {
  57. return officialPlatformArchCombos[platform] && officialPlatformArchCombos[platform].indexOf(arch) !== -1
  58. }
  59. function officialLinuxBuildExists (opts, minimumBuildVersion) {
  60. return semver.gte(opts.electronVersion, minimumBuildVersion)
  61. }
  62. function allPlatformsOrArchsSpecified (opts) {
  63. return opts.all || opts.arch === 'all' || opts.platform === 'all'
  64. }
  65. function warnIfAllNotSpecified (opts, message) {
  66. if (!allPlatformsOrArchsSpecified(opts)) {
  67. common.warning(message)
  68. }
  69. }
  70. function hostArch () {
  71. if (process.arch === 'arm') {
  72. switch (process.config.variables.arm_version) {
  73. case '6':
  74. return module.exports.unameArch()
  75. case '7':
  76. return 'armv7l'
  77. default:
  78. common.warning(`Could not determine specific ARM arch. Detected ARM version: ${JSON.stringify(process.config.variables.arm_version)}`)
  79. }
  80. }
  81. return process.arch
  82. }
  83. module.exports = {
  84. allOfficialArchsForPlatformAndVersion: function allOfficialArchsForPlatformAndVersion (platform, electronVersion) {
  85. const archs = officialPlatformArchCombos[platform]
  86. if (platform === 'linux') {
  87. const excludedArchs = Object.keys(minimumLinuxArchBuildVersions)
  88. .filter(arch => !officialLinuxBuildExists({electronVersion: electronVersion}, minimumLinuxArchBuildVersions[arch]))
  89. return archs.filter(arch => excludedArchs.indexOf(arch) === -1)
  90. }
  91. return archs
  92. },
  93. createPlatformArchPairs: createPlatformArchPairs,
  94. hostArch: hostArch,
  95. officialArchs: officialArchs,
  96. officialPlatformArchCombos: officialPlatformArchCombos,
  97. officialPlatforms: officialPlatforms,
  98. osModules: osModules,
  99. supported: supported,
  100. /**
  101. * Returns the arch name from the `uname` utility.
  102. */
  103. unameArch: function unameArch () {
  104. /* istanbul ignore next */
  105. return execSync('uname -m').toString().trim()
  106. },
  107. // Validates list of architectures or platforms.
  108. // Returns a normalized array if successful, or throws an Error.
  109. validateListFromOptions: function validateListFromOptions (opts, name) {
  110. if (opts.all) return Array.from(supported[name].values())
  111. let list = opts[name]
  112. if (!list) {
  113. if (name === 'arch') {
  114. list = hostArch()
  115. } else {
  116. list = process[name]
  117. }
  118. } else if (list === 'all') {
  119. return Array.from(supported[name].values())
  120. }
  121. if (!Array.isArray(list)) {
  122. if (typeof list === 'string') {
  123. list = list.split(/,\s*/)
  124. } else {
  125. return unsupportedListOption(name, list, supported[name])
  126. }
  127. }
  128. const officialElectronPackages = usingOfficialElectronPackages(opts)
  129. for (let value of list) {
  130. if (officialElectronPackages && !supported[name].has(value)) {
  131. return unsupportedListOption(name, value, supported[name])
  132. }
  133. }
  134. return list
  135. }
  136. }