blob: 5f2f944128e6ffa4a2fe24b58fa5dde830b01341 [file] [log] [blame]
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02001#!/usr/bin/perl
2
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +02003# create individual project files for example programs
4# for VS6 and VS2010
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +02005#
6# Must be run from PolarSSL root or scripts directory.
7# Takes no argument.
Manuel Pégourié-Gonnard684e9dc2013-09-20 15:11:44 +02008
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +02009use warnings;
10use strict;
11
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020012my $vs6_dir = "visualc/VS6";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020013my $vs6_ext = "dsp";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020014my $vs6_app_tpl_file = "scripts/data_files/vs6-app-template.$vs6_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020015my $vs6_main_tpl_file = "scripts/data_files/vs6-main-template.$vs6_ext";
16my $vs6_main_file = "$vs6_dir/polarssl.$vs6_ext";
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020017my $vs6_wsp_tpl_file = "scripts/data_files/vs6-workspace-template.dsw";
18my $vs6_wsp_file = "$vs6_dir/polarssl.dsw";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020019
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020020my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020021my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020022my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020023my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
24my $vsx_main_file = "$vsx_dir/PolarSSL.$vsx_ext";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020025
26my $programs_dir = 'programs';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020027my $header_dir = 'include/polarssl';
28my $source_dir = 'library';
29
30# Need windows line endings!
31my $vs6_file_tpl = <<EOT;
32# Begin Source File\r
33\r
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020034SOURCE=..\\..\\{NAME}\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020035# End Source File\r
36EOT
37
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020038my $vs6_wsp_entry_tpl = <<EOT;
39###############################################################################\r
40\r
41Project: "{NAME}"=.\\{NAME}.dsp - Package Owner=<4>\r
42\r
43Package=<5>\r
44{{{\r
45}}}\r
46\r
47Package=<4>\r
48{{{\r
49 Begin Project Dependency\r
50 Project_Dep_Name polarssl\r
51 End Project Dependency\r
52}}}\r
53\r
54EOT
55
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020056my $vsx_hdr_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020057 <ClInclude Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020058EOT
59my $vsx_src_tpl = <<EOT;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +020060 <ClCompile Include="..\\..\\{NAME}" />\r
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020061EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020062
63exit( main() );
64
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020065sub check_dirs {
66 return -d $vs6_dir
67 && -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020068 && -d $header_dir
69 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020070 && -d $programs_dir;
71}
72
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020073sub slurp_file {
74 my ($filename) = @_;
75
76 local $/ = undef;
77 open my $fh, '<', $filename or die "Could not read $filename\n";
78 my $content = <$fh>;
79 close $fh;
80
81 return $content;
82}
83
84sub gen_app {
85 my ($path, $template, $dir, $ext) = @_;
86
87 $path =~ s!/!\\!g;
88 (my $appname = $path) =~ s/.*\\//;
89
90 my $content = $template;
91 $content =~ s/<PATHNAME>/$path/g;
92 $content =~ s/<APPNAME>/$appname/g;
93
94 open my $app_fh, '>', "$dir/$appname.$ext";
95 print $app_fh $content;
96 close $app_fh;
97}
98
99sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200100 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200101 die "make list failed: $!\n" if $?;
102
103 return split /\s+/, $app_list;
104}
105
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200106sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200107 my @app_list = @_;
108
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200109 my $vs6_tpl = slurp_file( $vs6_app_tpl_file );
110 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200111
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200112 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200113 gen_app( $app, $vs6_tpl, $vs6_dir, $vs6_ext );
114 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
115 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200116}
117
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200118sub gen_entry_list {
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200119 my ($tpl, @names) = @_;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200120
121 my $entries;
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200122 for my $name (@names) {
123 (my $entry = $tpl) =~ s/{NAME}/$name/g;
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200124 $entries .= $entry;
125 }
126
127 return $entries;
128}
129
130sub gen_main_file {
131 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
132
133 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
134 my $source_entries = gen_entry_list( $src_tpl, @$sources );
135
136 my $out = slurp_file( $main_tpl );
137 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
138 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
139
140 open my $fh, '>', $main_out or die;
141 print $fh $out;
142 close $fh;
143}
144
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200145sub gen_vs6_workspace {
146 my (@app_names) = @_;
147
148 map { s!.*/!! } @app_names;
149 my $entries = gen_entry_list( $vs6_wsp_entry_tpl, @app_names );
150
151 my $out = slurp_file( $vs6_wsp_tpl_file );
152 $out =~ s/APP_ENTRIES\r\n/$entries/m;
153
154 open my $fh, '>', $vs6_wsp_file or die;
155 print $fh $out;
156 close $fh;
157}
158
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200159sub main {
160 if( ! check_dirs() ) {
161 chdir '..' or die;
162 check_dirs or die "Must but run from PolarSSL root or scripts dir\n";
163 }
164
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200165 my @app_list = get_app_list();
166 my @headers = <$header_dir/*.h>;
167 my @sources = <$source_dir/*.c>;
168 map { s!/!\\!g } @headers;
169 map { s!/!\\!g } @sources;
170
171 print "Generating apps files... ";
172 gen_app_files( @app_list );
173 print "done.\n";
174
175 print "Generating main files... ";
176 gen_main_file( \@headers, \@sources,
177 $vs6_file_tpl, $vs6_file_tpl,
178 $vs6_main_tpl_file, $vs6_main_file );
179 gen_main_file( \@headers, \@sources,
180 $vsx_hdr_tpl, $vsx_src_tpl,
181 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200182 print "done.\n";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200183
Manuel Pégourié-Gonnardcd8f8442014-05-08 12:53:58 +0200184 print "Generating VS6 workspace file... ";
185 gen_vs6_workspace( @app_list );
186 print "done.\n";
187
188
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200189 return 0;
190}