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
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.
23
24use warnings;
25use strict;
26use Digest::MD5 'md5_hex';
27
28my $vsx_dir = "visualc/VS2010";
29my $vsx_ext = "vcxproj";
30my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
31my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
32my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
33my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
34my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
35
36my $programs_dir = 'programs';
37my $mbedtls_header_dir = 'include/mbedtls';
38my $psa_header_dir = 'include/psa';
39my $source_dir = 'library';
40my $test_source_dir = 'tests/src';
41my $test_header_dir = 'tests/include/test';
42my $test_drivers_header_dir = 'tests/include/test/drivers';
43my $test_drivers_source_dir = 'tests/src/drivers';
44
45my @thirdparty_header_dirs = qw(
46    3rdparty/everest/include/everest
47);
48my @thirdparty_source_dirs = qw(
49    3rdparty/everest/library
50    3rdparty/everest/library/kremlib
51    3rdparty/everest/library/legacy
52);
53
54# Directories to add to the include path.
55# Order matters in case there are files with the same name in more than
56# one directory: the compiler will use the first match.
57my @include_directories = qw(
58    include
59    3rdparty/everest/include/
60    3rdparty/everest/include/everest
61    3rdparty/everest/include/everest/vs2010
62    3rdparty/everest/include/everest/kremlib
63    tests/include
64);
65my $include_directories = join(';', map {"../../$_"} @include_directories);
66
67# Directories to add to the include path when building the library, but not
68# when building tests or applications.
69my @library_include_directories = qw(
70    library
71);
72my $library_include_directories =
73  join(';', map {"../../$_"} (@library_include_directories,
74                              @include_directories));
75
76my @excluded_files = qw(
77    3rdparty/everest/library/Hacl_Curve25519.c
78);
79my %excluded_files = ();
80foreach (@excluded_files) { $excluded_files{$_} = 1 }
81
82my $vsx_hdr_tpl = <<EOT;
83    <ClInclude Include="..\\..\\{NAME}" />
84EOT
85my $vsx_src_tpl = <<EOT;
86    <ClCompile Include="..\\..\\{NAME}" />
87EOT
88
89my $vsx_sln_app_entry_tpl = <<EOT;
90Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"
91	ProjectSection(ProjectDependencies) = postProject
92		{46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}
93	EndProjectSection
94EndProject
95EOT
96
97my $vsx_sln_conf_entry_tpl = <<EOT;
98		{GUID}.Debug|Win32.ActiveCfg = Debug|Win32
99		{GUID}.Debug|Win32.Build.0 = Debug|Win32
100		{GUID}.Debug|x64.ActiveCfg = Debug|x64
101		{GUID}.Debug|x64.Build.0 = Debug|x64
102		{GUID}.Release|Win32.ActiveCfg = Release|Win32
103		{GUID}.Release|Win32.Build.0 = Release|Win32
104		{GUID}.Release|x64.ActiveCfg = Release|x64
105		{GUID}.Release|x64.Build.0 = Release|x64
106EOT
107
108exit( main() );
109
110sub check_dirs {
111    foreach my $d (@thirdparty_header_dirs, @thirdparty_source_dirs) {
112        if (not (-d $d)) { return 0; }
113    }
114    return -d $vsx_dir
115        && -d $mbedtls_header_dir
116        && -d $psa_header_dir
117        && -d $source_dir
118        && -d $test_source_dir
119        && -d $test_drivers_source_dir
120        && -d $test_header_dir
121        && -d $test_drivers_header_dir
122        && -d $programs_dir;
123}
124
125sub slurp_file {
126    my ($filename) = @_;
127
128    local $/ = undef;
129    open my $fh, '<:crlf', $filename or die "Could not read $filename\n";
130    my $content = <$fh>;
131    close $fh;
132
133    return $content;
134}
135
136sub content_to_file {
137    my ($content, $filename) = @_;
138
139    open my $fh, '>:crlf', $filename or die "Could not write to $filename\n";
140    print $fh $content;
141    close $fh;
142}
143
144sub gen_app_guid {
145    my ($path) = @_;
146
147    my $guid = md5_hex( "mbedTLS:$path" );
148    $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
149
150    return $guid;
151}
152
153sub gen_app {
154    my ($path, $template, $dir, $ext) = @_;
155
156    my $guid = gen_app_guid( $path );
157    $path =~ s!/!\\!g;
158    (my $appname = $path) =~ s/.*\\//;
159
160    my $srcs = "<ClCompile Include=\"..\\..\\programs\\$path.c\" \/>";
161    if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
162        $appname eq "query_compile_time_config" ) {
163        $srcs .= "\n    <ClCompile Include=\"..\\..\\programs\\test\\query_config.c\" \/>";
164    }
165    if( $appname eq "ssl_client2" or $appname eq "ssl_server2" ) {
166        $srcs .= "\n    <ClCompile Include=\"..\\..\\programs\\ssl\\ssl_test_lib.c\" \/>";
167    }
168
169    my $content = $template;
170    $content =~ s/<SOURCES>/$srcs/g;
171    $content =~ s/<APPNAME>/$appname/g;
172    $content =~ s/<GUID>/$guid/g;
173    $content =~ s/INCLUDE_DIRECTORIES\n/$include_directories/g;
174
175    content_to_file( $content, "$dir/$appname.$ext" );
176}
177
178sub get_app_list {
179    my $makefile_contents = slurp_file('programs/Makefile');
180    $makefile_contents =~ /\n\s*APPS\s*=[\\\s]*(.*?)(?<!\\)[\#\n]/s
181      or die "Cannot find APPS = ... in programs/Makefile\n";
182    return split /(?:\s|\\)+/, $1;
183}
184
185sub gen_app_files {
186    my @app_list = @_;
187
188    my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
189
190    for my $app ( @app_list ) {
191        gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
192    }
193}
194
195sub gen_entry_list {
196    my ($tpl, @names) = @_;
197
198    my $entries;
199    for my $name (@names) {
200        (my $entry = $tpl) =~ s/{NAME}/$name/g;
201        $entries .= $entry;
202    }
203
204    return $entries;
205}
206
207sub gen_main_file {
208    my ($headers, $sources,
209        $hdr_tpl, $src_tpl,
210        $main_tpl, $main_out) = @_;
211
212    my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
213    my $source_entries = gen_entry_list( $src_tpl, @$sources );
214
215    my $out = slurp_file( $main_tpl );
216    $out =~ s/SOURCE_ENTRIES\n/$source_entries/m;
217    $out =~ s/HEADER_ENTRIES\n/$header_entries/m;
218    $out =~ s/INCLUDE_DIRECTORIES\n/$library_include_directories/g;
219
220    content_to_file( $out, $main_out );
221}
222
223sub gen_vsx_solution {
224    my (@app_names) = @_;
225
226    my ($app_entries, $conf_entries);
227    for my $path (@app_names) {
228        my $guid = gen_app_guid( $path );
229        (my $appname = $path) =~ s!.*/!!;
230
231        my $app_entry = $vsx_sln_app_entry_tpl;
232        $app_entry =~ s/{APPNAME}/$appname/g;
233        $app_entry =~ s/{GUID}/$guid/g;
234
235        $app_entries .= $app_entry;
236
237        my $conf_entry = $vsx_sln_conf_entry_tpl;
238        $conf_entry =~ s/{GUID}/$guid/g;
239
240        $conf_entries .= $conf_entry;
241    }
242
243    my $out = slurp_file( $vsx_sln_tpl_file );
244    $out =~ s/APP_ENTRIES\n/$app_entries/m;
245    $out =~ s/CONF_ENTRIES\n/$conf_entries/m;
246
247    content_to_file( $out, $vsx_sln_file );
248}
249
250sub del_vsx_files {
251    unlink glob "'$vsx_dir/*.$vsx_ext'";
252    unlink $vsx_main_file;
253    unlink $vsx_sln_file;
254}
255
256sub main {
257    if( ! check_dirs() ) {
258        chdir '..' or die;
259        check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
260    }
261
262    # Remove old files to ensure that, for example, project files from deleted
263    # apps are not kept
264    del_vsx_files();
265
266    my @app_list = get_app_list();
267    my @header_dirs = (
268                       $mbedtls_header_dir,
269                       $psa_header_dir,
270                       $test_header_dir,
271                       $test_drivers_header_dir,
272                       $source_dir,
273                       @thirdparty_header_dirs,
274                      );
275    my @headers = (map { <$_/*.h> } @header_dirs);
276    my @source_dirs = (
277                       $source_dir,
278                       $test_source_dir,
279                       $test_drivers_source_dir,
280                       @thirdparty_source_dirs,
281                      );
282    my @sources = (map { <$_/*.c> } @source_dirs);
283
284    @headers = grep { ! $excluded_files{$_} } @headers;
285    @sources = grep { ! $excluded_files{$_} } @sources;
286    map { s!/!\\!g } @headers;
287    map { s!/!\\!g } @sources;
288
289    gen_app_files( @app_list );
290
291    gen_main_file( \@headers, \@sources,
292                   $vsx_hdr_tpl, $vsx_src_tpl,
293                   $vsx_main_tpl_file, $vsx_main_file );
294
295    gen_vsx_solution( @app_list );
296
297    return 0;
298}
299