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
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.
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.
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.
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";
32 use POSIX qw(strftime floor);
38 CrossReport - Analysis tool to help cross-compilation to Windows.
42 mingw32-crossreport [options] /path/to/linuxbinary
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
51 The simplest way to use it is to point it at an existing Linux binary,
52 and then read the generated report.
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.
64 Assuming that the excellent vector graphics editor Inkscape
65 (L<http://www.inkscape.org/>) is installed, you could do:
67 mingw32-crossreport /usr/bin/inkscape > inkscape-report.txt
68 less inkscape-report.txt
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.
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.
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.
96 my $result = GetOptions (
99 "verbose" => \$verbose,
101 die "crossreport: use --help for information about command line options\n"
104 pod2usage(1) if $help;
105 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
107 die "crossreport: no binary specified: use --help for more help\n"
117 foreach ("/usr/local/share/crossreport/crossreport.db",
118 "/usr/share/crossreport/crossreport.db",
121 tie %symbols, "GDBM_File", $_, &GDBM_READER, 0;
125 die "Could not find crossreport.db\n"
128 # Get the symbols (API calls) used by the binary.
130 my %api = (); # Count how each API is used.
131 my @unresolved = (); # List of unresolved symbols.
135 my $cmd = "nm -D $binary | grep ' U ' | awk '{print \$2}' | c++filt";
136 open CMD, "$cmd |" or die "$cmd: $!";
139 if (exists $symbols{$_}) {
140 my $rpm_name = $symbols{$_};
141 $api{$rpm_name} = 0 unless exists $api{$rpm_name};
144 push @unresolved, $_;
150 #----------------------------------------------------------------------
153 # This hash contains our area expertise about some unresolved symbols.
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";
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";
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".
174 "$suggest_portability_library".
176 "$warning_about_read_write_on_sockets",
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".
191 "$suggest_portability_library".
193 "$warning_about_read_write_on_sockets",
194 socketpair => '@socket',
197 connect => '@socket',
199 getsockopt => '@socket',
200 setsockopt => '@socket',
201 shutdown => '@socket',
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".
208 "$suggest_portability_library",
212 "The select/poll/etc system calls are not available on Windows. You\n".
213 "have to use WSAWaitForMultipleEvents instead.\n".
217 "$suggest_portability_library",
219 epoll_create => '@select',
220 epoll_ctl => '@select',
221 epoll_wait => '@select',
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".
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".
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".
238 "For server programs, we suggest using a portability library tuned\n".
239 "for the needs of servers, such as Apache Portable Runtime.\n",
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".
253 "$suggest_portability_library",
254 nanosleep => '@usleep',
257 "dup/dup2 may not work as expected in Win32.\n".
259 "$suggest_portability_library",
263 "GNU getopt_long is not available in Windows.\n".
265 "$suggest_portability_library",
268 "The -fstack-protector option may not work with the Fedora MinGW\n".
269 "cross-compiler at this time.\n",
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",
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".
291 "$suggest_portability_library",
292 clearenv => '@getenv',
295 unsetenv => '@getenv',
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".
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",
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".
315 "See: http://msdn.microsoft.com/en-us/library/kwwtf9ch(VS.71).aspx\n",
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".
323 "See: http://msdn.microsoft.com/en-us/library/xdkz3x12(VS.71).aspx\n",
324 sigaction => '@signal',
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".
332 "See: http://msdn.microsoft.com/en-us/library/96ayss4b(VS.80).aspx\n".
334 "$suggest_portability_library",
338 "mkdir exists in several different forms on various platforms.\n".
340 "$suggest_portability_library",
343 "stroll/strtold/strtoul/strtoull do not exist on Windows.\n".
345 "$suggest_portability_library",
346 strtold => '@strtoll',
347 strtoul => '@strtoll',
348 strtoull => '@strtoll',
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",
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".
360 "$suggest_portability_library",
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',
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".
375 "$suggest_portability_library",
387 # Additional reports, so we can match by regular expression.
388 # Returns a string, or undef if no match.
392 local $_ = shift; # Symbol.
394 if (m/^X[A-Z][A-Za-z0-9]+$/) {
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";
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";
411 # List of symbols for which there is no known problem.
416 __errno_location => 1,
417 __libc_start_main => 1,
423 acos => 1, # XXX any incompatibility in math library calls?
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_/;
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);
490 ----------------------------------------------------------------------
491 Cross-compilation report for: $binary
492 ----------------------------------------------------------------------
496 Report prepared on $date.
497 Symbol database last updated on $sym_date ($days days ago).
502 # Report resolved libraries.
507 2. SUPPORTED APIs USED BY THE PROGRAM
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.
517 foreach (sort (keys %api)) {
518 printf " %10d %s\n", $api{$_}, $_;
524 # Report unresolved symbols.
526 sub report_unresolved
528 @unresolved = sort @unresolved;
530 if (0 == @unresolved) {
532 3. UNSUPPORTED APIs USED BY THE PROGRAM
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.
540 my $nr_unresolved = @unresolved;
542 3. UNSUPPORTED APIs USED BY THE PROGRAM
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.
551 foreach (@unresolved) {
552 if (!exists $no_report{$_} && !no_report_extra($_)) {
553 if (exists $report{$_}) {
555 $r = $report{$1} while $r =~ /@(.*)/;
556 print "--- Program uses: $_\n\n$r\n";
558 my $r = report_extra ($_);
559 print "--- Program uses: $_\n\n$r\n" if defined $r;
566 APPENDIX - FULL LIST OF UNRESOLVED SYMBOLS
568 * = Symbol we were not able to give advice about. If you know
569 more about this symbol, consider providing a patch for the
572 \x{2714} = Win32 should supply this symbol, or it can be ignored because
573 it is a side-effect of the Unix toolchain.
575 \x{2191} = Read the report above for advice about using this symbol.
579 foreach (@unresolved) {
581 if (exists $no_report{$_} || no_report_extra($_)) {
585 $star = "\x{2191}" if exists $report{$_};
586 my $r = report_extra ($_);
587 $star = "\x{2191}" if defined $r;
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
620 report_unresolved ();
630 Copyright (C) 2009 Red Hat Inc.
631 Written by Richard W.M. Jones <rjones@redhat.com>.
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.
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.
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.