-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibtool_win32.pl
1788 lines (1469 loc) · 58.6 KB
/
libtool_win32.pl
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
# -*- mode: perl; -*-
# $Id: libtool_win32.pl,v 1.9 2022/03/20 10:53:57 cvsuser Exp $
# libtool emulation for WIN32 builds.
#
# **Warning**
#
# Functionality is limited to the current GRIEF/MC/WINRSH build requirements.
#
# Example usage:
#
# $(D_LIB)/%.la: $(D_OBJ)/%.lo
# $(LIBTOOL) --mode=link $(CC) $(LDFLAGS) -rpath $(D_LIB) -bindir $(D_BIN) -o $@ $(D_OBJ)/$<
#
# $(D_LIB)/%.lo: %.c
# $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -o $(D_OBJ)/$@ -c $<
#
# $(D_LIB)/%.lo: %.cpp
# $(LIBTOOL) --mode=compile $(CXX) $(CXXFLAGS) -o $(D_OBJ)/$@ -c $<
#
# Copyright Adam Young 2012-2022
# All rights reserved.
#
# The applications are free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, version 3.
#
# Redistributions of source code must retain the above copyright
# notice, and must be distributed with the license document above.
#
# Redistributions in binary form must reproduce the above copyright
# notice, and must include the license document above in
# the documentation and/or other materials provided with the
# distribution.
#
# The applications are distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ==end==
#
use strict;
use warnings 'all';
use Getopt::Long;
use File::Basename;
use File::Copy;
use Cwd;
my $x_libdir = ".libs/";
my $o_help;
my $o_config;
my $o_dryrun;
my $o_features;
my $o_mode;
my $o_tag;
my $o_preserve_dup_deps;
my $o_quiet = 1;
my $o_silent = 0;
my $o_verbose = 0;
my $o_keeptmp = 0;
my $o_debug;
my $o_version;
my $o_extra = '';
sub Compile();
sub Link();
sub true_object($);
sub true_library($);
sub unix2dos($);
sub dos2unix($);
sub Help;
sub Usage;
sub Usage_Link();
sub Usage_Compile();
sub System;
sub __SystemReturnCode($);
sub Label;
sub Debug;
sub Verbose;
sub Warning;
sub Error;
exit &Main();
# libtool ...
#
sub
Main
{
Help() if (!scalar @ARGV);
Getopt::Long::Configure("require_order");
my $ret =
GetOptions(
'h|help' => \$o_help,
'config' => \$o_config,
'n|dry-run' => \$o_dryrun,
'features' => \$o_features,
'mode=s' => \$o_mode,
'finish' => sub {$o_mode='finish';},
'tag=s' => \$o_tag,
'preserve-dup-deps' => \$o_preserve_dup_deps,
'quiet' => \$o_quiet,
'no-quiet' => sub {$o_quiet=0;},
'silent' => \$o_silent,
'no-silent' => sub {$o_silent=0;},
'v|verbose' => \$o_verbose,
'keeptmp' => \$o_keeptmp,
'no-verbose' => sub {$o_verbose=0;},
'debug' => \$o_debug,
'version' => \$o_version);
Help() if (!$ret);
if ($o_help && $o_mode) {
if ('compile' eq $o_mode) {
Usage_Compile();
} elsif ('link' eq $o_mode) {
Usage_Link();
}
}
Usage() if ($o_help);
Usage("missing arguments") if (!scalar @ARGV);
if ('compile' eq $o_mode) {
# Compile a source file into a libtool object.
Compile();
} elsif ('link' eq $o_mode) {
# Create a library or an executable.
Link();
} elsif ('install' eq $o_mode) {
# Install libraries or executables.
} elsif ('finish' eq $o_mode) {
# Complete the installation of libtool libraries on the system.
} elsif ('execute' eq $o_mode) {
# Automatically set the library path so that another program can use
# uninstalled libtool-generated programs or libraries.
} elsif ('uninstall' eq $o_mode) {
# Delete installed libraries or executables.
} elsif ('clean' eq $o_mode) {
# Delete uninstalled libraries or executables.
Clean();
} else {
Usage("unknown mode <$o_mode>");
}
return 0;
}
# Function: Compile
# Compile a library object.
#
sub
Compile() {
my $cc = shift @ARGV;
my $object;
my $source;
my $compile;
my @DEFINES;
my @INCLUDES;
my @STUFF;
while (scalar @ARGV > 1) {
$_ = shift @ARGV;
if (/^\@(.*)$/) { # @cmdfile
OpenCommandFile($1);
next;
}
if (/^-o$/) { # -o <object>
my $val = shift @ARGV;
Error("compile: missing output")
if (! $val);
Error("compile: multiple objects specified <$object> and <$val>")
if ($object);
$object = $val;
} elsif (/^[-\/]Fo[=]?(.*)/) { # -Fo[=]<object>
my $val = ($1 ? $1 : shift @ARGV);
Error("compile: multiple objects specified <$object> and <$val>")
if ($object);
$object = $val;
} elsif (/^[-\/]Fd[=]?(.*)/) { # -Fd[=]<pdb>
#consume
} elsif (/^-i[=]?(.*)$/) { # -i= <path>
push @INCLUDES, ($1 ? $1 : shift @ARGV);
} elsif (/^[-\/]I[=]?(.+)$/) { # -I[=]<path>
push @INCLUDES, $1;
} elsif (/^-I$/) { # -I <path>
push @INCLUDES, shift @ARGV;
} elsif (/^-d(.*)$/) { # -d <define[=value]>
if ('wcl386' eq $cc && (/^-d[1-3][ist]$/ || /^-db$/)) {
push @STUFF, $_;
} else {
push @DEFINES, ($1 ? $1 : shift @ARGV);
}
} elsif (/^[-\/]D[=]?(.+)/) { # -D[=]<define[=value]>
push @DEFINES, $1;
} elsif (/^[-\/]c$/) { # -c
$compile = 1;
} elsif (/^-prefer-pic$/) {
# Libtool will try to build only PIC objects.
} elsif (/^-prefer-non-pic$/) {
# Libtool will try to build only non-PIC objects.
} elsif (/^-shared$/) {
# Even if Libtool was configured with --enable-static, the object file Libtool builds will
# not be suitable for static linking. Libtool will signal an error if it was configured
# with --disable-shared, or if the host does not support shared libraries.
} elsif (/^-static$/) {
# Even if libtool was configured with --disable-static, the object file Libtool builds will
# be suitable for static linking.
} elsif (/^-Wc,(.*)$/) {
# Pass a flag directly to the compiler. With -Wc,, multiple flags may be separated by
# commas, whereas -Xcompiler passes through commas unchanged.
if ('owcc' eq $cc || 'gcc' eq $cc) {
push @STUFF, $_;
} else {
Error("compile: unexpected option <$_>");
}
} elsif (/^-Xcompiler$/) {
# Pass a flag directly to the compiler. With -Wc,, multiple flags may be separated by
# commas, whereas -Xcompiler passes through commas unchanged.
push @STUFF, shift @ARGV;
} else {
push @STUFF, $_;
}
}
$source = shift @ARGV;
Error("compile: unsupported compiler <$cc>")
if (!('cl' eq $cc || 'wcl386' eq $cc || 'owcc' eq $cc || 'gcc' eq $cc));
Error("compile: unable to determine object")
if (!$object);
Error("compile: object file suffix not <.lo>")
if ($object !~ /.lo$/);
Error("compile: missing source file")
if (!$source || $source =~ /^-/); # missing or an option.
Error("compile: -c options missing")
if (!$compile);
my $true_path = unix2dos(dirname($object))."\\${x_libdir}";
my $true_object = ${true_path}.basename($object, 'lo')."obj";
Verbose "cc: ${cc}";
Verbose "defines:";
foreach(@DEFINES) { Verbose "\t$_"; }
Verbose "includes:";
foreach(@INCLUDES) { Verbose "\t$_"; }
Verbose "object: ${object}";
Verbose " true: ${true_object}";
Verbose "source: ${source}";
Verbose "...: @STUFF";
my $cmd = '';
if ('cl' eq $cc) {
$cmd = "$cc @STUFF /DDLL=1";
foreach (@DEFINES) { $cmd .= " /D$_"; }
foreach (@INCLUDES) { $cmd .= " /I$_"; }
$cmd .= " /Fo$true_object";
$cmd .= " /Fd".${true_path}.'\\'; # VCx0.pdb
$cmd .= " /c $source";
} elsif ('wcl386' eq $cc) { # OpenWatcom, legacy interface.
# http://www.openwatcom.org/index.php/Writing_DLLs
$cmd = "$cc @STUFF -dDLL=1";
$cmd .= " -bd"; # DLL builds
foreach (@DEFINES) { $cmd .= " -d$_"; }
foreach (@INCLUDES) { $cmd .= " -I=\"$_\""; }
$cmd .= " -Fo=\"$true_object\"";
$cmd .= " -c $source";
} elsif ('owcc' eq $cc) { # OpenWatcom, posix driver.
$cmd = "$cc @STUFF -DDLL=1";
$cmd .= " -shared"; # DLL builds
foreach (@DEFINES) { $cmd .= " -D$_"; }
foreach (@INCLUDES) { $cmd .= " -I \"$_\""; }
$cmd .= " -o \"$true_object\"";
$cmd .= " -c $source";
} elsif ('gcc' eq $cc) {
$cmd = "$cc @STUFF -D DLL=1 -shared";
foreach (@DEFINES) { $cmd .= " -D $_"; }
foreach (@INCLUDES) { $cmd .= " -I \"$_\""; }
$cmd .= " -o \"$true_object\"";
$cmd .= " -c $source";
}
my $ret = 0;
mkdir $true_path;
unlink $object;
Verbose "cc: $cmd";
exit($ret)
if (0 != ($ret = System($cmd)));
# generate lo artifact
open(LO, ">${object}") or
die "cannot create <$object> : $!\n";
print LO "#libtool win32 generated, do not modify\n";
print LO "mode=dll\n";
print LO "cc=$cc\n";
print LO "cmd=$cmd\n";
print LO "source=$source\n";
print LO "object=$object\n";
print LO "true_object=".dos2unix($true_object)."\n";
close(LO);
return 0;
}
# Function: Link
# Link a library object.
#
sub
Link() {
my $cc = shift @ARGV;
my ($output, $dlbase, $rpath, $bindir, $module, $mapfile);
my $version_number = '';
my $wc_fastcall = -1; # fastcall (Watcom) convention.
my $wc_debugger = '';
my $linktype = 'dll';
my $cl_ltcg = 0;
my $cl_debug = undef;
my @OBJECTS;
my @RESOURCES;
my @EXPORTS;
my $DESCRIPTION;
my @LIBRARIES;
my @LIBPATHS;
my @STUFF;
while (scalar @ARGV) {
$_ = shift @ARGV;
if (/^\@(.*)$/) { # @cmdfile
OpenCommandFile($1);
next;
}
if (/^-o$/) { # -o <output>
my $val = shift @ARGV;
Error("link: missing output")
if (! $val);
Error("link: multiple outputs specified <$output> and <$val>")
if ($output);
if ($val =~ /\.exe$/i) {
$linktype = 'exe';
} elsif ($val =~ /\.(la|dll)$/i) {
$linktype = 'dll';
} else {
Error("link: unexpected output type <${val}>");
}
$output = $val;
} elsif (/^[-\/]Fo[=]?(.*)/) { # -Fo[=]<output>
my $val = ($1 ? $1 : shift @ARGV);
Error("link: multiple outputs specified <$output> and <$val>")
if ($output);
$output = $val;
} elsif (/^[-\/]Fe[=]?(.*)/) { # -Fe[=]<output>
my $val = ($1 ? $1 : shift @ARGV);
Error("link: multiple outputs specified <$output> and <$val>")
if ($output);
$linktype = 'exe';
$output = $val;
} elsif (('wcl386' eq $cc || 'owcc' eq $cc) and /^[-\/]Fm[=]?(.*)/i) {
# -Fm[=]<output>
my $val = ($1 ? $1 : shift @ARGV);
Error("link: multiple mapfile specified <$mapfile> and <$val>")
if ($mapfile);
$mapfile = $val;
} elsif (/^[-\/]Map[:=]?(.*)/i) { # -Map[:=]<output>
my $val = ($1 ? $1 : shift @ARGV);
Error("link: multiple mapfile specified <$mapfile> and <$val>")
if ($mapfile);
$mapfile = $val;
} elsif (/^(.*)\.lo$/ || /^(.*)\.o$/ || /^(.*)\.obj$/i) {
push @OBJECTS, $_;
} elsif (/^(.*)\.res$/) {
push @RESOURCES, $_;
} elsif (/^(.*)\.rc$/) {
Error("link: $_ not supported\n");
} elsif (/^(.*)\.la$/ || /^(.*)\.a$/ || /^(.*)\.lib$/i) {
push @LIBRARIES, $_;
} elsif (/^-l(.*)$/) {
push @LIBRARIES, $1;
} elsif (/^[-\/]LIBPATH[:]?\s*(.+)/) { # -LIBPATH[:]<path>
push @LIBPATHS, $1;
} elsif (/^-L(.*)/) {
push @LIBPATHS, $1;
} elsif (/^-all-static$/) {
Error("link: $_ not supported\n");
} elsif (/^-avoid-version$/) {
#ignore
} elsif (/^-bindir[=]?(.*)$/) {
my $val = ($1 ? $1 : shift @ARGV);
Error("link: $_ $val, not a valid directory : $!")
if (! -d $val);
$bindir = $val;
} elsif (/^-dlbase[=]?(.*)/) { # -dlbase[=]<output> (extension)
my $val = ($1 ? $1 : shift @ARGV);
Error("link: multiple dll base names specified <$dlbase> and <$val>")
if ($dlbase);
$dlbase = $val; # dll base name, version is appended.
} elsif (/^-dlopen(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-dlpreopen(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-export-dynamic$/) {
Error("link: $_ not supported\n");
} elsif (/^-export-fastcall$/) { # extension
if ($wc_fastcall eq -1 or $wc_fastcall eq 1) {
$wc_fastcall = 1;
} else {
Error("link: -export-fastcall and compiler switches are incompatible\n");
}
} elsif (/^-export-symbols[=]?(.*)$/) {
my $val = ($1 ? $1 : shift @ARGV);
Error("link: $_ $val, not a valid symbol file : $!")
if (! -f $val);
if ($val =~ /\.def$/i) { # <name.def>
ParseDefFile($val, \@EXPORTS, \$DESCRIPTION);
} else { # <name.sym>
ParseSymFile($val, \@EXPORTS);
}
} elsif (/^-export-symbols-regex(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-module$/) {
$module=1;
} elsif (/^-no-fast-install$/) {
Error("link: $_ not supported\n");
} elsif (/^-no-install$/) {
Error("link: $_ not supported\n");
} elsif (/^-no-undefined$/) {
Error("link: $_ not supported\n");
} elsif (/^-objectlist(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-precious-files-regex(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-release(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^[-]+rpath[=]?(.*)$/) {
my $val = ($1 ? $1 : shift @ARGV);
if (! -d $val) {
Warning("link: $_ $val, not a valid directory -- ignored");
} else {
$rpath = $val;
}
} elsif (/^-RTC[1csu]+$/ && ('cl' eq $cc)) {
push @STUFF, $_;
} elsif (/^-R(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-shared$/) {
#default
} elsif (/^-shrext(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-static$/) {
Error("link: $_ not supported\n");
} elsif (/^-static-libtool-libs$/) {
Error("link: $_ not supported\n");
} elsif (/^-version-info(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-version-number[=]?(.*)$/) {
my $val = ($1 ? $1 : shift @ARGV);
$version_number = $val;
} elsif (/^-weak(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-Wc,(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-Xcompiler(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-Wl,(.*)$/) {
if ('owcc' eq $cc) {
my $wl = $1;
if ($wl =~ /^LIBPATH[:= ]?\s*(.+)/) {
push @LIBPATHS, $1; # -LIBPATH[:= ]<path>
next;
}
}
Error("link: $_ not supported\n");
} elsif (/^-Xlinker(.*)$/) {
Error("link: $_ not supported\n");
} elsif (/^-XCClinker(.*)$/) {
Error("link: $_ not supported\n");
} else {
# toolchain specific
if ('cl' eq $cc) {
# Optimisation
if (/^[-\/]GL$/i) { # /GL (Whole Program Optimization)
$cl_ltcg = 1; # imply /LTCG
next;
} elsif (/^[-\/]LTCG$/i) {
$cl_ltcg = 1; # explicit /LTCG
next;
# Debugger
} elsif (/^[-\/]DEBUG$/ || /^[-\/]DEBUG:(.+_)$/) {
$cl_debug = $_; # explicit /DEBUG:NONE, /DEBUG:FULL and /DEBUG:FASTLINK
$cl_debug =~ s/^-/\//;
next;
}
} elsif ('wcl386' eq $cc) { # process
# Calling convention:
# [3-6]r register calling convention
# [3-6]s stack based calling convention
#
# ecc set default calling convention to __cdecl
# ecd set default calling convention to __stdcall
# ecf set default calling convention to __fastcall
# ecp set default calling convention to __pascal
# ecr set default calling convention to __fortran
# ecs set default calling convention to __syscall
# ecw set default calling convention to __watcall (default)
#
# Warning:
# The OpenWatcom run-time library utilise either register or stack based (by default register) calls,
# hence use of the alternative convention via a '-ecx' option may create major compatiblity issues.
#
# For example atexit() and qsort() shall assume register/stack yet as the default is applied the
# function argument, the caller can siliently pass an alternatively coded function; that shall be
# fatal at run-time.
#
if (/^-[3456]r$/ or /^-ecw$/) {
if ($wc_fastcall eq -1) {
$wc_fastcall = 1; # auto-selection
} elsif ($wc_fastcall eq 0) {
Error("link: -export-fastcall and compiler switches are incompatible\n");
}
} elsif (/^-[3456]s$/ or /^-ec[cdfprs]$/) {
if ($wc_fastcall eq 1) {
Error("link: -export-fastcall and compiler switches are incompatible\n");
}
# Target
} elsif (/^-bt=(.*)$/) {
my $target = uc($1);
Error("link: <$_> unexpected target\n")
if ($target ne 'NT');
# Debugger support:
# -h[wcd] Watcom,Codeview,Dwarf
#
} elsif (/^-h([wc])$/) {
$wc_debugger = $1;
}
} elsif ('owcc' eq $cc) {
# Call convention
if (/^mregparm=1$/) {
if ($wc_fastcall eq -1) {
$wc_fastcall = 1; # auto-selection
} elsif ($wc_fastcall eq 0) {
Error("link: -export-fastcall and compiler switches are incompatible\n");
}
} elsif (/^mregparm=0$/) {
if ($wc_fastcall eq 1) {
Error("link: -export-fastcall and compiler switches are incompatible\n");
}
# Target
} elsif (/^-b$/) {
my $target = uc(shift @ARGV);
Error("link: <$_> unexpected target\n")
if ($target ne 'NT');
} elsif (/^-m(console|windows)$/) {
my $target = $1;
Error("link: <$_> unexpected target\n")
if ($target ne 'console');
# Debugger
} elsif (/^-g([wcd])$/) { # -g[wcd] Watcom,Codeview,Dwarf
$wc_debugger = $1;
}
}
push @STUFF, $_;
}
}
Error("link: unsupported compiler <$cc>")
if (!('cl' eq $cc || 'wcl386' eq $cc || 'owcc' eq $cc || 'gcc' eq $cc));
Error("link: unable to determine output")
if (!$output);
if ($linktype eq 'dll') {
Error("link: output file suffix not <.la>")
if ($output !~ /.la$/);
}
Verbose "cc: $cc";
Verbose "output: $output";
Verbose "objects:";
foreach(@OBJECTS) {
Verbose "\t$_";
}
Verbose "libraries:";
foreach(@LIBRARIES) { Verbose "\t$_"; }
Verbose "...: @STUFF";
my ($dll_version, $dll_major, $dll_minor) = ('', 0, 0);
if ($version_number) {
if ($version_number =~ /^\s*(\d+)\s*$/) {
# <major>
$dll_version = "$1.0"; # <name>major.0
$version_number = ".$1";
} elsif ($version_number =~ /^\s*(\d+):(\d+)$/) {
# <major>:<minor>
$dll_version = "$1.$2"; # major.minor
$version_number = ".$1.$2"; # <name>.<major.dll
} elsif ($version_number =~ /^\s*(\d+):(\d+):(\d+)$/) {
# <major>:<minor>:<revision>
$dll_version = "$1.$2"; # major.minor
$version_number = ".$1.$2.$3"; # <name>.<major.<revision>.dll
} elsif ($version_number =~ /^\s*(\d+)\s*\.\s*(\d+)$/) {
# <major>.<minor>
$dll_version = "$1.$2"; # major.minor
$version_number = ".$1.$2"; # <name>.<major.<minor>.dll
} elsif ($version_number =~ /^\s*(\d+)\s*\.\s*(\d+)\.\s*(\d+)$/) {
# <major>.<minor>.<revision>
$dll_version = "$1.$2"; # major.minor
$version_number = ".$1.$2.$3"; # <name>.<major.<revision>.dll
} else {
Error("link: invalid -version_number <$version_number>\n");
}
}
my ($cmdfile, $deffile, $cmd) = ("__libtool__.lnk", "__libtool__.def");
my $basedir = unix2dos(dirname($output));
my $basename = unix2dos(basename($output, '.la'));
my $basepath = $basedir.'\\'.$basename;
my $dllbasename = $dlbase ? unix2dos($dlbase) : $basename;
my $dllbasepath = $basedir.'\\'.$dllbasename;
my $dllname = "${dllbasename}${version_number}.dll";
my $dllpath = "${dllbasepath}${version_number}.dll";
my $pdbpath = "${dllbasepath}${version_number}.pdb";
my $mappath = ($mapfile ? $mapfile :
($linktype eq 'dll' ? "${dllbasepath}${version_number}.map" : "${basepath}.map"));
my $manifestpath = "${dllbasepath}${version_number}.dll.manifest";
my $libpath = "${basepath}.lib"; # import library
my $exppath = "${basepath}.exp"; # export library
# Notes:
# Export (.exp) files contain information about exported functions and data items. When LIB creates an import library, it also creates an .exp file.
# You use the .exp file when you link a program that both exports to and imports from another program, either directly or indirectly.
# If you link with an .exp file, LINK does not produce an import library, because it assumes that LIB already created one.
# For details about .exp files and import libraries, see "Working with Import Libraries and Export Files" (MSDN).
#
my $sympath = undef;
if ($linktype eq 'dll') {
my @BAD_OBJECTS = ();
for my $obj (@OBJECTS) { # usage warning
push @BAD_OBJECTS, $obj
if ($obj !~ /\.lo$/i);
}
if (scalar @BAD_OBJECTS) {
print "*** Warning: Linking the shared library ${output} against the\n";
print "*** non-libtool objects @BAD_OBJECTS is not portable!\n";
}
} elsif ($linktype eq 'exe') {
my @BAD_OBJECTS = ();
for my $obj (@OBJECTS) { # usage warning
push @BAD_OBJECTS, $obj
if ($obj =~ /\.lo$/i);
}
if (scalar @BAD_OBJECTS) {
print "*** Warning: Linking the executable ${output} against the\n";
print "*** non-libtool objects @BAD_OBJECTS is not portable!\n";
}
$output .= '.exe'
if ($output !~ /\.exe$/i);
}
if ('cl' eq $cc) {
#
# MSVC/Watcom
#
if (defined $ENV{'VCToolsInstallDir'}) { # 2010 plus
my $toolbase = $ENV{'VCToolsInstallDir'};
$cmd = "\"${toolbase}\\bin\\Hostx86\\x86\\link\" \@$cmdfile";
} elsif (defined $ENV{'VCINSTALLDIR'}) { # 2008
my $toolbase = $ENV{'VCINSTALLDIR'};
$cmd = "\"${toolbase}\\bin\\link\" \@$cmdfile";
} else { # default
$cmd = "link \@$cmdfile";
}
open(CMD, ">${cmdfile}") or
die "cannot create <${$cmdfile}>: $!\n";
if ($linktype eq 'dll') {
print CMD "/DLL\n";
print CMD "/SUBSYSTEM:WINDOWS\n";
print CMD "/ENTRY:_DllMainCRTStartup\@12"."\n";
print CMD "/OUT:${dllpath}\n";
print CMD "/IMPLIB:${libpath}\n";
} else {
print CMD "/SUBSYSTEM:CONSOLE\n";
print CMD "/OUT:${output}\n";
}
print CMD "/MAP:${mappath}\n";
print CMD "/MAPINFO:EXPORTS\n";
print CMD "/VERSION:${dll_version}\n"
if ($dll_version);
print CMD "/NOLOGO\n"
if ($o_quiet || $o_silent);
print CMD "/OPT:REF\n";
print CMD (defined $cl_debug ? $cl_debug : "/DEBUG") . "\n";
print CMD "/INCREMENTAL:NO\n"; # after /DEBUG
print CMD "/LTCG\n"
if ($cl_ltcg);
foreach(@OBJECTS) {
print CMD true_object($_)."\n";
}
foreach(@RESOURCES) {
print CMD true_object($_)."\n";
}
if ($linktype eq 'dll') {
foreach(@EXPORTS) {
print CMD "/EXPORT:".MSVCExportDef($_)."\n";
}
}
foreach(@LIBPATHS) {
print CMD "/LIBPATH:$_\n";
}
foreach(@LIBRARIES) {
print CMD unix2dos(true_library($_))."\n";
}
close(CMD);
} elsif ('wcl386' eq $cc || 'owcc' eq $cc) {
#
# OpenWatcom
#
$cmd = "wlink \@$cmdfile";
open(CMD, ">${cmdfile}") or
die "cannot create <${$cmdfile}>: $!\n";
if ($linktype eq 'dll') {
print CMD "system nt_dll initinstance terminstance\n";
#
# nt_dll: Windows NT Dynamic Link Libraries.
#
# When a dynamic link library uses the Watcom C/C++ run-time libraries, an automatic data
# segment is created each time a new process accesses the dynamic link library. For this reason,
# initialization code must be executed when a process accesses the dynamic link library for the
# first time. To achieve this, "INITINSTANCE" must be specified in the "SYSTEM" directive.
# Similarly, "TERMINSTANCE" must be specified so that the termination code is executed when
# a process has completed its access to the dynamic link library. If the Watcom C/C++ run-time
# libraries are not used, these options are not required.
#
print CMD "name ${dllpath}\n";
print CMD "option implib=${libpath}\n";
print CMD "option checksum\n"; # create an MS-CRC32 checksum for image.
} else {
print CMD "system nt\n";
#
# nt: Windows NT Character-Mode Executable.
# nt_win: Windows NT Windowed Executable.
#
print CMD "name ${output}\n";
}
# print CMD "option modname='${modulepath}'\n";
# print CMD "option copyright=''\n";
# print CMD "option description=${DESCRIPTION}\n"
# if ($DESCRIPTION);
print CMD "option version=${dll_version}\n"
if ($dll_version);
print CMD "option map=${mappath}\n";
print CMD "option static\n"; # export statics within the map file.
print CMD "option artificial\n"; # export internal symbols.
print CMD "sort\n"; # sort symbol by address.
print CMD "option quiet\n"
if ($o_quiet || $o_silent);
if (! $wc_debugger) { # options are exclusive; plus before objects.
$sympath = "${dllbasepath}${version_number}.sym";
print CMD "option symfile=${sympath}\n";
#XXX: consider default when -d1, allowing use within a production environment
} elsif ($wc_debugger eq 'w') {
print CMD "debug watcom all\n"; # 'watcom all' is fatal at times!
} elsif ($wc_debugger eq 'c') {
print CMD "debug codeview\n";
print CMD "option cvpack\n";
} elsif ($wc_debugger eq 'd') {
print CMD "debug dwarf\n";
}
foreach(@OBJECTS) {
print CMD "file ".unix2dos(true_object($_))."\n";
}
foreach(@RESOURCES) {
print CMD "option resource=".unix2dos(true_object($_))."\n";
}
foreach(@EXPORTS) {
print CMD "export ".WatcomExportDef($_, $wc_fastcall)."\n";
}
foreach(@LIBPATHS) {
print CMD "libpath ".unix2dos($_)."\n";
}
foreach(@LIBRARIES) {
print CMD "library ".unix2dos(true_library($_))."\n";
}
close(CMD);
} elsif ('gcc' eq $cc) {
#
# MinGW
#
$cmd = "g++ \@${cmdfile}";
$libpath = "${basepath}.a";
$pdbpath = undef;
$exppath = undef;
$manifestpath = undef;
open(CMD, ">${cmdfile}") or
die "cannot create <${cmdfile}>: $!\n";
print CMD "-o ".dos2unix($dllpath)."\n";
print CMD "-shared\n";
foreach(@OBJECTS) {
print CMD dos2unix(true_object($_))."\n";
}
print "warning: resources ignored @RESOURCES\n"
if (scalar @RESOURCES);
print CMD "-Wl,--subsystem,windows\n";
print CMD "-Wl,--out-implib,".dos2unix("${basepath}.a")."\n";
print CMD "-Xlinker -Map=".dos2unix($mappath)."\n";
foreach(@LIBPATHS) {
print CMD "-L ".dos2unix($_)."\n";
}
foreach(@LIBRARIES) {
print CMD "-l".dos2unix($_)."\n";
}
if (scalar @EXPORTS) {
print CMD dos2unix($deffile)."\n";
open(DEF, ">${deffile}") or
die "cannot create <${deffile}>: $!\n";
print DEF "LIBRARY \"${dllname}\"\n";
print DEF "EXPORTS\n";
foreach(@EXPORTS) {
if (/^(.+)=(.+)$/) {
my ($name, $alias) = ($1, $2);
$alias =~ s/^_//
if ($alias =~ /\@/); # remove leading if <xxxx@##>
print DEF "'$name'='$alias'\n"; # quote internal name
} else {
s/^_// if (/\@/); # remove leading if <xxxx@##>
print DEF "$_\n";
}
}
close(DEF);
}
close(CMD);
}
my $ret = 0;
unlink $output;
if (0 != ($ret = System($cmd))) {
if ('cl' eq $cc) {
print "#\n".
"# LINK: fatal error LNK1123: failure during conversion to COFF; file invalid or corrupt\n".
"# can be result of an incorrect version of cvtres.exe due to dual VC10/VC2012 installations,\n".
"# rename 'C:/Program Files (x86)/Microsoft Visual Studio 10/VC/Bin/cvtres.exe' => cvtres_org.exe\n".
"#\n";
# $o_verbose = 0;
# System("which cvtres");
}
exit ($ret);
}
# generate la artifact
if ($linktype eq 'dll') {
open(LA, ">${output}") or
die "cannot create <$output> : $!\n";
print LA "#libtool win32 generated, do not modify\n";
print LA "mode=link\n";
print LA "cc=$cc\n";
print LA "lib=${libpath}\n";
print LA "exp=${exppath}\n" if ($exppath);
print LA "map=${mappath}\n" if ($mappath);
print LA "sym=${sympath}\n" if ($sympath);
print LA "dll=${dllpath}\n";
print LA "pdb=${pdbpath}\n" if ($pdbpath);
print LA "manifest=${manifestpath}\n" if ($manifestpath);
print LA "[objects]\n";
foreach(@OBJECTS) {
print LA true_object($_)."\n";
}
print LA "[libraries]\n";
foreach(@LIBRARIES) {
print LA "$_\n";
}
close(LA);
}
if ('gcc' eq $cc) {
copy("${basepath}.a", $libpath) or
die "link: unable to copy <${basepath}.a> to <${libpath}> : $!\n";
}