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