blob: 00a0c6322abafb08df64f986e2e6ae3d9278c6dd [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';
Gilles Peskine13fac982020-02-12 20:34:22 +010025
26my @thirdparty_header_dirs = qw(
27 3rdparty/everest/include/everest
28);
29my @thirdparty_source_dirs = qw(
30 3rdparty/everest/library
31 3rdparty/everest/library/kremlib
32 3rdparty/everest/library/legacy
33);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010034
Gilles Peskine7156d8c2020-02-19 20:08:44 +010035# Directories to add to the include path.
36# Order matters in case there are files with the same name in more than
37# one directory: the compiler will use the first match.
38my @include_directories = qw(
39 include
40 3rdparty/everest/include/
41 3rdparty/everest/include/everest
42 3rdparty/everest/include/everest/vs2010
43 3rdparty/everest/include/everest/kremlib
44);
45my $include_directories = join(';', map {"../../$_"} @include_directories);
46
Gilles Peskineb41f88f2020-02-12 20:45:18 +010047my @excluded_files = qw(
Gilles Peskine13fac982020-02-12 20:34:22 +010048 3rdparty/everest/library/Hacl_Curve25519.c
49);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010050my %excluded_files = ();
51foreach (@excluded_files) { $excluded_files{$_} = 1 }
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020052
53# Need windows line endings!
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020054my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020055 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020056EOT
57my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020058 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020059EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020060
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020061my $vsx_sln_app_entry_tpl = <<EOT;
62Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
63 ProjectSection(ProjectDependencies) = postProject\r
64 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
65 EndProjectSection\r
66EndProject\r
67EOT
68
69my $vsx_sln_conf_entry_tpl = <<EOT;
70 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
71 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
72 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
73 {GUID}.Debug|x64.Build.0 = Debug|x64\r
74 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
75 {GUID}.Release|Win32.Build.0 = Release|Win32\r
76 {GUID}.Release|x64.ActiveCfg = Release|x64\r
77 {GUID}.Release|x64.Build.0 = Release|x64\r
78EOT
79
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020080exit( main() );
81
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020082sub check_dirs {
Gilles Peskine13fac982020-02-12 20:34:22 +010083 foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) {
84 if (not (-d $d)) { return 0; }
85 }
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +020086 return -d $vsx_dir
Darryl Green588e8cb2018-07-20 13:27:58 +010087 && -d $mbedtls_header_dir
88 && -d $psa_header_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020089 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020090 && -d $programs_dir;
91}
92
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020093sub slurp_file {
94 my ($filename) = @_;
95
96 local $/ = undef;
97 open my $fh, '<', $filename or die "Could not read $filename\n";
98 my $content = <$fh>;
99 close $fh;
100
101 return $content;
102}
103
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200104sub content_to_file {
105 my ($content, $filename) = @_;
106
107 open my $fh, '>', $filename or die "Could not write to $filename\n";
108 print $fh $content;
109 close $fh;
110}
111
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200112sub gen_app_guid {
113 my ($path) = @_;
114
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000115 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200116 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
117
118 return $guid;
119}
120
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200121sub gen_app {
122 my ($path, $template, $dir, $ext) = @_;
123
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200124 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200125 $path =~ s!/!\\!g;
126 (my $appname = $path) =~ s/.*\\//;
127
Andres Amaya Garciabc432b82019-01-08 20:05:18 +0000128 my $srcs = "\n <ClCompile Include=\"..\\..\\programs\\$path.c\" \/>\r";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000129 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
130 $appname eq "query_compile_time_config" ) {
Jaeden Amero03c60de2019-02-28 11:37:23 +0000131 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>\r";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000132 }
133
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200134 my $content = $template;
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000135 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200136 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200137 $content =~ s/<GUID>/$guid/g;
Gilles Peskined362d0b2020-02-26 14:32:42 +0100138 $content =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200139
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200140 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200141}
142
143sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200144 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200145 die "make list failed: $!\n" if $?;
146
147 return split /\s+/, $app_list;
148}
149
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200150sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200151 my @app_list = @_;
152
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200153 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200154
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200155 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200156 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
157 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200158}
159
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200160sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200161 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200162
163 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200164 for my $name (@names) {
165 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200166 $entries .= $entry;
167 }
168
169 return $entries;
170}
171
172sub gen_main_file {
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100173 my ($headers, $sources,
174 $hdr_tpl, $src_tpl,
175 $main_tpl, $main_out) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200176
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100177 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200178 my $source_entries = gen_entry_list( $src_tpl, @$sources );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200179
180 my $out = slurp_file( $main_tpl );
181 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
182 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
Gilles Peskined362d0b2020-02-26 14:32:42 +0100183 $out =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200184
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200185 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200186}
187
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200188sub gen_vsx_solution {
189 my (@app_names) = @_;
190
191 my ($app_entries, $conf_entries);
192 for my $path (@app_names) {
193 my $guid = gen_app_guid( $path );
194 (my $appname = $path) =~ s!.*/!!;
195
196 my $app_entry = $vsx_sln_app_entry_tpl;
197 $app_entry =~ s/{APPNAME}/$appname/g;
198 $app_entry =~ s/{GUID}/$guid/g;
199
200 $app_entries .= $app_entry;
201
202 my $conf_entry = $vsx_sln_conf_entry_tpl;
203 $conf_entry =~ s/{GUID}/$guid/g;
204
205 $conf_entries .= $conf_entry;
206 }
207
208 my $out = slurp_file( $vsx_sln_tpl_file );
209 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
210 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
211
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200212 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200213}
214
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000215sub del_vsx_files {
216 unlink glob "'$vsx_dir/*.$vsx_ext'";
217 unlink $vsx_main_file;
218 unlink $vsx_sln_file;
219}
220
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200221sub main {
222 if( ! check_dirs() ) {
223 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000224 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200225 }
226
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000227 # Remove old files to ensure that, for example, project files from deleted
228 # apps are not kept
229 del_vsx_files();
230
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200231 my @app_list = get_app_list();
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100232 my @header_dirs = (
233 $mbedtls_header_dir,
234 $psa_header_dir,
235 $source_dir,
236 @thirdparty_header_dirs,
237 );
238 my @headers = (map { <$_/*.h> } @header_dirs);
239 my @source_dirs = (
240 $source_dir,
241 @thirdparty_source_dirs,
242 );
243 my @sources = (map { <$_/*.c> } @source_dirs);
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200244
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100245 @headers = grep { ! $excluded_files{$_} } @headers;
246 @sources = grep { ! $excluded_files{$_} } @sources;
247 map { s!/!\\!g } @headers;
248 map { s!/!\\!g } @sources;
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000249
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200250 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200251
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100252 gen_main_file( \@headers, \@sources,
253 $vsx_hdr_tpl, $vsx_src_tpl,
254 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200255
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200256 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200257
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200258 return 0;
259}