blob: 8d36653b4ca9cc7abd4ef37cb6d118146dcf92ea [file] [log] [blame]
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02001#!/usr/bin/perl
2
Manuel Pégourié-Gonnard50868a72014-05-09 13:01:21 +02003# Generate files for MS Visual Studio:
4# - for VS6: main project (library) file, individual app files, workspace
5# - for VS2010: main file, individual apps, solution file
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02006#
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +00007# Must be run from mbedTLS root or scripts directory.
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02008# Takes no argument.
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +02009
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020010use warnings;
11use strict;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020012use Digest::MD5 'md5_hex';
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020013
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020014my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020015my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020016my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020017my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000018my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020019my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000020my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020021
22my $programs_dir = 'programs';
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023my $header_dir = 'include/mbedtls';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020024my $source_dir = 'library';
25
26# Need windows line endings!
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020027my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020028 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020029EOT
30my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020031 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020032EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020033
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +020034my $vsx_sln_app_entry_tpl = <<EOT;
35Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "{APPNAME}", "{APPNAME}.vcxproj", "{GUID}"\r
36 ProjectSection(ProjectDependencies) = postProject\r
37 {46CF2D25-6A36-4189-B59C-E4815388E554} = {46CF2D25-6A36-4189-B59C-E4815388E554}\r
38 EndProjectSection\r
39EndProject\r
40EOT
41
42my $vsx_sln_conf_entry_tpl = <<EOT;
43 {GUID}.Debug|Win32.ActiveCfg = Debug|Win32\r
44 {GUID}.Debug|Win32.Build.0 = Debug|Win32\r
45 {GUID}.Debug|x64.ActiveCfg = Debug|x64\r
46 {GUID}.Debug|x64.Build.0 = Debug|x64\r
47 {GUID}.Release|Win32.ActiveCfg = Release|Win32\r
48 {GUID}.Release|Win32.Build.0 = Release|Win32\r
49 {GUID}.Release|x64.ActiveCfg = Release|x64\r
50 {GUID}.Release|x64.Build.0 = Release|x64\r
51EOT
52
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020053exit( main() );
54
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020055sub check_dirs {
Manuel Pégourié-Gonnard51f14be2015-05-14 13:04:03 +020056 return -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020057 && -d $header_dir
58 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020059 && -d $programs_dir;
60}
61
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020062sub slurp_file {
63 my ($filename) = @_;
64
65 local $/ = undef;
66 open my $fh, '<', $filename or die "Could not read $filename\n";
67 my $content = <$fh>;
68 close $fh;
69
70 return $content;
71}
72
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +020073sub content_to_file {
74 my ($content, $filename) = @_;
75
76 open my $fh, '>', $filename or die "Could not write to $filename\n";
77 print $fh $content;
78 close $fh;
79}
80
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020081sub gen_app_guid {
82 my ($path) = @_;
83
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +000084 my $guid = md5_hex( "mbedTLS:$path" );
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020085 $guid =~ s/(.{8})(.{4})(.{4})(.{4})(.{12})/\U{$1-$2-$3-$4-$5}/;
86
87 return $guid;
88}
89
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020090sub gen_app {
91 my ($path, $template, $dir, $ext) = @_;
92
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +020093 my $guid = gen_app_guid( $path );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020094 $path =~ s!/!\\!g;
95 (my $appname = $path) =~ s/.*\\//;
96
97 my $content = $template;
98 $content =~ s/<PATHNAME>/$path/g;
99 $content =~ s/<APPNAME>/$appname/g;
Manuel Pégourié-Gonnard41e8b622014-05-09 12:27:49 +0200100 $content =~ s/<GUID>/$guid/g;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200101
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200102 content_to_file( $content, "$dir/$appname.$ext" );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200103}
104
105sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200106 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200107 die "make list failed: $!\n" if $?;
108
109 return split /\s+/, $app_list;
110}
111
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200112sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200113 my @app_list = @_;
114
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200115 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200116
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200117 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200118 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
119 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200120}
121
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200122sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200123 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200124
125 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200126 for my $name (@names) {
127 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200128 $entries .= $entry;
129 }
130
131 return $entries;
132}
133
134sub gen_main_file {
135 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
136
137 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
138 my $source_entries = gen_entry_list( $src_tpl, @$sources );
139
140 my $out = slurp_file( $main_tpl );
141 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
142 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
143
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200144 content_to_file( $out, $main_out );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200145}
146
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200147sub gen_vsx_solution {
148 my (@app_names) = @_;
149
150 my ($app_entries, $conf_entries);
151 for my $path (@app_names) {
152 my $guid = gen_app_guid( $path );
153 (my $appname = $path) =~ s!.*/!!;
154
155 my $app_entry = $vsx_sln_app_entry_tpl;
156 $app_entry =~ s/{APPNAME}/$appname/g;
157 $app_entry =~ s/{GUID}/$guid/g;
158
159 $app_entries .= $app_entry;
160
161 my $conf_entry = $vsx_sln_conf_entry_tpl;
162 $conf_entry =~ s/{GUID}/$guid/g;
163
164 $conf_entries .= $conf_entry;
165 }
166
167 my $out = slurp_file( $vsx_sln_tpl_file );
168 $out =~ s/APP_ENTRIES\r\n/$app_entries/m;
169 $out =~ s/CONF_ENTRIES\r\n/$conf_entries/m;
170
Manuel Pégourié-Gonnard411f73e2014-05-09 13:00:18 +0200171 content_to_file( $out, $vsx_sln_file );
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200172}
173
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200174sub main {
175 if( ! check_dirs() ) {
176 chdir '..' or die;
Manuel Pégourié-Gonnard813e5852015-01-26 15:42:27 +0000177 check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200178 }
179
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200180 my @app_list = get_app_list();
181 my @headers = <$header_dir/*.h>;
182 my @sources = <$source_dir/*.c>;
183 map { s!/!\\!g } @headers;
184 map { s!/!\\!g } @sources;
185
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200186 gen_app_files( @app_list );
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200187
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200188 gen_main_file( \@headers, \@sources,
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200189 $vsx_hdr_tpl, $vsx_src_tpl,
190 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200191
Manuel Pégourié-Gonnard0598faf2014-05-09 12:56:03 +0200192 gen_vsx_solution( @app_list );
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200193
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200194 return 0;
195}