blob: b27f795fcace0131afba544d4a3da6291e10fdd6 [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.
Bence Szépkúti700ee442020-05-26 00:33:31 +02008#
Bence Szépkúti1e148272020-08-07 13:07:28 +02009# Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000010# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +020011
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020012use warnings;
13use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020014use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020015
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020016my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020017my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020018my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020019my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000020my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020021my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000022my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020023
24my $programs_dir = 'programs';
Darryl Green588e8cb2018-07-20 13:27:58 +010025my $mbedtls_header_dir = 'include/mbedtls';
26my $psa_header_dir = 'include/psa';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020027my $source_dir = 'library';
Ronald Cron8dc0af22020-06-05 16:00:22 +020028my $test_source_dir = 'tests/src';
29my $test_header_dir = 'tests/include/test';
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020030my $test_drivers_header_dir = 'tests/include/test/drivers';
Steven Cooreman16141ed2021-04-08 10:58:33 +020031my $test_drivers_source_dir = 'tests/src/drivers';
Gilles Peskine13fac982020-02-12 20:34:22 +010032
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);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010041
Gilles Peskine7156d8c2020-02-19 20:08:44 +010042# 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/vs2010
50 3rdparty/everest/include/everest/kremlib
Ronald Cron8dc0af22020-06-05 16:00:22 +020051 tests/include
Gilles Peskine7156d8c2020-02-19 20:08:44 +010052);
53my $include_directories = join(';', map {"../../$_"} @include_directories);
54
Gilles Peskine66c3dc42020-06-03 02:25:17 +020055# 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
Gilles Peskineb41f88f2020-02-12 20:45:18 +010064my @excluded_files = qw(
Gilles Peskine13fac982020-02-12 20:34:22 +010065 3rdparty/everest/library/Hacl_Curve25519.c
66);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010067my %excluded_files = ();
68foreach (@excluded_files) { $excluded_files{$_} = 1 }
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020069
70# Need windows line endings!
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020071my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020072 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020073EOT
74my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020075 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020076EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020077
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020078my $vsx_sln_app_entry_tpl = <<EOT;
79Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
80 ProjectSection(ProjectDependencies) = postProject\r
81 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
82 EndProjectSection\r
83EndProject\r
84EOT
85
86my $vsx_sln_conf_entry_tpl = <<EOT;
87 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
88 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
89 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
90 {GUID}.Debug|x64.Build.0 = Debug|x64\r
91 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
92 {GUID}.Release|Win32.Build.0 = Release|Win32\r
93 {GUID}.Release|x64.ActiveCfg = Release|x64\r
94 {GUID}.Release|x64.Build.0 = Release|x64\r
95EOT
96
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020097exit( main() );
98
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020099sub check_dirs {
Gilles Peskine13fac982020-02-12 20:34:22 +0100100 foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) {
101 if (not (-d $d)) { return 0; }
102 }
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +0200103 return -d $vsx_dir
Darryl Green588e8cb2018-07-20 13:27:58 +0100104 && -d $mbedtls_header_dir
105 && -d $psa_header_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200106 && -d $source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200107 && -d $test_source_dir
Steven Cooreman16141ed2021-04-08 10:58:33 +0200108 && -d $test_drivers_source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200109 && -d $test_header_dir
Steven Cooremana70d5882020-07-16 20:26:18 +0200110 && -d $test_drivers_header_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200111 && -d $programs_dir;
112}
113
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200114sub slurp_file {
115 my ($filename) = @_;
116
117 local $/ = undef;
118 open my $fh, '<', $filename or die "Could not read $filename\n";
119 my $content = <$fh>;
120 close $fh;
121
122 return $content;
123}
124
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200125sub content_to_file {
126 my ($content, $filename) = @_;
127
128 open my $fh, '>', $filename or die "Could not write to $filename\n";
129 print $fh $content;
130 close $fh;
131}
132
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200133sub gen_app_guid {
134 my ($path) = @_;
135
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000136 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200137 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
138
139 return $guid;
140}
141
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200142sub gen_app {
143 my ($path, $template, $dir, $ext) = @_;
144
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200145 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200146 $path =~ s!/!\\!g;
147 (my $appname = $path) =~ s/.*\\//;
Gilles Peskine2c04f572023-11-03 19:16:56 +0100148 my $is_test_app = ($path =~ m/^test\\/);
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200149
Gilles Peskine0f3f9c32020-04-01 13:34:50 +0200150 my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000151 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
152 $appname eq "query_compile_time_config" ) {
Gilles Peskine0f3f9c32020-04-01 13:34:50 +0200153 $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000154 }
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100155 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" ) {
156 $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\ssl\\ssl_test_lib.c\" \/>";
157 }
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000158
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200159 my $content = $template;
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000160 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200161 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200162 $content =~ s/<GUID>/$guid/g;
Gilles Peskine2c04f572023-11-03 19:16:56 +0100163 $content =~ s/INCLUDE_DIRECTORIES\r\n/($is_test_app ?
164 $library_include_directories :
165 $include_directories)/ge;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200166
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200167 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200168}
169
170sub get_app_list {
Dave Rodgmane264a7d2024-02-29 21:22:59 +0000171 my $app_list = `cd $programs_dir && VERBOSE_LOGS=1 make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200172 die "make list failed: $!\n" if $?;
173
174 return split /\s+/, $app_list;
175}
176
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200177sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200178 my @app_list = @_;
179
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200180 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200181
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200182 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200183 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
184 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200185}
186
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200187sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200188 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200189
190 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200191 for my $name (@names) {
192 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200193 $entries .= $entry;
194 }
195
196 return $entries;
197}
198
199sub gen_main_file {
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100200 my ($headers, $sources,
201 $hdr_tpl, $src_tpl,
202 $main_tpl, $main_out) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200203
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100204 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200205 my $source_entries = gen_entry_list( $src_tpl, @$sources );
206
207 my $out = slurp_file( $main_tpl );
208 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
209 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
Gilles Peskine66c3dc42020-06-03 02:25:17 +0200210 $out =~ s/INCLUDE_DIRECTORIES\r\n/$library_include_directories/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200211
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200212 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200213}
214
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200215sub gen_vsx_solution {
216 my (@app_names) = @_;
217
218 my ($app_entries, $conf_entries);
219 for my $path (@app_names) {
220 my $guid = gen_app_guid( $path );
221 (my $appname = $path) =~ s!.*/!!;
222
223 my $app_entry = $vsx_sln_app_entry_tpl;
224 $app_entry =~ s/{APPNAME}/$appname/g;
225 $app_entry =~ s/{GUID}/$guid/g;
226
227 $app_entries .= $app_entry;
228
229 my $conf_entry = $vsx_sln_conf_entry_tpl;
230 $conf_entry =~ s/{GUID}/$guid/g;
231
232 $conf_entries .= $conf_entry;
233 }
234
235 my $out = slurp_file( $vsx_sln_tpl_file );
236 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
237 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
238
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200239 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200240}
241
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000242sub del_vsx_files {
243 unlink glob "'$vsx_dir/*.$vsx_ext'";
244 unlink $vsx_main_file;
245 unlink $vsx_sln_file;
246}
247
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200248sub main {
249 if( ! check_dirs() ) {
250 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000251 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200252 }
253
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000254 # Remove old files to ensure that, for example, project files from deleted
255 # apps are not kept
256 del_vsx_files();
257
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200258 my @app_list = get_app_list();
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100259 my @header_dirs = (
260 $mbedtls_header_dir,
261 $psa_header_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200262 $test_header_dir,
Steven Cooremana70d5882020-07-16 20:26:18 +0200263 $test_drivers_header_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100264 $source_dir,
265 @thirdparty_header_dirs,
266 );
267 my @headers = (map { <$_/*.h> } @header_dirs);
268 my @source_dirs = (
269 $source_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200270 $test_source_dir,
Steven Cooreman16141ed2021-04-08 10:58:33 +0200271 $test_drivers_source_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100272 @thirdparty_source_dirs,
273 );
274 my @sources = (map { <$_/*.c> } @source_dirs);
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200275
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100276 @headers = grep { ! $excluded_files{$_} } @headers;
277 @sources = grep { ! $excluded_files{$_} } @sources;
278 map { s!/!\\!g } @headers;
279 map { s!/!\\!g } @sources;
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000280
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200281 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200282
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100283 gen_main_file( \@headers, \@sources,
284 $vsx_hdr_tpl, $vsx_src_tpl,
285 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200286
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200287 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200288
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200289 return 0;
290}