Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / scripts / generate_visualc_files.pl
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 "include_crypto" as an argument that can be either 0 (don't include) or
8 # 1 (include). On by default.
9
10 use warnings;
11 use strict;
12 use Digest::MD5 'md5_hex';
13
14 my $vsx_dir = "visualc/VS2010";
15 my $vsx_ext = "vcxproj";
16 my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
17 my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
18 my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
19 my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
20 my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
21
22 my $include_crypto = 1;
23 if( @ARGV ) {
24     die "Invalid number of arguments" if scalar @ARGV != 1;
25     ($include_crypto) = @ARGV;
26 }
27
28 my $programs_dir = 'programs';
29 my $header_dir = 'include/mbedtls';
30 my $crypto_headers_dir = 'include/psa';
31 my $source_dir = 'library';
32 my $crypto_dir = 'crypto';
33
34 # Need windows line endings!
35 my $include_directories = <<EOT;
36 ../../include\r
37 EOT
38
39 if ($include_crypto) {
40   $include_directories = <<EOT;
41 ../../include;../../crypto/include\r
42 EOT
43 }
44
45 my $vsx_hdr_tpl = <<EOT;
46     <ClInclude Include="..\\..\\{NAME}" />\r
47 EOT
48 my $vsx_src_tpl = <<EOT;
49     <ClCompile Include="..\\..\\{NAME}" />\r
50 EOT
51
52 my $vsx_sln_app_entry_tpl = <<EOT;
53 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
54         ProjectSection(ProjectDependencies) = postProject\r
55                 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
56         EndProjectSection\r
57 EndProject\r
58 EOT
59
60 my $vsx_sln_conf_entry_tpl = <<EOT;
61                 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
62                 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
63                 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
64                 {GUID}.Debug|x64.Build.0 = Debug|x64\r
65                 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
66                 {GUID}.Release|Win32.Build.0 = Release|Win32\r
67                 {GUID}.Release|x64.ActiveCfg = Release|x64\r
68                 {GUID}.Release|x64.Build.0 = Release|x64\r
69 EOT
70
71 exit( main() );
72
73 sub check_dirs {
74     return -d $vsx_dir
75         && -d $header_dir
76         && -d $source_dir
77         && -d $programs_dir
78         && -d $crypto_dir
79         && -d "$crypto_dir/$crypto_headers_dir";
80 }
81
82 sub slurp_file {
83     my ($filename) = @_;
84
85     local $/ = undef;
86     open my $fh, '<', $filename or die "Could not read $filename\n";
87     my $content = <$fh>;
88     close $fh;
89
90     return $content;
91 }
92
93 sub content_to_file {
94     my ($content, $filename) = @_;
95
96     open my $fh, '>', $filename or die "Could not write to $filename\n";
97     print $fh $content;
98     close $fh;
99 }
100
101 sub gen_app_guid {
102     my ($path) = @_;
103
104     my $guid = md5_hex( "mbedTLS:$path" );
105     $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
106
107     return $guid;
108 }
109
110 sub gen_app {
111     my ($path, $template, $dir, $ext) = @_;
112
113     my $guid = gen_app_guid( $path );
114     $path =~ s!/!\\!g;
115     (my $appname = $path) =~ s/.*\\//;
116
117     my $srcs = "\n    <ClCompile Include=\"..\\..\\programs\\$path.c\" \/>\r";
118     if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or
119         $appname eq "query_compile_time_config" ) {
120         $srcs .= "\n    <ClCompile Include=\"..\\..\\programs\\ssl\\query_config.c\" \/>\r";
121     }
122
123     my $content = $template;
124     $content =~ s/<SOURCES>/$srcs/g;
125     $content =~ s/<APPNAME>/$appname/g;
126     $content =~ s/<GUID>/$guid/g;
127     $content =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
128
129     content_to_file( $content, "$dir/$appname.$ext" );
130 }
131
132 sub get_app_list {
133     my $app_list = `cd $programs_dir && make list`;
134     die "make list failed: $!\n" if $?;
135
136     return split /\s+/, $app_list;
137 }
138
139 sub gen_app_files {
140     my @app_list = @_;
141
142     my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
143
144     for my $app ( @app_list ) {
145         gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
146     }
147 }
148
149 sub gen_entry_list {
150     my ($tpl, @names) = @_;
151
152     my $entries;
153     for my $name (@names) {
154         (my $entry = $tpl) =~ s/{NAME}/$name/g;
155         $entries .= $entry;
156     }
157
158     return $entries;
159 }
160
161 sub gen_main_file {
162     my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
163
164     my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
165     my $source_entries = gen_entry_list( $src_tpl, @$sources );
166
167     my $out = slurp_file( $main_tpl );
168     $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
169     $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
170     $out =~ s/INCLUDE_DIRECTORIES\r\n/$include_directories/g;
171
172     content_to_file( $out, $main_out );
173 }
174
175 sub gen_vsx_solution {
176     my (@app_names) = @_;
177
178     my ($app_entries, $conf_entries);
179     for my $path (@app_names) {
180         my $guid = gen_app_guid( $path );
181         (my $appname = $path) =~ s!.*/!!;
182
183         my $app_entry = $vsx_sln_app_entry_tpl;
184         $app_entry =~ s/{APPNAME}/$appname/g;
185         $app_entry =~ s/{GUID}/$guid/g;
186
187         $app_entries .= $app_entry;
188
189         my $conf_entry = $vsx_sln_conf_entry_tpl;
190         $conf_entry =~ s/{GUID}/$guid/g;
191
192         $conf_entries .= $conf_entry;
193     }
194
195     my $out = slurp_file( $vsx_sln_tpl_file );
196     $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
197     $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
198
199     content_to_file( $out, $vsx_sln_file );
200 }
201
202 sub del_vsx_files {
203     unlink glob "'$vsx_dir/*.$vsx_ext'";
204     unlink $vsx_main_file;
205     unlink $vsx_sln_file;
206 }
207
208 sub main {
209     if( ! check_dirs() ) {
210         chdir '..' or die;
211         check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
212     }
213
214     # Remove old files to ensure that, for example, project files from deleted
215     # apps are not kept
216     del_vsx_files();
217
218     my @app_list = get_app_list();
219     my @headers = <$header_dir/*.h>;
220
221     my @sources = ();
222     if ($include_crypto) {
223         @sources = <$crypto_dir/$source_dir/*.c>;
224         foreach my $file (<$source_dir/*.c>) {
225             my $basename = $file; $basename =~ s!.*/!!;
226             push @sources, $file unless -e "$crypto_dir/$source_dir/$basename";
227         }
228         push @headers, <$crypto_dir/$crypto_headers_dir/*.h>;
229     } else {
230          @sources = <$source_dir/*.c>;
231     }
232
233     map { s!/!\\!g } @headers;
234     map { s!/!\\!g } @sources;
235
236     gen_app_files( @app_list );
237
238     gen_main_file( \@headers, \@sources,
239                    $vsx_hdr_tpl, $vsx_src_tpl,
240                    $vsx_main_tpl_file, $vsx_main_file );
241
242     gen_vsx_solution( @app_list );
243
244     return 0;
245 }