blob: 8dda51153f6d12c9cce4c3019a835be5bedae4a0 [file] [log] [blame]
fbrosson3a745712018-04-04 22:26:56 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02002
Andres AG430e68c2018-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útib7246ad2020-05-26 00:33:31 +02008#
9# Copyright (C) 2013-2018, Arm Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-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úti09b4f192020-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útib7246ad2020-05-26 00:33:31 +020029#
Bence Szépkúti4e9f7122020-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# **********
50#
Bence Szépkútib7246ad2020-05-26 00:33:31 +020051# This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +020052
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020053use warnings;
54use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020055use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020056
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020057my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020058my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020059my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020060my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000061my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020062my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000063my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020064
65my $programs_dir = 'programs';
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066my $header_dir = 'include/mbedtls';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020067my $source_dir = 'library';
68
69# Need windows line endings!
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020070my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020071 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020072EOT
73my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020074 <ClCompile Include="..\\..\\{NAME}" />\r
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;
78Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
79 ProjectSection(ProjectDependencies) = postProject\r
80 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
81 EndProjectSection\r
82EndProject\r
83EOT
84
85my $vsx_sln_conf_entry_tpl = <<EOT;
86 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
87 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
88 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
89 {GUID}.Debug|x64.Build.0 = Debug|x64\r
90 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
91 {GUID}.Release|Win32.Build.0 = Release|Win32\r
92 {GUID}.Release|x64.ActiveCfg = Release|x64\r
93 {GUID}.Release|x64.Build.0 = Release|x64\r
94EOT
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 {
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +020099 return -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200100 && -d $header_dir
101 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200102 && -d $programs_dir;
103}
104
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200105sub slurp_file {
106 my ($filename) = @_;
107
108 local $/ = undef;
109 open my $fh, '<', $filename or die "Could not read $filename\n";
110 my $content = <$fh>;
111 close $fh;
112
113 return $content;
114}
115
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200116sub content_to_file {
117 my ($content, $filename) = @_;
118
119 open my $fh, '>', $filename or die "Could not write to $filename\n";
120 print $fh $content;
121 close $fh;
122}
123
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200124sub gen_app_guid {
125 my ($path) = @_;
126
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000127 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200128 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
129
130 return $guid;
131}
132
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200133sub gen_app {
134 my ($path, $template, $dir, $ext) = @_;
135
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200136 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200137 $path =~ s!/!\\!g;
138 (my $appname = $path) =~ s/.*\\//;
139
140 my $content = $template;
141 $content =~ s/<PATHNAME>/$path/g;
142 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200143 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200144
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200145 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200146}
147
148sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200149 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200150 die "make list failed: $!\n" if $?;
151
152 return split /\s+/, $app_list;
153}
154
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200155sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200156 my @app_list = @_;
157
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200158 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200159
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200160 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200161 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
162 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200163}
164
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200165sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200166 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200167
168 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200169 for my $name (@names) {
170 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200171 $entries .= $entry;
172 }
173
174 return $entries;
175}
176
177sub gen_main_file {
178 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
179
180 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
181 my $source_entries = gen_entry_list( $src_tpl, @$sources );
182
183 my $out = slurp_file( $main_tpl );
184 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
185 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
186
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200187 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200188}
189
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200190sub gen_vsx_solution {
191 my (@app_names) = @_;
192
193 my ($app_entries, $conf_entries);
194 for my $path (@app_names) {
195 my $guid = gen_app_guid( $path );
196 (my $appname = $path) =~ s!.*/!!;
197
198 my $app_entry = $vsx_sln_app_entry_tpl;
199 $app_entry =~ s/{APPNAME}/$appname/g;
200 $app_entry =~ s/{GUID}/$guid/g;
201
202 $app_entries .= $app_entry;
203
204 my $conf_entry = $vsx_sln_conf_entry_tpl;
205 $conf_entry =~ s/{GUID}/$guid/g;
206
207 $conf_entries .= $conf_entry;
208 }
209
210 my $out = slurp_file( $vsx_sln_tpl_file );
211 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
212 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
213
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200214 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200215}
216
Andres Amaya Garcia464b1492018-01-11 19:51:27 +0000217sub del_vsx_files {
218 unlink glob "'$vsx_dir/*.$vsx_ext'";
219 unlink $vsx_main_file;
220 unlink $vsx_sln_file;
221}
222
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200223sub main {
224 if( ! check_dirs() ) {
225 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000226 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200227 }
228
Andres Amaya Garcia464b1492018-01-11 19:51:27 +0000229 # Remove old files to ensure that, for example, project files from deleted
230 # apps are not kept
231 del_vsx_files();
232
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200233 my @app_list = get_app_list();
234 my @headers = <$header_dir/*.h>;
235 my @sources = <$source_dir/*.c>;
236 map { s!/!\\!g } @headers;
237 map { s!/!\\!g } @sources;
238
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200239 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200240
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200241 gen_main_file( \@headers, \@sources,
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200242 $vsx_hdr_tpl, $vsx_src_tpl,
243 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200244
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200245 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200246
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200247 return 0;
248}