1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2025 Northeastern University 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19from dataclasses import dataclass, field, asdict 20from typing import List, Dict, Any 21 22 23@dataclass 24class Target: 25 target_name: str = "" 26 type: str = "" 27 args: List[str] = field(default_factory=list) 28 deps: List[str] = field(default_factory=list) 29 depfile: str = "" 30 inputs: List[str] = field(default_factory=list) 31 metadata: Dict[str, Any] = field(default_factory=dict) 32 outputs: List[str] = field(default_factory=list) 33 public: str = "" 34 script: str = "" 35 testonly: bool = False 36 toolchain: str = "" 37 visibility: List[str] = field(default_factory=list) 38 source_outputs: Dict[str, Any] = field(default_factory=dict) 39 sources: List[str] = field(default_factory=list) 40 libs: List[str] = field(default_factory=list) 41 ldflags: List[str] = field(default_factory=list) 42 43 @classmethod 44 def from_dict(cls, target_name: str, d: Dict[str, Any]) -> "Target": 45 allowed = {f.name for f in cls.__dataclass_fields__.values()} 46 return cls(target_name=target_name, 47 **{k: v for k, v in d.items() if k in allowed}) 48 49 def to_dict(self) -> Dict[str, Any]: 50 return asdict(self) 51 52 def to_dict_without_name(self) -> Dict[str, Any]: 53 data = asdict(self) 54 data.pop('target_name', None) 55 return data 56