This commit is contained in:
ZtRXR 2024-04-21 23:08:33 +08:00
parent b32c5cb81a
commit 43c515a7a4
3 changed files with 33 additions and 29 deletions

1
.gitignore vendored
View File

@ -12,3 +12,4 @@ __pycache__/
/build /build
/poetry.lock /poetry.lock
/test.py

View File

@ -1,62 +1,63 @@
from typing import Self
from .tools import * from .tools import *
from .values import * from .values import *
class cmake: class cmake:
__write_stack:"list[str]" = [] __write_stack__:List[str] = []
__file_name = "" __file_name__:str
def __init__(self,version_min:"str",file_name:"str"="CMakeLists.txt") -> None: def __init__(self,version_min:str,file_name:str="CMakeLists.txt") -> None:
self.__file_name = file_name self.__file_name__ = file_name
self.__write_stack.append(f"cmake_minimum_required(VERSION {version_min})") self.__write_stack__.append(f"cmake_minimum_required(VERSION {version_min})")
def write(self)->None: def write(self)->None:
with open(file=self.__file_name,mode="+w",encoding="utf8") as f: with open(file=self.__file_name__,mode="+w",encoding="utf8") as f:
for i in self.__write_stack: for i in self.__write_stack__:
f.write(i) f.write(i)
f.write("\n") f.write("\n")
def project(self,name:"str"="default",version:"str"="1.0",description:"str"="a default project",languages:"str"="CXX")->"cmake": def project(self,name:str="default",version:str="1.0",description:str="a default project",languages:str="CXX")->Self:
self.__write_stack.append(f"project({name} VERSION {version} DESCRIPTION \"{description}\" LANGUAGES {languages})") self.__write_stack__.append(f"project({name} VERSION {version} DESCRIPTION \"{description}\" LANGUAGES {languages})")
return self return self
def add_executable(self,name:"str"=var(project_name),*source:"str")->"cmake": def add_executable(self,name:str=var(project_name),*source:str)->Self:
self.__write_stack.append(f"add_executable({name} {args_to_str(source)})") self.__write_stack__.append(f"add_executable({name} {args_to_str(source)})")
return self return self
def add_library(self,name:"str"=var(project_name),mode:"str"=static,*source:"str")->"cmake": def add_library(self,name:str=var(project_name),mode:str=static,*source:str)->Self:
""" """
ARGS: ARGS:
mode: mode can be STATIC , SHARED or MODULE mode: mode can be STATIC , SHARED or MODULE
""" """
self.__write_stack.append(f"add_library({name} {mode} {args_to_str(source)})") self.__write_stack__.append(f"add_library({name} {mode} {args_to_str(source)})")
return self return self
def target_include_directories(self,for_project:"str"=var(project_name),mode:"str"=public,*directories:"str")->"cmake": def target_include_directories(self,for_project:str=var(project_name),mode:str=public,*directories:str)->Self:
""" """
ARGS: ARGS:
mode : mode can be PUBLIC PRIVATE INTERFACE mode : mode can be PUBLIC PRIVATE INTERFACE
""" """
self.__write_stack.append(f"target_include_directories({for_project} {mode} {args_to_str(directories)})") self.__write_stack__.append(f"target_include_directories({for_project} {mode} {args_to_str(directories)})")
return self return self
def set(self,name:"str",value:"str")->"cmake": def set(self,name:str,value:str)->Self:
self.__write_stack.append(f"set({name} {value})") self.__write_stack__.append(f"set({name} {value})")
return self return self
def target_link_libraries(self,lib_name:"str",name:"str"=var(project_name),mode:"str"=public)->"cmake": def target_link_libraries(self,lib_name:str,name:str=var(project_name),mode:str=public)->Self:
self.__write_stack.append(f"target_link_libraries({name} {mode} {lib_name})") self.__write_stack__.append(f"target_link_libraries({name} {mode} {lib_name})")
return self return self
def target_link_directories(self,name:"str"=var(project_name),mode:"str"=public,*directories:"str")->"cmake": def target_link_directories(self,name:str=var(project_name),mode:str=public,*directories:str)->Self:
self.__write_stack.append(f"target_link_directories({name} {mode} {args_to_str(directories)})") self.__write_stack__.append(f"target_link_directories({name} {mode} {args_to_str(directories)})")
return self return self
def just_add(self,sentence:"str")->"cmake": def just_add(self,sentence:str)->Self:
self.__write_stack.append(sentence) self.__write_stack__.append(sentence)
return self return self
def file(self,new_var_name:"str",mode:"str"=glob_recurse,*search_str:"str")->"cmake": def file(self,new_var_name:str,mode:str=glob_recurse,*search_str:str)->Self:
self.__write_stack.append(f"file({mode} {new_var_name} {args_to_str(search_str)})") self.__write_stack__.append(f"file({mode} {new_var_name} {args_to_str(search_str)})")
return self return self
def add_subdirectory(self,path:str)->'cmake': def add_subdirectory(self,path:str)->'cmake':
self.__write_stack.append(f"add_subdirectory({path})") self.__write_stack__.append(f"add_subdirectory({path})")
return self return self

View File

@ -1,8 +1,10 @@
from typing import * from typing import List, Tuple, Union
def var(name:str)->str: def var(name:str)->str:
return f"${{{name}}}" return f"${{{name}}}"
def args_to_str(args:list[str])->str: def args_to_str(args:Union[List[str],Tuple[str,...]])->str:
vars_str = "" vars_str = ""
for i in args: for i in args:
vars_str=f"{vars_str} {i}" vars_str=f"{vars_str} {i}"