]> Creatis software - cpPlugins.git/blob - appli/bash/CreateWin32Installer.cxx
...
[cpPlugins.git] / appli / bash / CreateWin32Installer.cxx
1 /* =========================================================================
2  * @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3  * =========================================================================
4  */
5 #include <climits>
6 #include <cstdlib>
7 #include <iostream>
8 #include <memory>
9 #include <queue>
10 #include <set>
11 #include <sstream>
12 #include <stdexcept>
13 #include <string>
14 #include <sys/stat.h>
15 #include <unistd.h>
16
17 // -------------------------------------------------------------------------
18 void Split( const std::string& s, std::string& d, std::string& f )
19 {
20   size_t found = s.find_last_of( "/\\" );
21   d = s.substr( 0, found );
22   f = s.substr( found + 1 );
23 }
24
25 // -------------------------------------------------------------------------
26 inline bool Exists( const std::string& name )
27 {
28   struct stat buffer;
29   return( stat( name.c_str( ), &buffer) == 0 );
30 }
31
32 // -------------------------------------------------------------------------
33 std::string Exec( const std::string& cmd )
34 {
35   std::array< char, 128 > buffer;
36   std::string result;
37   std::shared_ptr< FILE > pipe( popen( cmd.c_str( ), "r" ), pclose );
38   if( !pipe )
39     throw std::runtime_error( "popen( ) failed!" );
40   while( !feof( pipe.get( ) ) )
41     if( fgets( buffer.data( ), 128, pipe.get( ) ) != NULL )
42       result += buffer.data( );
43   return( result );
44 }
45
46 // -------------------------------------------------------------------------
47 int main( int argc, char* argv[] )
48 {
49   if( argc < 3 )
50   {
51     std::cerr
52       << "Usage: " << argv[ 0 ]
53       << " entry_point objdump_tool [other_dir(s)]"
54       << std::endl;
55       return( 1 );
56
57   } // fi
58   std::string entry_point, objdump_tool;
59   std::set< std::string > dirs;
60
61   if( !Exists( argv[ 1 ] ) )
62   {
63     std::cerr
64       << "Error: file \"" << argv[ 1 ] << "\" does not exist."
65       << std::endl;
66     return( 1 );
67
68   } // fi
69   if( !Exists( argv[ 2 ] ) )
70   {
71     std::cerr
72       << "Error: file \"" << argv[ 2 ] << "\" does not exist."
73       << std::endl;
74     return( 1 );
75
76   } // fi
77
78   entry_point = realpath( argv[ 1 ], NULL );
79   objdump_tool = realpath( argv[ 2 ], NULL );
80
81   std::string entry_point_dir, entry_point_file;
82   Split( entry_point, entry_point_dir, entry_point_file );
83
84   dirs.insert( entry_point_dir );
85   for( int i = 3; i < argc; ++i )
86     if( Exists( argv[ i ] ) )
87       dirs.insert( std::string( realpath( argv[ i ], NULL ) ) );
88
89   std::set< std::string > files;
90   std::queue< std::string > q;
91   q.push( entry_point );
92   while( !q.empty( ) )
93   {
94     std::string e = q.front( );
95     q.pop( );
96     if( files.find( e ) == files.end( ) )
97     {
98       try
99       {
100         if( Exists( e ) )
101         {
102           std::istringstream ss(
103             Exec( objdump_tool + " -x " + e + " | grep DLL\\ Name" )
104             );
105           files.insert( e );
106           std::string line;
107           while( std::getline( ss, line ) )
108           {
109             size_t p = line.find( ":" ) + 2;
110             std::set< std::string >::const_iterator d;
111             for( d = dirs.begin( ); d != dirs.end( ); ++d )
112               q.push( *d + "/" + line.substr( p ) );
113
114           } // elihw
115
116         } // fi
117       }
118       catch( std::exception& err )
119       {
120         // Just ignore
121       } // yrt
122
123     } // fi
124
125   } // elihw
126
127   for( std::string f: files )
128     std::cout << "---> " << f << std::endl;
129
130   return( 0 );
131 }
132
133 // eof - $RCSfile$