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. |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 7 | # Takes no argument. |
Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame^] | 8 | # |
| 9 | # Copyright (C) 2013-2020, Arm Limited, All Rights Reserved |
| 10 | # |
| 11 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 684e9dc | 2013-09-20 15:11:44 +0200 | [diff] [blame] | 12 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 13 | use warnings; |
| 14 | use strict; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 15 | use Digest::MD5 'md5_hex'; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 16 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 17 | my $vsx_dir = "visualc/VS2010"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 18 | my $vsx_ext = "vcxproj"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 19 | 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] | 20 | 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] | 21 | my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext"; |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 22 | 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] | 23 | my $vsx_sln_file = "$vsx_dir/mbedTLS.sln"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 24 | |
| 25 | my $programs_dir = 'programs'; |
Darryl Green | 588e8cb | 2018-07-20 13:27:58 +0100 | [diff] [blame] | 26 | my $mbedtls_header_dir = 'include/mbedtls'; |
| 27 | my $psa_header_dir = 'include/psa'; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 28 | my $source_dir = 'library'; |
Gilles Peskine | 13fac98 | 2020-02-12 20:34:22 +0100 | [diff] [blame] | 29 | |
| 30 | my @thirdparty_header_dirs = qw( |
| 31 | 3rdparty/everest/include/everest |
| 32 | ); |
| 33 | my @thirdparty_source_dirs = qw( |
| 34 | 3rdparty/everest/library |
| 35 | 3rdparty/everest/library/kremlib |
| 36 | 3rdparty/everest/library/legacy |
| 37 | ); |
Gilles Peskine | b41f88f | 2020-02-12 20:45:18 +0100 | [diff] [blame] | 38 | |
Gilles Peskine | 7156d8c | 2020-02-19 20:08:44 +0100 | [diff] [blame] | 39 | # Directories to add to the include path. |
| 40 | # Order matters in case there are files with the same name in more than |
| 41 | # one directory: the compiler will use the first match. |
| 42 | my @include_directories = qw( |
| 43 | include |
| 44 | 3rdparty/everest/include/ |
| 45 | 3rdparty/everest/include/everest |
| 46 | 3rdparty/everest/include/everest/vs2010 |
| 47 | 3rdparty/everest/include/everest/kremlib |
| 48 | ); |
| 49 | my $include_directories = join(';', map {"../../$_"} @include_directories); |
| 50 | |
Gilles Peskine | b41f88f | 2020-02-12 20:45:18 +0100 | [diff] [blame] | 51 | my @excluded_files = qw( |
Gilles Peskine | 13fac98 | 2020-02-12 20:34:22 +0100 | [diff] [blame] | 52 | 3rdparty/everest/library/Hacl_Curve25519.c |
| 53 | ); |
Gilles Peskine | b41f88f | 2020-02-12 20:45:18 +0100 | [diff] [blame] | 54 | my %excluded_files = (); |
| 55 | foreach (@excluded_files) { $excluded_files{$_} = 1 } |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 56 | |
| 57 | # Need windows line endings! |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 58 | my $vsx_hdr_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 59 | <ClInclude Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 60 | EOT |
| 61 | my $vsx_src_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 62 | <ClCompile Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 63 | EOT |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 64 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 65 | my $vsx_sln_app_entry_tpl = <<EOT; |
| 66 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r |
| 67 | ProjectSection(ProjectDependencies) = postProject\r |
| 68 | {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r |
| 69 | EndProjectSection\r |
| 70 | EndProject\r |
| 71 | EOT |
| 72 | |
| 73 | my $vsx_sln_conf_entry_tpl = <<EOT; |
| 74 | {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r |
| 75 | {GUID}.Debug|Win32.Build.0 = Debug|Win32\r |
| 76 | {GUID}.Debug|x64.ActiveCfg = Debug|x64\r |
| 77 | {GUID}.Debug|x64.Build.0 = Debug|x64\r |
| 78 | {GUID}.Release|Win32.ActiveCfg = Release|Win32\r |
| 79 | {GUID}.Release|Win32.Build.0 = Release|Win32\r |
| 80 | {GUID}.Release|x64.ActiveCfg = Release|x64\r |
| 81 | {GUID}.Release|x64.Build.0 = Release|x64\r |
| 82 | EOT |
| 83 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 84 | exit( main() ); |
| 85 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 86 | sub check_dirs { |
Gilles Peskine | 13fac98 | 2020-02-12 20:34:22 +0100 | [diff] [blame] | 87 | foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) { |
| 88 | if (not (-d $d)) { return 0; } |
| 89 | } |
Manuel Pégourié-Gonnard | 51f14be | 2015-05-14 13:04:03 +0200 | [diff] [blame] | 90 | return -d $vsx_dir |
Darryl Green | 588e8cb | 2018-07-20 13:27:58 +0100 | [diff] [blame] | 91 | && -d $mbedtls_header_dir |
| 92 | && -d $psa_header_dir |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 93 | && -d $source_dir |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 94 | && -d $programs_dir; |
| 95 | } |
| 96 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 97 | sub slurp_file { |
| 98 | my ($filename) = @_; |
| 99 | |
| 100 | local $/ = undef; |
| 101 | open my $fh, '<', $filename or die "Could not read $filename\n"; |
| 102 | my $content = <$fh>; |
| 103 | close $fh; |
| 104 | |
| 105 | return $content; |
| 106 | } |
| 107 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 108 | sub content_to_file { |
| 109 | my ($content, $filename) = @_; |
| 110 | |
| 111 | open my $fh, '>', $filename or die "Could not write to $filename\n"; |
| 112 | print $fh $content; |
| 113 | close $fh; |
| 114 | } |
| 115 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 116 | sub gen_app_guid { |
| 117 | my ($path) = @_; |
| 118 | |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 119 | my $guid = md5_hex( "mbedTLS:$path" ); |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 120 | $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/; |
| 121 | |
| 122 | return $guid; |
| 123 | } |
| 124 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 125 | sub gen_app { |
| 126 | my ($path, $template, $dir, $ext) = @_; |
| 127 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 128 | my $guid = gen_app_guid( $path ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 129 | $path =~ s!/!\\!g; |
| 130 | (my $appname = $path) =~ s/.*\\//; |
| 131 | |
Gilles Peskine | 0f3f9c3 | 2020-04-01 13:34:50 +0200 | [diff] [blame] | 132 | my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>"; |
Andres Amaya Garcia | c84a65d | 2018-10-30 21:46:21 +0000 | [diff] [blame] | 133 | if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or |
| 134 | $appname eq "query_compile_time_config" ) { |
Gilles Peskine | 0f3f9c3 | 2020-04-01 13:34:50 +0200 | [diff] [blame] | 135 | $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>"; |
Andres Amaya Garcia | c84a65d | 2018-10-30 21:46:21 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 138 | my $content = $template; |
Andres Amaya Garcia | c84a65d | 2018-10-30 21:46:21 +0000 | [diff] [blame] | 139 | $content =~ s/<SOURCES>/$srcs/g; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 140 | $content =~ s/<APPNAME>/$appname/g; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 141 | $content =~ s/<GUID>/$guid/g; |
Gilles Peskine | d362d0b | 2020-02-26 14:32:42 +0100 | [diff] [blame] | 142 | $content =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 143 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 144 | content_to_file( $content, "$dir/$appname.$ext" ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | sub get_app_list { |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 148 | my $app_list = `cd $programs_dir && make list`; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 149 | die "make list failed: $!\n" if $?; |
| 150 | |
| 151 | return split /\s+/, $app_list; |
| 152 | } |
| 153 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 154 | sub gen_app_files { |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 155 | my @app_list = @_; |
| 156 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 157 | my $vsx_tpl = slurp_file( $vsx_app_tpl_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 158 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 159 | for my $app ( @app_list ) { |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 160 | gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext ); |
| 161 | } |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 164 | sub gen_entry_list { |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 165 | my ($tpl, @names) = @_; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 166 | |
| 167 | my $entries; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 168 | for my $name (@names) { |
| 169 | (my $entry = $tpl) =~ s/{NAME}/$name/g; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 170 | $entries .= $entry; |
| 171 | } |
| 172 | |
| 173 | return $entries; |
| 174 | } |
| 175 | |
| 176 | sub gen_main_file { |
Gilles Peskine | b41f88f | 2020-02-12 20:45:18 +0100 | [diff] [blame] | 177 | my ($headers, $sources, |
| 178 | $hdr_tpl, $src_tpl, |
| 179 | $main_tpl, $main_out) = @_; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 180 | |
Gilles Peskine | b41f88f | 2020-02-12 20:45:18 +0100 | [diff] [blame] | 181 | my $header_entries = gen_entry_list( $hdr_tpl, @$headers ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 182 | my $source_entries = gen_entry_list( $src_tpl, @$sources ); |
| 183 | |
| 184 | my $out = slurp_file( $main_tpl ); |
| 185 | $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m; |
| 186 | $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m; |
Gilles Peskine | d362d0b | 2020-02-26 14:32:42 +0100 | [diff] [blame] | 187 | $out =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 188 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 189 | content_to_file( $out, $main_out ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 192 | sub gen_vsx_solution { |
| 193 | my (@app_names) = @_; |
| 194 | |
| 195 | my ($app_entries, $conf_entries); |
| 196 | for my $path (@app_names) { |
| 197 | my $guid = gen_app_guid( $path ); |
| 198 | (my $appname = $path) =~ s!.*/!!; |
| 199 | |
| 200 | my $app_entry = $vsx_sln_app_entry_tpl; |
| 201 | $app_entry =~ s/{APPNAME}/$appname/g; |
| 202 | $app_entry =~ s/{GUID}/$guid/g; |
| 203 | |
| 204 | $app_entries .= $app_entry; |
| 205 | |
| 206 | my $conf_entry = $vsx_sln_conf_entry_tpl; |
| 207 | $conf_entry =~ s/{GUID}/$guid/g; |
| 208 | |
| 209 | $conf_entries .= $conf_entry; |
| 210 | } |
| 211 | |
| 212 | my $out = slurp_file( $vsx_sln_tpl_file ); |
| 213 | $out =~ s/APP_ENTRIES\r\n/$app_entries/m; |
| 214 | $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m; |
| 215 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 216 | content_to_file( $out, $vsx_sln_file ); |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Andres Amaya Garcia | 3c5f949 | 2018-01-11 19:51:27 +0000 | [diff] [blame] | 219 | sub del_vsx_files { |
| 220 | unlink glob "'$vsx_dir/*.$vsx_ext'"; |
| 221 | unlink $vsx_main_file; |
| 222 | unlink $vsx_sln_file; |
| 223 | } |
| 224 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 225 | sub main { |
| 226 | if( ! check_dirs() ) { |
| 227 | chdir '..' or die; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 228 | 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] | 229 | } |
| 230 | |
Andres Amaya Garcia | 3c5f949 | 2018-01-11 19:51:27 +0000 | [diff] [blame] | 231 | # Remove old files to ensure that, for example, project files from deleted |
| 232 | # apps are not kept |
| 233 | del_vsx_files(); |
| 234 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 235 | my @app_list = get_app_list(); |
Gilles Peskine | b41f88f | 2020-02-12 20:45:18 +0100 | [diff] [blame] | 236 | my @header_dirs = ( |
| 237 | $mbedtls_header_dir, |
| 238 | $psa_header_dir, |
| 239 | $source_dir, |
| 240 | @thirdparty_header_dirs, |
| 241 | ); |
| 242 | my @headers = (map { <$_/*.h> } @header_dirs); |
| 243 | my @source_dirs = ( |
| 244 | $source_dir, |
| 245 | @thirdparty_source_dirs, |
| 246 | ); |
| 247 | my @sources = (map { <$_/*.c> } @source_dirs); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 248 | |
Gilles Peskine | b41f88f | 2020-02-12 20:45:18 +0100 | [diff] [blame] | 249 | @headers = grep { ! $excluded_files{$_} } @headers; |
| 250 | @sources = grep { ! $excluded_files{$_} } @sources; |
| 251 | map { s!/!\\!g } @headers; |
| 252 | map { s!/!\\!g } @sources; |
Christoph M. Wintersteiger | 48d26c2 | 2018-12-06 18:59:19 +0000 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 254 | gen_app_files( @app_list ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 255 | |
Gilles Peskine | b41f88f | 2020-02-12 20:45:18 +0100 | [diff] [blame] | 256 | gen_main_file( \@headers, \@sources, |
| 257 | $vsx_hdr_tpl, $vsx_src_tpl, |
| 258 | $vsx_main_tpl_file, $vsx_main_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 259 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 260 | gen_vsx_solution( @app_list ); |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 261 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 262 | return 0; |
| 263 | } |