Skip to content

Commit

Permalink
cmake: Move CMake API classes to common
Browse files Browse the repository at this point in the history
  • Loading branch information
mensinda committed Oct 20, 2019
1 parent 5eb3875 commit b2c89e6
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 144 deletions.
2 changes: 2 additions & 0 deletions mesonbuild/cmake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'CMakeClient',
'CMakeExecutor',
'CMakeException',
'CMakeFileAPI',
'CMakeInterpreter',
'CMakeTarget',
'CMakeTraceLine',
Expand All @@ -29,6 +30,7 @@
from .common import CMakeException
from .client import CMakeClient
from .executor import CMakeExecutor
from .fileapi import CMakeFileAPI
from .generator import parse_generator_expressions
from .interpreter import CMakeInterpreter
from .traceparser import CMakeTarget, CMakeTraceLine, CMakeTraceParser
143 changes: 1 addition & 142 deletions mesonbuild/cmake/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# This class contains the basic functionality needed to run any interpreter
# or an interpreter-based tool.

from .common import CMakeException
from .common import CMakeException, CMakeConfiguration, CMakeBuildFile
from .executor import CMakeExecutor
from ..environment import Environment
from ..mesonlib import MachineChoice
Expand Down Expand Up @@ -186,15 +186,6 @@ class ReplyCompute(ReplyBase):
def __init__(self, cookie: str):
super().__init__(cookie, 'compute')

class CMakeBuildFile:
def __init__(self, file: str, is_cmake: bool, is_temp: bool):
self.file = file
self.is_cmake = is_cmake
self.is_temp = is_temp

def __repr__(self):
return '<{}: {}; cmake={}; temp={}>'.format(self.__class__.__name__, self.file, self.is_cmake, self.is_temp)

class ReplyCMakeInputs(ReplyBase):
def __init__(self, cookie: str, cmake_root: str, src_dir: str, build_files: List[CMakeBuildFile]):
super().__init__(cookie, 'cmakeInputs')
Expand All @@ -210,138 +201,6 @@ def log(self) -> None:
for i in self.build_files:
mlog.log(str(i))

def _flags_to_list(raw: str) -> List[str]:
# Convert a raw commandline string into a list of strings
res = []
curr = ''
escape = False
in_string = False
for i in raw:
if escape:
# If the current char is not a quote, the '\' is probably important
if i not in ['"', "'"]:
curr += '\\'
curr += i
escape = False
elif i == '\\':
escape = True
elif i in ['"', "'"]:
in_string = not in_string
elif i in [' ', '\n']:
if in_string:
curr += i
else:
res += [curr]
curr = ''
else:
curr += i
res += [curr]
res = list(filter(lambda x: len(x) > 0, res))
return res

class CMakeFileGroup:
def __init__(self, data: dict):
self.defines = data.get('defines', '')
self.flags = _flags_to_list(data.get('compileFlags', ''))
self.includes = data.get('includePath', [])
self.is_generated = data.get('isGenerated', False)
self.language = data.get('language', 'C')
self.sources = data.get('sources', [])

# Fix the include directories
tmp = []
for i in self.includes:
if isinstance(i, dict) and 'path' in i:
tmp += [i['path']]
elif isinstance(i, str):
tmp += [i]
self.includes = tmp

def log(self) -> None:
mlog.log('flags =', mlog.bold(', '.join(self.flags)))
mlog.log('defines =', mlog.bold(', '.join(self.defines)))
mlog.log('includes =', mlog.bold(', '.join(self.includes)))
mlog.log('is_generated =', mlog.bold('true' if self.is_generated else 'false'))
mlog.log('language =', mlog.bold(self.language))
mlog.log('sources:')
for i in self.sources:
with mlog.nested():
mlog.log(i)

class CMakeTarget:
def __init__(self, data: dict):
self.artifacts = data.get('artifacts', [])
self.src_dir = data.get('sourceDirectory', '')
self.build_dir = data.get('buildDirectory', '')
self.name = data.get('name', '')
self.full_name = data.get('fullName', '')
self.install = data.get('hasInstallRule', False)
self.install_paths = list(set(data.get('installPaths', [])))
self.link_lang = data.get('linkerLanguage', '')
self.link_libraries = _flags_to_list(data.get('linkLibraries', ''))
self.link_flags = _flags_to_list(data.get('linkFlags', ''))
self.link_lang_flags = _flags_to_list(data.get('linkLanguageFlags', ''))
self.link_path = data.get('linkPath', '')
self.type = data.get('type', 'EXECUTABLE')
self.is_generator_provided = data.get('isGeneratorProvided', False)
self.files = []

for i in data.get('fileGroups', []):
self.files += [CMakeFileGroup(i)]

def log(self) -> None:
mlog.log('artifacts =', mlog.bold(', '.join(self.artifacts)))
mlog.log('src_dir =', mlog.bold(self.src_dir))
mlog.log('build_dir =', mlog.bold(self.build_dir))
mlog.log('name =', mlog.bold(self.name))
mlog.log('full_name =', mlog.bold(self.full_name))
mlog.log('install =', mlog.bold('true' if self.install else 'false'))
mlog.log('install_paths =', mlog.bold(', '.join(self.install_paths)))
mlog.log('link_lang =', mlog.bold(self.link_lang))
mlog.log('link_libraries =', mlog.bold(', '.join(self.link_libraries)))
mlog.log('link_flags =', mlog.bold(', '.join(self.link_flags)))
mlog.log('link_lang_flags =', mlog.bold(', '.join(self.link_lang_flags)))
mlog.log('link_path =', mlog.bold(self.link_path))
mlog.log('type =', mlog.bold(self.type))
mlog.log('is_generator_provided =', mlog.bold('true' if self.is_generator_provided else 'false'))
for idx, i in enumerate(self.files):
mlog.log('Files {}:'.format(idx))
with mlog.nested():
i.log()

class CMakeProject:
def __init__(self, data: dict):
self.src_dir = data.get('sourceDirectory', '')
self.build_dir = data.get('buildDirectory', '')
self.name = data.get('name', '')
self.targets = []

for i in data.get('targets', []):
self.targets += [CMakeTarget(i)]

def log(self) -> None:
mlog.log('src_dir =', mlog.bold(self.src_dir))
mlog.log('build_dir =', mlog.bold(self.build_dir))
mlog.log('name =', mlog.bold(self.name))
for idx, i in enumerate(self.targets):
mlog.log('Target {}:'.format(idx))
with mlog.nested():
i.log()

class CMakeConfiguration:
def __init__(self, data: dict):
self.name = data.get('name', '')
self.projects = []
for i in data.get('projects', []):
self.projects += [CMakeProject(i)]

def log(self) -> None:
mlog.log('name =', mlog.bold(self.name))
for idx, i in enumerate(self.projects):
mlog.log('Project {}:'.format(idx))
with mlog.nested():
i.log()

class ReplyCodeModel(ReplyBase):
def __init__(self, data: dict):
super().__init__(data['cookie'], 'codemodel')
Expand Down
143 changes: 143 additions & 0 deletions mesonbuild/cmake/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,149 @@
# or an interpreter-based tool.

from ..mesonlib import MesonException
from .. import mlog
from typing import List

class CMakeException(MesonException):
pass

class CMakeBuildFile:
def __init__(self, file: str, is_cmake: bool, is_temp: bool):
self.file = file
self.is_cmake = is_cmake
self.is_temp = is_temp

def __repr__(self):
return '<{}: {}; cmake={}; temp={}>'.format(self.__class__.__name__, self.file, self.is_cmake, self.is_temp)

def _flags_to_list(raw: str) -> List[str]:
# Convert a raw commandline string into a list of strings
res = []
curr = ''
escape = False
in_string = False
for i in raw:
if escape:
# If the current char is not a quote, the '\' is probably important
if i not in ['"', "'"]:
curr += '\\'
curr += i
escape = False
elif i == '\\':
escape = True
elif i in ['"', "'"]:
in_string = not in_string
elif i in [' ', '\n']:
if in_string:
curr += i
else:
res += [curr]
curr = ''
else:
curr += i
res += [curr]
res = list(filter(lambda x: len(x) > 0, res))
return res

class CMakeFileGroup:
def __init__(self, data: dict):
self.defines = data.get('defines', '')
self.flags = _flags_to_list(data.get('compileFlags', ''))
self.includes = data.get('includePath', [])
self.is_generated = data.get('isGenerated', False)
self.language = data.get('language', 'C')
self.sources = data.get('sources', [])

# Fix the include directories
tmp = []
for i in self.includes:
if isinstance(i, dict) and 'path' in i:
tmp += [i['path']]
elif isinstance(i, str):
tmp += [i]
self.includes = tmp

def log(self) -> None:
mlog.log('flags =', mlog.bold(', '.join(self.flags)))
mlog.log('defines =', mlog.bold(', '.join(self.defines)))
mlog.log('includes =', mlog.bold(', '.join(self.includes)))
mlog.log('is_generated =', mlog.bold('true' if self.is_generated else 'false'))
mlog.log('language =', mlog.bold(self.language))
mlog.log('sources:')
for i in self.sources:
with mlog.nested():
mlog.log(i)

class CMakeTarget:
def __init__(self, data: dict):
self.artifacts = data.get('artifacts', [])
self.src_dir = data.get('sourceDirectory', '')
self.build_dir = data.get('buildDirectory', '')
self.name = data.get('name', '')
self.full_name = data.get('fullName', '')
self.install = data.get('hasInstallRule', False)
self.install_paths = list(set(data.get('installPaths', [])))
self.link_lang = data.get('linkerLanguage', '')
self.link_libraries = _flags_to_list(data.get('linkLibraries', ''))
self.link_flags = _flags_to_list(data.get('linkFlags', ''))
self.link_lang_flags = _flags_to_list(data.get('linkLanguageFlags', ''))
self.link_path = data.get('linkPath', '')
self.type = data.get('type', 'EXECUTABLE')
self.is_generator_provided = data.get('isGeneratorProvided', False)
self.files = []

for i in data.get('fileGroups', []):
self.files += [CMakeFileGroup(i)]

def log(self) -> None:
mlog.log('artifacts =', mlog.bold(', '.join(self.artifacts)))
mlog.log('src_dir =', mlog.bold(self.src_dir))
mlog.log('build_dir =', mlog.bold(self.build_dir))
mlog.log('name =', mlog.bold(self.name))
mlog.log('full_name =', mlog.bold(self.full_name))
mlog.log('install =', mlog.bold('true' if self.install else 'false'))
mlog.log('install_paths =', mlog.bold(', '.join(self.install_paths)))
mlog.log('link_lang =', mlog.bold(self.link_lang))
mlog.log('link_libraries =', mlog.bold(', '.join(self.link_libraries)))
mlog.log('link_flags =', mlog.bold(', '.join(self.link_flags)))
mlog.log('link_lang_flags =', mlog.bold(', '.join(self.link_lang_flags)))
mlog.log('link_path =', mlog.bold(self.link_path))
mlog.log('type =', mlog.bold(self.type))
mlog.log('is_generator_provided =', mlog.bold('true' if self.is_generator_provided else 'false'))
for idx, i in enumerate(self.files):
mlog.log('Files {}:'.format(idx))
with mlog.nested():
i.log()

class CMakeProject:
def __init__(self, data: dict):
self.src_dir = data.get('sourceDirectory', '')
self.build_dir = data.get('buildDirectory', '')
self.name = data.get('name', '')
self.targets = []

for i in data.get('targets', []):
self.targets += [CMakeTarget(i)]

def log(self) -> None:
mlog.log('src_dir =', mlog.bold(self.src_dir))
mlog.log('build_dir =', mlog.bold(self.build_dir))
mlog.log('name =', mlog.bold(self.name))
for idx, i in enumerate(self.targets):
mlog.log('Target {}:'.format(idx))
with mlog.nested():
i.log()

class CMakeConfiguration:
def __init__(self, data: dict):
self.name = data.get('name', '')
self.projects = []
for i in data.get('projects', []):
self.projects += [CMakeProject(i)]

def log(self) -> None:
mlog.log('name =', mlog.bold(self.name))
for idx, i in enumerate(self.projects):
mlog.log('Project {}:'.format(idx))
with mlog.nested():
i.log()
4 changes: 2 additions & 2 deletions mesonbuild/cmake/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# This class contains the basic functionality needed to run any interpreter
# or an interpreter-based tool.

from .common import CMakeException
from .client import CMakeClient, RequestCMakeInputs, RequestConfigure, RequestCompute, RequestCodeModel, CMakeTarget
from .common import CMakeException, CMakeTarget
from .client import CMakeClient, RequestCMakeInputs, RequestConfigure, RequestCompute, RequestCodeModel
from .executor import CMakeExecutor
from .traceparser import CMakeTraceParser, CMakeGeneratorTarget
from .. import mlog
Expand Down

0 comments on commit b2c89e6

Please sign in to comment.