3 # Generate main file, individual apps and solution files for MS Visual Studio
6 # Must be run from mbedTLS root or scripts directory.
7 # Takes "include_crypto" as an argument that can be either 0 (don't include) or
8 # 1 (include). On by default.
12 use Digest::MD5 'md5_hex';
14 my $vsx_dir = "visualc/VS2010";
15 my $vsx_ext = "vcxproj";
16 my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
17 my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
18 my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
19 my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
20 my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
22 my $include_crypto = 1;
24 die "Invalid number of arguments" if scalar @ARGV != 1;
25 ($include_crypto) = @ARGV;
28 my $programs_dir = 'programs';
29 my $header_dir = 'include/mbedtls';
30 my $crypto_headers_dir = 'include/psa';
31 my $source_dir = 'library';
32 my $crypto_dir = 'crypto';
34 # Need windows line endings!
35 my $include_directories = <<EOT;
39 if ($include_crypto) {
40 $include_directories = <<EOT;
41 ../../include;../../crypto/include\r
45 my $vsx_hdr_tpl = <<EOT;
46 <ClInclude Include="..\\..\\{NAME}" />\r
48 my $vsx_src_tpl = <<EOT;
49 <ClCompile Include="..\\..\\{NAME}" />\r
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
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
79 && -d "$crypto_dir/$crypto_headers_dir";
86 open my $fh, '<', $filename or die "Could not read $filename\n";
94 my ($content, $filename) = @_;
96 open my $fh, '>', $filename or die "Could not write to $filename\n";
104 my $guid = md5_hex( "mbedTLS:$path" );
105 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
111 my ($path, $template, $dir, $ext) = @_;
113 my $guid = gen_app_guid( $path );
115 (my $appname = $path) =~ s/.*\\//;
117 my $srcs = "\n <ClCompile Include=\"..\\..\\programs\\$path.c\" \/>\r";
118 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
119 $appname eq "query_compile_time_config" ) {
120 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\query_config.c\" \/>\r";
123 my $content = $template;
124 $content =~ s/<SOURCES>/$srcs/g;
125 $content =~ s/<APPNAME>/$appname/g;
126 $content =~ s/<GUID>/$guid/g;
127 $content =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
129 content_to_file( $content, "$dir/$appname.$ext" );
133 my $app_list = `cd $programs_dir && make list`;
134 die "make list failed: $!\n" if $?;
136 return split /\s+/, $app_list;
142 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
144 for my $app ( @app_list ) {
145 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
150 my ($tpl, @names) = @_;
153 for my $name (@names) {
154 (my $entry = $tpl) =~ s/{NAME}/$name/g;
162 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
164 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
165 my $source_entries = gen_entry_list( $src_tpl, @$sources );
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;
170 $out =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
172 content_to_file( $out, $main_out );
175 sub gen_vsx_solution {
176 my (@app_names) = @_;
178 my ($app_entries, $conf_entries);
179 for my $path (@app_names) {
180 my $guid = gen_app_guid( $path );
181 (my $appname = $path) =~ s!.*/!!;
183 my $app_entry = $vsx_sln_app_entry_tpl;
184 $app_entry =~ s/{APPNAME}/$appname/g;
185 $app_entry =~ s/{GUID}/$guid/g;
187 $app_entries .= $app_entry;
189 my $conf_entry = $vsx_sln_conf_entry_tpl;
190 $conf_entry =~ s/{GUID}/$guid/g;
192 $conf_entries .= $conf_entry;
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;
199 content_to_file( $out, $vsx_sln_file );
203 unlink glob "'$vsx_dir/*.$vsx_ext'";
204 unlink $vsx_main_file;
205 unlink $vsx_sln_file;
209 if( ! check_dirs() ) {
211 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
214 # Remove old files to ensure that, for example, project files from deleted
218 my @app_list = get_app_list();
219 my @headers = <$header_dir/*.h>;
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";
228 push @headers, <$crypto_dir/$crypto_headers_dir/*.h>;
230 @sources = <$source_dir/*.c>;
233 map { s!/!\\!g } @headers;
234 map { s!/!\\!g } @sources;
236 gen_app_files( @app_list );
238 gen_main_file( \@headers, \@sources,
239 $vsx_hdr_tpl, $vsx_src_tpl,
240 $vsx_main_tpl_file, $vsx_main_file );
242 gen_vsx_solution( @app_list );