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

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

View File

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