fbrosson | 3a74571 | 2018-04-04 22:26:56 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 2 | |
Andres AG | 430e68c | 2018-04-11 20:35:19 -0500 | [diff] [blame] | 3 | # Generate main file, individual apps and solution files for MS Visual Studio |
| 4 | # 2010 |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 5 | # |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 6 | # Must be run from mbedTLS root or scripts directory. |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 7 | # Takes no argument. |
Bence Szépkúti | b7246ad | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 8 | # |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 9 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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: |
Bence Szépkúti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 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. |
Bence Szépkúti | b7246ad | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 29 | # |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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 | # ********** |
Manuel Pégourié-Gonnard | 684e9dc | 2013-09-20 15:11:44 +0200 | [diff] [blame] | 50 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 51 | use warnings; |
| 52 | use strict; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 53 | use Digest::MD5 'md5_hex'; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 55 | my $vsx_dir = "visualc/VS2010"; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 56 | my $vsx_ext = "vcxproj"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 57 | my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext"; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 58 | my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext"; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 59 | my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext"; |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 60 | my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln"; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 61 | my $vsx_sln_file = "$vsx_dir/mbedTLS.sln"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 62 | |
| 63 | my $programs_dir = 'programs'; |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 64 | my $header_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 65 | my $source_dir = 'library'; |
| 66 | |
| 67 | # Need windows line endings! |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 68 | my $vsx_hdr_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 69 | <ClInclude Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 70 | EOT |
| 71 | my $vsx_src_tpl = <<EOT; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 72 | <ClCompile Include="..\\..\\{NAME}" />\r |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 73 | EOT |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 74 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 75 | my $vsx_sln_app_entry_tpl = <<EOT; |
| 76 | Project("{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 |
| 80 | EndProject\r |
| 81 | EOT |
| 82 | |
| 83 | my $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 |
| 92 | EOT |
| 93 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 94 | exit( main() ); |
| 95 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 96 | sub check_dirs { |
Manuel Pégourié-Gonnard | 51f14be | 2015-05-14 13:04:03 +0200 | [diff] [blame] | 97 | return -d $vsx_dir |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 98 | && -d $header_dir |
| 99 | && -d $source_dir |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 100 | && -d $programs_dir; |
| 101 | } |
| 102 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 103 | sub 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é-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 114 | sub 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é-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 122 | sub gen_app_guid { |
| 123 | my ($path) = @_; |
| 124 | |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 125 | my $guid = md5_hex( "mbedTLS:$path" ); |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 126 | $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/; |
| 127 | |
| 128 | return $guid; |
| 129 | } |
| 130 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 131 | sub gen_app { |
| 132 | my ($path, $template, $dir, $ext) = @_; |
| 133 | |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 134 | my $guid = gen_app_guid( $path ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 135 | $path =~ s!/!\\!g; |
| 136 | (my $appname = $path) =~ s/.*\\//; |
| 137 | |
| 138 | my $content = $template; |
| 139 | $content =~ s/<PATHNAME>/$path/g; |
| 140 | $content =~ s/<APPNAME>/$appname/g; |
Manuel Pégourié-Gonnard | 41e8b62 | 2014-05-09 12:27:49 +0200 | [diff] [blame] | 141 | $content =~ s/<GUID>/$guid/g; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 143 | content_to_file( $content, "$dir/$appname.$ext" ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | sub get_app_list { |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 147 | my $app_list = `cd $programs_dir && make list`; |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 148 | die "make list failed: $!\n" if $?; |
| 149 | |
| 150 | return split /\s+/, $app_list; |
| 151 | } |
| 152 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 153 | sub gen_app_files { |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 154 | my @app_list = @_; |
| 155 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 156 | my $vsx_tpl = slurp_file( $vsx_app_tpl_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 158 | for my $app ( @app_list ) { |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 159 | gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext ); |
| 160 | } |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 163 | sub gen_entry_list { |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 164 | my ($tpl, @names) = @_; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 165 | |
| 166 | my $entries; |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 167 | for my $name (@names) { |
| 168 | (my $entry = $tpl) =~ s/{NAME}/$name/g; |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 169 | $entries .= $entry; |
| 170 | } |
| 171 | |
| 172 | return $entries; |
| 173 | } |
| 174 | |
| 175 | sub gen_main_file { |
| 176 | my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_; |
| 177 | |
| 178 | my $header_entries = gen_entry_list( $hdr_tpl, @$headers ); |
| 179 | my $source_entries = gen_entry_list( $src_tpl, @$sources ); |
| 180 | |
| 181 | my $out = slurp_file( $main_tpl ); |
| 182 | $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m; |
| 183 | $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m; |
| 184 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 185 | content_to_file( $out, $main_out ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 188 | sub gen_vsx_solution { |
| 189 | my (@app_names) = @_; |
| 190 | |
| 191 | my ($app_entries, $conf_entries); |
| 192 | for my $path (@app_names) { |
| 193 | my $guid = gen_app_guid( $path ); |
| 194 | (my $appname = $path) =~ s!.*/!!; |
| 195 | |
| 196 | my $app_entry = $vsx_sln_app_entry_tpl; |
| 197 | $app_entry =~ s/{APPNAME}/$appname/g; |
| 198 | $app_entry =~ s/{GUID}/$guid/g; |
| 199 | |
| 200 | $app_entries .= $app_entry; |
| 201 | |
| 202 | my $conf_entry = $vsx_sln_conf_entry_tpl; |
| 203 | $conf_entry =~ s/{GUID}/$guid/g; |
| 204 | |
| 205 | $conf_entries .= $conf_entry; |
| 206 | } |
| 207 | |
| 208 | my $out = slurp_file( $vsx_sln_tpl_file ); |
| 209 | $out =~ s/APP_ENTRIES\r\n/$app_entries/m; |
| 210 | $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m; |
| 211 | |
Manuel Pégourié-Gonnard | 411f73e | 2014-05-09 13:00:18 +0200 | [diff] [blame] | 212 | content_to_file( $out, $vsx_sln_file ); |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Andres Amaya Garcia | 464b149 | 2018-01-11 19:51:27 +0000 | [diff] [blame] | 215 | sub del_vsx_files { |
| 216 | unlink glob "'$vsx_dir/*.$vsx_ext'"; |
| 217 | unlink $vsx_main_file; |
| 218 | unlink $vsx_sln_file; |
| 219 | } |
| 220 | |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 221 | sub main { |
| 222 | if( ! check_dirs() ) { |
| 223 | chdir '..' or die; |
Manuel Pégourié-Gonnard | 813e585 | 2015-01-26 15:42:27 +0000 | [diff] [blame] | 224 | check_dirs or die "Must but run from mbedTLS root or scripts dir\n"; |
Manuel Pégourié-Gonnard | 2d34fe3 | 2014-05-07 17:51:20 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Andres Amaya Garcia | 464b149 | 2018-01-11 19:51:27 +0000 | [diff] [blame] | 227 | # Remove old files to ensure that, for example, project files from deleted |
| 228 | # apps are not kept |
| 229 | del_vsx_files(); |
| 230 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 231 | my @app_list = get_app_list(); |
| 232 | my @headers = <$header_dir/*.h>; |
| 233 | my @sources = <$source_dir/*.c>; |
| 234 | map { s!/!\\!g } @headers; |
| 235 | map { s!/!\\!g } @sources; |
| 236 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 237 | gen_app_files( @app_list ); |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 239 | gen_main_file( \@headers, \@sources, |
Manuel Pégourié-Gonnard | 0aafa5c | 2014-05-08 11:33:30 +0200 | [diff] [blame] | 240 | $vsx_hdr_tpl, $vsx_src_tpl, |
| 241 | $vsx_main_tpl_file, $vsx_main_file ); |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 242 | |
Manuel Pégourié-Gonnard | 0598faf | 2014-05-09 12:56:03 +0200 | [diff] [blame] | 243 | gen_vsx_solution( @app_list ); |
Manuel Pégourié-Gonnard | cd8f844 | 2014-05-08 12:53:58 +0200 | [diff] [blame] | 244 | |
Manuel Pégourié-Gonnard | 1b57878 | 2013-09-16 13:33:42 +0200 | [diff] [blame] | 245 | return 0; |
| 246 | } |