File Coverage

blib/lib/SPVM/Builder/CompileInfo.pm
Criterion Covered Total %
statement 75 85 88.2
branch 11 18 61.1
condition n/a
subroutine 11 12 91.6
pod 7 7 100.0
total 104 122 85.2


line stmt bran cond sub pod time code
1             package SPVM::Builder::CompileInfo;
2              
3 279     279   1908 use strict;
  279         567  
  279         8022  
4 279     279   1410 use warnings;
  279         550  
  279         6326  
5 279     279   1346 use Config;
  279         513  
  279         10439  
6 279     279   1618 use Carp 'confess';
  279         555  
  279         13785  
7 279     279   1777 use File::Basename 'dirname';
  279         629  
  279         206600  
8              
9             # Fields
10             sub source_file {
11 678     678 1 1594 my $self = shift;
12 678 50       2022 if (@_) {
13 0         0 $self->{source_file} = $_[0];
14 0         0 return $self;
15             }
16             else {
17 678         2097 return $self->{source_file};
18             }
19             }
20              
21             sub output_file {
22 447     447 1 2064 my $self = shift;
23 447 50       2418 if (@_) {
24 0         0 $self->{output_file} = $_[0];
25 0         0 return $self;
26             }
27             else {
28 447         2083 return $self->{output_file};
29             }
30             }
31              
32             sub config {
33 1017     1017 1 2469 my $self = shift;
34 1017 50       3103 if (@_) {
35 0         0 $self->{config} = $_[0];
36 0         0 return $self;
37             }
38             else {
39 1017         3457 return $self->{config};
40             }
41             }
42              
43             # Class methods
44             sub new {
45 480     480 1 1772 my $class = shift;
46            
47 480         2626 my $self = {@_};
48              
49 480         1553 bless $self, $class;
50            
51 480         1520 return $self;
52             }
53              
54             # Instance Methods
55             sub create_compile_command {
56 339     339 1 1418 my ($self) = @_;
57            
58 339         2132 my $config = $self->config;
59              
60 339         2563 my $cc = $config->cc;
61 339         1966 my $output_file = $self->output_file;
62 339         1328 my $source_file = $self->source_file;
63            
64 339         1807 my $compile_command_args = $self->create_compile_command_args;
65            
66 339         3163 my @compile_command = ($cc, '-c', '-o', $output_file, @$compile_command_args, $source_file);
67            
68 339         1845 return \@compile_command;
69             }
70              
71             sub create_compile_command_args {
72 339     339 1 1116 my ($self) = @_;
73            
74 339         1000 my $config = $self->config;
75            
76 339         817 my @compile_command_args;
77            
78 339         2041 my $std = $config->std;
79 339 50       1667 if (defined $std) {
80 339         1889 push @compile_command_args, "-std=$std";
81             }
82            
83 339         1764 my $optimize = $config->optimize;
84 339 50       1401 if (defined $optimize) {
85 339         2093 push @compile_command_args, split(/ +/, $optimize);
86             }
87            
88 339         941 push @compile_command_args, @{$config->ccflags};
  339         1659  
89            
90 339         839 push @compile_command_args, @{$config->thread_ccflags};
  339         1632  
91            
92 339         1895 my $output_type = $config->output_type;
93            
94 339 50       1529 if ($output_type eq 'dynamic_lib') {
95 339         714 push @compile_command_args, @{$config->dynamic_lib_ccflags};
  339         1532  
96             }
97            
98             # include directories
99             {
100 339         791 my @all_include_dirs;
  339         736  
101            
102             # SPVM core native directory
103 339         1626 my $spvm_core_include_dir = $config->spvm_core_include_dir;
104 339         1777 push @all_include_dirs, $spvm_core_include_dir;
105            
106             # Native include directory
107 339         1631 my $native_include_dir = $config->native_include_dir;
108 339 100       1538 if (defined $native_include_dir) {
109 37         110 push @all_include_dirs, $native_include_dir;
110             }
111            
112             # Resource include directories
113 339         2805 my $disable_resource = $config->disable_resource;
114 339 100       3360 unless ($disable_resource) {
115 318         2541 my $resource_names = $config->get_resource_names;
116 318         2853 for my $resource_name (@$resource_names) {
117 8         52 my $resource = $config->get_resource($resource_name);
118 8         42 my $config = $resource->config;
119 8         27 my $resource_include_dir = $config->native_include_dir;
120 8 50       37 if (defined $resource_include_dir) {
121 8         44 push @all_include_dirs, $resource_include_dir;
122             }
123             }
124             }
125            
126             # include directories
127 339         2276 my $include_dirs = $config->include_dirs;
128 339         1287 push @all_include_dirs, @$include_dirs;
129            
130 339         1430 my @all_include_dirs_args = map { "-I$_" } @all_include_dirs;
  418         2981  
131            
132 339         1722 push @compile_command_args, @all_include_dirs_args;
133             }
134            
135 339         1670 return \@compile_command_args;
136             }
137              
138             sub to_cmd {
139 0     0 1   my ($self) = @_;
140              
141 0           my $compile_command = $self->create_compile_command;
142 0           my $compile_command_string = "@$compile_command";
143            
144 0           return $compile_command_string;
145             }
146              
147             1;
148              
149             =head1 Name
150              
151             SPVM::Builder::CompileInfo - Compilation Information
152              
153             =head1 Description
154              
155             The SPVM::Builder::CompileInfo class has methods to manipulate compilation information.
156              
157             =head1 Fields
158              
159             =head2 config
160              
161             my $config = $compile_info->config;
162             $compile_info->config($config);
163              
164             Gets and sets the C field.
165              
166             This is a L object used to compile the source file.
167              
168             =head2 source_file
169              
170             my $source_file = $compile_info->source_file;
171             $compile_info->source_file($source_file);
172              
173             Gets and sets the C field.
174              
175             This field is a source file.
176              
177             =head2 output_file
178              
179             my $output_file = $compile_info->output_file;
180             $compile_info->output_file($output_file);
181              
182             Gets and sets the C field.
183              
184             This field is an output file.
185              
186             =head1 Class Methods
187              
188             =head2 new
189              
190             my $compile_info = SPVM::Builder::CompileInfo->new(%fields);
191              
192             Creates a new L object with L.
193              
194             Default Field Values:
195              
196             If a field is not defined, the field is set to the following default value.
197              
198             =over 2
199              
200             =item * L
201              
202             undef
203              
204             =item * L
205              
206             undef
207              
208             =item * L
209              
210             undef
211              
212             =back
213              
214             =head1 Instance Methods
215              
216             =head2 create_compile_command
217              
218             my $compile_command = $compile_info->create_compile_command;
219              
220             Creates the compilation command, and returns it.
221              
222             The return value is an array reference.
223              
224             The following one is an example of the return value.
225              
226             [qw(cc -o foo.o -c -O2 -Ipath/include foo.c)]
227              
228             =head2 create_compile_command_args
229              
230             my $config_args = $compile_info->create_compile_command_args;
231              
232             Creates the parts of the arguments of the compilation command from the information of the L field, and returns it. The return value is an array reference.
233              
234             The C<-c> option, the C<-o> option and the source file name are not contained.
235              
236             The following one is an example of the return value.
237              
238             [qw(-O2 -Ipath/include)]
239              
240             =head2 to_cmd
241              
242             my $compile_command_string = $compile_info->to_cmd;
243              
244             Calls the L method and joins all elements of the returned array reference with a space, and returns it.
245              
246             The following one is an example of the return value.
247              
248             "cc -c -o foo.o -O2 -Ipath/include foo.c"
249              
250             =head1 Copyright & License
251              
252             Copyright (c) 2023 Yuki Kimoto
253              
254             MIT License