-
Notifications
You must be signed in to change notification settings - Fork 26
/
configure.pl
executable file
·190 lines (165 loc) · 5.95 KB
/
configure.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env perl
package main;
my $root_dir = `pwd | tr -d "\n"`;
my $src_dir = $root_dir.'/src';
my $m = new MakefileGen($root_dir, $src_dir, 'build', 'cpp', 'g++');
$m->generate();
exit 0;
package MakefileGen;
sub new()
{
my $super = shift;
my $root_dir = shift || '.';
my $src_dir = shift || '.';
my $build_dir = shift || 'build';
my $ext = shift || 'cpp';
my $compiler = shift || 'g++';
# Save arguments and init member variables
my $this = {
root_dir => $root_dir,
build_dir => $build_dir,
source_dir => $src_dir,
extension => $ext,
compiler => $compiler,
files => [],
tests => [],
makefile_header => [],
makefile_targets => [],
makefile_footer => [],
};
bless($this, $super);
return $this;
}
sub generate()
{
my $self = shift;
$self->clean_build_directory();
$self->scan_source_directory();
$self->create_makefile_header();
$self->create_makefile_targets();
$self->create_makefile_footer();
$self->write_makefile();
print("Done.\n");
}
sub clean_build_directory()
{
my $self = shift;
if (-d $self->{build_dir})
{
my $cmd = 'rm -Rf '.$self->{build_dir};
system($cmd);
print("Existing build directory removed.\n");
}
}
sub scan_source_directory()
{
my $self = shift;
my @f = glob($self->{source_dir}.'/*.'.$self->{extension});
my $ext = $self->{extension};
my @t = map { `basename $_ .$ext | tr -d '\n'` } @f;
$self->{files} = \@f;
$self->{tests} = \@t;
print("Source directory scanned for test files.\n");
}
sub create_makefile_header()
{
my $self = shift;
$self->{makefile_header} = [];
# Get configuration options
my $CXX = $ENV{CXX} || $self->{compiler};
my $CPPFLAGS = $ENV{CPPFLAGS} || '';
my $CXXFLAGS = $ENV{CXXFLAGS} || '';
# Add command that can be used to recreate the same Makefile
push @{$self->{makefile_header}}, "# Command to regenerate this Makefile:";
push @{$self->{makefile_header}}, "# CXX=$CXX CPPFLAGS=$CPPFLAGS CXXFLAGS=$CXXFLAGS";
push @{$self->{makefile_header}}, "";
# Add directories and executables
push @{$self->{makefile_header}}, "ROOT := ".$self->{root_dir};
push @{$self->{makefile_header}}, "SRC := ".$self->{source_dir};
push @{$self->{makefile_header}}, "BLD := ".$self->{build_dir};
push @{$self->{makefile_header}}, "RT := \$(ROOT)/runtest.pl";
# Add compiler executable if specified
push @{$self->{makefile_header}}, "CXX := $CXX";
# Add preprocessor flags if specified
push @{$self->{makefile_header}}, "CPPFLAGS := $CPPFLAGS";
# Add compilers flags if specified
push @{$self->{makefile_header}}, "CXXFLAGS := $CXXFLAGS";
# Enable colors by default
push @{$self->{makefile_header}}, "export COLORIZED ?= 1";
print("Makefile header generated.\n");
}
sub create_makefile_targets()
{
my $self = shift;
# Add default target
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Default target";
push @{$self->{makefile_targets}}, "all: tests";
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Create build directory";
push @{$self->{makefile_targets}}, "\$(BLD):";
push @{$self->{makefile_targets}}, "\t\@mkdir -p \$(BLD)";
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Run all tests";
push @{$self->{makefile_targets}}, "tests: ".join(' ', @{$self->{tests}});
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Force rebuilding all tests";
push @{$self->{makefile_targets}}, "force: clean all";
# Add test targets
for (my $i = 0; $i < scalar @{$self->{tests}}; $i++)
{
my $test = $self->{tests}->[$i];
my $filename = $self->{files}->[$i];
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Build rules for $test";
push @{$self->{makefile_targets}}, "\$(BLD)/$test.o: \$(SRC)/$test.cpp | \$(BLD)";
push @{$self->{makefile_targets}}, "\t\@\$(RT) $test \$(BLD)/$test.log \\";
push @{$self->{makefile_targets}}, "\t\t\$(CXX) -c \$(CPPFLAGS) \$(CXXFLAGS) -o \\";
push @{$self->{makefile_targets}}, "\t\t\$(BLD)/$test.o \$(SRC)/$test.cpp";
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "$test: \$(BLD)/$test.o";
}
# Add additional targets
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Phony targets";
push @{$self->{makefile_targets}}, ".PHONY: all clean distclean help";
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Clean up test files";
push @{$self->{makefile_targets}}, "clean:";
push @{$self->{makefile_targets}}, "\t\@rm -f \$(BLD)/*.o \$(BLD)/*.log";
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Clean up everything";
push @{$self->{makefile_targets}}, "distclean: clean";
push @{$self->{makefile_targets}}, "\t\@rm -f Makefile";
push @{$self->{makefile_targets}}, "";
push @{$self->{makefile_targets}}, "# Show targets that can be built";
push @{$self->{makefile_targets}}, "help:";
push @{$self->{makefile_targets}}, "\t\@echo 'The following targets can be built:'";
push @{$self->{makefile_targets}}, "\t\@echo ' all (subault)'";
push @{$self->{makefile_targets}}, "\t\@echo ' tests'";
push @{$self->{makefile_targets}}, "\t\@echo ' force'";
push @{$self->{makefile_targets}}, "\t\@echo ' clean'";
push @{$self->{makefile_targets}}, "\t\@echo ' distclean'";
push @{$self->{makefile_targets}}, "\t\@echo ' help'";
foreach my $test (@{$self->{tests}})
{
push @{$self->{makefile_targets}}, "\t\@echo ' $test'";
}
print("Makefile targets generated.\n");
}
sub create_makefile_footer()
{
print("Makefile footer generated.\n")
}
sub write_makefile()
{
my $self = shift;
open F, "> Makefile" or die "open Makefile: $!";
print F join("\n", @{$self->{makefile_header}});
print F "\n";
print F join("\n", @{$self->{makefile_targets}});
print F "\n";
print F join("\n", @{$self->{makefile_footer}});
close F;
print("Makefile written to disk.\n")
}