summaryrefslogtreecommitdiff
path: root/Makefile.PL
blob: ea715c2c02b9388b407d91606e108be355618ff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use inc::Module::Install;
use Devel::CheckLib;
use Getopt::Long;
use File::Spec::Functions;

BEGIN {
    if ( $Module::Install::AUTHOR ) {
        use Module::Install::XSUtil;
    }
}

name 'Zonemaster-LDNS';
all_from 'lib/Zonemaster/LDNS.pm';
repository 'https://github.com/zonemaster/zonemaster-ldns';
bugtracker 'https://github.com/zonemaster/zonemaster-ldns/issues';

=head1 Optional features

=over

=item --[no-]ed25519

Enable (or disable) support for Ed25519 in both openssl and ldns.
Enabled by default.

=item --[no-]idn

Enable (or disable) support for converting IDN labels in U-label format (with
non-ASCII Unicode characters) to the same IDN labels in A-label format (encoded
in ASCII).
Enabled by default.

=item --[no-]internal-ldns

When enabled, an included version of ldns is statically linked into
Zonemaster::LDNS.
When disabled, libldns is dynamically linked just like other dependencies.
Enabled by default.

=item --[no-]randomize

Experimental.
Randomizes the capitalization of returned domain names.
Disabled by default.

=item --prefix-openssl=PATH

Search for OpenSSL headers and libraries in PATH.
The LDNS script will look for an "include" and a "lib" folder.

=item --openssl-inc=PATH

Search for OpenSSL include in PATH.
The PATH is passed to the LDNS compiler via the CFLAGS variable.

=item --openssl-lib=PATH

Search for OpenSSL library in PATH.
The PATH is passed to the LDNS compiler via the LDFLAGS variable.

=item --libidn-inc=PATH

Search for Libidn include in PATH.

=item --libidn-lib=PATH

Search for Libidn library in PATH.

=item --ldns-inc=PATH

Search for LDNS include in PATH.

=item --ldns-lib=PATH

Search for LDNS library in PATH.

=item --debug

Enable debug mode, more verbose output.

=back

=cut

my $opt_ed25519        = 1;
my $opt_idn            = 1;
my $opt_internal_ldns  = 1;
my $opt_randomize      = 0;
my $opt_debug          = 0;
my $opt_assets         = {
    openssl => {
        prefix => "",
        inc    => "",
        lib    => ""
    },
    ldns    => {
        inc    => "",
        lib    => ""
    },
    libidn => {
        inc    => "",
        lib    => ""
    }
};

GetOptions(
    'ed25519!'         => \$opt_ed25519,
    'idn!'             => \$opt_idn,
    'internal-ldns!'   => \$opt_internal_ldns,
    'randomize!'       => \$opt_randomize,
    'debug!'           => \$opt_debug,
    'prefix-openssl=s' => \$$opt_assets{openssl}{prefix},
    'openssl-inc=s'    => \$$opt_assets{openssl}{inc},
    'openssl-lib=s'    => \$$opt_assets{openssl}{lib},
    'libidn-inc=s'     => \$$opt_assets{libidn}{inc},
    'libidn-lib=s'     => \$$opt_assets{libidn}{lib},
    'ldns-inc=s'       => \$$opt_assets{ldns}{inc},
    'ldns-lib=s'       => \$$opt_assets{ldns}{lib},
);

configure_requires 'Devel::CheckLib';
configure_requires 'Module::Install' => 1.19;
configure_requires 'Module::Install::XSUtil';
test_requires 'JSON::PP';
test_requires 'Test::Fatal';
test_requires 'Test::More' => 1.302015;

use_ppport 3.19;
cc_include_paths 'include';
cc_src_paths 'src';


my %assert_lib_args = (
    openssl => {},
    libidn  => {},
    ldns    => {}
);

sub custom_assets
{
    my ( $href ) = @_;
    # $href = { key => "openssl", lib => "crypto", name => "OpenSSL" }

    my $key  = $$href{key};
    my $name = $$href{name};
    my $lib  = $$href{lib};

    my $input_prefix = $$opt_assets{$key}{prefix};
    my $input_inc    = $$opt_assets{$key}{inc};
    my $input_lib    = $$opt_assets{$key}{lib};

    my $custom_lib = ( $input_prefix or $input_inc or $input_lib );
    if ( $custom_lib ) {
        my $incpath = "";
        my $libpath = "";

        if ( $input_prefix ) {
            print "Custom prefix for $name: $input_prefix\n";
            $incpath = "$input_prefix/include";
            $libpath = "$input_prefix/lib";
        }

        if ( $input_inc ) {
            print "Custom include directory for $name: $input_inc\n";
            $incpath = "$input_inc";
        }

        if ( $input_lib ) {
            print "Custom library directory for $name: $input_lib\n";
            $libpath = "$input_lib";
        }

        cc_include_paths "$incpath";
        cc_libs "-L$libpath", "$lib";

        $assert_lib_args{$key}{incpath} = "$incpath";
        $assert_lib_args{$key}{libpath} = "$libpath";
    }
    else {
        cc_libs "$lib";
    }
}

# OpenSSL

custom_assets(
    {
        name => "OpenSSL",
        lib  => "crypto",
        key  => "openssl"
    }
);

cc_assert_lib(
    debug    => $opt_debug,
    lib      => 'crypto',
    header   => 'openssl/crypto.h',
    function => 'if(SSLeay()) return 0; else return 1;',
    %{ $assert_lib_args{openssl} },
);
if ( $opt_ed25519 ) {
    print "Feature Ed25519 enabled\n";
    cc_assert_lib(
        debug    => $opt_debug,
        lib      => 'crypto',
        header   => 'openssl/evp.h',
        function => 'EVP_PKEY_ED25519; return 0;',
        %{ $assert_lib_args{openssl} },
    );
}
else {
    print "Feature Ed25519 disabled\n";
}


# LDNS and NSID

my $ldns_has_nsid;

if ( $opt_internal_ldns ) {
    print "Feature internal ldns enabled\n";
    cc_libs '-Lldns/lib';
    cc_include_paths 'ldns';
    $ldns_has_nsid = 1;
}
else {
    print "Feature internal ldns disabled\n";

    custom_assets(
        {
            name => "LDNS",
            lib  => "ldns",
            key  => "ldns"
        }
    );

    if ( $opt_ed25519 ) {
        cc_assert_lib(
            debug    => $opt_debug,
            lib      => 'ldns',
            header   => 'ldns/ldns.h',
            %{ $assert_lib_args{ldns} },
            ccflags  => '-DUSE_ED25519',
            function => 'if(LDNS_ED25519) return 0; else return 1;'
        );
    }

    # NSID feature requires LDNS version >= 1.8.2
    $ldns_has_nsid = check_lib(
        debug    => $opt_debug,
        lib      => 'ldns',
        header   => 'ldns/util.h',
        %{ $assert_lib_args{ldns} },
        function => 'if ( LDNS_REVISION >= ((1<<16)|(8<<8)|(2)) ) return 0; else return 1;'
    );
}

if ( $ldns_has_nsid ) {
    print "Feature NSID enabled\n";
    cc_define '-DNSID_SUPPORT';
}
else {
    print "Feature NSID disabled\n";
}


# Libidn

if ( $opt_idn ) {
    print "Feature idn enabled\n";

    custom_assets(
        {
            name => "Libidn",
            lib  => "idn2",
            key  => "libidn"
        }
    );

    check_lib_or_exit(
        debug    => $opt_debug,
        lib    => 'idn2',
        header => 'idn2.h',
        %{ $assert_lib_args{libidn} },
        function => 'return IDN2_OK;'
    );
    cc_define '-DWE_CAN_HAZ_IDN';
}
else {
    print "Feature idn disabled\n";
}


# Internals

if ( $opt_randomize ) {
    print "Feature randomized capitalization enabled\n";
    cc_define '-DRANDOMIZE';
}
else {
    print "Feature randomized capitalization disabled\n";
}


sub MY::postamble {

    my $contributors_make = <<'END_CONTRIBUTORS';

CONTRIBUTORS.txt:
	@( \
	echo "This module is based on the ldns library from NLnet Labs <https://www.nlnetlabs.nl/projects/ldns/>" ; \
    echo ; \
	echo "Contributors to this module:" ; \
	git shortlog -sne | cut -b8- \
	) >| CONTRIBUTORS.txt

END_CONTRIBUTORS

    my $docker_make = <<'END_DOCKER';

docker-build:
	docker build --tag zonemaster/ldns:local --build-arg version=$(VERSION) .

docker-tag-version:
	docker tag zonemaster/ldns:local zonemaster/ldns:$(VERSION)

docker-tag-latest:
	docker tag zonemaster/ldns:local zonemaster/ldns:latest

END_DOCKER

    my $configure_flags_make = <<'END_CONFIGURE_FLAGS';

CONFIGURE_FLAGS += --disable-ldns-config --disable-dane

END_CONFIGURE_FLAGS

    my $openssl_make = <<END_OPENSSL_MAKE;

CONFIGURE_FLAGS += --with-ssl=$$opt_assets{openssl}{prefix}

END_OPENSSL_MAKE

    my $openssl_flags = <<END_OPENSSL_FLAGS;

CFLAGS += -I$$opt_assets{openssl}{inc}
LDFLAGS += -L$$opt_assets{openssl}{lib}

END_OPENSSL_FLAGS

    my $ed25519_make = <<'END_ED25519';

CONFIGURE_FLAGS += --enable-ed25519

END_ED25519

    my $no_ed25519_make = <<'END_NO_ED25519';

CONFIGURE_FLAGS += --disable-ed25519

END_NO_ED25519

    my $internal_ldns_make = <<'END_INTERNAL_LDNS';

CFLAGS += -fPIC
LDFROM += ldns/.libs/libldns.a

config :: ldns/.libs/libldns.a

ldns/.libs/libldns.a: ldns/configure
	cd ldns ;\
	./configure CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" $(CONFIGURE_FLAGS) ;\
	make lib

ldns/configure: ldns/Changelog
	cd ldns ; libtoolize -ci
	cd ldns ; autoreconf -fi

ldns/Changelog:
	git submodule init
	git submodule sync
	git submodule update

END_INTERNAL_LDNS

    my $postamble = '';

    $postamble .= $contributors_make;
    $postamble .= $docker_make;
    if ( $opt_internal_ldns ) {
        $postamble .= $configure_flags_make;
        $postamble .= $openssl_make if $$opt_assets{openssl}{prefix};
        $postamble .= $ed25519_make if $opt_ed25519;
        $postamble .= $no_ed25519_make if !$opt_ed25519;
        $postamble .= $openssl_flags if ( $$opt_assets{openssl}{inc} or $$opt_assets{openssl}{lib} );
        $postamble .= $internal_ldns_make;
    }

    return $postamble;
}

sub MY::test_via_harness {
    local $_ = shift()->MM::test_via_harness(@_);
    s/\bPERL_DL_NONLAZY=1 +//g;
    return $_;
}

sub MY::test_via_script {
    local $_ = shift()->MM::test_via_script(@_);
    s/\bPERL_DL_NON_LAZY=1 +//g;
    return $_;
}

WriteAll;