|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:code_assets/code_assets.dart'; |
| 4 | +import 'package:hooks/hooks.dart'; |
| 5 | + |
| 6 | +void main(List<String> args) async { |
| 7 | + const version = '3.5.4'; |
| 8 | + const sourceCodeUrl = 'https://github.com/openssl/openssl/releases/download/openssl-$version/openssl-$version.tar.gz'; |
| 9 | + const openSslDirName = 'openssl-$version'; |
| 10 | + |
| 11 | + await build(args, (input, output) async { |
| 12 | + if (input.config.buildCodeAssets) { |
| 13 | + final workDir = Directory(input.outputDirectory.path); |
| 14 | + // download source code from openssl |
| 15 | + await runProcess('curl', ['-L', sourceCodeUrl, '-o', '$openSslDirName.tar.gz'], workingDirectory: workDir); |
| 16 | + |
| 17 | + // unzip source code |
| 18 | + await runProcess('tar', ['-xzf', '$openSslDirName.tar.gz'], workingDirectory: workDir); |
| 19 | + |
| 20 | + final openSslDir = Directory(workDir.uri.resolve(openSslDirName).path); |
| 21 | + |
| 22 | + // build source code, depends on the OS we are running on |
| 23 | + // Read https://github.com/openssl/openssl/blob/openssl-3.5.4/INSTALL.md#building-openssl |
| 24 | + final configName = resolveConfigName(input.config.code.targetOS, input.config.code.targetArchitecture); |
| 25 | + switch (OS.current) { |
| 26 | + case OS.windows: |
| 27 | + // run ./Configure with the target OS and architecture |
| 28 | + await runProcess('perl', [ |
| 29 | + 'Configure', |
| 30 | + configName, |
| 31 | + 'no-unit-test', |
| 32 | + 'no-asm', |
| 33 | + 'no-makedepend', |
| 34 | + // needed to build using multiple threads on Windows |
| 35 | + '/FS', |
| 36 | + ], workingDirectory: openSslDir); |
| 37 | + |
| 38 | + // run jom to build the library |
| 39 | + await runProcess('jom', [ |
| 40 | + // TODO: don't know if this is needed |
| 41 | + // 'build_sw', |
| 42 | + '-j', |
| 43 | + '${Platform.numberOfProcessors}', |
| 44 | + '-c', |
| 45 | + 'user.openssl:windows_use_jom=True', |
| 46 | + ], workingDirectory: openSslDir); |
| 47 | + break; |
| 48 | + case OS.macOS: |
| 49 | + case OS.linux: |
| 50 | + // run ./Configure with the target OS and architecture |
| 51 | + await runProcess('./Configure', [ |
| 52 | + configName, |
| 53 | + 'no-unit-test', |
| 54 | + 'no-asm', |
| 55 | + 'no-makedepend', |
| 56 | + ], workingDirectory: openSslDir); |
| 57 | + |
| 58 | + // run make |
| 59 | + await runProcess('make', ['-j', '${Platform.numberOfProcessors}'], workingDirectory: openSslDir); |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +String resolveConfigName(OS os, Architecture architecture) { |
| 67 | + return switch ((os, architecture)) { |
| 68 | + (OS.android, Architecture.arm) => 'android-arm', |
| 69 | + (OS.android, Architecture.arm64) => 'android-arm64', |
| 70 | + (OS.android, Architecture.ia32) => 'android-x86', |
| 71 | + (OS.android, Architecture.x64) => 'android-x86_64', |
| 72 | + (OS.android, Architecture.riscv64) => 'android-riscv64', |
| 73 | + |
| 74 | + (OS.iOS, Architecture.arm) => 'ios-xcrun', |
| 75 | + (OS.iOS, Architecture.arm64) => 'ios64-xcrun', |
| 76 | + (OS.iOS, Architecture.ia32) => 'iossimulator-i386-xcrun', |
| 77 | + (OS.iOS, Architecture.x64) => 'iossimulator-x86_64-xcrun', |
| 78 | + |
| 79 | + (OS.macOS, Architecture.arm64) => 'darwin64-arm64', |
| 80 | + (OS.macOS, Architecture.x64) => 'darwin64-x86_64', |
| 81 | + (OS.macOS, Architecture.ia32) => 'darwin-i386', |
| 82 | + |
| 83 | + (OS.linux, Architecture.arm) => 'linux-armv4', |
| 84 | + (OS.linux, Architecture.arm64) => 'linux-aarch64', |
| 85 | + (OS.linux, Architecture.ia32) => 'linux-x86', |
| 86 | + (OS.linux, Architecture.x64) => 'linux-x86_64', |
| 87 | + (OS.linux, Architecture.riscv32) => 'linux32-riscv32', |
| 88 | + (OS.linux, Architecture.riscv64) => 'linux64-riscv64', |
| 89 | + |
| 90 | + (OS.windows, Architecture.arm) => 'VC-WIN32-ARM', |
| 91 | + (OS.windows, Architecture.arm64) => 'VC-WIN64-ARM', |
| 92 | + (OS.windows, Architecture.ia32) => 'VC-WIN32', |
| 93 | + (OS.windows, Architecture.x64) => 'VC-WIN64A', |
| 94 | + |
| 95 | + _ => throw UnsupportedError('Unsupported target combination: ${os.name}-${architecture.name}'), |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +Future<void> runProcess(String executable, List<String> arguments, {Directory? workingDirectory}) async { |
| 100 | + print('----------'); |
| 101 | + print('Running `$executable $arguments` in $workingDirectory'); |
| 102 | + final processResult = await Process.run(executable, arguments, workingDirectory: workingDirectory?.path); |
| 103 | + print('stdout:'); |
| 104 | + print(processResult.stdout); |
| 105 | + if ((processResult.stderr as String).isNotEmpty) { |
| 106 | + print('stderr:'); |
| 107 | + print(processResult.stderr); |
| 108 | + } |
| 109 | + if (processResult.exitCode != 0) { |
| 110 | + throw ProcessException(executable, arguments, '', processResult.exitCode); |
| 111 | + } |
| 112 | + print('=========='); |
| 113 | +} |
0 commit comments