Skip to content

Commit ce9fd3f

Browse files
authored
Merge pull request #66 from Geod24/openssl-3
Move the version detection script from `vibe-d:tls` to `deimos/openssl`
2 parents a033f18 + 74c1adb commit ce9fd3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+312
-89
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated version file
2+
/source/deimos/openssl/version_.d
3+
4+
# DUB artifacts
5+
/.dub/
6+
/openssl
7+
/*-test-library

README

Lines changed: 0 additions & 31 deletions
This file was deleted.

README.md

Lines changed: 36 additions & 0 deletions

dub.sdl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@ description "Deimos bindings for the OpenSSL cryptographic library"
33
homepage "http://www.openssl.org/"
44
license "OpenSSL or SSLeay"
55
libs "ssl" "crypto" platform="posix"
6-
importPaths "."
76

8-
configuration "library" {
7+
configuration "library-autodetect" {
98
targetType "sourceLibrary"
10-
excludedSourceFiles "deimos/openssl/applink.d"
9+
excludedSourceFiles "source/deimos/openssl/applink.d"
10+
preGenerateCommands `${DUB} scripts/generate_version.d` platform="posix"
11+
versions `DeimosOpenSSLAutoDetect`
1112
}
1213

14+
configuration "library-manual-version" {
15+
targetType "sourceLibrary"
16+
excludedSourceFiles "source/deimos/openssl/applink.d"
17+
}
18+
19+
// Includes a module to replace `applink.c` as described in:
20+
// https://www.openssl.org/docs/manmaster/man3/OPENSSL_Applink.html
1321
configuration "library-applink" {
1422
targetType "sourceLibrary"
1523
}
1624

1725
configuration "unittest" {
1826
targetType "executable"
1927
dflags "-main"
20-
sourcePaths "deimos/openssl"
21-
excludedSourceFiles "deimos/openssl/applink.d"
28+
excludedSourceFiles "source/deimos/openssl/applink.d"
29+
preGenerateCommands `${DUB} scripts/generate_version.d` platform="posix"
30+
versions `DeimosOpenSSLAutoDetect`
2231
}

scripts/generate_version.d

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/+ dub.sdl:
2+
name "script"
3+
+/
4+
5+
/**
6+
* This program will attempt to detect which version of openssl is installed
7+
*
8+
* End-users might have different versions of OpenSSL installed.
9+
* The version might ever differ among members of a development team.
10+
*
11+
* This script attempts to first calls `pkg-config` to find out the version,
12+
* then reverts to calling the `openssl` binary if `pkg-config` didn't work.
13+
*
14+
* It is called directly as a `preGenerateCommand` (see dub.sdl).
15+
* To use it with another build system, pass the directory in which to write
16+
* the `version_.d` file as first and only argument. The directory
17+
* must exist, this script will not create it.
18+
*/
19+
module generate_version;
20+
21+
import std.algorithm;
22+
import std.conv;
23+
import std.file;
24+
import std.functional;
25+
import std.path;
26+
import std.process;
27+
import std.range;
28+
import std.stdio;
29+
import std.string;
30+
import std.uni;
31+
32+
// file full path is: $SOME_PATH/openssl/scripts/generate_version.d
33+
// We want: $SOME_PATH/openssl/deimos/openssl/
34+
immutable TARGET_DIR_PATH = __FILE_FULL_PATH__
35+
.dirName.dirName.buildPath("source", "deimos", "openssl");
36+
37+
void main(string[] args)
38+
{
39+
string target;
40+
41+
if (args.length == 2)
42+
{
43+
assert(args[1].isDir(),
44+
"OpenSSL version detection: Argument '" ~ args[1] ~ "' is not a directory");
45+
target = args[1].buildPath("version_.d");
46+
}
47+
else
48+
{
49+
assert(args.length == 1,
50+
"OpenSSL version detection expects only one argument, " ~
51+
"a directory path where to write `version_.d`");
52+
target = TARGET_DIR_PATH.buildPath("version_.d");
53+
}
54+
55+
string opensslVersion;
56+
try
57+
{
58+
const res = execute(["pkg-config", "openssl", "--modversion"]);
59+
if (res.status == 0)
60+
opensslVersion = res.output.strip();
61+
}
62+
catch (Exception e) {}
63+
64+
if (!opensslVersion.length) try
65+
{
66+
const res = execute(["openssl", "version"]).output;
67+
if (res.canFind("OpenSSL "))
68+
{
69+
opensslVersion = res.splitter(" ").dropOne.front.filter!(not!(std.uni.isAlpha)).text;
70+
}
71+
else if (res.canFind("LibreSSL "))
72+
{
73+
writeln("\tWarning: Your default openssl binary points to LibreSSL, which is not supported.");
74+
version (OSX)
75+
{
76+
writeln("\tOn Mac OSX, this is the default behavior.");
77+
writeln("\tIf you installed openssl via a package manager, you need to tell DUB how to find it.");
78+
writeln("\tAssuming brew, run [brew link openssl] and follow the instructions for pkg-config.\n");
79+
}
80+
}
81+
}
82+
catch (Exception e) {}
83+
84+
if (!opensslVersion.length)
85+
{
86+
writeln("\tWarning: Could not find OpenSSL version via pkg-config nor by calling the openssl binary.");
87+
writeln("\tAssuming version 1.1.0.");
88+
writeln("\tYou might need to export PKG_CONFIG_PATH or install the openssl package if you have a library-only package.");
89+
opensslVersion = "1.1.0h";
90+
}
91+
auto data = format(q{/**
92+
* Provide the version of the libssl being linked to at compile time
93+
*
94+
* This module was auto-generated by deimos/openssl's script/generate_version.d
95+
* Manual edit might get overwritten by later build.
96+
*
97+
* This module should not be directly dependend upon.
98+
* Instead, use `deimos.openssl.opensslv`, which handles explicit overrides
99+
* provides a uniform interface, and a few utilities.
100+
*/
101+
module deimos.openssl.version_;
102+
103+
/// Ditto
104+
package enum OpenSSLTextVersion = "%s";
105+
}, opensslVersion);
106+
107+
// Only write the file iff it has changed or didn't exist before.
108+
// This way timestamp-based build system will not rebuild,
109+
// and changes on the installed OpenSSL will be correctly detected.
110+
if (!target.exists || target.readText.strip != data.strip)
111+
data.toFile(target);
112+
}
File renamed without changes.
File renamed without changes.
Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
/**
2+
* Glue between OpenSSL BIO and Win32 compiler run-time
3+
*
4+
* Duplicate the content of the `applink.c` source file
5+
* to avoid linking it in user code without adding a dependency
6+
* to a C build system/compiler.
7+
*
8+
* See_Also: https://www.openssl.org/docs/manmaster/man3/OPENSSL_Applink.html
9+
*/
110
module deimos.openssl.applink;
11+
212
import core.stdc.stdio;
313
import std.stdio : _fileno, _setmode, _O_BINARY;
414
import core.sys.posix.fcntl;
@@ -35,53 +45,53 @@ enum _O_TEXT = 0x4000;
3545

3646
extern(C)
3747
{
38-
void *app_stdin()
39-
{
40-
return cast(void*)stdin;
48+
void *app_stdin()
49+
{
50+
return cast(void*)stdin;
4151
}
42-
43-
void *app_stdout()
44-
{
45-
return cast(void*)stdout;
52+
53+
void *app_stdout()
54+
{
55+
return cast(void*)stdout;
4656
}
47-
48-
void *app_stderr()
49-
{
50-
return cast(void*)stderr;
57+
58+
void *app_stderr()
59+
{
60+
return cast(void*)stderr;
5161
}
52-
53-
int app_feof(FILE *fp)
54-
{
55-
return feof(fp);
62+
63+
int app_feof(FILE *fp)
64+
{
65+
return feof(fp);
5666
}
57-
58-
int app_ferror(FILE *fp)
59-
{
60-
return ferror(fp);
67+
68+
int app_ferror(FILE *fp)
69+
{
70+
return ferror(fp);
6171
}
62-
72+
6373
void app_clearerr(FILE *fp)
64-
{
65-
clearerr(fp);
74+
{
75+
clearerr(fp);
6676
}
67-
68-
int app_fileno(FILE *fp)
69-
{
70-
return _fileno(fp);
77+
78+
int app_fileno(FILE *fp)
79+
{
80+
return _fileno(fp);
7181
}
72-
82+
7383
int app_fsetmod(FILE *fp, char mod)
74-
{
75-
return _setmode (_fileno(fp),mod=='b'?_O_BINARY:_O_TEXT);
84+
{
85+
return _setmode (_fileno(fp),mod=='b'?_O_BINARY:_O_TEXT);
7686
}
77-
87+
7888
__gshared bool once = true;
7989
__gshared void*[APPLINK_MAX+1] OPENSSL_ApplinkTable = cast(void*)APPLINK_MAX;
80-
90+
8191
export void** OPENSSL_Applink()
82-
{
92+
{
8393
if (once)
84-
{
94+
{
8595
OPENSSL_ApplinkTable[APPLINK_STDIN] = &app_stdin;
8696
OPENSSL_ApplinkTable[APPLINK_STDOUT] = &app_stdout;
8797
OPENSSL_ApplinkTable[APPLINK_STDERR] = &app_stderr;
@@ -92,24 +102,24 @@ extern(C)
92102
OPENSSL_ApplinkTable[APPLINK_FSETMOD] = &app_fsetmod;
93103
OPENSSL_ApplinkTable[APPLINK_FEOF] = &app_feof;
94104
OPENSSL_ApplinkTable[APPLINK_FCLOSE] = &fclose;
95-
105+
96106
OPENSSL_ApplinkTable[APPLINK_FOPEN] = &fopen;
97107
OPENSSL_ApplinkTable[APPLINK_FSEEK] = &fseek;
98108
OPENSSL_ApplinkTable[APPLINK_FTELL] = &ftell;
99109
OPENSSL_ApplinkTable[APPLINK_FFLUSH] = &fflush;
100110
OPENSSL_ApplinkTable[APPLINK_FERROR] = &app_ferror;
101111
OPENSSL_ApplinkTable[APPLINK_CLEARERR] = &app_clearerr;
102112
OPENSSL_ApplinkTable[APPLINK_FILENO] = &app_fileno;
103-
113+
104114
OPENSSL_ApplinkTable[APPLINK_OPEN] = &fopen;
105115
OPENSSL_ApplinkTable[APPLINK_READ] = &fread;
106116
OPENSSL_ApplinkTable[APPLINK_WRITE] = &fwrite;
107117
OPENSSL_ApplinkTable[APPLINK_LSEEK] = &fseek;
108118
OPENSSL_ApplinkTable[APPLINK_CLOSE] = &fclose;
109-
119+
110120
once = false;
111121
}
112-
122+
113123
return OPENSSL_ApplinkTable.ptr;
114124
}
115-
}
125+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)