blob: 2861241a08c64660d99d7f193a8dc6a6669d119b [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é-Gonnard1b578782013-09-16 13:33:42 +020017
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020018my $vsx_dir = "visualc/VS2010";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020019my $vsx_ext = "vcxproj";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020020my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020021my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
22my $vsx_main_file = "$vsx_dir/PolarSSL.$vsx_ext";
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020023
24my $programs_dir = 'programs';
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020025my $header_dir = 'include/polarssl';
26my $source_dir = 'library';
27
28# Need windows line endings!
29my $vs6_file_tpl = <<EOT;
30# Begin Source File\r
31\r
32SOURCE=..\\..\\{FILE}\r
33# End Source File\r
34EOT
35
36my $vsx_hdr_tpl = <<EOT;
37 <ClInclude Include="..\\..\\{FILE}" />\r
38EOT
39my $vsx_src_tpl = <<EOT;
40 <ClCompile Include="..\\..\\{FILE}" />\r
41EOT
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020042
43exit( main() );
44
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020045sub check_dirs {
46 return -d $vs6_dir
47 && -d $vsx_dir
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020048 && -d $header_dir
49 && -d $source_dir
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020050 && -d $programs_dir;
51}
52
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020053sub slurp_file {
54 my ($filename) = @_;
55
56 local $/ = undef;
57 open my $fh, '<', $filename or die "Could not read $filename\n";
58 my $content = <$fh>;
59 close $fh;
60
61 return $content;
62}
63
64sub gen_app {
65 my ($path, $template, $dir, $ext) = @_;
66
67 $path =~ s!/!\\!g;
68 (my $appname = $path) =~ s/.*\\//;
69
70 my $content = $template;
71 $content =~ s/<PATHNAME>/$path/g;
72 $content =~ s/<APPNAME>/$appname/g;
73
74 open my $app_fh, '>', "$dir/$appname.$ext";
75 print $app_fh $content;
76 close $app_fh;
77}
78
79sub get_app_list {
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020080 my $app_list = `cd $programs_dir && make list`;
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020081 die "make list failed: $!\n" if $?;
82
83 return split /\s+/, $app_list;
84}
85
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020086sub gen_app_files {
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020087 my @app_list = @_;
88
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020089 my $vs6_tpl = slurp_file( $vs6_app_tpl_file );
90 my $vsx_tpl = slurp_file( $vsx_app_tpl_file );
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020091
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020092 for my $app ( @app_list ) {
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +020093 gen_app( $app, $vs6_tpl, $vs6_dir, $vs6_ext );
94 gen_app( $app, $vsx_tpl, $vsx_dir, $vsx_ext );
95 }
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +020096}
97
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +020098sub gen_entry_list {
99 my ($tpl, @files) = @_;
100
101 my $entries;
102 for my $file (@files) {
103 (my $entry = $tpl) =~ s/{FILE}/$file/;
104 $entries .= $entry;
105 }
106
107 return $entries;
108}
109
110sub gen_main_file {
111 my ($headers, $sources, $hdr_tpl, $src_tpl, $main_tpl, $main_out) = @_;
112
113 my $header_entries = gen_entry_list( $hdr_tpl, @$headers );
114 my $source_entries = gen_entry_list( $src_tpl, @$sources );
115
116 my $out = slurp_file( $main_tpl );
117 $out =~ s/SOURCE_ENTRIES\r\n/$source_entries/m;
118 $out =~ s/HEADER_ENTRIES\r\n/$header_entries/m;
119
120 open my $fh, '>', $main_out or die;
121 print $fh $out;
122 close $fh;
123}
124
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200125sub main {
126 if( ! check_dirs() ) {
127 chdir '..' or die;
128 check_dirs or die "Must but run from PolarSSL root or scripts dir\n";
129 }
130
Manuel Pégourié-Gonnard0aafa5c2014-05-08 11:33:30 +0200131 my @app_list = get_app_list();
132 my @headers = <$header_dir/*.h>;
133 my @sources = <$source_dir/*.c>;
134 map { s!/!\\!g } @headers;
135 map { s!/!\\!g } @sources;
136
137 print "Generating apps files... ";
138 gen_app_files( @app_list );
139 print "done.\n";
140
141 print "Generating main files... ";
142 gen_main_file( \@headers, \@sources,
143 $vs6_file_tpl, $vs6_file_tpl,
144 $vs6_main_tpl_file, $vs6_main_file );
145 gen_main_file( \@headers, \@sources,
146 $vsx_hdr_tpl, $vsx_src_tpl,
147 $vsx_main_tpl_file, $vsx_main_file );
Manuel Pégourié-Gonnard2d34fe32014-05-07 17:51:20 +0200148 print "done.\n";
Manuel Pégourié-Gonnard1b578782013-09-16 13:33:42 +0200149
150 return 0;
151}