Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
Manuel Pégourié-Gonnard | 684e9dc | 2013-09-20 15:11:44 +0200 | [diff] [blame] | 3 | # create individual project files for example programs |
| 4 | # for VS6 and VS2010 |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 5 | # |
| 6 | # Must be run from PolarSSL root or scripts directory. |
| 7 | # Takes no argument. |
Manuel Pégourié-Gonnard | 684e9dc | 2013-09-20 15:11:44 +0200 | [diff] [blame] | 8 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 9 | use warnings; |
| 10 | use strict; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame^] | 11 | use Digest::MD5 'md5_hex'; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 12 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 13 | my $vs6_dir = "visualc/VS6"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 14 | my $vs6_ext = "dsp"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 15 | my $vs6_app_tpl_file = "scripts/data_files/vs6-app-template.$vs6_ext"; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 16 | my $vs6_main_tpl_file = "scripts/data_files/vs6-main-template.$vs6_ext"; |
| 17 | my $vs6_main_file = "$vs6_dir/polarssl.$vs6_ext"; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 18 | my $vs6_wsp_tpl_file = "scripts/data_files/vs6-workspace-template.dsw"; |
| 19 | my $vs6_wsp_file = "$vs6_dir/polarssl.dsw"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 20 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 21 | my $vsx_dir = "visualc/VS2010"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 22 | my $vsx_ext = "vcxproj"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 23 | my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext"; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 24 | my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext"; |
| 25 | my $vsx_main_file = "$vsx_dir/PolarSSL.$vsx_ext"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 26 | |
| 27 | my $programs_dir = 'programs'; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 28 | my $header_dir = 'include/polarssl'; |
| 29 | my $source_dir = 'library'; |
| 30 | |
| 31 | # Need windows line endings! |
| 32 | my $vs6_file_tpl = <<EOT; |
| 33 | # Begin Source File\r |
| 34 | \r |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 35 | SOURCE=..\\..\\{NAME}\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 36 | # End Source File\r |
| 37 | EOT |
| 38 | |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 39 | my $vs6_wsp_entry_tpl = <<EOT; |
| 40 | ###############################################################################\r |
| 41 | \r |
| 42 | Project: "{NAME}"=.\\{NAME}.dsp - Package Owner=<4>\r |
| 43 | \r |
| 44 | Package=<5>\r |
| 45 | {{{\r |
| 46 | }}}\r |
| 47 | \r |
| 48 | Package=<4>\r |
| 49 | {{{\r |
| 50 | Begin Project Dependency\r |
| 51 | Project_Dep_Name polarssl\r |
| 52 | End Project Dependency\r |
| 53 | }}}\r |
| 54 | \r |
| 55 | EOT |
| 56 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 57 | my $vsx_hdr_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 58 | <ClInclude Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 59 | EOT |
| 60 | my $vsx_src_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 61 | <ClCompile Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 62 | EOT |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 63 | |
| 64 | exit( main() ); |
| 65 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 66 | sub check_dirs { |
| 67 | return -d $vs6_dir |
| 68 | && -d $vsx_dir |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 69 | && -d $header_dir |
| 70 | && -d $source_dir |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 71 | && -d $programs_dir; |
| 72 | } |
| 73 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 74 | sub slurp_file { |
| 75 | my ($filename) = @_; |
| 76 | |
| 77 | local $/ = undef; |
| 78 | open my $fh, '<', $filename or die "Could not read $filename\n"; |
| 79 | my $content = <$fh>; |
| 80 | close $fh; |
| 81 | |
| 82 | return $content; |
| 83 | } |
| 84 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame^] | 85 | sub gen_app_guid { |
| 86 | my ($path) = @_; |
| 87 | |
| 88 | my $guid = md5_hex( "PolarSSL:$path" ); |
| 89 | $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/; |
| 90 | |
| 91 | return $guid; |
| 92 | } |
| 93 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 94 | sub gen_app { |
| 95 | my ($path, $template, $dir, $ext) = @_; |
| 96 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame^] | 97 | my $guid = gen_app_guid( $path ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 98 | $path =~ s!/!\\!g; |
| 99 | (my $appname = $path) =~ s/.*\\//; |
| 100 | |
| 101 | my $content = $template; |
| 102 | $content =~ s/<PATHNAME>/$path/g; |
| 103 | $content =~ s/<APPNAME>/$appname/g; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame^] | 104 | $content =~ s/<GUID>/$guid/g; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 105 | |
| 106 | open my $app_fh, '>', "$dir/$appname.$ext"; |
| 107 | print $app_fh $content; |
| 108 | close $app_fh; |
| 109 | } |
| 110 | |
| 111 | sub get_app_list { |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 112 | my $app_list = `cd $programs_dir && make list`; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 113 | die "make list failed: $!\n" if $?; |
| 114 | |
| 115 | return split /\s+/, $app_list; |
| 116 | } |
| 117 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 118 | sub gen_app_files { |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 119 | my @app_list = @_; |
| 120 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 121 | my $vs6_tpl = slurp_file( $vs6_app_tpl_file ); |
| 122 | my $vsx_tpl = slurp_file( $vsx_app_tpl_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 123 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 124 | for my $app ( @app_list ) { |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 125 | gen_app( $app, $vs6_tpl, $vs6_dir, $vs6_ext ); |
| 126 | gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext ); |
| 127 | } |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 130 | sub gen_entry_list { |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 131 | my ($tpl, @names) = @_; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 132 | |
| 133 | my $entries; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 134 | for my $name (@names) { |
| 135 | (my $entry = $tpl) =~ s/{NAME}/$name/g; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 136 | $entries .= $entry; |
| 137 | } |
| 138 | |
| 139 | return $entries; |
| 140 | } |
| 141 | |
| 142 | sub gen_main_file { |
| 143 | my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_; |
| 144 | |
| 145 | my $header_entries = gen_entry_list( $hdr_tpl, @$headers ); |
| 146 | my $source_entries = gen_entry_list( $src_tpl, @$sources ); |
| 147 | |
| 148 | my $out = slurp_file( $main_tpl ); |
| 149 | $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m; |
| 150 | $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m; |
| 151 | |
| 152 | open my $fh, '>', $main_out or die; |
| 153 | print $fh $out; |
| 154 | close $fh; |
| 155 | } |
| 156 | |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 157 | sub gen_vs6_workspace { |
| 158 | my (@app_names) = @_; |
| 159 | |
| 160 | map { s!.*/!! } @app_names; |
| 161 | my $entries = gen_entry_list( $vs6_wsp_entry_tpl, @app_names ); |
| 162 | |
| 163 | my $out = slurp_file( $vs6_wsp_tpl_file ); |
| 164 | $out =~ s/APP_ENTRIES\r\n/$entries/m; |
| 165 | |
| 166 | open my $fh, '>', $vs6_wsp_file or die; |
| 167 | print $fh $out; |
| 168 | close $fh; |
| 169 | } |
| 170 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 171 | sub main { |
| 172 | if( ! check_dirs() ) { |
| 173 | chdir '..' or die; |
| 174 | check_dirs or die "Must but run from PolarSSL root or scripts dir\n"; |
| 175 | } |
| 176 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 177 | my @app_list = get_app_list(); |
| 178 | my @headers = <$header_dir/*.h>; |
| 179 | my @sources = <$source_dir/*.c>; |
| 180 | map { s!/!\\!g } @headers; |
| 181 | map { s!/!\\!g } @sources; |
| 182 | |
| 183 | print "Generating apps files... "; |
| 184 | gen_app_files( @app_list ); |
| 185 | print "done.\n"; |
| 186 | |
| 187 | print "Generating main files... "; |
| 188 | gen_main_file( \@headers, \@sources, |
| 189 | $vs6_file_tpl, $vs6_file_tpl, |
| 190 | $vs6_main_tpl_file, $vs6_main_file ); |
| 191 | gen_main_file( \@headers, \@sources, |
| 192 | $vsx_hdr_tpl, $vsx_src_tpl, |
| 193 | $vsx_main_tpl_file, $vsx_main_file ); |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 194 | print "done.\n"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 195 | |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 196 | print "Generating VS6 workspace file... "; |
| 197 | gen_vs6_workspace( @app_list ); |
| 198 | print "done.\n"; |
| 199 | |
| 200 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 201 | return 0; |
| 202 | } |