1#!/usr/bin/env perl 2 3# Generate main file, individual apps and solution files for MS Visual Studio 4# 2010 5# 6# Must be run from mbedTLS root or scripts directory. 7# Takes no argument. 8# 9# Copyright The Mbed TLS Contributors 10# 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: 17# 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. 29# 30# ********** 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 51use warnings; 52use strict; 53use Digest::MD5 'md5_hex'; 54 55my $vsx_dir = "visualc/VS2010"; 56my $vsx_ext = "vcxproj"; 57my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext"; 58my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext"; 59my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext"; 60my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln"; 61my $vsx_sln_file = "$vsx_dir/mbedTLS.sln"; 62 63my $programs_dir = 'programs'; 64my $header_dir = 'include/mbedtls'; 65my $source_dir = 'library'; 66 67# Need windows line endings! 68my $vsx_hdr_tpl = <<EOT; 69 <ClInclude Include="..\\..\\{NAME}" />\r 70EOT 71my $vsx_src_tpl = <<EOT; 72 <ClCompile Include="..\\..\\{NAME}" />\r 73EOT 74 75my $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 94exit( main() ); 95 96sub check_dirs { 97 return -d $vsx_dir 98 && -d $header_dir 99 && -d $source_dir 100 && -d $programs_dir; 101} 102 103sub 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 114sub 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 122sub gen_app_guid { 123 my ($path) = @_; 124 125 my $guid = md5_hex( "mbedTLS:$path" ); 126 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/; 127 128 return $guid; 129} 130 131sub gen_app { 132 my ($path, $template, $dir, $ext) = @_; 133 134 my $guid = gen_app_guid( $path ); 135 $path =~ s!/!\\!g; 136 (my $appname = $path) =~ s/.*\\//; 137 138 my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>"; 139 if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or 140 $appname eq "query_compile_time_config" ) { 141 $srcs .= "\r\n <ClCompile Include=\"..\\..\\programs\\ssl\\query_config.c\" \/>"; 142 } 143 144 my $content = $template; 145 $content =~ s/<SOURCES>/$srcs/g; 146 $content =~ s/<APPNAME>/$appname/g; 147 $content =~ s/<GUID>/$guid/g; 148 149 content_to_file( $content, "$dir/$appname.$ext" ); 150} 151 152sub get_app_list { 153 my $app_list = `cd $programs_dir && make list`; 154 die "make list failed: $!\n" if $?; 155 156 return split /\s+/, $app_list; 157} 158 159sub gen_app_files { 160 my @app_list = @_; 161 162 my $vsx_tpl = slurp_file( $vsx_app_tpl_file ); 163 164 for my $app ( @app_list ) { 165 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext ); 166 } 167} 168 169sub gen_entry_list { 170 my ($tpl, @names) = @_; 171 172 my $entries; 173 for my $name (@names) { 174 (my $entry = $tpl) =~ s/{NAME}/$name/g; 175 $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 191 content_to_file( $out, $main_out ); 192} 193 194sub 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 218 content_to_file( $out, $vsx_sln_file ); 219} 220 221sub del_vsx_files { 222 unlink glob "'$vsx_dir/*.$vsx_ext'"; 223 unlink $vsx_main_file; 224 unlink $vsx_sln_file; 225} 226 227sub main { 228 if( ! check_dirs() ) { 229 chdir '..' or die; 230 check_dirs or die "Must but run from mbedTLS root or scripts dir\n"; 231 } 232 233 # Remove old files to ensure that, for example, project files from deleted 234 # apps are not kept 235 del_vsx_files(); 236 237 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 243 gen_app_files( @app_list ); 244 245 gen_main_file( \@headers, \@sources, 246 $vsx_hdr_tpl, $vsx_src_tpl, 247 $vsx_main_tpl_file, $vsx_main_file ); 248 249 gen_vsx_solution( @app_list ); 250 251 return 0; 252} 253