4a1d35b6325ede5ff9fc338fe7e6fdcaa9fe770767a0114242b817dcba28253f.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. const { execSync } = require('node:child_process')
  2. const { readFileSync, writeFileSync } = require('node:fs')
  3. const path = require('node:path')
  4. const { platform, arch } = require('node:os')
  5. const PLATFORM_NAME = platform()
  6. const HOST_ARCH = arch()
  7. const HOST_LIBC =
  8. PLATFORM_NAME === 'linux' ? (process.report?.getReport()?.header?.glibcVersionRuntime ? 'glibc' : 'musl') : null
  9. const [, , TARGET] = process.argv
  10. let TARGET_TRIPLE = ''
  11. if (TARGET && TARGET.startsWith('--target=')) {
  12. TARGET_TRIPLE = TARGET.replace('--target=', '')
  13. }
  14. // Skia m148 (commit e179431b2b, "[pdf] Allow table based font subsetting")
  15. // introduced a new path in src/pdf/SkPDFSubsetFont.cpp::subset_harfbuzz that
  16. // wraps woff/woff2 typefaces through hb_face_create_for_tables +
  17. // hb_face_set_get_table_tags_func (HB_VERSION_ATLEAST(10,0,0)) and then calls
  18. // hb_subset_or_fail. For woff/woff2 fonts hb_face_count on the raw blob
  19. // returns 0 (HarfBuzz cannot parse woff2 natively), so execution always falls
  20. // through to the table-based path, and hb_subset_or_fail segfaults inside the
  21. // HarfBuzz 13.1.0 subset module on the targets listed below. glibc linux,
  22. // darwin, aarch64-pc-windows-msvc, android, and riscv64 are unaffected, so
  23. // this is very likely a toolchain/ABI interaction in the static-linked musl
  24. // and MSVC x64 builds rather than a pure logic bug. There is no upstream fix
  25. // as of HarfBuzz 14.1.0 and no revert of the Skia commit.
  26. //
  27. // Workaround: disable skia_pdf_subset_harfbuzz on the affected targets. The
  28. // flag is declared in skia/gn/skia.gni:158 and gates both the harfbuzz subset
  29. // dependency and the SK_PDF_USE_HARFBUZZ_SUBSET define at skia/BUILD.gn:1255.
  30. // With it off, SkPDFSubsetFont compiles as the #else branch and returns null,
  31. // which SkPDFFont.cpp:474-478 handles gracefully: "If subsetting fails, fall
  32. // back to original font data." TrueType fonts on these targets are embedded
  33. // whole instead of subsetted (slightly larger PDFs) and woff/woff2 fonts go
  34. // through the pre-m148 Type3 fallback. All other targets keep full subsetting.
  35. const PDF_HARFBUZZ_SUBSET_CRASHING_TARGETS = new Set([
  36. 'x86_64-pc-windows-msvc',
  37. 'x86_64-unknown-linux-musl',
  38. 'aarch64-unknown-linux-musl',
  39. ])
  40. // Windows-latest in skia.yaml invokes this script with no --target= flag
  41. // (native x64 host build), so TARGET_TRIPLE is empty even though the resulting
  42. // binary is x86_64-pc-windows-msvc and is affected by the crash. Match the
  43. // native host explicitly in addition to the --target= lookup.
  44. const IS_NATIVE_WIN_X64 = !TARGET_TRIPLE && PLATFORM_NAME === 'win32' && HOST_ARCH === 'x64'
  45. const PDF_HARFBUZZ_SUBSET_ENABLED =
  46. !PDF_HARFBUZZ_SUBSET_CRASHING_TARGETS.has(TARGET_TRIPLE) && !IS_NATIVE_WIN_X64
  47. function exec(command) {
  48. console.info(command)
  49. execSync(command, {
  50. stdio: 'inherit',
  51. cwd: path.join(__dirname, '..', 'skia'),
  52. env: process.env,
  53. shell: PLATFORM_NAME === 'win32' ? 'powershell' : 'bash',
  54. })
  55. }
  56. if (process.env.SKIP_SYNC_SK_DEPS !== 'false' && process.env.SKIP_SYNC_SK_DEPS !== '0') {
  57. exec('python ./tools/git-sync-deps')
  58. }
  59. let CC = PLATFORM_NAME === 'win32' ? '\\"clang-cl\\"' : '"clang"'
  60. let CXX = PLATFORM_NAME === 'win32' ? '\\"clang-cpp\\"' : '"clang++"'
  61. let ExtraCflagsCC = ''
  62. let ExtraSkiaBuildFlag = ''
  63. let ExtraCflags
  64. let ExtraLdFlags
  65. let ExtraAsmFlags
  66. const GN_ARGS = [
  67. `is_official_build=true`,
  68. `is_component_build=false`,
  69. `is_debug=false`,
  70. `werror=false`,
  71. `paragraph_gms_enabled=false`,
  72. `paragraph_tests_enabled=false`,
  73. `skia_enable_android_utils=false`,
  74. `skia_enable_discrete_gpu=false`,
  75. `skia_enable_ganesh=false`,
  76. `skia_enable_pdf=true`,
  77. `skia_enable_skottie=true`,
  78. `skia_enable_skshaper=true`,
  79. `skia_enable_tools=false`,
  80. `skia_enable_svg=true`,
  81. `skia_enable_skparagraph=true`,
  82. // See PDF_HARFBUZZ_SUBSET_CRASHING_TARGETS above for the crash details.
  83. `skia_pdf_subset_harfbuzz=${PDF_HARFBUZZ_SUBSET_ENABLED}`,
  84. `skia_use_expat=true`,
  85. `skia_use_system_expat=false`,
  86. `skia_use_gl=false`,
  87. `skia_use_harfbuzz=true`,
  88. `skia_use_icu=true`,
  89. // the libavif would conflict with the Rust libavif, use the Rust library to handle avif images
  90. `skia_use_libavif=false`,
  91. `skia_use_libjxl_decode=${!TARGET_TRIPLE.startsWith('riscv64')}`,
  92. `skia_use_libjpeg_turbo_decode=true`,
  93. `skia_use_libjpeg_turbo_encode=true`,
  94. `skia_use_libwebp_decode=true`,
  95. `skia_use_libwebp_encode=true`,
  96. `skia_use_freetype=true`,
  97. `skia_use_freetype_woff2=true`,
  98. `skia_use_fontconfig=false`,
  99. `skia_use_x11=false`,
  100. `skia_use_wuffs=true`,
  101. `skia_use_system_freetype2=false`,
  102. `skia_use_system_libjpeg_turbo=false`,
  103. `skia_use_system_libpng=false`,
  104. `skia_use_system_libwebp=false`,
  105. `skia_use_system_zlib=false`,
  106. `skia_use_system_icu=false`,
  107. `skia_use_system_harfbuzz=false`,
  108. `skia_use_lua=false`,
  109. `skia_use_piex=false`,
  110. `skia_enable_fontmgr_custom_directory=true`,
  111. `skia_enable_fontmgr_custom_embedded=false`,
  112. `skia_enable_fontmgr_custom_empty=true`,
  113. `skia_enable_fontmgr_android=false`,
  114. `skunicode_tests_enabled=false`,
  115. `skia_enable_skshaper_tests=false`,
  116. ]
  117. switch (PLATFORM_NAME) {
  118. case 'win32':
  119. ExtraCflagsCC =
  120. '\\"/std:c++20\\",' +
  121. '\\"/MT\\",' +
  122. '\\"-DSK_FORCE_RASTER_PIPELINE_BLITTER\\",' +
  123. '\\"-DSK_ENABLE_SVG\\",' +
  124. '\\"-DSK_RELEASE\\",' +
  125. '\\"-DSK_DISABLE_TRACING\\",' +
  126. '\\"-DSK_ENCODE_WEBP\\",' +
  127. '\\"-DSK_CODEC_DECODES_WEBP\\",' +
  128. '\\"-DSK_ENCODE_PNG\\",' +
  129. '\\"-DSK_CODEC_DECODES_PNG\\",' +
  130. '\\"-DSK_ENCODE_JPEG\\",' +
  131. '\\"-DSK_CODEC_DECODES_JPEG\\",' +
  132. '\\"-DSK_SHAPER_HARFBUZZ_AVAILABLE\\"'
  133. const clangVersion = findClangWinVersion()
  134. if (clangVersion) {
  135. console.info(`Found clang version: ${clangVersion}`)
  136. ExtraSkiaBuildFlag = `clang_win_version=\\"${clangVersion}\\"`
  137. }
  138. GN_ARGS.push(`clang_win=\\"C:\\\\Program Files\\\\LLVM\\"`)
  139. GN_ARGS.push(`skia_enable_fontmgr_win=false`)
  140. break
  141. case 'linux':
  142. case 'darwin':
  143. ExtraCflagsCC =
  144. '"-std=c++20",' +
  145. '"-fno-exceptions",' +
  146. '"-DSK_FORCE_RASTER_PIPELINE_BLITTER",' +
  147. '"-DSK_ENABLE_SVG",' +
  148. '"-DSK_RELEASE",' +
  149. '"-DSK_DISABLE_TRACING",' +
  150. '"-DSK_ENCODE_WEBP",' +
  151. '"-DSK_CODEC_DECODES_WEBP",' +
  152. '"-DSK_ENCODE_PNG",' +
  153. '"-DSK_CODEC_DECODES_PNG",' +
  154. '"-DSK_ENCODE_JPEG",' +
  155. '"-DSK_CODEC_DECODES_JPEG",' +
  156. '"-DSK_SHAPER_HARFBUZZ_AVAILABLE"'
  157. if (PLATFORM_NAME === 'linux' && !TARGET_TRIPLE && HOST_ARCH === 'x64') {
  158. if (HOST_LIBC === 'glibc') {
  159. ExtraCflagsCC += ',"-stdlib=libc++","-static","-I/usr/lib/llvm-19/include/c++/v1"'
  160. } else {
  161. ExtraCflagsCC += ',"-stdlib=libc++","-static","-I/usr/include/c++/v1","-fPIC","-fno-cxx-exceptions"'
  162. }
  163. }
  164. if (PLATFORM_NAME === 'linux' && (!TARGET_TRIPLE || TARGET_TRIPLE.startsWith('x86_64'))) {
  165. ExtraCflagsCC += ',"-Wno-psabi"'
  166. }
  167. break
  168. default:
  169. throw new TypeError(`Don't support ${PLATFORM_NAME} for now`)
  170. }
  171. switch (TARGET_TRIPLE) {
  172. case 'aarch64-unknown-linux-gnu':
  173. ExtraSkiaBuildFlag += ' target_cpu="arm64" target_os="linux"'
  174. ExtraCflags =
  175. '"--target=aarch64-unknown-linux-gnu", "--sysroot=/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot", "-I/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot/usr/include", "-march=armv8-a"'
  176. ExtraCflagsCC +=
  177. ', "--target=aarch64-unknown-linux-gnu", "--sysroot=/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot", "-I/usr/lib/llvm-19/include/c++/v1", "-I/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot/usr/include", "-march=armv8-a"'
  178. ExtraLdFlags =
  179. '"-fuse-ld=lld", "-L/usr/aarch64-unknown-linux-gnu/lib/llvm-19/lib", "-L/usr/aarch64-unknown-linux-gnu/lib", "-L/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot/lib", "-L/usr/aarch64-unknown-linux-gnu/lib/gcc/aarch64-unknown-linux-gnu/4.8.5"'
  180. ExtraAsmFlags = '"--target=aarch64-unknown-linux-gnu", "-march=armv8-a"'
  181. GN_ARGS.push(
  182. `extra_ldflags=[${ExtraLdFlags}]`,
  183. `ar="llvm-ar-19"`,
  184. `extra_asmflags=[${ExtraAsmFlags}]`,
  185. `extra_cflags=[${ExtraCflags}]`,
  186. `extra_cflags_c=[${ExtraCflags}]`,
  187. )
  188. break
  189. case 'aarch64-unknown-linux-musl':
  190. CC = '"zig cc"'
  191. CXX = '"zig c++"'
  192. ExtraSkiaBuildFlag += ' target_cpu="arm64" target_os="linux"'
  193. ExtraCflags = `"--target=aarch64-linux-musl", "-fPIC", "-march=cortex_a78"`
  194. ExtraCflagsCC += `, "--target=aarch64-linux-musl", "-static", "-fPIC", "-march=cortex_a78"`
  195. ExtraLdFlags = `"--target=aarch64-linux-musl"`
  196. ExtraAsmFlags = '"--target=aarch64-linux-musl", "-march=cortex_a78"'
  197. GN_ARGS.push(
  198. `extra_ldflags=[${ExtraLdFlags}]`,
  199. `ar="zig ar"`,
  200. `extra_asmflags=[${ExtraAsmFlags}]`,
  201. `extra_cflags=[${ExtraCflags}]`,
  202. `extra_cflags_c=[${ExtraCflags}]`,
  203. )
  204. break
  205. case 'x86_64-unknown-linux-musl':
  206. CC = '"zig cc"'
  207. CXX = '"zig c++"'
  208. ExtraSkiaBuildFlag += ' target_cpu="x64" target_os="linux"'
  209. ExtraCflags = `"--target=x86_64-linux-musl", "-fPIC"`
  210. ExtraCflagsCC += `, "--target=x86_64-linux-musl", "-static", "-fPIC", "-march=sandybridge", "-mevex512"`
  211. ExtraLdFlags = `"--target=x86_64-linux-musl"`
  212. ExtraAsmFlags = '"--target=x86_64-linux-musl"'
  213. GN_ARGS.push(
  214. `extra_ldflags=[${ExtraLdFlags}]`,
  215. `ar="zig ar"`,
  216. `extra_asmflags=[${ExtraAsmFlags}]`,
  217. `extra_cflags=[${ExtraCflags}]`,
  218. `extra_cflags_c=[${ExtraCflags}]`,
  219. )
  220. break
  221. case 'armv7-unknown-linux-gnueabihf':
  222. CC = '"arm-linux-gnueabihf-gcc"'
  223. CXX = '"arm-linux-gnueabihf-g++"'
  224. // Disable SkPathData backend - it has issues on 32-bit ARM under QEMU emulation
  225. // The kill switch was added in Chrome m144: skia commit 7f325708d2
  226. ExtraCflagsCC += ',"-DSK_DISABLE_PATHDATA"'
  227. // Use "armv7a" (not "arm") to avoid Skia's zlib bug where ARM CRC32
  228. // (armv8-only) is incorrectly enabled for all ARM targets
  229. ExtraSkiaBuildFlag += ' target_cpu="armv7a" target_os="linux"'
  230. break
  231. case 'aarch64-apple-darwin':
  232. ExtraSkiaBuildFlag += ' target_cpu="arm64" target_os="mac"'
  233. ExtraCflagsCC += ', "--target=arm64-apple-macos", "-mmacosx-version-min=11.0"'
  234. ExtraLdFlags = '"--target=arm64-apple-macos", "-mmacosx-version-min=11.0"'
  235. ExtraAsmFlags = '"--target=arm64-apple-macos", "-mmacosx-version-min=11.0"'
  236. ExtraCflags = '"--target=arm64-apple-macos", "-mmacosx-version-min=11.0"'
  237. GN_ARGS.push(
  238. `extra_ldflags=[${ExtraLdFlags}]`,
  239. `extra_asmflags=[${ExtraAsmFlags}]`,
  240. `extra_cflags=[${ExtraCflags}]`,
  241. `extra_cflags_c=[${ExtraCflags}]`,
  242. )
  243. break
  244. case 'aarch64-linux-android':
  245. const { ANDROID_NDK_LATEST_HOME } = process.env
  246. if (!ANDROID_NDK_LATEST_HOME) {
  247. throw new TypeError('ANDROID_NDK_LATEST_HOME must be specified in env variable')
  248. }
  249. ExtraSkiaBuildFlag += ` target_cpu="arm64" ndk="${ANDROID_NDK_LATEST_HOME}"`
  250. break
  251. case 'x86_64-apple-darwin':
  252. if (HOST_ARCH === 'arm64') {
  253. ExtraSkiaBuildFlag += ' target_cpu="x64" target_os="mac"'
  254. ExtraCflagsCC += ',"-Wno-psabi"'
  255. }
  256. ExtraCflagsCC += ', "-mmacosx-version-min=10.13"'
  257. ExtraLdFlags = ' "-mmacosx-version-min=10.13"'
  258. ExtraAsmFlags = '"-mmacosx-version-min=10.13"'
  259. ExtraCflags = '"-mmacosx-version-min=10.13"'
  260. GN_ARGS.push(
  261. `extra_ldflags=[${ExtraLdFlags}]`,
  262. `extra_asmflags=[${ExtraAsmFlags}]`,
  263. `extra_cflags=[${ExtraCflags}]`,
  264. `extra_cflags_c=[${ExtraCflags}]`,
  265. )
  266. break
  267. case 'riscv64gc-unknown-linux-gnu':
  268. ExtraSkiaBuildFlag += ' target_cpu="riscv64" target_os="linux"'
  269. CC = '"riscv64-linux-gnu-gcc"'
  270. CXX = '"riscv64-linux-gnu-g++"'
  271. break
  272. case 'aarch64-pc-windows-msvc':
  273. ExtraSkiaBuildFlag += ' target_cpu=\\"arm64\\"'
  274. break
  275. case '':
  276. break
  277. default:
  278. throw new TypeError(`[${TARGET_TRIPLE}] is not a valid target`)
  279. }
  280. const OUTPUT_PATH = path.join('out', 'Static')
  281. GN_ARGS.push(`cc=${CC}`, `cxx=${CXX}`, `extra_cflags_cc=[${ExtraCflagsCC}]`, ExtraSkiaBuildFlag)
  282. const SkLoadICUCppFilePath = path.join(__dirname, '..', 'skia', 'third_party', 'icu', 'SkLoadICU.cpp')
  283. const CODE_TO_PATCH = 'good = load_from(executable_directory()) || load_from(library_directory());'
  284. const CODE_I_WANT = 'good = load_from(library_directory()) || load_from(executable_directory());'
  285. const GNConfigPath = path.join(__dirname, '..', 'skia', 'BUILD.gn')
  286. const GNExampleCode = `skia_executable("skia_c_api_example") {
  287. sources = [ "experimental/c-api-example/skia-c-example.c" ]
  288. include_dirs = [ "." ]
  289. deps = [ ":skia" ]
  290. }`
  291. if (PLATFORM_NAME === 'win32') {
  292. const content = readFileSync(SkLoadICUCppFilePath, 'utf8')
  293. const patch = content.replace(CODE_TO_PATCH, CODE_I_WANT)
  294. writeFileSync(SkLoadICUCppFilePath, patch)
  295. process.once('beforeExit', () => {
  296. writeFileSync(SkLoadICUCppFilePath, content)
  297. })
  298. }
  299. const GN_BUILD_CONTENT = readFileSync(GNConfigPath, 'utf8')
  300. writeFileSync(GNConfigPath, GN_BUILD_CONTENT.replace(GNExampleCode, ''))
  301. process.once('beforeExit', () => {
  302. writeFileSync(GNConfigPath, GN_BUILD_CONTENT)
  303. })
  304. exec(
  305. `${process.env.GN_EXE ? process.env.GN_EXE : path.join('bin', 'gn')} gen ${OUTPUT_PATH} --args='${GN_ARGS.join(
  306. ' ',
  307. )}'`,
  308. )
  309. // linux musl
  310. // don't know why generated: python3 ../../third_party/externals/icu/scripts/make_data_assembly.py ../../third_party/externals/icu/common/icudtl.dat gen/third_party/icu/icudtl_dat.S
  311. // `python3` should be `python`
  312. if (process.env.GN_EXE) {
  313. const { readFileSync, writeFileSync } = require('fs')
  314. const { join } = require('path')
  315. const ninjaToolchain = join(__dirname, '..', 'skia', 'out', 'Static', 'toolchain.ninja')
  316. const ninjaToolchainContent = readFileSync(ninjaToolchain, 'utf8')
  317. writeFileSync(ninjaToolchain, ninjaToolchainContent.replace('python3', 'python'))
  318. }
  319. console.time('Build Skia')
  320. exec(`ninja -C ${OUTPUT_PATH}`)
  321. console.timeEnd('Build Skia')
  322. function findClangWinVersion() {
  323. const stdout = execSync('clang --version', {
  324. encoding: 'utf8',
  325. })
  326. const clangVersion = stdout.match(/clang version\s(\d+\.\d+\.\d+)/)
  327. if (!clangVersion) {
  328. return null
  329. }
  330. return clangVersion[1]?.split('.')?.at(0)
  331. }