mingw64-filesystem package.
[fedora-mingw.git] / crossreport / crossreport.pl
1 #!/usr/bin/perl -w
2 #
3 # CrossReport - analysis tool.
4 # Copyright (C) 2009 Red Hat Inc.
5 # Written by Richard W.M. Jones <rjones@redhat.com>,
6 # http://fedoraproject.org/wiki/MinGW
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or (at
11 # your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 use strict;
23 use utf8;
24
25 # This removes "Wide character in print" warning.  Perl should do
26 # the right thing based on the locale, but it doesn't, so ...
27 binmode STDOUT, ":utf8";
28
29 use Getopt::Long;
30 use Pod::Usage;
31 use GDBM_File;
32 use POSIX qw(strftime floor);
33
34 =pod
35
36 =head1 NAME
37
38 CrossReport - Analysis tool to help cross-compilation to Windows.
39
40 =head1 SYNOPSIS
41
42  mingw32-crossreport [options] /path/to/linuxbinary
43
44 =head1 DESCRIPTION
45
46 CrossReport is a tool to help you analyze the APIs used by a compiled
47 Linux program, in order to work out the effort required to
48 cross-compile that program for Windows, using the Fedora MinGW
49 cross-compiler.
50
51 The simplest way to use it is to point it at an existing Linux binary,
52 and then read the generated report.
53
54 What it does in more detail: It looks at the libraries and API calls
55 used by the Linux binary, and compares them to the libraries and API
56 calls that we currently support under the Fedora MinGW cross-compiler.
57 It then works out what is missing, and produces a report suggesting
58 the amount of work that needs to be done to port the program.  For
59 example, whether whole libraries need to be ported first, and/or how
60 to substitute individual calls to work on Windows.
61
62 =head1 EXAMPLE
63
64 Assuming that the excellent vector graphics editor Inkscape
65 (L<http://www.inkscape.org/>) is installed, you could do:
66
67  mingw32-crossreport /usr/bin/inkscape > inkscape-report.txt
68  less inkscape-report.txt
69
70 =head1 SHORTCOMINGS
71
72 The report is only a general guide.  CrossReport contains a lot of
73 knowledge about common Linux calls and APIs, but does not know about
74 every possible library.
75
76 =head1 DATABASE
77
78 The program relies on a database of MinGW APIs.  The default location
79 for this database is C</usr/share/crossreport/crossreport.db> or the
80 same file in the current working directory.  If the database cannot be
81 found in either location, the program will fail with an error message.
82
83 The database is updated regularly and distributed with CrossReport.
84 To get the best quality report, make sure you are running a recent
85 version of the program.
86
87 =cut
88
89 my $help = '';
90 my $man = '';
91 my $verbose = '';
92 my $binary = '';
93
94 sub get_options
95 {
96     my $result = GetOptions (
97         "help|?" => \$help,
98         "man" => \$man,
99         "verbose" => \$verbose,
100     );
101     die "crossreport: use --help for information about command line options\n"
102         unless $result;
103
104     pod2usage(1) if $help;
105     pod2usage(-exitstatus => 0, -verbose => 2) if $man;
106
107     die "crossreport: no binary specified: use --help for more help\n"
108         if @ARGV != 1;
109
110     $binary = $ARGV[0];
111 }
112
113 my %symbols;
114
115 sub get_db
116 {
117     foreach ("/usr/local/share/crossreport/crossreport.db",
118              "/usr/share/crossreport/crossreport.db",
119              "crossreport.db") {
120         if (-f $_) {
121             tie %symbols, "GDBM_File", $_, &GDBM_READER, 0;
122             return;
123         }
124     }
125     die "Could not find crossreport.db\n"
126 }
127
128 # Get the symbols (API calls) used by the binary.
129
130 my %api = ();                   # Count how each API is used.
131 my @unresolved = ();            # List of unresolved symbols.
132
133 sub get_symbols
134 {
135     my $cmd = "nm -D $binary | grep ' U ' | awk '{print \$2}' | c++filt";
136     open CMD, "$cmd |" or die "$cmd: $!";
137     foreach (<CMD>) {
138         chomp;
139         if (exists $symbols{$_}) {
140             my $rpm_name = $symbols{$_};
141             $api{$rpm_name} = 0 unless exists $api{$rpm_name};
142             $api{$rpm_name}++;
143         } else {
144             push @unresolved, $_;
145         }
146     }
147     close CMD;
148 }
149
150 #----------------------------------------------------------------------
151 # Reporting section.
152
153 # This hash contains our area expertise about some unresolved symbols.
154
155 my $suggest_portability_library =
156     "To get more reliable semantics, we suggest you use a portability\n".
157     "library such as Gnulib, glib2, QtCore, etc.\n";
158 my $warning_about_read_write_on_sockets =
159     "If you are using read/write on sockets, then this won't work on\n".
160     "Windows.  You should use recv/send instead.\n";
161 my $ifdef_win32 =
162     "Use #ifndef WIN32 ... #else ... #endif around the Linux/Windows API\n".
163     "differences to ensure that your code continues to compile on Linux.\n";
164
165 my %report = (
166     open =>
167     "Program uses POSIX open/close/read/write/... APIs.  You should be\n".
168     "aware that Win32 provides functions with the same name which do not\n".
169     "have POSIX semantics.  Simple file operations will be fine, but you\n".
170     "will not be able to, for example, open /dev/* or other special files,\n".
171     "and select, locking and other POSIX features will not work the same\n".
172     "way.\n".
173     "\n".
174     "$suggest_portability_library".
175     "\n".
176     "$warning_about_read_write_on_sockets",
177     close => '@open',
178     read => '@open',
179     write => '@open',
180
181     socket =>
182     "Program uses Berkeley sockets API.  Windows has a reasonable facsimile\n".
183     "called Winsock.  However it has some annoying API differences, in\n".
184     "particular: (1) You have to use WSAGetLastError instead of errno,\n".
185     "(2) error numbers have different names, (3) you cannot select on,\n".
186     "sockets, (4) a multitude of small API differences, (5) you have to\n".
187     "initialize Winsock before using it by calling WSAStartup.\n".
188     "\n".
189     "$ifdef_win32".
190     "\n".
191     "$suggest_portability_library".
192     "\n".
193     "$warning_about_read_write_on_sockets",
194     socketpair => '@socket',
195     accept => '@socket',
196     bind => '@socket',
197     connect => '@socket',
198     listen => '@socket',
199     getsockopt => '@socket',
200     setsockopt => '@socket',
201     shutdown => '@socket',
202
203     ioctl =>
204     "Program uses fcntl or ioctl system calls.  Only a tiny fraction of\n".
205     "the functionality of these system calls is available in Windows,\n".
206     "often with differences in semantics.\n".
207     "\n".
208     "$suggest_portability_library",
209     fcntl => '@ioctl',
210
211     select =>
212     "The select/poll/etc system calls are not available on Windows.  You\n".
213     "have to use WSAWaitForMultipleEvents instead.\n".
214     "\n".
215     "$ifdef_win32".
216     "\n".
217     "$suggest_portability_library",
218     poll => '@select',
219     epoll_create => '@select',
220     epoll_ctl => '@select',
221     epoll_wait => '@select',
222
223     fork =>
224     "You cannot use fork to create new processes under Windows.  You have\n".
225     "to replace calls to fork/exec with CreateProcess or CreateThread.\n".
226     "\n".
227     "If your program forks in order to run in parallel or to create\n".
228     "multiple identical workers, then you may have to restructure the\n".
229     "program.\n".
230     "\n".
231     "If your program needs to share resources such as file descriptors\n".
232     "across the fork, then some limited options are available through\n".
233     "CreateProcess, but nothing like as rich as what is available in\n".
234     "Unix.\n".
235     "\n".
236     "$ifdef_win32".
237     "\n".
238     "For server programs, we suggest using a portability library tuned\n".
239     "for the needs of servers, such as Apache Portable Runtime.\n",
240     execl => '@fork',
241     execlp => '@fork',
242     execle => '@fork',
243     execv => '@fork',
244     execvp => '@fork',
245     execve => '@fork',
246
247     usleep =>
248     "usleep/nanosleep system calls do not exist on Windows.  You should\n".
249     "replace this with one of the Win32 equivalents such as Sleep.\n".
250     "\n".
251     "$ifdef_win32".
252     "\n".
253     "$suggest_portability_library",
254     nanosleep => '@usleep',
255
256     dup =>
257     "dup/dup2 may not work as expected in Win32.\n".
258     "\n".
259     "$suggest_portability_library",
260     dup2 => '@dup',
261
262     getopt_long =>
263     "GNU getopt_long is not available in Windows.\n".
264     "\n".
265     "$suggest_portability_library",
266
267     __stack_chk_fail =>
268     "The -fstack-protector option may not work with the Fedora MinGW\n".
269     "cross-compiler at this time.\n",
270
271     fopen =>
272     "fopen/fclose/... work for simple file operations on Windows.  If you\n".
273     "are using binary files, you must add the 'b' character to the fopen\n".
274     "call, otherwise Windows will try to do line-end translation.  The\n".
275     "'b' works on Linux (it is silently ignored).\n",
276     fclose => '@fopen',
277     fread => '@fopen',
278     fwrite => '@fopen',
279     fseek => '@fopen',
280     fgetpos => '@fopen',
281     feof => '@fopen',
282     fflush => '@fopen',
283     fseek => '@fopen',
284
285     getenv =>
286     "clearenv/getenv/putenv/setenv/unsetenv and direct access to environ\n".
287     "should be used with care under Windows.  Commonly available environment\n".
288     "variables may not exist in Windows, and Windows does not have any\n".
289     "simple mechanism for setting environment variables.\n".
290     "\n".
291     "$suggest_portability_library",
292     clearenv => '@getenv',
293     putenv => '@getenv',
294     setenv => '@getenv',
295     unsetenv => '@getenv',
296
297     tmpfile =>
298     "tmpfile is available on Windows, but the function is deprecated by\n".
299     "Microsoft in favour of a secure but proprietary replacement\n".
300     "(tmpfile_s).  You might consider using the replacement function on\n".
301     "Windows.\n".
302     "\n".
303     "$ifdef_win32",
304
305     rename =>
306     "rename is available on Windows, but it may not have the atomic\n".
307     "properties found on some Unix variants.  Do not rely on this as a\n".
308     "replacement for file locking.\n",
309
310     scanf =>
311     "scanf/sscanf is available on Windows, but behaves differently: it does\n".
312     "not automatically skip whitespace before %-specifiers.  If your code\n".
313     "relies on this, then you may need to modify it.\n".
314     "\n".
315     "See: http://msdn.microsoft.com/en-us/library/kwwtf9ch(VS.71).aspx\n",
316     sscanf => '@scanf',
317
318     signal =>
319     "Signal handling under Windows is very different/limited compared to\n".
320     "Unix.  It is likely that your code will need careful inspection if\n".
321     "it does anything non-trivial with signals.\n".
322     "\n".
323     "See: http://msdn.microsoft.com/en-us/library/xdkz3x12(VS.71).aspx\n",
324     sigaction => '@signal',
325     kill => '@signal',
326
327     popen =>
328     "popen/pclose exist in a some form under Windows (as _popen/\n".
329     "_pclose), but have many limitations and you should generally avoid\n".
330     "using them.\n".
331     "\n".
332     "See: http://msdn.microsoft.com/en-us/library/96ayss4b(VS.80).aspx\n".
333     "\n".
334     "$suggest_portability_library",
335     pclose => '@popen',
336
337     mkdir =>
338     "mkdir exists in several different forms on various platforms.\n".
339     "\n".
340     "$suggest_portability_library",
341
342     strtoll =>
343     "stroll/strtold/strtoul/strtoull do not exist on Windows.\n".
344     "\n".
345     "$suggest_portability_library",
346     strtold => '@strtoll',
347     strtoul => '@strtoll',
348     strtoull => '@strtoll',
349
350     system =>
351     "You should be careful using the 'system' call on Windows.  It exists\n".
352     "but the commands that it runs are quite different.  Available commands,\n".
353     "paths, shells, redirection, directory separators, etc. are all likely\n".
354     "to be incompatible with Linux.\n",
355
356     rand =>
357     "The standard random functions are useless in Windows since they return\n".
358     "only a very limited range of numbers and do not have good randomness.\n".
359     "\n".
360     "$suggest_portability_library",
361     srand => '@rand',
362
363     regcomp =>
364     "Win32 does not have POSIX regular expression parsing (regcomp/\n".
365     "regexec/etc.)  Use an external regular expression library instead.\n",
366     regexec => '@regcomp',
367     regfree => '@regcomp',
368
369     wait =>
370     "Win32 does not support wait/waitpid system calls.  You will have to\n".
371     "rewrite code that depends on waiting & PIDs using Win32 specific APIs.\n".
372     "\n".
373     "$ifdef_win32".
374     "\n".
375     "$suggest_portability_library",
376     waitpid => '@wait',
377     waitid => '@wait',
378     wait4 => '@wait',
379
380
381
382
383
384
385 );
386
387 # Additional reports, so we can match by regular expression.
388 # Returns a string, or undef if no match.
389
390 sub report_extra
391 {
392     local $_ = shift;           # Symbol.
393
394     if (m/^X[A-Z][A-Za-z0-9]+$/) {
395         return
396         "X11 is not available on Windows, and in general your program\n".
397         "will not be talking to an X server.  Replace any X11 calls with\n".
398         "calls to a higher-level portable library like Gtk or Qt.\n";
399     }
400
401     if (m/^dbus_$/) {
402         return
403         "The DBus API has proven problematic to port to Windows in the\n".
404         "past.  For the latest information about this, contact the\n".
405         "Fedora MinGW mailing list.\n";
406     }
407
408     return undef;
409 }
410
411 # List of symbols for which there is no known problem.
412
413 my %no_report = (
414     __assert_fail => 1,
415     __dynamic_cast => 1,
416     __errno_location => 1,
417     __libc_start_main => 1,
418     _IO_getc => 1,
419     _IO_putc => 1,
420     _Unwind_Resume => 1,
421     _exit => 1,
422     abort => 1,
423     acos => 1,    # XXX any incompatibility in math library calls?
424     asin => 1,
425     atan => 1,
426     atan2 => 1,
427     cabs => 1,
428     calloc => 1,
429     ceil => 1,
430     ceilf => 1,
431     cos => 1,
432     exit => 1,
433     fgetc => 1,
434     fgets => 1,
435     floor => 1,
436     floorf => 1,
437     fmod => 1,
438     fputc => 1,
439     fputs => 1,
440     free => 1,
441     malloc => 1,
442     memchr => 1,
443     memcpy => 1,
444     memmove => 1,
445     memset => 1,
446     perror => 1,
447     recv => 1,
448     send => 1,
449     setlocale => 1,
450     sqrt => 1,
451     sin => 1,
452     strcasecmp => 1,
453     strchr => 1,
454     strcmp => 1,
455     strcpy => 1,
456     strdup => 1,
457     strftime => 1,
458     strlen => 1,
459     strncmp => 1,
460     strncpy => 1,
461     strrchr => 1,
462     strspn => 1,
463     strstr => 1,
464     strtod => 1,
465     strtol => 1,
466     tan => 1,
467 );
468
469 sub no_report_extra
470 {
471     local $_ = shift;
472
473     return 1 if m/^__cxa_/;
474     return 1 if m/^operator new\(/;
475     return 1 if m/^operator new\[/;
476     return 1 if m/^__\w+_chk$/;
477     return 1 if m/^gtk_x11_/;
478     return 0
479 }
480
481 sub report_start
482 {
483     my $time = time ();
484     my $date = strftime "%a %b %e %H:%M:%S %Y", localtime ($time);
485     my $sym_time = $symbols{__crossreport_time};
486     my $sym_date = strftime "%a %b %e %H:%M:%S %Y", localtime ($sym_time);
487     my $days = floor (($time - $sym_time) / 86400);
488
489     print <<EOT;
490 ----------------------------------------------------------------------
491 Cross-compilation report for: $binary
492 ----------------------------------------------------------------------
493
494 1. INTRODUCTION
495
496 Report prepared on $date.
497 Symbol database last updated on $sym_date ($days days ago).
498
499 EOT
500 }
501
502 # Report resolved libraries.
503
504 sub report_resolved
505 {
506     print <<EOT;
507 2. SUPPORTED APIs USED BY THE PROGRAM
508
509 This table shows the supported APIs that this program uses,
510 including the number of different calls made to each API.
511 In most cases, you just need to arrange it so that your program
512 'BuildRequires' these RPMs and links to the libraries within them.
513
514       #calls  RPM name
515 EOT
516
517     foreach (sort (keys %api)) {
518         printf "  %10d  %s\n", $api{$_}, $_;
519     }
520
521     print "\n";
522 }
523
524 # Report unresolved symbols.
525
526 sub report_unresolved
527 {
528     @unresolved = sort @unresolved;
529
530     if (0 == @unresolved) {
531         print <<EOT;
532 3. UNSUPPORTED APIs USED BY THE PROGRAM
533
534 No unresolved symbols were found.  Programs which have no
535 unresolved symbols at all are the easiest to port because
536 portability libraries (eg. glib, Qt) have already done all
537 the hard work for you.
538 EOT
539     } else {
540         my $nr_unresolved = @unresolved;
541         print <<EOT;
542 3. UNSUPPORTED APIs USED BY THE PROGRAM
543
544 $nr_unresolved unresolved symbols were found.  The full list of symbols
545 is listed as an appendix at the end of this report.  In this
546 section we try to identify known portability problems from
547 this list of symbols.
548
549 EOT
550
551         foreach (@unresolved) {
552             if (!exists $no_report{$_} && !no_report_extra($_)) {
553                 if (exists $report{$_}) {
554                     my $r = $report{$_};
555                     $r = $report{$1} while $r =~ /@(.*)/;
556                     print "--- Program uses: $_\n\n$r\n";
557                 } else {
558                     my $r = report_extra ($_);
559                     print "--- Program uses: $_\n\n$r\n" if defined $r;
560                 }
561             }
562         }
563
564
565         print <<EOT;
566 APPENDIX - FULL LIST OF UNRESOLVED SYMBOLS
567
568 * = Symbol we were not able to give advice about.  If you know
569     more about this symbol, consider providing a patch for the
570     CrossReport program.
571
572 \x{2714} = Win32 should supply this symbol, or it can be ignored because
573     it is a side-effect of the Unix toolchain.
574
575 \x{2191} = Read the report above for advice about using this symbol.
576
577 EOT
578
579         foreach (@unresolved) {
580             my $star = " ";
581             if (exists $no_report{$_} || no_report_extra($_)) {
582                 $star = "\x{2714}";
583             } else {
584                 $star = "*";
585                 $star = "\x{2191}" if exists $report{$_};
586                 my $r = report_extra ($_);
587                 $star = "\x{2191}" if defined $r;
588             }
589             print "\t$star $_\n"
590         }
591     }
592
593     print "\n";
594 }
595
596 sub report_links
597 {
598     print <<EOT;
599 USEFUL LINKS
600
601 http://www.gnu.org/software/gnulib/  Gnulib (portability library)
602 http://library.gnome.org/devel/glib/ Glib reference manual
603 http://www.gtk.org/                  Gtk+
604 http://www.qtsoftware.com/products   Qt
605 http://apr.apache.org/               Apache Portable Runtime
606 http://msdn.microsoft.com/           Microsoft Developer Network
607 http://fedoraproject.org/wiki/MinGW  Fedora MinGW project
608 EOT
609 }
610
611 # Main program.
612
613 sub main
614 {
615     get_options ();
616     get_db ();
617     get_symbols ();
618     report_start ();
619     report_resolved ();
620     report_unresolved ();
621     report_links ();
622 }
623
624 main ();
625
626 =pod
627
628 =head1 COPYRIGHT
629
630 Copyright (C) 2009 Red Hat Inc.
631 Written by Richard W.M. Jones <rjones@redhat.com>.
632
633 This program is free software; you can redistribute it and/or modify
634 it under the terms of the GNU General Public License as published by
635 the Free Software Foundation; either version 2 of the License, or (at
636 your option) any later version.
637
638 This program is distributed in the hope that it will be useful, but
639 WITHOUT ANY WARRANTY; without even the implied warranty of
640 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
641 General Public License for more details.
642
643 You should have received a copy of the GNU General Public License
644 along with this program; if not, write to the Free Software
645 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
646
647 =cut