blob: 6979f942a9fc73cf30f95ed1d697f4fd8c18e3ed [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úti468a76f2020-05-26 00:33:31 +02008#
Bence Szépkútia2947ac2020-08-19 16:37:36 +02009# Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020010# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11#
12# This file is provided under the Apache License 2.0, or the
13# GNU General Public License v2.0 or later.
14#
15# **********
16# Apache License 2.0:
Bence Szépkúti51b41d52020-05-26 01:54:15 +020017#
18# Licensed under the Apache License, Version 2.0 (the "License"); you may
19# not use this file except in compliance with the License.
20# You may obtain a copy of the License at
21#
22# http://www.apache.org/licenses/LICENSE-2.0
23#
24# Unless required by applicable law or agreed to in writing, software
25# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27# See the License for the specific language governing permissions and
28# limitations under the License.
Bence Szépkúti468a76f2020-05-26 00:33:31 +020029#
Bence Szépkútif744bd72020-06-05 13:02:18 +020030# **********
31#
32# **********
33# GNU General Public License v2.0 or later:
34#
35# This program is free software; you can redistribute it and/or modify
36# it under the terms of the GNU General Public License as published by
37# the Free Software Foundation; either version 2 of the License, or
38# (at your option) any later version.
39#
40# This program is distributed in the hope that it will be useful,
41# but WITHOUT ANY WARRANTY; without even the implied warranty of
42# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43# GNU General Public License for more details.
44#
45# You should have received a copy of the GNU General Public License along
46# with this program; if not, write to the Free Software Foundation, Inc.,
47# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
48#
49# **********
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +020050
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020051use warnings;
52use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020053use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020054
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020055my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020056my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020057my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020058my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000059my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020060my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000061my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020062
63my $programs_dir = 'programs';
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064my $header_dir = 'include/mbedtls';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020065my $source_dir = 'library';
66
67# Need windows line endings!
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020068my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020069 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020070EOT
71my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020072 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020073EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020074
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020075my $vsx_sln_app_entry_tpl = <<EOT;
76Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
77 ProjectSection(ProjectDependencies) = postProject\r
78 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
79 EndProjectSection\r
80EndProject\r
81EOT
82
83my $vsx_sln_conf_entry_tpl = <<EOT;
84 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
85 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
86 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
87 {GUID}.Debug|x64.Build.0 = Debug|x64\r
88 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
89 {GUID}.Release|Win32.Build.0 = Release|Win32\r
90 {GUID}.Release|x64.ActiveCfg = Release|x64\r
91 {GUID}.Release|x64.Build.0 = Release|x64\r
92EOT
93
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020094exit( main() );
95
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020096sub check_dirs {
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +020097 return -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020098 && -d $header_dir
99 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200100 && -d $programs_dir;
101}
102
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200103sub slurp_file {
104 my ($filename) = @_;
105
106 local $/ = undef;
107 open my $fh, '<', $filename or die "Could not read $filename\n";
108 my $content = <$fh>;
109 close $fh;
110
111 return $content;
112}
113
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200114sub content_to_file {
115 my ($content, $filename) = @_;
116
117 open my $fh, '>', $filename or die "Could not write to $filename\n";
118 print $fh $content;
119 close $fh;
120}
121
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200122sub gen_app_guid {
123 my ($path) = @_;
124
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000125 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200126 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
127
128 return $guid;
129}
130
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200131sub gen_app {
132 my ($path, $template, $dir, $ext) = @_;
133
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200134 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200135 $path =~ s!/!\\!g;
136 (my $appname = $path) =~ s/.*\\//;
137
Gilles Peskinefea21d42020-04-01 13:34:50 +0200138 my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
Andres Amaya Garcia024694e2018-10-30 21:46:21 +0000139 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
140 $appname eq "query_compile_time_config" ) {
Gilles Peskinefea21d42020-04-01 13:34:50 +0200141 $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\ssl\\query_config.c\" \/>";
Andres Amaya Garcia024694e2018-10-30 21:46:21 +0000142 }
143
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200144 my $content = $template;
Andres Amaya Garcia024694e2018-10-30 21:46:21 +0000145 $content =~ s/<SOURCES>/$srcs/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200146 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200147 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200148
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200149 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200150}
151
152sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200153 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200154 die "make list failed: $!\n" if $?;
155
156 return split /\s+/, $app_list;
157}
158
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200159sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200160 my @app_list = @_;
161
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200162 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200163
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200164 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200165 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
166 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200167}
168
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200169sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200170 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200171
172 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200173 for my $name (@names) {
174 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200175 $entries .= $entry;
176 }
177
178 return $entries;
179}
180
181sub gen_main_file {
182 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
183
184 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
185 my $source_entries = gen_entry_list( $src_tpl, @$sources );
186
187 my $out = slurp_file( $main_tpl );
188 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
189 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
190
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200191 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200192}
193
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200194sub gen_vsx_solution {
195 my (@app_names) = @_;
196
197 my ($app_entries, $conf_entries);
198 for my $path (@app_names) {
199 my $guid = gen_app_guid( $path );
200 (my $appname = $path) =~ s!.*/!!;
201
202 my $app_entry = $vsx_sln_app_entry_tpl;
203 $app_entry =~ s/{APPNAME}/$appname/g;
204 $app_entry =~ s/{GUID}/$guid/g;
205
206 $app_entries .= $app_entry;
207
208 my $conf_entry = $vsx_sln_conf_entry_tpl;
209 $conf_entry =~ s/{GUID}/$guid/g;
210
211 $conf_entries .= $conf_entry;
212 }
213
214 my $out = slurp_file( $vsx_sln_tpl_file );
215 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
216 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
217
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200218 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200219}
220
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000221sub del_vsx_files {
222 unlink glob "'$vsx_dir/*.$vsx_ext'";
223 unlink $vsx_main_file;
224 unlink $vsx_sln_file;
225}
226
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200227sub main {
228 if( ! check_dirs() ) {
229 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000230 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200231 }
232
Andres Amaya Garcia3c5f9492018-01-11 19:51:27 +0000233 # Remove old files to ensure that, for example, project files from deleted
234 # apps are not kept
235 del_vsx_files();
236
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200237 my @app_list = get_app_list();
238 my @headers = <$header_dir/*.h>;
239 my @sources = <$source_dir/*.c>;
240 map { s!/!\\!g } @headers;
241 map { s!/!\\!g } @sources;
242
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200243 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200244
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200245 gen_main_file( \@headers, \@sources,
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200246 $vsx_hdr_tpl, $vsx_src_tpl,
247 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200248
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200249 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200250
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200251 return 0;
252}