blob: 5d07b885c54ca8ac50620d8b4a8ff560e2ba24b3 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02002
Andres AG5121d4b2018-04-11 20:35:19 -05003# Generate main file, individual apps and solution files for MS Visual Studio
4# 2010
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02005#
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +00006# Must be run from mbedTLS root or scripts directory.
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02007# Takes no argument.
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +02008
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02009use warnings;
10use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020011use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020012
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020013my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020014my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020015my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020016my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000017my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020018my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000019my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020020
21my $programs_dir = 'programs';
Darryl Green588e8cb2018-07-20 13:27:58 +010022my $mbedtls_header_dir = 'include/mbedtls';
23my $psa_header_dir = 'include/psa';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020024my $source_dir = 'library';
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +000025my $everest_header_dir = '3rdparty/everest/include/everest';
26my @everest_source_dirs = ('3rdparty/everest/library', '3rdparty/everest/library/kremlib', '3rdparty/everest/library/vs2010');
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020027
28# Need windows line endings!
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020029my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020030 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020031EOT
32my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020033 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020034EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020035
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020036my $vsx_sln_app_entry_tpl = <<EOT;
37Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
38 ProjectSection(ProjectDependencies) = postProject\r
39 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
40 EndProjectSection\r
41EndProject\r
42EOT
43
44my $vsx_sln_conf_entry_tpl = <<EOT;
45 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
46 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
47 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
48 {GUID}.Debug|x64.Build.0 = Debug|x64\r
49 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
50 {GUID}.Release|Win32.Build.0 = Release|Win32\r
51 {GUID}.Release|x64.ActiveCfg = Release|x64\r
52 {GUID}.Release|x64.Build.0 = Release|x64\r
53EOT
54
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020055exit( main() );
56
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020057sub check_dirs {
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +000058 foreach my $d (@everest_source_dirs) { if (not (-d $d)) { return 0; } }
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +020059 return -d $vsx_dir
Darryl Green588e8cb2018-07-20 13:27:58 +010060 && -d $mbedtls_header_dir
61 && -d $psa_header_dir
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +000062 && -d $everest_header_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020063 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020064 && -d $programs_dir;
65}
66
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020067sub slurp_file {
68 my ($filename) = @_;
69
70 local $/ = undef;
71 open my $fh, '<', $filename or die "Could not read $filename\n";
72 my $content = <$fh>;
73 close $fh;
74
75 return $content;
76}
77
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +020078sub content_to_file {
79 my ($content, $filename) = @_;
80
81 open my $fh, '>', $filename or die "Could not write to $filename\n";
82 print $fh $content;
83 close $fh;
84}
85
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020086sub gen_app_guid {
87 my ($path) = @_;
88
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000089 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020090 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
91
92 return $guid;
93}
94
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020095sub gen_app {
96 my ($path, $template, $dir, $ext) = @_;
97
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020098 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020099 $path =~ s!/!\\!g;
100 (my $appname = $path) =~ s/.*\\//;
101
Andres Amaya Garciabc432b82019-01-08 20:05:18 +0000102 my $srcs = "\n <ClCompile Include=\"..\\..\\programs\\$path.c\" \/>\r";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000103 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
104 $appname eq "query_compile_time_config" ) {
Jaeden Amero03c60de2019-02-28 11:37:23 +0000105 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>\r";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000106 }
107
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200108 my $content = $template;
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000109 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200110 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200111 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200112
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200113 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200114}
115
116sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200117 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200118 die "make list failed: $!\n" if $?;
119
120 return split /\s+/, $app_list;
121}
122
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200123sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200124 my @app_list = @_;
125
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200126 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200127
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200128 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200129 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
130 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200131}
132
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200133sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200134 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200135
136 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200137 for my $name (@names) {
138 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200139 $entries .= $entry;
140 }
141
142 return $entries;
143}
144
145sub gen_main_file {
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000146 my ($mbedtls_headers, $psa_headers, $source_headers, $everest_headers, $sources, $everest_sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200147
Darryl Green588e8cb2018-07-20 13:27:58 +0100148 my $header_entries = gen_entry_list( $hdr_tpl, @$mbedtls_headers );
149 $header_entries .= gen_entry_list( $hdr_tpl, @$psa_headers );
Darryl Greend9eee3b2018-11-02 10:45:36 +0000150 $header_entries .= gen_entry_list( $hdr_tpl, @$source_headers );
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000151 $header_entries .= gen_entry_list( $hdr_tpl, @$everest_headers );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200152 my $source_entries = gen_entry_list( $src_tpl, @$sources );
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000153 $source_entries .= gen_entry_list( $src_tpl, @$everest_sources );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200154
155 my $out = slurp_file( $main_tpl );
156 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
157 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
158
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200159 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200160}
161
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200162sub gen_vsx_solution {
163 my (@app_names) = @_;
164
165 my ($app_entries, $conf_entries);
166 for my $path (@app_names) {
167 my $guid = gen_app_guid( $path );
168 (my $appname = $path) =~ s!.*/!!;
169
170 my $app_entry = $vsx_sln_app_entry_tpl;
171 $app_entry =~ s/{APPNAME}/$appname/g;
172 $app_entry =~ s/{GUID}/$guid/g;
173
174 $app_entries .= $app_entry;
175
176 my $conf_entry = $vsx_sln_conf_entry_tpl;
177 $conf_entry =~ s/{GUID}/$guid/g;
178
179 $conf_entries .= $conf_entry;
180 }
181
182 my $out = slurp_file( $vsx_sln_tpl_file );
183 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
184 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
185
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200186 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200187}
188
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000189sub del_vsx_files {
190 unlink glob "'$vsx_dir/*.$vsx_ext'";
191 unlink $vsx_main_file;
192 unlink $vsx_sln_file;
193}
194
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200195sub main {
196 if( ! check_dirs() ) {
197 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000198 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200199 }
200
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000201 # Remove old files to ensure that, for example, project files from deleted
202 # apps are not kept
203 del_vsx_files();
204
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200205 my @app_list = get_app_list();
Darryl Green588e8cb2018-07-20 13:27:58 +0100206 my @mbedtls_headers = <$mbedtls_header_dir/*.h>;
207 my @psa_headers = <$psa_header_dir/*.h>;
Darryl Greend9eee3b2018-11-02 10:45:36 +0000208 my @source_headers = <$source_dir/*.h>;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200209 my @sources = <$source_dir/*.c>;
Darryl Green588e8cb2018-07-20 13:27:58 +0100210 map { s!/!\\!g } @mbedtls_headers;
211 map { s!/!\\!g } @psa_headers;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200212 map { s!/!\\!g } @sources;
213
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000214 my @everest_headers = <$everest_header_dir/*.h>;
215 my @everest_sources = ();
216 foreach my $d (@everest_source_dirs) { push @everest_sources, <$d/*.c>; }
217 @everest_sources = grep !/3rdparty\/everest\/library\/Hacl_Curve25519.c/, @everest_sources;
218 map { s!/!\\!g } @everest_headers;
219 map { s!/!\\!g } @everest_sources;
220
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200221 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200222
Darryl Greend9eee3b2018-11-02 10:45:36 +0000223 gen_main_file( \@mbedtls_headers, \@psa_headers, \@source_headers,
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000224 \@everest_headers, \@sources, \@everest_sources, $vsx_hdr_tpl,
225 $vsx_src_tpl, $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200226
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200227 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200228
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200229 return 0;
230}