Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
Manuel Pégourié-Gonnard | 50868a7 | 2014-05-09 13:01:21 +0200 | [diff] [blame] | 3 | # Generate files for MS Visual Studio: |
| 4 | # - for VS6: main project (library) file, individual app files, workspace |
| 5 | # - for VS2010: main file, individual apps, solution file |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 6 | # |
| 7 | # Must be run from PolarSSL root or scripts directory. |
| 8 | # Takes no argument. |
Manuel Pégourié-Gonnard | 684e9dc | 2013-09-20 15:11:44 +0200 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 10 | use warnings; |
| 11 | use strict; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 12 | use Digest::MD5 'md5_hex'; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 14 | my $vs6_dir = "visualc/VS6"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 15 | my $vs6_ext = "dsp"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 16 | 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] | 17 | my $vs6_main_tpl_file = "scripts/data_files/vs6-main-template.$vs6_ext"; |
| 18 | my $vs6_main_file = "$vs6_dir/polarssl.$vs6_ext"; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 19 | my $vs6_wsp_tpl_file = "scripts/data_files/vs6-workspace-template.dsw"; |
| 20 | my $vs6_wsp_file = "$vs6_dir/polarssl.dsw"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 22 | my $vsx_dir = "visualc/VS2010"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 23 | my $vsx_ext = "vcxproj"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 24 | 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] | 25 | my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext"; |
| 26 | my $vsx_main_file = "$vsx_dir/PolarSSL.$vsx_ext"; |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 27 | my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln"; |
| 28 | my $vsx_sln_file = "$vsx_dir/PolarSSL.sln"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 29 | |
| 30 | my $programs_dir = 'programs'; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 31 | my $header_dir = 'include/polarssl'; |
| 32 | my $source_dir = 'library'; |
| 33 | |
| 34 | # Need windows line endings! |
| 35 | my $vs6_file_tpl = <<EOT; |
| 36 | # Begin Source File\r |
| 37 | \r |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 38 | SOURCE=..\\..\\{NAME}\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 39 | # End Source File\r |
| 40 | EOT |
| 41 | |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 42 | my $vs6_wsp_entry_tpl = <<EOT; |
| 43 | ###############################################################################\r |
| 44 | \r |
| 45 | Project: "{NAME}"=.\\{NAME}.dsp - Package Owner=<4>\r |
| 46 | \r |
| 47 | Package=<5>\r |
| 48 | {{{\r |
| 49 | }}}\r |
| 50 | \r |
| 51 | Package=<4>\r |
| 52 | {{{\r |
| 53 | Begin Project Dependency\r |
| 54 | Project_Dep_Name polarssl\r |
| 55 | End Project Dependency\r |
| 56 | }}}\r |
| 57 | \r |
| 58 | EOT |
| 59 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 60 | my $vsx_hdr_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 61 | <ClInclude Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 62 | EOT |
| 63 | my $vsx_src_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 64 | <ClCompile Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 65 | EOT |
| 66 | |
| 67 | my $vsx_sln_app_entry_tpl = <<EOT; |
| 68 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r |
| 69 | ProjectSection(ProjectDependencies) = postProject\r |
| 70 | {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r |
| 71 | EndProjectSection\r |
| 72 | EndProject\r |
| 73 | EOT |
| 74 | |
| 75 | my $vsx_sln_conf_entry_tpl = <<EOT; |
| 76 | {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r |
| 77 | {GUID}.Debug|Win32.Build.0 = Debug|Win32\r |
| 78 | {GUID}.Debug|x64.ActiveCfg = Debug|x64\r |
| 79 | {GUID}.Debug|x64.Build.0 = Debug|x64\r |
| 80 | {GUID}.Release|Win32.ActiveCfg = Release|Win32\r |
| 81 | {GUID}.Release|Win32.Build.0 = Release|Win32\r |
| 82 | {GUID}.Release|x64.ActiveCfg = Release|x64\r |
| 83 | {GUID}.Release|x64.Build.0 = Release|x64\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 84 | EOT |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 85 | |
| 86 | exit( main() ); |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 87 | |
| 88 | sub check_dirs { |
| 89 | return -d $vs6_dir |
| 90 | && -d $vsx_dir |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 91 | && -d $header_dir |
| 92 | && -d $source_dir |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 93 | && -d $programs_dir; |
| 94 | } |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 95 | |
| 96 | sub slurp_file { |
| 97 | my ($filename) = @_; |
| 98 | |
| 99 | local $/ = undef; |
| 100 | open my $fh, '<', $filename or die "Could not read $filename\n"; |
| 101 | my $content = <$fh>; |
| 102 | close $fh; |
| 103 | |
| 104 | return $content; |
| 105 | } |
| 106 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 107 | sub content_to_file { |
| 108 | my ($content, $filename) = @_; |
| 109 | |
| 110 | open my $fh, '>', $filename or die "Could not write to $filename\n"; |
| 111 | print $fh $content; |
| 112 | close $fh; |
| 113 | } |
| 114 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 115 | sub gen_app_guid { |
| 116 | my ($path) = @_; |
| 117 | |
| 118 | my $guid = md5_hex( "PolarSSL:$path" ); |
| 119 | $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/; |
| 120 | |
| 121 | return $guid; |
| 122 | } |
| 123 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 124 | sub gen_app { |
| 125 | my ($path, $template, $dir, $ext) = @_; |
| 126 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 127 | my $guid = gen_app_guid( $path ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 128 | $path =~ s!/!\\!g; |
| 129 | (my $appname = $path) =~ s/.*\\//; |
| 130 | |
| 131 | my $content = $template; |
| 132 | $content =~ s/<PATHNAME>/$path/g; |
| 133 | $content =~ s/<APPNAME>/$appname/g; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 134 | $content =~ s/<GUID>/$guid/g; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 135 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 136 | content_to_file( $content, "$dir/$appname.$ext" ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | sub get_app_list { |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 140 | my $app_list = `cd $programs_dir && make list`; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 141 | die "make list failed: $!\n" if $?; |
| 142 | |
| 143 | return split /\s+/, $app_list; |
| 144 | } |
| 145 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 146 | sub gen_app_files { |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 147 | my @app_list = @_; |
| 148 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 149 | my $vs6_tpl = slurp_file( $vs6_app_tpl_file ); |
| 150 | my $vsx_tpl = slurp_file( $vsx_app_tpl_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 151 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 152 | for my $app ( @app_list ) { |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 153 | gen_app( $app, $vs6_tpl, $vs6_dir, $vs6_ext ); |
| 154 | gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext ); |
| 155 | } |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | sub gen_entry_list { |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 159 | my ($tpl, @names) = @_; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 160 | |
| 161 | my $entries; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 162 | for my $name (@names) { |
| 163 | (my $entry = $tpl) =~ s/{NAME}/$name/g; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 164 | $entries .= $entry; |
| 165 | } |
| 166 | |
| 167 | return $entries; |
| 168 | } |
| 169 | |
| 170 | sub gen_main_file { |
| 171 | my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_; |
| 172 | |
| 173 | my $header_entries = gen_entry_list( $hdr_tpl, @$headers ); |
| 174 | my $source_entries = gen_entry_list( $src_tpl, @$sources ); |
| 175 | |
| 176 | my $out = slurp_file( $main_tpl ); |
| 177 | $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m; |
| 178 | $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m; |
| 179 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 180 | content_to_file( $out, $main_out ); |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | sub gen_vs6_workspace { |
| 184 | my (@app_names) = @_; |
| 185 | |
| 186 | map { s!.*/!! } @app_names; |
| 187 | my $entries = gen_entry_list( $vs6_wsp_entry_tpl, @app_names ); |
| 188 | |
| 189 | my $out = slurp_file( $vs6_wsp_tpl_file ); |
| 190 | $out =~ s/APP_ENTRIES\r\n/$entries/m; |
| 191 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 192 | content_to_file( $out, $vs6_wsp_file ); |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 195 | sub gen_vsx_solution { |
| 196 | my (@app_names) = @_; |
| 197 | |
| 198 | my ($app_entries, $conf_entries); |
| 199 | for my $path (@app_names) { |
| 200 | my $guid = gen_app_guid( $path ); |
| 201 | (my $appname = $path) =~ s!.*/!!; |
| 202 | |
| 203 | my $app_entry = $vsx_sln_app_entry_tpl; |
| 204 | $app_entry =~ s/{APPNAME}/$appname/g; |
| 205 | $app_entry =~ s/{GUID}/$guid/g; |
| 206 | |
| 207 | $app_entries .= $app_entry; |
| 208 | |
| 209 | my $conf_entry = $vsx_sln_conf_entry_tpl; |
| 210 | $conf_entry =~ s/{GUID}/$guid/g; |
| 211 | |
| 212 | $conf_entries .= $conf_entry; |
| 213 | } |
| 214 | |
| 215 | my $out = slurp_file( $vsx_sln_tpl_file ); |
| 216 | $out =~ s/APP_ENTRIES\r\n/$app_entries/m; |
| 217 | $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m; |
| 218 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 219 | content_to_file( $out, $vsx_sln_file ); |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 222 | sub main { |
| 223 | if( ! check_dirs() ) { |
| 224 | chdir '..' or die; |
| 225 | check_dirs or die "Must but run from PolarSSL root or scripts dir\n"; |
| 226 | } |
| 227 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 228 | my @app_list = get_app_list(); |
| 229 | my @headers = <$header_dir/*.h>; |
| 230 | my @sources = <$source_dir/*.c>; |
| 231 | map { s!/!\\!g } @headers; |
| 232 | map { s!/!\\!g } @sources; |
| 233 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 234 | gen_app_files( @app_list ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 235 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 236 | gen_main_file( \@headers, \@sources, |
| 237 | $vs6_file_tpl, $vs6_file_tpl, |
| 238 | $vs6_main_tpl_file, $vs6_main_file ); |
| 239 | gen_main_file( \@headers, \@sources, |
| 240 | $vsx_hdr_tpl, $vsx_src_tpl, |
| 241 | $vsx_main_tpl_file, $vsx_main_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 242 | |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 243 | gen_vs6_workspace( @app_list ); |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 244 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 245 | gen_vsx_solution( @app_list ); |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 246 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 247 | return 0; |
| 248 | } |