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   1932 use strict;
  279         611  
  279         8089  
4 279     279   1426 use warnings;
  279         639  
  279         6327  
5 279     279   1350 use Config;
  279         552  
  279         10610  
6 279     279   1773 use Carp 'confess';
  279         589  
  279         14261  
7 279     279   1828 use File::Basename 'dirname';
  279         650  
  279         217238  
8              
9             # Fields
10             sub source_file {
11 678     678 1 1422 my $self = shift;
12 678 50       1913 if (@_) {
13 0         0 $self->{source_file} = $_[0];
14 0         0 return $self;
15             }
16             else {
17 678         1943 return $self->{source_file};
18             }
19             }
20              
21             sub output_file {
22 447     447 1 2100 my $self = shift;
23 447 50       2524 if (@_) {
24 0         0 $self->{output_file} = $_[0];
25 0         0 return $self;
26             }
27             else {
28 447         1960 return $self->{output_file};
29             }
30             }
31              
32             sub config {
33 1017     1017 1 2527 my $self = shift;
34 1017 50       3378 if (@_) {
35 0         0 $self->{config} = $_[0];
36 0         0 return $self;
37             }
38             else {
39 1017         3719 return $self->{config};
40             }
41             }
42              
43             # Class methods
44             sub new {
45 468     468 1 1794 my $class = shift;
46            
47 468         2723 my $self = {@_};
48              
49 468         1627 bless $self, $class;
50            
51 468         1573 return $self;
52             }
53              
54             # Instance Methods
55             sub create_compile_command {
56 339     339 1 1568 my ($self) = @_;
57            
58 339         2024 my $config = $self->config;
59              
60 339         2233 my $cc = $config->cc;
61 339         1959 my $output_file = $self->output_file;
62 339         1466 my $source_file = $self->source_file;
63            
64 339         2213 my $compile_command_args = $self->create_compile_command_args;
65            
66 339         3074 my @compile_command = ($cc, '-c', '-o', $output_file, @$compile_command_args, $source_file);
67            
68 339         2237 return \@compile_command;
69             }
70              
71             sub create_compile_command_args {
72 339     339 1 1317 my ($self) = @_;
73            
74 339         1094 my $config = $self->config;
75            
76 339         856 my @compile_command_args;
77            
78 339         2124 my $std = $config->std;
79 339 50       2158 if (defined $std) {
80 339         2009 push @compile_command_args, "-std=$std";
81             }
82            
83 339         1702 my $optimize = $config->optimize;
84 339 50       1442 if (defined $optimize) {
85 339         2260 push @compile_command_args, split(/ +/, $optimize);
86             }
87            
88 339         931 push @compile_command_args, @{$config->ccflags};
  339         1533  
89            
90 339         906 push @compile_command_args, @{$config->thread_ccflags};
  339         1585  
91            
92 339         2167 my $output_type = $config->output_type;
93            
94 339 50       1690 if ($output_type eq 'dynamic_lib') {
95 339         864 push @compile_command_args, @{$config->dynamic_lib_ccflags};
  339         1677  
96             }
97            
98             # include directories
99             {
100 339         1230 my @all_include_dirs;
  339         708  
101            
102             # SPVM core native directory
103 339         1830 my $spvm_core_include_dir = $config->spvm_core_include_dir;
104 339         1649 push @all_include_dirs, $spvm_core_include_dir;
105            
106             # Native include directory
107 339         1828 my $native_include_dir = $config->native_include_dir;
108 339 100       1471 if (defined $native_include_dir) {
109 37         87 push @all_include_dirs, $native_include_dir;
110             }
111            
112             # Resource include directories
113 339         2928 my $disable_resource = $config->disable_resource;
114 339 100       2450 unless ($disable_resource) {
115 318         2105 my $resource_names = $config->get_resource_names;
116 318         3043 for my $resource_name (@$resource_names) {
117 8         37 my $resource = $config->get_resource($resource_name);
118 8         29 my $config = $resource->config;
119 8         23 my $resource_include_dir = $config->native_include_dir;
120 8 50       36 if (defined $resource_include_dir) {
121 8         43 push @all_include_dirs, $resource_include_dir;
122             }
123             }
124             }
125            
126             # include directories
127 339         2072 my $include_dirs = $config->include_dirs;
128 339         1442 push @all_include_dirs, @$include_dirs;
129            
130 339         1898 my @all_include_dirs_args = map { "-I$_" } @all_include_dirs;
  418         3165  
131            
132 339         1702 push @compile_command_args, @all_include_dirs_args;
133             }
134            
135 339         1833 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