| 1 | #!/usr/bin/env python3.7 |
| 2 | """Compiles the base source for output speed and size tests using different optimization levels""" |
| 3 |
|
| 4 | import subprocess |
| 5 | import os |
| 6 |
|
| 7 | PROJECT_PATH = os.path.abspath( |
| 8 | os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") |
| 9 | ) |
| 10 | HEADER_PATH = os.path.join(PROJECT_PATH, "src/c") |
| 11 | TEST_FILE = os.path.join(PROJECT_PATH, "tests/base_header.c") |
| 12 | OUT_FILE = os.path.join(PROJECT_PATH, "bin/performance") |
| 13 |
|
| 14 | CC = "gcc" |
| 15 | CFLAGS = ["-pedantic", "-Wall", "-Wextra", "-Werror"] |
| 16 | COUTFLAG = "-o" |
| 17 | COPTIMIZATIONS = ["-O0", "-O1", "-O2", "-O3", "-Os"] |
| 18 | CINCLUDES = ["-I", HEADER_PATH] |
| 19 |
|
| 20 |
|
| 21 | def gcc_compile(file_name: str, optimization_level: str = "-O0"): |
| 22 | """Uses gcc subprocess to compile at a set optimization level""" |
| 23 | process_command = ( |
| 24 | [CC] + CFLAGS + CINCLUDES + [COUTFLAG, file_name, TEST_FILE, optimization_level] |
| 25 | ) |
| 26 | process = subprocess.Popen(process_command) |
| 27 | process.wait() # wait for execution |
| 28 | if process.returncode: |
| 29 | raise Exception(f"gcc_compile: error in compilation") |
| 30 |
|
| 31 |
|
| 32 | def get_size(file_name: str): |
| 33 | """Uses os.stat to get the file size in bytes of a specified file""" |
| 34 | return os.stat(file_name).st_size |
| 35 |
|
| 36 |
|
| 37 | if __name__ == "__main__": |
| 38 | for optimization in COPTIMIZATIONS: |
| 39 | out_file = f"{OUT_FILE}{optimization}" |
| 40 | gcc_compile(out_file, optimization) |
| 41 | print(f"{out_file}: {get_size(out_file)} bytes") |
| 42 |
|