Subversion Repositories Kolibri OS

Rev

Rev 9387 | Rev 9412 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #!/usr/bin/python3
  2. # Copyright 2021 Magomed Kostoev
  3. # Published under MIT License
  4.  
  5. import os
  6. import sys
  7. import urllib
  8. from importlib.machinery import SourceFileLoader
  9. from shutil import which
  10. import timeit
  11. import urllib.request
  12. import subprocess
  13. from threading import Thread
  14. import filecmp
  15.  
  16. sys.path.append('test')
  17. import common
  18.  
  19. use_umka = False
  20.  
  21. def log(s, end = "\n"):
  22.     print(s, end = end, flush = True)
  23.  
  24. def execute(s, mute = False):
  25.     mute = ">/dev/null" if mute else ""
  26.     code = os.system(f"{s}{mute}")
  27.     if code:
  28.         print(f"Command returned {code}: \"{s}\"")
  29.         exit(-1)
  30.  
  31. def stage(name, command, mute = False):
  32.     print(f"{name}... ", end = "")
  33.     execute(command, mute = mute)
  34.     print("Done.")
  35.  
  36. def download(link, path):
  37.     log(f"Downloading {path}... ", end = "")
  38.     urllib.request.urlretrieve(link, path)
  39.     log("Done.")
  40.  
  41. def tool_exists(name):
  42.     assert(type(name) == str)
  43.     return which(name) != None
  44.  
  45. def check_tools(tools):
  46.     assert(type(tools) == tuple)
  47.     for name_package_pair in tools:
  48.         assert(type(name_package_pair) == tuple)
  49.         assert(len(name_package_pair) == 2)
  50.         assert(type(name_package_pair[0]) == str)
  51.         assert(type(name_package_pair[1]) == str)
  52.    
  53.     not_exists = []
  54.     for name, package in tools:
  55.         if not tool_exists(name):
  56.             not_exists.append((name, package))
  57.     if len(not_exists) != 0:
  58.         log("Sorry, I can't find some tools:")
  59.  
  60.         header_name = "Name"
  61.         header_package = "Package (probably)"
  62.  
  63.         max_name_len = len(header_name)
  64.         max_package_name_len = len(header_package)
  65.         for name, package in not_exists:
  66.             if len(package) > max_package_name_len:
  67.                 max_package_name_len = len(package)
  68.             if len(name) > max_name_len:
  69.                 max_name_len = len(name)
  70.  
  71.         def draw_row(name, package):
  72.             log(f" | {name.ljust(max_name_len)} | {package.ljust(max_package_name_len)} |")
  73.  
  74.         def draw_line():
  75.             log(f" +-{'-' * max_name_len}-+-{'-' * max_package_name_len}-+")
  76.  
  77.         draw_line()
  78.         draw_row(header_name, header_package)
  79.         draw_line()
  80.         for name, package in not_exists:
  81.             draw_row(name, package)
  82.         draw_line()
  83.         exit()
  84.  
  85. def prepare_test_img():
  86.     # TODO: Always recompile the kernel (after build system is done?)
  87.     # Get IMG
  88.     if not os.path.exists("kolibri_test.img"):
  89.         if len(sys.argv) == 1:
  90.             download("http://builds.kolibrios.org/eng/data/data/kolibri.img", "kolibri_test.img")
  91.         else:
  92.             builds_eng = sys.argv[1]
  93.             execute(f"cp {builds_eng}/data/data/kolibri.img kolibri_test.img")
  94.    
  95.     # Open the IMG
  96.     with open("kolibri_test.img", "rb") as img:
  97.         img_data = img.read()
  98.     img = common.Floppy(img_data)
  99.  
  100.     # Remove unuseful folders
  101.     img.delete_path("GAMES")
  102.     img.delete_path("DEMOS")
  103.     img.delete_path("3D")
  104.    
  105.     # Get test kernel
  106.     if not os.path.exists("kernel.mnt.pretest"):
  107.         if len(sys.argv) == 1:
  108.             with open("lang.inc", "w") as lang_inc:
  109.                 lang_inc.write("lang fix en\n")
  110.             execute("fasm bootbios.asm bootbios.bin.pretest -dpretest_build=1")
  111.             execute("fasm -m 65536 kernel.asm kernel.mnt.pretest -dpretest_build=1 -ddebug_com_base=0xe9")
  112.         else:
  113.             builds_eng = sys.argv[1]
  114.             execute(f"cp {builds_eng}/data/kernel/trunk/kernel.mnt.pretest kernel.mnt.pretest", mute = True)
  115.    
  116.     # Put the kernel into IMG
  117.     with open("kernel.mnt.pretest", "rb") as kernel_mnt_pretest:
  118.         kernel_mnt_pretest_data = kernel_mnt_pretest.read()
  119.     img.add_file_path("KERNEL.MNT", kernel_mnt_pretest_data)
  120.     img.save("kolibri_test.img")
  121.  
  122. def collect_tests():
  123.     tests = []
  124.  
  125.     # Collect tests from test folder (not recursively yet)
  126.     for test_folder in os.listdir("test"):
  127.         test_folder_path = f"test/{test_folder}"
  128.         test_file = f"{test_folder_path}/test.py"
  129.  
  130.         if not os.path.isdir(test_folder_path):
  131.             continue
  132.  
  133.         if os.path.exists(test_file):
  134.             tests.append(test_folder_path)
  135.     return tests
  136.  
  137. def run_tests_serially_thread(test, root_dir):
  138.     test_number = 1
  139.     for test in tests:
  140.         test_dir = f"{root_dir}/{test}"
  141.    
  142.         print(f"[{test_number}/{len(tests)}] {test}... ", end = "", flush=True)
  143.         start = timeit.default_timer()
  144.         try:
  145.             SourceFileLoader("test", f"{test_dir}/test.py").load_module().run(root_dir, test_dir)
  146.         except common.TestTimeoutException:
  147.             result = "TIMEOUT"
  148.         except common.TestFailureException:
  149.             result = "FAILURE"
  150.         else:
  151.             result = "SUCCESS"
  152.         finish = timeit.default_timer()
  153.         print(f"{result} ({finish - start:.2f} seconds)")
  154.    
  155.         test_number += 1
  156.  
  157. def run_tests_serially(tests, root_dir):
  158.     thread = Thread(target = run_tests_serially_thread, args = (tests, root_dir))
  159.     thread.start()
  160.     return thread
  161.  
  162. def build_umka_asm(object_output_dir):
  163.     umka_o = f"{object_output_dir}/umka.o"
  164.     kolibrios_folder = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  165.     env = os.environ
  166.     env["INCLUDE"] = ""
  167.     env["INCLUDE"] += f"{kolibrios_folder}/kernel/trunk;"
  168.     env["INCLUDE"] += f"{kolibrios_folder}/programs/develop/libraries/libcrash/hash"
  169.     stdout = subprocess.check_output(f"fasm -dUEFI=1 -dextended_primary_loader=1 -dUMKA=1 umka/umka.asm umka/build/umka.o -s umka/build/umka.fas -m 2000000", shell = True, env = env)
  170.     print(stdout)
  171.     return umka_o
  172.  
  173. def cc(src, obj, include_path):
  174.     command = "clang "
  175.     command += "-Wno-everything -std=c11 -g -O0 -fno-pie -m32 -c "
  176.     command += "-D_FILE_OFFSET_BITS=64 -DNDEBUG -masm=intel -D_POSIX_C_SOURCE=200809L "
  177.     command += f"-I {include_path} {src} -o {obj}"
  178.     if os.system(command) != 0:
  179.         exit()
  180.  
  181. def link(objects):
  182.     command = "clang "
  183.     command += "-Wno-everything -no-pie -m32 -o umka_shell -static -T umka/umka.ld "
  184.     command += " ".join(objects)
  185.     if os.system(command) != 0:
  186.         exit()
  187.  
  188. def build_umka():
  189.     if not use_umka:
  190.         return
  191.  
  192.     os.makedirs("umka/build", exist_ok = True)
  193.  
  194.     c_sources = [
  195.         "umka_shell.c",
  196.         "shell.c",
  197.         "trace.c",
  198.         "trace_lbr.c",
  199.         "vdisk.c",
  200.         "vnet.c",
  201.         "lodepng.c",
  202.         "linux/pci.c",
  203.         "linux/thread.c",
  204.         "util.c",
  205.     ]
  206.  
  207.     src_obj_pairs = [ (f"umka/{source}", f"umka/{source}.o") for source in c_sources ]
  208.  
  209.     for src, obj in src_obj_pairs:
  210.         cc(src, obj, "umka/linux")
  211.  
  212.     umka_o = build_umka_asm("umka/build")
  213.  
  214.     objects = [ obj for src, obj in src_obj_pairs ] + [ umka_o ]
  215.     link(objects)
  216.  
  217.     os.chdir("umka/test")
  218.     for test in [ t for t in os.listdir(".") if t.endswith(".t") ]:
  219.         out_log = f"{test[:-2]}.out.log"
  220.         ref_log = f"{test[:-2]}.ref.log"
  221.         cmd_umka = f"../../umka_shell < {test} > {out_log}"
  222.         print(cmd_umka)
  223.         os.system(cmd_umka)
  224.         cmd_cmp = f"cmp {out_log} {ref_log}"
  225.         print(cmd_cmp)
  226.         if os.system(cmd_cmp) != 0:
  227.             print("FAILURE")
  228.             exit()
  229.     os.chdir("../../")
  230.     print("SUCCESS")
  231.     exit()
  232.  
  233. if __name__ == "__main__":
  234.     root_dir = os.getcwd()
  235.  
  236.     # Check available tools
  237.     tools = (("qemu-system-i386", "qemu-system-x86"),
  238.              ("fasm", "fasm"))
  239.     check_tools(tools)
  240.    
  241.     prepare_test_img()
  242.     build_umka()
  243.     tests = collect_tests()
  244.     serial_executor_thread = run_tests_serially(tests, root_dir)
  245.     serial_executor_thread.join()
  246.  
  247.