blob: d72d19dad71298d2aa666422f85b1257e5399832 [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#
9# Copyright (C) 2013-2020, Arm Limited, All Rights Reserved
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.
Bence Szépkúti700ee442020-05-26 00:33:31 +020023#
24# This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +020025
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020026use warnings;
27use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020028use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020029
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020030my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020031my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020032my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020033my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000034my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020035my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000036my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020037
38my $programs_dir = 'programs';
Darryl Green588e8cb2018-07-20 13:27:58 +010039my $mbedtls_header_dir = 'include/mbedtls';
40my $psa_header_dir = 'include/psa';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020041my $source_dir = 'library';
Gilles Peskine13fac982020-02-12 20:34:22 +010042
43my @thirdparty_header_dirs = qw(
44 3rdparty/everest/include/everest
45);
46my @thirdparty_source_dirs = qw(
47 3rdparty/everest/library
48 3rdparty/everest/library/kremlib
49 3rdparty/everest/library/legacy
50);
Gilles Peskineb41f88f2020-02-12 20:45:18 +010051
Gilles Peskine7156d8c2020-02-19 20:08:44 +010052# Directories to add to the include path.
53# Order matters in case there are files with the same name in more than
54# one directory: the compiler will use the first match.
55my @include_directories = qw(
56 include
57 3rdparty/everest/include/
58 3rdparty/everest/include/everest
59 3rdparty/everest/include/everest/vs2010
60 3rdparty/everest/include/everest/kremlib
61);
62my $include_directories = join(';', map {"../../$_"} @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
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200107 && -d $programs_dir;
108}
109
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200110sub slurp_file {
111 my ($filename) = @_;
112
113 local $/ = undef;
114 open my $fh, '<', $filename or die "Could not read $filename\n";
115 my $content = <$fh>;
116 close $fh;
117
118 return $content;
119}
120
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200121sub content_to_file {
122 my ($content, $filename) = @_;
123
124 open my $fh, '>', $filename or die "Could not write to $filename\n";
125 print $fh $content;
126 close $fh;
127}
128
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200129sub gen_app_guid {
130 my ($path) = @_;
131
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000132 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200133 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
134
135 return $guid;
136}
137
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200138sub gen_app {
139 my ($path, $template, $dir, $ext) = @_;
140
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200141 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200142 $path =~ s!/!\\!g;
143 (my $appname = $path) =~ s/.*\\//;
144
Gilles Peskine0f3f9c32020-04-01 13:34:50 +0200145 my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000146 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
147 $appname eq "query_compile_time_config" ) {
Gilles Peskine0f3f9c32020-04-01 13:34:50 +0200148 $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>";
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000149 }
150
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200151 my $content = $template;
Andres Amaya Garciac84a65d2018-10-30 21:46:21 +0000152 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200153 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200154 $content =~ s/<GUID>/$guid/g;
Gilles Peskined362d0b2020-02-26 14:32:42 +0100155 $content =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200156
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200157 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200158}
159
160sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200161 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200162 die "make list failed: $!\n" if $?;
163
164 return split /\s+/, $app_list;
165}
166
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200167sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200168 my @app_list = @_;
169
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200170 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200171
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200172 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200173 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
174 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200175}
176
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200177sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200178 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200179
180 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200181 for my $name (@names) {
182 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200183 $entries .= $entry;
184 }
185
186 return $entries;
187}
188
189sub gen_main_file {
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100190 my ($headers, $sources,
191 $hdr_tpl, $src_tpl,
192 $main_tpl, $main_out) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200193
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100194 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200195 my $source_entries = gen_entry_list( $src_tpl, @$sources );
196
197 my $out = slurp_file( $main_tpl );
198 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
199 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
Gilles Peskined362d0b2020-02-26 14:32:42 +0100200 $out =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200201
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200202 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200203}
204
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200205sub gen_vsx_solution {
206 my (@app_names) = @_;
207
208 my ($app_entries, $conf_entries);
209 for my $path (@app_names) {
210 my $guid = gen_app_guid( $path );
211 (my $appname = $path) =~ s!.*/!!;
212
213 my $app_entry = $vsx_sln_app_entry_tpl;
214 $app_entry =~ s/{APPNAME}/$appname/g;
215 $app_entry =~ s/{GUID}/$guid/g;
216
217 $app_entries .= $app_entry;
218
219 my $conf_entry = $vsx_sln_conf_entry_tpl;
220 $conf_entry =~ s/{GUID}/$guid/g;
221
222 $conf_entries .= $conf_entry;
223 }
224
225 my $out = slurp_file( $vsx_sln_tpl_file );
226 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
227 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
228
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200229 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200230}
231
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000232sub del_vsx_files {
233 unlink glob "'$vsx_dir/*.$vsx_ext'";
234 unlink $vsx_main_file;
235 unlink $vsx_sln_file;
236}
237
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200238sub main {
239 if( ! check_dirs() ) {
240 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000241 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200242 }
243
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000244 # Remove old files to ensure that, for example, project files from deleted
245 # apps are not kept
246 del_vsx_files();
247
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200248 my @app_list = get_app_list();
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100249 my @header_dirs = (
250 $mbedtls_header_dir,
251 $psa_header_dir,
252 $source_dir,
253 @thirdparty_header_dirs,
254 );
255 my @headers = (map { <$_/*.h> } @header_dirs);
256 my @source_dirs = (
257 $source_dir,
258 @thirdparty_source_dirs,
259 );
260 my @sources = (map { <$_/*.c> } @source_dirs);
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200261
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100262 @headers = grep { ! $excluded_files{$_} } @headers;
263 @sources = grep { ! $excluded_files{$_} } @sources;
264 map { s!/!\\!g } @headers;
265 map { s!/!\\!g } @sources;
Christoph M. Wintersteiger48d26c22018-12-06 18:59:19 +0000266
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200267 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200268
Gilles Peskineb41f88f2020-02-12 20:45:18 +0100269 gen_main_file( \@headers, \@sources,
270 $vsx_hdr_tpl, $vsx_src_tpl,
271 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200272
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200273 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200274
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200275 return 0;
276}