1#!/usr/bin/env perl 2 3# Generate main file, individual apps and solution files for 4# MS Visual Studio 2013 5# 6# Must be run from Mbed TLS root or scripts directory. 7# Takes no argument. 8# 9# Copyright The Mbed TLS Contributors 10# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 11 12use warnings; 13use strict; 14use Digest::MD5 'md5_hex'; 15 16my $vsx_dir = "visualc/VS2013"; 17my $vsx_ext = "vcxproj"; 18my $vsx_app_tpl_file = "scripts/data_files/vs2013-app-template.$vsx_ext"; 19my $vsx_main_tpl_file = "scripts/data_files/vs2013-main-template.$vsx_ext"; 20my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext"; 21my $vsx_sln_tpl_file = "scripts/data_files/vs2013-sln-template.sln"; 22my $vsx_sln_file = "$vsx_dir/mbedTLS.sln"; 23 24my $programs_dir = 'programs'; 25my $mbedtls_header_dir = 'include/mbedtls'; 26my $psa_header_dir = 'include/psa'; 27my $source_dir = 'library'; 28my $test_source_dir = 'tests/src'; 29my $test_header_dir = 'tests/include/test'; 30my $test_drivers_header_dir = 'tests/include/test/drivers'; 31my $test_drivers_source_dir = 'tests/src/drivers'; 32 33my @thirdparty_header_dirs = qw( 34 3rdparty/everest/include/everest 35); 36my @thirdparty_source_dirs = qw( 37 3rdparty/everest/library 38 3rdparty/everest/library/kremlib 39 3rdparty/everest/library/legacy 40); 41 42# Directories to add to the include path. 43# Order matters in case there are files with the same name in more than 44# one directory: the compiler will use the first match. 45my @include_directories = qw( 46 include 47 3rdparty/everest/include/ 48 3rdparty/everest/include/everest 49 3rdparty/everest/include/everest/vs2013 50 3rdparty/everest/include/everest/kremlib 51 tests/include 52); 53my $include_directories = join(';', map {"../../$_"} @include_directories); 54 55# Directories to add to the include path when building the library, but not 56# when building tests or applications. 57my @library_include_directories = qw( 58 library 59); 60my $library_include_directories = 61 join(';', map {"../../$_"} (@library_include_directories, 62 @include_directories)); 63 64my @excluded_files = qw( 65 3rdparty/everest/library/Hacl_Curve25519.c 66); 67my %excluded_files = (); 68foreach (@excluded_files) { $excluded_files{$_} = 1 } 69 70my $vsx_hdr_tpl = <<EOT; 71 <ClInclude Include="..\\..\\{NAME}" /> 72EOT 73my $vsx_src_tpl = <<EOT; 74 <ClCompile Include="..\\..\\{NAME}" /> 75EOT 76 77my $vsx_sln_app_entry_tpl = <<EOT; 78Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}" 79 ProjectSection(ProjectDependencies) = postProject 80 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554} 81 EndProjectSection 82EndProject 83EOT 84 85my $vsx_sln_conf_entry_tpl = <<EOT; 86 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32 87 {GUID}.Debug|Win32.Build.0 = Debug|Win32 88 {GUID}.Debug|x64.ActiveCfg = Debug|x64 89 {GUID}.Debug|x64.Build.0 = Debug|x64 90 {GUID}.Release|Win32.ActiveCfg = Release|Win32 91 {GUID}.Release|Win32.Build.0 = Release|Win32 92 {GUID}.Release|x64.ActiveCfg = Release|x64 93 {GUID}.Release|x64.Build.0 = Release|x64 94EOT 95 96exit( main() ); 97 98sub check_dirs { 99 foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) { 100 if (not (-d $d)) { return 0; } 101 } 102 return -d $vsx_dir 103 && -d $mbedtls_header_dir 104 && -d $psa_header_dir 105 && -d $source_dir 106 && -d $test_source_dir 107 && -d $test_drivers_source_dir 108 && -d $test_header_dir 109 && -d $test_drivers_header_dir 110 && -d $programs_dir; 111} 112 113sub slurp_file { 114 my ($filename) = @_; 115 116 local $/ = undef; 117 open my $fh, '<:crlf', $filename or die "Could not read $filename\n"; 118 my $content = <$fh>; 119 close $fh; 120 121 return $content; 122} 123 124sub content_to_file { 125 my ($content, $filename) = @_; 126 127 open my $fh, '>:crlf', $filename or die "Could not write to $filename\n"; 128 print $fh $content; 129 close $fh; 130} 131 132sub gen_app_guid { 133 my ($path) = @_; 134 135 my $guid = md5_hex( "mbedTLS:$path" ); 136 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/; 137 138 return $guid; 139} 140 141sub gen_app { 142 my ($path, $template, $dir, $ext) = @_; 143 144 my $guid = gen_app_guid( $path ); 145 $path =~ s!/!\\!g; 146 (my $appname = $path) =~ s/.*\\//; 147 148 my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>"; 149 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or 150 $appname eq "query_compile_time_config" ) { 151 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>"; 152 } 153 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" ) { 154 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\ssl_test_lib.c\" \/>"; 155 } 156 157 my $content = $template; 158 $content =~ s/<SOURCES>/$srcs/g; 159 $content =~ s/<APPNAME>/$appname/g; 160 $content =~ s/<GUID>/$guid/g; 161 $content =~ s/INCLUDE_DIRECTORIES\n/$include_directories/g; 162 163 content_to_file( $content, "$dir/$appname.$ext" ); 164} 165 166sub get_app_list { 167 my $makefile_contents = slurp_file('programs/Makefile'); 168 $makefile_contents =~ /\n\s*APPS\s*=[\\\s]*(.*?)(?<!\\)[\#\n]/s 169 or die "Cannot find APPS = ... in programs/Makefile\n"; 170 return split /(?:\s|\\)+/, $1; 171} 172 173sub gen_app_files { 174 my @app_list = @_; 175 176 my $vsx_tpl = slurp_file( $vsx_app_tpl_file ); 177 178 for my $app ( @app_list ) { 179 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext ); 180 } 181} 182 183sub gen_entry_list { 184 my ($tpl, @names) = @_; 185 186 my $entries; 187 for my $name (@names) { 188 (my $entry = $tpl) =~ s/{NAME}/$name/g; 189 $entries .= $entry; 190 } 191 192 return $entries; 193} 194 195sub gen_main_file { 196 my ($headers, $sources, 197 $hdr_tpl, $src_tpl, 198 $main_tpl, $main_out) = @_; 199 200 my $header_entries = gen_entry_list( $hdr_tpl, @$headers ); 201 my $source_entries = gen_entry_list( $src_tpl, @$sources ); 202 203 my $out = slurp_file( $main_tpl ); 204 $out =~ s/SOURCE_ENTRIES\n/$source_entries/m; 205 $out =~ s/HEADER_ENTRIES\n/$header_entries/m; 206 $out =~ s/INCLUDE_DIRECTORIES\n/$library_include_directories/g; 207 208 content_to_file( $out, $main_out ); 209} 210 211sub gen_vsx_solution { 212 my (@app_names) = @_; 213 214 my ($app_entries, $conf_entries); 215 for my $path (@app_names) { 216 my $guid = gen_app_guid( $path ); 217 (my $appname = $path) =~ s!.*/!!; 218 219 my $app_entry = $vsx_sln_app_entry_tpl; 220 $app_entry =~ s/{APPNAME}/$appname/g; 221 $app_entry =~ s/{GUID}/$guid/g; 222 223 $app_entries .= $app_entry; 224 225 my $conf_entry = $vsx_sln_conf_entry_tpl; 226 $conf_entry =~ s/{GUID}/$guid/g; 227 228 $conf_entries .= $conf_entry; 229 } 230 231 my $out = slurp_file( $vsx_sln_tpl_file ); 232 $out =~ s/APP_ENTRIES\n/$app_entries/m; 233 $out =~ s/CONF_ENTRIES\n/$conf_entries/m; 234 235 content_to_file( $out, $vsx_sln_file ); 236} 237 238sub del_vsx_files { 239 unlink glob "'$vsx_dir/*.$vsx_ext'"; 240 unlink $vsx_main_file; 241 unlink $vsx_sln_file; 242} 243 244sub main { 245 if( ! check_dirs() ) { 246 chdir '..' or die; 247 check_dirs or die "Must be run from Mbed TLS root or scripts dir\n"; 248 } 249 250 # Remove old files to ensure that, for example, project files from deleted 251 # apps are not kept 252 del_vsx_files(); 253 254 my @app_list = get_app_list(); 255 my @header_dirs = ( 256 $mbedtls_header_dir, 257 $psa_header_dir, 258 $test_header_dir, 259 $test_drivers_header_dir, 260 $source_dir, 261 @thirdparty_header_dirs, 262 ); 263 my @headers = (map { <$_/*.h> } @header_dirs); 264 my @source_dirs = ( 265 $source_dir, 266 $test_source_dir, 267 $test_drivers_source_dir, 268 @thirdparty_source_dirs, 269 ); 270 my @sources = (map { <$_/*.c> } @source_dirs); 271 272 @headers = grep { ! $excluded_files{$_} } @headers; 273 @sources = grep { ! $excluded_files{$_} } @sources; 274 map { s!/!\\!g } @headers; 275 map { s!/!\\!g } @sources; 276 277 gen_app_files( @app_list ); 278 279 gen_main_file( \@headers, \@sources, 280 $vsx_hdr_tpl, $vsx_src_tpl, 281 $vsx_main_tpl_file, $vsx_main_file ); 282 283 gen_vsx_solution( @app_list ); 284 285 return 0; 286} 287