Skip to content

Commit f1d5842

Browse files
committed
Finished build implementation
1 parent 1e94a66 commit f1d5842

File tree

4 files changed

+79227
-137
lines changed

4 files changed

+79227
-137
lines changed

hook/build.dart

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ import 'dart:io';
33
import 'package:code_assets/code_assets.dart';
44
import 'package:hooks/hooks.dart';
55

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';
6+
const version = '3.5.4';
7+
const sourceCodeUrl = 'https://github.com/openssl/openssl/releases/download/openssl-$version/openssl-$version.tar.gz';
8+
const openSslDirName = 'openssl-$version';
9+
const configArgs = ['no-unit-test', 'no-asm', 'no-makedepend', 'no-ssl', 'no-apps', '-Wl,-headerpad_max_install_names'];
1010

11+
void main(List<String> args) async {
1112
await build(args, (input, output) async {
1213
if (input.config.buildCodeAssets) {
1314
final workDir = Directory(input.outputDirectory.path);
15+
final outputDir = Directory(input.outputDirectoryShared.path);
16+
1417
// download source code from openssl
1518
await runProcess('curl', ['-L', sourceCodeUrl, '-o', '$openSslDirName.tar.gz'], workingDirectory: workDir);
1619

1720
// unzip source code
1821
await runProcess('tar', ['-xzf', '$openSslDirName.tar.gz'], workingDirectory: workDir);
22+
// remove the tar.gz file
23+
await File(workDir.uri.resolve('$openSslDirName.tar.gz').toFilePath()).delete();
1924

2025
final openSslDir = Directory(workDir.uri.resolve(openSslDirName).path);
2126

@@ -28,9 +33,7 @@ void main(List<String> args) async {
2833
await runProcess('perl', [
2934
'Configure',
3035
configName,
31-
'no-unit-test',
32-
'no-asm',
33-
'no-makedepend',
36+
...configArgs,
3437
// needed to build using multiple threads on Windows
3538
'/FS',
3639
], workingDirectory: openSslDir);
@@ -48,21 +51,54 @@ void main(List<String> args) async {
4851
case OS.macOS:
4952
case OS.linux:
5053
// 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);
54+
await runProcess('./Configure', [configName, ...configArgs], workingDirectory: openSslDir);
5755

5856
// run make
5957
await runProcess('make', ['-j', '${Platform.numberOfProcessors}'], workingDirectory: openSslDir);
6058
break;
6159
}
60+
61+
// copy the library to the output directory
62+
final libName = switch ((input.config.code.targetOS, input.config.code.linkModePreference)) {
63+
(OS.windows, LinkModePreference.static || LinkModePreference.preferStatic) => 'libcrypto.lib',
64+
(OS.macOS, LinkModePreference.static || LinkModePreference.preferStatic) => 'libcrypto.a',
65+
(OS.linux, LinkModePreference.static || LinkModePreference.preferStatic) => 'libcrypto.a',
66+
(OS.windows, LinkModePreference.dynamic || LinkModePreference.preferDynamic) => 'libcrypto.dll',
67+
(OS.macOS, LinkModePreference.dynamic || LinkModePreference.preferDynamic) => 'libcrypto.dylib',
68+
(OS.linux, LinkModePreference.dynamic || LinkModePreference.preferDynamic) => 'libcrypto.so',
69+
_ => throw UnsupportedError(
70+
'Unsupported target OS: ${input.config.code.targetOS.name} or link mode preference: ${input.config.code.linkModePreference.name}',
71+
),
72+
};
73+
74+
final libPath = outputDir.uri.resolve(libName).toFilePath();
75+
await File(openSslDir.uri.resolve(libName).path).copy(libPath);
76+
77+
// delete the source code
78+
await openSslDir.delete(recursive: true);
79+
80+
// add the library to dart code assets
81+
output.assets.code.add(
82+
CodeAsset(
83+
package: input.packageName,
84+
name: 'src/third_party/openssl.g.dart',
85+
linkMode: libName.linkMode,
86+
file: outputDir.uri.resolve(libName),
87+
),
88+
);
6289
}
6390
});
6491
}
6592

93+
extension on String {
94+
LinkMode get linkMode {
95+
if (endsWith('.dylib') || endsWith('.so') || endsWith('.dll')) {
96+
return DynamicLoadingBundled();
97+
}
98+
return StaticLinking();
99+
}
100+
}
101+
66102
String resolveConfigName(OS os, Architecture architecture) {
67103
return switch ((os, architecture)) {
68104
(OS.android, Architecture.arm) => 'android-arm',
@@ -97,17 +133,12 @@ String resolveConfigName(OS os, Architecture architecture) {
97133
}
98134

99135
Future<void> runProcess(String executable, List<String> arguments, {Directory? workingDirectory}) async {
100-
print('----------');
101-
print('Running `$executable $arguments` in $workingDirectory');
102136
final processResult = await Process.run(executable, arguments, workingDirectory: workingDirectory?.path);
103-
print('stdout:');
104137
print(processResult.stdout);
105138
if ((processResult.stderr as String).isNotEmpty) {
106-
print('stderr:');
107139
print(processResult.stderr);
108140
}
109141
if (processResult.exitCode != 0) {
110142
throw ProcessException(executable, arguments, '', processResult.exitCode);
111143
}
112-
print('==========');
113144
}

0 commit comments

Comments
 (0)