| 1 |
!> Define tests for the `fpm_compiler` module |
| 2 |
module test_compiler |
| 3 |
use testsuite, only : new_unittest, unittest_t, error_t, test_failed, & |
| 4 |
& check_string |
| 5 |
use fpm_environment, only : OS_WINDOWS, OS_LINUX |
| 6 |
use fpm_compiler , only : compiler_t, new_compiler, tokenize_flags |
| 7 |
use fpm_strings , only : string_t, operator(==) |
| 8 |
use fpm_command_line, only: get_fpm_env |
| 9 |
use fpm_compile_commands, only: compile_command_table_t |
| 10 |
implicit none |
| 11 |
private |
| 12 |
|
| 13 |
public :: collect_compiler |
| 14 |
|
| 15 |
|
| 16 |
contains |
| 17 |
|
| 18 |
!> Collect all exported unit tests |
| 19 |
subroutine collect_compiler(testsuite) |
| 20 |
!> Collection of tests |
| 21 |
type(unittest_t), allocatable, intent(out) :: testsuite(:) |
| 22 |
|
| 23 |
testsuite = [ & |
| 24 |
& new_unittest("check-fortran-source-runs", test_check_fortran_source_runs), & |
| 25 |
& new_unittest("check-c-source-runs", test_check_c_source_runs), & |
| 26 |
& new_unittest("check-cxx-source-runs", test_check_cxx_source_runs), & |
| 27 |
& new_unittest("tokenize-flags", test_tokenize_flags), & |
| 28 |
& new_unittest("compile-commands-unix", test_register_compile_command_unix), & |
| 29 |
& new_unittest("compile-commands-windows", test_register_compile_command_windows), & |
| 30 |
& new_unittest("get-default-flags-pic", test_get_default_flags_pic)] |
| 31 |
|
| 32 |
end subroutine collect_compiler |
| 33 |
|
| 34 |
subroutine test_check_fortran_source_runs(error) |
| 35 |
!> Error handling |
| 36 |
type(error_t), allocatable, intent(out) :: error |
| 37 |
|
| 38 |
character(:), allocatable :: fc,cc,cxx |
| 39 |
|
| 40 |
|
| 41 |
type(compiler_t) :: compiler |
| 42 |
|
| 43 |
!> Get default compiler |
| 44 |
fc = get_fpm_env("FC", default="gfortran") |
| 45 |
cc = get_fpm_env("CC", default=" ") |
| 46 |
cxx = get_fpm_env("CXX", default=" ") |
| 47 |
|
| 48 |
call new_compiler(compiler, fc, cc, cxx, echo=.false., verbose=.false.) |
| 49 |
|
| 50 |
if (compiler%is_unknown()) then |
| 51 |
call test_failed(error, "Cannot initialize Fortran compiler") |
| 52 |
return |
| 53 |
end if |
| 54 |
|
| 55 |
!> Test fortran-source runs |
| 56 |
if (.not.compiler%check_fortran_source_runs("print *, 'Hello world!'; end")) then |
| 57 |
call test_failed(error, "Cannot run Fortran hello world") |
| 58 |
return |
| 59 |
end if |
| 60 |
|
| 61 |
!> Test with invalid flags |
| 62 |
if (compiler%check_fortran_source_runs("print *, 'Hello world!'; end", & |
| 63 |
link_flags=" -some-really-invalid-link-flag")) then |
| 64 |
call test_failed(error, "Invalid link flags did not trigger an error") |
| 65 |
return |
| 66 |
end if |
| 67 |
if (compiler%check_fortran_source_runs("print *, 'Hello world!'; end", & |
| 68 |
compile_flags=" -certainly-not-a-build/flag")) then |
| 69 |
call test_failed(error, "Invalid compile flags did not trigger an error") |
| 70 |
return |
| 71 |
end if |
| 72 |
if (compiler%check_fortran_source_runs("print *, 'Hello world!'; end", & |
| 73 |
compile_flags=" -certainly-not-a-build/flag", & |
| 74 |
link_flags=" -some-really-invalid-link-flag")) then |
| 75 |
call test_failed(error, "Invalid build and link flags did not trigger an error") |
| 76 |
return |
| 77 |
end if |
| 78 |
|
| 79 |
!> Test the flag check wrapper |
| 80 |
if (compiler%check_flags_supported(compile_flags='-Werror=unknown-flag') & |
| 81 |
.and. .not. compiler%is_intel()) then ! Intel will not trigger an error |
| 82 |
call test_failed(error, "Invalid compile flags did not trigger an error") |
| 83 |
return |
| 84 |
end if |
| 85 |
if (compiler%check_flags_supported(compile_flags='-not-a-compile-flag')) then |
| 86 |
call test_failed(error, "Invalid compile flags did not trigger an error") |
| 87 |
return |
| 88 |
end if |
| 89 |
if (compiler%check_flags_supported(link_flags='-Wl,--nonexistent-linker-option')) then |
| 90 |
call test_failed(error, "Invalid link flags did not trigger an error") |
| 91 |
return |
| 92 |
end if |
| 93 |
if (compiler%check_flags_supported(compile_flags='-Werror=eunknown-flag', & |
| 94 |
link_flags='-Wl,--nonexistent-linker-option')) then |
| 95 |
call test_failed(error, "Invalid compile and link flags did not trigger an error") |
| 96 |
return |
| 97 |
end if |
| 98 |
|
| 99 |
end subroutine test_check_fortran_source_runs |
| 100 |
|
| 101 |
subroutine test_check_c_source_runs(error) |
| 102 |
!> Error handling |
| 103 |
type(error_t), allocatable, intent(out) :: error |
| 104 |
|
| 105 |
character(:), allocatable :: fc,cc,cxx |
| 106 |
type(compiler_t) :: compiler |
| 107 |
|
| 108 |
!> Get default compiler |
| 109 |
fc = get_fpm_env("FC", default="gfortran") |
| 110 |
cc = get_fpm_env("CC", default=" ") |
| 111 |
cxx = get_fpm_env("CXX", default=" ") |
| 112 |
|
| 113 |
call new_compiler(compiler, fc, cc, cxx, echo=.false., verbose=.false.) |
| 114 |
|
| 115 |
if (compiler%is_unknown()) then |
| 116 |
call test_failed(error, "Cannot initialize compiler") |
| 117 |
return |
| 118 |
end if |
| 119 |
|
| 120 |
!> Skip tests if no C compiler is available |
| 121 |
if (len_trim(compiler%cc) == 0) then |
| 122 |
return |
| 123 |
end if |
| 124 |
|
| 125 |
!> Test C source runs with simple hello world |
| 126 |
if (.not.compiler%check_c_source_runs( & |
| 127 |
'#include <stdio.h>' // new_line('a') // & |
| 128 |
'int main() { printf("Hello C world!"); return 0; }')) then |
| 129 |
call test_failed(error, "Cannot run C hello world with compiler "//compiler%cc) |
| 130 |
return |
| 131 |
end if |
| 132 |
|
| 133 |
!> Test with invalid source that should fail |
| 134 |
if (compiler%check_c_source_runs( & |
| 135 |
'#include <stdio.h>' // new_line('a') // & |
| 136 |
'int main() { return 1; }')) then ! Returns error code 1 |
| 137 |
call test_failed(error, "C program returning error code 1 did not fail") |
| 138 |
return |
| 139 |
end if |
| 140 |
|
| 141 |
!> Test with invalid flags |
| 142 |
if (compiler%check_c_source_runs( & |
| 143 |
'#include <stdio.h>' // new_line('a') // & |
| 144 |
'int main() { return 0; }', & |
| 145 |
compile_flags=" -invalid-c-flag")) then |
| 146 |
call test_failed(error, "Invalid C compile flags did not trigger an error") |
| 147 |
return |
| 148 |
end if |
| 149 |
|
| 150 |
!> Test the C flag check wrapper |
| 151 |
if (compiler%check_c_flags_supported(compile_flags='-not-a-c-flag')) then |
| 152 |
call test_failed(error, "Invalid C compile flags did not trigger an error") |
| 153 |
return |
| 154 |
end if |
| 155 |
|
| 156 |
end subroutine test_check_c_source_runs |
| 157 |
|
| 158 |
subroutine test_check_cxx_source_runs(error) |
| 159 |
!> Error handling |
| 160 |
type(error_t), allocatable, intent(out) :: error |
| 161 |
|
| 162 |
character(:), allocatable :: fc,cc,cxx |
| 163 |
type(compiler_t) :: compiler |
| 164 |
|
| 165 |
!> Get default compiler |
| 166 |
fc = get_fpm_env("FC", default="gfortran") |
| 167 |
cc = get_fpm_env("CC", default=" ") |
| 168 |
cxx = get_fpm_env("CXX", default=" ") |
| 169 |
|
| 170 |
call new_compiler(compiler, fc, cc, cxx, echo=.false., verbose=.false.) |
| 171 |
|
| 172 |
if (compiler%is_unknown()) then |
| 173 |
call test_failed(error, "Cannot initialize compiler") |
| 174 |
return |
| 175 |
end if |
| 176 |
|
| 177 |
!> Skip tests if no C++ compiler is available or if it's set to a space |
| 178 |
if (len_trim(compiler%cxx) == 0 .or. trim(compiler%cxx) == " ") then |
| 179 |
return |
| 180 |
end if |
| 181 |
|
| 182 |
!> Test C++ source runs with simple hello world |
| 183 |
!> Only fail if we're sure the compiler is available |
| 184 |
if (.not.compiler%check_cxx_source_runs( & |
| 185 |
'#include <cstdio>' // new_line('a') // & |
| 186 |
'int main() { return 0; }')) then |
| 187 |
!> This might fail if C++ compiler is misconfigured, so just skip further tests |
| 188 |
return |
| 189 |
end if |
| 190 |
|
| 191 |
!> Test with invalid source that should fail |
| 192 |
if (compiler%check_cxx_source_runs( & |
| 193 |
'#include <cstdio>' // new_line('a') // & |
| 194 |
'int main() { return 1; }')) then ! Returns error code 1 |
| 195 |
call test_failed(error, "C++ program returning error code 1 did not fail") |
| 196 |
return |
| 197 |
end if |
| 198 |
|
| 199 |
!> Test with invalid flags |
| 200 |
if (compiler%check_cxx_source_runs( & |
| 201 |
'#include <cstdio>' // new_line('a') // & |
| 202 |
'int main() { return 0; }', & |
| 203 |
compile_flags=" -invalid-cxx-flag")) then |
| 204 |
call test_failed(error, "Invalid C++ compile flags did not trigger an error") |
| 205 |
return |
| 206 |
end if |
| 207 |
|
| 208 |
!> Test the C++ flag check wrapper |
| 209 |
if (compiler%check_cxx_flags_supported(compile_flags='-not-a-cxx-flag')) then |
| 210 |
call test_failed(error, "Invalid C++ compile flags did not trigger an error") |
| 211 |
return |
| 212 |
end if |
| 213 |
|
| 214 |
end subroutine test_check_cxx_source_runs |
| 215 |
|
| 216 |
subroutine test_tokenize_flags(error) |
| 217 |
type(error_t), allocatable, intent(out) :: error |
| 218 |
|
| 219 |
character(:), allocatable :: flags |
| 220 |
type(string_t), allocatable :: tokens(:) |
| 221 |
integer :: i |
| 222 |
|
| 223 |
flags = '-I/path/to/include -I /test -I"/path/to/include with spaces" ' // & |
| 224 |
'-I "spaces here too" -L/path/to/lib -lmylib -O2 -g -Wall' |
| 225 |
call tokenize_flags(flags, tokens) |
| 226 |
|
| 227 |
do i = 1, size(tokens) |
| 228 |
print *, "Tokens ", i, ": ", tokens(i)%s |
| 229 |
end do |
| 230 |
|
| 231 |
if (tokens(1)%s /= '-I/path/to/include') then |
| 232 |
call test_failed(error, "Tokenization of flags failed: expected '-I/path/to/include'") |
| 233 |
return |
| 234 |
end if |
| 235 |
if (tokens(2)%s /= '-I/test') then |
| 236 |
call test_failed(error, "Tokenization of flags failed: expected '-I/test'") |
| 237 |
return |
| 238 |
end if |
| 239 |
if (tokens(3)%s /= '-I"/path/to/include with spaces"') then |
| 240 |
call test_failed(error, 'Tokenization of flags failed: expected -I"/path/to/include with spaces"') |
| 241 |
return |
| 242 |
end if |
| 243 |
|
| 244 |
if (size(tokens) /= 9) then |
| 245 |
call test_failed(error, "Tokenization of flags failed: expected 9 tokens") |
| 246 |
return |
| 247 |
end if |
| 248 |
|
| 249 |
end subroutine test_tokenize_flags |
| 250 |
|
| 251 |
subroutine test_register_compile_command_unix(error) |
| 252 |
type(error_t), allocatable, intent(out) :: error |
| 253 |
|
| 254 |
type(compile_command_table_t) :: table |
| 255 |
type(string_t), allocatable :: expected(:) |
| 256 |
integer :: i |
| 257 |
|
| 258 |
call table%register('gfortran -c -I/usr/include -O2 -Wall main.f90', OS_LINUX, error) |
| 259 |
if (allocated(error)) return |
| 260 |
|
| 261 |
if (size(table%command) /= 1) then |
| 262 |
call test_failed(error, "Expected 1 command registered") |
| 263 |
return |
| 264 |
end if |
| 265 |
|
| 266 |
associate(c => table%command(1)) |
| 267 |
! Expect these arguments in order |
| 268 |
expected = [ string_t('gfortran'), string_t('-c'), & |
| 269 |
string_t('-I/usr/include'), string_t('-O2'), & |
| 270 |
string_t('-Wall'), string_t('main.f90') ] |
| 271 |
|
| 272 |
if (.not. c%arguments == expected) then |
| 273 |
do i = 1, size(c%arguments) |
| 274 |
print *, "Argument", i, ":", c%arguments(i)%s |
| 275 |
end do |
| 276 |
call test_failed(error, "Unix compile command arguments did not match expected tokens") |
| 277 |
return |
| 278 |
end if |
| 279 |
end associate |
| 280 |
end subroutine test_register_compile_command_unix |
| 281 |
|
| 282 |
subroutine test_register_compile_command_windows(error) |
| 283 |
type(error_t), allocatable, intent(out) :: error |
| 284 |
|
| 285 |
type(compile_command_table_t) :: table |
| 286 |
type(string_t), allocatable :: expected(:) |
| 287 |
integer :: i |
| 288 |
|
| 289 |
call table%register('ifort /c /I"C:\Program Files\Libs" /O2 /W4 main.f90', OS_WINDOWS, error) |
| 290 |
if (allocated(error)) return |
| 291 |
|
| 292 |
if (size(table%command) /= 1) then |
| 293 |
call test_failed(error, "Expected 1 command registered") |
| 294 |
return |
| 295 |
end if |
| 296 |
|
| 297 |
associate(c => table%command(1)) |
| 298 |
! Expected Windows-style tokens |
| 299 |
expected = [ string_t('ifort'), string_t('/c'), & |
| 300 |
string_t('/IC:\Program Files\Libs'), string_t('/O2'), & |
| 301 |
string_t('/W4'), string_t('main.f90') ] |
| 302 |
|
| 303 |
if (.not. c%arguments == expected) then |
| 304 |
do i = 1, size(c%arguments) |
| 305 |
print *, "Argument", i, ":", c%arguments(i)%s |
| 306 |
end do |
| 307 |
call test_failed(error, "Windows compile command arguments did not match expected tokens") |
| 308 |
return |
| 309 |
end if |
| 310 |
end associate |
| 311 |
end subroutine test_register_compile_command_windows |
| 312 |
|
| 313 |
subroutine test_get_default_flags_pic(error) |
| 314 |
!> Error handling |
| 315 |
type(error_t), allocatable, intent(out) :: error |
| 316 |
|
| 317 |
type(compiler_t) :: compiler |
| 318 |
character(:), allocatable :: flags |
| 319 |
integer :: first_pos, second_pos |
| 320 |
|
| 321 |
!> Create a gfortran compiler |
| 322 |
call new_compiler(compiler, "gfortran", "gcc", "g++", echo=.false., verbose=.false.) |
| 323 |
|
| 324 |
if (compiler%is_unknown()) then |
| 325 |
call test_failed(error, "Cannot initialize gfortran compiler for PIC test") |
| 326 |
return |
| 327 |
end if |
| 328 |
|
| 329 |
!> Test that -fPIC is included in release flags |
| 330 |
flags = compiler%get_default_flags(release=.true.) |
| 331 |
first_pos = index(flags, "-fPIC") |
| 332 |
if (first_pos == 0) then |
| 333 |
call test_failed(error, "Release flags should contain -fPIC, got: "//flags) |
| 334 |
return |
| 335 |
end if |
| 336 |
|
| 337 |
!> Test that -fPIC is not duplicated in release flags |
| 338 |
second_pos = index(flags(first_pos+5:), "-fPIC") |
| 339 |
if (second_pos /= 0) then |
| 340 |
call test_failed(error, "Release flags should not contain duplicate -fPIC, got: "//flags) |
| 341 |
return |
| 342 |
end if |
| 343 |
|
| 344 |
!> Test that -fPIC is included in debug flags |
| 345 |
flags = compiler%get_default_flags(release=.false.) |
| 346 |
first_pos = index(flags, "-fPIC") |
| 347 |
if (first_pos == 0) then |
| 348 |
call test_failed(error, "Debug flags should contain -fPIC, got: "//flags) |
| 349 |
return |
| 350 |
end if |
| 351 |
|
| 352 |
!> Test that -fPIC is not duplicated in debug flags |
| 353 |
second_pos = index(flags(first_pos+5:), "-fPIC") |
| 354 |
if (second_pos /= 0) then |
| 355 |
call test_failed(error, "Debug flags should not contain duplicate -fPIC, got: "//flags) |
| 356 |
return |
| 357 |
end if |
| 358 |
|
| 359 |
end subroutine test_get_default_flags_pic |
| 360 |
|
| 361 |
|
| 362 |
end module test_compiler |