| 1 | // vtest retry: 3 |
| 2 | import os |
| 3 | import time |
| 4 | |
| 5 | const vexe = @VEXE |
| 6 | const tfolder = os.join_path(os.vtmp_dir(), 'custom_compile') |
| 7 | |
| 8 | fn testsuite_begin() { |
| 9 | os.mkdir_all(tfolder) or {} |
| 10 | } |
| 11 | |
| 12 | fn testsuite_end() { |
| 13 | os.rmdir_all(tfolder) or {} |
| 14 | } |
| 15 | |
| 16 | fn test_cflags() { |
| 17 | os.chdir(os.real_path(@VMODROOT)) or {} |
| 18 | mut debug_arg := '-g3 -O0' |
| 19 | mut optimised_arg := '-O1' |
| 20 | $if msvc { |
| 21 | debug_arg = '/MDd /D_DEBUG' |
| 22 | optimised_arg = '/O1' |
| 23 | } |
| 24 | |
| 25 | println('> test whether -cflags is passed to the backend C compiler') |
| 26 | fail := custom_compile('failing.exe', 'NONSENSE_OPTION') |
| 27 | assert fail.compilation.exit_code != 0 |
| 28 | println('> NONSENSE_OPTION failed the C build, OK') |
| 29 | |
| 30 | dbg := custom_compile('debug_hw.exe', debug_arg) |
| 31 | assert dbg.compilation.exit_code == 0 |
| 32 | assert dbg.file_size > 0 |
| 33 | |
| 34 | opt := custom_compile('optimised_hw.exe', optimised_arg) |
| 35 | assert opt.compilation.exit_code == 0 |
| 36 | assert opt.file_size > 0 |
| 37 | |
| 38 | $if !tinyc { |
| 39 | // tcc does almost no optimisations, so the differences are very insignificant |
| 40 | // optimised_file_size should be smaller in general, but not on the Ubuntu CI for some reason :-| |
| 41 | // assert opt.file_size != dbg.file_size |
| 42 | // assert optimised_delta >= debug_delta // this is not reliable on the CIs :-| |
| 43 | } |
| 44 | os.rm(opt.exe) or {} |
| 45 | os.rm(dbg.exe) or {} |
| 46 | } |
| 47 | |
| 48 | fn custom_compile(fname string, cflags_options string) Results { |
| 49 | mut res := Results{} |
| 50 | res.exe = os.join_path(tfolder, fname) |
| 51 | res.sw = time.new_stopwatch() |
| 52 | res.compilation = |
| 53 | os.execute('${os.quoted_path(vexe)} -cflags "${cflags_options}" -o ${os.quoted_path(res.exe)} examples/hello_world.v') |
| 54 | res.delta = res.sw.elapsed().milliseconds() |
| 55 | res.file_size = os.file_size(res.exe) |
| 56 | println('> ${fname} build took: ${res.delta} ms with "${cflags_options}", file size: ${res.file_size}') |
| 57 | return res |
| 58 | } |
| 59 | |
| 60 | struct Results { |
| 61 | mut: |
| 62 | exe string |
| 63 | sw time.StopWatch |
| 64 | compilation os.Result |
| 65 | delta i64 |
| 66 | file_size u64 |
| 67 | } |
| 68 | |