Merge pull request #224 from DOCGroup/jwillemsen-patch-1
[MPC.git] / generate_export_header.pl
blob2f6c3ae257342ec06b5a52880eb9e62322ee9c60
1 #!/usr/bin/env perl
2 eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
3 & eval 'exec perl -w -S $0 $argv:q'
4 if 0;
6 # ******************************************************************
7 # Author: Chad Elliott
8 # Date: 3/1/2006
9 # Description: Generate an export header file for use with various compilers
10 # ******************************************************************
12 # ******************************************************************
13 # Pragma Section
14 # ******************************************************************
16 use strict;
17 use FileHandle;
18 use File::Basename;
20 # ******************************************************************
21 # Data Section
22 # ******************************************************************
24 my $version = '1.2';
26 # ******************************************************************
27 # Subroutine Section
28 # ******************************************************************
30 sub generate_export_header {
31 my($name, $output) = @_;
32 my $fh = new FileHandle();
33 my $status = 0;
35 if (open($fh, ">$output")) {
36 $name = uc($name);
37 print $fh <<EOM
38 #ifndef ${name}_EXPORT_H
39 #define ${name}_EXPORT_H
41 #if !defined(${name}_HAS_DLL)
42 # if defined(${name}_AS_STATIC_LIBS)
43 # define ${name}_HAS_DLL 0
44 # else
45 # define ${name}_HAS_DLL 1
46 # endif
47 #endif
49 #if (${name}_HAS_DLL == 1)
50 # if defined(__SUNPRO_CC) && (__SUNPRO_CC >= 0x550)
51 # if defined(${name}_BUILD_DLL)
52 # define ${name}_Export __symbolic
53 # else
54 # define ${name}_Export __global
55 # endif
56 # elif defined(WIN32) || defined(UNDER_CE) || defined(__CYGWIN__)
57 # if defined(${name}_BUILD_DLL)
58 # define ${name}_Export __declspec(dllexport)
59 # else
60 # define ${name}_Export __declspec(dllimport)
61 # endif
62 # elif (defined(__GNUC__) && (__GNUC__ >= 4))
63 # if defined(${name}_BUILD_DLL)
64 # define ${name}_Export __attribute__((visibility("default")))
65 # else
66 # define ${name}_Export
67 # endif
68 # else
69 # define ${name}_Export
70 # endif
71 #else
72 # define ${name}_Export
73 #endif
75 #endif
76 EOM
78 close($fh);
79 print "Output written to $output\n";
81 else {
82 print STDERR "ERROR: Unable to write to $output\n";
83 ++$status;
86 return $status;
89 sub usageAndExit {
90 my $str = shift;
92 print STDERR "$str\n" if (defined $str);
93 print STDERR "Generate Export Header v$version\n",
94 "Usage: ", basename($0), " <library name> [output file]\n";
95 exit(0);
98 # ******************************************************************
99 # Main Section
100 # ******************************************************************
102 my $name = shift;
103 my $output = shift;
105 if (!defined $name) {
106 usageAndExit();
108 elsif (index($name, '-') == 0) {
109 usageAndExit();
112 if (!defined $output) {
113 $output = $name . '_' . ($name =~ /^[A-Z]/ ? 'E' : 'e') .
114 'xport.h';
117 if ($name =~ s/^\d+//) {
118 print "WARNING: Removing beginning numbers from export name.\n";
120 if ($name =~ s/-\s/_/g) {
121 print "WARNING: Converting dashes and ",
122 "whitespace to underscores in export name.\n";
125 exit(generate_export_header($name, $output));