Обновить логику сборки и запуска проекта, добавив поддержку флагов и исправив ошибки вывода

This commit is contained in:
2026-02-11 19:54:53 +08:00
parent b328cf215d
commit 7939e710a6
3 changed files with 51 additions and 26 deletions

View File

@@ -1,6 +1,6 @@
import os
import subprocess
from dcr.config import flags
from ..config import flags, profile, c_comp
def check_dir(dir: str | None = None) -> list[str]:
@@ -10,25 +10,40 @@ def check_dir(dir: str | None = None) -> list[str]:
return os.listdir(os.getcwd() + "/" + dir)
def build(args: list[str] | str) -> int:
def build(args: list[str] | None = None) -> int:
active_profile: str = profile
if "dcr.toml" not in check_dir():
print("error: Not font dcr.toml")
print("Ошибка: не найден файл dcr.toml")
return 1
if "target" not in check_dir():
os.mkdir("./target")
if len(args) == 1 and args[0] in flags:
profile: str = args[0][2:]
else:
print("error: Unkown flags")
return 1
if len(args) == 0:
profile: str = "debug"
if args:
if len(args) >= 1 and args[0].startswith("--"):
candidate: str = args[0][2:]
if candidate in flags:
active_profile = candidate
else:
print("Ошибка: неверный флаг сборки")
return 1
else:
print("Ошибка: флаг сборки не найден")
return 1
if profile not in check_dir("target"):
os.mkdir(f"./target/{profile}")
if active_profile not in check_dir("target"):
os.mkdir(f"./target/{active_profile}")
if "main.c" in check_dir("src"):
print(f"Run build project as profiles {profile}")
subprocess.run(["gcc", "./src/main.c", "-o", f"./target/{profile}/main"])
print(f"Run build project as profiles {active_profile}")
compile_flags = flags[active_profile]
subprocess.run(
[
c_comp,
"./src/main.c",
*compile_flags,
"-o",
f"./target/{active_profile}/main",
],
check=True,
)
print("Build complite")
return 0

View File

@@ -1,7 +1,7 @@
import os
import subprocess
from dcr.config import flags
from ..config import flags, profile
from .build import build
@@ -12,19 +12,24 @@ def check_dir(dir: str | None = None) -> list[str]:
return os.listdir(os.getcwd() + "/" + dir)
def run(args: list[str]) -> int:
def run(args: list[str] | None = None) -> int:
active_profile: str = profile
if "dcr.toml" not in os.listdir(os.getcwd()):
print("error: Not fond dcr.toml")
print("Ошибка: не найден файл dcr.toml")
return 1
if len(args) == 0:
profile: str = "debug"
elif len(args) == 1 and args[0] in flags:
profile = args[0][2:]
else:
print("error: Unkown flags")
return 1
if args:
if len(args) >= 1 and args[0].startswith("--"):
candidate: str = args[0][2:]
if candidate in flags:
active_profile = candidate
else:
print("Ошибка: неверный флаг сборки")
return 1
else:
print("Ошибка: флаг сборки не найден")
return 1
build(args)
print("Run project")
return subprocess.run([f"./target/{profile}/main"]).returncode
return subprocess.run([f"./target/{active_profile}/main"]).returncode

View File

@@ -1 +1,6 @@
flags: list[str] = ["--release", "--debug"]
profile: str = "debug"
c_comp: str = "clang"
flags: dict[str, list[str]] = {
"release": ["-O3", "-DNDEBUG", "-march=native"],
"debug": ["-O0", "-g", "-Wall", "-Wextra", "-fno-omit-frame-pointer", "-DDEBUG"],
}