fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 2 | |
Andres AG | 5121d4b | 2018-04-11 20:35:19 -0500 | [diff] [blame] | 3 | # Generate main file, individual apps and solution files for MS Visual Studio |
| 4 | # 2010 |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 5 | # |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 6 | # Must be run from mbedTLS root or scripts directory. |
Andrzej Kurek | 021dc3f | 2019-04-12 10:51:27 -0400 | [diff] [blame] | 7 | # Takes "include_crypto" as an argument that can be either 0 (don't include) or |
| 8 | # 1 (include). Off by default. |
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 $vsx_dir = "visualc/VS2010"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 15 | my $vsx_ext = "vcxproj"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 16 | 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] | 17 | my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext"; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 18 | my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext"; |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 19 | my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln"; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 20 | my $vsx_sln_file = "$vsx_dir/mbedTLS.sln"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 21 | |
Andrzej Kurek | 92f91fc | 2019-04-05 05:49:53 -0400 | [diff] [blame] | 22 | my $include_crypto = 0; |
| 23 | if( @ARGV ) { |
| 24 | die "Invalid number of arguments" if scalar @ARGV != 1; |
| 25 | ($include_crypto) = @ARGV; |
| 26 | } |
| 27 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 28 | my $programs_dir = 'programs'; |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | my $header_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 30 | my $source_dir = 'library'; |
Andrzej Kurek | 021dc3f | 2019-04-12 10:51:27 -0400 | [diff] [blame] | 31 | my $crypto_dir = 'crypto'; |
Andrzej Kurek | 92f91fc | 2019-04-05 05:49:53 -0400 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 33 | # Need windows line endings! |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 34 | my $vsx_hdr_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 35 | <ClInclude Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 36 | EOT |
| 37 | my $vsx_src_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 38 | <ClCompile Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 39 | EOT |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 40 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 41 | my $vsx_sln_app_entry_tpl = <<EOT; |
| 42 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r |
| 43 | ProjectSection(ProjectDependencies) = postProject\r |
| 44 | {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r |
| 45 | EndProjectSection\r |
| 46 | EndProject\r |
| 47 | EOT |
| 48 | |
| 49 | my $vsx_sln_conf_entry_tpl = <<EOT; |
| 50 | {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r |
| 51 | {GUID}.Debug|Win32.Build.0 = Debug|Win32\r |
| 52 | {GUID}.Debug|x64.ActiveCfg = Debug|x64\r |
| 53 | {GUID}.Debug|x64.Build.0 = Debug|x64\r |
| 54 | {GUID}.Release|Win32.ActiveCfg = Release|Win32\r |
| 55 | {GUID}.Release|Win32.Build.0 = Release|Win32\r |
| 56 | {GUID}.Release|x64.ActiveCfg = Release|x64\r |
| 57 | {GUID}.Release|x64.Build.0 = Release|x64\r |
| 58 | EOT |
| 59 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 60 | exit( main() ); |
| 61 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 62 | sub check_dirs { |
Manuel Pégourié-Gonnard | 51f14be | 2015-05-14 13:04:03 +0200 | [diff] [blame] | 63 | return -d $vsx_dir |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 64 | && -d $header_dir |
| 65 | && -d $source_dir |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 66 | && -d $programs_dir; |
| 67 | } |
| 68 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 69 | sub slurp_file { |
| 70 | my ($filename) = @_; |
| 71 | |
| 72 | local $/ = undef; |
| 73 | open my $fh, '<', $filename or die "Could not read $filename\n"; |
| 74 | my $content = <$fh>; |
| 75 | close $fh; |
| 76 | |
| 77 | return $content; |
| 78 | } |
| 79 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 80 | sub content_to_file { |
| 81 | my ($content, $filename) = @_; |
| 82 | |
| 83 | open my $fh, '>', $filename or die "Could not write to $filename\n"; |
| 84 | print $fh $content; |
| 85 | close $fh; |
| 86 | } |
| 87 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 88 | sub gen_app_guid { |
| 89 | my ($path) = @_; |
| 90 | |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 91 | my $guid = md5_hex( "mbedTLS:$path" ); |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 92 | $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/; |
| 93 | |
| 94 | return $guid; |
| 95 | } |
| 96 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 97 | sub gen_app { |
| 98 | my ($path, $template, $dir, $ext) = @_; |
| 99 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 100 | my $guid = gen_app_guid( $path ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 101 | $path =~ s!/!\\!g; |
| 102 | (my $appname = $path) =~ s/.*\\//; |
| 103 | |
Andres Amaya Garcia | bc432b8 | 2019-01-08 20:05:18 +0000 | [diff] [blame] | 104 | my $srcs = "\n <ClCompile Include=\"..\\..\\programs\\$path.c\" \/>\r"; |
Andres Amaya Garcia | c84a65d | 2018-10-30 21:46:21 +0000 | [diff] [blame] | 105 | if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or |
| 106 | $appname eq "query_compile_time_config" ) { |
Andres Amaya Garcia | bc432b8 | 2019-01-08 20:05:18 +0000 | [diff] [blame] | 107 | $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\query_config.c\" \/>\r"; |
Andres Amaya Garcia | c84a65d | 2018-10-30 21:46:21 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 110 | my $content = $template; |
Andres Amaya Garcia | c84a65d | 2018-10-30 21:46:21 +0000 | [diff] [blame] | 111 | $content =~ s/<SOURCES>/$srcs/g; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 112 | $content =~ s/<APPNAME>/$appname/g; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 113 | $content =~ s/<GUID>/$guid/g; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 114 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 115 | content_to_file( $content, "$dir/$appname.$ext" ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | sub get_app_list { |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 119 | my $app_list = `cd $programs_dir && make list`; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 120 | die "make list failed: $!\n" if $?; |
| 121 | |
| 122 | return split /\s+/, $app_list; |
| 123 | } |
| 124 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 125 | sub gen_app_files { |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 126 | my @app_list = @_; |
| 127 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 128 | my $vsx_tpl = slurp_file( $vsx_app_tpl_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 129 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 130 | for my $app ( @app_list ) { |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 131 | gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext ); |
| 132 | } |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 135 | sub gen_entry_list { |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 136 | my ($tpl, @names) = @_; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 137 | |
| 138 | my $entries; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 139 | for my $name (@names) { |
| 140 | (my $entry = $tpl) =~ s/{NAME}/$name/g; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 141 | $entries .= $entry; |
| 142 | } |
| 143 | |
| 144 | return $entries; |
| 145 | } |
| 146 | |
| 147 | sub gen_main_file { |
| 148 | my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_; |
| 149 | |
| 150 | my $header_entries = gen_entry_list( $hdr_tpl, @$headers ); |
| 151 | my $source_entries = gen_entry_list( $src_tpl, @$sources ); |
| 152 | |
| 153 | my $out = slurp_file( $main_tpl ); |
| 154 | $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m; |
| 155 | $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m; |
| 156 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 157 | content_to_file( $out, $main_out ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 160 | sub gen_vsx_solution { |
| 161 | my (@app_names) = @_; |
| 162 | |
| 163 | my ($app_entries, $conf_entries); |
| 164 | for my $path (@app_names) { |
| 165 | my $guid = gen_app_guid( $path ); |
| 166 | (my $appname = $path) =~ s!.*/!!; |
| 167 | |
| 168 | my $app_entry = $vsx_sln_app_entry_tpl; |
| 169 | $app_entry =~ s/{APPNAME}/$appname/g; |
| 170 | $app_entry =~ s/{GUID}/$guid/g; |
| 171 | |
| 172 | $app_entries .= $app_entry; |
| 173 | |
| 174 | my $conf_entry = $vsx_sln_conf_entry_tpl; |
| 175 | $conf_entry =~ s/{GUID}/$guid/g; |
| 176 | |
| 177 | $conf_entries .= $conf_entry; |
| 178 | } |
| 179 | |
| 180 | my $out = slurp_file( $vsx_sln_tpl_file ); |
| 181 | $out =~ s/APP_ENTRIES\r\n/$app_entries/m; |
| 182 | $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m; |
| 183 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 184 | content_to_file( $out, $vsx_sln_file ); |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Andres Amaya Garcia | 3c5f949 | 2018-01-11 19:51:27 +0000 | [diff] [blame] | 187 | sub del_vsx_files { |
| 188 | unlink glob "'$vsx_dir/*.$vsx_ext'"; |
| 189 | unlink $vsx_main_file; |
| 190 | unlink $vsx_sln_file; |
| 191 | } |
| 192 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 193 | sub main { |
| 194 | if( ! check_dirs() ) { |
| 195 | chdir '..' or die; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 196 | check_dirs or die "Must but run from mbedTLS root or scripts dir\n"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Andres Amaya Garcia | 3c5f949 | 2018-01-11 19:51:27 +0000 | [diff] [blame] | 199 | # Remove old files to ensure that, for example, project files from deleted |
| 200 | # apps are not kept |
| 201 | del_vsx_files(); |
| 202 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 203 | my @app_list = get_app_list(); |
| 204 | my @headers = <$header_dir/*.h>; |
Andrzej Kurek | 021dc3f | 2019-04-12 10:51:27 -0400 | [diff] [blame] | 205 | |
| 206 | my @sources = (); |
| 207 | if ($include_crypto) { |
| 208 | @sources = <$crypto_dir/$source_dir/*.c>; |
| 209 | foreach my $file (<$source_dir/*.c>) { |
| 210 | my $basename = $file; $basename =~ s!.*/!!; |
| 211 | push @sources, $file unless -e "$crypto_dir/$source_dir/$basename"; |
| 212 | } |
| 213 | } else { |
| 214 | @sources = <$source_dir/*.c>; |
| 215 | } |
| 216 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 217 | map { s!/!\\!g } @headers; |
| 218 | map { s!/!\\!g } @sources; |
| 219 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 220 | gen_app_files( @app_list ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 221 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 222 | gen_main_file( \@headers, \@sources, |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 223 | $vsx_hdr_tpl, $vsx_src_tpl, |
| 224 | $vsx_main_tpl_file, $vsx_main_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 225 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 226 | gen_vsx_solution( @app_list ); |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 227 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 228 | return 0; |
| 229 | } |