Skip to content

Commit

Permalink
2025-01-10T15:49:52Z
Browse files Browse the repository at this point in the history
  • Loading branch information
wrmsr committed Jan 10, 2025
1 parent 6e6dc0a commit a9ac337
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions omdev/tools/cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
//$(which true); clang++ -std=c++20 -o ${D=`mktemp -d`}/x ${0} && ${D}/x ${@:1}; R=${?}; rm -rf ${D}; exit ${R}
"""
import os
import shlex
import shutil
import subprocess
import tempfile

from omlish import check
from omlish.argparse import all as ap

from .. import magic
from ..cli import CliModule


Expand All @@ -31,21 +33,47 @@ class Cli(ap.Cli):
)
def run(self) -> int:
src_file = self.args.src_file
check.state(os.path.isfile(src_file))

#

with open(src_file) as f:
src = f.read()

src_magic = magic.find_magic( # noqa
magic.C_MAGIC_STYLE,
src.splitlines(),
file=src_file,
preparer=magic.json_magic_preparer,
)

# print(src_magic)

#

src_file_name = os.path.basename(src_file)

sh_parts: list[str] = [
'clang++',
]

if cflags := os.environ.get('CFLAGS'):
sh_parts.append(cflags) # Explicitly shell-unquoted

sh_parts.extend([
'-std=c++20',
shlex.quote(os.path.abspath(src_file)),
'-o',
shlex.quote(src_file_name),
])

#

tmp_dir = tempfile.mkdtemp()
try:
proc = subprocess.run(
[
check.non_empty_str(shutil.which('clang++')),
'-std=c++20',
os.path.abspath(src_file),
'-o',
src_file_name,
],
proc = subprocess.run( # noqa
' '.join(sh_parts),
cwd=tmp_dir,
shell=True,
check=False,
)

Expand All @@ -68,11 +96,12 @@ def run(self) -> int:

return proc.returncode

@ap.cmd(
ap.arg('src-file', nargs='+'),
)
def add_shebang(self) -> None:
print(self.args.src_file)
# @ap.cmd(
# ap.arg('src-file', nargs='+'),
# )
# def add_shebang(self) -> None:
# # //$(which true); exec om cc run "$0" "$@"
# print(self.args.src_file)


def _main() -> None:
Expand Down

0 comments on commit a9ac337

Please sign in to comment.