blob: 40c118497db70ec2b1f54ff404979834e7c4be94 [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
Bence Szépkútic7da1fe2020-05-26 01:54:15 +020010# SPDX-License-Identifier: Apache-2.0
11#
12# Licensed under the Apache License, Version 2.0 (the "License"); you may
13# not use this file except in compliance with the License.
14# You may obtain a copy of the License at
15#
16# http://www.apache.org/licenses/LICENSE-2.0
17#
18# Unless required by applicable law or agreed to in writing, software
19# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21# See the License for the specific language governing permissions and
22# limitations under the License.
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +020023
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020024use warnings;
25use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020026use Digest::MD5 'md5_hex';
Gilles Peskinedd7a9732021-04-22 01:09:59 +020027use File::Path;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020028
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020029my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020030my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020031my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020032my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000033my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020034my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000035my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020036
37my $programs_dir = 'programs';
Darryl Green588e8cb2018-07-20 13:27:58 +010038my $mbedtls_header_dir = 'include/mbedtls';
39my $psa_header_dir = 'include/psa';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020040my $source_dir = 'library';
Ronald Cron8dc0af22020-06-05 16:00:22 +020041my $test_source_dir = 'tests/src';
42my $test_header_dir = 'tests/include/test';
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020043my $test_drivers_header_dir = 'tests/include/test/drivers';
Gilles Peskine13fac982020-02-12 20:34:22 +010044
45my @thirdparty_header_dirs = qw(
46 3rdparty/everest/include/everest
47);
48my @thirdparty_source_dirs = qw(
49 3rdparty/everest/library
50 3rdparty/everest/library/kremlib
51 3rdparty/everest/library/legacy
52);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010053
Gilles Peskine7156d8c2020-02-19 20:08:44 +010054# Directories to add to the include path.
55# Order matters in case there are files with the same name in more than
56# one directory: the compiler will use the first match.
57my @include_directories = qw(
58 include
59 3rdparty/everest/include/
60 3rdparty/everest/include/everest
61 3rdparty/everest/include/everest/vs2010
62 3rdparty/everest/include/everest/kremlib
Ronald Cron8dc0af22020-06-05 16:00:22 +020063 tests/include
Gilles Peskine7156d8c2020-02-19 20:08:44 +010064);
65my $include_directories = join(';', map {"../../$_"} @include_directories);
66
Gilles Peskine66c3dc42020-06-03 02:25:17 +020067# Directories to add to the include path when building the library, but not
68# when building tests or applications.
69my @library_include_directories = qw(
70 library
71);
72my $library_include_directories =
73 join(';', map {"../../$_"} (@library_include_directories,
74 @include_directories));
75
Gilles Peskineb41f88f2020-02-12 20:45:18 +010076my @excluded_files = qw(
Gilles Peskine13fac982020-02-12 20:34:22 +010077 3rdparty/everest/library/Hacl_Curve25519.c
78);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010079my %excluded_files = ();
80foreach (@excluded_files) { $excluded_files{$_} = 1 }
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020081
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020082my $vsx_hdr_tpl = <<EOT;
Gilles Peskine351aea72021-04-22 20:09:25 +020083 <ClInclude Include="..\\..\\{NAME}" />
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020084EOT
85my $vsx_src_tpl = <<EOT;
Gilles Peskine351aea72021-04-22 20:09:25 +020086 <ClCompile Include="..\\..\\{NAME}" />
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020087EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020088
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020089my $vsx_sln_app_entry_tpl = <<EOT;
Gilles Peskine351aea72021-04-22 20:09:25 +020090Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"
91 ProjectSection(ProjectDependencies) = postProject
92 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}
93 EndProjectSection
94EndProject
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020095EOT
96
97my $vsx_sln_conf_entry_tpl = <<EOT;
Gilles Peskine351aea72021-04-22 20:09:25 +020098 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32
99 {GUID}.Debug|Win32.Build.0 = Debug|Win32
100 {GUID}.Debug|x64.ActiveCfg = Debug|x64
101 {GUID}.Debug|x64.Build.0 = Debug|x64
102 {GUID}.Release|Win32.ActiveCfg = Release|Win32
103 {GUID}.Release|Win32.Build.0 = Release|Win32
104 {GUID}.Release|x64.ActiveCfg = Release|x64
105 {GUID}.Release|x64.Build.0 = Release|x64
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200106EOT
107
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200108exit( main() );
109
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200110sub check_dirs {
Gilles Peskine13fac982020-02-12 20:34:22 +0100111 foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) {
112 if (not (-d $d)) { return 0; }
113 }
Gilles Peskinedd7a9732021-04-22 01:09:59 +0200114 return -d $mbedtls_header_dir
Darryl Green588e8cb2018-07-20 13:27:58 +0100115 && -d $psa_header_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200116 && -d $source_dir
Ronald Cron8dc0af22020-06-05 16:00:22 +0200117 && -d $test_source_dir
118 && -d $test_header_dir
Steven Cooremana70d5882020-07-16 20:26:18 +0200119 && -d $test_drivers_header_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200120 && -d $programs_dir;
121}
122
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200123sub slurp_file {
124 my ($filename) = @_;
125
126 local $/ = undef;
Gilles Peskine351aea72021-04-22 20:09:25 +0200127 open my $fh, '<:crlf', $filename or die "Could not read $filename\n";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200128 my $content = <$fh>;
129 close $fh;
130
131 return $content;
132}
133
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200134sub content_to_file {
135 my ($content, $filename) = @_;
136
Gilles Peskine351aea72021-04-22 20:09:25 +0200137 open my $fh, '>:crlf', $filename or die "Could not write to $filename\n";
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200138 print $fh $content;
139 close $fh;
140}
141
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200142sub gen_app_guid {
143 my ($path) = @_;
144
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000145 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200146 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
147
148 return $guid;
149}
150
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200151sub gen_app {
152 my ($path, $template, $dir, $ext) = @_;
153
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200154 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200155 $path =~ s!/!\\!g;
156 (my $appname = $path) =~ s/.*\\//;
157
Gilles Peskine0f3f9c32020-04-01 13:34:50 +0200158 my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000159 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
160 $appname eq "query_compile_time_config" ) {
Gilles Peskine351aea72021-04-22 20:09:25 +0200161 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000162 }
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100163 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" ) {
Gilles Peskine351aea72021-04-22 20:09:25 +0200164 $srcs .= "\n <ClCompile Include=\"..\\..\\programs\\ssl\\ssl_test_lib.c\" \/>";
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100165 }
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000166
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200167 my $content = $template;
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000168 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200169 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200170 $content =~ s/<GUID>/$guid/g;
Gilles Peskine351aea72021-04-22 20:09:25 +0200171 $content =~ s/INCLUDE_DIRECTORIES\n/$include_directories/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200172
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200173 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200174}
175
176sub get_app_list {
Gilles Peskinec145c332021-04-22 20:07:44 +0200177 my $makefile_contents = slurp_file('programs/Makefile');
178 $makefile_contents =~ /\n\s*APPS\s*=[\\\s]*(.*?)(?<!\\)[\#\n]/s
179 or die "Cannot find APPS = ... in programs/Makefile\n";
180 return split /(?:\s|\\)+/, $1;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200181}
182
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200183sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200184 my @app_list = @_;
185
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200186 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200187
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200188 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200189 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
190 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200191}
192
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200193sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200194 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200195
196 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200197 for my $name (@names) {
198 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200199 $entries .= $entry;
200 }
201
202 return $entries;
203}
204
205sub gen_main_file {
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100206 my ($headers, $sources,
207 $hdr_tpl, $src_tpl,
208 $main_tpl, $main_out) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200209
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100210 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200211 my $source_entries = gen_entry_list( $src_tpl, @$sources );
212
213 my $out = slurp_file( $main_tpl );
Gilles Peskine351aea72021-04-22 20:09:25 +0200214 $out =~ s/SOURCE_ENTRIES\n/$source_entries/m;
215 $out =~ s/HEADER_ENTRIES\n/$header_entries/m;
216 $out =~ s/INCLUDE_DIRECTORIES\n/$library_include_directories/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200217
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200218 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200219}
220
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200221sub gen_vsx_solution {
222 my (@app_names) = @_;
223
224 my ($app_entries, $conf_entries);
225 for my $path (@app_names) {
226 my $guid = gen_app_guid( $path );
227 (my $appname = $path) =~ s!.*/!!;
228
229 my $app_entry = $vsx_sln_app_entry_tpl;
230 $app_entry =~ s/{APPNAME}/$appname/g;
231 $app_entry =~ s/{GUID}/$guid/g;
232
233 $app_entries .= $app_entry;
234
235 my $conf_entry = $vsx_sln_conf_entry_tpl;
236 $conf_entry =~ s/{GUID}/$guid/g;
237
238 $conf_entries .= $conf_entry;
239 }
240
241 my $out = slurp_file( $vsx_sln_tpl_file );
Gilles Peskine351aea72021-04-22 20:09:25 +0200242 $out =~ s/APP_ENTRIES\n/$app_entries/m;
243 $out =~ s/CONF_ENTRIES\n/$conf_entries/m;
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200244
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200245 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200246}
247
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000248sub del_vsx_files {
249 unlink glob "'$vsx_dir/*.$vsx_ext'";
250 unlink $vsx_main_file;
251 unlink $vsx_sln_file;
252}
253
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200254sub main {
255 if( ! check_dirs() ) {
256 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000257 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200258 }
Gilles Peskinedd7a9732021-04-22 01:09:59 +0200259 File::Path::make_path($vsx_dir);
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200260
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000261 # Remove old files to ensure that, for example, project files from deleted
262 # apps are not kept
263 del_vsx_files();
264
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200265 my @app_list = get_app_list();
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100266 my @header_dirs = (
267 $mbedtls_header_dir,
268 $psa_header_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200269 $test_header_dir,
Steven Cooremana70d5882020-07-16 20:26:18 +0200270 $test_drivers_header_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100271 $source_dir,
272 @thirdparty_header_dirs,
273 );
274 my @headers = (map { <$_/*.h> } @header_dirs);
275 my @source_dirs = (
276 $source_dir,
Ronald Cron8dc0af22020-06-05 16:00:22 +0200277 $test_source_dir,
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100278 @thirdparty_source_dirs,
279 );
280 my @sources = (map { <$_/*.c> } @source_dirs);
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200281
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100282 @headers = grep { ! $excluded_files{$_} } @headers;
283 @sources = grep { ! $excluded_files{$_} } @sources;
284 map { s!/!\\!g } @headers;
285 map { s!/!\\!g } @sources;
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000286
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200287 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200288
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100289 gen_main_file( \@headers, \@sources,
290 $vsx_hdr_tpl, $vsx_src_tpl,
291 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200292
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200293 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200294
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200295 return 0;
296}