Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Change kbuild to produce targets locally (#641)
Browse files Browse the repository at this point in the history
Fixes #586

The tool now produces the distribution under a directory `kdist` in the
project folder by default, or at a location specified by the user (using
`--output`).

Further changes:
* Since there is no global repository, hashes have also been dropped
from the distribution hierarchy
* Command `kbuild clean` has been removed

---------

Co-authored-by: devops <devops@runtimeverification.com>
  • Loading branch information
tothtamas28 and devops authored Sep 12, 2023
1 parent 16b75f5 commit d19598b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.438
0.1.439
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "pyk"
version = "0.1.438"
version = "0.1.439"
description = ""
authors = [
"Runtime Verification, Inc. <contact@runtimeverification.com>",
Expand Down
50 changes: 30 additions & 20 deletions src/pyk/kbuild/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import shutil
import sys
from argparse import ArgumentParser
from pathlib import Path
from typing import Any

from ..cli.utils import check_dir_path, dir_path
from .config import KBUILD_DIR, PROJECT_FILE_NAME
from .config import DIST_DIR_NAME, PROJECT_FILE_NAME
from .kbuild import KBuild
from .package import Package
from .utils import find_file_upwards
Expand All @@ -15,9 +14,6 @@ def main() -> None:
args = vars(_argument_parser().parse_args())
command = args['command']

if command == 'clean':
return do_clean(**args)

if command == 'kompile':
return do_kompile(**args)

Expand All @@ -35,38 +31,52 @@ def _argument_parser() -> ArgumentParser:

command_parser = parser.add_subparsers(dest='command', metavar='COMMAND', required=True)

command_parser.add_parser('clean', help='clean build cache')

kompile_parser = command_parser.add_parser('kompile', help='kompile target')
kompile_parser.add_argument('target_name', metavar='TARGET', help='target to build')
kompile_parser.add_argument(
'-o',
'--output',
dest='output_dir',
metavar='OUTPUT',
type=Path,
help='output directory',
)

which_parser = command_parser.add_parser('which', help='print definition directory for target')
which_parser.add_argument('target_name', metavar='TARGET', help='target to print definition directory for')
which_parser.add_argument(
'-o',
'--output',
dest='output_dir',
metavar='OUTPUT',
type=Path,
help='output directory',
)

return parser


def _package(start_dir: Path) -> Package:
project_file = find_file_upwards(PROJECT_FILE_NAME, start_dir)
return Package.create(project_file)
def _project_file(start_dir: Path) -> Path:
return find_file_upwards(PROJECT_FILE_NAME, start_dir)


def do_clean(**kwargs: Any) -> None:
shutil.rmtree(KBUILD_DIR, ignore_errors=True)
def do_kompile(start_dir: Path, target_name: str, output_dir: Path | None, **kwargs: Any) -> None:
project_file = _project_file(start_dir)
package = Package.create(project_file)
kdist_dir = output_dir or project_file.parent / DIST_DIR_NAME
kbuild = KBuild(kdist_dir)


def do_kompile(start_dir: Path, target_name: str, **kwargs: Any) -> None:
package = _package(start_dir)
kbuild = KBuild()
definition_dir = kbuild.kompile(package, target_name)
print(definition_dir)


def do_which(start_dir: Path, target_name: str, **kwargs: Any) -> None:
package = _package(start_dir)
kbuild = KBuild()
definition_dir = kbuild.definition_dir(package, target_name)
def do_which(start_dir: Path, target_name: str, output_dir: Path | None, **kwargs: Any) -> None:
project_file = _project_file(start_dir)
package = Package.create(project_file)
kdist_dir = output_dir or project_file.parent / DIST_DIR_NAME
kbuild = KBuild(kdist_dir)

definition_dir = kbuild.definition_dir(package, target_name)
try:
check_dir_path(definition_dir)
except ValueError as e:
Expand Down
7 changes: 1 addition & 6 deletions src/pyk/kbuild/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import os
from pathlib import Path
from typing import Final

PROJECT_FILE_NAME: Final = 'kbuild.toml'
KBUILD_DIR_NAME: Final = '.kbuild'

KBUILD_DIR_ENV: Final = os.getenv('KBUILD_DIR')
KBUILD_DIR: Final = Path(KBUILD_DIR_ENV) if KBUILD_DIR_ENV else Path.home() / KBUILD_DIR_NAME
DIST_DIR_NAME: Final = 'kdist'
22 changes: 10 additions & 12 deletions src/pyk/kbuild/kbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from ..ktool.kompile import kompile
from ..utils import single
from .config import KBUILD_DIR
from .utils import k_version, sync_files

if TYPE_CHECKING:
Expand All @@ -25,22 +24,21 @@
@final
@dataclass(frozen=True)
class KBuild:
kbuild_dir: Path
kdist_dir: Path

def __init__(self, kbuild_dir: str | Path | None = None):
kbuild_dir = kbuild_dir if kbuild_dir is not None else KBUILD_DIR
kbuild_dir = Path(kbuild_dir).resolve()
object.__setattr__(self, 'kbuild_dir', kbuild_dir)
def __init__(self, kdist_dir: str | Path):
kdist_dir = Path(kdist_dir).resolve()
object.__setattr__(self, 'kdist_dir', kdist_dir)

@cached_property
def k_version(self) -> str:
return k_version().text

def definition_dir(self, package: Package, target_name: str) -> Path:
return self.kbuild_dir / package.target_dir / self.k_version / target_name
return self.kdist_dir / package.target_dir / self.k_version / target_name

def resource_dir(self, package: Package, resource_name: str) -> Path:
return self.kbuild_dir / package.resource_dir / resource_name
return self.kdist_dir / package.resource_dir / resource_name

def resource_files(self, package: Package, resource_name: str) -> list[Path]:
return [
Expand All @@ -49,7 +47,7 @@ def resource_files(self, package: Package, resource_name: str) -> list[Path]:
]

def include_dir(self, package: Package) -> Path:
return self.kbuild_dir / package.include_dir
return self.kdist_dir / package.include_dir

def source_dir(self, package: Package) -> Path:
return self.include_dir(package) / package.name
Expand Down Expand Up @@ -81,8 +79,8 @@ def sync(self, package: Package) -> list[Path]:
return res

def kompile(self, package: Package, target_name: str) -> Path:
self.kbuild_dir.mkdir(parents=True, exist_ok=True)
with FileLock(self.kbuild_dir / '.lock'):
self.kdist_dir.mkdir(parents=True, exist_ok=True)
with FileLock(self.kdist_dir / '.lock'):
for sub_package in package.sub_packages:
self.sync(sub_package)

Expand All @@ -96,7 +94,7 @@ def kompile(self, package: Package, target_name: str) -> Path:
kompile(
output_dir=output_dir,
include_dirs=[self.include_dir(sub_package) for sub_package in package.sub_packages],
cwd=self.kbuild_dir,
cwd=self.kdist_dir,
**args,
)

Expand Down
3 changes: 1 addition & 2 deletions src/pyk/kbuild/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path
from typing import TYPE_CHECKING, final

from ..utils import hash_str
from .project import GitSource, PathSource, Project

if TYPE_CHECKING:
Expand Down Expand Up @@ -38,7 +37,7 @@ def project(self) -> Project:

@property
def path(self) -> Path:
return Path(self.name) / hash_str(self.source)[:7]
return Path(self.name)

@property
def target_dir(self) -> Path:
Expand Down

0 comments on commit d19598b

Please sign in to comment.