blob: a0dfc57bffb7dbf35814f4007a9a7549673f4e2f [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02002
Dave Rodgman0f459d72023-01-10 15:09:19 +00003# Generate main file, individual apps and solution files for
Bence Szépkúti0719d7c2024-03-12 16:45:55 +01004# MS Visual Studio 2017
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02005#
Dave Rodgman0f459d72023-01-10 15:09:19 +00006# Must be run from Mbed TLS 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 Rodgman16799db2023-11-02 19:47:20 +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
Bence Szépkútifac11222024-03-12 16:38:23 +010016my $vsx_dir = "visualc/VS2017";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020017my $vsx_ext = "vcxproj";
Bence Szépkútifac11222024-03-12 16:38:23 +010018my $vsx_app_tpl_file = "scripts/data_files/vs2017-app-template.$vsx_ext";
19my $vsx_main_tpl_file = "scripts/data_files/vs2017-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000020my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Bence Szépkútifac11222024-03-12 16:38:23 +010021my $vsx_sln_tpl_file = "scripts/data_files/vs2017-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
Dave Rodgman378ecdd2023-01-10 15:08:30 +000049 3rdparty/everest/include/everest/vs2013
Gilles Peskine7156d8c2020-02-19 20:08:44 +010050 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
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020070my $vsx_hdr_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020071 <ClInclude Include="..\\..\\{NAME}" />
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020072EOT
73my $vsx_src_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020074 <ClCompile Include="..\\..\\{NAME}" />
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020075EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020076
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020077my $vsx_sln_app_entry_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020078Project("{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
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020083EOT
84
85my $vsx_sln_conf_entry_tpl = <<EOT;
Gilles Peskine6e245c62021-04-22 20:09:25 +020086 {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
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020094EOT
95
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020096exit( main() );
97
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020098sub check_dirs {
Gilles Peskine13fac982020-02-12 20:34:22 +010099 foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) {
100 if (not (-d $d)) { return 0; }
101 }
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +0200102 return -d $vsx_dir
Darryl Green588e8cb2018-07-20 13:27:58 +0100103 && -d $mbedtls_header_dir
104 && -d $psa_header_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200105 && -d $source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200106 && -d $test_source_dir
Steven Cooreman16141ed2021-04-08 10:58:33 +0200107 && -d $test_drivers_source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200108 && -d $test_header_dir
Steven Cooremana70d5882020-07-16 20:26:18 +0200109 && -d $test_drivers_header_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200110 && -d $programs_dir;
111}
112
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200113sub slurp_file {
114 my ($filename) = @_;
115
116 local $/ = undef;
Gilles Peskine6e245c62021-04-22 20:09:25 +0200117 open my $fh, '<:crlf', $filename or die "Could not read $filename\n";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200118 my $content = <$fh>;
119 close $fh;
120
121 return $content;
122}
123
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200124sub content_to_file {
125 my ($content, $filename) = @_;
126
Gilles Peskine6e245c62021-04-22 20:09:25 +0200127 open my $fh, '>:crlf', $filename or die "Could not write to $filename\n";
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200128 print $fh $content;
129 close $fh;
130}
131
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200132sub gen_app_guid {
133 my ($path) = @_;
134
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000135 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200136 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
137
138 return $guid;
139}
140
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200141sub gen_app {
142 my ($path, $template, $dir, $ext) = @_;
143
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200144 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200145 $path =~ s!/!\\!g;
146 (my $appname = $path) =~ s/.*\\//;
Gilles Peskinea1023e22023-11-03 19:16:56 +0100147 my $is_test_app = ($path =~ m/^test\\/);
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200148
Gilles Peskine0f3f9c32020-04-01 13:34:50 +0200149 my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000150 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
151 $appname eq "query_compile_time_config" ) {
Gilles Peskine6e245c62021-04-22 20:09:25 +0200152 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000153 }
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100154 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" ) {
Gilles Peskine6e245c62021-04-22 20:09:25 +0200155 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\ssl_test_lib.c\" \/>";
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100156 }
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000157
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200158 my $content = $template;
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000159 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200160 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200161 $content =~ s/<GUID>/$guid/g;
Gilles Peskinea1023e22023-11-03 19:16:56 +0100162 $content =~ s/INCLUDE_DIRECTORIES\n/($is_test_app ?
163 $library_include_directories :
164 $include_directories)/ge;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200165
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200166 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200167}
168
169sub get_app_list {
Gilles Peskineb0fa9d22021-04-22 20:07:44 +0200170 my $makefile_contents = slurp_file('programs/Makefile');
171 $makefile_contents =~ /\n\s*APPS\s*=[\\\s]*(.*?)(?<!\\)[\#\n]/s
172 or die "Cannot find APPS = ... in programs/Makefile\n";
173 return split /(?:\s|\\)+/, $1;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200174}
175
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200176sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200177 my @app_list = @_;
178
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200179 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200180
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200181 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200182 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
183 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200184}
185
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200186sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200187 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200188
189 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200190 for my $name (@names) {
191 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200192 $entries .= $entry;
193 }
194
195 return $entries;
196}
197
198sub gen_main_file {
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100199 my ($headers, $sources,
200 $hdr_tpl, $src_tpl,
201 $main_tpl, $main_out) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200202
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100203 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200204 my $source_entries = gen_entry_list( $src_tpl, @$sources );
205
206 my $out = slurp_file( $main_tpl );
Gilles Peskine6e245c62021-04-22 20:09:25 +0200207 $out =~ s/SOURCE_ENTRIES\n/$source_entries/m;
208 $out =~ s/HEADER_ENTRIES\n/$header_entries/m;
209 $out =~ s/INCLUDE_DIRECTORIES\n/$library_include_directories/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200210
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200211 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200212}
213
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200214sub gen_vsx_solution {
215 my (@app_names) = @_;
216
217 my ($app_entries, $conf_entries);
218 for my $path (@app_names) {
219 my $guid = gen_app_guid( $path );
220 (my $appname = $path) =~ s!.*/!!;
221
222 my $app_entry = $vsx_sln_app_entry_tpl;
223 $app_entry =~ s/{APPNAME}/$appname/g;
224 $app_entry =~ s/{GUID}/$guid/g;
225
226 $app_entries .= $app_entry;
227
228 my $conf_entry = $vsx_sln_conf_entry_tpl;
229 $conf_entry =~ s/{GUID}/$guid/g;
230
231 $conf_entries .= $conf_entry;
232 }
233
234 my $out = slurp_file( $vsx_sln_tpl_file );
Gilles Peskine6e245c62021-04-22 20:09:25 +0200235 $out =~ s/APP_ENTRIES\n/$app_entries/m;
236 $out =~ s/CONF_ENTRIES\n/$conf_entries/m;
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200237
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200238 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200239}
240
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000241sub del_vsx_files {
242 unlink glob "'$vsx_dir/*.$vsx_ext'";
243 unlink $vsx_main_file;
244 unlink $vsx_sln_file;
245}
246
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200247sub main {
248 if( ! check_dirs() ) {
249 chdir '..' or die;
Dave Rodgman0f459d72023-01-10 15:09:19 +0000250 check_dirs or die "Must be run from Mbed TLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200251 }
252
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000253 # Remove old files to ensure that, for example, project files from deleted
254 # apps are not kept
255 del_vsx_files();
256
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200257 my @app_list = get_app_list();
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100258 my @header_dirs = (
259 $mbedtls_header_dir,
260 $psa_header_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200261 $test_header_dir,
Steven Cooremana70d5882020-07-16 20:26:18 +0200262 $test_drivers_header_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100263 $source_dir,
264 @thirdparty_header_dirs,
265 );
266 my @headers = (map { <$_/*.h> } @header_dirs);
267 my @source_dirs = (
268 $source_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200269 $test_source_dir,
Steven Cooreman16141ed2021-04-08 10:58:33 +0200270 $test_drivers_source_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100271 @thirdparty_source_dirs,
272 );
273 my @sources = (map { <$_/*.c> } @source_dirs);
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200274
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100275 @headers = grep { ! $excluded_files{$_} } @headers;
276 @sources = grep { ! $excluded_files{$_} } @sources;
277 map { s!/!\\!g } @headers;
278 map { s!/!\\!g } @sources;
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000279
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200280 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200281
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100282 gen_main_file( \@headers, \@sources,
283 $vsx_hdr_tpl, $vsx_src_tpl,
284 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200285
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200286 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200287
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200288 return 0;
289}