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
/poetry.lock
/test.py

View File

@ -1,62 +1,63 @@
from typing import Self
from .tools import *
from .values import *
class cmake:
__write_stack:"list[str]" = []
__file_name = ""
def __init__(self,version_min:"str",file_name:"str"="CMakeLists.txt") -> None:
self.__file_name = file_name
self.__write_stack.append(f"cmake_minimum_required(VERSION {version_min})")
__write_stack__:List[str] = []
__file_name__:str
def __init__(self,version_min:str,file_name:str="CMakeLists.txt") -> None:
self.__file_name__ = file_name
self.__write_stack__.append(f"cmake_minimum_required(VERSION {version_min})")
def write(self)->None:
with open(file=self.__file_name,mode="+w",encoding="utf8") as f:
for i in self.__write_stack:
with open(file=self.__file_name__,mode="+w",encoding="utf8") as f:
for i in self.__write_stack__:
f.write(i)
f.write("\n")
def project(self,name:"str"="default",version:"str"="1.0",description:"str"="a default project",languages:"str"="CXX")->"cmake":
self.__write_stack.append(f"project({name} VERSION {version} DESCRIPTION \"{description}\" LANGUAGES {languages})")
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})")
return self
def add_executable(self,name:"str"=var(project_name),*source:"str")->"cmake":
self.__write_stack.append(f"add_executable({name} {args_to_str(source)})")
def add_executable(self,name:str=var(project_name),*source:str)->Self:
self.__write_stack__.append(f"add_executable({name} {args_to_str(source)})")
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:
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
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:
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
def set(self,name:"str",value:"str")->"cmake":
self.__write_stack.append(f"set({name} {value})")
def set(self,name:str,value:str)->Self:
self.__write_stack__.append(f"set({name} {value})")
return self
def target_link_libraries(self,lib_name:"str",name:"str"=var(project_name),mode:"str"=public)->"cmake":
self.__write_stack.append(f"target_link_libraries({name} {mode} {lib_name})")
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})")
return self
def target_link_directories(self,name:"str"=var(project_name),mode:"str"=public,*directories:"str")->"cmake":
self.__write_stack.append(f"target_link_directories({name} {mode} {args_to_str(directories)})")
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)})")
return self
def just_add(self,sentence:"str")->"cmake":
self.__write_stack.append(sentence)
def just_add(self,sentence:str)->Self:
self.__write_stack__.append(sentence)
return self
def file(self,new_var_name:"str",mode:"str"=glob_recurse,*search_str:"str")->"cmake":
self.__write_stack.append(f"file({mode} {new_var_name} {args_to_str(search_str)})")
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)})")
return self
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

View File

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